blob: 3ff9d0b340fa85b8fd228345750b9b6aaec34582 [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
27 call writefile(['one', 'two', 'three'], 'Xfile')
28 let before =<< trim END
29 set updatetime=10
30 au CursorHold * call writefile([line('.')], 'Xoutput', 'a')
31 END
32 call writefile(before, 'Xinit')
33 let buf = RunVimInTerminal('-S Xinit Xfile', {})
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 Moolenaar17f67542020-08-20 18:29:13 +020038 call WaitForAssert({-> assert_equal(['1'], readfile('Xoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020039 call term_sendkeys(buf, "j")
40 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020041 call WaitForAssert({-> assert_equal(['1', '2'], readfile('Xoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020042 call term_sendkeys(buf, "j")
43 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020044 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('Xoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call StopVimInTerminal(buf)
46
Bram Moolenaar75911162020-07-21 19:44:47 +020047 call delete('Xinit')
48 call delete('Xoutput')
49 call delete('Xfile')
50endfunc
51
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020053
Bram Moolenaarc67e8922016-05-24 16:07:40 +020054 func ExitInsertMode(id)
55 call feedkeys("\<Esc>")
56 endfunc
57
58 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020059 " Need to move the cursor.
60 call feedkeys("ggG", "xt")
61
Bram Moolenaarc67e8922016-05-24 16:07:40 +020062 let g:triggered = 0
63 au CursorHoldI * let g:triggered += 1
64 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010065 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020066 call feedkeys('a', 'x!')
67 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010068 unlet g:triggered
69 au! CursorHoldI
70 set updatetime&
71 endfunc
72
73 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020074 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010075 " Need to move the cursor.
76 call feedkeys("ggG", "xt")
77
78 " Confirm the timer invoked in exit_cb of the job doesn't disturb
79 " CursorHoldI event.
80 let g:triggered = 0
81 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020082 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010083 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020084 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010085 call feedkeys('a', 'x!')
86 call assert_equal(1, g:triggered)
87 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020088 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020089 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020090 endfunc
91
92 func Test_cursorhold_insert_ctrl_x()
93 let g:triggered = 0
94 au CursorHoldI * let g:triggered += 1
95 set updatetime=20
96 call timer_start(100, 'ExitInsertMode')
97 " CursorHoldI does not trigger after CTRL-X
98 call feedkeys("a\<C-X>", 'x!')
99 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +0100100 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200101 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200102 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200103 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200104
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100105 func Test_cursorhold_insert_ctrl_g_U()
106 au CursorHoldI * :
107 set updatetime=20
108 new
109 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
110 call feedkeys("i()\<C-g>U", 'tx!')
111 sleep 200m
112 call assert_equal('(foo)', getline(1))
113 undo
114 call assert_equal('', getline(1))
115
116 bwipe!
117 au! CursorHoldI
118 set updatetime&
119 endfunc
120
Bram Moolenaar97b00752019-05-12 13:07:14 +0200121 func Test_OptionSet_modeline()
122 call test_override('starting', 1)
123 au! OptionSet
124 augroup set_tabstop
125 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
126 augroup END
127 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline')
128 set modeline
129 let v:errmsg = ''
130 call assert_fails('split XoptionsetModeline', 'E12:')
131 call assert_equal(7, &ts)
132 call assert_equal('', v:errmsg)
133
134 augroup set_tabstop
135 au!
136 augroup END
137 bwipe!
138 set ts&
139 call delete('XoptionsetModeline')
140 call test_override('starting', 0)
141 endfunc
142
143endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200144
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200145func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200146 augroup test_bufunload_group
147 autocmd!
148 autocmd BufUnload * call add(s:li, "bufunload")
149 autocmd BufDelete * call add(s:li, "bufdelete")
150 autocmd BufWipeout * call add(s:li, "bufwipeout")
151 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200152
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100153 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200154 new
155 setlocal bufhidden=
156 bunload
157 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200158
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100159 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200160 new
161 setlocal bufhidden=delete
162 bunload
163 call assert_equal(["bufunload", "bufdelete"], s:li)
164
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100165 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200166 new
167 setlocal bufhidden=unload
168 bwipeout
169 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
170
Bram Moolenaare99e8442016-07-26 20:43:40 +0200171 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200172 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200173endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200174
175" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200176func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200177 tabedit
178 tabfirst
179
180 augroup test_autocmd_bufunload_with_tabnext_group
181 autocmd!
182 autocmd BufUnload <buffer> tabnext
183 augroup END
184
185 quit
186 call assert_equal(2, tabpagenr('$'))
187
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200188 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200189 augroup! test_autocmd_bufunload_with_tabnext_group
190 tablast
191 quit
192endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200193
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100194func Test_argdelete_in_next()
195 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
196 call assert_fails('next a b', 'E1156:')
197 au! BufNew,BufEnter,BufLeave,BufWinEnter *
198endfunc
199
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200200func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200201 tabedit
202 augroup sample
203 autocmd!
204 autocmd BufWinLeave <buffer> tabfirst
205 augroup END
206 call setline(1, ['a', 'b', 'c'])
207 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200208 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200209endfunc
210
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200211" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200212func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200213 split aa.txt
214 let lastbuf = bufnr('$')
215
216 augroup test_autocmd_bufunload
217 autocmd!
218 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
219 augroup END
220
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100221 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200222
223 autocmd! test_autocmd_bufunload
224 augroup! test_autocmd_bufunload
225 bwipe! aa.txt
226 bwipe! bb.txt
227endfunc
228
229" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200230func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200231 setlocal buftype=nowrite
232 let lastbuf = bufnr('$')
233
234 augroup test_autocmd_bufunload
235 autocmd!
236 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
237 augroup END
238
239 normal! i1
240 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200241
242 autocmd! test_autocmd_bufunload
243 augroup! test_autocmd_bufunload
244 bwipe! a.txt
245endfunc
246
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100247func Test_autocmd_dummy_wipeout()
248 " prepare files
249 call writefile([''], 'Xdummywipetest1.txt')
250 call writefile([''], 'Xdummywipetest2.txt')
251 augroup test_bufunload_group
252 autocmd!
253 autocmd BufUnload * call add(s:li, "bufunload")
254 autocmd BufDelete * call add(s:li, "bufdelete")
255 autocmd BufWipeout * call add(s:li, "bufwipeout")
256 augroup END
257
258 let s:li = []
259 split Xdummywipetest1.txt
260 silent! vimgrep /notmatched/ Xdummywipetest*
261 call assert_equal(["bufunload", "bufwipeout"], s:li)
262
263 bwipeout
264 call delete('Xdummywipetest1.txt')
265 call delete('Xdummywipetest2.txt')
266 au! test_bufunload_group
267 augroup! test_bufunload_group
268endfunc
269
Bram Moolenaarc917da42016-07-19 22:31:36 +0200270func Test_win_tab_autocmd()
271 let g:record = []
272
273 augroup testing
274 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000275 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200276 au WinEnter * call add(g:record, 'WinEnter')
277 au WinLeave * call add(g:record, 'WinLeave')
278 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200279 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200280 au TabEnter * call add(g:record, 'TabEnter')
281 au TabLeave * call add(g:record, 'TabLeave')
282 augroup END
283
284 split
285 tabnew
286 close
287 close
288
289 call assert_equal([
290 \ 'WinLeave', 'WinNew', 'WinEnter',
291 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000292 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
293 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200294 \ ], g:record)
295
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200296 let g:record = []
297 tabnew somefile
298 tabnext
299 bwipe somefile
300
301 call assert_equal([
302 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
303 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000304 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200305 \ ], g:record)
306
Bram Moolenaarc917da42016-07-19 22:31:36 +0200307 augroup testing
308 au!
309 augroup END
310 unlet g:record
311endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200312
LemonBoy09371822022-04-08 15:18:45 +0100313func Test_WinScrolled()
314 CheckRunVimInTerminal
315
316 let lines =<< trim END
317 set nowrap scrolloff=0
318 for ii in range(1, 18)
319 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
320 endfor
321 let win_id = win_getid()
322 let g:matched = v:false
323 execute 'au WinScrolled' win_id 'let g:matched = v:true'
324 let g:scrolled = 0
325 au WinScrolled * let g:scrolled += 1
326 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
327 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
328 END
329 call writefile(lines, 'Xtest_winscrolled')
330 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
331
332 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
333 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
334
335 " Scroll left/right in Normal mode.
336 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
337 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
338
339 " Scroll up/down in Normal mode.
340 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
341 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
342
343 " Scroll up/down in Insert mode.
344 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
345 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
346 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
347
348 " Scroll the window horizontally to focus the last letter of the third line
349 " containing only six characters. Moving to the previous and shorter lines
350 " should trigger another autocommand as Vim has to make them visible.
351 call term_sendkeys(buf, "5zl2k")
352 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
353 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
354
355 " Ensure the command was triggered for the specified window ID.
356 call term_sendkeys(buf, ":echo g:matched\<CR>")
357 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
358
359 " Ensure the expansion of <amatch> and <afile> matches the window ID.
360 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
361 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
362
363 call StopVimInTerminal(buf)
364 call delete('Xtest_winscrolled')
365endfunc
366
naohiro ono23beefe2021-11-13 12:38:49 +0000367func Test_WinClosed()
368 " Test that the pattern is matched against the closed window's ID, and both
369 " <amatch> and <afile> are set to it.
370 new
371 let winid = win_getid()
372 let g:matched = v:false
373 augroup test-WinClosed
374 autocmd!
375 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
376 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
377 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
378 augroup END
379 close
380 call assert_true(g:matched)
381 call assert_equal(winid, g:amatch)
382 call assert_equal(winid, g:afile)
383
384 " Test that WinClosed is non-recursive.
385 new
386 new
387 call assert_equal(3, winnr('$'))
388 let g:triggered = 0
389 augroup test-WinClosed
390 autocmd!
391 autocmd WinClosed * let g:triggered += 1
392 autocmd WinClosed * 2 wincmd c
393 augroup END
394 close
395 call assert_equal(1, winnr('$'))
396 call assert_equal(1, g:triggered)
397
398 autocmd! test-WinClosed
399 augroup! test-WinClosed
400 unlet g:matched
401 unlet g:amatch
402 unlet g:afile
403 unlet g:triggered
404endfunc
405
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100406func Test_WinClosed_throws()
407 vnew
408 let bnr = bufnr()
409 call assert_equal(1, bufloaded(bnr))
410 augroup test-WinClosed
411 autocmd WinClosed * throw 'foo'
412 augroup END
413 try
414 close
415 catch /.*/
416 endtry
417 call assert_equal(0, bufloaded(bnr))
418
419 autocmd! test-WinClosed
420 augroup! test-WinClosed
421endfunc
422
zeertzjq6a069402022-04-07 14:08:29 +0100423func Test_WinClosed_throws_with_tabs()
424 tabnew
425 let bnr = bufnr()
426 call assert_equal(1, bufloaded(bnr))
427 augroup test-WinClosed
428 autocmd WinClosed * throw 'foo'
429 augroup END
430 try
431 close
432 catch /.*/
433 endtry
434 call assert_equal(0, bufloaded(bnr))
435
436 autocmd! test-WinClosed
437 augroup! test-WinClosed
438endfunc
439
Bram Moolenaare99e8442016-07-26 20:43:40 +0200440func s:AddAnAutocmd()
441 augroup vimBarTest
442 au BufReadCmd * echo 'hello'
443 augroup END
444 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
445endfunc
446
447func Test_early_bar()
448 " test that a bar is recognized before the {event}
449 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000450 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200451 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000452 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200453
454 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000455 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200456 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000457 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200458
459 " test that a bar is recognized after the {event}
460 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000461 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200462 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000463 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200464
465 " test that a bar is recognized after the {group}
466 call s:AddAnAutocmd()
467 au! vimBarTest|echo 'hello'
468 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
469endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200470
Bram Moolenaar5c809082016-09-01 16:21:48 +0200471func RemoveGroup()
472 autocmd! StartOK
473 augroup! StartOK
474endfunc
475
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200476func Test_augroup_warning()
477 augroup TheWarning
478 au VimEnter * echo 'entering'
479 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100480 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200481 redir => res
482 augroup! TheWarning
483 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100484 call assert_match("W19:", res)
485 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200486
487 " check "Another" does not take the pace of the deleted entry
488 augroup Another
489 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100490 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200491 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200492
493 " no warning for postpone aucmd delete
494 augroup StartOK
495 au VimEnter * call RemoveGroup()
496 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100497 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200498 redir => res
499 doautocmd VimEnter
500 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100501 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200502 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200503
504 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200505endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200506
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200507func Test_BufReadCmdHelp()
508 " This used to cause access to free memory
509 au BufReadCmd * e +h
510 help
511
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200512 au! BufReadCmd
513endfunc
514
515func Test_BufReadCmdHelpJump()
516 " This used to cause access to free memory
517 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200518 " } to fix highlighting
519 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200520
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200521 au! BufReadCmd
522endfunc
523
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200524func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200525 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200526 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200527 call assert_fails('augroup! x', 'E936:')
528 au VimEnter * echo
529 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200530 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100531 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200532 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200533endfunc
534
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200535" Tests for autocommands on :close command.
536" This used to be in test13.
537func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200538 " Clean up buffers, because in some cases this function fails.
539 call s:cleanup_buffers()
540
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200541 " Write three files and open them, each in a window.
542 " Then go to next window, with autocommand that deletes the previous one.
543 " Do this twice, writing the file.
544 e! Xtestje1
545 call setline(1, 'testje1')
546 w
547 sp Xtestje2
548 call setline(1, 'testje2')
549 w
550 sp Xtestje3
551 call setline(1, 'testje3')
552 w
553 wincmd w
554 au WinLeave Xtestje2 bwipe
555 wincmd w
556 call assert_equal('Xtestje1', expand('%'))
557
558 au WinLeave Xtestje1 bwipe Xtestje3
559 close
560 call assert_equal('Xtestje1', expand('%'))
561
562 " Test deleting the buffer on a Unload event. If this goes wrong there
563 " will be the ATTENTION prompt.
564 e Xtestje1
565 au!
566 au! BufUnload Xtestje1 bwipe
567 call assert_fails('e Xtestje3', 'E937:')
568 call assert_equal('Xtestje3', expand('%'))
569
570 e Xtestje2
571 sp Xtestje1
572 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200573 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200574
575 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
576 " there are ml_line errors and/or a Crash.
577 au!
578 only
579 e Xanother
580 e Xtestje1
581 bwipe Xtestje2
582 bwipe Xtestje3
583 au BufWipeout Xtestje1 buf Xtestje1
584 bwipe
585 call assert_equal('Xanother', expand('%'))
586
587 only
588 help
589 wincmd w
590 1quit
591 call assert_equal('Xanother', expand('%'))
592
593 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100594 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200595 call delete('Xtestje1')
596 call delete('Xtestje2')
597 call delete('Xtestje3')
598endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100599
600func Test_BufEnter()
601 au! BufEnter
602 au Bufenter * let val = val . '+'
603 let g:val = ''
604 split NewFile
605 call assert_equal('+', g:val)
606 bwipe!
607 call assert_equal('++', g:val)
608
609 " Also get BufEnter when editing a directory
610 call mkdir('Xdir')
611 split Xdir
612 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100613
614 " On MS-Windows we can't edit the directory, make sure we wipe the right
615 " buffer.
616 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100617
618 call delete('Xdir', 'd')
619 au! BufEnter
620endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100621
622" Closing a window might cause an endless loop
623" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200624func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200625 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100626 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200627 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100628 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100629 mksession!
630
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200631 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200632 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200633 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100634 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200635 augroup test_autocmd_sessionload
636 autocmd!
637 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
638 augroup END
639
640 func WriteErrors()
641 call writefile([execute("messages")], "Xerrors")
642 endfunc
643 au VimLeave * call WriteErrors()
644 [CODE]
645
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100646 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200647 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100648 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200649 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100650
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100651 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100652 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100653 call delete(file)
654 endfor
655endfunc
656
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100657" Using :blast and :ball for many events caused a crash, because b_nwindows was
658" not incremented correctly.
659func Test_autocmd_blast_badd()
660 let content =<< trim [CODE]
661 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
662 edit foo1
663 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
664 edit foo2
665 call writefile(['OK'], 'Xerrors')
666 qall
667 [CODE]
668
669 call writefile(content, 'XblastBall')
670 call system(GetVimCommand() .. ' --clean -S XblastBall')
671 call assert_match('OK', readfile('Xerrors')->join())
672
673 call delete('XblastBall')
674 call delete('Xerrors')
675endfunc
676
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100677" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200678func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100679 tabnew
680 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100681 mksession!
682
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200683 let content =<< trim [CODE]
684 set nocp noswapfile
685 function! DeleteInactiveBufs()
686 tabfirst
687 let tabblist = []
688 for i in range(1, tabpagenr(''$''))
689 call extend(tabblist, tabpagebuflist(i))
690 endfor
691 for b in range(1, bufnr(''$''))
692 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
693 exec ''bwipeout '' . b
694 endif
695 endfor
696 echomsg "SessionLoadPost DONE"
697 endfunction
698 au SessionLoadPost * call DeleteInactiveBufs()
699
700 func WriteErrors()
701 call writefile([execute("messages")], "Xerrors")
702 endfunc
703 au VimLeave * call WriteErrors()
704 [CODE]
705
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100706 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200707 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100708 let errors = join(readfile('Xerrors'))
709 " This probably only ever matches on unix.
710 call assert_notmatch('Caught deadly signal SEGV', errors)
711 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100712
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100713 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100714 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100715 call delete(file)
716 endfor
717endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200718
719func Test_empty_doau()
720 doau \|
721endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200722
723func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200724 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200725 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200726 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
727 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 +0200728 let g:opt = [expected, actual]
729 "call assert_equal(expected, actual)
730endfunc
731
732func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200733 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200734
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200735 badd test_autocmd.vim
736
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200737 call test_override('starting', 1)
738 set nocp
739 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
740
741 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100742 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200743 set nu
744 call assert_equal([], g:options)
745 call assert_equal(g:opt[0], g:opt[1])
746
747 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100748 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200749 setlocal nonu
750 call assert_equal([], g:options)
751 call assert_equal(g:opt[0], g:opt[1])
752
753 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100754 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200755 setglobal nonu
756 call assert_equal([], g:options)
757 call assert_equal(g:opt[0], g:opt[1])
758
759 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100760 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200761 setlocal ai
762 call assert_equal([], g:options)
763 call assert_equal(g:opt[0], g:opt[1])
764
765 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100766 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200767 setglobal ai
768 call assert_equal([], g:options)
769 call assert_equal(g:opt[0], g:opt[1])
770
771 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100772 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200773 set ai!
774 call assert_equal([], g:options)
775 call assert_equal(g:opt[0], g:opt[1])
776
777 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100778 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200779 noa setlocal ai
780 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200781 set ai!
782 call assert_equal([], g:options)
783 call assert_equal(g:opt[0], g:opt[1])
784
785 " Should not print anything, use :noa
786 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100787 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200788 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200789 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200790 call assert_equal(g:opt[0], g:opt[1])
791
792 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100793 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200794 set list nu
795 call assert_equal([], g:options)
796 call assert_equal(g:opt[0], g:opt[1])
797
798 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100799 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200800 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200801 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 +0200802 call assert_equal(g:opt[0], g:opt[1])
803
804 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100805 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200806 setlocal acd
807 call assert_equal([], g:options)
808 call assert_equal(g:opt[0], g:opt[1])
809
810 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100811 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200812 set ar
813 call assert_equal([], g:options)
814 call assert_equal(g:opt[0], g:opt[1])
815
816 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100817 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200818 setlocal ar
819 call assert_equal([], g:options)
820 call assert_equal(g:opt[0], g:opt[1])
821
822 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100823 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200824 setglobal invar
825 call assert_equal([], g:options)
826 call assert_equal(g:opt[0], g:opt[1])
827
828 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100829 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
830 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200831 call assert_equal([], g:options)
832 call assert_equal(g:opt[0], g:opt[1])
833
834 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100835 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200836 " try twice, first time, shouldn't trigger because option name is invalid,
837 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200838 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200839 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200840 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200841 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200842 call assert_equal([], g:options)
843 call assert_equal(g:opt[0], g:opt[1])
844
845 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100846 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200847 call setwinvar(0, '&number', 1)
848 call assert_equal([], g:options)
849 call assert_equal(g:opt[0], g:opt[1])
850
851 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100852 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200853 setlocal key=blah
854 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200855 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200856 call assert_equal(g:opt[0], g:opt[1])
857
Bram Moolenaard7c96872019-06-15 17:12:48 +0200858
859 " 18a: Setting string global option"
860 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100861 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200862 set backupext=foo
863 call assert_equal([], g:options)
864 call assert_equal(g:opt[0], g:opt[1])
865
866 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100867 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200868 set backupext&
869 call assert_equal([], g:options)
870 call assert_equal(g:opt[0], g:opt[1])
871
872 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100873 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200874 setglobal backupext=bar
875 call assert_equal([], g:options)
876 call assert_equal(g:opt[0], g:opt[1])
877
878 " 18d: Setting local string global option"
879 " As this is a global option this sets the global value even though
880 " :setlocal is used!
881 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100882 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200883 setlocal backupext=baz
884 call assert_equal([], g:options)
885 call assert_equal(g:opt[0], g:opt[1])
886
887 " 18e: Setting again string global option"
888 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
889 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100890 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200891 set backupext=fuu
892 call assert_equal([], g:options)
893 call assert_equal(g:opt[0], g:opt[1])
894
895
zeertzjqb811de52021-10-21 10:50:44 +0100896 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200897 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100898 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200899 set tags=tagpath
900 call assert_equal([], g:options)
901 call assert_equal(g:opt[0], g:opt[1])
902
zeertzjqb811de52021-10-21 10:50:44 +0100903 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100904 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200905 set tags&
906 call assert_equal([], g:options)
907 call assert_equal(g:opt[0], g:opt[1])
908
zeertzjqb811de52021-10-21 10:50:44 +0100909 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100910 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200911 setglobal tags=tagpath1
912 call assert_equal([], g:options)
913 call assert_equal(g:opt[0], g:opt[1])
914
zeertzjqb811de52021-10-21 10:50:44 +0100915 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100916 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200917 setlocal tags=tagpath2
918 call assert_equal([], g:options)
919 call assert_equal(g:opt[0], g:opt[1])
920
zeertzjqb811de52021-10-21 10:50:44 +0100921 " 19e: Setting again string global-local (to buffer) option"
922 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200923 " but the old local value for all other kinds of options.
924 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
925 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100926 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200927 set tags=tagpath
928 call assert_equal([], g:options)
929 call assert_equal(g:opt[0], g:opt[1])
930
zeertzjqb811de52021-10-21 10:50:44 +0100931 " 19f: Setting string global-local (to buffer) option to an empty string"
932 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200933 " but the old local value for all other kinds of options.
934 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
935 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100936 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200937 set tags=tagpath
938 call assert_equal([], g:options)
939 call assert_equal(g:opt[0], g:opt[1])
940
941
942 " 20a: Setting string local (to buffer) option"
943 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100944 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200945 set spelllang=elvish,klingon
946 call assert_equal([], g:options)
947 call assert_equal(g:opt[0], g:opt[1])
948
949 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100950 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200951 set spelllang&
952 call assert_equal([], g:options)
953 call assert_equal(g:opt[0], g:opt[1])
954
955 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100956 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200957 setglobal spelllang=elvish
958 call assert_equal([], g:options)
959 call assert_equal(g:opt[0], g:opt[1])
960
961 " 20d: Setting local string local (to buffer) option"
962 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100963 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200964 setlocal spelllang=klingon
965 call assert_equal([], g:options)
966 call assert_equal(g:opt[0], g:opt[1])
967
968 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +0100969 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200970 " but the old local value for all other kinds of options.
971 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
972 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100973 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200974 set spelllang=foo
975 call assert_equal([], g:options)
976 call assert_equal(g:opt[0], g:opt[1])
977
978
zeertzjqb811de52021-10-21 10:50:44 +0100979 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200980 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100981 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200982 set statusline=foo
983 call assert_equal([], g:options)
984 call assert_equal(g:opt[0], g:opt[1])
985
zeertzjqb811de52021-10-21 10:50:44 +0100986 " 21b: Resetting string global-local (to window) option"
987 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200988 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100989 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200990 set statusline&
991 call assert_equal([], g:options)
992 call assert_equal(g:opt[0], g:opt[1])
993
zeertzjqb811de52021-10-21 10:50:44 +0100994 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100995 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200996 setglobal statusline=bar
997 call assert_equal([], g:options)
998 call assert_equal(g:opt[0], g:opt[1])
999
zeertzjqb811de52021-10-21 10:50:44 +01001000 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001001 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001002 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001003 setlocal statusline=baz
1004 call assert_equal([], g:options)
1005 call assert_equal(g:opt[0], g:opt[1])
1006
zeertzjqb811de52021-10-21 10:50:44 +01001007 " 21e: Setting again string global-local (to window) option"
1008 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001009 " but the old local value for all other kinds of options.
1010 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1011 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001012 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001013 set statusline=foo
1014 call assert_equal([], g:options)
1015 call assert_equal(g:opt[0], g:opt[1])
1016
1017
1018 " 22a: Setting string local (to window) option"
1019 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001020 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001021 set foldignore=fo
1022 call assert_equal([], g:options)
1023 call assert_equal(g:opt[0], g:opt[1])
1024
1025 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001026 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001027 set foldignore&
1028 call assert_equal([], g:options)
1029 call assert_equal(g:opt[0], g:opt[1])
1030
1031 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001032 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001033 setglobal foldignore=bar
1034 call assert_equal([], g:options)
1035 call assert_equal(g:opt[0], g:opt[1])
1036
1037 " 22d: Setting local string local (to window) option"
1038 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001039 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001040 setlocal foldignore=baz
1041 call assert_equal([], g:options)
1042 call assert_equal(g:opt[0], g:opt[1])
1043
1044 " 22e: Setting again string local (to window) option"
1045 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1046 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001047 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001048 set foldignore=fo
1049 call assert_equal([], g:options)
1050 call assert_equal(g:opt[0], g:opt[1])
1051
1052
zeertzjqb811de52021-10-21 10:50:44 +01001053 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001054 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1055 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001056 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001057 setglobal cmdheight=2
1058 call assert_equal([], g:options)
1059 call assert_equal(g:opt[0], g:opt[1])
1060
1061 " 23b: Setting local number global option"
1062 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1063 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001064 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001065 setlocal cmdheight=2
1066 call assert_equal([], g:options)
1067 call assert_equal(g:opt[0], g:opt[1])
1068
1069 " 23c: Setting again number global option"
1070 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1071 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001072 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001073 set cmdheight=2
1074 call assert_equal([], g:options)
1075 call assert_equal(g:opt[0], g:opt[1])
1076
1077 " 23d: Setting again number global option"
1078 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001079 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001080 set cmdheight=2
1081 call assert_equal([], g:options)
1082 call assert_equal(g:opt[0], g:opt[1])
1083
1084
1085 " 24a: Setting global number global-local (to buffer) option"
1086 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1087 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001088 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001089 setglobal undolevels=2
1090 call assert_equal([], g:options)
1091 call assert_equal(g:opt[0], g:opt[1])
1092
1093 " 24b: Setting local number global-local (to buffer) option"
1094 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1095 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001096 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001097 setlocal undolevels=2
1098 call assert_equal([], g:options)
1099 call assert_equal(g:opt[0], g:opt[1])
1100
1101 " 24c: Setting again number global-local (to buffer) option"
1102 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1103 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001104 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001105 set undolevels=2
1106 call assert_equal([], g:options)
1107 call assert_equal(g:opt[0], g:opt[1])
1108
1109 " 24d: Setting again global number global-local (to buffer) option"
1110 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001111 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001112 set undolevels=2
1113 call assert_equal([], g:options)
1114 call assert_equal(g:opt[0], g:opt[1])
1115
1116
1117 " 25a: Setting global number local (to buffer) option"
1118 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1119 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001120 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001121 setglobal wrapmargin=2
1122 call assert_equal([], g:options)
1123 call assert_equal(g:opt[0], g:opt[1])
1124
1125 " 25b: Setting local number local (to buffer) option"
1126 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1127 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001128 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001129 setlocal wrapmargin=2
1130 call assert_equal([], g:options)
1131 call assert_equal(g:opt[0], g:opt[1])
1132
1133 " 25c: Setting again number local (to buffer) option"
1134 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1135 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001136 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001137 set wrapmargin=2
1138 call assert_equal([], g:options)
1139 call assert_equal(g:opt[0], g:opt[1])
1140
1141 " 25d: Setting again global number local (to buffer) option"
1142 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001143 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001144 set wrapmargin=2
1145 call assert_equal([], g:options)
1146 call assert_equal(g:opt[0], g:opt[1])
1147
1148
1149 " 26: Setting number global-local (to window) option.
1150 " Such option does currently not exist.
1151
1152
1153 " 27a: Setting global number local (to window) option"
1154 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1155 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001156 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001157 setglobal foldcolumn=2
1158 call assert_equal([], g:options)
1159 call assert_equal(g:opt[0], g:opt[1])
1160
1161 " 27b: Setting local number local (to window) option"
1162 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1163 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001164 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001165 setlocal foldcolumn=2
1166 call assert_equal([], g:options)
1167 call assert_equal(g:opt[0], g:opt[1])
1168
1169 " 27c: Setting again number local (to window) option"
1170 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1171 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001172 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001173 set foldcolumn=2
1174 call assert_equal([], g:options)
1175 call assert_equal(g:opt[0], g:opt[1])
1176
zeertzjqb811de52021-10-21 10:50:44 +01001177 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001178 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001179 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001180 set foldcolumn=2
1181 call assert_equal([], g:options)
1182 call assert_equal(g:opt[0], g:opt[1])
1183
1184
1185 " 28a: Setting global boolean global option"
1186 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1187 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001188 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001189 setglobal nowrapscan
1190 call assert_equal([], g:options)
1191 call assert_equal(g:opt[0], g:opt[1])
1192
1193 " 28b: Setting local boolean global option"
1194 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1195 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001196 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001197 setlocal nowrapscan
1198 call assert_equal([], g:options)
1199 call assert_equal(g:opt[0], g:opt[1])
1200
1201 " 28c: Setting again boolean global option"
1202 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1203 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001204 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001205 set nowrapscan
1206 call assert_equal([], g:options)
1207 call assert_equal(g:opt[0], g:opt[1])
1208
1209 " 28d: Setting again global boolean global option"
1210 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001211 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001212 set wrapscan
1213 call assert_equal([], g:options)
1214 call assert_equal(g:opt[0], g:opt[1])
1215
1216
1217 " 29a: Setting global boolean global-local (to buffer) option"
1218 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1219 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001220 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001221 setglobal autoread
1222 call assert_equal([], g:options)
1223 call assert_equal(g:opt[0], g:opt[1])
1224
1225 " 29b: Setting local boolean global-local (to buffer) option"
1226 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1227 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001228 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001229 setlocal noautoread
1230 call assert_equal([], g:options)
1231 call assert_equal(g:opt[0], g:opt[1])
1232
1233 " 29c: Setting again boolean global-local (to buffer) option"
1234 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1235 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001236 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001237 set autoread
1238 call assert_equal([], g:options)
1239 call assert_equal(g:opt[0], g:opt[1])
1240
1241 " 29d: Setting again global boolean global-local (to buffer) option"
1242 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001243 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001244 set autoread
1245 call assert_equal([], g:options)
1246 call assert_equal(g:opt[0], g:opt[1])
1247
1248
1249 " 30a: Setting global boolean local (to buffer) option"
1250 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1251 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001252 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001253 setglobal cindent
1254 call assert_equal([], g:options)
1255 call assert_equal(g:opt[0], g:opt[1])
1256
1257 " 30b: Setting local boolean local (to buffer) option"
1258 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1259 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001260 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001261 setlocal nocindent
1262 call assert_equal([], g:options)
1263 call assert_equal(g:opt[0], g:opt[1])
1264
1265 " 30c: Setting again boolean local (to buffer) option"
1266 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1267 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001268 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001269 set cindent
1270 call assert_equal([], g:options)
1271 call assert_equal(g:opt[0], g:opt[1])
1272
1273 " 30d: Setting again global boolean local (to buffer) option"
1274 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001275 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001276 set cindent
1277 call assert_equal([], g:options)
1278 call assert_equal(g:opt[0], g:opt[1])
1279
1280
1281 " 31: Setting boolean global-local (to window) option
1282 " Currently no such option exists.
1283
1284
1285 " 32a: Setting global boolean local (to window) option"
1286 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1287 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001288 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001289 setglobal cursorcolumn
1290 call assert_equal([], g:options)
1291 call assert_equal(g:opt[0], g:opt[1])
1292
1293 " 32b: Setting local boolean local (to window) option"
1294 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1295 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001296 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001297 setlocal nocursorcolumn
1298 call assert_equal([], g:options)
1299 call assert_equal(g:opt[0], g:opt[1])
1300
1301 " 32c: Setting again boolean local (to window) option"
1302 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1303 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001304 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001305 set cursorcolumn
1306 call assert_equal([], g:options)
1307 call assert_equal(g:opt[0], g:opt[1])
1308
1309 " 32d: Setting again global boolean local (to window) option"
1310 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001311 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001312 set cursorcolumn
1313 call assert_equal([], g:options)
1314 call assert_equal(g:opt[0], g:opt[1])
1315
1316
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001317 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001318 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001319 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001320 set backspace=2
1321 call assert_equal([], g:options)
1322 call assert_equal(g:opt[0], g:opt[1])
1323
1324
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001325 " Cleanup
1326 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001327 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001328 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 +02001329 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001330 endfor
1331 call test_override('starting', 0)
1332 delfunc! AutoCommandOptionSet
1333endfunc
1334
1335func Test_OptionSet_diffmode()
1336 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001337 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001338 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001339 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001340
1341 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1342 call assert_equal(0, &l:cul)
1343 diffthis
1344 call assert_equal(1, &l:cul)
1345
1346 vnew
1347 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1348 call assert_equal(0, &l:cul)
1349 diffthis
1350 call assert_equal(1, &l:cul)
1351
1352 diffoff
1353 call assert_equal(0, &l:cul)
1354 call assert_equal(1, getwinvar(2, '&l:cul'))
1355 bw!
1356
1357 call assert_equal(1, &l:cul)
1358 diffoff!
1359 call assert_equal(0, &l:cul)
1360 call assert_equal(0, getwinvar(1, '&l:cul'))
1361 bw!
1362
1363 " Cleanup
1364 au! OptionSet
1365 call test_override('starting', 0)
1366endfunc
1367
1368func Test_OptionSet_diffmode_close()
1369 call test_override('starting', 1)
1370 " 19: Try to close the current window when entering diff mode
1371 " should not segfault
1372 new
1373 au OptionSet diff close
1374
1375 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001376 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001377 call assert_equal(1, &diff)
1378 vnew
1379 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001380 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001381 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001382 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001383 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001384 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001385 bw!
1386
1387 " Cleanup
1388 au! OptionSet
1389 call test_override('starting', 0)
1390 "delfunc! AutoCommandOptionSet
1391endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001392
1393" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1394func Test_BufleaveWithDelete()
1395 new | edit Xfile1
1396
1397 augroup test_bufleavewithdelete
1398 autocmd!
1399 autocmd BufLeave Xfile1 bwipe Xfile2
1400 augroup END
1401
1402 call assert_fails('edit Xfile2', 'E143:')
1403 call assert_equal('Xfile1', bufname('%'))
1404
1405 autocmd! test_bufleavewithdelete BufLeave Xfile1
1406 augroup! test_bufleavewithdelete
1407
1408 new
1409 bwipe! Xfile1
1410endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001411
1412" Test for autocommand that changes the buffer list, when doing ":ball".
1413func Test_Acmd_BufAll()
1414 enew!
1415 %bwipe!
1416 call writefile(['Test file Xxx1'], 'Xxx1')
1417 call writefile(['Test file Xxx2'], 'Xxx2')
1418 call writefile(['Test file Xxx3'], 'Xxx3')
1419
1420 " Add three files to the buffer list
1421 split Xxx1
1422 close
1423 split Xxx2
1424 close
1425 split Xxx3
1426 close
1427
1428 " Wipe the buffer when the buffer is opened
1429 au BufReadPost Xxx2 bwipe
1430
1431 call append(0, 'Test file Xxx4')
1432 ball
1433
1434 call assert_equal(2, winnr('$'))
1435 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1436 wincmd t
1437
1438 au! BufReadPost
1439 %bwipe!
1440 call delete('Xxx1')
1441 call delete('Xxx2')
1442 call delete('Xxx3')
1443 enew! | only
1444endfunc
1445
1446" Test for autocommand that changes current buffer on BufEnter event.
1447" Check if modelines are interpreted for the correct buffer.
1448func Test_Acmd_BufEnter()
1449 %bwipe!
1450 call writefile(['start of test file Xxx1',
1451 \ "\<Tab>this is a test",
1452 \ 'end of test file Xxx1'], 'Xxx1')
1453 call writefile(['start of test file Xxx2',
1454 \ 'vim: set noai :',
1455 \ "\<Tab>this is a test",
1456 \ 'end of test file Xxx2'], 'Xxx2')
1457
1458 au BufEnter Xxx2 brew
1459 set ai modeline modelines=3
1460 edit Xxx1
1461 " edit Xxx2, autocmd will do :brew
1462 edit Xxx2
1463 exe "normal G?this is a\<CR>"
1464 " Append text with autoindent to this file
1465 normal othis should be auto-indented
1466 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1467 call assert_equal(3, line('.'))
1468 " Remove autocmd and edit Xxx2 again
1469 au! BufEnter Xxx2
1470 buf! Xxx2
1471 exe "normal G?this is a\<CR>"
1472 " append text without autoindent to Xxx
1473 normal othis should be in column 1
1474 call assert_equal("this should be in column 1", getline('.'))
1475 call assert_equal(4, line('.'))
1476
1477 %bwipe!
1478 call delete('Xxx1')
1479 call delete('Xxx2')
1480 set ai&vim modeline&vim modelines&vim
1481endfunc
1482
1483" Test for issue #57
1484" do not move cursor on <c-o> when autoindent is set
1485func Test_ai_CTRL_O()
1486 enew!
1487 set ai
1488 let save_fo = &fo
1489 set fo+=r
1490 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1491 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1492 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1493
1494 set ai&vim
1495 let &fo = save_fo
1496 enew!
1497endfunc
1498
1499" Test for autocommand that deletes the current buffer on BufLeave event.
1500" Also test deleting the last buffer, should give a new, empty buffer.
1501func Test_BufLeave_Wipe()
1502 %bwipe!
1503 let content = ['start of test file Xxx',
1504 \ 'this is a test',
1505 \ 'end of test file Xxx']
1506 call writefile(content, 'Xxx1')
1507 call writefile(content, 'Xxx2')
1508
1509 au BufLeave Xxx2 bwipe
1510 edit Xxx1
1511 split Xxx2
1512 " delete buffer Xxx2, we should be back to Xxx1
1513 bwipe
1514 call assert_equal('Xxx1', bufname('%'))
1515 call assert_equal(1, winnr('$'))
1516
1517 " Create an alternate buffer
1518 %write! test.out
1519 call assert_equal('test.out', bufname('#'))
1520 " delete alternate buffer
1521 bwipe test.out
1522 call assert_equal('Xxx1', bufname('%'))
1523 call assert_equal('', bufname('#'))
1524
1525 au BufLeave Xxx1 bwipe
1526 " delete current buffer, get an empty one
1527 bwipe!
1528 call assert_equal(1, line('$'))
1529 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001530 let g:bufinfo = getbufinfo()
1531 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001532
1533 call delete('Xxx1')
1534 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001535 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001536 %bwipe
1537 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001538
1539 " check that bufinfo doesn't contain a pointer to freed memory
1540 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001541endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001542
1543func Test_QuitPre()
1544 edit Xfoo
1545 let winid = win_getid(winnr())
1546 split Xbar
1547 au! QuitPre * let g:afile = expand('<afile>')
1548 " Close the other window, <afile> should be correct.
1549 exe win_id2win(winid) . 'q'
1550 call assert_equal('Xfoo', g:afile)
1551
1552 unlet g:afile
1553 bwipe Xfoo
1554 bwipe Xbar
1555endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001556
1557func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001558 au! CmdlineChanged : let g:text = getcmdline()
1559 let g:text = 0
1560 call feedkeys(":echom 'hello'\<CR>", 'xt')
1561 call assert_equal("echom 'hello'", g:text)
1562 au! CmdlineChanged
1563
1564 au! CmdlineChanged : let g:entered = expand('<afile>')
1565 let g:entered = 0
1566 call feedkeys(":echom 'hello'\<CR>", 'xt')
1567 call assert_equal(':', g:entered)
1568 au! CmdlineChanged
1569
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001570 au! CmdlineEnter : let g:entered = expand('<afile>')
1571 au! CmdlineLeave : let g:left = expand('<afile>')
1572 let g:entered = 0
1573 let g:left = 0
1574 call feedkeys(":echo 'hello'\<CR>", 'xt')
1575 call assert_equal(':', g:entered)
1576 call assert_equal(':', g:left)
1577 au! CmdlineEnter
1578 au! CmdlineLeave
1579
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001580 let save_shellslash = &shellslash
1581 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001582 au! CmdlineEnter / let g:entered = expand('<afile>')
1583 au! CmdlineLeave / let g:left = expand('<afile>')
1584 let g:entered = 0
1585 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001586 new
1587 call setline(1, 'hello')
1588 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001589 call assert_equal('/', g:entered)
1590 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001591 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001592 au! CmdlineEnter
1593 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001594 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001595endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001596
1597" Test for BufWritePre autocommand that deletes or unloads the buffer.
1598func Test_BufWritePre()
1599 %bwipe
1600 au BufWritePre Xxx1 bunload
1601 au BufWritePre Xxx2 bwipe
1602
1603 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1604 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1605
1606 edit Xtest
1607 e! Xxx2
1608 bdel Xtest
1609 e Xxx1
1610 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001611 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001612 call assert_equal('Xxx2', bufname('%'))
1613 edit Xtest
1614 e! Xxx2
1615 bwipe Xtest
1616 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001617 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001618 call assert_equal('Xxx1', bufname('%'))
1619 au! BufWritePre
1620 call delete('Xxx1')
1621 call delete('Xxx2')
1622endfunc
1623
1624" Test for BufUnload autocommand that unloads all the other buffers
1625func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001626 let g:test_is_flaky = 1
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001627 call writefile(['Test file Xxx1'], 'Xxx1')"
1628 call writefile(['Test file Xxx2'], 'Xxx2')"
1629
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001630 let content =<< trim [CODE]
1631 func UnloadAllBufs()
1632 let i = 1
1633 while i <= bufnr('$')
1634 if i != bufnr('%') && bufloaded(i)
1635 exe i . 'bunload'
1636 endif
1637 let i += 1
1638 endwhile
1639 endfunc
1640 au BufUnload * call UnloadAllBufs()
1641 au VimLeave * call writefile(['Test Finished'], 'Xout')
1642 edit Xxx1
1643 split Xxx2
1644 q
1645 [CODE]
1646
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001647 call writefile(content, 'Xtest')
1648
1649 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001650 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001651 call assert_true(filereadable('Xout'))
1652
1653 call delete('Xxx1')
1654 call delete('Xxx2')
1655 call delete('Xtest')
1656 call delete('Xout')
1657endfunc
1658
1659" Some tests for buffer-local autocommands
1660func Test_buflocal_autocmd()
1661 let g:bname = ''
1662 edit xx
1663 au BufLeave <buffer> let g:bname = expand("%")
1664 " here, autocommand for xx should trigger.
1665 " but autocommand shall not apply to buffer named <buffer>.
1666 edit somefile
1667 call assert_equal('xx', g:bname)
1668 let g:bname = ''
1669 " here, autocommand shall be auto-deleted
1670 bwipe xx
1671 " autocmd should not trigger
1672 edit xx
1673 call assert_equal('', g:bname)
1674 " autocmd should not trigger
1675 edit somefile
1676 call assert_equal('', g:bname)
1677 enew
1678 unlet g:bname
1679endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001680
1681" Test for "*Cmd" autocommands
1682func Test_Cmd_Autocmds()
1683 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1684
1685 enew!
1686 au BufReadCmd XtestA 0r Xxx|$del
1687 edit XtestA " will read text of Xxd instead
1688 call assert_equal('start of Xxx', getline(1))
1689
1690 au BufWriteCmd XtestA call append(line("$"), "write")
1691 write " will append a line to the file
1692 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001693 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001694 call assert_equal('write', getline(4))
1695
1696 " now we have:
1697 " 1 start of Xxx
1698 " 2 abc2
1699 " 3 end of Xxx
1700 " 4 write
1701
1702 au FileReadCmd XtestB '[r Xxx
1703 2r XtestB " will read Xxx below line 2 instead
1704 call assert_equal('start of Xxx', getline(3))
1705
1706 " now we have:
1707 " 1 start of Xxx
1708 " 2 abc2
1709 " 3 start of Xxx
1710 " 4 abc2
1711 " 5 end of Xxx
1712 " 6 end of Xxx
1713 " 7 write
1714
1715 au FileWriteCmd XtestC '[,']copy $
1716 normal 4GA1
1717 4,5w XtestC " will copy lines 4 and 5 to the end
1718 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001719 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001720 call assert_equal("end of Xxx", getline(9))
1721
1722 " now we have:
1723 " 1 start of Xxx
1724 " 2 abc2
1725 " 3 start of Xxx
1726 " 4 abc21
1727 " 5 end of Xxx
1728 " 6 end of Xxx
1729 " 7 write
1730 " 8 abc21
1731 " 9 end of Xxx
1732
1733 let g:lines = []
1734 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1735 w >>XtestD " will add lines to 'lines'
1736 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001737 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001738 call assert_equal(9, line('$'))
1739 call assert_equal('end of Xxx', getline('$'))
1740
1741 au BufReadCmd XtestE 0r Xxx|$del
1742 sp XtestE " split window with test.out
1743 call assert_equal('end of Xxx', getline(3))
1744
1745 let g:lines = []
1746 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1747 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1748 wall " will write other window to 'lines'
1749 call assert_equal(4, len(g:lines), g:lines)
1750 call assert_equal('asdf', g:lines[2])
1751
1752 au! BufReadCmd
1753 au! BufWriteCmd
1754 au! FileReadCmd
1755 au! FileWriteCmd
1756 au! FileAppendCmd
1757 %bwipe!
1758 call delete('Xxx')
1759 enew!
1760endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001761
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001762func s:ReadFile()
1763 setl noswapfile nomodified
1764 let filename = resolve(expand("<afile>:p"))
1765 execute 'read' fnameescape(filename)
1766 1d_
1767 exe 'file' fnameescape(filename)
1768 setl buftype=acwrite
1769endfunc
1770
1771func s:WriteFile()
1772 let filename = resolve(expand("<afile>:p"))
1773 setl buftype=
1774 noautocmd execute 'write' fnameescape(filename)
1775 setl buftype=acwrite
1776 setl nomodified
1777endfunc
1778
1779func Test_BufReadCmd()
1780 autocmd BufReadCmd *.test call s:ReadFile()
1781 autocmd BufWriteCmd *.test call s:WriteFile()
1782
1783 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1784 edit Xcmd.test
1785 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1786 normal! Gofour
1787 write
1788 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1789
1790 bwipe!
1791 call delete('Xcmd.test')
1792 au! BufReadCmd
1793 au! BufWriteCmd
1794endfunc
1795
Bram Moolenaaraace2152017-11-05 16:23:10 +01001796func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001797 exe a:start .. 'mark ['
1798 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001799endfunc
1800
1801" Verify the effects of autocmds on '[ and ']
1802func Test_change_mark_in_autocmds()
1803 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001804 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001805
1806 call SetChangeMarks(2, 3)
1807 write
1808 call assert_equal([1, 4], [line("'["), line("']")])
1809
1810 call SetChangeMarks(2, 3)
1811 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1812 write
1813 au! BufWritePre
1814
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001815 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001816 write XtestFilter
1817 write >> XtestFilter
1818
1819 call SetChangeMarks(2, 3)
1820 " Marks are set to the entire range of the write
1821 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1822 " '[ is adjusted to just before the line that will receive the filtered
1823 " data
1824 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1825 " The filtered data is read into the buffer, and the source lines are
1826 " still present, so the range is after the source lines
1827 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1828 %!cat XtestFilter
1829 " After the filtered data is read, the original lines are deleted
1830 call assert_equal([1, 8], [line("'["), line("']")])
1831 au! FilterWritePre,FilterReadPre,FilterReadPost
1832 undo
1833
1834 call SetChangeMarks(1, 4)
1835 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1836 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1837 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1838 2,3!cat XtestFilter
1839 call assert_equal([2, 9], [line("'["), line("']")])
1840 au! FilterWritePre,FilterReadPre,FilterReadPost
1841 undo
1842
1843 call delete('XtestFilter')
1844 endif
1845
1846 call SetChangeMarks(1, 4)
1847 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1848 2,3write Xtest2
1849 au! FileWritePre
1850
1851 call SetChangeMarks(2, 3)
1852 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1853 write >> Xtest2
1854 au! FileAppendPre
1855
1856 call SetChangeMarks(1, 4)
1857 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1858 2,3write >> Xtest2
1859 au! FileAppendPre
1860
1861 call SetChangeMarks(1, 1)
1862 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1863 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1864 3read Xtest2
1865 au! FileReadPre,FileReadPost
1866 undo
1867
1868 call SetChangeMarks(4, 4)
1869 " When the line is 0, it's adjusted to 1
1870 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1871 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1872 0read Xtest2
1873 au! FileReadPre,FileReadPost
1874 undo
1875
1876 call SetChangeMarks(4, 4)
1877 " When the line is 0, it's adjusted to 1
1878 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1879 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1880 1read Xtest2
1881 au! FileReadPre,FileReadPost
1882 undo
1883
1884 bwipe!
1885 call delete('Xtest')
1886 call delete('Xtest2')
1887endfunc
1888
1889func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01001890 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01001891
1892 enew!
1893 call setline(1, ['a', 'b', 'c', 'd'])
1894
1895 let shelltemp = &shelltemp
1896 set shelltemp
1897
1898 let g:filter_au = 0
1899 au FilterWritePre * let g:filter_au += 1
1900 au FilterReadPre * let g:filter_au += 1
1901 au FilterReadPost * let g:filter_au += 1
1902 %!cat
1903 call assert_equal(3, g:filter_au)
1904
1905 if has('filterpipe')
1906 set noshelltemp
1907
1908 let g:filter_au = 0
1909 au FilterWritePre * let g:filter_au += 1
1910 au FilterReadPre * let g:filter_au += 1
1911 au FilterReadPost * let g:filter_au += 1
1912 %!cat
1913 call assert_equal(0, g:filter_au)
1914 endif
1915
1916 au! FilterWritePre,FilterReadPre,FilterReadPost
1917 let &shelltemp = shelltemp
1918 bwipe!
1919endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001920
1921func Test_TextYankPost()
1922 enew!
1923 call setline(1, ['foo'])
1924
1925 let g:event = []
1926 au TextYankPost * let g:event = copy(v:event)
1927
1928 call assert_equal({}, v:event)
1929 call assert_fails('let v:event = {}', 'E46:')
1930 call assert_fails('let v:event.mykey = 0', 'E742:')
1931
1932 norm "ayiw
1933 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001934 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001935 \g:event)
1936 norm y_
1937 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001938 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false},
1939 \g:event)
1940 norm Vy
1941 call assert_equal(
1942 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001943 \g:event)
1944 call feedkeys("\<C-V>y", 'x')
1945 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001946 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001947 \g:event)
1948 norm "xciwbar
1949 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001950 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001951 \g:event)
1952 norm "bdiw
1953 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001954 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001955 \g:event)
1956
1957 call assert_equal({}, v:event)
1958
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001959 if has('clipboard_working') && !has('gui_running')
1960 " Test that when the visual selection is automatically copied to clipboard
1961 " register a TextYankPost is emitted
1962 call setline(1, ['foobar'])
1963
1964 let @* = ''
1965 set clipboard=autoselect
1966 exe "norm! ggviw\<Esc>"
1967 call assert_equal(
1968 \{'regcontents': ['foobar'], 'regname': '*', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1969 \g:event)
1970
1971 let @+ = ''
1972 set clipboard=autoselectplus
1973 exe "norm! ggviw\<Esc>"
1974 call assert_equal(
1975 \{'regcontents': ['foobar'], 'regname': '+', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1976 \g:event)
1977
1978 set clipboard&vim
1979 endif
1980
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001981 au! TextYankPost
1982 unlet g:event
1983 bwipe!
1984endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001985
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01001986func Test_autocommand_all_events()
1987 call assert_fails('au * * bwipe', 'E1155:')
1988 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00001989 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001990endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001991
Bram Moolenaarf6246f52022-02-11 16:30:12 +00001992func Test_autocmd_user()
1993 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
1994 doautocmd User MyEvent
1995 call assert_equal(['MyEvent', 'MyEvent'], s:res)
1996 au! User
1997 unlet s:res
1998endfunc
1999
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002000function s:Before_test_dirchanged()
2001 augroup test_dirchanged
2002 autocmd!
2003 augroup END
2004 let s:li = []
2005 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002006 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002007 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002008 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002009 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002010endfunc
2011
2012function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002013 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002014 call delete(s:dir_foo, 'd')
2015 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002016 augroup test_dirchanged
2017 autocmd!
2018 augroup END
2019endfunc
2020
2021function Test_dirchanged_global()
2022 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002023 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002024 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2025 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002026 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002027 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002028 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002029 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002030 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002031 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002032 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002033 call s:After_test_dirchanged()
2034endfunc
2035
2036function Test_dirchanged_local()
2037 call s:Before_test_dirchanged()
2038 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2039 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002040 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002041 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002042 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002043 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002044 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002045 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002046 call s:After_test_dirchanged()
2047endfunc
2048
2049function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002050 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002051 call s:Before_test_dirchanged()
2052 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002053 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002054 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2055 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2056 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002057 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002058 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002059 exe 'edit ' . s:dir_foo . '/Xfile'
2060 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002061 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2062 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002063 set noacd
2064 bwipe!
2065 call s:After_test_dirchanged()
2066endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002067
2068" Test TextChangedI and TextChangedP
2069func Test_ChangedP()
2070 new
2071 call setline(1, ['foo', 'bar', 'foobar'])
2072 call test_override("char_avail", 1)
2073 set complete=. completeopt=menuone
2074
2075 func! TextChangedAutocmd(char)
2076 let g:autocmd .= a:char
2077 endfunc
2078
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002079 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002080 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2081 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2082 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2083
2084 call cursor(3, 1)
2085 let g:autocmd = ''
2086 call feedkeys("o\<esc>", 'tnix')
2087 call assert_equal('I', g:autocmd)
2088
2089 let g:autocmd = ''
2090 call feedkeys("Sf", 'tnix')
2091 call assert_equal('II', g:autocmd)
2092
2093 let g:autocmd = ''
2094 call feedkeys("Sf\<C-N>", 'tnix')
2095 call assert_equal('IIP', g:autocmd)
2096
2097 let g:autocmd = ''
2098 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2099 call assert_equal('IIPP', g:autocmd)
2100
2101 let g:autocmd = ''
2102 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2103 call assert_equal('IIPPP', g:autocmd)
2104
2105 let g:autocmd = ''
2106 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2107 call assert_equal('IIPPPP', g:autocmd)
2108
2109 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2110 " TODO: how should it handle completeopt=noinsert,noselect?
2111
2112 " CleanUp
2113 call test_override("char_avail", 0)
2114 au! TextChanged
2115 au! TextChangedI
2116 au! TextChangedP
2117 delfu TextChangedAutocmd
2118 unlet! g:autocmd
2119 set complete&vim completeopt&vim
2120
2121 bw!
2122endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002123
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002124let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002125func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002126 if !g:setline_handled
2127 call setline(1, "(x)")
2128 let g:setline_handled = v:true
2129 endif
2130endfunc
2131
2132func Test_TextChangedI_with_setline()
2133 new
2134 call test_override('char_avail', 1)
2135 autocmd TextChangedI <buffer> call SetLineOne()
2136 call feedkeys("i(\<CR>\<Esc>", 'tx')
2137 call assert_equal('(', getline(1))
2138 call assert_equal('x)', getline(2))
2139 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002140 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002141 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002142
Bram Moolenaarca34db32022-01-20 11:17:18 +00002143 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002144 bwipe!
2145endfunc
2146
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002147func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002148 CheckFeature terminal
2149 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002150 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002151 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002152
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002153 " Prepare file for TextChanged event.
2154 call writefile([''], 'Xchanged.txt')
2155 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2156 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002157 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002158 " In ConPTY, there is additional character which is drawn up to the width of
2159 " the screen.
2160 if has('conpty')
2161 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2162 else
2163 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2164 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002165 " It's only adding autocmd, so that no event occurs.
2166 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2167 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002168 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002169 call assert_equal([''], readfile('Xchanged.txt'))
2170
2171 " clean up
2172 call delete('Xchanged.txt')
2173 bwipe!
2174endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002175
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002176func Test_autocmd_nested()
2177 let g:did_nested = 0
2178 augroup Testing
2179 au WinNew * edit somefile
2180 au BufNew * let g:did_nested = 1
2181 augroup END
2182 split
2183 call assert_equal(0, g:did_nested)
2184 close
2185 bwipe! somefile
2186
2187 " old nested argument still works
2188 augroup Testing
2189 au!
2190 au WinNew * nested edit somefile
2191 au BufNew * let g:did_nested = 1
2192 augroup END
2193 split
2194 call assert_equal(1, g:did_nested)
2195 close
2196 bwipe! somefile
2197
2198 " New ++nested argument works
2199 augroup Testing
2200 au!
2201 au WinNew * ++nested edit somefile
2202 au BufNew * let g:did_nested = 1
2203 augroup END
2204 split
2205 call assert_equal(1, g:did_nested)
2206 close
2207 bwipe! somefile
2208
Bram Moolenaarf0775142022-03-04 20:10:38 +00002209 " nested without ++ does not work in Vim9 script
2210 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2211
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002212 augroup Testing
2213 au!
2214 augroup END
2215
2216 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2217 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2218endfunc
2219
2220func Test_autocmd_once()
2221 " Without ++once WinNew triggers twice
2222 let g:did_split = 0
2223 augroup Testing
2224 au WinNew * let g:did_split += 1
2225 augroup END
2226 split
2227 split
2228 call assert_equal(2, g:did_split)
2229 call assert_true(exists('#WinNew'))
2230 close
2231 close
2232
2233 " With ++once WinNew triggers once
2234 let g:did_split = 0
2235 augroup Testing
2236 au!
2237 au WinNew * ++once let g:did_split += 1
2238 augroup END
2239 split
2240 split
2241 call assert_equal(1, g:did_split)
2242 call assert_false(exists('#WinNew'))
2243 close
2244 close
2245
2246 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2247endfunc
2248
Bram Moolenaara68e5952019-04-25 22:22:01 +02002249func Test_autocmd_bufreadpre()
2250 new
2251 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002252 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002253 w! XAutocmdBufReadPre.txt
2254 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002255 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002256 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002257 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002258 wincmd p
2259 let g:wsv1 = winsaveview()
2260 wincmd p
2261 let g:wsv2 = winsaveview()
2262 " triggers BufReadPre, should not move the cursor in either window
2263 " The topline may change one line in a large window.
2264 edit
2265 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2266 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2267 call assert_equal(2, b:bufreadpre)
2268 wincmd p
2269 call assert_equal(g:wsv1.topline, winsaveview().topline)
2270 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2271 call assert_equal(2, b:bufreadpre)
2272 " Now set the cursor position in an BufReadPre autocommand
2273 " (even though the position will be invalid, this should make Vim reset the
2274 " cursor position in the other window.
2275 wincmd p
2276 set cpo+=g
2277 " won't do anything, but try to set the cursor on an invalid lnum
2278 autocmd BufReadPre <buffer> :norm! 70gg
2279 " triggers BufReadPre, should not move the cursor in either window
2280 e
2281 call assert_equal(1, winsaveview().topline)
2282 call assert_equal(1, winsaveview().lnum)
2283 call assert_equal(3, b:bufreadpre)
2284 wincmd p
2285 call assert_equal(g:wsv1.topline, winsaveview().topline)
2286 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2287 call assert_equal(3, b:bufreadpre)
2288 close
2289 close
2290 call delete('XAutocmdBufReadPre.txt')
2291 set cpo-=g
2292endfunc
2293
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002294" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002295
2296" Tests for the following autocommands:
2297" - FileWritePre writing a compressed file
2298" - FileReadPost reading a compressed file
2299" - BufNewFile reading a file template
2300" - BufReadPre decompressing the file to be read
2301" - FilterReadPre substituting characters in the temp file
2302" - FilterReadPost substituting characters after filtering
2303" - FileReadPre set options for decompression
2304" - FileReadPost decompress the file
2305func Test_ReadWrite_Autocmds()
2306 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002307 CheckUnix
2308 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002309
2310 " Make $GZIP empty, "-v" would cause trouble.
2311 let $GZIP = ""
2312
2313 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2314 " being modified outside of Vim (noticed on Solaris).
2315 au FileChangedShell * echo 'caught FileChangedShell'
2316
2317 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2318 augroup Test1
2319 au!
2320 au FileWritePre *.gz '[,']!gzip
2321 au FileWritePost *.gz undo
2322 au FileReadPost *.gz '[,']!gzip -d
2323 augroup END
2324
2325 new
2326 set bin
2327 call append(0, [
2328 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2329 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2330 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2331 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2332 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2333 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2334 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2335 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2336 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2337 \ ])
2338 1,9write! Xtestfile.gz
2339 enew! | close
2340
2341 new
2342 " Read and decompress the testfile
2343 0read Xtestfile.gz
2344 call assert_equal([
2345 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2346 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2347 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2348 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2349 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2350 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2351 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2352 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2353 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2354 \ ], getline(1, 9))
2355 enew! | close
2356
2357 augroup Test1
2358 au!
2359 augroup END
2360
2361 " Test for the FileAppendPre and FileAppendPost autocmds
2362 augroup Test2
2363 au!
2364 au BufNewFile *.c read Xtest.c
2365 au FileAppendPre *.out '[,']s/new/NEW/
2366 au FileAppendPost *.out !cat Xtest.c >> test.out
2367 augroup END
2368
2369 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2370 new foo.c " should load Xtest.c
2371 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2372 w! >> test.out " append it to the output file
2373
2374 let contents = readfile('test.out')
2375 call assert_equal(' * Here is a NEW .c file', contents[2])
2376 call assert_equal(' * Here is a new .c file', contents[5])
2377
2378 call delete('test.out')
2379 enew! | close
2380 augroup Test2
2381 au!
2382 augroup END
2383
2384 " Test for the BufReadPre and BufReadPost autocmds
2385 augroup Test3
2386 au!
2387 " setup autocommands to decompress before reading and re-compress
2388 " afterwards
2389 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2390 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2391 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2392 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2393 augroup END
2394
2395 e! Xtestfile.gz " Edit compressed file
2396 call assert_equal([
2397 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2398 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2399 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2400 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2401 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2402 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2403 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2404 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2405 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2406 \ ], getline(1, 9))
2407
2408 w! >> test.out " Append it to the output file
2409
2410 augroup Test3
2411 au!
2412 augroup END
2413
2414 " Test for the FilterReadPre and FilterReadPost autocmds.
2415 set shelltemp " need temp files here
2416 augroup Test4
2417 au!
2418 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2419 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2420 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2421 au FilterReadPost *.out '[,']s/x/X/g
2422 augroup END
2423
2424 e! test.out " Edit the output file
2425 1,$!cat
2426 call assert_equal([
2427 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2428 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2429 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2430 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2431 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2432 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2433 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2434 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2435 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2436 \ ], getline(1, 9))
2437 call assert_equal([
2438 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2439 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2440 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2441 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2442 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2443 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2444 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2445 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2446 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2447 \ ], readfile('test.out'))
2448
2449 augroup Test4
2450 au!
2451 augroup END
2452 set shelltemp&vim
2453
2454 " Test for the FileReadPre and FileReadPost autocmds.
2455 augroup Test5
2456 au!
2457 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2458 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2459 au FileReadPost *.gz '[,']s/l/L/
2460 augroup END
2461
2462 new
2463 0r Xtestfile.gz " Read compressed file
2464 call assert_equal([
2465 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2466 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2467 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2468 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2469 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2470 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2471 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2472 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2473 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2474 \ ], getline(1, 9))
2475 call assert_equal([
2476 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2477 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2478 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2479 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2480 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2481 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2482 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2483 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2484 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2485 \ ], readfile('Xtestfile.gz'))
2486
2487 augroup Test5
2488 au!
2489 augroup END
2490
2491 au! FileChangedShell
2492 call delete('Xtestfile.gz')
2493 call delete('Xtest.c')
2494 call delete('test.out')
2495endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002496
2497func Test_throw_in_BufWritePre()
2498 new
2499 call setline(1, ['one', 'two', 'three'])
2500 call assert_false(filereadable('Xthefile'))
2501 augroup throwing
2502 au BufWritePre X* throw 'do not write'
2503 augroup END
2504 try
2505 w Xthefile
2506 catch
2507 let caught = 1
2508 endtry
2509 call assert_equal(1, caught)
2510 call assert_false(filereadable('Xthefile'))
2511
2512 bwipe!
2513 au! throwing
2514endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002515
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002516func Test_autocmd_in_try_block()
2517 call mkdir('Xdir')
2518 au BufEnter * let g:fname = expand('%')
2519 try
2520 edit Xdir/
2521 endtry
2522 call assert_match('Xdir', g:fname)
2523
2524 unlet g:fname
2525 au! BufEnter
2526 call delete('Xdir', 'rf')
2527endfunc
2528
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002529func Test_autocmd_SafeState()
2530 CheckRunVimInTerminal
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002531 let g:test_is_flaky = 1
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002532
2533 let lines =<< trim END
2534 let g:safe = 0
2535 let g:again = ''
2536 au SafeState * let g:safe += 1
2537 au SafeStateAgain * let g:again ..= 'x'
2538 func CallTimer()
2539 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2540 endfunc
2541 END
2542 call writefile(lines, 'XSafeState')
2543 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2544
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002545 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002546 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002547 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002548 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002549
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002550 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002551 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002552 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002553
2554 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002555 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002556 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002557 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002558 call term_sendkeys(buf, ":echo g:again\<CR>")
2559 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2560
2561 call StopVimInTerminal(buf)
2562 call delete('XSafeState')
2563endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002564
2565func Test_autocmd_CmdWinEnter()
2566 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002567 CheckFeature cmdwin
2568
Bram Moolenaar23324a02019-10-01 17:39:04 +02002569 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002570 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002571 let b:dummy_var = 'This is a dummy'
2572 autocmd CmdWinEnter * quit
2573 let winnr = winnr('$')
2574 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002575 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002576 call writefile(lines, filename)
2577 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2578
2579 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002580 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002581 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002582 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002583 call term_sendkeys(buf, ":echo &buftype\<cr>")
2584 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2585 call term_sendkeys(buf, ":echo winnr\<cr>")
2586 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2587
2588 " clean up
2589 call StopVimInTerminal(buf)
2590 call delete(filename)
2591endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002592
2593func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002594 CheckFeature quickfix
2595
Bram Moolenaarec66c412019-10-11 21:19:13 +02002596 pedit xx
2597 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002598 augroup winenter
2599 au WinEnter * if winnr('$') > 2 | quit | endif
2600 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002601 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002602
2603 augroup winenter
2604 au! WinEnter
2605 augroup END
2606
2607 bwipe xx
2608 bwipe x
2609 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002610endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002611
2612func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002613 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002614 edit! Xtest
2615 call setline(1, ['a', 'b', 'c', 'd'])
2616
2617 " :lockmarks preserves the marks
2618 call SetChangeMarks(2, 3)
2619 lockmarks write
2620 call assert_equal([2, 3], [line("'["), line("']")])
2621
2622 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2623 " original values for the user
2624 augroup lockmarks
2625 au!
2626 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2627 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2628 augroup END
2629
2630 lockmarks write
2631 call assert_equal([2, 3], [line("'["), line("']")])
2632
2633 if executable('cat')
2634 lockmarks %!cat
2635 call assert_equal([2, 3], [line("'["), line("']")])
2636 endif
2637
2638 lockmarks 3,4write Xtest2
2639 call assert_equal([2, 3], [line("'["), line("']")])
2640
2641 au! lockmarks
2642 augroup! lockmarks
2643 call delete('Xtest')
2644 call delete('Xtest2')
2645endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002646
2647func Test_FileType_spell()
2648 if !isdirectory('/tmp')
2649 throw "Skipped: requires /tmp directory"
2650 endif
2651
2652 " this was crashing with an invalid free()
2653 setglobal spellfile=/tmp/en.utf-8.add
2654 augroup crash
2655 autocmd!
2656 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2657 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2658 autocmd FileType anotherfiletype setlocal spell
2659 augroup END
2660 func! NoCrash() abort
2661 edit /tmp/crashfile
2662 endfunc
2663 call NoCrash()
2664
2665 au! crash
2666 setglobal spellfile=
2667endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002668
Bram Moolenaar406cd902020-02-18 21:54:41 +01002669" Test closing a window or editing another buffer from a FileChangedRO handler
2670" in a readonly buffer
2671func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002672 call test_override('ui_delay', 10)
2673
Bram Moolenaar406cd902020-02-18 21:54:41 +01002674 augroup FileChangedROTest
2675 au!
2676 autocmd FileChangedRO * quit
2677 augroup END
2678 new
2679 set readonly
2680 call assert_fails('normal i', 'E788:')
2681 close
2682 augroup! FileChangedROTest
2683
2684 augroup FileChangedROTest
2685 au!
2686 autocmd FileChangedRO * edit Xfile
2687 augroup END
2688 new
2689 set readonly
2690 call assert_fails('normal i', 'E788:')
2691 close
2692 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002693 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002694endfunc
2695
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002696func LogACmd()
2697 call add(g:logged, line('$'))
2698endfunc
2699
2700func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002701 CheckNotGui
2702
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002703 enew!
2704 tabnew
2705 call setline(1, ['a', 'b', 'c', 'd'])
2706 $
2707 au TermChanged * call LogACmd()
2708 let g:logged = []
2709 let term_save = &term
2710 set term=xterm
2711 call assert_equal([1, 4], g:logged)
2712
2713 au! TermChanged
2714 let &term = term_save
2715 bwipe!
2716endfunc
2717
Bram Moolenaare3284872020-03-19 13:55:03 +01002718" Test for FileReadCmd autocmd
2719func Test_autocmd_FileReadCmd()
2720 func ReadFileCmd()
2721 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2722 endfunc
2723 augroup FileReadCmdTest
2724 au!
2725 au FileReadCmd Xtest call ReadFileCmd()
2726 augroup END
2727
2728 new
2729 read ++bin Xtest
2730 read ++nobin Xtest
2731 read ++edit Xtest
2732 read ++bad=keep Xtest
2733 read ++bad=drop Xtest
2734 read ++bad=- Xtest
2735 read ++ff=unix Xtest
2736 read ++ff=dos Xtest
2737 read ++ff=mac Xtest
2738 read ++enc=utf-8 Xtest
2739
2740 call assert_equal(['',
2741 \ 'v:cmdarg = ++bin',
2742 \ 'v:cmdarg = ++nobin',
2743 \ 'v:cmdarg = ++edit',
2744 \ 'v:cmdarg = ++bad=keep',
2745 \ 'v:cmdarg = ++bad=drop',
2746 \ 'v:cmdarg = ++bad=-',
2747 \ 'v:cmdarg = ++ff=unix',
2748 \ 'v:cmdarg = ++ff=dos',
2749 \ 'v:cmdarg = ++ff=mac',
2750 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2751
2752 close!
2753 augroup FileReadCmdTest
2754 au!
2755 augroup END
2756 delfunc ReadFileCmd
2757endfunc
2758
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002759" Test for passing invalid arguments to autocmd
2760func Test_autocmd_invalid_args()
2761 " Additional character after * for event
2762 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2763 augroup Test
2764 augroup END
2765 " Invalid autocmd event
2766 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2767 " Invalid autocmd event in a autocmd group
2768 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2769 augroup! Test
2770 " Execute all autocmds
2771 call assert_fails('doautocmd * BufEnter', 'E217:')
2772 call assert_fails('augroup! x1a2b3', 'E367:')
2773 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002774 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002775endfunc
2776
2777" Test for deep nesting of autocmds
2778func Test_autocmd_deep_nesting()
2779 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2780 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2781 autocmd! BufEnter Xfile
2782endfunc
2783
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002784" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2785func Test_autocmd_sigusr1()
2786 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002787 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002788
2789 let g:sigusr1_passed = 0
2790 au SigUSR1 * let g:sigusr1_passed = 1
2791 call system('/bin/kill -s usr1 ' . getpid())
2792 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2793
2794 au! SigUSR1
2795 unlet g:sigusr1_passed
2796endfunc
2797
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002798" Test for BufReadPre autocmd deleting the file
2799func Test_BufReadPre_delfile()
2800 augroup TestAuCmd
2801 au!
2802 autocmd BufReadPre Xfile call delete('Xfile')
2803 augroup END
2804 call writefile([], 'Xfile')
2805 call assert_fails('new Xfile', 'E200:')
2806 call assert_equal('Xfile', @%)
2807 call assert_equal(1, &readonly)
2808 call delete('Xfile')
2809 augroup TestAuCmd
2810 au!
2811 augroup END
2812 close!
2813endfunc
2814
2815" Test for BufReadPre autocmd changing the current buffer
2816func Test_BufReadPre_changebuf()
2817 augroup TestAuCmd
2818 au!
2819 autocmd BufReadPre Xfile edit Xsomeotherfile
2820 augroup END
2821 call writefile([], 'Xfile')
2822 call assert_fails('new Xfile', 'E201:')
2823 call assert_equal('Xsomeotherfile', @%)
2824 call assert_equal(1, &readonly)
2825 call delete('Xfile')
2826 augroup TestAuCmd
2827 au!
2828 augroup END
2829 close!
2830endfunc
2831
2832" Test for BufWipeouti autocmd changing the current buffer when reading a file
2833" in an empty buffer with 'f' flag in 'cpo'
2834func Test_BufDelete_changebuf()
2835 new
2836 augroup TestAuCmd
2837 au!
2838 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2839 augroup END
2840 let save_cpo = &cpo
2841 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002842 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002843 call assert_equal('somefile', @%)
2844 let &cpo = save_cpo
2845 augroup TestAuCmd
2846 au!
2847 augroup END
2848 close!
2849endfunc
2850
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002851" Test for the temporary internal window used to execute autocmds
2852func Test_autocmd_window()
2853 %bw!
2854 edit one.txt
2855 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002856 vnew three.txt
2857 tabnew four.txt
2858 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002859 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002860 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002861 au!
2862 au BufEnter * call add(g:blist, [expand('<afile>'),
2863 \ win_gettype(bufwinnr(expand('<afile>')))])
2864 augroup END
2865
2866 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002867 call assert_equal([
2868 \ ['one.txt', 'autocmd'],
2869 \ ['two.txt', ''],
2870 \ ['four.txt', 'autocmd'],
2871 \ ['three.txt', ''],
2872 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002873
Bram Moolenaar832adf92020-06-25 19:01:36 +02002874 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002875 au!
2876 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002877 augroup! aucmd_win_test1
2878 %bw!
2879endfunc
2880
2881" Test for trying to close the temporary window used for executing an autocmd
2882func Test_close_autocmd_window()
2883 %bw!
2884 edit one.txt
2885 tabnew two.txt
2886 augroup aucmd_win_test2
2887 au!
2888 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2889 augroup END
2890
2891 call assert_fails('doautoall BufEnter', 'E813:')
2892
2893 augroup aucmd_win_test2
2894 au!
2895 augroup END
2896 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002897 %bwipe!
2898endfunc
2899
2900" Test for trying to close the tab that has the temporary window for exeucing
2901" an autocmd.
2902func Test_close_autocmd_tab()
2903 edit one.txt
2904 tabnew two.txt
2905 augroup aucmd_win_test
2906 au!
2907 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2908 augroup END
2909
2910 call assert_fails('doautoall BufEnter', 'E813:')
2911
2912 tabonly
2913 augroup aucmd_win_test
2914 au!
2915 augroup END
2916 augroup! aucmd_win_test
2917 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002918endfunc
2919
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00002920func Test_Visual_doautoall_redraw()
2921 call setline(1, ['a', 'b'])
2922 new
2923 wincmd p
2924 call feedkeys("G\<C-V>", 'txn')
2925 autocmd User Explode ++once redraw
2926 doautoall User Explode
2927 %bwipe!
2928endfunc
2929
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01002930" This was using freed memory.
2931func Test_BufNew_arglocal()
2932 arglocal
2933 au BufNew * arglocal
2934 call assert_fails('drop xx', 'E1156:')
2935
2936 au! BufNew
2937endfunc
2938
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002939func Test_autocmd_closes_window()
2940 au BufNew,BufWinLeave * e %e
2941 file yyy
2942 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002943 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002944
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002945 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002946 au! BufNew
2947 au! BufWinLeave
2948endfunc
2949
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002950func Test_autocmd_quit_psearch()
2951 sn aa bb
2952 augroup aucmd_win_test
2953 au!
2954 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
2955 augroup END
2956 ps /
2957
2958 augroup aucmd_win_test
2959 au!
2960 augroup END
2961endfunc
2962
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002963" Fuzzer found some strange combination that caused a crash.
2964func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002965 " For unknown reason this hangs on MS-Windows
2966 CheckNotMSWindows
2967
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002968 augroup aucmd_normal_test
2969 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
2970 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002971 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002972 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002973 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002974 normal G
2975
2976 augroup aucmd_normal_test
2977 au!
2978 augroup END
2979endfunc
2980
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002981func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002982 " For unknown reason this hangs on MS-Windows
2983 CheckNotMSWindows
2984
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002985 au BufWinLeave * nested q
2986 call assert_fails("norm 7q?\n", 'E855:')
2987
2988 au! BufWinLeave
2989 new
2990 only
2991endfunc
2992
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002993func Test_autocmd_vimgrep()
2994 augroup aucmd_vimgrep
2995 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * sb
2996 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * q9�
2997 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002998 %bwipe!
Bram Moolenaardd07c022021-02-07 13:32:46 +01002999 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003000
3001 augroup aucmd_vimgrep
3002 au!
3003 augroup END
3004endfunc
3005
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003006func Test_autocmd_with_block()
3007 augroup block_testing
3008 au BufReadPost *.xml {
3009 setlocal matchpairs+=<:>
3010 /<start
3011 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003012 au CursorHold * {
3013 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3014 g:gotSafeState = 77
3015 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003016 augroup END
3017
3018 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3019 call assert_equal(expected, execute('au BufReadPost *.xml'))
3020
Bram Moolenaar63b91732021-08-05 20:40:03 +02003021 doautocmd CursorHold
3022 call assert_equal(77, g:gotSafeState)
3023 unlet g:gotSafeState
3024
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003025 augroup block_testing
3026 au!
3027 augroup END
3028endfunc
3029
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003030" Test TextChangedI and TextChanged
3031func Test_Changed_ChangedI()
3032 new
3033 call test_override("char_avail", 1)
3034 let [g:autocmd_i, g:autocmd_n] = ['','']
3035
3036 func! TextChangedAutocmdI(char)
3037 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3038 endfunc
3039
3040 augroup Test_TextChanged
3041 au!
3042 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3043 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3044 augroup END
3045
3046 call feedkeys("ifoo\<esc>", 'tnix')
3047 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3048 " requires running Vim in a terminal window.
3049 " call assert_equal('N3', g:autocmd_n)
3050 call assert_equal('I3', g:autocmd_i)
3051
3052 call feedkeys("yyp", 'tnix')
3053 " TODO: Test test does not seem to trigger TextChanged autocommand.
3054 " call assert_equal('N4', g:autocmd_n)
3055 call assert_equal('I3', g:autocmd_i)
3056
3057 " CleanUp
3058 call test_override("char_avail", 0)
3059 au! TextChanged <buffer>
3060 au! TextChangedI <buffer>
3061 augroup! Test_TextChanged
3062 delfu TextChangedAutocmdI
3063 unlet! g:autocmd_i g:autocmd_n
3064
3065 bw!
3066endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003067
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003068func Test_closing_autocmd_window()
3069 let lines =<< trim END
3070 edit Xa.txt
3071 tabnew Xb.txt
3072 autocmd BufEnter Xa.txt unhide 1
3073 doautoall BufEnter
3074 END
3075 call v9.CheckScriptFailure(lines, 'E814:')
3076 au! BufEnter
3077 only!
3078 bwipe Xa.txt
3079 bwipe Xb.txt
3080endfunc
3081
Bram Moolenaar347538f2022-03-26 16:28:06 +00003082func Test_bufwipeout_changes_window()
3083 " This should not crash, but we don't have any expectations about what
3084 " happens, changing window in BufWipeout has unpredictable results.
3085 tabedit
3086 let g:window_id = win_getid()
3087 topleft new
3088 setlocal bufhidden=wipe
3089 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3090 tabprevious
3091 +tabclose
3092
3093 unlet g:window_id
3094 au! BufWipeout
3095 %bwipe!
3096endfunc
3097
3098
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003099" vim: shiftwidth=2 sts=2 expandtab