blob: 9af79d8e1ba31ed2dbbff3b379ce8c0e16f4c54c [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 Moolenaara016eeb2022-04-09 11:37:38 +01001934 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
1935 \ regtype: 'v', visual: v:false, inclusive: v:true},
1936 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001937 norm y_
1938 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01001939 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
1940 \ visual: v:false, inclusive: v:false},
1941 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02001942 norm Vy
1943 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01001944 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
1945 \ visual: v:true, inclusive: v:true},
1946 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001947 call feedkeys("\<C-V>y", 'x')
1948 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01001949 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
1950 \ visual: v:true, inclusive: v:true},
1951 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001952 norm "xciwbar
1953 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01001954 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
1955 \ visual: v:false, inclusive: v:true},
1956 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001957 norm "bdiw
1958 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01001959 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
1960 \ visual: v:false, inclusive: v:true},
1961 \ g:event)
1962
1963 call setline(1, 'foobar')
1964 " exclusive motion
1965 norm $"ay0
1966 call assert_equal(
1967 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
1968 \ visual: v:false, inclusive: v:false},
1969 \ g:event)
1970 " inclusive motion
1971 norm 0"ay$
1972 call assert_equal(
1973 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
1974 \ visual: v:false, inclusive: v:true},
1975 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001976
1977 call assert_equal({}, v:event)
1978
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001979 if has('clipboard_working') && !has('gui_running')
1980 " Test that when the visual selection is automatically copied to clipboard
1981 " register a TextYankPost is emitted
1982 call setline(1, ['foobar'])
1983
1984 let @* = ''
1985 set clipboard=autoselect
1986 exe "norm! ggviw\<Esc>"
1987 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01001988 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
1989 \ regtype: 'v', visual: v:true, inclusive: v:false},
1990 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001991
1992 let @+ = ''
1993 set clipboard=autoselectplus
1994 exe "norm! ggviw\<Esc>"
1995 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01001996 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
1997 \ regtype: 'v', visual: v:true, inclusive: v:false},
1998 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001999
2000 set clipboard&vim
2001 endif
2002
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002003 au! TextYankPost
2004 unlet g:event
2005 bwipe!
2006endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002007
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002008func Test_autocommand_all_events()
2009 call assert_fails('au * * bwipe', 'E1155:')
2010 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002011 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002012endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002013
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002014func Test_autocmd_user()
2015 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2016 doautocmd User MyEvent
2017 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2018 au! User
2019 unlet s:res
2020endfunc
2021
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002022function s:Before_test_dirchanged()
2023 augroup test_dirchanged
2024 autocmd!
2025 augroup END
2026 let s:li = []
2027 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002028 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002029 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002030 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002031 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002032endfunc
2033
2034function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002035 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002036 call delete(s:dir_foo, 'd')
2037 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002038 augroup test_dirchanged
2039 autocmd!
2040 augroup END
2041endfunc
2042
2043function Test_dirchanged_global()
2044 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002045 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002046 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2047 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002048 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002049 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002050 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002051 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002052 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002053 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002054 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002055 call s:After_test_dirchanged()
2056endfunc
2057
2058function Test_dirchanged_local()
2059 call s:Before_test_dirchanged()
2060 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2061 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002062 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002063 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002064 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002065 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002066 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002067 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002068 call s:After_test_dirchanged()
2069endfunc
2070
2071function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002072 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002073 call s:Before_test_dirchanged()
2074 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002075 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002076 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2077 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2078 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002079 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002080 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002081 exe 'edit ' . s:dir_foo . '/Xfile'
2082 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002083 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2084 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002085 set noacd
2086 bwipe!
2087 call s:After_test_dirchanged()
2088endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002089
2090" Test TextChangedI and TextChangedP
2091func Test_ChangedP()
2092 new
2093 call setline(1, ['foo', 'bar', 'foobar'])
2094 call test_override("char_avail", 1)
2095 set complete=. completeopt=menuone
2096
2097 func! TextChangedAutocmd(char)
2098 let g:autocmd .= a:char
2099 endfunc
2100
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002101 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002102 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2103 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2104 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2105
2106 call cursor(3, 1)
2107 let g:autocmd = ''
2108 call feedkeys("o\<esc>", 'tnix')
2109 call assert_equal('I', g:autocmd)
2110
2111 let g:autocmd = ''
2112 call feedkeys("Sf", 'tnix')
2113 call assert_equal('II', g:autocmd)
2114
2115 let g:autocmd = ''
2116 call feedkeys("Sf\<C-N>", 'tnix')
2117 call assert_equal('IIP', g:autocmd)
2118
2119 let g:autocmd = ''
2120 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2121 call assert_equal('IIPP', g:autocmd)
2122
2123 let g:autocmd = ''
2124 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2125 call assert_equal('IIPPP', g:autocmd)
2126
2127 let g:autocmd = ''
2128 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2129 call assert_equal('IIPPPP', g:autocmd)
2130
2131 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2132 " TODO: how should it handle completeopt=noinsert,noselect?
2133
2134 " CleanUp
2135 call test_override("char_avail", 0)
2136 au! TextChanged
2137 au! TextChangedI
2138 au! TextChangedP
2139 delfu TextChangedAutocmd
2140 unlet! g:autocmd
2141 set complete&vim completeopt&vim
2142
2143 bw!
2144endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002145
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002146let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002147func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002148 if !g:setline_handled
2149 call setline(1, "(x)")
2150 let g:setline_handled = v:true
2151 endif
2152endfunc
2153
2154func Test_TextChangedI_with_setline()
2155 new
2156 call test_override('char_avail', 1)
2157 autocmd TextChangedI <buffer> call SetLineOne()
2158 call feedkeys("i(\<CR>\<Esc>", 'tx')
2159 call assert_equal('(', getline(1))
2160 call assert_equal('x)', getline(2))
2161 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002162 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002163 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002164
Bram Moolenaarca34db32022-01-20 11:17:18 +00002165 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002166 bwipe!
2167endfunc
2168
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002169func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002170 CheckFeature terminal
2171 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002172 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002173 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002174
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002175 " Prepare file for TextChanged event.
2176 call writefile([''], 'Xchanged.txt')
2177 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2178 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002179 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002180 " In ConPTY, there is additional character which is drawn up to the width of
2181 " the screen.
2182 if has('conpty')
2183 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2184 else
2185 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2186 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002187 " It's only adding autocmd, so that no event occurs.
2188 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2189 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002190 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002191 call assert_equal([''], readfile('Xchanged.txt'))
2192
2193 " clean up
2194 call delete('Xchanged.txt')
2195 bwipe!
2196endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002197
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002198func Test_autocmd_nested()
2199 let g:did_nested = 0
2200 augroup Testing
2201 au WinNew * edit somefile
2202 au BufNew * let g:did_nested = 1
2203 augroup END
2204 split
2205 call assert_equal(0, g:did_nested)
2206 close
2207 bwipe! somefile
2208
2209 " old nested argument still works
2210 augroup Testing
2211 au!
2212 au WinNew * nested edit somefile
2213 au BufNew * let g:did_nested = 1
2214 augroup END
2215 split
2216 call assert_equal(1, g:did_nested)
2217 close
2218 bwipe! somefile
2219
2220 " New ++nested argument works
2221 augroup Testing
2222 au!
2223 au WinNew * ++nested edit somefile
2224 au BufNew * let g:did_nested = 1
2225 augroup END
2226 split
2227 call assert_equal(1, g:did_nested)
2228 close
2229 bwipe! somefile
2230
Bram Moolenaarf0775142022-03-04 20:10:38 +00002231 " nested without ++ does not work in Vim9 script
2232 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2233
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002234 augroup Testing
2235 au!
2236 augroup END
2237
2238 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2239 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2240endfunc
2241
2242func Test_autocmd_once()
2243 " Without ++once WinNew triggers twice
2244 let g:did_split = 0
2245 augroup Testing
2246 au WinNew * let g:did_split += 1
2247 augroup END
2248 split
2249 split
2250 call assert_equal(2, g:did_split)
2251 call assert_true(exists('#WinNew'))
2252 close
2253 close
2254
2255 " With ++once WinNew triggers once
2256 let g:did_split = 0
2257 augroup Testing
2258 au!
2259 au WinNew * ++once let g:did_split += 1
2260 augroup END
2261 split
2262 split
2263 call assert_equal(1, g:did_split)
2264 call assert_false(exists('#WinNew'))
2265 close
2266 close
2267
2268 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2269endfunc
2270
Bram Moolenaara68e5952019-04-25 22:22:01 +02002271func Test_autocmd_bufreadpre()
2272 new
2273 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002274 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002275 w! XAutocmdBufReadPre.txt
2276 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002277 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002278 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002279 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002280 wincmd p
2281 let g:wsv1 = winsaveview()
2282 wincmd p
2283 let g:wsv2 = winsaveview()
2284 " triggers BufReadPre, should not move the cursor in either window
2285 " The topline may change one line in a large window.
2286 edit
2287 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2288 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2289 call assert_equal(2, b:bufreadpre)
2290 wincmd p
2291 call assert_equal(g:wsv1.topline, winsaveview().topline)
2292 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2293 call assert_equal(2, b:bufreadpre)
2294 " Now set the cursor position in an BufReadPre autocommand
2295 " (even though the position will be invalid, this should make Vim reset the
2296 " cursor position in the other window.
2297 wincmd p
2298 set cpo+=g
2299 " won't do anything, but try to set the cursor on an invalid lnum
2300 autocmd BufReadPre <buffer> :norm! 70gg
2301 " triggers BufReadPre, should not move the cursor in either window
2302 e
2303 call assert_equal(1, winsaveview().topline)
2304 call assert_equal(1, winsaveview().lnum)
2305 call assert_equal(3, b:bufreadpre)
2306 wincmd p
2307 call assert_equal(g:wsv1.topline, winsaveview().topline)
2308 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2309 call assert_equal(3, b:bufreadpre)
2310 close
2311 close
2312 call delete('XAutocmdBufReadPre.txt')
2313 set cpo-=g
2314endfunc
2315
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002316" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002317
2318" Tests for the following autocommands:
2319" - FileWritePre writing a compressed file
2320" - FileReadPost reading a compressed file
2321" - BufNewFile reading a file template
2322" - BufReadPre decompressing the file to be read
2323" - FilterReadPre substituting characters in the temp file
2324" - FilterReadPost substituting characters after filtering
2325" - FileReadPre set options for decompression
2326" - FileReadPost decompress the file
2327func Test_ReadWrite_Autocmds()
2328 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002329 CheckUnix
2330 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002331
2332 " Make $GZIP empty, "-v" would cause trouble.
2333 let $GZIP = ""
2334
2335 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2336 " being modified outside of Vim (noticed on Solaris).
2337 au FileChangedShell * echo 'caught FileChangedShell'
2338
2339 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2340 augroup Test1
2341 au!
2342 au FileWritePre *.gz '[,']!gzip
2343 au FileWritePost *.gz undo
2344 au FileReadPost *.gz '[,']!gzip -d
2345 augroup END
2346
2347 new
2348 set bin
2349 call append(0, [
2350 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2351 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2352 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2353 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2354 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2355 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2356 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2357 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2358 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2359 \ ])
2360 1,9write! Xtestfile.gz
2361 enew! | close
2362
2363 new
2364 " Read and decompress the testfile
2365 0read Xtestfile.gz
2366 call assert_equal([
2367 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2368 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2369 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2370 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2371 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2372 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2373 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2374 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2375 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2376 \ ], getline(1, 9))
2377 enew! | close
2378
2379 augroup Test1
2380 au!
2381 augroup END
2382
2383 " Test for the FileAppendPre and FileAppendPost autocmds
2384 augroup Test2
2385 au!
2386 au BufNewFile *.c read Xtest.c
2387 au FileAppendPre *.out '[,']s/new/NEW/
2388 au FileAppendPost *.out !cat Xtest.c >> test.out
2389 augroup END
2390
2391 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2392 new foo.c " should load Xtest.c
2393 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2394 w! >> test.out " append it to the output file
2395
2396 let contents = readfile('test.out')
2397 call assert_equal(' * Here is a NEW .c file', contents[2])
2398 call assert_equal(' * Here is a new .c file', contents[5])
2399
2400 call delete('test.out')
2401 enew! | close
2402 augroup Test2
2403 au!
2404 augroup END
2405
2406 " Test for the BufReadPre and BufReadPost autocmds
2407 augroup Test3
2408 au!
2409 " setup autocommands to decompress before reading and re-compress
2410 " afterwards
2411 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2412 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2413 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2414 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2415 augroup END
2416
2417 e! Xtestfile.gz " Edit compressed file
2418 call assert_equal([
2419 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2420 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2421 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2422 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2423 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2424 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2425 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2426 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2427 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2428 \ ], getline(1, 9))
2429
2430 w! >> test.out " Append it to the output file
2431
2432 augroup Test3
2433 au!
2434 augroup END
2435
2436 " Test for the FilterReadPre and FilterReadPost autocmds.
2437 set shelltemp " need temp files here
2438 augroup Test4
2439 au!
2440 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2441 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2442 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2443 au FilterReadPost *.out '[,']s/x/X/g
2444 augroup END
2445
2446 e! test.out " Edit the output file
2447 1,$!cat
2448 call assert_equal([
2449 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2450 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2451 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2452 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2453 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2454 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2455 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2456 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2457 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2458 \ ], getline(1, 9))
2459 call assert_equal([
2460 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2461 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2462 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2463 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2464 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2465 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2466 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2467 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2468 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2469 \ ], readfile('test.out'))
2470
2471 augroup Test4
2472 au!
2473 augroup END
2474 set shelltemp&vim
2475
2476 " Test for the FileReadPre and FileReadPost autocmds.
2477 augroup Test5
2478 au!
2479 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2480 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2481 au FileReadPost *.gz '[,']s/l/L/
2482 augroup END
2483
2484 new
2485 0r Xtestfile.gz " Read compressed file
2486 call assert_equal([
2487 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2488 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2489 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2490 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2491 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2492 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2493 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2494 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2495 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2496 \ ], getline(1, 9))
2497 call assert_equal([
2498 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2499 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2500 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2501 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2502 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2503 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2504 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2505 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2506 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2507 \ ], readfile('Xtestfile.gz'))
2508
2509 augroup Test5
2510 au!
2511 augroup END
2512
2513 au! FileChangedShell
2514 call delete('Xtestfile.gz')
2515 call delete('Xtest.c')
2516 call delete('test.out')
2517endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002518
2519func Test_throw_in_BufWritePre()
2520 new
2521 call setline(1, ['one', 'two', 'three'])
2522 call assert_false(filereadable('Xthefile'))
2523 augroup throwing
2524 au BufWritePre X* throw 'do not write'
2525 augroup END
2526 try
2527 w Xthefile
2528 catch
2529 let caught = 1
2530 endtry
2531 call assert_equal(1, caught)
2532 call assert_false(filereadable('Xthefile'))
2533
2534 bwipe!
2535 au! throwing
2536endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002537
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002538func Test_autocmd_in_try_block()
2539 call mkdir('Xdir')
2540 au BufEnter * let g:fname = expand('%')
2541 try
2542 edit Xdir/
2543 endtry
2544 call assert_match('Xdir', g:fname)
2545
2546 unlet g:fname
2547 au! BufEnter
2548 call delete('Xdir', 'rf')
2549endfunc
2550
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002551func Test_autocmd_SafeState()
2552 CheckRunVimInTerminal
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002553 let g:test_is_flaky = 1
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002554
2555 let lines =<< trim END
2556 let g:safe = 0
2557 let g:again = ''
2558 au SafeState * let g:safe += 1
2559 au SafeStateAgain * let g:again ..= 'x'
2560 func CallTimer()
2561 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2562 endfunc
2563 END
2564 call writefile(lines, 'XSafeState')
2565 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2566
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002567 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002568 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002569 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002570 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002571
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002572 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002573 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002574 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002575
2576 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002577 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002578 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002579 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002580 call term_sendkeys(buf, ":echo g:again\<CR>")
2581 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2582
2583 call StopVimInTerminal(buf)
2584 call delete('XSafeState')
2585endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002586
2587func Test_autocmd_CmdWinEnter()
2588 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002589 CheckFeature cmdwin
2590
Bram Moolenaar23324a02019-10-01 17:39:04 +02002591 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002592 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002593 let b:dummy_var = 'This is a dummy'
2594 autocmd CmdWinEnter * quit
2595 let winnr = winnr('$')
2596 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002597 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002598 call writefile(lines, filename)
2599 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2600
2601 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002602 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002603 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002604 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002605 call term_sendkeys(buf, ":echo &buftype\<cr>")
2606 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2607 call term_sendkeys(buf, ":echo winnr\<cr>")
2608 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2609
2610 " clean up
2611 call StopVimInTerminal(buf)
2612 call delete(filename)
2613endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002614
2615func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002616 CheckFeature quickfix
2617
Bram Moolenaarec66c412019-10-11 21:19:13 +02002618 pedit xx
2619 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002620 augroup winenter
2621 au WinEnter * if winnr('$') > 2 | quit | endif
2622 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002623 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002624
2625 augroup winenter
2626 au! WinEnter
2627 augroup END
2628
2629 bwipe xx
2630 bwipe x
2631 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002632endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002633
2634func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002635 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002636 edit! Xtest
2637 call setline(1, ['a', 'b', 'c', 'd'])
2638
2639 " :lockmarks preserves the marks
2640 call SetChangeMarks(2, 3)
2641 lockmarks write
2642 call assert_equal([2, 3], [line("'["), line("']")])
2643
2644 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2645 " original values for the user
2646 augroup lockmarks
2647 au!
2648 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2649 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2650 augroup END
2651
2652 lockmarks write
2653 call assert_equal([2, 3], [line("'["), line("']")])
2654
2655 if executable('cat')
2656 lockmarks %!cat
2657 call assert_equal([2, 3], [line("'["), line("']")])
2658 endif
2659
2660 lockmarks 3,4write Xtest2
2661 call assert_equal([2, 3], [line("'["), line("']")])
2662
2663 au! lockmarks
2664 augroup! lockmarks
2665 call delete('Xtest')
2666 call delete('Xtest2')
2667endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002668
2669func Test_FileType_spell()
2670 if !isdirectory('/tmp')
2671 throw "Skipped: requires /tmp directory"
2672 endif
2673
2674 " this was crashing with an invalid free()
2675 setglobal spellfile=/tmp/en.utf-8.add
2676 augroup crash
2677 autocmd!
2678 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2679 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2680 autocmd FileType anotherfiletype setlocal spell
2681 augroup END
2682 func! NoCrash() abort
2683 edit /tmp/crashfile
2684 endfunc
2685 call NoCrash()
2686
2687 au! crash
2688 setglobal spellfile=
2689endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002690
Bram Moolenaar406cd902020-02-18 21:54:41 +01002691" Test closing a window or editing another buffer from a FileChangedRO handler
2692" in a readonly buffer
2693func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002694 call test_override('ui_delay', 10)
2695
Bram Moolenaar406cd902020-02-18 21:54:41 +01002696 augroup FileChangedROTest
2697 au!
2698 autocmd FileChangedRO * quit
2699 augroup END
2700 new
2701 set readonly
2702 call assert_fails('normal i', 'E788:')
2703 close
2704 augroup! FileChangedROTest
2705
2706 augroup FileChangedROTest
2707 au!
2708 autocmd FileChangedRO * edit Xfile
2709 augroup END
2710 new
2711 set readonly
2712 call assert_fails('normal i', 'E788:')
2713 close
2714 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002715 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002716endfunc
2717
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002718func LogACmd()
2719 call add(g:logged, line('$'))
2720endfunc
2721
2722func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002723 CheckNotGui
2724
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002725 enew!
2726 tabnew
2727 call setline(1, ['a', 'b', 'c', 'd'])
2728 $
2729 au TermChanged * call LogACmd()
2730 let g:logged = []
2731 let term_save = &term
2732 set term=xterm
2733 call assert_equal([1, 4], g:logged)
2734
2735 au! TermChanged
2736 let &term = term_save
2737 bwipe!
2738endfunc
2739
Bram Moolenaare3284872020-03-19 13:55:03 +01002740" Test for FileReadCmd autocmd
2741func Test_autocmd_FileReadCmd()
2742 func ReadFileCmd()
2743 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2744 endfunc
2745 augroup FileReadCmdTest
2746 au!
2747 au FileReadCmd Xtest call ReadFileCmd()
2748 augroup END
2749
2750 new
2751 read ++bin Xtest
2752 read ++nobin Xtest
2753 read ++edit Xtest
2754 read ++bad=keep Xtest
2755 read ++bad=drop Xtest
2756 read ++bad=- Xtest
2757 read ++ff=unix Xtest
2758 read ++ff=dos Xtest
2759 read ++ff=mac Xtest
2760 read ++enc=utf-8 Xtest
2761
2762 call assert_equal(['',
2763 \ 'v:cmdarg = ++bin',
2764 \ 'v:cmdarg = ++nobin',
2765 \ 'v:cmdarg = ++edit',
2766 \ 'v:cmdarg = ++bad=keep',
2767 \ 'v:cmdarg = ++bad=drop',
2768 \ 'v:cmdarg = ++bad=-',
2769 \ 'v:cmdarg = ++ff=unix',
2770 \ 'v:cmdarg = ++ff=dos',
2771 \ 'v:cmdarg = ++ff=mac',
2772 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2773
2774 close!
2775 augroup FileReadCmdTest
2776 au!
2777 augroup END
2778 delfunc ReadFileCmd
2779endfunc
2780
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002781" Test for passing invalid arguments to autocmd
2782func Test_autocmd_invalid_args()
2783 " Additional character after * for event
2784 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2785 augroup Test
2786 augroup END
2787 " Invalid autocmd event
2788 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2789 " Invalid autocmd event in a autocmd group
2790 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2791 augroup! Test
2792 " Execute all autocmds
2793 call assert_fails('doautocmd * BufEnter', 'E217:')
2794 call assert_fails('augroup! x1a2b3', 'E367:')
2795 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002796 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002797endfunc
2798
2799" Test for deep nesting of autocmds
2800func Test_autocmd_deep_nesting()
2801 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2802 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2803 autocmd! BufEnter Xfile
2804endfunc
2805
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002806" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2807func Test_autocmd_sigusr1()
2808 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002809 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002810
2811 let g:sigusr1_passed = 0
2812 au SigUSR1 * let g:sigusr1_passed = 1
2813 call system('/bin/kill -s usr1 ' . getpid())
2814 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2815
2816 au! SigUSR1
2817 unlet g:sigusr1_passed
2818endfunc
2819
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002820" Test for BufReadPre autocmd deleting the file
2821func Test_BufReadPre_delfile()
2822 augroup TestAuCmd
2823 au!
2824 autocmd BufReadPre Xfile call delete('Xfile')
2825 augroup END
2826 call writefile([], 'Xfile')
2827 call assert_fails('new Xfile', 'E200:')
2828 call assert_equal('Xfile', @%)
2829 call assert_equal(1, &readonly)
2830 call delete('Xfile')
2831 augroup TestAuCmd
2832 au!
2833 augroup END
2834 close!
2835endfunc
2836
2837" Test for BufReadPre autocmd changing the current buffer
2838func Test_BufReadPre_changebuf()
2839 augroup TestAuCmd
2840 au!
2841 autocmd BufReadPre Xfile edit Xsomeotherfile
2842 augroup END
2843 call writefile([], 'Xfile')
2844 call assert_fails('new Xfile', 'E201:')
2845 call assert_equal('Xsomeotherfile', @%)
2846 call assert_equal(1, &readonly)
2847 call delete('Xfile')
2848 augroup TestAuCmd
2849 au!
2850 augroup END
2851 close!
2852endfunc
2853
2854" Test for BufWipeouti autocmd changing the current buffer when reading a file
2855" in an empty buffer with 'f' flag in 'cpo'
2856func Test_BufDelete_changebuf()
2857 new
2858 augroup TestAuCmd
2859 au!
2860 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2861 augroup END
2862 let save_cpo = &cpo
2863 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002864 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002865 call assert_equal('somefile', @%)
2866 let &cpo = save_cpo
2867 augroup TestAuCmd
2868 au!
2869 augroup END
2870 close!
2871endfunc
2872
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002873" Test for the temporary internal window used to execute autocmds
2874func Test_autocmd_window()
2875 %bw!
2876 edit one.txt
2877 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002878 vnew three.txt
2879 tabnew four.txt
2880 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002881 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002882 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002883 au!
2884 au BufEnter * call add(g:blist, [expand('<afile>'),
2885 \ win_gettype(bufwinnr(expand('<afile>')))])
2886 augroup END
2887
2888 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002889 call assert_equal([
2890 \ ['one.txt', 'autocmd'],
2891 \ ['two.txt', ''],
2892 \ ['four.txt', 'autocmd'],
2893 \ ['three.txt', ''],
2894 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002895
Bram Moolenaar832adf92020-06-25 19:01:36 +02002896 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002897 au!
2898 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002899 augroup! aucmd_win_test1
2900 %bw!
2901endfunc
2902
2903" Test for trying to close the temporary window used for executing an autocmd
2904func Test_close_autocmd_window()
2905 %bw!
2906 edit one.txt
2907 tabnew two.txt
2908 augroup aucmd_win_test2
2909 au!
2910 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2911 augroup END
2912
2913 call assert_fails('doautoall BufEnter', 'E813:')
2914
2915 augroup aucmd_win_test2
2916 au!
2917 augroup END
2918 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002919 %bwipe!
2920endfunc
2921
2922" Test for trying to close the tab that has the temporary window for exeucing
2923" an autocmd.
2924func Test_close_autocmd_tab()
2925 edit one.txt
2926 tabnew two.txt
2927 augroup aucmd_win_test
2928 au!
2929 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2930 augroup END
2931
2932 call assert_fails('doautoall BufEnter', 'E813:')
2933
2934 tabonly
2935 augroup aucmd_win_test
2936 au!
2937 augroup END
2938 augroup! aucmd_win_test
2939 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002940endfunc
2941
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00002942func Test_Visual_doautoall_redraw()
2943 call setline(1, ['a', 'b'])
2944 new
2945 wincmd p
2946 call feedkeys("G\<C-V>", 'txn')
2947 autocmd User Explode ++once redraw
2948 doautoall User Explode
2949 %bwipe!
2950endfunc
2951
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01002952" This was using freed memory.
2953func Test_BufNew_arglocal()
2954 arglocal
2955 au BufNew * arglocal
2956 call assert_fails('drop xx', 'E1156:')
2957
2958 au! BufNew
2959endfunc
2960
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002961func Test_autocmd_closes_window()
2962 au BufNew,BufWinLeave * e %e
2963 file yyy
2964 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002965 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002966
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002967 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002968 au! BufNew
2969 au! BufWinLeave
2970endfunc
2971
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002972func Test_autocmd_quit_psearch()
2973 sn aa bb
2974 augroup aucmd_win_test
2975 au!
2976 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
2977 augroup END
2978 ps /
2979
2980 augroup aucmd_win_test
2981 au!
2982 augroup END
2983endfunc
2984
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002985" Fuzzer found some strange combination that caused a crash.
2986func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002987 " For unknown reason this hangs on MS-Windows
2988 CheckNotMSWindows
2989
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002990 augroup aucmd_normal_test
2991 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
2992 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002993 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002994 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002995 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002996 normal G
2997
2998 augroup aucmd_normal_test
2999 au!
3000 augroup END
3001endfunc
3002
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003003func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003004 " For unknown reason this hangs on MS-Windows
3005 CheckNotMSWindows
3006
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003007 au BufWinLeave * nested q
3008 call assert_fails("norm 7q?\n", 'E855:')
3009
3010 au! BufWinLeave
3011 new
3012 only
3013endfunc
3014
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003015func Test_autocmd_vimgrep()
3016 augroup aucmd_vimgrep
3017 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * sb
3018 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * q9�
3019 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003020 %bwipe!
Bram Moolenaardd07c022021-02-07 13:32:46 +01003021 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003022
3023 augroup aucmd_vimgrep
3024 au!
3025 augroup END
3026endfunc
3027
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003028func Test_autocmd_with_block()
3029 augroup block_testing
3030 au BufReadPost *.xml {
3031 setlocal matchpairs+=<:>
3032 /<start
3033 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003034 au CursorHold * {
3035 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3036 g:gotSafeState = 77
3037 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003038 augroup END
3039
3040 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3041 call assert_equal(expected, execute('au BufReadPost *.xml'))
3042
Bram Moolenaar63b91732021-08-05 20:40:03 +02003043 doautocmd CursorHold
3044 call assert_equal(77, g:gotSafeState)
3045 unlet g:gotSafeState
3046
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003047 augroup block_testing
3048 au!
3049 augroup END
3050endfunc
3051
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003052" Test TextChangedI and TextChanged
3053func Test_Changed_ChangedI()
3054 new
3055 call test_override("char_avail", 1)
3056 let [g:autocmd_i, g:autocmd_n] = ['','']
3057
3058 func! TextChangedAutocmdI(char)
3059 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3060 endfunc
3061
3062 augroup Test_TextChanged
3063 au!
3064 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3065 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3066 augroup END
3067
3068 call feedkeys("ifoo\<esc>", 'tnix')
3069 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3070 " requires running Vim in a terminal window.
3071 " call assert_equal('N3', g:autocmd_n)
3072 call assert_equal('I3', g:autocmd_i)
3073
3074 call feedkeys("yyp", 'tnix')
3075 " TODO: Test test does not seem to trigger TextChanged autocommand.
3076 " call assert_equal('N4', g:autocmd_n)
3077 call assert_equal('I3', g:autocmd_i)
3078
3079 " CleanUp
3080 call test_override("char_avail", 0)
3081 au! TextChanged <buffer>
3082 au! TextChangedI <buffer>
3083 augroup! Test_TextChanged
3084 delfu TextChangedAutocmdI
3085 unlet! g:autocmd_i g:autocmd_n
3086
3087 bw!
3088endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003089
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003090func Test_closing_autocmd_window()
3091 let lines =<< trim END
3092 edit Xa.txt
3093 tabnew Xb.txt
3094 autocmd BufEnter Xa.txt unhide 1
3095 doautoall BufEnter
3096 END
3097 call v9.CheckScriptFailure(lines, 'E814:')
3098 au! BufEnter
3099 only!
3100 bwipe Xa.txt
3101 bwipe Xb.txt
3102endfunc
3103
Bram Moolenaar347538f2022-03-26 16:28:06 +00003104func Test_bufwipeout_changes_window()
3105 " This should not crash, but we don't have any expectations about what
3106 " happens, changing window in BufWipeout has unpredictable results.
3107 tabedit
3108 let g:window_id = win_getid()
3109 topleft new
3110 setlocal bufhidden=wipe
3111 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3112 tabprevious
3113 +tabclose
3114
3115 unlet g:window_id
3116 au! BufWipeout
3117 %bwipe!
3118endfunc
3119
zeertzjq021996f2022-04-10 11:44:04 +01003120func Test_v_event_readonly()
3121 autocmd CompleteChanged * let v:event.width = 0
3122 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3123 au! CompleteChanged
3124
3125 autocmd DirChangedPre * let v:event.directory = ''
3126 call assert_fails('cd .', 'E46:')
3127 au! DirChangedPre
3128
3129 autocmd ModeChanged * let v:event.new_mode = ''
3130 call assert_fails('normal! cc', 'E46:')
3131 au! ModeChanged
3132
3133 autocmd TextYankPost * let v:event.operator = ''
3134 call assert_fails('normal! yy', 'E46:')
3135 au! TextYankPost
3136endfunc
3137
Bram Moolenaar347538f2022-03-26 16:28:06 +00003138
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003139" vim: shiftwidth=2 sts=2 expandtab