blob: 1e1bb4540a9cfc08e98d0285f5e2a75ca24990c8 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02005source term_util.vim
LemonBoy09371822022-04-08 15:18:45 +01006source screendump.vim
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00007import './vim9.vim' as v9
Bram Moolenaar8c64a362018-03-23 22:39:31 +01008
Bram Moolenaar1e115362019-01-09 23:01:02 +01009func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +020010 for bnr in range(1, bufnr('$'))
11 if bufloaded(bnr) && bufnr('%') != bnr
12 execute 'bd! ' . bnr
13 endif
14 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020015endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020016
Bram Moolenaar14735512016-03-26 21:00:08 +010017func Test_vim_did_enter()
18 call assert_false(v:vim_did_enter)
19
20 " This script will never reach the main loop, can't check if v:vim_did_enter
21 " becomes one.
22endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020023
Bram Moolenaar75911162020-07-21 19:44:47 +020024" Test for the CursorHold autocmd
25func Test_CursorHold_autocmd()
26 CheckRunVimInTerminal
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010027 call writefile(['one', 'two', 'three'], 'XoneTwoThree', 'D')
Bram Moolenaar75911162020-07-21 19:44:47 +020028 let before =<< trim END
29 set updatetime=10
Bram Moolenaare7cda972022-08-29 11:02:59 +010030 au CursorHold * call writefile([line('.')], 'XCHoutput', 'a')
Bram Moolenaar75911162020-07-21 19:44:47 +020031 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +010032 call writefile(before, 'XCHinit', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +010033 let buf = RunVimInTerminal('-S XCHinit XoneTwoThree', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020034 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020035 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020036 call term_sendkeys(buf, "gg")
37 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010038 call WaitForAssert({-> assert_equal(['1'], readfile('XCHoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020039 call term_sendkeys(buf, "j")
40 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010041 call WaitForAssert({-> assert_equal(['1', '2'], readfile('XCHoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020042 call term_sendkeys(buf, "j")
43 call term_wait(buf)
Bram Moolenaare7cda972022-08-29 11:02:59 +010044 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('XCHoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call StopVimInTerminal(buf)
46
Bram Moolenaare7cda972022-08-29 11:02:59 +010047 call delete('XCHoutput')
Bram Moolenaar75911162020-07-21 19:44:47 +020048endfunc
49
Bram Moolenaarc67e8922016-05-24 16:07:40 +020050if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020051
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052 func ExitInsertMode(id)
53 call feedkeys("\<Esc>")
54 endfunc
55
56 func Test_cursorhold_insert()
zeertzjq657b31f2023-04-15 21:28:02 +010057 " depends on timing
58 let g:test_is_flaky = 1
59
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020060 " Need to move the cursor.
61 call feedkeys("ggG", "xt")
62
Bram Moolenaarc67e8922016-05-24 16:07:40 +020063 let g:triggered = 0
64 au CursorHoldI * let g:triggered += 1
65 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010066 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020067 call feedkeys('a', 'x!')
Bram Moolenaar3b014be2022-11-13 17:53:46 +000068 sleep 30m
Bram Moolenaarc67e8922016-05-24 16:07:40 +020069 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010070 unlet g:triggered
71 au! CursorHoldI
72 set updatetime&
73 endfunc
74
75 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020076 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010077 " Need to move the cursor.
78 call feedkeys("ggG", "xt")
79
80 " Confirm the timer invoked in exit_cb of the job doesn't disturb
81 " CursorHoldI event.
82 let g:triggered = 0
83 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020084 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010085 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020086 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010087 call feedkeys('a', 'x!')
88 call assert_equal(1, g:triggered)
89 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020090 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020091 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020092 endfunc
93
94 func Test_cursorhold_insert_ctrl_x()
95 let g:triggered = 0
96 au CursorHoldI * let g:triggered += 1
97 set updatetime=20
98 call timer_start(100, 'ExitInsertMode')
99 " CursorHoldI does not trigger after CTRL-X
100 call feedkeys("a\<C-X>", 'x!')
101 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +0100102 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200103 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200104 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200105 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200106
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100107 func Test_cursorhold_insert_ctrl_g_U()
108 au CursorHoldI * :
109 set updatetime=20
110 new
111 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
112 call feedkeys("i()\<C-g>U", 'tx!')
113 sleep 200m
114 call assert_equal('(foo)', getline(1))
115 undo
116 call assert_equal('', getline(1))
117
118 bwipe!
119 au! CursorHoldI
120 set updatetime&
121 endfunc
122
Bram Moolenaar97b00752019-05-12 13:07:14 +0200123 func Test_OptionSet_modeline()
124 call test_override('starting', 1)
125 au! OptionSet
126 augroup set_tabstop
127 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
128 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100129 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline', 'D')
Bram Moolenaar97b00752019-05-12 13:07:14 +0200130 set modeline
131 let v:errmsg = ''
132 call assert_fails('split XoptionsetModeline', 'E12:')
133 call assert_equal(7, &ts)
134 call assert_equal('', v:errmsg)
135
136 augroup set_tabstop
137 au!
138 augroup END
139 bwipe!
140 set ts&
Bram Moolenaar97b00752019-05-12 13:07:14 +0200141 call test_override('starting', 0)
142 endfunc
143
144endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200145
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200146func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200147 augroup test_bufunload_group
148 autocmd!
149 autocmd BufUnload * call add(s:li, "bufunload")
150 autocmd BufDelete * call add(s:li, "bufdelete")
151 autocmd BufWipeout * call add(s:li, "bufwipeout")
152 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200153
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100154 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200155 new
156 setlocal bufhidden=
157 bunload
158 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200159
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100160 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200161 new
162 setlocal bufhidden=delete
163 bunload
164 call assert_equal(["bufunload", "bufdelete"], s:li)
165
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100166 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200167 new
168 setlocal bufhidden=unload
169 bwipeout
170 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
171
Bram Moolenaare99e8442016-07-26 20:43:40 +0200172 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200173 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200174endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200175
176" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200177func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200178 tabedit
179 tabfirst
180
181 augroup test_autocmd_bufunload_with_tabnext_group
182 autocmd!
183 autocmd BufUnload <buffer> tabnext
184 augroup END
185
186 quit
187 call assert_equal(2, tabpagenr('$'))
188
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200189 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200190 augroup! test_autocmd_bufunload_with_tabnext_group
191 tablast
192 quit
193endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200194
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100195func Test_argdelete_in_next()
196 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
197 call assert_fails('next a b', 'E1156:')
198 au! BufNew,BufEnter,BufLeave,BufWinEnter *
199endfunc
200
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200201func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200202 tabedit
203 augroup sample
204 autocmd!
205 autocmd BufWinLeave <buffer> tabfirst
206 augroup END
207 call setline(1, ['a', 'b', 'c'])
208 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200209 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200210endfunc
211
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200212" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200213func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200214 split aa.txt
215 let lastbuf = bufnr('$')
216
217 augroup test_autocmd_bufunload
218 autocmd!
219 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
220 augroup END
221
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100222 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200223
224 autocmd! test_autocmd_bufunload
225 augroup! test_autocmd_bufunload
226 bwipe! aa.txt
227 bwipe! bb.txt
228endfunc
229
230" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200231func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200232 setlocal buftype=nowrite
233 let lastbuf = bufnr('$')
234
235 augroup test_autocmd_bufunload
236 autocmd!
237 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
238 augroup END
239
240 normal! i1
241 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200242
243 autocmd! test_autocmd_bufunload
244 augroup! test_autocmd_bufunload
245 bwipe! a.txt
246endfunc
247
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100248func Test_autocmd_dummy_wipeout()
249 " prepare files
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100250 call writefile([''], 'Xdummywipetest1.txt', 'D')
251 call writefile([''], 'Xdummywipetest2.txt', 'D')
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100252 augroup test_bufunload_group
253 autocmd!
254 autocmd BufUnload * call add(s:li, "bufunload")
255 autocmd BufDelete * call add(s:li, "bufdelete")
256 autocmd BufWipeout * call add(s:li, "bufwipeout")
257 augroup END
258
259 let s:li = []
260 split Xdummywipetest1.txt
261 silent! vimgrep /notmatched/ Xdummywipetest*
262 call assert_equal(["bufunload", "bufwipeout"], s:li)
263
264 bwipeout
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100265 au! test_bufunload_group
266 augroup! test_bufunload_group
267endfunc
268
Bram Moolenaarc917da42016-07-19 22:31:36 +0200269func Test_win_tab_autocmd()
270 let g:record = []
271
272 augroup testing
273 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000274 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaar94722c52023-01-28 19:19:03 +0000275 au WinEnter * call add(g:record, 'WinEnter')
276 au WinLeave * call add(g:record, 'WinLeave')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200277 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200278 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200279 au TabEnter * call add(g:record, 'TabEnter')
280 au TabLeave * call add(g:record, 'TabLeave')
281 augroup END
282
283 split
284 tabnew
285 close
286 close
287
288 call assert_equal([
289 \ 'WinLeave', 'WinNew', 'WinEnter',
290 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000291 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
292 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200293 \ ], g:record)
294
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200295 let g:record = []
296 tabnew somefile
297 tabnext
298 bwipe somefile
299
300 call assert_equal([
301 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
302 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000303 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200304 \ ], g:record)
305
Bram Moolenaarc917da42016-07-19 22:31:36 +0200306 augroup testing
307 au!
308 augroup END
309 unlet g:record
310endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200311
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000312func Test_WinResized()
313 CheckRunVimInTerminal
314
315 let lines =<< trim END
316 set scrolloff=0
317 call setline(1, ['111', '222'])
318 vnew
319 call setline(1, ['aaa', 'bbb'])
320 new
321 call setline(1, ['foo', 'bar'])
322
323 let g:resized = 0
324 au WinResized * let g:resized += 1
325
326 func WriteResizedEvent()
327 call writefile([json_encode(v:event)], 'XresizeEvent')
328 endfunc
329 au WinResized * call WriteResizedEvent()
330 END
331 call writefile(lines, 'Xtest_winresized', 'D')
332 let buf = RunVimInTerminal('-S Xtest_winresized', {'rows': 10})
333
334 " redraw now to avoid a redraw after the :echo command
335 call term_sendkeys(buf, ":redraw!\<CR>")
336 call TermWait(buf)
337
338 call term_sendkeys(buf, ":echo g:resized\<CR>")
339 call WaitForAssert({-> assert_match('^0$', term_getline(buf, 10))}, 1000)
340
341 " increase window height, two windows will be reported
342 call term_sendkeys(buf, "\<C-W>+")
343 call TermWait(buf)
344 call term_sendkeys(buf, ":echo g:resized\<CR>")
345 call WaitForAssert({-> assert_match('^1$', term_getline(buf, 10))}, 1000)
346
347 let event = readfile('XresizeEvent')[0]->json_decode()
348 call assert_equal({
349 \ 'windows': [1002, 1001],
350 \ }, event)
351
352 " increase window width, three windows will be reported
353 call term_sendkeys(buf, "\<C-W>>")
354 call TermWait(buf)
355 call term_sendkeys(buf, ":echo g:resized\<CR>")
356 call WaitForAssert({-> assert_match('^2$', term_getline(buf, 10))}, 1000)
357
358 let event = readfile('XresizeEvent')[0]->json_decode()
359 call assert_equal({
360 \ 'windows': [1002, 1001, 1000],
361 \ }, event)
362
363 call delete('XresizeEvent')
364 call StopVimInTerminal(buf)
365endfunc
366
LemonBoy09371822022-04-08 15:18:45 +0100367func Test_WinScrolled()
368 CheckRunVimInTerminal
369
370 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100371 set nowrap scrolloff=0
372 for ii in range(1, 18)
373 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
374 endfor
375 let win_id = win_getid()
376 let g:matched = v:false
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000377 func WriteScrollEvent()
378 call writefile([json_encode(v:event)], 'XscrollEvent')
379 endfunc
zeertzjqd58862d2022-04-12 11:32:48 +0100380 execute 'au WinScrolled' win_id 'let g:matched = v:true'
381 let g:scrolled = 0
382 au WinScrolled * let g:scrolled += 1
383 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
384 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000385 au WinScrolled * call WriteScrollEvent()
LemonBoy09371822022-04-08 15:18:45 +0100386 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100387 call writefile(lines, 'Xtest_winscrolled', 'D')
LemonBoy09371822022-04-08 15:18:45 +0100388 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
389
390 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
391 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
392
393 " Scroll left/right in Normal mode.
394 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
395 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
396
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000397 let event = readfile('XscrollEvent')[0]->json_decode()
398 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000399 \ 'all': {'leftcol': 1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
400 \ '1000': {'leftcol': -1, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000401 \ }, event)
402
LemonBoy09371822022-04-08 15:18:45 +0100403 " Scroll up/down in Normal mode.
404 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
405 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
406
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000407 let event = readfile('XscrollEvent')[0]->json_decode()
408 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000409 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
410 \ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000411 \ }, event)
412
LemonBoy09371822022-04-08 15:18:45 +0100413 " Scroll up/down in Insert mode.
414 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
415 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
416 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
417
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000418 let event = readfile('XscrollEvent')[0]->json_decode()
419 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000420 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
421 \ '1000': {'leftcol': 0, 'topline': -1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000422 \ }, event)
423
LemonBoy09371822022-04-08 15:18:45 +0100424 " Scroll the window horizontally to focus the last letter of the third line
425 " containing only six characters. Moving to the previous and shorter lines
426 " should trigger another autocommand as Vim has to make them visible.
427 call term_sendkeys(buf, "5zl2k")
428 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
429 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
430
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000431 let event = readfile('XscrollEvent')[0]->json_decode()
432 call assert_equal({
zeertzjq3fc84dc2022-12-07 09:17:59 +0000433 \ 'all': {'leftcol': 5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
434 \ '1000': {'leftcol': -5, 'topline': 0, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000435 \ }, event)
436
LemonBoy09371822022-04-08 15:18:45 +0100437 " Ensure the command was triggered for the specified window ID.
438 call term_sendkeys(buf, ":echo g:matched\<CR>")
439 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
440
441 " Ensure the expansion of <amatch> and <afile> matches the window ID.
442 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
443 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
444
Bram Moolenaar35fc61c2022-11-22 12:40:50 +0000445 call delete('XscrollEvent')
LemonBoy09371822022-04-08 15:18:45 +0100446 call StopVimInTerminal(buf)
LemonBoy09371822022-04-08 15:18:45 +0100447endfunc
448
LemonBoy66e13ae2022-04-21 22:52:11 +0100449func Test_WinScrolled_mouse()
450 CheckRunVimInTerminal
451
452 let lines =<< trim END
453 set nowrap scrolloff=0
454 set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard=
455 call setline(1, ['foo']->repeat(32))
456 split
457 let g:scrolled = 0
458 au WinScrolled * let g:scrolled += 1
459 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100460 call writefile(lines, 'Xtest_winscrolled_mouse', 'D')
LemonBoy66e13ae2022-04-21 22:52:11 +0100461 let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10})
462
463 " With the upper split focused, send a scroll-down event to the unfocused one.
464 call test_setmouse(7, 1)
465 call term_sendkeys(buf, "\<ScrollWheelDown>")
466 call TermWait(buf)
467 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
468 call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000)
469
470 " Again, but this time while we're in insert mode.
471 call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>")
472 call TermWait(buf)
473 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
474 call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000)
475
476 call StopVimInTerminal(buf)
LemonBoy66e13ae2022-04-21 22:52:11 +0100477endfunc
478
zeertzjqd58862d2022-04-12 11:32:48 +0100479func Test_WinScrolled_close_curwin()
480 CheckRunVimInTerminal
481
482 let lines =<< trim END
483 set nowrap scrolloff=0
484 call setline(1, ['aaa', 'bbb'])
485 vsplit
486 au WinScrolled * close
487 au VimLeave * call writefile(['123456'], 'Xtestout')
488 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100489 call writefile(lines, 'Xtest_winscrolled_close_curwin', 'D')
zeertzjqd58862d2022-04-12 11:32:48 +0100490 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
491
492 " This was using freed memory
493 call term_sendkeys(buf, "\<C-E>")
494 call TermWait(buf)
495 call StopVimInTerminal(buf)
496
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000497 " check the startup script finished to the end
zeertzjqd58862d2022-04-12 11:32:48 +0100498 call assert_equal(['123456'], readfile('Xtestout'))
zeertzjqd58862d2022-04-12 11:32:48 +0100499 call delete('Xtestout')
500endfunc
501
Bram Moolenaar0a60f792022-11-19 21:18:11 +0000502func Test_WinScrolled_once_only()
503 CheckRunVimInTerminal
504
505 let lines =<< trim END
506 set cmdheight=2
507 call setline(1, ['aaa', 'bbb'])
508 let trigger_count = 0
509 func ShowInfo(id)
510 echo g:trigger_count g:winid winlayout()
511 endfunc
512
513 vsplit
514 split
515 " use a timer to show the info after a redraw
516 au WinScrolled * let trigger_count += 1 | let winid = expand('<amatch>') | call timer_start(100, 'ShowInfo')
517 wincmd j
518 wincmd l
519 END
520 call writefile(lines, 'Xtest_winscrolled_once', 'D')
521 let buf = RunVimInTerminal('-S Xtest_winscrolled_once', #{rows: 10, cols: 60, statusoff: 2})
522
523 call term_sendkeys(buf, "\<C-E>")
524 call VerifyScreenDump(buf, 'Test_winscrolled_once_only_1', {})
525
526 call StopVimInTerminal(buf)
527endfunc
528
Bram Moolenaar29967732022-11-20 12:11:45 +0000529" Check that WinScrolled is not triggered immediately when defined and there
530" are split windows.
531func Test_WinScrolled_not_when_defined()
532 CheckRunVimInTerminal
533
534 let lines =<< trim END
535 call setline(1, ['aaa', 'bbb'])
536 echo 'nothing happened'
537 func ShowTriggered(id)
538 echo 'triggered'
539 endfunc
540 END
541 call writefile(lines, 'Xtest_winscrolled_not', 'D')
542 let buf = RunVimInTerminal('-S Xtest_winscrolled_not', #{rows: 10, cols: 60, statusoff: 2})
543 call term_sendkeys(buf, ":split\<CR>")
544 call TermWait(buf)
545 " use a timer to show the message after redrawing
546 call term_sendkeys(buf, ":au WinScrolled * call timer_start(100, 'ShowTriggered')\<CR>")
547 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_1', {})
548
549 call term_sendkeys(buf, "\<C-E>")
550 call VerifyScreenDump(buf, 'Test_winscrolled_not_when_defined_2', {})
551
552 call StopVimInTerminal(buf)
553endfunc
554
zeertzjq670ab032022-08-28 19:16:15 +0100555func Test_WinScrolled_long_wrapped()
556 CheckRunVimInTerminal
557
558 let lines =<< trim END
559 set scrolloff=0
560 let height = winheight(0)
561 let width = winwidth(0)
562 let g:scrolled = 0
563 au WinScrolled * let g:scrolled += 1
564 call setline(1, repeat('foo', height * width))
565 call cursor(1, height * width)
566 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100567 call writefile(lines, 'Xtest_winscrolled_long_wrapped', 'D')
zeertzjq670ab032022-08-28 19:16:15 +0100568 let buf = RunVimInTerminal('-S Xtest_winscrolled_long_wrapped', {'rows': 6})
569
570 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
571 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
572
573 call term_sendkeys(buf, 'gj')
574 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
575 call WaitForAssert({-> assert_match('^1 ', term_getline(buf, 6))}, 1000)
576
577 call term_sendkeys(buf, '0')
578 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
579 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
580
581 call term_sendkeys(buf, '$')
582 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
583 call WaitForAssert({-> assert_match('^3 ', term_getline(buf, 6))}, 1000)
Bram Moolenaar23526d22022-12-05 15:50:41 +0000584
585 call StopVimInTerminal(buf)
zeertzjq670ab032022-08-28 19:16:15 +0100586endfunc
587
zeertzjq3fc84dc2022-12-07 09:17:59 +0000588func Test_WinScrolled_diff()
589 CheckRunVimInTerminal
590
591 let lines =<< trim END
592 set diffopt+=foldcolumn:0
593 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
594 vnew
595 call setline(1, ['d', 'e', 'f', 'g', 'h', 'i'])
596 windo diffthis
597 func WriteScrollEvent()
598 call writefile([json_encode(v:event)], 'XscrollEvent')
599 endfunc
600 au WinScrolled * call WriteScrollEvent()
601 END
602 call writefile(lines, 'Xtest_winscrolled_diff', 'D')
603 let buf = RunVimInTerminal('-S Xtest_winscrolled_diff', {'rows': 8})
604
605 call term_sendkeys(buf, "\<C-E>")
606 call WaitForAssert({-> assert_match('^d', term_getline(buf, 3))}, 1000)
607
608 let event = readfile('XscrollEvent')[0]->json_decode()
609 call assert_equal({
610 \ 'all': {'leftcol': 0, 'topline': 1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
611 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
612 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -1, 'width': 0, 'height': 0, 'skipcol': 0}
613 \ }, event)
614
615 call term_sendkeys(buf, "2\<C-E>")
616 call WaitForAssert({-> assert_match('^f', term_getline(buf, 3))}, 1000)
617
618 let event = readfile('XscrollEvent')[0]->json_decode()
619 call assert_equal({
620 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 2, 'width': 0, 'height': 0, 'skipcol': 0},
621 \ '1000': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
622 \ '1001': {'leftcol': 0, 'topline': 0, 'topfill': -2, 'width': 0, 'height': 0, 'skipcol': 0}
623 \ }, event)
624
625 call term_sendkeys(buf, "\<C-E>")
626 call WaitForAssert({-> assert_match('^g', term_getline(buf, 3))}, 1000)
627
628 let event = readfile('XscrollEvent')[0]->json_decode()
629 call assert_equal({
630 \ 'all': {'leftcol': 0, 'topline': 2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
631 \ '1000': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
632 \ '1001': {'leftcol': 0, 'topline': 1, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0}
633 \ }, event)
634
635 call term_sendkeys(buf, "2\<C-Y>")
636 call WaitForAssert({-> assert_match('^e', term_getline(buf, 3))}, 1000)
637
638 let event = readfile('XscrollEvent')[0]->json_decode()
639 call assert_equal({
640 \ 'all': {'leftcol': 0, 'topline': 3, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0},
641 \ '1000': {'leftcol': 0, 'topline': -2, 'topfill': 0, 'width': 0, 'height': 0, 'skipcol': 0},
642 \ '1001': {'leftcol': 0, 'topline': -1, 'topfill': 1, 'width': 0, 'height': 0, 'skipcol': 0}
643 \ }, event)
644
645 call StopVimInTerminal(buf)
Dominique Pelle541c87c2023-01-17 21:20:44 +0000646 call delete('XscrollEvent')
zeertzjq3fc84dc2022-12-07 09:17:59 +0000647endfunc
648
naohiro ono23beefe2021-11-13 12:38:49 +0000649func Test_WinClosed()
650 " Test that the pattern is matched against the closed window's ID, and both
651 " <amatch> and <afile> are set to it.
652 new
653 let winid = win_getid()
654 let g:matched = v:false
655 augroup test-WinClosed
656 autocmd!
657 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
658 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
659 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
660 augroup END
661 close
662 call assert_true(g:matched)
663 call assert_equal(winid, g:amatch)
664 call assert_equal(winid, g:afile)
665
666 " Test that WinClosed is non-recursive.
667 new
668 new
669 call assert_equal(3, winnr('$'))
670 let g:triggered = 0
671 augroup test-WinClosed
672 autocmd!
673 autocmd WinClosed * let g:triggered += 1
674 autocmd WinClosed * 2 wincmd c
675 augroup END
676 close
677 call assert_equal(1, winnr('$'))
678 call assert_equal(1, g:triggered)
679
680 autocmd! test-WinClosed
681 augroup! test-WinClosed
682 unlet g:matched
683 unlet g:amatch
684 unlet g:afile
685 unlet g:triggered
686endfunc
687
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100688func Test_WinClosed_throws()
689 vnew
690 let bnr = bufnr()
691 call assert_equal(1, bufloaded(bnr))
692 augroup test-WinClosed
693 autocmd WinClosed * throw 'foo'
694 augroup END
695 try
696 close
697 catch /.*/
698 endtry
699 call assert_equal(0, bufloaded(bnr))
700
701 autocmd! test-WinClosed
702 augroup! test-WinClosed
703endfunc
704
zeertzjq6a069402022-04-07 14:08:29 +0100705func Test_WinClosed_throws_with_tabs()
706 tabnew
707 let bnr = bufnr()
708 call assert_equal(1, bufloaded(bnr))
709 augroup test-WinClosed
710 autocmd WinClosed * throw 'foo'
711 augroup END
712 try
713 close
714 catch /.*/
715 endtry
716 call assert_equal(0, bufloaded(bnr))
717
718 autocmd! test-WinClosed
719 augroup! test-WinClosed
720endfunc
721
zeertzjq62de54b2022-09-22 18:08:37 +0100722" This used to trigger WinClosed twice for the same window, and the window's
723" buffer was NULL in the second autocommand.
724func Test_WinClosed_switch_tab()
725 edit Xa
726 split Xb
727 split Xc
728 tab split
729 new
730 augroup test-WinClosed
731 autocmd WinClosed * tabprev | bwipe!
732 augroup END
733 close
734 " Check that the tabline has been fully removed
735 call assert_equal([1, 1], win_screenpos(0))
736
737 autocmd! test-WinClosed
738 augroup! test-WinClosed
739 %bwipe!
740endfunc
741
Bram Moolenaare99e8442016-07-26 20:43:40 +0200742func s:AddAnAutocmd()
743 augroup vimBarTest
744 au BufReadCmd * echo 'hello'
745 augroup END
746 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
747endfunc
748
749func Test_early_bar()
750 " test that a bar is recognized before the {event}
751 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000752 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200753 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000754 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200755
756 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000757 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200758 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000759 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200760
761 " test that a bar is recognized after the {event}
762 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000763 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200764 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000765 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200766
767 " test that a bar is recognized after the {group}
768 call s:AddAnAutocmd()
769 au! vimBarTest|echo 'hello'
770 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
771endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200772
Bram Moolenaar5c809082016-09-01 16:21:48 +0200773func RemoveGroup()
774 autocmd! StartOK
775 augroup! StartOK
776endfunc
777
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200778func Test_augroup_warning()
779 augroup TheWarning
780 au VimEnter * echo 'entering'
781 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100782 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200783 redir => res
784 augroup! TheWarning
785 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100786 call assert_match("W19:", res)
787 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200788
789 " check "Another" does not take the pace of the deleted entry
790 augroup Another
791 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100792 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200793 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200794
795 " no warning for postpone aucmd delete
796 augroup StartOK
797 au VimEnter * call RemoveGroup()
798 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100799 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200800 redir => res
801 doautocmd VimEnter
802 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100803 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200804 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200805
806 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200807endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200808
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200809func Test_BufReadCmdHelp()
810 " This used to cause access to free memory
811 au BufReadCmd * e +h
812 help
813
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200814 au! BufReadCmd
815endfunc
816
817func Test_BufReadCmdHelpJump()
818 " This used to cause access to free memory
819 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200820 " } to fix highlighting
821 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200822
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200823 au! BufReadCmd
824endfunc
825
zeertzjq93f72cc2022-08-26 15:34:52 +0100826" BufReadCmd is triggered for a "nofile" buffer. Check all values.
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100827func Test_BufReadCmdNofile()
zeertzjq93f72cc2022-08-26 15:34:52 +0100828 for val in ['nofile',
829 \ 'nowrite',
830 \ 'acwrite',
831 \ 'quickfix',
832 \ 'help',
833 \ 'terminal',
834 \ 'prompt',
835 \ 'popup',
836 \ ]
837 new somefile
838 exe 'set buftype=' .. val
839 au BufReadCmd somefile call setline(1, 'triggered')
840 edit
841 call assert_equal('triggered', getline(1))
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100842
zeertzjq93f72cc2022-08-26 15:34:52 +0100843 au! BufReadCmd
844 bwipe!
845 endfor
Bram Moolenaarb1d2c812022-08-26 11:55:01 +0100846endfunc
847
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200848func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200849 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200850 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200851 call assert_fails('augroup! x', 'E936:')
852 au VimEnter * echo
853 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200854 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100855 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200856 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200857endfunc
858
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200859" Tests for autocommands on :close command.
860" This used to be in test13.
861func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200862 " Clean up buffers, because in some cases this function fails.
863 call s:cleanup_buffers()
864
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200865 " Write three files and open them, each in a window.
866 " Then go to next window, with autocommand that deletes the previous one.
867 " Do this twice, writing the file.
868 e! Xtestje1
869 call setline(1, 'testje1')
870 w
871 sp Xtestje2
872 call setline(1, 'testje2')
873 w
874 sp Xtestje3
875 call setline(1, 'testje3')
876 w
877 wincmd w
878 au WinLeave Xtestje2 bwipe
879 wincmd w
880 call assert_equal('Xtestje1', expand('%'))
881
882 au WinLeave Xtestje1 bwipe Xtestje3
883 close
884 call assert_equal('Xtestje1', expand('%'))
885
886 " Test deleting the buffer on a Unload event. If this goes wrong there
887 " will be the ATTENTION prompt.
888 e Xtestje1
889 au!
890 au! BufUnload Xtestje1 bwipe
891 call assert_fails('e Xtestje3', 'E937:')
892 call assert_equal('Xtestje3', expand('%'))
893
894 e Xtestje2
895 sp Xtestje1
896 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200897 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200898
899 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
900 " there are ml_line errors and/or a Crash.
901 au!
902 only
903 e Xanother
904 e Xtestje1
905 bwipe Xtestje2
906 bwipe Xtestje3
907 au BufWipeout Xtestje1 buf Xtestje1
908 bwipe
909 call assert_equal('Xanother', expand('%'))
910
911 only
912 help
913 wincmd w
914 1quit
915 call assert_equal('Xanother', expand('%'))
916
917 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100918 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200919 call delete('Xtestje1')
920 call delete('Xtestje2')
921 call delete('Xtestje3')
922endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100923
924func Test_BufEnter()
925 au! BufEnter
926 au Bufenter * let val = val . '+'
927 let g:val = ''
928 split NewFile
929 call assert_equal('+', g:val)
930 bwipe!
931 call assert_equal('++', g:val)
932
933 " Also get BufEnter when editing a directory
Bram Moolenaar6f14da12022-09-07 21:30:44 +0100934 call mkdir('Xbufenterdir', 'D')
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100935 split Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100936 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100937
938 " On MS-Windows we can't edit the directory, make sure we wipe the right
939 " buffer.
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +0100940 bwipe! Xbufenterdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100941 au! BufEnter
Bram Moolenaara9b5b852022-08-26 13:16:20 +0100942
943 " Editing a "nofile" buffer doesn't read the file but does trigger BufEnter
zeertzjq93f72cc2022-08-26 15:34:52 +0100944 " for historic reasons. Also test other 'buftype' values.
945 for val in ['nofile',
946 \ 'nowrite',
947 \ 'acwrite',
948 \ 'quickfix',
949 \ 'help',
950 \ 'terminal',
951 \ 'prompt',
952 \ 'popup',
953 \ ]
954 new somefile
955 exe 'set buftype=' .. val
956 au BufEnter somefile call setline(1, 'some text')
957 edit
958 call assert_equal('some text', getline(1))
959 bwipe!
960 au! BufEnter
961 endfor
Bram Moolenaar9fda8152022-11-19 13:14:10 +0000962
963 new
964 new
965 autocmd BufEnter * ++once close
966 call assert_fails('close', 'E1312:')
967
968 au! BufEnter
969 only
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100970endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100971
972" Closing a window might cause an endless loop
973" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200974func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200975 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100976 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200977 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100978 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100979 mksession!
980
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200981 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200982 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200983 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100984 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200985 augroup test_autocmd_sessionload
986 autocmd!
987 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
988 augroup END
989
990 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100991 call writefile([execute("messages")], "XerrorsBwipe")
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200992 endfunc
993 au VimLeave * call WriteErrors()
994 [CODE]
995
Bram Moolenaare1f3ab72022-09-04 21:29:08 +0100996 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200997 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +0100998 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +0100999 let errors = join(readfile('XerrorsBwipe'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001000 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001001
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001002 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001003 for file in ['Session.vim', 'XerrorsBwipe']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001004 call delete(file)
1005 endfor
1006endfunc
1007
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001008" Using :blast and :ball for many events caused a crash, because b_nwindows was
1009" not incremented correctly.
1010func Test_autocmd_blast_badd()
1011 let content =<< trim [CODE]
1012 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
1013 edit foo1
1014 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
1015 edit foo2
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001016 call writefile(['OK'], 'XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001017 qall
1018 [CODE]
1019
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001020 call writefile(content, 'XblastBall', 'D')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001021 call system(GetVimCommand() .. ' --clean -S XblastBall')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001022 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001023 call assert_match('OK', readfile('XerrorsBlast')->join())
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001024
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001025 call delete('XerrorsBlast')
Bram Moolenaar797e63b2021-01-15 16:22:52 +01001026endfunc
1027
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001028" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001029func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001030 tabnew
1031 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001032 mksession!
1033
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001034 let content =<< trim [CODE]
1035 set nocp noswapfile
1036 function! DeleteInactiveBufs()
1037 tabfirst
1038 let tabblist = []
1039 for i in range(1, tabpagenr(''$''))
1040 call extend(tabblist, tabpagebuflist(i))
1041 endfor
1042 for b in range(1, bufnr(''$''))
1043 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
1044 exec ''bwipeout '' . b
1045 endif
1046 endfor
1047 echomsg "SessionLoadPost DONE"
1048 endfunction
1049 au SessionLoadPost * call DeleteInactiveBufs()
1050
1051 func WriteErrors()
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001052 call writefile([execute("messages")], "XerrorsPost")
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001053 endfunc
1054 au VimLeave * call WriteErrors()
1055 [CODE]
1056
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001057 call writefile(content, 'Xvimrc', 'D')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001058 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaarae04a602022-09-09 15:08:10 +01001059 sleep 100m
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001060 let errors = join(readfile('XerrorsPost'))
Bram Moolenaare94260f2017-03-21 15:50:12 +01001061 " This probably only ever matches on unix.
1062 call assert_notmatch('Caught deadly signal SEGV', errors)
1063 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001064
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001065 set swapfile
Bram Moolenaarbe9fc5b2022-09-09 17:09:35 +01001066 for file in ['Session.vim', 'XerrorsPost']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +01001067 call delete(file)
1068 endfor
1069endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +02001070
1071func Test_empty_doau()
1072 doau \|
1073endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001074
1075func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001076 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001077 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +02001078 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
1079 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 +02001080 let g:opt = [expected, actual]
1081 "call assert_equal(expected, actual)
1082endfunc
1083
1084func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001085 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001086
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001087 badd test_autocmd.vim
1088
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001089 call test_override('starting', 1)
1090 set nocp
1091 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
1092
1093 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001094 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001095 set nu
1096 call assert_equal([], g:options)
1097 call assert_equal(g:opt[0], g:opt[1])
1098
1099 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001100 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001101 setlocal nonu
1102 call assert_equal([], g:options)
1103 call assert_equal(g:opt[0], g:opt[1])
1104
1105 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001106 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001107 setglobal nonu
1108 call assert_equal([], g:options)
1109 call assert_equal(g:opt[0], g:opt[1])
1110
1111 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001112 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001113 setlocal ai
1114 call assert_equal([], g:options)
1115 call assert_equal(g:opt[0], g:opt[1])
1116
1117 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001118 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001119 setglobal ai
1120 call assert_equal([], g:options)
1121 call assert_equal(g:opt[0], g:opt[1])
1122
1123 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001124 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001125 set ai!
1126 call assert_equal([], g:options)
1127 call assert_equal(g:opt[0], g:opt[1])
1128
1129 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001130 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001131 noa setlocal ai
1132 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001133 set ai!
1134 call assert_equal([], g:options)
1135 call assert_equal(g:opt[0], g:opt[1])
1136
1137 " Should not print anything, use :noa
1138 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001139 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001140 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001141 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001142 call assert_equal(g:opt[0], g:opt[1])
1143
1144 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001145 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001146 set list nu
1147 call assert_equal([], g:options)
1148 call assert_equal(g:opt[0], g:opt[1])
1149
1150 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001151 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001152 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +02001153 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 +02001154 call assert_equal(g:opt[0], g:opt[1])
1155
1156 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001157 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001158 setlocal acd
1159 call assert_equal([], g:options)
1160 call assert_equal(g:opt[0], g:opt[1])
1161
1162 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001163 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001164 set ar
1165 call assert_equal([], g:options)
1166 call assert_equal(g:opt[0], g:opt[1])
1167
1168 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001169 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001170 setlocal ar
1171 call assert_equal([], g:options)
1172 call assert_equal(g:opt[0], g:opt[1])
1173
1174 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001175 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001176 setglobal invar
1177 call assert_equal([], g:options)
1178 call assert_equal(g:opt[0], g:opt[1])
1179
1180 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001181 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
1182 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001183 call assert_equal([], g:options)
1184 call assert_equal(g:opt[0], g:opt[1])
1185
1186 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001187 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001188 " try twice, first time, shouldn't trigger because option name is invalid,
1189 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001190 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +02001191 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001192 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001193 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001194 call assert_equal([], g:options)
1195 call assert_equal(g:opt[0], g:opt[1])
1196
1197 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001198 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001199 call setwinvar(0, '&number', 1)
1200 call assert_equal([], g:options)
1201 call assert_equal(g:opt[0], g:opt[1])
1202
1203 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001204 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001205 setlocal key=blah
1206 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +02001207 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001208 call assert_equal(g:opt[0], g:opt[1])
1209
Bram Moolenaard7c96872019-06-15 17:12:48 +02001210
1211 " 18a: Setting string global option"
1212 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001213 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001214 set backupext=foo
1215 call assert_equal([], g:options)
1216 call assert_equal(g:opt[0], g:opt[1])
1217
1218 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001219 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001220 set backupext&
1221 call assert_equal([], g:options)
1222 call assert_equal(g:opt[0], g:opt[1])
1223
1224 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001225 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001226 setglobal backupext=bar
1227 call assert_equal([], g:options)
1228 call assert_equal(g:opt[0], g:opt[1])
1229
1230 " 18d: Setting local string global option"
1231 " As this is a global option this sets the global value even though
1232 " :setlocal is used!
1233 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001234 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001235 setlocal backupext=baz
1236 call assert_equal([], g:options)
1237 call assert_equal(g:opt[0], g:opt[1])
1238
1239 " 18e: Setting again string global option"
1240 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
1241 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001242 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001243 set backupext=fuu
1244 call assert_equal([], g:options)
1245 call assert_equal(g:opt[0], g:opt[1])
1246
1247
zeertzjqb811de52021-10-21 10:50:44 +01001248 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001249 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001250 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001251 set tags=tagpath
1252 call assert_equal([], g:options)
1253 call assert_equal(g:opt[0], g:opt[1])
1254
zeertzjqb811de52021-10-21 10:50:44 +01001255 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001256 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +02001257 set tags&
1258 call assert_equal([], g:options)
1259 call assert_equal(g:opt[0], g:opt[1])
1260
zeertzjqb811de52021-10-21 10:50:44 +01001261 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001262 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001263 setglobal tags=tagpath1
1264 call assert_equal([], g:options)
1265 call assert_equal(g:opt[0], g:opt[1])
1266
zeertzjqb811de52021-10-21 10:50:44 +01001267 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001268 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001269 setlocal tags=tagpath2
1270 call assert_equal([], g:options)
1271 call assert_equal(g:opt[0], g:opt[1])
1272
zeertzjqb811de52021-10-21 10:50:44 +01001273 " 19e: Setting again string global-local (to buffer) option"
1274 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001275 " but the old local value for all other kinds of options.
1276 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
1277 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001278 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001279 set tags=tagpath
1280 call assert_equal([], g:options)
1281 call assert_equal(g:opt[0], g:opt[1])
1282
zeertzjqb811de52021-10-21 10:50:44 +01001283 " 19f: Setting string global-local (to buffer) option to an empty string"
1284 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001285 " but the old local value for all other kinds of options.
1286 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
1287 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001288 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001289 set tags=tagpath
1290 call assert_equal([], g:options)
1291 call assert_equal(g:opt[0], g:opt[1])
1292
1293
1294 " 20a: Setting string local (to buffer) option"
1295 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001296 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001297 set spelllang=elvish,klingon
1298 call assert_equal([], g:options)
1299 call assert_equal(g:opt[0], g:opt[1])
1300
1301 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001302 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001303 set spelllang&
1304 call assert_equal([], g:options)
1305 call assert_equal(g:opt[0], g:opt[1])
1306
1307 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001308 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001309 setglobal spelllang=elvish
1310 call assert_equal([], g:options)
1311 call assert_equal(g:opt[0], g:opt[1])
1312
1313 " 20d: Setting local string local (to buffer) option"
1314 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001315 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001316 setlocal spelllang=klingon
1317 call assert_equal([], g:options)
1318 call assert_equal(g:opt[0], g:opt[1])
1319
1320 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +01001321 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001322 " but the old local value for all other kinds of options.
1323 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
1324 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001325 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001326 set spelllang=foo
1327 call assert_equal([], g:options)
1328 call assert_equal(g:opt[0], g:opt[1])
1329
1330
zeertzjqb811de52021-10-21 10:50:44 +01001331 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001332 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001333 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001334 set statusline=foo
1335 call assert_equal([], g:options)
1336 call assert_equal(g:opt[0], g:opt[1])
1337
zeertzjqb811de52021-10-21 10:50:44 +01001338 " 21b: Resetting string global-local (to window) option"
1339 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001340 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001341 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001342 set statusline&
1343 call assert_equal([], g:options)
1344 call assert_equal(g:opt[0], g:opt[1])
1345
zeertzjqb811de52021-10-21 10:50:44 +01001346 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001347 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001348 setglobal statusline=bar
1349 call assert_equal([], g:options)
1350 call assert_equal(g:opt[0], g:opt[1])
1351
zeertzjqb811de52021-10-21 10:50:44 +01001352 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001353 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001354 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001355 setlocal statusline=baz
1356 call assert_equal([], g:options)
1357 call assert_equal(g:opt[0], g:opt[1])
1358
zeertzjqb811de52021-10-21 10:50:44 +01001359 " 21e: Setting again string global-local (to window) option"
1360 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001361 " but the old local value for all other kinds of options.
1362 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1363 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001364 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001365 set statusline=foo
1366 call assert_equal([], g:options)
1367 call assert_equal(g:opt[0], g:opt[1])
1368
1369
1370 " 22a: Setting string local (to window) option"
1371 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001372 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001373 set foldignore=fo
1374 call assert_equal([], g:options)
1375 call assert_equal(g:opt[0], g:opt[1])
1376
1377 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001378 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001379 set foldignore&
1380 call assert_equal([], g:options)
1381 call assert_equal(g:opt[0], g:opt[1])
1382
1383 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001384 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001385 setglobal foldignore=bar
1386 call assert_equal([], g:options)
1387 call assert_equal(g:opt[0], g:opt[1])
1388
1389 " 22d: Setting local string local (to window) option"
1390 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001391 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001392 setlocal foldignore=baz
1393 call assert_equal([], g:options)
1394 call assert_equal(g:opt[0], g:opt[1])
1395
1396 " 22e: Setting again string local (to window) option"
1397 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1398 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001399 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001400 set foldignore=fo
1401 call assert_equal([], g:options)
1402 call assert_equal(g:opt[0], g:opt[1])
1403
1404
zeertzjqb811de52021-10-21 10:50:44 +01001405 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001406 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1407 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001408 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001409 setglobal cmdheight=2
1410 call assert_equal([], g:options)
1411 call assert_equal(g:opt[0], g:opt[1])
1412
1413 " 23b: Setting local number global option"
1414 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1415 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001416 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001417 setlocal cmdheight=2
1418 call assert_equal([], g:options)
1419 call assert_equal(g:opt[0], g:opt[1])
1420
1421 " 23c: Setting again number global option"
1422 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1423 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001424 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001425 set cmdheight=2
1426 call assert_equal([], g:options)
1427 call assert_equal(g:opt[0], g:opt[1])
1428
1429 " 23d: Setting again number global option"
1430 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001431 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001432 set cmdheight=2
1433 call assert_equal([], g:options)
1434 call assert_equal(g:opt[0], g:opt[1])
1435
1436
1437 " 24a: Setting global number global-local (to buffer) option"
1438 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1439 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001440 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001441 setglobal undolevels=2
1442 call assert_equal([], g:options)
1443 call assert_equal(g:opt[0], g:opt[1])
1444
1445 " 24b: Setting local number global-local (to buffer) option"
1446 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1447 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001448 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001449 setlocal undolevels=2
1450 call assert_equal([], g:options)
1451 call assert_equal(g:opt[0], g:opt[1])
1452
1453 " 24c: Setting again number global-local (to buffer) option"
1454 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1455 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001456 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001457 set undolevels=2
1458 call assert_equal([], g:options)
1459 call assert_equal(g:opt[0], g:opt[1])
1460
1461 " 24d: Setting again global number global-local (to buffer) option"
1462 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001463 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001464 set undolevels=2
1465 call assert_equal([], g:options)
1466 call assert_equal(g:opt[0], g:opt[1])
1467
1468
1469 " 25a: Setting global number local (to buffer) option"
1470 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1471 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001472 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001473 setglobal wrapmargin=2
1474 call assert_equal([], g:options)
1475 call assert_equal(g:opt[0], g:opt[1])
1476
1477 " 25b: Setting local number local (to buffer) option"
1478 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1479 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001480 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001481 setlocal wrapmargin=2
1482 call assert_equal([], g:options)
1483 call assert_equal(g:opt[0], g:opt[1])
1484
1485 " 25c: Setting again number local (to buffer) option"
1486 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1487 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001488 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001489 set wrapmargin=2
1490 call assert_equal([], g:options)
1491 call assert_equal(g:opt[0], g:opt[1])
1492
1493 " 25d: Setting again global number local (to buffer) option"
1494 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001495 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001496 set wrapmargin=2
1497 call assert_equal([], g:options)
1498 call assert_equal(g:opt[0], g:opt[1])
1499
1500
1501 " 26: Setting number global-local (to window) option.
1502 " Such option does currently not exist.
1503
1504
1505 " 27a: Setting global number local (to window) option"
1506 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1507 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001508 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001509 setglobal foldcolumn=2
1510 call assert_equal([], g:options)
1511 call assert_equal(g:opt[0], g:opt[1])
1512
1513 " 27b: Setting local number local (to window) option"
1514 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1515 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001516 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001517 setlocal foldcolumn=2
1518 call assert_equal([], g:options)
1519 call assert_equal(g:opt[0], g:opt[1])
1520
1521 " 27c: Setting again number local (to window) option"
1522 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1523 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001524 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001525 set foldcolumn=2
1526 call assert_equal([], g:options)
1527 call assert_equal(g:opt[0], g:opt[1])
1528
zeertzjqb811de52021-10-21 10:50:44 +01001529 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001530 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001531 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001532 set foldcolumn=2
1533 call assert_equal([], g:options)
1534 call assert_equal(g:opt[0], g:opt[1])
1535
1536
1537 " 28a: Setting global boolean global option"
1538 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1539 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001540 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001541 setglobal nowrapscan
1542 call assert_equal([], g:options)
1543 call assert_equal(g:opt[0], g:opt[1])
1544
1545 " 28b: Setting local boolean global option"
1546 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1547 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001548 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001549 setlocal nowrapscan
1550 call assert_equal([], g:options)
1551 call assert_equal(g:opt[0], g:opt[1])
1552
1553 " 28c: Setting again boolean global option"
1554 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1555 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001556 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001557 set nowrapscan
1558 call assert_equal([], g:options)
1559 call assert_equal(g:opt[0], g:opt[1])
1560
1561 " 28d: Setting again global boolean global option"
1562 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001563 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001564 set wrapscan
1565 call assert_equal([], g:options)
1566 call assert_equal(g:opt[0], g:opt[1])
1567
1568
1569 " 29a: Setting global boolean global-local (to buffer) option"
1570 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1571 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001572 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001573 setglobal autoread
1574 call assert_equal([], g:options)
1575 call assert_equal(g:opt[0], g:opt[1])
1576
1577 " 29b: Setting local boolean global-local (to buffer) option"
1578 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1579 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001580 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001581 setlocal noautoread
1582 call assert_equal([], g:options)
1583 call assert_equal(g:opt[0], g:opt[1])
1584
1585 " 29c: Setting again boolean global-local (to buffer) option"
1586 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1587 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001588 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001589 set autoread
1590 call assert_equal([], g:options)
1591 call assert_equal(g:opt[0], g:opt[1])
1592
1593 " 29d: Setting again global boolean global-local (to buffer) option"
1594 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001595 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001596 set autoread
1597 call assert_equal([], g:options)
1598 call assert_equal(g:opt[0], g:opt[1])
1599
1600
1601 " 30a: Setting global boolean local (to buffer) option"
1602 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1603 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001604 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001605 setglobal cindent
1606 call assert_equal([], g:options)
1607 call assert_equal(g:opt[0], g:opt[1])
1608
1609 " 30b: Setting local boolean local (to buffer) option"
1610 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1611 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001612 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001613 setlocal nocindent
1614 call assert_equal([], g:options)
1615 call assert_equal(g:opt[0], g:opt[1])
1616
1617 " 30c: Setting again boolean local (to buffer) option"
1618 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1619 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001620 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001621 set cindent
1622 call assert_equal([], g:options)
1623 call assert_equal(g:opt[0], g:opt[1])
1624
1625 " 30d: Setting again global boolean local (to buffer) option"
1626 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001627 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001628 set cindent
1629 call assert_equal([], g:options)
1630 call assert_equal(g:opt[0], g:opt[1])
1631
1632
1633 " 31: Setting boolean global-local (to window) option
1634 " Currently no such option exists.
1635
1636
1637 " 32a: Setting global boolean local (to window) option"
1638 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1639 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001640 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001641 setglobal cursorcolumn
1642 call assert_equal([], g:options)
1643 call assert_equal(g:opt[0], g:opt[1])
1644
1645 " 32b: Setting local boolean local (to window) option"
1646 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1647 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001648 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001649 setlocal nocursorcolumn
1650 call assert_equal([], g:options)
1651 call assert_equal(g:opt[0], g:opt[1])
1652
1653 " 32c: Setting again boolean local (to window) option"
1654 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1655 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001656 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001657 set cursorcolumn
1658 call assert_equal([], g:options)
1659 call assert_equal(g:opt[0], g:opt[1])
1660
1661 " 32d: Setting again global boolean local (to window) option"
1662 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001663 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001664 set cursorcolumn
1665 call assert_equal([], g:options)
1666 call assert_equal(g:opt[0], g:opt[1])
1667
1668
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001669 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001670 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001671 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001672 set backspace=2
1673 call assert_equal([], g:options)
1674 call assert_equal(g:opt[0], g:opt[1])
1675
1676
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001677 " Cleanup
1678 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001679 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001680 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 +02001681 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001682 endfor
1683 call test_override('starting', 0)
1684 delfunc! AutoCommandOptionSet
1685endfunc
1686
1687func Test_OptionSet_diffmode()
1688 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001689 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001690 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001691 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001692
1693 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1694 call assert_equal(0, &l:cul)
1695 diffthis
1696 call assert_equal(1, &l:cul)
1697
1698 vnew
1699 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1700 call assert_equal(0, &l:cul)
1701 diffthis
1702 call assert_equal(1, &l:cul)
1703
1704 diffoff
1705 call assert_equal(0, &l:cul)
1706 call assert_equal(1, getwinvar(2, '&l:cul'))
1707 bw!
1708
1709 call assert_equal(1, &l:cul)
1710 diffoff!
1711 call assert_equal(0, &l:cul)
1712 call assert_equal(0, getwinvar(1, '&l:cul'))
1713 bw!
1714
1715 " Cleanup
1716 au! OptionSet
1717 call test_override('starting', 0)
1718endfunc
1719
1720func Test_OptionSet_diffmode_close()
1721 call test_override('starting', 1)
1722 " 19: Try to close the current window when entering diff mode
1723 " should not segfault
1724 new
1725 au OptionSet diff close
1726
1727 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001728 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001729 call assert_equal(1, &diff)
1730 vnew
1731 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001732 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001733 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001734 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001735 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001736 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001737 bw!
1738
1739 " Cleanup
1740 au! OptionSet
1741 call test_override('starting', 0)
1742 "delfunc! AutoCommandOptionSet
1743endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001744
1745" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1746func Test_BufleaveWithDelete()
Bram Moolenaare7cda972022-08-29 11:02:59 +01001747 new | edit XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001748
1749 augroup test_bufleavewithdelete
1750 autocmd!
Bram Moolenaare7cda972022-08-29 11:02:59 +01001751 autocmd BufLeave XbufLeave1 bwipe XbufLeave2
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001752 augroup END
1753
Bram Moolenaare7cda972022-08-29 11:02:59 +01001754 call assert_fails('edit XbufLeave2', 'E143:')
1755 call assert_equal('XbufLeave1', bufname('%'))
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001756
Bram Moolenaare7cda972022-08-29 11:02:59 +01001757 autocmd! test_bufleavewithdelete BufLeave XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001758 augroup! test_bufleavewithdelete
1759
1760 new
Bram Moolenaare7cda972022-08-29 11:02:59 +01001761 bwipe! XbufLeave1
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001762endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001763
1764" Test for autocommand that changes the buffer list, when doing ":ball".
1765func Test_Acmd_BufAll()
1766 enew!
1767 %bwipe!
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001768 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
1769 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
1770 call writefile(['Test file Xxx3'], 'Xxx3', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001771
1772 " Add three files to the buffer list
1773 split Xxx1
1774 close
1775 split Xxx2
1776 close
1777 split Xxx3
1778 close
1779
1780 " Wipe the buffer when the buffer is opened
1781 au BufReadPost Xxx2 bwipe
1782
1783 call append(0, 'Test file Xxx4')
1784 ball
1785
1786 call assert_equal(2, winnr('$'))
1787 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1788 wincmd t
1789
1790 au! BufReadPost
1791 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001792 enew! | only
1793endfunc
1794
1795" Test for autocommand that changes current buffer on BufEnter event.
1796" Check if modelines are interpreted for the correct buffer.
1797func Test_Acmd_BufEnter()
1798 %bwipe!
1799 call writefile(['start of test file Xxx1',
1800 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001801 \ 'end of test file Xxx1'], 'Xxx1', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001802 call writefile(['start of test file Xxx2',
1803 \ 'vim: set noai :',
1804 \ "\<Tab>this is a test",
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001805 \ 'end of test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001806
1807 au BufEnter Xxx2 brew
1808 set ai modeline modelines=3
1809 edit Xxx1
1810 " edit Xxx2, autocmd will do :brew
1811 edit Xxx2
1812 exe "normal G?this is a\<CR>"
1813 " Append text with autoindent to this file
1814 normal othis should be auto-indented
1815 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1816 call assert_equal(3, line('.'))
1817 " Remove autocmd and edit Xxx2 again
1818 au! BufEnter Xxx2
1819 buf! Xxx2
1820 exe "normal G?this is a\<CR>"
1821 " append text without autoindent to Xxx
1822 normal othis should be in column 1
1823 call assert_equal("this should be in column 1", getline('.'))
1824 call assert_equal(4, line('.'))
1825
1826 %bwipe!
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001827 set ai&vim modeline&vim modelines&vim
1828endfunc
1829
1830" Test for issue #57
1831" do not move cursor on <c-o> when autoindent is set
1832func Test_ai_CTRL_O()
1833 enew!
1834 set ai
1835 let save_fo = &fo
1836 set fo+=r
1837 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1838 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1839 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1840
1841 set ai&vim
1842 let &fo = save_fo
1843 enew!
1844endfunc
1845
1846" Test for autocommand that deletes the current buffer on BufLeave event.
1847" Also test deleting the last buffer, should give a new, empty buffer.
1848func Test_BufLeave_Wipe()
1849 %bwipe!
1850 let content = ['start of test file Xxx',
1851 \ 'this is a test',
1852 \ 'end of test file Xxx']
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01001853 call writefile(content, 'Xxx1', 'D')
1854 call writefile(content, 'Xxx2', 'D')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001855
1856 au BufLeave Xxx2 bwipe
1857 edit Xxx1
1858 split Xxx2
1859 " delete buffer Xxx2, we should be back to Xxx1
1860 bwipe
1861 call assert_equal('Xxx1', bufname('%'))
1862 call assert_equal(1, winnr('$'))
1863
1864 " Create an alternate buffer
1865 %write! test.out
1866 call assert_equal('test.out', bufname('#'))
1867 " delete alternate buffer
1868 bwipe test.out
1869 call assert_equal('Xxx1', bufname('%'))
1870 call assert_equal('', bufname('#'))
1871
1872 au BufLeave Xxx1 bwipe
1873 " delete current buffer, get an empty one
1874 bwipe!
1875 call assert_equal(1, line('$'))
1876 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001877 let g:bufinfo = getbufinfo()
1878 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001879
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001880 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001881 %bwipe
1882 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001883
1884 " check that bufinfo doesn't contain a pointer to freed memory
1885 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001886endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001887
1888func Test_QuitPre()
1889 edit Xfoo
1890 let winid = win_getid(winnr())
1891 split Xbar
1892 au! QuitPre * let g:afile = expand('<afile>')
1893 " Close the other window, <afile> should be correct.
1894 exe win_id2win(winid) . 'q'
1895 call assert_equal('Xfoo', g:afile)
LemonBoy66e13ae2022-04-21 22:52:11 +01001896
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001897 unlet g:afile
1898 bwipe Xfoo
1899 bwipe Xbar
1900endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001901
1902func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001903 au! CmdlineChanged : let g:text = getcmdline()
1904 let g:text = 0
1905 call feedkeys(":echom 'hello'\<CR>", 'xt')
1906 call assert_equal("echom 'hello'", g:text)
1907 au! CmdlineChanged
1908
1909 au! CmdlineChanged : let g:entered = expand('<afile>')
1910 let g:entered = 0
1911 call feedkeys(":echom 'hello'\<CR>", 'xt')
1912 call assert_equal(':', g:entered)
1913 au! CmdlineChanged
1914
zeertzjq412e0e42023-02-11 10:34:07 +00001915 autocmd CmdlineChanged : let g:log += [getcmdline()]
1916
Bram Moolenaarbb393d82022-12-09 12:21:50 +00001917 let g:log = []
1918 cnoremap <F1> <Cmd>call setcmdline('ls')<CR>
Bram Moolenaarbb393d82022-12-09 12:21:50 +00001919 call feedkeys(":\<F1>", 'xt')
1920 call assert_equal(['ls'], g:log)
Bram Moolenaarbb393d82022-12-09 12:21:50 +00001921 cunmap <F1>
1922
zeertzjqaf9e28a2023-02-06 20:58:09 +00001923 let g:log = []
zeertzjqaf9e28a2023-02-06 20:58:09 +00001924 call feedkeys(":sign \<Tab>\<Tab>\<C-N>\<C-P>\<S-Tab>\<S-Tab>\<Esc>", 'xt')
1925 call assert_equal([
1926 \ 's',
1927 \ 'si',
1928 \ 'sig',
1929 \ 'sign',
1930 \ 'sign ',
1931 \ 'sign define',
1932 \ 'sign jump',
1933 \ 'sign list',
1934 \ 'sign jump',
1935 \ 'sign define',
1936 \ 'sign ',
1937 \ ], g:log)
1938 let g:log = []
1939 set wildmenu wildoptions+=pum
1940 call feedkeys(":sign \<S-Tab>\<PageUp>\<kPageUp>\<kPageDown>\<PageDown>\<Esc>", 'xt')
1941 call assert_equal([
1942 \ 's',
1943 \ 'si',
1944 \ 'sig',
1945 \ 'sign',
1946 \ 'sign ',
1947 \ 'sign unplace',
1948 \ 'sign jump',
1949 \ 'sign define',
1950 \ 'sign undefine',
1951 \ 'sign unplace',
1952 \ ], g:log)
1953 set wildmenu& wildoptions&
zeertzjq412e0e42023-02-11 10:34:07 +00001954
1955 let g:log = []
1956 let @r = 'abc'
1957 call feedkeys(":0\<C-R>r1\<C-R>\<C-O>r2\<C-R>\<C-R>r3\<Esc>", 'xt')
1958 call assert_equal([
1959 \ '0',
1960 \ '0a',
1961 \ '0ab',
1962 \ '0abc',
1963 \ '0abc1',
1964 \ '0abc1abc',
1965 \ '0abc1abc2',
1966 \ '0abc1abc2abc',
1967 \ '0abc1abc2abc3',
1968 \ ], g:log)
1969
zeertzjqaf9e28a2023-02-06 20:58:09 +00001970 unlet g:log
1971 au! CmdlineChanged
1972
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001973 au! CmdlineEnter : let g:entered = expand('<afile>')
1974 au! CmdlineLeave : let g:left = expand('<afile>')
1975 let g:entered = 0
1976 let g:left = 0
1977 call feedkeys(":echo 'hello'\<CR>", 'xt')
1978 call assert_equal(':', g:entered)
1979 call assert_equal(':', g:left)
1980 au! CmdlineEnter
1981 au! CmdlineLeave
1982
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001983 let save_shellslash = &shellslash
1984 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001985 au! CmdlineEnter / let g:entered = expand('<afile>')
1986 au! CmdlineLeave / let g:left = expand('<afile>')
1987 let g:entered = 0
1988 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001989 new
1990 call setline(1, 'hello')
1991 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001992 call assert_equal('/', g:entered)
1993 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001994 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001995 au! CmdlineEnter
1996 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001997 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001998endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001999
2000" Test for BufWritePre autocommand that deletes or unloads the buffer.
2001func Test_BufWritePre()
2002 %bwipe
2003 au BufWritePre Xxx1 bunload
2004 au BufWritePre Xxx2 bwipe
2005
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002006 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1', 'D')
2007 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002008
2009 edit Xtest
2010 e! Xxx2
2011 bdel Xtest
2012 e Xxx1
2013 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002014 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002015 call assert_equal('Xxx2', bufname('%'))
2016 edit Xtest
2017 e! Xxx2
2018 bwipe Xtest
2019 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02002020 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002021 call assert_equal('Xxx1', bufname('%'))
2022 au! BufWritePre
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002023endfunc
2024
2025" Test for BufUnload autocommand that unloads all the other buffers
2026func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002027 let g:test_is_flaky = 1
Christian Brabandtee17b6f2023-09-09 11:23:50 +02002028 call writefile(['Test file Xxx1'], 'Xxx1', 'D')
2029 call writefile(['Test file Xxx2'], 'Xxx2', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002030
Bram Moolenaarc79745a2019-05-20 22:12:34 +02002031 let content =<< trim [CODE]
2032 func UnloadAllBufs()
2033 let i = 1
2034 while i <= bufnr('$')
2035 if i != bufnr('%') && bufloaded(i)
2036 exe i . 'bunload'
2037 endif
2038 let i += 1
2039 endwhile
2040 endfunc
2041 au BufUnload * call UnloadAllBufs()
2042 au VimLeave * call writefile(['Test Finished'], 'Xout')
2043 edit Xxx1
2044 split Xxx2
2045 q
2046 [CODE]
2047
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002048 call writefile(content, 'Xbunloadtest', 'D')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002049
2050 call delete('Xout')
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002051 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xbunloadtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002052 call assert_true(filereadable('Xout'))
2053
Bram Moolenaar53f0c962017-10-22 14:23:59 +02002054 call delete('Xout')
2055endfunc
2056
2057" Some tests for buffer-local autocommands
2058func Test_buflocal_autocmd()
2059 let g:bname = ''
2060 edit xx
2061 au BufLeave <buffer> let g:bname = expand("%")
2062 " here, autocommand for xx should trigger.
2063 " but autocommand shall not apply to buffer named <buffer>.
2064 edit somefile
2065 call assert_equal('xx', g:bname)
2066 let g:bname = ''
2067 " here, autocommand shall be auto-deleted
2068 bwipe xx
2069 " autocmd should not trigger
2070 edit xx
2071 call assert_equal('', g:bname)
2072 " autocmd should not trigger
2073 edit somefile
2074 call assert_equal('', g:bname)
2075 enew
2076 unlet g:bname
2077endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002078
2079" Test for "*Cmd" autocommands
2080func Test_Cmd_Autocmds()
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002081 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx', 'D')
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002082
2083 enew!
2084 au BufReadCmd XtestA 0r Xxx|$del
2085 edit XtestA " will read text of Xxd instead
2086 call assert_equal('start of Xxx', getline(1))
2087
2088 au BufWriteCmd XtestA call append(line("$"), "write")
2089 write " will append a line to the file
2090 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002091 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002092 call assert_equal('write', getline(4))
2093
2094 " now we have:
2095 " 1 start of Xxx
2096 " 2 abc2
2097 " 3 end of Xxx
2098 " 4 write
2099
2100 au FileReadCmd XtestB '[r Xxx
2101 2r XtestB " will read Xxx below line 2 instead
2102 call assert_equal('start of Xxx', getline(3))
2103
2104 " now we have:
2105 " 1 start of Xxx
2106 " 2 abc2
2107 " 3 start of Xxx
2108 " 4 abc2
2109 " 5 end of Xxx
2110 " 6 end of Xxx
2111 " 7 write
2112
2113 au FileWriteCmd XtestC '[,']copy $
2114 normal 4GA1
2115 4,5w XtestC " will copy lines 4 and 5 to the end
2116 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002117 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002118 call assert_equal("end of Xxx", getline(9))
2119
2120 " now we have:
2121 " 1 start of Xxx
2122 " 2 abc2
2123 " 3 start of Xxx
2124 " 4 abc21
2125 " 5 end of Xxx
2126 " 6 end of Xxx
2127 " 7 write
2128 " 8 abc21
2129 " 9 end of Xxx
2130
2131 let g:lines = []
2132 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
2133 w >>XtestD " will add lines to 'lines'
2134 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02002135 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002136 call assert_equal(9, line('$'))
2137 call assert_equal('end of Xxx', getline('$'))
2138
2139 au BufReadCmd XtestE 0r Xxx|$del
2140 sp XtestE " split window with test.out
2141 call assert_equal('end of Xxx', getline(3))
2142
2143 let g:lines = []
2144 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
2145 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
2146 wall " will write other window to 'lines'
2147 call assert_equal(4, len(g:lines), g:lines)
2148 call assert_equal('asdf', g:lines[2])
2149
2150 au! BufReadCmd
2151 au! BufWriteCmd
2152 au! FileReadCmd
2153 au! FileWriteCmd
2154 au! FileAppendCmd
2155 %bwipe!
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01002156 enew!
2157endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01002158
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002159func s:ReadFile()
2160 setl noswapfile nomodified
2161 let filename = resolve(expand("<afile>:p"))
2162 execute 'read' fnameescape(filename)
2163 1d_
2164 exe 'file' fnameescape(filename)
2165 setl buftype=acwrite
2166endfunc
2167
2168func s:WriteFile()
2169 let filename = resolve(expand("<afile>:p"))
2170 setl buftype=
2171 noautocmd execute 'write' fnameescape(filename)
2172 setl buftype=acwrite
2173 setl nomodified
2174endfunc
2175
2176func Test_BufReadCmd()
2177 autocmd BufReadCmd *.test call s:ReadFile()
2178 autocmd BufWriteCmd *.test call s:WriteFile()
2179
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002180 call writefile(['one', 'two', 'three'], 'Xcmd.test', 'D')
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002181 edit Xcmd.test
2182 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
2183 normal! Gofour
2184 write
2185 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
2186
2187 bwipe!
Bram Moolenaar0fff4412020-03-29 16:06:29 +02002188 au! BufReadCmd
2189 au! BufWriteCmd
2190endfunc
2191
zeertzjq9c8f9462022-08-30 18:17:15 +01002192func Test_BufWriteCmd()
2193 autocmd BufWriteCmd Xbufwritecmd let g:written = 1
2194 new
2195 file Xbufwritecmd
2196 set buftype=acwrite
Bram Moolenaar6f14da12022-09-07 21:30:44 +01002197 call mkdir('Xbufwritecmd', 'D')
zeertzjq9c8f9462022-08-30 18:17:15 +01002198 write
2199 " BufWriteCmd should be triggered even if a directory has the same name
2200 call assert_equal(1, g:written)
zeertzjq9c8f9462022-08-30 18:17:15 +01002201 unlet g:written
2202 au! BufWriteCmd
2203 bwipe!
2204endfunc
2205
Bram Moolenaaraace2152017-11-05 16:23:10 +01002206func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01002207 exe a:start .. 'mark ['
2208 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01002209endfunc
2210
2211" Verify the effects of autocmds on '[ and ']
2212func Test_change_mark_in_autocmds()
2213 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01002214 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002215
2216 call SetChangeMarks(2, 3)
2217 write
2218 call assert_equal([1, 4], [line("'["), line("']")])
2219
2220 call SetChangeMarks(2, 3)
2221 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2222 write
2223 au! BufWritePre
2224
Bram Moolenaar14ddd222020-08-05 12:02:40 +02002225 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01002226 write XtestFilter
2227 write >> XtestFilter
2228
2229 call SetChangeMarks(2, 3)
2230 " Marks are set to the entire range of the write
2231 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2232 " '[ is adjusted to just before the line that will receive the filtered
2233 " data
2234 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
2235 " The filtered data is read into the buffer, and the source lines are
2236 " still present, so the range is after the source lines
2237 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
2238 %!cat XtestFilter
2239 " After the filtered data is read, the original lines are deleted
2240 call assert_equal([1, 8], [line("'["), line("']")])
2241 au! FilterWritePre,FilterReadPre,FilterReadPost
2242 undo
2243
2244 call SetChangeMarks(1, 4)
2245 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2246 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
2247 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2248 2,3!cat XtestFilter
2249 call assert_equal([2, 9], [line("'["), line("']")])
2250 au! FilterWritePre,FilterReadPre,FilterReadPost
2251 undo
2252
2253 call delete('XtestFilter')
2254 endif
2255
2256 call SetChangeMarks(1, 4)
2257 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
2258 2,3write Xtest2
2259 au! FileWritePre
2260
2261 call SetChangeMarks(2, 3)
2262 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
2263 write >> Xtest2
2264 au! FileAppendPre
2265
2266 call SetChangeMarks(1, 4)
2267 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
2268 2,3write >> Xtest2
2269 au! FileAppendPre
2270
2271 call SetChangeMarks(1, 1)
2272 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
2273 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
2274 3read Xtest2
2275 au! FileReadPre,FileReadPost
2276 undo
2277
2278 call SetChangeMarks(4, 4)
2279 " When the line is 0, it's adjusted to 1
2280 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2281 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
2282 0read Xtest2
2283 au! FileReadPre,FileReadPost
2284 undo
2285
2286 call SetChangeMarks(4, 4)
2287 " When the line is 0, it's adjusted to 1
2288 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
2289 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
2290 1read Xtest2
2291 au! FileReadPre,FileReadPost
2292 undo
2293
2294 bwipe!
2295 call delete('Xtest')
2296 call delete('Xtest2')
2297endfunc
2298
2299func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01002300 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01002301
2302 enew!
2303 call setline(1, ['a', 'b', 'c', 'd'])
2304
2305 let shelltemp = &shelltemp
2306 set shelltemp
2307
2308 let g:filter_au = 0
2309 au FilterWritePre * let g:filter_au += 1
2310 au FilterReadPre * let g:filter_au += 1
2311 au FilterReadPost * let g:filter_au += 1
2312 %!cat
2313 call assert_equal(3, g:filter_au)
2314
2315 if has('filterpipe')
2316 set noshelltemp
2317
2318 let g:filter_au = 0
2319 au FilterWritePre * let g:filter_au += 1
2320 au FilterReadPre * let g:filter_au += 1
2321 au FilterReadPost * let g:filter_au += 1
2322 %!cat
2323 call assert_equal(0, g:filter_au)
2324 endif
2325
2326 au! FilterWritePre,FilterReadPre,FilterReadPost
2327 let &shelltemp = shelltemp
2328 bwipe!
2329endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002330
2331func Test_TextYankPost()
2332 enew!
2333 call setline(1, ['foo'])
2334
2335 let g:event = []
2336 au TextYankPost * let g:event = copy(v:event)
2337
2338 call assert_equal({}, v:event)
2339 call assert_fails('let v:event = {}', 'E46:')
2340 call assert_fails('let v:event.mykey = 0', 'E742:')
2341
2342 norm "ayiw
2343 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002344 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
2345 \ regtype: 'v', visual: v:false, inclusive: v:true},
2346 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002347 norm y_
2348 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002349 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2350 \ visual: v:false, inclusive: v:false},
2351 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02002352 norm Vy
2353 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002354 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
2355 \ visual: v:true, inclusive: v:true},
2356 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002357 call feedkeys("\<C-V>y", 'x')
2358 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002359 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
2360 \ visual: v:true, inclusive: v:true},
2361 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002362 norm "xciwbar
2363 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002364 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
2365 \ visual: v:false, inclusive: v:true},
2366 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002367 norm "bdiw
2368 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002369 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
2370 \ visual: v:false, inclusive: v:true},
2371 \ g:event)
2372
2373 call setline(1, 'foobar')
2374 " exclusive motion
2375 norm $"ay0
2376 call assert_equal(
2377 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
2378 \ visual: v:false, inclusive: v:false},
2379 \ g:event)
2380 " inclusive motion
2381 norm 0"ay$
2382 call assert_equal(
2383 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
2384 \ visual: v:false, inclusive: v:true},
2385 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002386
2387 call assert_equal({}, v:event)
2388
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002389 if has('clipboard_working') && !has('gui_running')
2390 " Test that when the visual selection is automatically copied to clipboard
2391 " register a TextYankPost is emitted
2392 call setline(1, ['foobar'])
2393
2394 let @* = ''
2395 set clipboard=autoselect
2396 exe "norm! ggviw\<Esc>"
2397 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002398 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2399 \ regtype: 'v', visual: v:true, inclusive: v:false},
2400 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002401
2402 let @+ = ''
2403 set clipboard=autoselectplus
2404 exe "norm! ggviw\<Esc>"
2405 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002406 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2407 \ regtype: 'v', visual: v:true, inclusive: v:false},
2408 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002409
2410 set clipboard&vim
2411 endif
2412
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002413 au! TextYankPost
2414 unlet g:event
2415 bwipe!
2416endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002417
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002418func Test_autocommand_all_events()
2419 call assert_fails('au * * bwipe', 'E1155:')
2420 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002421 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002422endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002423
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002424func Test_autocmd_user()
2425 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2426 doautocmd User MyEvent
2427 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2428 au! User
2429 unlet s:res
2430endfunc
2431
Bram Moolenaar3b014be2022-11-13 17:53:46 +00002432func Test_autocmd_user_clear_group()
2433 CheckRunVimInTerminal
2434
2435 let lines =<< trim END
2436 autocmd! User
2437 for i in range(1, 999)
2438 exe 'autocmd User ' .. 'Foo' .. i .. ' bar'
2439 endfor
2440 au CmdlineLeave : call timer_start(0, {-> execute('autocmd! User')})
2441 END
2442 call writefile(lines, 'XautoUser', 'D')
2443 let buf = RunVimInTerminal('-S XautoUser', {'rows': 10})
2444
2445 " this was using freed memory
2446 call term_sendkeys(buf, ":autocmd User\<CR>")
2447 call TermWait(buf, 50)
2448 call term_sendkeys(buf, "G")
2449
2450 call StopVimInTerminal(buf)
2451endfunc
2452
Bram Moolenaaref2c3252022-11-25 16:31:51 +00002453func Test_autocmd_CmdlineLeave_unlet()
2454 CheckRunVimInTerminal
2455
2456 let lines =<< trim END
2457 for i in range(1, 999)
2458 exe 'let g:var' .. i '=' i
2459 endfor
2460 au CmdlineLeave : call timer_start(0, {-> execute('unlet g:var990')})
2461 END
2462 call writefile(lines, 'XleaveUnlet', 'D')
2463 let buf = RunVimInTerminal('-S XleaveUnlet', {'rows': 10})
2464
2465 " this was using freed memory
2466 call term_sendkeys(buf, ":let g:\<CR>")
2467 call TermWait(buf, 50)
2468 call term_sendkeys(buf, "G")
2469 call TermWait(buf, 50)
2470 call term_sendkeys(buf, "\<CR>") " for the hit-enter prompt
2471
2472 call StopVimInTerminal(buf)
2473endfunc
2474
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002475function s:Before_test_dirchanged()
2476 augroup test_dirchanged
2477 autocmd!
2478 augroup END
2479 let s:li = []
2480 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002481 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002482 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002483 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002484 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002485endfunc
2486
2487function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002488 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002489 call delete(s:dir_foo, 'd')
2490 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002491 augroup test_dirchanged
2492 autocmd!
2493 augroup END
2494endfunc
2495
2496function Test_dirchanged_global()
2497 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002498 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002499 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2500 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002501 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002502 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002503 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002504 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002505 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002506 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002507 call assert_equal(expected, s:li)
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002508
2509 exe 'cd ' .. s:dir_foo
2510 exe 'cd ' .. s:dir_bar
2511 autocmd! test_dirchanged DirChanged global let g:result = expand("<afile>")
2512 cd -
Bram Moolenaardb77c492022-06-12 23:26:50 +01002513 call assert_equal(s:dir_foo, substitute(g:result, '\\', '/', 'g'))
Bram Moolenaard8c9d322022-06-12 11:49:16 +01002514
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002515 call s:After_test_dirchanged()
2516endfunc
2517
2518function Test_dirchanged_local()
2519 call s:Before_test_dirchanged()
2520 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2521 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002522 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002523 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002524 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002525 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002526 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002527 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002528 call s:After_test_dirchanged()
2529endfunc
2530
2531function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002532 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002533 call s:Before_test_dirchanged()
2534 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002535 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002536 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2537 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2538 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002539 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002540 call assert_equal([], s:li)
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01002541 exe 'edit ' . s:dir_foo . '/Xautofile'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002542 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002543 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2544 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002545 set noacd
2546 bwipe!
2547 call s:After_test_dirchanged()
2548endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002549
2550" Test TextChangedI and TextChangedP
2551func Test_ChangedP()
2552 new
2553 call setline(1, ['foo', 'bar', 'foobar'])
2554 call test_override("char_avail", 1)
2555 set complete=. completeopt=menuone
2556
2557 func! TextChangedAutocmd(char)
2558 let g:autocmd .= a:char
2559 endfunc
2560
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002561 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002562 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2563 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2564 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2565
2566 call cursor(3, 1)
2567 let g:autocmd = ''
2568 call feedkeys("o\<esc>", 'tnix')
Evgeni Chasnovskid7ae2632023-10-15 09:59:00 +02002569 " `TextChangedI` triggers only if text is actually changed in Insert mode
2570 call assert_equal('', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002571
2572 let g:autocmd = ''
2573 call feedkeys("Sf", 'tnix')
Evgeni Chasnovskid7ae2632023-10-15 09:59:00 +02002574 call assert_equal('I', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002575
2576 let g:autocmd = ''
2577 call feedkeys("Sf\<C-N>", 'tnix')
Evgeni Chasnovskid7ae2632023-10-15 09:59:00 +02002578 call assert_equal('IP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002579
2580 let g:autocmd = ''
2581 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
Evgeni Chasnovskid7ae2632023-10-15 09:59:00 +02002582 call assert_equal('IPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002583
2584 let g:autocmd = ''
2585 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
Evgeni Chasnovskid7ae2632023-10-15 09:59:00 +02002586 call assert_equal('IPPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002587
2588 let g:autocmd = ''
2589 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
Evgeni Chasnovskid7ae2632023-10-15 09:59:00 +02002590 call assert_equal('IPPPP', g:autocmd)
Bram Moolenaar5a093432018-02-10 18:15:19 +01002591
2592 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2593 " TODO: how should it handle completeopt=noinsert,noselect?
2594
2595 " CleanUp
2596 call test_override("char_avail", 0)
2597 au! TextChanged
2598 au! TextChangedI
2599 au! TextChangedP
2600 delfu TextChangedAutocmd
2601 unlet! g:autocmd
2602 set complete&vim completeopt&vim
2603
2604 bw!
2605endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002606
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002607let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002608func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002609 if !g:setline_handled
2610 call setline(1, "(x)")
2611 let g:setline_handled = v:true
2612 endif
2613endfunc
2614
2615func Test_TextChangedI_with_setline()
2616 new
2617 call test_override('char_avail', 1)
2618 autocmd TextChangedI <buffer> call SetLineOne()
2619 call feedkeys("i(\<CR>\<Esc>", 'tx')
2620 call assert_equal('(', getline(1))
2621 call assert_equal('x)', getline(2))
2622 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002623 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002624 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002625
Bram Moolenaarca34db32022-01-20 11:17:18 +00002626 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002627 bwipe!
2628endfunc
2629
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002630func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002631 CheckFeature terminal
2632 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002633 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002634 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002635
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002636 " Prepare file for TextChanged event.
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002637 call writefile([''], 'Xchanged.txt', 'D')
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002638 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2639 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002640 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002641 " In ConPTY, there is additional character which is drawn up to the width of
2642 " the screen.
2643 if has('conpty')
2644 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2645 else
2646 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2647 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002648 " It's only adding autocmd, so that no event occurs.
2649 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2650 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002651 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002652 call assert_equal([''], readfile('Xchanged.txt'))
2653
2654 " clean up
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002655 bwipe!
2656endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002657
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002658func Test_autocmd_nested()
2659 let g:did_nested = 0
2660 augroup Testing
2661 au WinNew * edit somefile
2662 au BufNew * let g:did_nested = 1
2663 augroup END
2664 split
2665 call assert_equal(0, g:did_nested)
2666 close
2667 bwipe! somefile
2668
2669 " old nested argument still works
2670 augroup Testing
2671 au!
2672 au WinNew * nested edit somefile
2673 au BufNew * let g:did_nested = 1
2674 augroup END
2675 split
2676 call assert_equal(1, g:did_nested)
2677 close
2678 bwipe! somefile
2679
2680 " New ++nested argument works
2681 augroup Testing
2682 au!
2683 au WinNew * ++nested edit somefile
2684 au BufNew * let g:did_nested = 1
2685 augroup END
2686 split
2687 call assert_equal(1, g:did_nested)
2688 close
2689 bwipe! somefile
2690
Bram Moolenaarf0775142022-03-04 20:10:38 +00002691 " nested without ++ does not work in Vim9 script
2692 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2693
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002694 augroup Testing
2695 au!
2696 augroup END
2697
2698 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2699 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2700endfunc
2701
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002702func Test_autocmd_nested_cursor_invalid()
2703 set laststatus=0
2704 copen
2705 cclose
2706 call setline(1, ['foo', 'bar', 'baz'])
2707 3
2708 augroup nested_inv
2709 autocmd User foo ++nested copen
2710 autocmd BufAdd * let &laststatus = 2 - &laststatus
2711 augroup END
2712 doautocmd User foo
2713
2714 augroup nested_inv
2715 au!
2716 augroup END
2717 set laststatus&
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002718 cclose
Bram Moolenaar5fa9f232022-07-23 09:06:48 +01002719 bwipe!
2720endfunc
2721
Bram Moolenaar3d6ee8b2022-07-27 15:23:35 +01002722func Test_autocmd_nested_keeps_cursor_pos()
2723 enew
2724 call setline(1, 'foo')
2725 autocmd User foo ++nested normal! $a
2726 autocmd InsertLeave * :
2727 doautocmd User foo
2728 call assert_equal([0, 1, 3, 0], getpos('.'))
2729
2730 bwipe!
2731endfunc
2732
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002733func Test_autocmd_nested_switch_window()
2734 " run this in a separate Vim so that SafeState works
2735 CheckRunVimInTerminal
2736
2737 let lines =<< trim END
2738 vim9script
2739 ['()']->writefile('Xautofile')
2740 autocmd VimEnter * ++nested edit Xautofile | split
2741 autocmd BufReadPost * autocmd SafeState * ++once foldclosed('.')
2742 autocmd WinEnter * matchadd('ErrorMsg', 'pat')
2743 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002744 call writefile(lines, 'Xautoscript', 'D')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002745 let buf = RunVimInTerminal('-S Xautoscript', {'rows': 10})
2746 call VerifyScreenDump(buf, 'Test_autocmd_nested_switch', {})
2747
2748 call StopVimInTerminal(buf)
2749 call delete('Xautofile')
Bram Moolenaarb03950f2022-07-26 13:47:13 +01002750endfunc
2751
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002752func Test_autocmd_once()
2753 " Without ++once WinNew triggers twice
2754 let g:did_split = 0
2755 augroup Testing
2756 au WinNew * let g:did_split += 1
2757 augroup END
2758 split
2759 split
2760 call assert_equal(2, g:did_split)
2761 call assert_true(exists('#WinNew'))
2762 close
2763 close
2764
2765 " With ++once WinNew triggers once
2766 let g:did_split = 0
2767 augroup Testing
2768 au!
2769 au WinNew * ++once let g:did_split += 1
2770 augroup END
2771 split
2772 split
2773 call assert_equal(1, g:did_split)
2774 call assert_false(exists('#WinNew'))
2775 close
2776 close
2777
2778 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2779endfunc
2780
Bram Moolenaara68e5952019-04-25 22:22:01 +02002781func Test_autocmd_bufreadpre()
2782 new
2783 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002784 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002785 w! XAutocmdBufReadPre.txt
2786 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002787 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002788 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002789 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002790 wincmd p
2791 let g:wsv1 = winsaveview()
2792 wincmd p
2793 let g:wsv2 = winsaveview()
2794 " triggers BufReadPre, should not move the cursor in either window
2795 " The topline may change one line in a large window.
2796 edit
2797 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2798 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2799 call assert_equal(2, b:bufreadpre)
2800 wincmd p
2801 call assert_equal(g:wsv1.topline, winsaveview().topline)
2802 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2803 call assert_equal(2, b:bufreadpre)
2804 " Now set the cursor position in an BufReadPre autocommand
2805 " (even though the position will be invalid, this should make Vim reset the
2806 " cursor position in the other window.
2807 wincmd p
2808 set cpo+=g
2809 " won't do anything, but try to set the cursor on an invalid lnum
2810 autocmd BufReadPre <buffer> :norm! 70gg
2811 " triggers BufReadPre, should not move the cursor in either window
2812 e
2813 call assert_equal(1, winsaveview().topline)
2814 call assert_equal(1, winsaveview().lnum)
2815 call assert_equal(3, b:bufreadpre)
2816 wincmd p
2817 call assert_equal(g:wsv1.topline, winsaveview().topline)
2818 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2819 call assert_equal(3, b:bufreadpre)
2820 close
2821 close
2822 call delete('XAutocmdBufReadPre.txt')
2823 set cpo-=g
2824endfunc
2825
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002826" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002827
2828" Tests for the following autocommands:
2829" - FileWritePre writing a compressed file
2830" - FileReadPost reading a compressed file
2831" - BufNewFile reading a file template
2832" - BufReadPre decompressing the file to be read
2833" - FilterReadPre substituting characters in the temp file
2834" - FilterReadPost substituting characters after filtering
2835" - FileReadPre set options for decompression
2836" - FileReadPost decompress the file
2837func Test_ReadWrite_Autocmds()
2838 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002839 CheckUnix
2840 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002841
2842 " Make $GZIP empty, "-v" would cause trouble.
2843 let $GZIP = ""
2844
2845 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2846 " being modified outside of Vim (noticed on Solaris).
2847 au FileChangedShell * echo 'caught FileChangedShell'
2848
2849 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2850 augroup Test1
2851 au!
2852 au FileWritePre *.gz '[,']!gzip
2853 au FileWritePost *.gz undo
2854 au FileReadPost *.gz '[,']!gzip -d
2855 augroup END
2856
2857 new
2858 set bin
2859 call append(0, [
2860 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2861 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2862 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2863 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2864 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2865 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2866 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2867 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2868 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2869 \ ])
2870 1,9write! Xtestfile.gz
2871 enew! | close
2872
2873 new
2874 " Read and decompress the testfile
2875 0read Xtestfile.gz
2876 call assert_equal([
2877 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2878 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2879 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2880 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2881 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2882 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2883 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2884 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2885 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2886 \ ], getline(1, 9))
2887 enew! | close
2888
2889 augroup Test1
2890 au!
2891 augroup END
2892
2893 " Test for the FileAppendPre and FileAppendPost autocmds
2894 augroup Test2
2895 au!
2896 au BufNewFile *.c read Xtest.c
2897 au FileAppendPre *.out '[,']s/new/NEW/
2898 au FileAppendPost *.out !cat Xtest.c >> test.out
2899 augroup END
2900
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01002901 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c', 'D')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002902 new foo.c " should load Xtest.c
2903 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2904 w! >> test.out " append it to the output file
2905
2906 let contents = readfile('test.out')
2907 call assert_equal(' * Here is a NEW .c file', contents[2])
2908 call assert_equal(' * Here is a new .c file', contents[5])
2909
2910 call delete('test.out')
2911 enew! | close
2912 augroup Test2
2913 au!
2914 augroup END
2915
2916 " Test for the BufReadPre and BufReadPost autocmds
2917 augroup Test3
2918 au!
2919 " setup autocommands to decompress before reading and re-compress
2920 " afterwards
2921 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2922 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2923 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2924 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2925 augroup END
2926
2927 e! Xtestfile.gz " Edit compressed file
2928 call assert_equal([
2929 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2930 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2931 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2932 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2933 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2934 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2935 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2936 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2937 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2938 \ ], getline(1, 9))
2939
2940 w! >> test.out " Append it to the output file
2941
2942 augroup Test3
2943 au!
2944 augroup END
2945
2946 " Test for the FilterReadPre and FilterReadPost autocmds.
2947 set shelltemp " need temp files here
2948 augroup Test4
2949 au!
2950 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2951 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2952 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2953 au FilterReadPost *.out '[,']s/x/X/g
2954 augroup END
2955
2956 e! test.out " Edit the output file
2957 1,$!cat
2958 call assert_equal([
2959 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2960 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2961 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2962 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2963 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2964 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2965 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2966 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2967 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2968 \ ], getline(1, 9))
2969 call assert_equal([
2970 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2971 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2972 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2973 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2974 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2975 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2976 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2977 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2978 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2979 \ ], readfile('test.out'))
2980
2981 augroup Test4
2982 au!
2983 augroup END
2984 set shelltemp&vim
2985
2986 " Test for the FileReadPre and FileReadPost autocmds.
2987 augroup Test5
2988 au!
2989 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2990 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2991 au FileReadPost *.gz '[,']s/l/L/
2992 augroup END
2993
2994 new
2995 0r Xtestfile.gz " Read compressed file
2996 call assert_equal([
2997 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2998 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2999 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
3000 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3001 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
3002 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3003 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
3004 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3005 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
3006 \ ], getline(1, 9))
3007 call assert_equal([
3008 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
3009 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3010 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
3011 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3012 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
3013 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3014 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
3015 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
3016 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
3017 \ ], readfile('Xtestfile.gz'))
3018
3019 augroup Test5
3020 au!
3021 augroup END
3022
3023 au! FileChangedShell
3024 call delete('Xtestfile.gz')
Bram Moolenaar69ea5872019-04-25 20:29:00 +02003025 call delete('test.out')
3026endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02003027
3028func Test_throw_in_BufWritePre()
3029 new
3030 call setline(1, ['one', 'two', 'three'])
3031 call assert_false(filereadable('Xthefile'))
3032 augroup throwing
3033 au BufWritePre X* throw 'do not write'
3034 augroup END
3035 try
3036 w Xthefile
3037 catch
3038 let caught = 1
3039 endtry
3040 call assert_equal(1, caught)
3041 call assert_false(filereadable('Xthefile'))
3042
3043 bwipe!
3044 au! throwing
3045endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003046
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003047func Test_autocmd_in_try_block()
Bram Moolenaar6f14da12022-09-07 21:30:44 +01003048 call mkdir('Xintrydir', 'R')
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003049 au BufEnter * let g:fname = expand('%')
3050 try
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003051 edit Xintrydir/
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003052 endtry
Bram Moolenaar3b0d70f2022-08-29 22:31:20 +01003053 call assert_match('Xintrydir', g:fname)
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003054
3055 unlet g:fname
3056 au! BufEnter
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02003057endfunc
3058
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003059func Test_autocmd_SafeState()
3060 CheckRunVimInTerminal
3061
3062 let lines =<< trim END
3063 let g:safe = 0
3064 let g:again = ''
3065 au SafeState * let g:safe += 1
3066 au SafeStateAgain * let g:again ..= 'x'
3067 func CallTimer()
3068 call timer_start(10, {id -> execute('let g:again ..= "t"')})
3069 endfunc
3070 END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003071 call writefile(lines, 'XSafeState', 'D')
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003072 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
3073
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003074 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003075 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003076 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003077 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003078
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003079 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003080 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01003081 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003082
3083 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003084 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02003085 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003086 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003087 call term_sendkeys(buf, ":echo g:again\<CR>")
3088 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
3089
3090 call StopVimInTerminal(buf)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02003091endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02003092
3093func Test_autocmd_CmdWinEnter()
3094 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01003095
Bram Moolenaar23324a02019-10-01 17:39:04 +02003096 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00003097 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02003098 let b:dummy_var = 'This is a dummy'
3099 autocmd CmdWinEnter * quit
3100 let winnr = winnr('$')
3101 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01003102 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02003103 call writefile(lines, filename)
3104 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
3105
3106 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02003107 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003108 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01003109 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02003110 call term_sendkeys(buf, ":echo &buftype\<cr>")
3111 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
3112 call term_sendkeys(buf, ":echo winnr\<cr>")
3113 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
3114
3115 " clean up
3116 call StopVimInTerminal(buf)
3117 call delete(filename)
3118endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02003119
3120func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01003121 CheckFeature quickfix
3122
Bram Moolenaarec66c412019-10-11 21:19:13 +02003123 pedit xx
3124 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003125 augroup winenter
3126 au WinEnter * if winnr('$') > 2 | quit | endif
3127 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02003128 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003129
3130 augroup winenter
3131 au! WinEnter
3132 augroup END
3133
3134 bwipe xx
3135 bwipe x
3136 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02003137endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003138
3139func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01003140 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01003141 edit! Xtest
3142 call setline(1, ['a', 'b', 'c', 'd'])
3143
3144 " :lockmarks preserves the marks
3145 call SetChangeMarks(2, 3)
3146 lockmarks write
3147 call assert_equal([2, 3], [line("'["), line("']")])
3148
3149 " *WritePre autocmds get the correct line range, but lockmarks preserves the
3150 " original values for the user
3151 augroup lockmarks
3152 au!
3153 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
3154 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
3155 augroup END
3156
3157 lockmarks write
3158 call assert_equal([2, 3], [line("'["), line("']")])
3159
3160 if executable('cat')
3161 lockmarks %!cat
3162 call assert_equal([2, 3], [line("'["), line("']")])
3163 endif
3164
3165 lockmarks 3,4write Xtest2
3166 call assert_equal([2, 3], [line("'["), line("']")])
3167
3168 au! lockmarks
3169 augroup! lockmarks
3170 call delete('Xtest')
3171 call delete('Xtest2')
3172endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01003173
3174func Test_FileType_spell()
3175 if !isdirectory('/tmp')
3176 throw "Skipped: requires /tmp directory"
3177 endif
3178
3179 " this was crashing with an invalid free()
3180 setglobal spellfile=/tmp/en.utf-8.add
3181 augroup crash
3182 autocmd!
3183 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
3184 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
3185 autocmd FileType anotherfiletype setlocal spell
3186 augroup END
3187 func! NoCrash() abort
3188 edit /tmp/crashfile
3189 endfunc
3190 call NoCrash()
3191
3192 au! crash
3193 setglobal spellfile=
3194endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003195
Bram Moolenaaref976322022-09-28 11:48:30 +01003196" this was wiping out the current buffer and using freed memory
3197func Test_SpellFileMissing_bwipe()
3198 next 0
3199 au SpellFileMissing 0 bwipe
3200 call assert_fails('set spell spelllang=0', 'E937:')
3201
3202 au! SpellFileMissing
Bram Moolenaar0a60f792022-11-19 21:18:11 +00003203 set nospell spelllang=en
Bram Moolenaaref976322022-09-28 11:48:30 +01003204 bwipe
3205endfunc
3206
Bram Moolenaar406cd902020-02-18 21:54:41 +01003207" Test closing a window or editing another buffer from a FileChangedRO handler
3208" in a readonly buffer
3209func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003210 call test_override('ui_delay', 10)
3211
Bram Moolenaar406cd902020-02-18 21:54:41 +01003212 augroup FileChangedROTest
3213 au!
3214 autocmd FileChangedRO * quit
3215 augroup END
3216 new
3217 set readonly
3218 call assert_fails('normal i', 'E788:')
3219 close
3220 augroup! FileChangedROTest
3221
3222 augroup FileChangedROTest
3223 au!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003224 autocmd FileChangedRO * edit Xrofile
Bram Moolenaar406cd902020-02-18 21:54:41 +01003225 augroup END
3226 new
3227 set readonly
3228 call assert_fails('normal i', 'E788:')
3229 close
3230 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003231 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01003232endfunc
3233
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003234func LogACmd()
3235 call add(g:logged, line('$'))
3236endfunc
3237
3238func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01003239 CheckNotGui
3240
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01003241 enew!
3242 tabnew
3243 call setline(1, ['a', 'b', 'c', 'd'])
3244 $
3245 au TermChanged * call LogACmd()
3246 let g:logged = []
3247 let term_save = &term
3248 set term=xterm
3249 call assert_equal([1, 4], g:logged)
3250
3251 au! TermChanged
3252 let &term = term_save
3253 bwipe!
3254endfunc
3255
Bram Moolenaare3284872020-03-19 13:55:03 +01003256" Test for FileReadCmd autocmd
3257func Test_autocmd_FileReadCmd()
3258 func ReadFileCmd()
3259 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
3260 endfunc
3261 augroup FileReadCmdTest
3262 au!
3263 au FileReadCmd Xtest call ReadFileCmd()
3264 augroup END
3265
3266 new
3267 read ++bin Xtest
3268 read ++nobin Xtest
3269 read ++edit Xtest
3270 read ++bad=keep Xtest
3271 read ++bad=drop Xtest
3272 read ++bad=- Xtest
3273 read ++ff=unix Xtest
3274 read ++ff=dos Xtest
3275 read ++ff=mac Xtest
3276 read ++enc=utf-8 Xtest
3277
3278 call assert_equal(['',
3279 \ 'v:cmdarg = ++bin',
3280 \ 'v:cmdarg = ++nobin',
3281 \ 'v:cmdarg = ++edit',
3282 \ 'v:cmdarg = ++bad=keep',
3283 \ 'v:cmdarg = ++bad=drop',
3284 \ 'v:cmdarg = ++bad=-',
3285 \ 'v:cmdarg = ++ff=unix',
3286 \ 'v:cmdarg = ++ff=dos',
3287 \ 'v:cmdarg = ++ff=mac',
3288 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
3289
Bram Moolenaar23526d22022-12-05 15:50:41 +00003290 bwipe!
Bram Moolenaare3284872020-03-19 13:55:03 +01003291 augroup FileReadCmdTest
3292 au!
3293 augroup END
3294 delfunc ReadFileCmd
3295endfunc
3296
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003297" Test for passing invalid arguments to autocmd
3298func Test_autocmd_invalid_args()
3299 " Additional character after * for event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003300 call assert_fails('autocmd *a Xinvfile set ff=unix', 'E215:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003301 augroup Test
3302 augroup END
3303 " Invalid autocmd event
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003304 call assert_fails('autocmd Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003305 " Invalid autocmd event in a autocmd group
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003306 call assert_fails('autocmd Test Bufabc Xinvfile set ft=vim', 'E216:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003307 augroup! Test
3308 " Execute all autocmds
3309 call assert_fails('doautocmd * BufEnter', 'E217:')
3310 call assert_fails('augroup! x1a2b3', 'E367:')
3311 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02003312 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003313endfunc
3314
3315" Test for deep nesting of autocmds
3316func Test_autocmd_deep_nesting()
Bram Moolenaar61abe7d2022-08-30 21:46:08 +01003317 autocmd BufEnter Xdeepfile doautocmd BufEnter Xdeepfile
3318 call assert_fails('doautocmd BufEnter Xdeepfile', 'E218:')
3319 autocmd! BufEnter Xdeepfile
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02003320endfunc
3321
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003322" Tests for SigUSR1 autocmd event, which is only available on posix systems.
3323func Test_autocmd_sigusr1()
3324 CheckUnix
Bram Moolenaar0056ca72022-09-23 21:26:39 +01003325 " FIXME: should this work on MacOS M1?
3326 CheckNotMacM1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02003327 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02003328
3329 let g:sigusr1_passed = 0
3330 au SigUSR1 * let g:sigusr1_passed = 1
3331 call system('/bin/kill -s usr1 ' . getpid())
3332 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
3333
3334 au! SigUSR1
3335 unlet g:sigusr1_passed
3336endfunc
3337
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003338" Test for BufReadPre autocmd deleting the file
3339func Test_BufReadPre_delfile()
3340 augroup TestAuCmd
3341 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003342 autocmd BufReadPre XbufreadPre call delete('XbufreadPre')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003343 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003344 call writefile([], 'XbufreadPre', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003345 call assert_fails('new XbufreadPre', 'E200:')
3346 call assert_equal('XbufreadPre', @%)
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003347 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003348
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003349 augroup TestAuCmd
3350 au!
3351 augroup END
3352 close!
3353endfunc
3354
3355" Test for BufReadPre autocmd changing the current buffer
3356func Test_BufReadPre_changebuf()
3357 augroup TestAuCmd
3358 au!
Bram Moolenaare7cda972022-08-29 11:02:59 +01003359 autocmd BufReadPre Xchangebuf edit Xsomeotherfile
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003360 augroup END
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003361 call writefile([], 'Xchangebuf', 'D')
Bram Moolenaare7cda972022-08-29 11:02:59 +01003362 call assert_fails('new Xchangebuf', 'E201:')
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003363 call assert_equal('Xsomeotherfile', @%)
3364 call assert_equal(1, &readonly)
Bram Moolenaare1f3ab72022-09-04 21:29:08 +01003365
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003366 augroup TestAuCmd
3367 au!
3368 augroup END
3369 close!
3370endfunc
3371
3372" Test for BufWipeouti autocmd changing the current buffer when reading a file
3373" in an empty buffer with 'f' flag in 'cpo'
3374func Test_BufDelete_changebuf()
3375 new
3376 augroup TestAuCmd
3377 au!
3378 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
3379 augroup END
3380 let save_cpo = &cpo
3381 set cpo+=f
Bram Moolenaarb18b4962022-09-02 21:55:50 +01003382 call assert_fails('r Xchangebuf', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02003383 call assert_equal('somefile', @%)
3384 let &cpo = save_cpo
3385 augroup TestAuCmd
3386 au!
3387 augroup END
3388 close!
3389endfunc
3390
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003391" Test for the temporary internal window used to execute autocmds
3392func Test_autocmd_window()
3393 %bw!
3394 edit one.txt
3395 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003396 vnew three.txt
3397 tabnew four.txt
3398 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003399 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02003400 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003401 au!
3402 au BufEnter * call add(g:blist, [expand('<afile>'),
3403 \ win_gettype(bufwinnr(expand('<afile>')))])
3404 augroup END
3405
3406 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01003407 call assert_equal([
3408 \ ['one.txt', 'autocmd'],
3409 \ ['two.txt', ''],
3410 \ ['four.txt', 'autocmd'],
3411 \ ['three.txt', ''],
3412 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003413
Bram Moolenaar832adf92020-06-25 19:01:36 +02003414 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003415 au!
3416 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02003417 augroup! aucmd_win_test1
3418 %bw!
3419endfunc
3420
3421" Test for trying to close the temporary window used for executing an autocmd
3422func Test_close_autocmd_window()
3423 %bw!
3424 edit one.txt
3425 tabnew two.txt
3426 augroup aucmd_win_test2
3427 au!
3428 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
3429 augroup END
3430
3431 call assert_fails('doautoall BufEnter', 'E813:')
3432
3433 augroup aucmd_win_test2
3434 au!
3435 augroup END
3436 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02003437 %bwipe!
3438endfunc
3439
3440" Test for trying to close the tab that has the temporary window for exeucing
3441" an autocmd.
3442func Test_close_autocmd_tab()
3443 edit one.txt
3444 tabnew two.txt
3445 augroup aucmd_win_test
3446 au!
3447 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
3448 augroup END
3449
3450 call assert_fails('doautoall BufEnter', 'E813:')
3451
3452 tabonly
3453 augroup aucmd_win_test
3454 au!
3455 augroup END
3456 augroup! aucmd_win_test
3457 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02003458endfunc
3459
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003460func Test_Visual_doautoall_redraw()
3461 call setline(1, ['a', 'b'])
Bram Moolenaar94722c52023-01-28 19:19:03 +00003462 new
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00003463 wincmd p
3464 call feedkeys("G\<C-V>", 'txn')
3465 autocmd User Explode ++once redraw
3466 doautoall User Explode
3467 %bwipe!
3468endfunc
3469
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01003470" This was using freed memory.
3471func Test_BufNew_arglocal()
3472 arglocal
3473 au BufNew * arglocal
3474 call assert_fails('drop xx', 'E1156:')
3475
3476 au! BufNew
3477endfunc
3478
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003479func Test_autocmd_closes_window()
3480 au BufNew,BufWinLeave * e %e
3481 file yyy
3482 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003483 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003484
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003485 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01003486 au! BufNew
3487 au! BufWinLeave
3488endfunc
3489
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003490func Test_autocmd_quit_psearch()
3491 sn aa bb
3492 augroup aucmd_win_test
3493 au!
3494 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3495 augroup END
3496 ps /
3497
3498 augroup aucmd_win_test
3499 au!
3500 augroup END
zeertzjq7851c692022-04-21 11:14:01 +01003501 new
3502 pclose
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01003503endfunc
3504
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003505" Fuzzer found some strange combination that caused a crash.
3506func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003507 " For unknown reason this hangs on MS-Windows
3508 CheckNotMSWindows
3509
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003510 augroup aucmd_normal_test
3511 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3512 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003513 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003514 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003515 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003516 normal G
3517
3518 augroup aucmd_normal_test
3519 au!
3520 augroup END
3521endfunc
3522
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003523func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003524 " For unknown reason this hangs on MS-Windows
3525 CheckNotMSWindows
3526
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003527 au BufWinLeave * nested q
3528 call assert_fails("norm 7q?\n", 'E855:')
3529
3530 au! BufWinLeave
3531 new
3532 only
3533endfunc
3534
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003535func Test_autocmd_vimgrep()
3536 augroup aucmd_vimgrep
Charlie Grovesfef44852022-04-19 16:24:12 +01003537 au QuickfixCmdPre,BufNew,BufReadCmd * sb
zeertzjq7851c692022-04-21 11:14:01 +01003538 au QuickfixCmdPre,BufNew,BufReadCmd * q9
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003539 augroup END
Bram Moolenaardd07c022021-02-07 13:32:46 +01003540 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003541
3542 augroup aucmd_vimgrep
3543 au!
3544 augroup END
3545endfunc
3546
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003547func Test_autocmd_with_block()
3548 augroup block_testing
3549 au BufReadPost *.xml {
3550 setlocal matchpairs+=<:>
3551 /<start
3552 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003553 au CursorHold * {
3554 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3555 g:gotSafeState = 77
3556 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003557 augroup END
3558
3559 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3560 call assert_equal(expected, execute('au BufReadPost *.xml'))
3561
Bram Moolenaar63b91732021-08-05 20:40:03 +02003562 doautocmd CursorHold
3563 call assert_equal(77, g:gotSafeState)
3564 unlet g:gotSafeState
3565
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003566 augroup block_testing
3567 au!
Bram Moolenaar75ebd2a2022-06-03 17:39:46 +01003568 autocmd CursorHold * {
3569 if true
3570 # comment
3571 && true
3572
3573 && true
3574 g:done = 'yes'
3575 endif
3576 }
3577 augroup END
3578 doautocmd CursorHold
3579 call assert_equal('yes', g:done)
3580
3581 unlet g:done
3582 augroup block_testing
3583 au!
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003584 augroup END
3585endfunc
3586
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003587" Test TextChangedI and TextChanged
3588func Test_Changed_ChangedI()
3589 new
3590 call test_override("char_avail", 1)
3591 let [g:autocmd_i, g:autocmd_n] = ['','']
3592
3593 func! TextChangedAutocmdI(char)
3594 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3595 endfunc
3596
3597 augroup Test_TextChanged
3598 au!
3599 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3600 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3601 augroup END
3602
3603 call feedkeys("ifoo\<esc>", 'tnix')
3604 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3605 " requires running Vim in a terminal window.
3606 " call assert_equal('N3', g:autocmd_n)
3607 call assert_equal('I3', g:autocmd_i)
3608
3609 call feedkeys("yyp", 'tnix')
3610 " TODO: Test test does not seem to trigger TextChanged autocommand.
3611 " call assert_equal('N4', g:autocmd_n)
3612 call assert_equal('I3', g:autocmd_i)
3613
Evgeni Chasnovskid7ae2632023-10-15 09:59:00 +02003614 " TextChangedI should only trigger if change was done in Insert mode
3615 let g:autocmd_i = ''
3616 call feedkeys("yypi\<esc>", 'tnix')
3617 call assert_equal('', g:autocmd_i)
3618
3619 " TextChanged should only trigger if change was done in Normal mode
3620 let g:autocmd_n = ''
3621 call feedkeys("ibar\<esc>", 'tnix')
3622 call assert_equal('', g:autocmd_n)
3623
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003624 " CleanUp
3625 call test_override("char_avail", 0)
3626 au! TextChanged <buffer>
3627 au! TextChangedI <buffer>
3628 augroup! Test_TextChanged
3629 delfu TextChangedAutocmdI
3630 unlet! g:autocmd_i g:autocmd_n
3631
3632 bw!
3633endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003634
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003635func Test_closing_autocmd_window()
3636 let lines =<< trim END
3637 edit Xa.txt
3638 tabnew Xb.txt
3639 autocmd BufEnter Xa.txt unhide 1
3640 doautoall BufEnter
3641 END
3642 call v9.CheckScriptFailure(lines, 'E814:')
3643 au! BufEnter
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003644 bwipe Xa.txt
3645 bwipe Xb.txt
3646endfunc
3647
zeertzjq46bdae02023-09-24 23:16:08 +02003648func Test_switch_window_in_autocmd_window()
3649 edit Xa.txt
3650 tabnew Xb.txt
3651 autocmd BufEnter Xa.txt wincmd w
3652 doautoall BufEnter
3653 au! BufEnter
3654 bwipe Xa.txt
3655 call assert_false(bufexists('Xa.txt'))
3656 bwipe Xb.txt
3657 call assert_false(bufexists('Xb.txt'))
3658endfunc
3659
Bram Moolenaar347538f2022-03-26 16:28:06 +00003660func Test_bufwipeout_changes_window()
3661 " This should not crash, but we don't have any expectations about what
3662 " happens, changing window in BufWipeout has unpredictable results.
3663 tabedit
3664 let g:window_id = win_getid()
3665 topleft new
3666 setlocal bufhidden=wipe
3667 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3668 tabprevious
3669 +tabclose
3670
3671 unlet g:window_id
3672 au! BufWipeout
3673 %bwipe!
3674endfunc
3675
zeertzjq021996f2022-04-10 11:44:04 +01003676func Test_v_event_readonly()
3677 autocmd CompleteChanged * let v:event.width = 0
3678 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3679 au! CompleteChanged
3680
3681 autocmd DirChangedPre * let v:event.directory = ''
3682 call assert_fails('cd .', 'E46:')
3683 au! DirChangedPre
3684
3685 autocmd ModeChanged * let v:event.new_mode = ''
3686 call assert_fails('normal! cc', 'E46:')
3687 au! ModeChanged
3688
3689 autocmd TextYankPost * let v:event.operator = ''
3690 call assert_fails('normal! yy', 'E46:')
3691 au! TextYankPost
3692endfunc
3693
zeertzjqc9e8fd62022-07-26 18:12:38 +01003694" Test for ModeChanged pattern
3695func Test_mode_changes()
3696 let g:index = 0
zeertzjq73916ba2023-04-26 16:50:19 +01003697 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 +01003698 func! TestMode()
3699 call assert_equal(g:mode_seq[g:index], get(v:event, "old_mode"))
3700 call assert_equal(g:mode_seq[g:index + 1], get(v:event, "new_mode"))
3701 call assert_equal(mode(1), get(v:event, "new_mode"))
3702 let g:index += 1
3703 endfunc
3704
3705 au ModeChanged * :call TestMode()
3706 let g:n_to_any = 0
3707 au ModeChanged n:* let g:n_to_any += 1
zeertzjq73916ba2023-04-26 16:50:19 +01003708 call feedkeys("i\<esc>vVca\<CR>\<C-X>\<C-L>\<esc>ggdV\<MouseMove>G", 'tnix')
zeertzjqc9e8fd62022-07-26 18:12:38 +01003709
3710 let g:V_to_v = 0
3711 au ModeChanged V:v let g:V_to_v += 1
3712 call feedkeys("Vv\<C-G>\<esc>", 'tnix')
3713 call assert_equal(len(filter(g:mode_seq[1:], {idx, val -> val == 'n'})), g:n_to_any)
3714 call assert_equal(1, g:V_to_v)
3715 call assert_equal(len(g:mode_seq) - 1, g:index)
3716
3717 let g:n_to_i = 0
3718 au ModeChanged n:i let g:n_to_i += 1
3719 let g:n_to_niI = 0
3720 au ModeChanged i:niI let g:n_to_niI += 1
3721 let g:niI_to_i = 0
3722 au ModeChanged niI:i let g:niI_to_i += 1
3723 let g:nany_to_i = 0
3724 au ModeChanged n*:i let g:nany_to_i += 1
3725 let g:i_to_n = 0
3726 au ModeChanged i:n let g:i_to_n += 1
3727 let g:nori_to_any = 0
3728 au ModeChanged [ni]:* let g:nori_to_any += 1
3729 let g:i_to_any = 0
3730 au ModeChanged i:* let g:i_to_any += 1
3731 let g:index = 0
3732 let g:mode_seq = ['n', 'i', 'niI', 'i', 'n']
3733 call feedkeys("a\<C-O>l\<esc>", 'tnix')
3734 call assert_equal(len(g:mode_seq) - 1, g:index)
3735 call assert_equal(1, g:n_to_i)
3736 call assert_equal(1, g:n_to_niI)
3737 call assert_equal(1, g:niI_to_i)
3738 call assert_equal(2, g:nany_to_i)
3739 call assert_equal(1, g:i_to_n)
3740 call assert_equal(2, g:i_to_any)
3741 call assert_equal(3, g:nori_to_any)
3742
3743 if has('terminal')
3744 let g:mode_seq += ['c', 'n', 't', 'nt', 'c', 'nt', 'n']
3745 call feedkeys(":term\<CR>\<C-W>N:bd!\<CR>", 'tnix')
3746 call assert_equal(len(g:mode_seq) - 1, g:index)
3747 call assert_equal(1, g:n_to_i)
3748 call assert_equal(1, g:n_to_niI)
3749 call assert_equal(1, g:niI_to_i)
3750 call assert_equal(2, g:nany_to_i)
3751 call assert_equal(1, g:i_to_n)
3752 call assert_equal(2, g:i_to_any)
3753 call assert_equal(5, g:nori_to_any)
3754 endif
3755
zeertzjqd1955982022-10-05 11:24:46 +01003756 let g:n_to_c = 0
3757 au ModeChanged n:c let g:n_to_c += 1
3758 let g:c_to_n = 0
3759 au ModeChanged c:n let g:c_to_n += 1
3760 let g:mode_seq += ['c', 'n', 'c', 'n']
3761 call feedkeys("q:\<C-C>\<Esc>", 'tnix')
3762 call assert_equal(len(g:mode_seq) - 1, g:index)
3763 call assert_equal(2, g:n_to_c)
3764 call assert_equal(2, g:c_to_n)
3765 unlet g:n_to_c
3766 unlet g:c_to_n
zeertzjqc9e8fd62022-07-26 18:12:38 +01003767
Bram Moolenaar61c4b042022-10-18 15:10:11 +01003768 let g:n_to_v = 0
3769 au ModeChanged n:v let g:n_to_v += 1
3770 let g:v_to_n = 0
3771 au ModeChanged v:n let g:v_to_n += 1
3772 let g:mode_seq += ['v', 'n']
3773 call feedkeys("v\<C-C>", 'tnix')
3774 call assert_equal(len(g:mode_seq) - 1, g:index)
3775 call assert_equal(1, g:n_to_v)
3776 call assert_equal(1, g:v_to_n)
3777 unlet g:n_to_v
3778 unlet g:v_to_n
3779
zeertzjqc9e8fd62022-07-26 18:12:38 +01003780 au! ModeChanged
3781 delfunc TestMode
3782 unlet! g:mode_seq
3783 unlet! g:index
3784 unlet! g:n_to_any
3785 unlet! g:V_to_v
3786 unlet! g:n_to_i
3787 unlet! g:n_to_niI
3788 unlet! g:niI_to_i
3789 unlet! g:nany_to_i
3790 unlet! g:i_to_n
3791 unlet! g:nori_to_any
3792 unlet! g:i_to_any
3793endfunc
3794
3795func Test_recursive_ModeChanged()
3796 au! ModeChanged * norm 0u
3797 sil! norm 
3798 au! ModeChanged
3799endfunc
3800
3801func Test_ModeChanged_starts_visual()
3802 " This was triggering ModeChanged before setting VIsual, causing a crash.
3803 au! ModeChanged * norm 0u
3804 sil! norm 
3805
3806 au! ModeChanged
3807endfunc
Bram Moolenaar347538f2022-03-26 16:28:06 +00003808
Charlie Grovesfef44852022-04-19 16:24:12 +01003809func Test_noname_autocmd()
3810 augroup test_noname_autocmd_group
3811 autocmd!
3812 autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")])
3813 autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")])
3814 autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")])
3815 autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")])
3816 autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")])
3817 augroup END
3818
3819 let s:li = []
3820 edit foo
3821 call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li)
3822
3823 au! test_noname_autocmd_group
3824 augroup! test_noname_autocmd_group
3825endfunc
3826
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01003827" Test for the autocmd_get() function
3828func Test_autocmd_get()
3829 augroup TestAutoCmdFns
3830 au!
3831 autocmd BufAdd *.vim echo "bufadd-vim"
3832 autocmd BufAdd *.py echo "bufadd-py"
3833 autocmd BufHidden *.vim echo "bufhidden"
3834 augroup END
3835 augroup TestAutoCmdFns2
3836 autocmd BufAdd *.vim echo "bufadd-vim-2"
3837 autocmd BufRead *.a1b2c3 echo "bufadd-vim-2"
3838 augroup END
3839
3840 let l = autocmd_get()
3841 call assert_true(l->len() > 0)
3842
3843 " Test for getting all the autocmds in a group
3844 let expected = [
3845 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3846 \ pattern: '*.vim', nested: v:false, once: v:false,
3847 \ event: 'BufAdd'},
3848 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3849 \ pattern: '*.py', nested: v:false, once: v:false,
3850 \ event: 'BufAdd'},
3851 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3852 \ pattern: '*.vim', nested: v:false,
3853 \ once: v:false, event: 'BufHidden'}]
3854 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3855
3856 " Test for getting autocmds for all the patterns in a group
3857 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3858 \ event: '*'}))
3859
3860 " Test for getting autocmds for an event in a group
3861 let expected = [
3862 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3863 \ pattern: '*.vim', nested: v:false, once: v:false,
3864 \ event: 'BufAdd'},
3865 \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns',
3866 \ pattern: '*.py', nested: v:false, once: v:false,
3867 \ event: 'BufAdd'}]
3868 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns',
3869 \ event: 'BufAdd'}))
3870
3871 " Test for getting the autocmds for all the events in a group for particular
3872 " pattern
3873 call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns',
3874 \ 'pattern': '*.py', 'nested': v:false, 'once': v:false,
3875 \ 'event': 'BufAdd'}],
3876 \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'}))
3877
3878 " Test for getting the autocmds for an events in a group for particular
3879 " pattern
3880 let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd',
3881 \ pattern: '*.vim'})
3882 call assert_equal([
3883 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3884 \ pattern: '*.vim', nested: v:false, once: v:false,
3885 \ event: 'BufAdd'}], l)
3886
3887 " Test for getting the autocmds for a pattern in a group
3888 let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'})
3889 call assert_equal([
3890 \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns',
3891 \ pattern: '*.vim', nested: v:false, once: v:false,
3892 \ event: 'BufAdd'},
3893 \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns',
3894 \ pattern: '*.vim', nested: v:false,
3895 \ once: v:false, event: 'BufHidden'}], l)
3896
3897 " Test for getting the autocmds for a pattern in all the groups
3898 let l = autocmd_get(#{pattern: '*.a1b2c3'})
3899 call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2',
3900 \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false,
3901 \ 'event': 'BufRead'}], l)
3902
3903 " Test for getting autocmds for a pattern without any autocmds
3904 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3905 \ pattern: '*.abc'}))
3906 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3907 \ event: 'BufAdd', pattern: '*.abc'}))
3908 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns',
3909 \ event: 'BufWipeout'}))
3910 call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})",
3911 \ 'E367:')
3912 let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})"
3913 call assert_fails(cmd, 'E216:')
3914 call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:')
3915 call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:')
3916
3917 augroup TestAutoCmdFns
3918 au!
3919 augroup END
3920 call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'}))
3921
3922 " Test for nested and once autocmds
3923 augroup TestAutoCmdFns
3924 au!
3925 autocmd VimSuspend * ++nested echo "suspend"
3926 autocmd VimResume * ++once echo "resume"
3927 augroup END
3928
3929 let expected = [
3930 \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3931 \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'},
3932 \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*',
3933 \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}]
3934 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3935
3936 " Test for buffer-local autocmd
3937 augroup TestAutoCmdFns
3938 au!
3939 autocmd TextYankPost <buffer> echo "textyankpost"
3940 augroup END
3941
3942 let expected = [
3943 \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns',
3944 \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false,
3945 \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}]
3946 call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'}))
3947
3948 augroup TestAutoCmdFns
3949 au!
3950 augroup END
3951 augroup! TestAutoCmdFns
3952 augroup TestAutoCmdFns2
3953 au!
3954 augroup END
3955 augroup! TestAutoCmdFns2
3956
3957 call assert_fails("echo autocmd_get(#{group: []})", 'E730:')
3958 call assert_fails("echo autocmd_get(#{event: {}})", 'E731:')
3959 call assert_fails("echo autocmd_get([])", 'E1206:')
3960endfunc
3961
3962" Test for the autocmd_add() function
3963func Test_autocmd_add()
3964 " Define a single autocmd in a group
3965 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3966 \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}])
3967 call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet',
3968 \ pattern: '*.sh', nested: v:true, once: v:true,
3969 \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'}))
3970
3971 " Define two autocmds in the same group
3972 call autocmd_delete([#{group: 'TestAcSet'}])
3973 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh',
3974 \ cmd: 'echo "bufadd"'},
3975 \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh',
3976 \ cmd: 'echo "bufenter"'}])
3977 call assert_equal([
3978 \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh',
3979 \ nested: v:false, once: v:false, event: 'BufAdd'},
3980 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh',
3981 \ nested: v:false, once: v:false, event: 'BufEnter'}],
3982 \ autocmd_get(#{group: 'TestAcSet'}))
3983
3984 " Define a buffer-local autocmd
3985 call autocmd_delete([#{group: 'TestAcSet'}])
3986 call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold',
3987 \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}])
3988 call assert_equal([
3989 \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet',
3990 \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false,
3991 \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}],
3992 \ autocmd_get(#{group: 'TestAcSet'}))
3993
3994 " Use an invalid buffer number
3995 call autocmd_delete([#{group: 'TestAcSet'}])
3996 call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter',
3997 \ bufnr: -1, cmd: 'echo "bufenter"'}])
3998 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
3999 \ cmd: 'echo "bufadd"'}]
4000 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004001 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4002 \ pattern: '*.py', cmd: 'echo "bufadd"'}]
4003 call assert_fails("echo autocmd_add(l)", 'E680:')
4004 let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999,
4005 \ pattern: ['*.py', '*.c'], cmd: 'echo "bufadd"'}]
4006 call assert_fails("echo autocmd_add(l)", 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004007 let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [],
4008 \ cmd: 'echo "bufread"'}]
4009 call assert_fails("echo autocmd_add(l)", 'E745:')
4010 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4011
4012 " Add two commands to the same group, event and pattern
4013 call autocmd_delete([#{group: 'TestAcSet'}])
4014 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
4015 \ pattern: 'abc', cmd: 'echo "cmd1"'}])
4016 call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload',
4017 \ pattern: 'abc', cmd: 'echo "cmd2"'}])
4018 call assert_equal([
4019 \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc',
4020 \ nested: v:false, once: v:false, event: 'BufUnload'},
4021 \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc',
4022 \ nested: v:false, once: v:false, event: 'BufUnload'}],
4023 \ autocmd_get(#{group: 'TestAcSet'}))
4024
4025 " When adding a new autocmd, if the autocmd 'group' is not specified, then
4026 " the current autocmd group should be used.
4027 call autocmd_delete([#{group: 'TestAcSet'}])
4028 augroup TestAcSet
4029 call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}])
4030 augroup END
4031 call assert_equal([
4032 \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc',
4033 \ nested: v:false, once: v:false, event: 'BufHidden'}],
4034 \ autocmd_get(#{group: 'TestAcSet'}))
4035
Yegappan Lakshmanan971f6822022-05-24 11:40:11 +01004036 " Test for replacing a cmd for an event in a group
4037 call autocmd_delete([#{group: 'TestAcSet'}])
4038 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4039 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4040 call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter',
4041 \ pattern: '*.py', cmd: 'echo "bufenter"'}])
4042 call assert_equal([
4043 \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py',
4044 \ nested: v:false, once: v:false, event: 'BufEnter'}],
4045 \ autocmd_get(#{group: 'TestAcSet'}))
4046
4047 " Test for adding a command for an unsupported autocmd event
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004048 let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh',
4049 \ cmd: 'echo "bufadd"'}]
4050 call assert_fails('call autocmd_add(l)', 'E216:')
4051
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004052 " Test for using a list of events and patterns
4053 call autocmd_delete([#{group: 'TestAcSet'}])
4054 let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'],
4055 \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}]
4056 call autocmd_add(l)
4057 call assert_equal([
4058 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4059 \ nested: v:false, once: v:false, event: 'BufEnter'},
4060 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4061 \ nested: v:false, once: v:false, event: 'BufEnter'},
4062 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py',
4063 \ nested: v:false, once: v:false, event: 'BufLeave'},
4064 \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh',
4065 \ nested: v:false, once: v:false, event: 'BufLeave'}],
4066 \ autocmd_get(#{group: 'TestAcSet'}))
4067
4068 " Test for invalid values for 'event' item
4069 call autocmd_delete([#{group: 'TestAcSet'}])
4070 let l = [#{group: 'TestAcSet', event: test_null_string(),
4071 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4072 call assert_fails('call autocmd_add(l)', 'E928:')
4073 let l = [#{group: 'TestAcSet', event: test_null_list(),
4074 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4075 call assert_fails('call autocmd_add(l)', 'E714:')
4076 let l = [#{group: 'TestAcSet', event: {},
4077 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4078 call assert_fails('call autocmd_add(l)', 'E777:')
4079 let l = [#{group: 'TestAcSet', event: [{}],
4080 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4081 call assert_fails('call autocmd_add(l)', 'E928:')
4082 let l = [#{group: 'TestAcSet', event: [test_null_string()],
4083 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4084 call assert_fails('call autocmd_add(l)', 'E928:')
4085 let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave',
4086 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4087 call assert_fails('call autocmd_add(l)', 'E216:')
4088 let l = [#{group: 'TestAcSet', event: [],
4089 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4090 call autocmd_add(l)
4091 let l = [#{group: 'TestAcSet', event: [""],
4092 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4093 call assert_fails('call autocmd_add(l)', 'E216:')
4094 let l = [#{group: 'TestAcSet', event: "",
4095 \ pattern: "*.py", cmd: 'echo "bufcmds"'}]
4096 call autocmd_add(l)
4097 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4098
4099 " Test for invalid values for 'pattern' item
4100 let l = [#{group: 'TestAcSet', event: "BufEnter",
4101 \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}]
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004102 call assert_fails('call autocmd_add(l)', 'E928:')
Yegappan Lakshmanane0ff3a72022-05-27 18:05:33 +01004103 let l = [#{group: 'TestAcSet', event: "BufEnter",
4104 \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}]
4105 call assert_fails('call autocmd_add(l)', 'E714:')
4106 let l = [#{group: 'TestAcSet', event: "BufEnter",
4107 \ pattern: {}, cmd: 'echo "bufcmds"'}]
4108 call assert_fails('call autocmd_add(l)', 'E777:')
4109 let l = [#{group: 'TestAcSet', event: "BufEnter",
4110 \ pattern: [{}], cmd: 'echo "bufcmds"'}]
4111 call assert_fails('call autocmd_add(l)', 'E928:')
4112 let l = [#{group: 'TestAcSet', event: "BufEnter",
4113 \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}]
4114 call assert_fails('call autocmd_add(l)', 'E928:')
4115 let l = [#{group: 'TestAcSet', event: "BufEnter",
4116 \ pattern: [], cmd: 'echo "bufcmds"'}]
4117 call autocmd_add(l)
4118 let l = [#{group: 'TestAcSet', event: "BufEnter",
4119 \ pattern: [""], cmd: 'echo "bufcmds"'}]
4120 call autocmd_add(l)
4121 let l = [#{group: 'TestAcSet', event: "BufEnter",
4122 \ pattern: "", cmd: 'echo "bufcmds"'}]
4123 call autocmd_add(l)
4124 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4125
4126 let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave',
4127 \ pattern: '*.py', cmd: 'echo "bufcmds"'}]
4128 call assert_fails('call autocmd_add(l)', 'E216:')
4129
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004130 call assert_fails("call autocmd_add({})", 'E1211:')
4131 call assert_equal(v:false, autocmd_add(test_null_list()))
4132 call assert_true(autocmd_add([[]]))
4133 call assert_true(autocmd_add([test_null_dict()]))
4134
4135 augroup TestAcSet
4136 au!
4137 augroup END
4138
4139 call autocmd_add([#{group: 'TestAcSet'}])
4140 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}])
4141 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}])
4142 call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}])
4143 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}])
4144 call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}])
4145 call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}])
4146 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4147
4148 augroup! TestAcSet
4149endfunc
4150
4151" Test for deleting autocmd events and groups
4152func Test_autocmd_delete()
4153 " Delete an event in an autocmd group
4154 augroup TestAcSet
4155 au!
4156 au BufAdd *.sh echo "bufadd"
4157 au BufEnter *.sh echo "bufenter"
4158 augroup END
4159 call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}])
4160 call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet',
4161 \ pattern: '*.sh', nested: v:false, once: v:false,
4162 \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'}))
4163
4164 " Delete all the events in an autocmd group
4165 augroup TestAcSet
4166 au BufAdd *.sh echo "bufadd"
4167 augroup END
4168 call autocmd_delete([#{group: 'TestAcSet', event: '*'}])
4169 call assert_equal([], autocmd_get(#{group: 'TestAcSet'}))
4170
4171 " Delete a non-existing autocmd group
4172 call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:')
4173 " Delete a non-existing autocmd event
4174 let l = [#{group: 'TestAcSet', event: 'abc'}]
4175 call assert_fails("call autocmd_delete(l)", 'E216:')
4176 " Delete a non-existing autocmd pattern
4177 let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}]
4178 call assert_true(autocmd_delete(l))
Yegappan Lakshmanan00e977c2022-06-01 12:31:53 +01004179 " Delete an autocmd for a non-existing buffer
4180 let l = [#{event: '*', bufnr: 9999, cmd: 'echo "x"'}]
4181 call assert_fails('call autocmd_delete(l)', 'E680:')
Yegappan Lakshmanan1755a912022-05-19 10:31:47 +01004182
4183 " Delete an autocmd group
4184 augroup TestAcSet
4185 au!
4186 au BufAdd *.sh echo "bufadd"
4187 au BufEnter *.sh echo "bufenter"
4188 augroup END
4189 call autocmd_delete([#{group: 'TestAcSet'}])
4190 call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:')
4191
4192 call assert_true(autocmd_delete([[]]))
4193 call assert_true(autocmd_delete([test_null_dict()]))
4194endfunc
4195
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004196func Test_autocmd_split_dummy()
4197 " Autocommand trying to split a window containing a dummy buffer.
Bram Moolenaar94722c52023-01-28 19:19:03 +00004198 auto BufReadPre * exe "sbuf " .. expand("<abuf>")
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004199 " Avoid the "W11" prompt
4200 au FileChangedShell * let v:fcs_choice = 'reload'
4201 func Xautocmd_changelist()
4202 cal writefile(['Xtestfile2:4:4'], 'Xerr')
4203 edit Xerr
4204 lex 'Xtestfile2:4:4'
4205 endfunc
4206 call Xautocmd_changelist()
Bram Moolenaar53c5c9f2022-10-18 17:25:03 +01004207 " Should get E86, but it doesn't always happen (timing?)
4208 silent! call Xautocmd_changelist()
Bram Moolenaar8f3c3c62022-10-18 17:05:54 +01004209
4210 au! BufReadPre
4211 au! FileChangedShell
4212 delfunc Xautocmd_changelist
4213 bwipe! Xerr
4214 call delete('Xerr')
4215endfunc
4216
Bram Moolenaare76062c2022-11-28 18:51:43 +00004217" This was crashing because there was only one window to execute autocommands
4218" in.
4219func Test_autocmd_nested_setbufvar()
4220 CheckFeature python3
4221
4222 set hidden
4223 edit Xaaa
4224 edit Xbbb
4225 call setline(1, 'bar')
4226 enew
4227 au BufWriteCmd Xbbb ++nested call setbufvar('Xaaa', '&ft', 'foo') | bw! Xaaa
4228 au FileType foo call py3eval('vim.current.buffer.options["cindent"]')
4229 wall
4230
4231 au! BufWriteCmd
4232 au! FileType foo
4233 set nohidden
4234 call delete('Xaaa')
4235 call delete('Xbbb')
4236 %bwipe!
4237endfunc
4238
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004239func SetupVimTest_shm()
4240 let g:bwe = []
4241 let g:brp = []
4242 set shortmess+=F
zeertzjq657b31f2023-04-15 21:28:02 +01004243 messages clear
Christian Brabandt9aee8ec2022-12-16 16:41:23 +00004244
4245 let dirname='XVimTestSHM'
4246 call mkdir(dirname, 'R')
4247 call writefile(['test'], dirname .. '/1')
4248 call writefile(['test'], dirname .. '/2')
4249 call writefile(['test'], dirname .. '/3')
4250
4251 augroup test
4252 autocmd!
4253 autocmd BufWinEnter * call add(g:bwe, $'BufWinEnter: {expand('<amatch>')}')
4254 autocmd BufReadPost * call add(g:brp, $'BufReadPost: {expand('<amatch>')}')
4255 augroup END
4256
4257 call setqflist([
4258 \ {'filename': dirname .. '/1', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4259 \ {'filename': dirname .. '/2', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0},
4260 \ {'filename': dirname .. '/3', 'lnum': 1, 'col': 1, 'text': 'test', 'vcol': 0}
4261 \ ])
4262 cdo! substitute/test/TEST
4263
4264 " clean up
4265 noa enew!
4266 set shortmess&vim
4267 augroup test
4268 autocmd!
4269 augroup END
4270 augroup! test
4271endfunc
4272
4273func Test_autocmd_shortmess()
4274 CheckNotMSWindows
4275
4276 call SetupVimTest_shm()
4277 let output = execute(':mess')->split('\n')
4278
4279 let info = copy(output)->filter({idx, val -> val =~# '\d of 3'} )
4280 let bytes = copy(output)->filter({idx, val -> val =~# 'bytes'} )
4281
4282 " We test the following here:
4283 " BufReadPost should have been triggered 3 times, once per file
4284 " BufWinEnter should have been triggered 3 times, once per file
4285 " FileInfoMessage should have been shown 3 times, regardless of shm option
4286 " "(x of 3)" message from :cnext has been shown 3 times
4287
4288 call assert_equal(3, g:brp->len())
4289 call assert_equal(3, g:bwe->len())
4290 call assert_equal(3, info->len())
4291 call assert_equal(3, bytes->len())
4292
4293 delfunc SetupVimTest_shm
4294endfunc
Bram Moolenaare76062c2022-11-28 18:51:43 +00004295
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01004296" vim: shiftwidth=2 sts=2 expandtab