blob: 7faa4551d17afe35cbf889618879b2b35d4f0326 [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
Bram Moolenaar8c64a362018-03-23 22:39:31 +01006
Bram Moolenaar1e115362019-01-09 23:01:02 +01007func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +02008 for bnr in range(1, bufnr('$'))
9 if bufloaded(bnr) && bufnr('%') != bnr
10 execute 'bd! ' . bnr
11 endif
12 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020013endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020014
Bram Moolenaar14735512016-03-26 21:00:08 +010015func Test_vim_did_enter()
16 call assert_false(v:vim_did_enter)
17
18 " This script will never reach the main loop, can't check if v:vim_did_enter
19 " becomes one.
20endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020021
Bram Moolenaar75911162020-07-21 19:44:47 +020022" Test for the CursorHold autocmd
23func Test_CursorHold_autocmd()
24 CheckRunVimInTerminal
25 call writefile(['one', 'two', 'three'], 'Xfile')
26 let before =<< trim END
27 set updatetime=10
28 au CursorHold * call writefile([line('.')], 'Xoutput', 'a')
29 END
30 call writefile(before, 'Xinit')
31 let buf = RunVimInTerminal('-S Xinit Xfile', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020032 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020033 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020034 call term_sendkeys(buf, "gg")
35 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020036 call WaitForAssert({-> assert_equal(['1'], readfile('Xoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020037 call term_sendkeys(buf, "j")
38 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020039 call WaitForAssert({-> assert_equal(['1', '2'], readfile('Xoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020040 call term_sendkeys(buf, "j")
41 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020042 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('Xoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020043 call StopVimInTerminal(buf)
44
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call delete('Xinit')
46 call delete('Xoutput')
47 call delete('Xfile')
48endfunc
49
Bram Moolenaarc67e8922016-05-24 16:07:40 +020050if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020051
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052 func ExitInsertMode(id)
53 call feedkeys("\<Esc>")
54 endfunc
55
56 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020057 " Need to move the cursor.
58 call feedkeys("ggG", "xt")
59
Bram Moolenaarc67e8922016-05-24 16:07:40 +020060 let g:triggered = 0
61 au CursorHoldI * let g:triggered += 1
62 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010063 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020064 call feedkeys('a', 'x!')
65 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010066 unlet g:triggered
67 au! CursorHoldI
68 set updatetime&
69 endfunc
70
71 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020072 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010073 " Need to move the cursor.
74 call feedkeys("ggG", "xt")
75
76 " Confirm the timer invoked in exit_cb of the job doesn't disturb
77 " CursorHoldI event.
78 let g:triggered = 0
79 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020080 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010081 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020082 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010083 call feedkeys('a', 'x!')
84 call assert_equal(1, g:triggered)
85 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020086 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020087 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020088 endfunc
89
90 func Test_cursorhold_insert_ctrl_x()
91 let g:triggered = 0
92 au CursorHoldI * let g:triggered += 1
93 set updatetime=20
94 call timer_start(100, 'ExitInsertMode')
95 " CursorHoldI does not trigger after CTRL-X
96 call feedkeys("a\<C-X>", 'x!')
97 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010098 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020099 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200100 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200101 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200102
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100103 func Test_cursorhold_insert_ctrl_g_U()
104 au CursorHoldI * :
105 set updatetime=20
106 new
107 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
108 call feedkeys("i()\<C-g>U", 'tx!')
109 sleep 200m
110 call assert_equal('(foo)', getline(1))
111 undo
112 call assert_equal('', getline(1))
113
114 bwipe!
115 au! CursorHoldI
116 set updatetime&
117 endfunc
118
Bram Moolenaar97b00752019-05-12 13:07:14 +0200119 func Test_OptionSet_modeline()
120 call test_override('starting', 1)
121 au! OptionSet
122 augroup set_tabstop
123 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
124 augroup END
125 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline')
126 set modeline
127 let v:errmsg = ''
128 call assert_fails('split XoptionsetModeline', 'E12:')
129 call assert_equal(7, &ts)
130 call assert_equal('', v:errmsg)
131
132 augroup set_tabstop
133 au!
134 augroup END
135 bwipe!
136 set ts&
137 call delete('XoptionsetModeline')
138 call test_override('starting', 0)
139 endfunc
140
141endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200142
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200143func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200144 augroup test_bufunload_group
145 autocmd!
146 autocmd BufUnload * call add(s:li, "bufunload")
147 autocmd BufDelete * call add(s:li, "bufdelete")
148 autocmd BufWipeout * call add(s:li, "bufwipeout")
149 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200150
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100151 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200152 new
153 setlocal bufhidden=
154 bunload
155 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200156
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100157 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200158 new
159 setlocal bufhidden=delete
160 bunload
161 call assert_equal(["bufunload", "bufdelete"], s:li)
162
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100163 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200164 new
165 setlocal bufhidden=unload
166 bwipeout
167 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
168
Bram Moolenaare99e8442016-07-26 20:43:40 +0200169 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200170 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200171endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200172
173" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200174func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200175 tabedit
176 tabfirst
177
178 augroup test_autocmd_bufunload_with_tabnext_group
179 autocmd!
180 autocmd BufUnload <buffer> tabnext
181 augroup END
182
183 quit
184 call assert_equal(2, tabpagenr('$'))
185
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200186 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200187 augroup! test_autocmd_bufunload_with_tabnext_group
188 tablast
189 quit
190endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200191
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100192func Test_argdelete_in_next()
193 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
194 call assert_fails('next a b', 'E1156:')
195 au! BufNew,BufEnter,BufLeave,BufWinEnter *
196endfunc
197
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200198func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200199 tabedit
200 augroup sample
201 autocmd!
202 autocmd BufWinLeave <buffer> tabfirst
203 augroup END
204 call setline(1, ['a', 'b', 'c'])
205 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200206 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200207endfunc
208
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200209" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200210func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200211 split aa.txt
212 let lastbuf = bufnr('$')
213
214 augroup test_autocmd_bufunload
215 autocmd!
216 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
217 augroup END
218
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100219 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200220
221 autocmd! test_autocmd_bufunload
222 augroup! test_autocmd_bufunload
223 bwipe! aa.txt
224 bwipe! bb.txt
225endfunc
226
227" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200228func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200229 setlocal buftype=nowrite
230 let lastbuf = bufnr('$')
231
232 augroup test_autocmd_bufunload
233 autocmd!
234 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
235 augroup END
236
237 normal! i1
238 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200239
240 autocmd! test_autocmd_bufunload
241 augroup! test_autocmd_bufunload
242 bwipe! a.txt
243endfunc
244
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100245func Test_autocmd_dummy_wipeout()
246 " prepare files
247 call writefile([''], 'Xdummywipetest1.txt')
248 call writefile([''], 'Xdummywipetest2.txt')
249 augroup test_bufunload_group
250 autocmd!
251 autocmd BufUnload * call add(s:li, "bufunload")
252 autocmd BufDelete * call add(s:li, "bufdelete")
253 autocmd BufWipeout * call add(s:li, "bufwipeout")
254 augroup END
255
256 let s:li = []
257 split Xdummywipetest1.txt
258 silent! vimgrep /notmatched/ Xdummywipetest*
259 call assert_equal(["bufunload", "bufwipeout"], s:li)
260
261 bwipeout
262 call delete('Xdummywipetest1.txt')
263 call delete('Xdummywipetest2.txt')
264 au! test_bufunload_group
265 augroup! test_bufunload_group
266endfunc
267
Bram Moolenaarc917da42016-07-19 22:31:36 +0200268func Test_win_tab_autocmd()
269 let g:record = []
270
271 augroup testing
272 au WinNew * call add(g:record, 'WinNew')
273 au WinEnter * call add(g:record, 'WinEnter')
274 au WinLeave * call add(g:record, 'WinLeave')
275 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200276 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200277 au TabEnter * call add(g:record, 'TabEnter')
278 au TabLeave * call add(g:record, 'TabLeave')
279 augroup END
280
281 split
282 tabnew
283 close
284 close
285
286 call assert_equal([
287 \ 'WinLeave', 'WinNew', 'WinEnter',
288 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200289 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200290 \ 'WinLeave', 'WinEnter'
291 \ ], g:record)
292
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200293 let g:record = []
294 tabnew somefile
295 tabnext
296 bwipe somefile
297
298 call assert_equal([
299 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
300 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
301 \ 'TabClosed'
302 \ ], g:record)
303
Bram Moolenaarc917da42016-07-19 22:31:36 +0200304 augroup testing
305 au!
306 augroup END
307 unlet g:record
308endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200309
310func s:AddAnAutocmd()
311 augroup vimBarTest
312 au BufReadCmd * echo 'hello'
313 augroup END
314 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
315endfunc
316
317func Test_early_bar()
318 " test that a bar is recognized before the {event}
319 call s:AddAnAutocmd()
320 augroup vimBarTest | au! | augroup END
321 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
322
323 call s:AddAnAutocmd()
324 augroup vimBarTest| au!| augroup END
325 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
326
327 " test that a bar is recognized after the {event}
328 call s:AddAnAutocmd()
329 augroup vimBarTest| au!BufReadCmd| augroup END
330 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
331
332 " test that a bar is recognized after the {group}
333 call s:AddAnAutocmd()
334 au! vimBarTest|echo 'hello'
335 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
336endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200337
Bram Moolenaar5c809082016-09-01 16:21:48 +0200338func RemoveGroup()
339 autocmd! StartOK
340 augroup! StartOK
341endfunc
342
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200343func Test_augroup_warning()
344 augroup TheWarning
345 au VimEnter * echo 'entering'
346 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100347 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200348 redir => res
349 augroup! TheWarning
350 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100351 call assert_match("W19:", res)
352 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200353
354 " check "Another" does not take the pace of the deleted entry
355 augroup Another
356 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100357 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200358 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200359
360 " no warning for postpone aucmd delete
361 augroup StartOK
362 au VimEnter * call RemoveGroup()
363 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100364 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200365 redir => res
366 doautocmd VimEnter
367 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100368 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200369 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200370
371 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200372endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200373
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200374func Test_BufReadCmdHelp()
375 " This used to cause access to free memory
376 au BufReadCmd * e +h
377 help
378
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200379 au! BufReadCmd
380endfunc
381
382func Test_BufReadCmdHelpJump()
383 " This used to cause access to free memory
384 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200385 " } to fix highlighting
386 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200387
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200388 au! BufReadCmd
389endfunc
390
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200391func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200392 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200393 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200394 call assert_fails('augroup! x', 'E936:')
395 au VimEnter * echo
396 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200397 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100398 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200399 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200400endfunc
401
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200402" Tests for autocommands on :close command.
403" This used to be in test13.
404func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200405 " Clean up buffers, because in some cases this function fails.
406 call s:cleanup_buffers()
407
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200408 " Write three files and open them, each in a window.
409 " Then go to next window, with autocommand that deletes the previous one.
410 " Do this twice, writing the file.
411 e! Xtestje1
412 call setline(1, 'testje1')
413 w
414 sp Xtestje2
415 call setline(1, 'testje2')
416 w
417 sp Xtestje3
418 call setline(1, 'testje3')
419 w
420 wincmd w
421 au WinLeave Xtestje2 bwipe
422 wincmd w
423 call assert_equal('Xtestje1', expand('%'))
424
425 au WinLeave Xtestje1 bwipe Xtestje3
426 close
427 call assert_equal('Xtestje1', expand('%'))
428
429 " Test deleting the buffer on a Unload event. If this goes wrong there
430 " will be the ATTENTION prompt.
431 e Xtestje1
432 au!
433 au! BufUnload Xtestje1 bwipe
434 call assert_fails('e Xtestje3', 'E937:')
435 call assert_equal('Xtestje3', expand('%'))
436
437 e Xtestje2
438 sp Xtestje1
439 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200440 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200441
442 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
443 " there are ml_line errors and/or a Crash.
444 au!
445 only
446 e Xanother
447 e Xtestje1
448 bwipe Xtestje2
449 bwipe Xtestje3
450 au BufWipeout Xtestje1 buf Xtestje1
451 bwipe
452 call assert_equal('Xanother', expand('%'))
453
454 only
455 help
456 wincmd w
457 1quit
458 call assert_equal('Xanother', expand('%'))
459
460 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100461 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200462 call delete('Xtestje1')
463 call delete('Xtestje2')
464 call delete('Xtestje3')
465endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100466
467func Test_BufEnter()
468 au! BufEnter
469 au Bufenter * let val = val . '+'
470 let g:val = ''
471 split NewFile
472 call assert_equal('+', g:val)
473 bwipe!
474 call assert_equal('++', g:val)
475
476 " Also get BufEnter when editing a directory
477 call mkdir('Xdir')
478 split Xdir
479 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100480
481 " On MS-Windows we can't edit the directory, make sure we wipe the right
482 " buffer.
483 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100484
485 call delete('Xdir', 'd')
486 au! BufEnter
487endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100488
489" Closing a window might cause an endless loop
490" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200491func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200492 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100493 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200494 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100495 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100496 mksession!
497
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200498 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200499 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200500 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100501 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200502 augroup test_autocmd_sessionload
503 autocmd!
504 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
505 augroup END
506
507 func WriteErrors()
508 call writefile([execute("messages")], "Xerrors")
509 endfunc
510 au VimLeave * call WriteErrors()
511 [CODE]
512
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100513 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200514 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100515 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200516 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100517
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100518 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100519 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100520 call delete(file)
521 endfor
522endfunc
523
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100524" Using :blast and :ball for many events caused a crash, because b_nwindows was
525" not incremented correctly.
526func Test_autocmd_blast_badd()
527 let content =<< trim [CODE]
528 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
529 edit foo1
530 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
531 edit foo2
532 call writefile(['OK'], 'Xerrors')
533 qall
534 [CODE]
535
536 call writefile(content, 'XblastBall')
537 call system(GetVimCommand() .. ' --clean -S XblastBall')
538 call assert_match('OK', readfile('Xerrors')->join())
539
540 call delete('XblastBall')
541 call delete('Xerrors')
542endfunc
543
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100544" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200545func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100546 tabnew
547 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100548 mksession!
549
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200550 let content =<< trim [CODE]
551 set nocp noswapfile
552 function! DeleteInactiveBufs()
553 tabfirst
554 let tabblist = []
555 for i in range(1, tabpagenr(''$''))
556 call extend(tabblist, tabpagebuflist(i))
557 endfor
558 for b in range(1, bufnr(''$''))
559 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
560 exec ''bwipeout '' . b
561 endif
562 endfor
563 echomsg "SessionLoadPost DONE"
564 endfunction
565 au SessionLoadPost * call DeleteInactiveBufs()
566
567 func WriteErrors()
568 call writefile([execute("messages")], "Xerrors")
569 endfunc
570 au VimLeave * call WriteErrors()
571 [CODE]
572
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100573 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200574 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100575 let errors = join(readfile('Xerrors'))
576 " This probably only ever matches on unix.
577 call assert_notmatch('Caught deadly signal SEGV', errors)
578 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100579
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100580 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100581 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100582 call delete(file)
583 endfor
584endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200585
586func Test_empty_doau()
587 doau \|
588endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200589
590func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200591 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200592 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200593 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
594 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 +0200595 let g:opt = [expected, actual]
596 "call assert_equal(expected, actual)
597endfunc
598
599func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200600 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200601
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200602 badd test_autocmd.vim
603
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200604 call test_override('starting', 1)
605 set nocp
606 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
607
608 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100609 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200610 set nu
611 call assert_equal([], g:options)
612 call assert_equal(g:opt[0], g:opt[1])
613
614 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100615 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200616 setlocal nonu
617 call assert_equal([], g:options)
618 call assert_equal(g:opt[0], g:opt[1])
619
620 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100621 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200622 setglobal nonu
623 call assert_equal([], g:options)
624 call assert_equal(g:opt[0], g:opt[1])
625
626 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100627 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200628 setlocal ai
629 call assert_equal([], g:options)
630 call assert_equal(g:opt[0], g:opt[1])
631
632 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100633 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200634 setglobal ai
635 call assert_equal([], g:options)
636 call assert_equal(g:opt[0], g:opt[1])
637
638 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100639 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200640 set ai!
641 call assert_equal([], g:options)
642 call assert_equal(g:opt[0], g:opt[1])
643
644 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100645 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200646 noa setlocal ai
647 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200648 set ai!
649 call assert_equal([], g:options)
650 call assert_equal(g:opt[0], g:opt[1])
651
652 " Should not print anything, use :noa
653 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100654 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200655 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200656 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200657 call assert_equal(g:opt[0], g:opt[1])
658
659 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100660 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200661 set list nu
662 call assert_equal([], g:options)
663 call assert_equal(g:opt[0], g:opt[1])
664
665 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100666 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200667 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200668 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 +0200669 call assert_equal(g:opt[0], g:opt[1])
670
671 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100672 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200673 setlocal acd
674 call assert_equal([], g:options)
675 call assert_equal(g:opt[0], g:opt[1])
676
677 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100678 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200679 set ar
680 call assert_equal([], g:options)
681 call assert_equal(g:opt[0], g:opt[1])
682
683 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100684 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200685 setlocal ar
686 call assert_equal([], g:options)
687 call assert_equal(g:opt[0], g:opt[1])
688
689 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100690 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200691 setglobal invar
692 call assert_equal([], g:options)
693 call assert_equal(g:opt[0], g:opt[1])
694
695 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100696 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
697 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200698 call assert_equal([], g:options)
699 call assert_equal(g:opt[0], g:opt[1])
700
701 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100702 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200703 " try twice, first time, shouldn't trigger because option name is invalid,
704 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200705 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200706 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200707 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200708 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200709 call assert_equal([], g:options)
710 call assert_equal(g:opt[0], g:opt[1])
711
712 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100713 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200714 call setwinvar(0, '&number', 1)
715 call assert_equal([], g:options)
716 call assert_equal(g:opt[0], g:opt[1])
717
718 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100719 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200720 setlocal key=blah
721 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200722 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200723 call assert_equal(g:opt[0], g:opt[1])
724
Bram Moolenaard7c96872019-06-15 17:12:48 +0200725
726 " 18a: Setting string global option"
727 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100728 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200729 set backupext=foo
730 call assert_equal([], g:options)
731 call assert_equal(g:opt[0], g:opt[1])
732
733 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100734 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200735 set backupext&
736 call assert_equal([], g:options)
737 call assert_equal(g:opt[0], g:opt[1])
738
739 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100740 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200741 setglobal backupext=bar
742 call assert_equal([], g:options)
743 call assert_equal(g:opt[0], g:opt[1])
744
745 " 18d: Setting local string global option"
746 " As this is a global option this sets the global value even though
747 " :setlocal is used!
748 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100749 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200750 setlocal backupext=baz
751 call assert_equal([], g:options)
752 call assert_equal(g:opt[0], g:opt[1])
753
754 " 18e: Setting again string global option"
755 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
756 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100757 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200758 set backupext=fuu
759 call assert_equal([], g:options)
760 call assert_equal(g:opt[0], g:opt[1])
761
762
763 " 19a: Setting string local-global (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200764 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100765 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200766 set tags=tagpath
767 call assert_equal([], g:options)
768 call assert_equal(g:opt[0], g:opt[1])
769
Bram Moolenaard7c96872019-06-15 17:12:48 +0200770 " 19b: Resetting string local-global (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100771 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200772 set tags&
773 call assert_equal([], g:options)
774 call assert_equal(g:opt[0], g:opt[1])
775
Bram Moolenaard7c96872019-06-15 17:12:48 +0200776 " 19c: Setting global string local-global (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100777 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200778 setglobal tags=tagpath1
779 call assert_equal([], g:options)
780 call assert_equal(g:opt[0], g:opt[1])
781
782 " 19d: Setting local string local-global (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100783 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200784 setlocal tags=tagpath2
785 call assert_equal([], g:options)
786 call assert_equal(g:opt[0], g:opt[1])
787
788 " 19e: Setting again string local-global (to buffer) option"
789 " Note: v:option_old is the old global value for local-global string options
790 " but the old local value for all other kinds of options.
791 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
792 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100793 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200794 set tags=tagpath
795 call assert_equal([], g:options)
796 call assert_equal(g:opt[0], g:opt[1])
797
798 " 19f: Setting string local-global (to buffer) option to an empty string"
799 " Note: v:option_old is the old global value for local-global string options
800 " but the old local value for all other kinds of options.
801 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
802 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100803 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200804 set tags=tagpath
805 call assert_equal([], g:options)
806 call assert_equal(g:opt[0], g:opt[1])
807
808
809 " 20a: Setting string local (to buffer) option"
810 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100811 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200812 set spelllang=elvish,klingon
813 call assert_equal([], g:options)
814 call assert_equal(g:opt[0], g:opt[1])
815
816 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100817 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200818 set spelllang&
819 call assert_equal([], g:options)
820 call assert_equal(g:opt[0], g:opt[1])
821
822 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100823 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200824 setglobal spelllang=elvish
825 call assert_equal([], g:options)
826 call assert_equal(g:opt[0], g:opt[1])
827
828 " 20d: Setting local string local (to buffer) option"
829 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100830 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200831 setlocal spelllang=klingon
832 call assert_equal([], g:options)
833 call assert_equal(g:opt[0], g:opt[1])
834
835 " 20e: Setting again string local (to buffer) option"
836 " Note: v:option_old is the old global value for local-global string options
837 " but the old local value for all other kinds of options.
838 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
839 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100840 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200841 set spelllang=foo
842 call assert_equal([], g:options)
843 call assert_equal(g:opt[0], g:opt[1])
844
845
846 " 21a: Setting string local-global (to window) option"
847 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100848 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200849 set statusline=foo
850 call assert_equal([], g:options)
851 call assert_equal(g:opt[0], g:opt[1])
852
853 " 21b: Resetting string local-global (to window) option"
854 " Note: v:option_old is the old global value for local-global string options
855 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100856 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200857 set statusline&
858 call assert_equal([], g:options)
859 call assert_equal(g:opt[0], g:opt[1])
860
861 " 21c: Setting global string local-global (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100862 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200863 setglobal statusline=bar
864 call assert_equal([], g:options)
865 call assert_equal(g:opt[0], g:opt[1])
866
867 " 21d: Setting local string local-global (to window) option"
868 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100869 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200870 setlocal statusline=baz
871 call assert_equal([], g:options)
872 call assert_equal(g:opt[0], g:opt[1])
873
874 " 21e: Setting again string local-global (to window) option"
875 " Note: v:option_old is the old global value for local-global string options
876 " but the old local value for all other kinds of options.
877 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
878 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100879 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200880 set statusline=foo
881 call assert_equal([], g:options)
882 call assert_equal(g:opt[0], g:opt[1])
883
884
885 " 22a: Setting string local (to window) option"
886 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100887 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200888 set foldignore=fo
889 call assert_equal([], g:options)
890 call assert_equal(g:opt[0], g:opt[1])
891
892 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100893 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200894 set foldignore&
895 call assert_equal([], g:options)
896 call assert_equal(g:opt[0], g:opt[1])
897
898 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100899 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200900 setglobal foldignore=bar
901 call assert_equal([], g:options)
902 call assert_equal(g:opt[0], g:opt[1])
903
904 " 22d: Setting local string local (to window) option"
905 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100906 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200907 setlocal foldignore=baz
908 call assert_equal([], g:options)
909 call assert_equal(g:opt[0], g:opt[1])
910
911 " 22e: Setting again string local (to window) option"
912 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
913 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100914 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200915 set foldignore=fo
916 call assert_equal([], g:options)
917 call assert_equal(g:opt[0], g:opt[1])
918
919
920 " 23a: Setting global number local option"
921 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
922 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100923 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200924 setglobal cmdheight=2
925 call assert_equal([], g:options)
926 call assert_equal(g:opt[0], g:opt[1])
927
928 " 23b: Setting local number global option"
929 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
930 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100931 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200932 setlocal cmdheight=2
933 call assert_equal([], g:options)
934 call assert_equal(g:opt[0], g:opt[1])
935
936 " 23c: Setting again number global option"
937 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
938 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100939 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200940 set cmdheight=2
941 call assert_equal([], g:options)
942 call assert_equal(g:opt[0], g:opt[1])
943
944 " 23d: Setting again number global option"
945 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100946 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200947 set cmdheight=2
948 call assert_equal([], g:options)
949 call assert_equal(g:opt[0], g:opt[1])
950
951
952 " 24a: Setting global number global-local (to buffer) option"
953 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
954 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100955 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200956 setglobal undolevels=2
957 call assert_equal([], g:options)
958 call assert_equal(g:opt[0], g:opt[1])
959
960 " 24b: Setting local number global-local (to buffer) option"
961 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
962 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100963 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200964 setlocal undolevels=2
965 call assert_equal([], g:options)
966 call assert_equal(g:opt[0], g:opt[1])
967
968 " 24c: Setting again number global-local (to buffer) option"
969 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
970 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100971 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200972 set undolevels=2
973 call assert_equal([], g:options)
974 call assert_equal(g:opt[0], g:opt[1])
975
976 " 24d: Setting again global number global-local (to buffer) option"
977 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100978 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200979 set undolevels=2
980 call assert_equal([], g:options)
981 call assert_equal(g:opt[0], g:opt[1])
982
983
984 " 25a: Setting global number local (to buffer) option"
985 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
986 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100987 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200988 setglobal wrapmargin=2
989 call assert_equal([], g:options)
990 call assert_equal(g:opt[0], g:opt[1])
991
992 " 25b: Setting local number local (to buffer) option"
993 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
994 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100995 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200996 setlocal wrapmargin=2
997 call assert_equal([], g:options)
998 call assert_equal(g:opt[0], g:opt[1])
999
1000 " 25c: Setting again number local (to buffer) option"
1001 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1002 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001003 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001004 set wrapmargin=2
1005 call assert_equal([], g:options)
1006 call assert_equal(g:opt[0], g:opt[1])
1007
1008 " 25d: Setting again global number local (to buffer) option"
1009 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001010 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001011 set wrapmargin=2
1012 call assert_equal([], g:options)
1013 call assert_equal(g:opt[0], g:opt[1])
1014
1015
1016 " 26: Setting number global-local (to window) option.
1017 " Such option does currently not exist.
1018
1019
1020 " 27a: Setting global number local (to window) option"
1021 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1022 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001023 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001024 setglobal foldcolumn=2
1025 call assert_equal([], g:options)
1026 call assert_equal(g:opt[0], g:opt[1])
1027
1028 " 27b: Setting local number local (to window) option"
1029 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1030 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001031 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001032 setlocal foldcolumn=2
1033 call assert_equal([], g:options)
1034 call assert_equal(g:opt[0], g:opt[1])
1035
1036 " 27c: Setting again number local (to window) option"
1037 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1038 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001039 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001040 set foldcolumn=2
1041 call assert_equal([], g:options)
1042 call assert_equal(g:opt[0], g:opt[1])
1043
1044 " 27d: Ssettin again global number local (to window) option"
1045 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001046 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001047 set foldcolumn=2
1048 call assert_equal([], g:options)
1049 call assert_equal(g:opt[0], g:opt[1])
1050
1051
1052 " 28a: Setting global boolean global option"
1053 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1054 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001055 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001056 setglobal nowrapscan
1057 call assert_equal([], g:options)
1058 call assert_equal(g:opt[0], g:opt[1])
1059
1060 " 28b: Setting local boolean global option"
1061 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1062 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001063 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001064 setlocal nowrapscan
1065 call assert_equal([], g:options)
1066 call assert_equal(g:opt[0], g:opt[1])
1067
1068 " 28c: Setting again boolean global option"
1069 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1070 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001071 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001072 set nowrapscan
1073 call assert_equal([], g:options)
1074 call assert_equal(g:opt[0], g:opt[1])
1075
1076 " 28d: Setting again global boolean global option"
1077 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001078 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001079 set wrapscan
1080 call assert_equal([], g:options)
1081 call assert_equal(g:opt[0], g:opt[1])
1082
1083
1084 " 29a: Setting global boolean global-local (to buffer) option"
1085 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1086 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001087 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001088 setglobal autoread
1089 call assert_equal([], g:options)
1090 call assert_equal(g:opt[0], g:opt[1])
1091
1092 " 29b: Setting local boolean global-local (to buffer) option"
1093 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1094 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001095 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001096 setlocal noautoread
1097 call assert_equal([], g:options)
1098 call assert_equal(g:opt[0], g:opt[1])
1099
1100 " 29c: Setting again boolean global-local (to buffer) option"
1101 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1102 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001103 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001104 set autoread
1105 call assert_equal([], g:options)
1106 call assert_equal(g:opt[0], g:opt[1])
1107
1108 " 29d: Setting again global boolean global-local (to buffer) option"
1109 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001110 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001111 set autoread
1112 call assert_equal([], g:options)
1113 call assert_equal(g:opt[0], g:opt[1])
1114
1115
1116 " 30a: Setting global boolean local (to buffer) option"
1117 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1118 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001119 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001120 setglobal cindent
1121 call assert_equal([], g:options)
1122 call assert_equal(g:opt[0], g:opt[1])
1123
1124 " 30b: Setting local boolean local (to buffer) option"
1125 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1126 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001127 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001128 setlocal nocindent
1129 call assert_equal([], g:options)
1130 call assert_equal(g:opt[0], g:opt[1])
1131
1132 " 30c: Setting again boolean local (to buffer) option"
1133 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1134 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001135 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001136 set cindent
1137 call assert_equal([], g:options)
1138 call assert_equal(g:opt[0], g:opt[1])
1139
1140 " 30d: Setting again global boolean local (to buffer) option"
1141 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001142 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001143 set cindent
1144 call assert_equal([], g:options)
1145 call assert_equal(g:opt[0], g:opt[1])
1146
1147
1148 " 31: Setting boolean global-local (to window) option
1149 " Currently no such option exists.
1150
1151
1152 " 32a: Setting global boolean local (to window) option"
1153 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1154 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001155 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001156 setglobal cursorcolumn
1157 call assert_equal([], g:options)
1158 call assert_equal(g:opt[0], g:opt[1])
1159
1160 " 32b: Setting local boolean local (to window) option"
1161 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1162 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001163 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001164 setlocal nocursorcolumn
1165 call assert_equal([], g:options)
1166 call assert_equal(g:opt[0], g:opt[1])
1167
1168 " 32c: Setting again boolean local (to window) option"
1169 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1170 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001171 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001172 set cursorcolumn
1173 call assert_equal([], g:options)
1174 call assert_equal(g:opt[0], g:opt[1])
1175
1176 " 32d: Setting again global boolean local (to window) option"
1177 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001178 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001179 set cursorcolumn
1180 call assert_equal([], g:options)
1181 call assert_equal(g:opt[0], g:opt[1])
1182
1183
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001184 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001185 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001186 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001187 set backspace=2
1188 call assert_equal([], g:options)
1189 call assert_equal(g:opt[0], g:opt[1])
1190
1191
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001192 " Cleanup
1193 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001194 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001195 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 +02001196 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001197 endfor
1198 call test_override('starting', 0)
1199 delfunc! AutoCommandOptionSet
1200endfunc
1201
1202func Test_OptionSet_diffmode()
1203 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001204 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001205 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001206 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001207
1208 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1209 call assert_equal(0, &l:cul)
1210 diffthis
1211 call assert_equal(1, &l:cul)
1212
1213 vnew
1214 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1215 call assert_equal(0, &l:cul)
1216 diffthis
1217 call assert_equal(1, &l:cul)
1218
1219 diffoff
1220 call assert_equal(0, &l:cul)
1221 call assert_equal(1, getwinvar(2, '&l:cul'))
1222 bw!
1223
1224 call assert_equal(1, &l:cul)
1225 diffoff!
1226 call assert_equal(0, &l:cul)
1227 call assert_equal(0, getwinvar(1, '&l:cul'))
1228 bw!
1229
1230 " Cleanup
1231 au! OptionSet
1232 call test_override('starting', 0)
1233endfunc
1234
1235func Test_OptionSet_diffmode_close()
1236 call test_override('starting', 1)
1237 " 19: Try to close the current window when entering diff mode
1238 " should not segfault
1239 new
1240 au OptionSet diff close
1241
1242 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001243 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001244 call assert_equal(1, &diff)
1245 vnew
1246 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001247 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001248 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001249 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001250 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001251 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001252 bw!
1253
1254 " Cleanup
1255 au! OptionSet
1256 call test_override('starting', 0)
1257 "delfunc! AutoCommandOptionSet
1258endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001259
1260" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1261func Test_BufleaveWithDelete()
1262 new | edit Xfile1
1263
1264 augroup test_bufleavewithdelete
1265 autocmd!
1266 autocmd BufLeave Xfile1 bwipe Xfile2
1267 augroup END
1268
1269 call assert_fails('edit Xfile2', 'E143:')
1270 call assert_equal('Xfile1', bufname('%'))
1271
1272 autocmd! test_bufleavewithdelete BufLeave Xfile1
1273 augroup! test_bufleavewithdelete
1274
1275 new
1276 bwipe! Xfile1
1277endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001278
1279" Test for autocommand that changes the buffer list, when doing ":ball".
1280func Test_Acmd_BufAll()
1281 enew!
1282 %bwipe!
1283 call writefile(['Test file Xxx1'], 'Xxx1')
1284 call writefile(['Test file Xxx2'], 'Xxx2')
1285 call writefile(['Test file Xxx3'], 'Xxx3')
1286
1287 " Add three files to the buffer list
1288 split Xxx1
1289 close
1290 split Xxx2
1291 close
1292 split Xxx3
1293 close
1294
1295 " Wipe the buffer when the buffer is opened
1296 au BufReadPost Xxx2 bwipe
1297
1298 call append(0, 'Test file Xxx4')
1299 ball
1300
1301 call assert_equal(2, winnr('$'))
1302 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1303 wincmd t
1304
1305 au! BufReadPost
1306 %bwipe!
1307 call delete('Xxx1')
1308 call delete('Xxx2')
1309 call delete('Xxx3')
1310 enew! | only
1311endfunc
1312
1313" Test for autocommand that changes current buffer on BufEnter event.
1314" Check if modelines are interpreted for the correct buffer.
1315func Test_Acmd_BufEnter()
1316 %bwipe!
1317 call writefile(['start of test file Xxx1',
1318 \ "\<Tab>this is a test",
1319 \ 'end of test file Xxx1'], 'Xxx1')
1320 call writefile(['start of test file Xxx2',
1321 \ 'vim: set noai :',
1322 \ "\<Tab>this is a test",
1323 \ 'end of test file Xxx2'], 'Xxx2')
1324
1325 au BufEnter Xxx2 brew
1326 set ai modeline modelines=3
1327 edit Xxx1
1328 " edit Xxx2, autocmd will do :brew
1329 edit Xxx2
1330 exe "normal G?this is a\<CR>"
1331 " Append text with autoindent to this file
1332 normal othis should be auto-indented
1333 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1334 call assert_equal(3, line('.'))
1335 " Remove autocmd and edit Xxx2 again
1336 au! BufEnter Xxx2
1337 buf! Xxx2
1338 exe "normal G?this is a\<CR>"
1339 " append text without autoindent to Xxx
1340 normal othis should be in column 1
1341 call assert_equal("this should be in column 1", getline('.'))
1342 call assert_equal(4, line('.'))
1343
1344 %bwipe!
1345 call delete('Xxx1')
1346 call delete('Xxx2')
1347 set ai&vim modeline&vim modelines&vim
1348endfunc
1349
1350" Test for issue #57
1351" do not move cursor on <c-o> when autoindent is set
1352func Test_ai_CTRL_O()
1353 enew!
1354 set ai
1355 let save_fo = &fo
1356 set fo+=r
1357 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1358 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1359 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1360
1361 set ai&vim
1362 let &fo = save_fo
1363 enew!
1364endfunc
1365
1366" Test for autocommand that deletes the current buffer on BufLeave event.
1367" Also test deleting the last buffer, should give a new, empty buffer.
1368func Test_BufLeave_Wipe()
1369 %bwipe!
1370 let content = ['start of test file Xxx',
1371 \ 'this is a test',
1372 \ 'end of test file Xxx']
1373 call writefile(content, 'Xxx1')
1374 call writefile(content, 'Xxx2')
1375
1376 au BufLeave Xxx2 bwipe
1377 edit Xxx1
1378 split Xxx2
1379 " delete buffer Xxx2, we should be back to Xxx1
1380 bwipe
1381 call assert_equal('Xxx1', bufname('%'))
1382 call assert_equal(1, winnr('$'))
1383
1384 " Create an alternate buffer
1385 %write! test.out
1386 call assert_equal('test.out', bufname('#'))
1387 " delete alternate buffer
1388 bwipe test.out
1389 call assert_equal('Xxx1', bufname('%'))
1390 call assert_equal('', bufname('#'))
1391
1392 au BufLeave Xxx1 bwipe
1393 " delete current buffer, get an empty one
1394 bwipe!
1395 call assert_equal(1, line('$'))
1396 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001397 let g:bufinfo = getbufinfo()
1398 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001399
1400 call delete('Xxx1')
1401 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001402 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001403 %bwipe
1404 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001405
1406 " check that bufinfo doesn't contain a pointer to freed memory
1407 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001408endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001409
1410func Test_QuitPre()
1411 edit Xfoo
1412 let winid = win_getid(winnr())
1413 split Xbar
1414 au! QuitPre * let g:afile = expand('<afile>')
1415 " Close the other window, <afile> should be correct.
1416 exe win_id2win(winid) . 'q'
1417 call assert_equal('Xfoo', g:afile)
1418
1419 unlet g:afile
1420 bwipe Xfoo
1421 bwipe Xbar
1422endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001423
1424func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001425 au! CmdlineChanged : let g:text = getcmdline()
1426 let g:text = 0
1427 call feedkeys(":echom 'hello'\<CR>", 'xt')
1428 call assert_equal("echom 'hello'", g:text)
1429 au! CmdlineChanged
1430
1431 au! CmdlineChanged : let g:entered = expand('<afile>')
1432 let g:entered = 0
1433 call feedkeys(":echom 'hello'\<CR>", 'xt')
1434 call assert_equal(':', g:entered)
1435 au! CmdlineChanged
1436
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001437 au! CmdlineEnter : let g:entered = expand('<afile>')
1438 au! CmdlineLeave : let g:left = expand('<afile>')
1439 let g:entered = 0
1440 let g:left = 0
1441 call feedkeys(":echo 'hello'\<CR>", 'xt')
1442 call assert_equal(':', g:entered)
1443 call assert_equal(':', g:left)
1444 au! CmdlineEnter
1445 au! CmdlineLeave
1446
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001447 let save_shellslash = &shellslash
1448 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001449 au! CmdlineEnter / let g:entered = expand('<afile>')
1450 au! CmdlineLeave / let g:left = expand('<afile>')
1451 let g:entered = 0
1452 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001453 new
1454 call setline(1, 'hello')
1455 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001456 call assert_equal('/', g:entered)
1457 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001458 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001459 au! CmdlineEnter
1460 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001461 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001462endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001463
1464" Test for BufWritePre autocommand that deletes or unloads the buffer.
1465func Test_BufWritePre()
1466 %bwipe
1467 au BufWritePre Xxx1 bunload
1468 au BufWritePre Xxx2 bwipe
1469
1470 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1471 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1472
1473 edit Xtest
1474 e! Xxx2
1475 bdel Xtest
1476 e Xxx1
1477 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001478 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001479 call assert_equal('Xxx2', bufname('%'))
1480 edit Xtest
1481 e! Xxx2
1482 bwipe Xtest
1483 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001484 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001485 call assert_equal('Xxx1', bufname('%'))
1486 au! BufWritePre
1487 call delete('Xxx1')
1488 call delete('Xxx2')
1489endfunc
1490
1491" Test for BufUnload autocommand that unloads all the other buffers
1492func Test_bufunload_all()
1493 call writefile(['Test file Xxx1'], 'Xxx1')"
1494 call writefile(['Test file Xxx2'], 'Xxx2')"
1495
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001496 let content =<< trim [CODE]
1497 func UnloadAllBufs()
1498 let i = 1
1499 while i <= bufnr('$')
1500 if i != bufnr('%') && bufloaded(i)
1501 exe i . 'bunload'
1502 endif
1503 let i += 1
1504 endwhile
1505 endfunc
1506 au BufUnload * call UnloadAllBufs()
1507 au VimLeave * call writefile(['Test Finished'], 'Xout')
1508 edit Xxx1
1509 split Xxx2
1510 q
1511 [CODE]
1512
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001513 call writefile(content, 'Xtest')
1514
1515 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001516 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001517 call assert_true(filereadable('Xout'))
1518
1519 call delete('Xxx1')
1520 call delete('Xxx2')
1521 call delete('Xtest')
1522 call delete('Xout')
1523endfunc
1524
1525" Some tests for buffer-local autocommands
1526func Test_buflocal_autocmd()
1527 let g:bname = ''
1528 edit xx
1529 au BufLeave <buffer> let g:bname = expand("%")
1530 " here, autocommand for xx should trigger.
1531 " but autocommand shall not apply to buffer named <buffer>.
1532 edit somefile
1533 call assert_equal('xx', g:bname)
1534 let g:bname = ''
1535 " here, autocommand shall be auto-deleted
1536 bwipe xx
1537 " autocmd should not trigger
1538 edit xx
1539 call assert_equal('', g:bname)
1540 " autocmd should not trigger
1541 edit somefile
1542 call assert_equal('', g:bname)
1543 enew
1544 unlet g:bname
1545endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001546
1547" Test for "*Cmd" autocommands
1548func Test_Cmd_Autocmds()
1549 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1550
1551 enew!
1552 au BufReadCmd XtestA 0r Xxx|$del
1553 edit XtestA " will read text of Xxd instead
1554 call assert_equal('start of Xxx', getline(1))
1555
1556 au BufWriteCmd XtestA call append(line("$"), "write")
1557 write " will append a line to the file
1558 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001559 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001560 call assert_equal('write', getline(4))
1561
1562 " now we have:
1563 " 1 start of Xxx
1564 " 2 abc2
1565 " 3 end of Xxx
1566 " 4 write
1567
1568 au FileReadCmd XtestB '[r Xxx
1569 2r XtestB " will read Xxx below line 2 instead
1570 call assert_equal('start of Xxx', getline(3))
1571
1572 " now we have:
1573 " 1 start of Xxx
1574 " 2 abc2
1575 " 3 start of Xxx
1576 " 4 abc2
1577 " 5 end of Xxx
1578 " 6 end of Xxx
1579 " 7 write
1580
1581 au FileWriteCmd XtestC '[,']copy $
1582 normal 4GA1
1583 4,5w XtestC " will copy lines 4 and 5 to the end
1584 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001585 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001586 call assert_equal("end of Xxx", getline(9))
1587
1588 " now we have:
1589 " 1 start of Xxx
1590 " 2 abc2
1591 " 3 start of Xxx
1592 " 4 abc21
1593 " 5 end of Xxx
1594 " 6 end of Xxx
1595 " 7 write
1596 " 8 abc21
1597 " 9 end of Xxx
1598
1599 let g:lines = []
1600 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1601 w >>XtestD " will add lines to 'lines'
1602 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001603 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001604 call assert_equal(9, line('$'))
1605 call assert_equal('end of Xxx', getline('$'))
1606
1607 au BufReadCmd XtestE 0r Xxx|$del
1608 sp XtestE " split window with test.out
1609 call assert_equal('end of Xxx', getline(3))
1610
1611 let g:lines = []
1612 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1613 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1614 wall " will write other window to 'lines'
1615 call assert_equal(4, len(g:lines), g:lines)
1616 call assert_equal('asdf', g:lines[2])
1617
1618 au! BufReadCmd
1619 au! BufWriteCmd
1620 au! FileReadCmd
1621 au! FileWriteCmd
1622 au! FileAppendCmd
1623 %bwipe!
1624 call delete('Xxx')
1625 enew!
1626endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001627
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001628func s:ReadFile()
1629 setl noswapfile nomodified
1630 let filename = resolve(expand("<afile>:p"))
1631 execute 'read' fnameescape(filename)
1632 1d_
1633 exe 'file' fnameescape(filename)
1634 setl buftype=acwrite
1635endfunc
1636
1637func s:WriteFile()
1638 let filename = resolve(expand("<afile>:p"))
1639 setl buftype=
1640 noautocmd execute 'write' fnameescape(filename)
1641 setl buftype=acwrite
1642 setl nomodified
1643endfunc
1644
1645func Test_BufReadCmd()
1646 autocmd BufReadCmd *.test call s:ReadFile()
1647 autocmd BufWriteCmd *.test call s:WriteFile()
1648
1649 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1650 edit Xcmd.test
1651 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1652 normal! Gofour
1653 write
1654 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1655
1656 bwipe!
1657 call delete('Xcmd.test')
1658 au! BufReadCmd
1659 au! BufWriteCmd
1660endfunc
1661
Bram Moolenaaraace2152017-11-05 16:23:10 +01001662func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001663 exe a:start .. 'mark ['
1664 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001665endfunc
1666
1667" Verify the effects of autocmds on '[ and ']
1668func Test_change_mark_in_autocmds()
1669 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001670 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001671
1672 call SetChangeMarks(2, 3)
1673 write
1674 call assert_equal([1, 4], [line("'["), line("']")])
1675
1676 call SetChangeMarks(2, 3)
1677 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1678 write
1679 au! BufWritePre
1680
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001681 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001682 write XtestFilter
1683 write >> XtestFilter
1684
1685 call SetChangeMarks(2, 3)
1686 " Marks are set to the entire range of the write
1687 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1688 " '[ is adjusted to just before the line that will receive the filtered
1689 " data
1690 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1691 " The filtered data is read into the buffer, and the source lines are
1692 " still present, so the range is after the source lines
1693 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1694 %!cat XtestFilter
1695 " After the filtered data is read, the original lines are deleted
1696 call assert_equal([1, 8], [line("'["), line("']")])
1697 au! FilterWritePre,FilterReadPre,FilterReadPost
1698 undo
1699
1700 call SetChangeMarks(1, 4)
1701 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1702 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1703 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1704 2,3!cat XtestFilter
1705 call assert_equal([2, 9], [line("'["), line("']")])
1706 au! FilterWritePre,FilterReadPre,FilterReadPost
1707 undo
1708
1709 call delete('XtestFilter')
1710 endif
1711
1712 call SetChangeMarks(1, 4)
1713 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1714 2,3write Xtest2
1715 au! FileWritePre
1716
1717 call SetChangeMarks(2, 3)
1718 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1719 write >> Xtest2
1720 au! FileAppendPre
1721
1722 call SetChangeMarks(1, 4)
1723 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1724 2,3write >> Xtest2
1725 au! FileAppendPre
1726
1727 call SetChangeMarks(1, 1)
1728 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1729 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1730 3read Xtest2
1731 au! FileReadPre,FileReadPost
1732 undo
1733
1734 call SetChangeMarks(4, 4)
1735 " When the line is 0, it's adjusted to 1
1736 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1737 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1738 0read Xtest2
1739 au! FileReadPre,FileReadPost
1740 undo
1741
1742 call SetChangeMarks(4, 4)
1743 " When the line is 0, it's adjusted to 1
1744 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1745 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1746 1read Xtest2
1747 au! FileReadPre,FileReadPost
1748 undo
1749
1750 bwipe!
1751 call delete('Xtest')
1752 call delete('Xtest2')
1753endfunc
1754
1755func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01001756 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01001757
1758 enew!
1759 call setline(1, ['a', 'b', 'c', 'd'])
1760
1761 let shelltemp = &shelltemp
1762 set shelltemp
1763
1764 let g:filter_au = 0
1765 au FilterWritePre * let g:filter_au += 1
1766 au FilterReadPre * let g:filter_au += 1
1767 au FilterReadPost * let g:filter_au += 1
1768 %!cat
1769 call assert_equal(3, g:filter_au)
1770
1771 if has('filterpipe')
1772 set noshelltemp
1773
1774 let g:filter_au = 0
1775 au FilterWritePre * let g:filter_au += 1
1776 au FilterReadPre * let g:filter_au += 1
1777 au FilterReadPost * let g:filter_au += 1
1778 %!cat
1779 call assert_equal(0, g:filter_au)
1780 endif
1781
1782 au! FilterWritePre,FilterReadPre,FilterReadPost
1783 let &shelltemp = shelltemp
1784 bwipe!
1785endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001786
1787func Test_TextYankPost()
1788 enew!
1789 call setline(1, ['foo'])
1790
1791 let g:event = []
1792 au TextYankPost * let g:event = copy(v:event)
1793
1794 call assert_equal({}, v:event)
1795 call assert_fails('let v:event = {}', 'E46:')
1796 call assert_fails('let v:event.mykey = 0', 'E742:')
1797
1798 norm "ayiw
1799 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001800 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001801 \g:event)
1802 norm y_
1803 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001804 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false},
1805 \g:event)
1806 norm Vy
1807 call assert_equal(
1808 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001809 \g:event)
1810 call feedkeys("\<C-V>y", 'x')
1811 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001812 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001813 \g:event)
1814 norm "xciwbar
1815 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001816 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001817 \g:event)
1818 norm "bdiw
1819 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001820 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001821 \g:event)
1822
1823 call assert_equal({}, v:event)
1824
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001825 if has('clipboard_working') && !has('gui_running')
1826 " Test that when the visual selection is automatically copied to clipboard
1827 " register a TextYankPost is emitted
1828 call setline(1, ['foobar'])
1829
1830 let @* = ''
1831 set clipboard=autoselect
1832 exe "norm! ggviw\<Esc>"
1833 call assert_equal(
1834 \{'regcontents': ['foobar'], 'regname': '*', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1835 \g:event)
1836
1837 let @+ = ''
1838 set clipboard=autoselectplus
1839 exe "norm! ggviw\<Esc>"
1840 call assert_equal(
1841 \{'regcontents': ['foobar'], 'regname': '+', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1842 \g:event)
1843
1844 set clipboard&vim
1845 endif
1846
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001847 au! TextYankPost
1848 unlet g:event
1849 bwipe!
1850endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001851
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01001852func Test_autocommand_all_events()
1853 call assert_fails('au * * bwipe', 'E1155:')
1854 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001855endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001856
1857function s:Before_test_dirchanged()
1858 augroup test_dirchanged
1859 autocmd!
1860 augroup END
1861 let s:li = []
1862 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001863 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001864 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001865 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001866 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001867endfunc
1868
1869function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001870 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001871 call delete(s:dir_foo, 'd')
1872 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001873 augroup test_dirchanged
1874 autocmd!
1875 augroup END
1876endfunc
1877
1878function Test_dirchanged_global()
1879 call s:Before_test_dirchanged()
1880 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1881 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001882 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001883 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001884 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001885 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001886 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001887 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001888 call s:After_test_dirchanged()
1889endfunc
1890
1891function Test_dirchanged_local()
1892 call s:Before_test_dirchanged()
1893 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1894 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001895 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001896 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001897 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001898 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001899 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001900 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001901 call s:After_test_dirchanged()
1902endfunc
1903
1904function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001905 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001906 call s:Before_test_dirchanged()
1907 call test_autochdir()
1908 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1909 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1910 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001911 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001912 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001913 exe 'edit ' . s:dir_foo . '/Xfile'
1914 call assert_equal(s:dir_foo, getcwd())
1915 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001916 set noacd
1917 bwipe!
1918 call s:After_test_dirchanged()
1919endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001920
1921" Test TextChangedI and TextChangedP
1922func Test_ChangedP()
1923 new
1924 call setline(1, ['foo', 'bar', 'foobar'])
1925 call test_override("char_avail", 1)
1926 set complete=. completeopt=menuone
1927
1928 func! TextChangedAutocmd(char)
1929 let g:autocmd .= a:char
1930 endfunc
1931
Christian Brabandtdb3b4462021-10-16 11:58:55 +01001932 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01001933 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1934 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1935 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1936
1937 call cursor(3, 1)
1938 let g:autocmd = ''
1939 call feedkeys("o\<esc>", 'tnix')
1940 call assert_equal('I', g:autocmd)
1941
1942 let g:autocmd = ''
1943 call feedkeys("Sf", 'tnix')
1944 call assert_equal('II', g:autocmd)
1945
1946 let g:autocmd = ''
1947 call feedkeys("Sf\<C-N>", 'tnix')
1948 call assert_equal('IIP', g:autocmd)
1949
1950 let g:autocmd = ''
1951 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1952 call assert_equal('IIPP', g:autocmd)
1953
1954 let g:autocmd = ''
1955 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1956 call assert_equal('IIPPP', g:autocmd)
1957
1958 let g:autocmd = ''
1959 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1960 call assert_equal('IIPPPP', g:autocmd)
1961
1962 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1963 " TODO: how should it handle completeopt=noinsert,noselect?
1964
1965 " CleanUp
1966 call test_override("char_avail", 0)
1967 au! TextChanged
1968 au! TextChangedI
1969 au! TextChangedP
1970 delfu TextChangedAutocmd
1971 unlet! g:autocmd
1972 set complete&vim completeopt&vim
1973
1974 bw!
1975endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001976
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001977let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001978func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001979 if !g:setline_handled
1980 call setline(1, "(x)")
1981 let g:setline_handled = v:true
1982 endif
1983endfunc
1984
1985func Test_TextChangedI_with_setline()
1986 new
1987 call test_override('char_avail', 1)
1988 autocmd TextChangedI <buffer> call SetLineOne()
1989 call feedkeys("i(\<CR>\<Esc>", 'tx')
1990 call assert_equal('(', getline(1))
1991 call assert_equal('x)', getline(2))
1992 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001993 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001994 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001995
1996 call test_override('starting', 0)
1997 bwipe!
1998endfunc
1999
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002000func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002001 CheckFeature terminal
2002 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002003 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002004 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002005
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002006 " Prepare file for TextChanged event.
2007 call writefile([''], 'Xchanged.txt')
2008 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2009 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002010 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002011 " In ConPTY, there is additional character which is drawn up to the width of
2012 " the screen.
2013 if has('conpty')
2014 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2015 else
2016 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2017 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002018 " It's only adding autocmd, so that no event occurs.
2019 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2020 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002021 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002022 call assert_equal([''], readfile('Xchanged.txt'))
2023
2024 " clean up
2025 call delete('Xchanged.txt')
2026 bwipe!
2027endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002028
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002029func Test_autocmd_nested()
2030 let g:did_nested = 0
2031 augroup Testing
2032 au WinNew * edit somefile
2033 au BufNew * let g:did_nested = 1
2034 augroup END
2035 split
2036 call assert_equal(0, g:did_nested)
2037 close
2038 bwipe! somefile
2039
2040 " old nested argument still works
2041 augroup Testing
2042 au!
2043 au WinNew * nested edit somefile
2044 au BufNew * let g:did_nested = 1
2045 augroup END
2046 split
2047 call assert_equal(1, g:did_nested)
2048 close
2049 bwipe! somefile
2050
2051 " New ++nested argument works
2052 augroup Testing
2053 au!
2054 au WinNew * ++nested edit somefile
2055 au BufNew * let g:did_nested = 1
2056 augroup END
2057 split
2058 call assert_equal(1, g:did_nested)
2059 close
2060 bwipe! somefile
2061
2062 augroup Testing
2063 au!
2064 augroup END
2065
2066 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2067 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2068endfunc
2069
2070func Test_autocmd_once()
2071 " Without ++once WinNew triggers twice
2072 let g:did_split = 0
2073 augroup Testing
2074 au WinNew * let g:did_split += 1
2075 augroup END
2076 split
2077 split
2078 call assert_equal(2, g:did_split)
2079 call assert_true(exists('#WinNew'))
2080 close
2081 close
2082
2083 " With ++once WinNew triggers once
2084 let g:did_split = 0
2085 augroup Testing
2086 au!
2087 au WinNew * ++once let g:did_split += 1
2088 augroup END
2089 split
2090 split
2091 call assert_equal(1, g:did_split)
2092 call assert_false(exists('#WinNew'))
2093 close
2094 close
2095
2096 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2097endfunc
2098
Bram Moolenaara68e5952019-04-25 22:22:01 +02002099func Test_autocmd_bufreadpre()
2100 new
2101 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002102 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002103 w! XAutocmdBufReadPre.txt
2104 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002105 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002106 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002107 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002108 wincmd p
2109 let g:wsv1 = winsaveview()
2110 wincmd p
2111 let g:wsv2 = winsaveview()
2112 " triggers BufReadPre, should not move the cursor in either window
2113 " The topline may change one line in a large window.
2114 edit
2115 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2116 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2117 call assert_equal(2, b:bufreadpre)
2118 wincmd p
2119 call assert_equal(g:wsv1.topline, winsaveview().topline)
2120 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2121 call assert_equal(2, b:bufreadpre)
2122 " Now set the cursor position in an BufReadPre autocommand
2123 " (even though the position will be invalid, this should make Vim reset the
2124 " cursor position in the other window.
2125 wincmd p
2126 set cpo+=g
2127 " won't do anything, but try to set the cursor on an invalid lnum
2128 autocmd BufReadPre <buffer> :norm! 70gg
2129 " triggers BufReadPre, should not move the cursor in either window
2130 e
2131 call assert_equal(1, winsaveview().topline)
2132 call assert_equal(1, winsaveview().lnum)
2133 call assert_equal(3, b:bufreadpre)
2134 wincmd p
2135 call assert_equal(g:wsv1.topline, winsaveview().topline)
2136 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2137 call assert_equal(3, b:bufreadpre)
2138 close
2139 close
2140 call delete('XAutocmdBufReadPre.txt')
2141 set cpo-=g
2142endfunc
2143
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002144" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002145
2146" Tests for the following autocommands:
2147" - FileWritePre writing a compressed file
2148" - FileReadPost reading a compressed file
2149" - BufNewFile reading a file template
2150" - BufReadPre decompressing the file to be read
2151" - FilterReadPre substituting characters in the temp file
2152" - FilterReadPost substituting characters after filtering
2153" - FileReadPre set options for decompression
2154" - FileReadPost decompress the file
2155func Test_ReadWrite_Autocmds()
2156 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002157 CheckUnix
2158 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002159
2160 " Make $GZIP empty, "-v" would cause trouble.
2161 let $GZIP = ""
2162
2163 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2164 " being modified outside of Vim (noticed on Solaris).
2165 au FileChangedShell * echo 'caught FileChangedShell'
2166
2167 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2168 augroup Test1
2169 au!
2170 au FileWritePre *.gz '[,']!gzip
2171 au FileWritePost *.gz undo
2172 au FileReadPost *.gz '[,']!gzip -d
2173 augroup END
2174
2175 new
2176 set bin
2177 call append(0, [
2178 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2179 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2180 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2181 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2182 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2183 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2184 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2185 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2186 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2187 \ ])
2188 1,9write! Xtestfile.gz
2189 enew! | close
2190
2191 new
2192 " Read and decompress the testfile
2193 0read Xtestfile.gz
2194 call assert_equal([
2195 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2196 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2197 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2198 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2199 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2200 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2201 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2202 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2203 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2204 \ ], getline(1, 9))
2205 enew! | close
2206
2207 augroup Test1
2208 au!
2209 augroup END
2210
2211 " Test for the FileAppendPre and FileAppendPost autocmds
2212 augroup Test2
2213 au!
2214 au BufNewFile *.c read Xtest.c
2215 au FileAppendPre *.out '[,']s/new/NEW/
2216 au FileAppendPost *.out !cat Xtest.c >> test.out
2217 augroup END
2218
2219 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2220 new foo.c " should load Xtest.c
2221 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2222 w! >> test.out " append it to the output file
2223
2224 let contents = readfile('test.out')
2225 call assert_equal(' * Here is a NEW .c file', contents[2])
2226 call assert_equal(' * Here is a new .c file', contents[5])
2227
2228 call delete('test.out')
2229 enew! | close
2230 augroup Test2
2231 au!
2232 augroup END
2233
2234 " Test for the BufReadPre and BufReadPost autocmds
2235 augroup Test3
2236 au!
2237 " setup autocommands to decompress before reading and re-compress
2238 " afterwards
2239 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2240 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2241 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2242 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2243 augroup END
2244
2245 e! Xtestfile.gz " Edit compressed file
2246 call assert_equal([
2247 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2248 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2249 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2250 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2251 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2252 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2253 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2254 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2255 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2256 \ ], getline(1, 9))
2257
2258 w! >> test.out " Append it to the output file
2259
2260 augroup Test3
2261 au!
2262 augroup END
2263
2264 " Test for the FilterReadPre and FilterReadPost autocmds.
2265 set shelltemp " need temp files here
2266 augroup Test4
2267 au!
2268 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2269 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2270 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2271 au FilterReadPost *.out '[,']s/x/X/g
2272 augroup END
2273
2274 e! test.out " Edit the output file
2275 1,$!cat
2276 call assert_equal([
2277 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2278 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2279 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2280 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2281 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2282 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2283 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2284 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2285 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2286 \ ], getline(1, 9))
2287 call assert_equal([
2288 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2289 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2290 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2291 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2292 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2293 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2294 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2295 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2296 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2297 \ ], readfile('test.out'))
2298
2299 augroup Test4
2300 au!
2301 augroup END
2302 set shelltemp&vim
2303
2304 " Test for the FileReadPre and FileReadPost autocmds.
2305 augroup Test5
2306 au!
2307 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2308 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2309 au FileReadPost *.gz '[,']s/l/L/
2310 augroup END
2311
2312 new
2313 0r Xtestfile.gz " Read compressed file
2314 call assert_equal([
2315 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2316 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2317 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2318 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2319 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2320 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2321 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2322 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2323 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2324 \ ], getline(1, 9))
2325 call assert_equal([
2326 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2327 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2328 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2329 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2330 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2331 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2332 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2333 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2334 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2335 \ ], readfile('Xtestfile.gz'))
2336
2337 augroup Test5
2338 au!
2339 augroup END
2340
2341 au! FileChangedShell
2342 call delete('Xtestfile.gz')
2343 call delete('Xtest.c')
2344 call delete('test.out')
2345endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002346
2347func Test_throw_in_BufWritePre()
2348 new
2349 call setline(1, ['one', 'two', 'three'])
2350 call assert_false(filereadable('Xthefile'))
2351 augroup throwing
2352 au BufWritePre X* throw 'do not write'
2353 augroup END
2354 try
2355 w Xthefile
2356 catch
2357 let caught = 1
2358 endtry
2359 call assert_equal(1, caught)
2360 call assert_false(filereadable('Xthefile'))
2361
2362 bwipe!
2363 au! throwing
2364endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002365
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002366func Test_autocmd_in_try_block()
2367 call mkdir('Xdir')
2368 au BufEnter * let g:fname = expand('%')
2369 try
2370 edit Xdir/
2371 endtry
2372 call assert_match('Xdir', g:fname)
2373
2374 unlet g:fname
2375 au! BufEnter
2376 call delete('Xdir', 'rf')
2377endfunc
2378
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002379func Test_autocmd_SafeState()
2380 CheckRunVimInTerminal
2381
2382 let lines =<< trim END
2383 let g:safe = 0
2384 let g:again = ''
2385 au SafeState * let g:safe += 1
2386 au SafeStateAgain * let g:again ..= 'x'
2387 func CallTimer()
2388 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2389 endfunc
2390 END
2391 call writefile(lines, 'XSafeState')
2392 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2393
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002394 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002395 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002396 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002397 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002398
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002399 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002400 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002401 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002402
2403 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002404 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002405 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002406 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002407 call term_sendkeys(buf, ":echo g:again\<CR>")
2408 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2409
2410 call StopVimInTerminal(buf)
2411 call delete('XSafeState')
2412endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002413
2414func Test_autocmd_CmdWinEnter()
2415 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002416 CheckFeature cmdwin
2417
Bram Moolenaar23324a02019-10-01 17:39:04 +02002418 let lines =<< trim END
2419 let b:dummy_var = 'This is a dummy'
2420 autocmd CmdWinEnter * quit
2421 let winnr = winnr('$')
2422 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002423 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002424 call writefile(lines, filename)
2425 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2426
2427 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002428 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002429 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002430 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002431 call term_sendkeys(buf, ":echo &buftype\<cr>")
2432 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2433 call term_sendkeys(buf, ":echo winnr\<cr>")
2434 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2435
2436 " clean up
2437 call StopVimInTerminal(buf)
2438 call delete(filename)
2439endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002440
2441func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002442 CheckFeature quickfix
2443
Bram Moolenaarec66c412019-10-11 21:19:13 +02002444 pedit xx
2445 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002446 augroup winenter
2447 au WinEnter * if winnr('$') > 2 | quit | endif
2448 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002449 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002450
2451 augroup winenter
2452 au! WinEnter
2453 augroup END
2454
2455 bwipe xx
2456 bwipe x
2457 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002458endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002459
2460func Test_BufWrite_lockmarks()
2461 edit! Xtest
2462 call setline(1, ['a', 'b', 'c', 'd'])
2463
2464 " :lockmarks preserves the marks
2465 call SetChangeMarks(2, 3)
2466 lockmarks write
2467 call assert_equal([2, 3], [line("'["), line("']")])
2468
2469 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2470 " original values for the user
2471 augroup lockmarks
2472 au!
2473 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2474 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2475 augroup END
2476
2477 lockmarks write
2478 call assert_equal([2, 3], [line("'["), line("']")])
2479
2480 if executable('cat')
2481 lockmarks %!cat
2482 call assert_equal([2, 3], [line("'["), line("']")])
2483 endif
2484
2485 lockmarks 3,4write Xtest2
2486 call assert_equal([2, 3], [line("'["), line("']")])
2487
2488 au! lockmarks
2489 augroup! lockmarks
2490 call delete('Xtest')
2491 call delete('Xtest2')
2492endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002493
2494func Test_FileType_spell()
2495 if !isdirectory('/tmp')
2496 throw "Skipped: requires /tmp directory"
2497 endif
2498
2499 " this was crashing with an invalid free()
2500 setglobal spellfile=/tmp/en.utf-8.add
2501 augroup crash
2502 autocmd!
2503 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2504 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2505 autocmd FileType anotherfiletype setlocal spell
2506 augroup END
2507 func! NoCrash() abort
2508 edit /tmp/crashfile
2509 endfunc
2510 call NoCrash()
2511
2512 au! crash
2513 setglobal spellfile=
2514endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002515
Bram Moolenaar406cd902020-02-18 21:54:41 +01002516" Test closing a window or editing another buffer from a FileChangedRO handler
2517" in a readonly buffer
2518func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002519 call test_override('ui_delay', 10)
2520
Bram Moolenaar406cd902020-02-18 21:54:41 +01002521 augroup FileChangedROTest
2522 au!
2523 autocmd FileChangedRO * quit
2524 augroup END
2525 new
2526 set readonly
2527 call assert_fails('normal i', 'E788:')
2528 close
2529 augroup! FileChangedROTest
2530
2531 augroup FileChangedROTest
2532 au!
2533 autocmd FileChangedRO * edit Xfile
2534 augroup END
2535 new
2536 set readonly
2537 call assert_fails('normal i', 'E788:')
2538 close
2539 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002540 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002541endfunc
2542
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002543func LogACmd()
2544 call add(g:logged, line('$'))
2545endfunc
2546
2547func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002548 CheckNotGui
2549
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002550 enew!
2551 tabnew
2552 call setline(1, ['a', 'b', 'c', 'd'])
2553 $
2554 au TermChanged * call LogACmd()
2555 let g:logged = []
2556 let term_save = &term
2557 set term=xterm
2558 call assert_equal([1, 4], g:logged)
2559
2560 au! TermChanged
2561 let &term = term_save
2562 bwipe!
2563endfunc
2564
Bram Moolenaare3284872020-03-19 13:55:03 +01002565" Test for FileReadCmd autocmd
2566func Test_autocmd_FileReadCmd()
2567 func ReadFileCmd()
2568 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2569 endfunc
2570 augroup FileReadCmdTest
2571 au!
2572 au FileReadCmd Xtest call ReadFileCmd()
2573 augroup END
2574
2575 new
2576 read ++bin Xtest
2577 read ++nobin Xtest
2578 read ++edit Xtest
2579 read ++bad=keep Xtest
2580 read ++bad=drop Xtest
2581 read ++bad=- Xtest
2582 read ++ff=unix Xtest
2583 read ++ff=dos Xtest
2584 read ++ff=mac Xtest
2585 read ++enc=utf-8 Xtest
2586
2587 call assert_equal(['',
2588 \ 'v:cmdarg = ++bin',
2589 \ 'v:cmdarg = ++nobin',
2590 \ 'v:cmdarg = ++edit',
2591 \ 'v:cmdarg = ++bad=keep',
2592 \ 'v:cmdarg = ++bad=drop',
2593 \ 'v:cmdarg = ++bad=-',
2594 \ 'v:cmdarg = ++ff=unix',
2595 \ 'v:cmdarg = ++ff=dos',
2596 \ 'v:cmdarg = ++ff=mac',
2597 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2598
2599 close!
2600 augroup FileReadCmdTest
2601 au!
2602 augroup END
2603 delfunc ReadFileCmd
2604endfunc
2605
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002606" Test for passing invalid arguments to autocmd
2607func Test_autocmd_invalid_args()
2608 " Additional character after * for event
2609 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2610 augroup Test
2611 augroup END
2612 " Invalid autocmd event
2613 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2614 " Invalid autocmd event in a autocmd group
2615 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2616 augroup! Test
2617 " Execute all autocmds
2618 call assert_fails('doautocmd * BufEnter', 'E217:')
2619 call assert_fails('augroup! x1a2b3', 'E367:')
2620 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002621 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002622endfunc
2623
2624" Test for deep nesting of autocmds
2625func Test_autocmd_deep_nesting()
2626 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2627 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2628 autocmd! BufEnter Xfile
2629endfunc
2630
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002631" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2632func Test_autocmd_sigusr1()
2633 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002634 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002635
2636 let g:sigusr1_passed = 0
2637 au SigUSR1 * let g:sigusr1_passed = 1
2638 call system('/bin/kill -s usr1 ' . getpid())
2639 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2640
2641 au! SigUSR1
2642 unlet g:sigusr1_passed
2643endfunc
2644
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002645" Test for BufReadPre autocmd deleting the file
2646func Test_BufReadPre_delfile()
2647 augroup TestAuCmd
2648 au!
2649 autocmd BufReadPre Xfile call delete('Xfile')
2650 augroup END
2651 call writefile([], 'Xfile')
2652 call assert_fails('new Xfile', 'E200:')
2653 call assert_equal('Xfile', @%)
2654 call assert_equal(1, &readonly)
2655 call delete('Xfile')
2656 augroup TestAuCmd
2657 au!
2658 augroup END
2659 close!
2660endfunc
2661
2662" Test for BufReadPre autocmd changing the current buffer
2663func Test_BufReadPre_changebuf()
2664 augroup TestAuCmd
2665 au!
2666 autocmd BufReadPre Xfile edit Xsomeotherfile
2667 augroup END
2668 call writefile([], 'Xfile')
2669 call assert_fails('new Xfile', 'E201:')
2670 call assert_equal('Xsomeotherfile', @%)
2671 call assert_equal(1, &readonly)
2672 call delete('Xfile')
2673 augroup TestAuCmd
2674 au!
2675 augroup END
2676 close!
2677endfunc
2678
2679" Test for BufWipeouti autocmd changing the current buffer when reading a file
2680" in an empty buffer with 'f' flag in 'cpo'
2681func Test_BufDelete_changebuf()
2682 new
2683 augroup TestAuCmd
2684 au!
2685 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2686 augroup END
2687 let save_cpo = &cpo
2688 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002689 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002690 call assert_equal('somefile', @%)
2691 let &cpo = save_cpo
2692 augroup TestAuCmd
2693 au!
2694 augroup END
2695 close!
2696endfunc
2697
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002698" Test for the temporary internal window used to execute autocmds
2699func Test_autocmd_window()
2700 %bw!
2701 edit one.txt
2702 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002703 vnew three.txt
2704 tabnew four.txt
2705 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002706 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002707 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002708 au!
2709 au BufEnter * call add(g:blist, [expand('<afile>'),
2710 \ win_gettype(bufwinnr(expand('<afile>')))])
2711 augroup END
2712
2713 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002714 call assert_equal([
2715 \ ['one.txt', 'autocmd'],
2716 \ ['two.txt', ''],
2717 \ ['four.txt', 'autocmd'],
2718 \ ['three.txt', ''],
2719 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002720
Bram Moolenaar832adf92020-06-25 19:01:36 +02002721 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002722 au!
2723 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002724 augroup! aucmd_win_test1
2725 %bw!
2726endfunc
2727
2728" Test for trying to close the temporary window used for executing an autocmd
2729func Test_close_autocmd_window()
2730 %bw!
2731 edit one.txt
2732 tabnew two.txt
2733 augroup aucmd_win_test2
2734 au!
2735 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2736 augroup END
2737
2738 call assert_fails('doautoall BufEnter', 'E813:')
2739
2740 augroup aucmd_win_test2
2741 au!
2742 augroup END
2743 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002744 %bwipe!
2745endfunc
2746
2747" Test for trying to close the tab that has the temporary window for exeucing
2748" an autocmd.
2749func Test_close_autocmd_tab()
2750 edit one.txt
2751 tabnew two.txt
2752 augroup aucmd_win_test
2753 au!
2754 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2755 augroup END
2756
2757 call assert_fails('doautoall BufEnter', 'E813:')
2758
2759 tabonly
2760 augroup aucmd_win_test
2761 au!
2762 augroup END
2763 augroup! aucmd_win_test
2764 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002765endfunc
2766
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01002767" This was using freed memory.
2768func Test_BufNew_arglocal()
2769 arglocal
2770 au BufNew * arglocal
2771 call assert_fails('drop xx', 'E1156:')
2772
2773 au! BufNew
2774endfunc
2775
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002776func Test_autocmd_closes_window()
2777 au BufNew,BufWinLeave * e %e
2778 file yyy
2779 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002780 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002781
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002782 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002783 au! BufNew
2784 au! BufWinLeave
2785endfunc
2786
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002787func Test_autocmd_quit_psearch()
2788 sn aa bb
2789 augroup aucmd_win_test
2790 au!
2791 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
2792 augroup END
2793 ps /
2794
2795 augroup aucmd_win_test
2796 au!
2797 augroup END
2798endfunc
2799
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002800" Fuzzer found some strange combination that caused a crash.
2801func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002802 " For unknown reason this hangs on MS-Windows
2803 CheckNotMSWindows
2804
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002805 augroup aucmd_normal_test
2806 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
2807 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002808 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002809 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002810 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002811 normal G
2812
2813 augroup aucmd_normal_test
2814 au!
2815 augroup END
2816endfunc
2817
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002818func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002819 " For unknown reason this hangs on MS-Windows
2820 CheckNotMSWindows
2821
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002822 au BufWinLeave * nested q
2823 call assert_fails("norm 7q?\n", 'E855:')
2824
2825 au! BufWinLeave
2826 new
2827 only
2828endfunc
2829
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002830func Test_autocmd_vimgrep()
2831 augroup aucmd_vimgrep
2832 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * sb
2833 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * q9�
2834 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002835 %bwipe!
Bram Moolenaardd07c022021-02-07 13:32:46 +01002836 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002837
2838 augroup aucmd_vimgrep
2839 au!
2840 augroup END
2841endfunc
2842
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002843func Test_autocmd_with_block()
2844 augroup block_testing
2845 au BufReadPost *.xml {
2846 setlocal matchpairs+=<:>
2847 /<start
2848 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02002849 au CursorHold * {
2850 autocmd BufReadPre * ++once echo 'one' | echo 'two'
2851 g:gotSafeState = 77
2852 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002853 augroup END
2854
2855 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
2856 call assert_equal(expected, execute('au BufReadPost *.xml'))
2857
Bram Moolenaar63b91732021-08-05 20:40:03 +02002858 doautocmd CursorHold
2859 call assert_equal(77, g:gotSafeState)
2860 unlet g:gotSafeState
2861
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002862 augroup block_testing
2863 au!
2864 augroup END
2865endfunc
2866
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002867" Test TextChangedI and TextChanged
2868func Test_Changed_ChangedI()
2869 new
2870 call test_override("char_avail", 1)
2871 let [g:autocmd_i, g:autocmd_n] = ['','']
2872
2873 func! TextChangedAutocmdI(char)
2874 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
2875 endfunc
2876
2877 augroup Test_TextChanged
2878 au!
2879 au TextChanged <buffer> :call TextChangedAutocmdI('N')
2880 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
2881 augroup END
2882
2883 call feedkeys("ifoo\<esc>", 'tnix')
2884 " TODO: Test test does not seem to trigger TextChanged autocommand, this
2885 " requires running Vim in a terminal window.
2886 " call assert_equal('N3', g:autocmd_n)
2887 call assert_equal('I3', g:autocmd_i)
2888
2889 call feedkeys("yyp", 'tnix')
2890 " TODO: Test test does not seem to trigger TextChanged autocommand.
2891 " call assert_equal('N4', g:autocmd_n)
2892 call assert_equal('I3', g:autocmd_i)
2893
2894 " CleanUp
2895 call test_override("char_avail", 0)
2896 au! TextChanged <buffer>
2897 au! TextChangedI <buffer>
2898 augroup! Test_TextChanged
2899 delfu TextChangedAutocmdI
2900 unlet! g:autocmd_i g:autocmd_n
2901
2902 bw!
2903endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002904
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002905" vim: shiftwidth=2 sts=2 expandtab