blob: db1efba12db6f369d75e557d954da73747003ed7 [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
1932 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1933 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1934 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1935
1936 call cursor(3, 1)
1937 let g:autocmd = ''
1938 call feedkeys("o\<esc>", 'tnix')
1939 call assert_equal('I', g:autocmd)
1940
1941 let g:autocmd = ''
1942 call feedkeys("Sf", 'tnix')
1943 call assert_equal('II', g:autocmd)
1944
1945 let g:autocmd = ''
1946 call feedkeys("Sf\<C-N>", 'tnix')
1947 call assert_equal('IIP', g:autocmd)
1948
1949 let g:autocmd = ''
1950 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1951 call assert_equal('IIPP', g:autocmd)
1952
1953 let g:autocmd = ''
1954 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1955 call assert_equal('IIPPP', g:autocmd)
1956
1957 let g:autocmd = ''
1958 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1959 call assert_equal('IIPPPP', g:autocmd)
1960
1961 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1962 " TODO: how should it handle completeopt=noinsert,noselect?
1963
1964 " CleanUp
1965 call test_override("char_avail", 0)
1966 au! TextChanged
1967 au! TextChangedI
1968 au! TextChangedP
1969 delfu TextChangedAutocmd
1970 unlet! g:autocmd
1971 set complete&vim completeopt&vim
1972
1973 bw!
1974endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001975
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001976let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001977func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001978 if !g:setline_handled
1979 call setline(1, "(x)")
1980 let g:setline_handled = v:true
1981 endif
1982endfunc
1983
1984func Test_TextChangedI_with_setline()
1985 new
1986 call test_override('char_avail', 1)
1987 autocmd TextChangedI <buffer> call SetLineOne()
1988 call feedkeys("i(\<CR>\<Esc>", 'tx')
1989 call assert_equal('(', getline(1))
1990 call assert_equal('x)', getline(2))
1991 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001992 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001993 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001994
1995 call test_override('starting', 0)
1996 bwipe!
1997endfunc
1998
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001999func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002000 CheckFeature terminal
2001 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002002 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002003 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002004
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002005 " Prepare file for TextChanged event.
2006 call writefile([''], 'Xchanged.txt')
2007 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2008 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002009 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002010 " In ConPTY, there is additional character which is drawn up to the width of
2011 " the screen.
2012 if has('conpty')
2013 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2014 else
2015 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2016 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002017 " It's only adding autocmd, so that no event occurs.
2018 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2019 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002020 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002021 call assert_equal([''], readfile('Xchanged.txt'))
2022
2023 " clean up
2024 call delete('Xchanged.txt')
2025 bwipe!
2026endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002027
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002028func Test_autocmd_nested()
2029 let g:did_nested = 0
2030 augroup Testing
2031 au WinNew * edit somefile
2032 au BufNew * let g:did_nested = 1
2033 augroup END
2034 split
2035 call assert_equal(0, g:did_nested)
2036 close
2037 bwipe! somefile
2038
2039 " old nested argument still works
2040 augroup Testing
2041 au!
2042 au WinNew * nested edit somefile
2043 au BufNew * let g:did_nested = 1
2044 augroup END
2045 split
2046 call assert_equal(1, g:did_nested)
2047 close
2048 bwipe! somefile
2049
2050 " New ++nested argument works
2051 augroup Testing
2052 au!
2053 au WinNew * ++nested edit somefile
2054 au BufNew * let g:did_nested = 1
2055 augroup END
2056 split
2057 call assert_equal(1, g:did_nested)
2058 close
2059 bwipe! somefile
2060
2061 augroup Testing
2062 au!
2063 augroup END
2064
2065 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2066 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2067endfunc
2068
2069func Test_autocmd_once()
2070 " Without ++once WinNew triggers twice
2071 let g:did_split = 0
2072 augroup Testing
2073 au WinNew * let g:did_split += 1
2074 augroup END
2075 split
2076 split
2077 call assert_equal(2, g:did_split)
2078 call assert_true(exists('#WinNew'))
2079 close
2080 close
2081
2082 " With ++once WinNew triggers once
2083 let g:did_split = 0
2084 augroup Testing
2085 au!
2086 au WinNew * ++once let g:did_split += 1
2087 augroup END
2088 split
2089 split
2090 call assert_equal(1, g:did_split)
2091 call assert_false(exists('#WinNew'))
2092 close
2093 close
2094
2095 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2096endfunc
2097
Bram Moolenaara68e5952019-04-25 22:22:01 +02002098func Test_autocmd_bufreadpre()
2099 new
2100 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002101 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002102 w! XAutocmdBufReadPre.txt
2103 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002104 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002105 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002106 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002107 wincmd p
2108 let g:wsv1 = winsaveview()
2109 wincmd p
2110 let g:wsv2 = winsaveview()
2111 " triggers BufReadPre, should not move the cursor in either window
2112 " The topline may change one line in a large window.
2113 edit
2114 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2115 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2116 call assert_equal(2, b:bufreadpre)
2117 wincmd p
2118 call assert_equal(g:wsv1.topline, winsaveview().topline)
2119 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2120 call assert_equal(2, b:bufreadpre)
2121 " Now set the cursor position in an BufReadPre autocommand
2122 " (even though the position will be invalid, this should make Vim reset the
2123 " cursor position in the other window.
2124 wincmd p
2125 set cpo+=g
2126 " won't do anything, but try to set the cursor on an invalid lnum
2127 autocmd BufReadPre <buffer> :norm! 70gg
2128 " triggers BufReadPre, should not move the cursor in either window
2129 e
2130 call assert_equal(1, winsaveview().topline)
2131 call assert_equal(1, winsaveview().lnum)
2132 call assert_equal(3, b:bufreadpre)
2133 wincmd p
2134 call assert_equal(g:wsv1.topline, winsaveview().topline)
2135 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2136 call assert_equal(3, b:bufreadpre)
2137 close
2138 close
2139 call delete('XAutocmdBufReadPre.txt')
2140 set cpo-=g
2141endfunc
2142
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002143" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002144
2145" Tests for the following autocommands:
2146" - FileWritePre writing a compressed file
2147" - FileReadPost reading a compressed file
2148" - BufNewFile reading a file template
2149" - BufReadPre decompressing the file to be read
2150" - FilterReadPre substituting characters in the temp file
2151" - FilterReadPost substituting characters after filtering
2152" - FileReadPre set options for decompression
2153" - FileReadPost decompress the file
2154func Test_ReadWrite_Autocmds()
2155 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002156 CheckUnix
2157 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002158
2159 " Make $GZIP empty, "-v" would cause trouble.
2160 let $GZIP = ""
2161
2162 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2163 " being modified outside of Vim (noticed on Solaris).
2164 au FileChangedShell * echo 'caught FileChangedShell'
2165
2166 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2167 augroup Test1
2168 au!
2169 au FileWritePre *.gz '[,']!gzip
2170 au FileWritePost *.gz undo
2171 au FileReadPost *.gz '[,']!gzip -d
2172 augroup END
2173
2174 new
2175 set bin
2176 call append(0, [
2177 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2178 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2179 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2180 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2181 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2182 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2183 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2184 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2185 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2186 \ ])
2187 1,9write! Xtestfile.gz
2188 enew! | close
2189
2190 new
2191 " Read and decompress the testfile
2192 0read Xtestfile.gz
2193 call assert_equal([
2194 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2195 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2196 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2197 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2198 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2199 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2200 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2201 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2202 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2203 \ ], getline(1, 9))
2204 enew! | close
2205
2206 augroup Test1
2207 au!
2208 augroup END
2209
2210 " Test for the FileAppendPre and FileAppendPost autocmds
2211 augroup Test2
2212 au!
2213 au BufNewFile *.c read Xtest.c
2214 au FileAppendPre *.out '[,']s/new/NEW/
2215 au FileAppendPost *.out !cat Xtest.c >> test.out
2216 augroup END
2217
2218 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2219 new foo.c " should load Xtest.c
2220 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2221 w! >> test.out " append it to the output file
2222
2223 let contents = readfile('test.out')
2224 call assert_equal(' * Here is a NEW .c file', contents[2])
2225 call assert_equal(' * Here is a new .c file', contents[5])
2226
2227 call delete('test.out')
2228 enew! | close
2229 augroup Test2
2230 au!
2231 augroup END
2232
2233 " Test for the BufReadPre and BufReadPost autocmds
2234 augroup Test3
2235 au!
2236 " setup autocommands to decompress before reading and re-compress
2237 " afterwards
2238 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2239 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2240 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2241 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2242 augroup END
2243
2244 e! Xtestfile.gz " Edit compressed file
2245 call assert_equal([
2246 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2247 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2248 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2249 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2250 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2251 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2252 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2253 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2254 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2255 \ ], getline(1, 9))
2256
2257 w! >> test.out " Append it to the output file
2258
2259 augroup Test3
2260 au!
2261 augroup END
2262
2263 " Test for the FilterReadPre and FilterReadPost autocmds.
2264 set shelltemp " need temp files here
2265 augroup Test4
2266 au!
2267 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2268 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2269 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2270 au FilterReadPost *.out '[,']s/x/X/g
2271 augroup END
2272
2273 e! test.out " Edit the output file
2274 1,$!cat
2275 call assert_equal([
2276 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2277 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2278 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2279 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2280 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2281 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2282 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2283 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2284 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2285 \ ], getline(1, 9))
2286 call assert_equal([
2287 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2288 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2289 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2290 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2291 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2292 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2293 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2294 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2295 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2296 \ ], readfile('test.out'))
2297
2298 augroup Test4
2299 au!
2300 augroup END
2301 set shelltemp&vim
2302
2303 " Test for the FileReadPre and FileReadPost autocmds.
2304 augroup Test5
2305 au!
2306 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2307 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2308 au FileReadPost *.gz '[,']s/l/L/
2309 augroup END
2310
2311 new
2312 0r Xtestfile.gz " Read compressed file
2313 call assert_equal([
2314 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2315 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2316 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2317 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2318 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2319 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2320 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2321 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2322 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2323 \ ], getline(1, 9))
2324 call assert_equal([
2325 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2326 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2327 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2328 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2329 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2330 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2331 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2332 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2333 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2334 \ ], readfile('Xtestfile.gz'))
2335
2336 augroup Test5
2337 au!
2338 augroup END
2339
2340 au! FileChangedShell
2341 call delete('Xtestfile.gz')
2342 call delete('Xtest.c')
2343 call delete('test.out')
2344endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002345
2346func Test_throw_in_BufWritePre()
2347 new
2348 call setline(1, ['one', 'two', 'three'])
2349 call assert_false(filereadable('Xthefile'))
2350 augroup throwing
2351 au BufWritePre X* throw 'do not write'
2352 augroup END
2353 try
2354 w Xthefile
2355 catch
2356 let caught = 1
2357 endtry
2358 call assert_equal(1, caught)
2359 call assert_false(filereadable('Xthefile'))
2360
2361 bwipe!
2362 au! throwing
2363endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002364
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002365func Test_autocmd_in_try_block()
2366 call mkdir('Xdir')
2367 au BufEnter * let g:fname = expand('%')
2368 try
2369 edit Xdir/
2370 endtry
2371 call assert_match('Xdir', g:fname)
2372
2373 unlet g:fname
2374 au! BufEnter
2375 call delete('Xdir', 'rf')
2376endfunc
2377
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002378func Test_autocmd_SafeState()
2379 CheckRunVimInTerminal
2380
2381 let lines =<< trim END
2382 let g:safe = 0
2383 let g:again = ''
2384 au SafeState * let g:safe += 1
2385 au SafeStateAgain * let g:again ..= 'x'
2386 func CallTimer()
2387 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2388 endfunc
2389 END
2390 call writefile(lines, 'XSafeState')
2391 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2392
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002393 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002394 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002395 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002396 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002397
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002398 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002399 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002400 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002401
2402 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002403 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002404 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002405 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002406 call term_sendkeys(buf, ":echo g:again\<CR>")
2407 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2408
2409 call StopVimInTerminal(buf)
2410 call delete('XSafeState')
2411endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002412
2413func Test_autocmd_CmdWinEnter()
2414 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002415 CheckFeature cmdwin
2416
Bram Moolenaar23324a02019-10-01 17:39:04 +02002417 let lines =<< trim END
2418 let b:dummy_var = 'This is a dummy'
2419 autocmd CmdWinEnter * quit
2420 let winnr = winnr('$')
2421 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002422 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002423 call writefile(lines, filename)
2424 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2425
2426 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002427 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002428 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002429 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002430 call term_sendkeys(buf, ":echo &buftype\<cr>")
2431 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2432 call term_sendkeys(buf, ":echo winnr\<cr>")
2433 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2434
2435 " clean up
2436 call StopVimInTerminal(buf)
2437 call delete(filename)
2438endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002439
2440func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002441 CheckFeature quickfix
2442
Bram Moolenaarec66c412019-10-11 21:19:13 +02002443 pedit xx
2444 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002445 augroup winenter
2446 au WinEnter * if winnr('$') > 2 | quit | endif
2447 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002448 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002449
2450 augroup winenter
2451 au! WinEnter
2452 augroup END
2453
2454 bwipe xx
2455 bwipe x
2456 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002457endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002458
2459func Test_BufWrite_lockmarks()
2460 edit! Xtest
2461 call setline(1, ['a', 'b', 'c', 'd'])
2462
2463 " :lockmarks preserves the marks
2464 call SetChangeMarks(2, 3)
2465 lockmarks write
2466 call assert_equal([2, 3], [line("'["), line("']")])
2467
2468 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2469 " original values for the user
2470 augroup lockmarks
2471 au!
2472 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2473 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2474 augroup END
2475
2476 lockmarks write
2477 call assert_equal([2, 3], [line("'["), line("']")])
2478
2479 if executable('cat')
2480 lockmarks %!cat
2481 call assert_equal([2, 3], [line("'["), line("']")])
2482 endif
2483
2484 lockmarks 3,4write Xtest2
2485 call assert_equal([2, 3], [line("'["), line("']")])
2486
2487 au! lockmarks
2488 augroup! lockmarks
2489 call delete('Xtest')
2490 call delete('Xtest2')
2491endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002492
2493func Test_FileType_spell()
2494 if !isdirectory('/tmp')
2495 throw "Skipped: requires /tmp directory"
2496 endif
2497
2498 " this was crashing with an invalid free()
2499 setglobal spellfile=/tmp/en.utf-8.add
2500 augroup crash
2501 autocmd!
2502 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2503 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2504 autocmd FileType anotherfiletype setlocal spell
2505 augroup END
2506 func! NoCrash() abort
2507 edit /tmp/crashfile
2508 endfunc
2509 call NoCrash()
2510
2511 au! crash
2512 setglobal spellfile=
2513endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002514
Bram Moolenaar406cd902020-02-18 21:54:41 +01002515" Test closing a window or editing another buffer from a FileChangedRO handler
2516" in a readonly buffer
2517func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002518 call test_override('ui_delay', 10)
2519
Bram Moolenaar406cd902020-02-18 21:54:41 +01002520 augroup FileChangedROTest
2521 au!
2522 autocmd FileChangedRO * quit
2523 augroup END
2524 new
2525 set readonly
2526 call assert_fails('normal i', 'E788:')
2527 close
2528 augroup! FileChangedROTest
2529
2530 augroup FileChangedROTest
2531 au!
2532 autocmd FileChangedRO * edit Xfile
2533 augroup END
2534 new
2535 set readonly
2536 call assert_fails('normal i', 'E788:')
2537 close
2538 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002539 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002540endfunc
2541
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002542func LogACmd()
2543 call add(g:logged, line('$'))
2544endfunc
2545
2546func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002547 CheckNotGui
2548
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002549 enew!
2550 tabnew
2551 call setline(1, ['a', 'b', 'c', 'd'])
2552 $
2553 au TermChanged * call LogACmd()
2554 let g:logged = []
2555 let term_save = &term
2556 set term=xterm
2557 call assert_equal([1, 4], g:logged)
2558
2559 au! TermChanged
2560 let &term = term_save
2561 bwipe!
2562endfunc
2563
Bram Moolenaare3284872020-03-19 13:55:03 +01002564" Test for FileReadCmd autocmd
2565func Test_autocmd_FileReadCmd()
2566 func ReadFileCmd()
2567 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2568 endfunc
2569 augroup FileReadCmdTest
2570 au!
2571 au FileReadCmd Xtest call ReadFileCmd()
2572 augroup END
2573
2574 new
2575 read ++bin Xtest
2576 read ++nobin Xtest
2577 read ++edit Xtest
2578 read ++bad=keep Xtest
2579 read ++bad=drop Xtest
2580 read ++bad=- Xtest
2581 read ++ff=unix Xtest
2582 read ++ff=dos Xtest
2583 read ++ff=mac Xtest
2584 read ++enc=utf-8 Xtest
2585
2586 call assert_equal(['',
2587 \ 'v:cmdarg = ++bin',
2588 \ 'v:cmdarg = ++nobin',
2589 \ 'v:cmdarg = ++edit',
2590 \ 'v:cmdarg = ++bad=keep',
2591 \ 'v:cmdarg = ++bad=drop',
2592 \ 'v:cmdarg = ++bad=-',
2593 \ 'v:cmdarg = ++ff=unix',
2594 \ 'v:cmdarg = ++ff=dos',
2595 \ 'v:cmdarg = ++ff=mac',
2596 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2597
2598 close!
2599 augroup FileReadCmdTest
2600 au!
2601 augroup END
2602 delfunc ReadFileCmd
2603endfunc
2604
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002605" Test for passing invalid arguments to autocmd
2606func Test_autocmd_invalid_args()
2607 " Additional character after * for event
2608 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2609 augroup Test
2610 augroup END
2611 " Invalid autocmd event
2612 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2613 " Invalid autocmd event in a autocmd group
2614 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2615 augroup! Test
2616 " Execute all autocmds
2617 call assert_fails('doautocmd * BufEnter', 'E217:')
2618 call assert_fails('augroup! x1a2b3', 'E367:')
2619 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002620 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002621endfunc
2622
2623" Test for deep nesting of autocmds
2624func Test_autocmd_deep_nesting()
2625 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2626 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2627 autocmd! BufEnter Xfile
2628endfunc
2629
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002630" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2631func Test_autocmd_sigusr1()
2632 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002633 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002634
2635 let g:sigusr1_passed = 0
2636 au SigUSR1 * let g:sigusr1_passed = 1
2637 call system('/bin/kill -s usr1 ' . getpid())
2638 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2639
2640 au! SigUSR1
2641 unlet g:sigusr1_passed
2642endfunc
2643
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002644" Test for BufReadPre autocmd deleting the file
2645func Test_BufReadPre_delfile()
2646 augroup TestAuCmd
2647 au!
2648 autocmd BufReadPre Xfile call delete('Xfile')
2649 augroup END
2650 call writefile([], 'Xfile')
2651 call assert_fails('new Xfile', 'E200:')
2652 call assert_equal('Xfile', @%)
2653 call assert_equal(1, &readonly)
2654 call delete('Xfile')
2655 augroup TestAuCmd
2656 au!
2657 augroup END
2658 close!
2659endfunc
2660
2661" Test for BufReadPre autocmd changing the current buffer
2662func Test_BufReadPre_changebuf()
2663 augroup TestAuCmd
2664 au!
2665 autocmd BufReadPre Xfile edit Xsomeotherfile
2666 augroup END
2667 call writefile([], 'Xfile')
2668 call assert_fails('new Xfile', 'E201:')
2669 call assert_equal('Xsomeotherfile', @%)
2670 call assert_equal(1, &readonly)
2671 call delete('Xfile')
2672 augroup TestAuCmd
2673 au!
2674 augroup END
2675 close!
2676endfunc
2677
2678" Test for BufWipeouti autocmd changing the current buffer when reading a file
2679" in an empty buffer with 'f' flag in 'cpo'
2680func Test_BufDelete_changebuf()
2681 new
2682 augroup TestAuCmd
2683 au!
2684 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2685 augroup END
2686 let save_cpo = &cpo
2687 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002688 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002689 call assert_equal('somefile', @%)
2690 let &cpo = save_cpo
2691 augroup TestAuCmd
2692 au!
2693 augroup END
2694 close!
2695endfunc
2696
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002697" Test for the temporary internal window used to execute autocmds
2698func Test_autocmd_window()
2699 %bw!
2700 edit one.txt
2701 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002702 vnew three.txt
2703 tabnew four.txt
2704 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002705 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002706 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002707 au!
2708 au BufEnter * call add(g:blist, [expand('<afile>'),
2709 \ win_gettype(bufwinnr(expand('<afile>')))])
2710 augroup END
2711
2712 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002713 call assert_equal([
2714 \ ['one.txt', 'autocmd'],
2715 \ ['two.txt', ''],
2716 \ ['four.txt', 'autocmd'],
2717 \ ['three.txt', ''],
2718 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002719
Bram Moolenaar832adf92020-06-25 19:01:36 +02002720 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002721 au!
2722 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002723 augroup! aucmd_win_test1
2724 %bw!
2725endfunc
2726
2727" Test for trying to close the temporary window used for executing an autocmd
2728func Test_close_autocmd_window()
2729 %bw!
2730 edit one.txt
2731 tabnew two.txt
2732 augroup aucmd_win_test2
2733 au!
2734 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2735 augroup END
2736
2737 call assert_fails('doautoall BufEnter', 'E813:')
2738
2739 augroup aucmd_win_test2
2740 au!
2741 augroup END
2742 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002743 %bwipe!
2744endfunc
2745
2746" Test for trying to close the tab that has the temporary window for exeucing
2747" an autocmd.
2748func Test_close_autocmd_tab()
2749 edit one.txt
2750 tabnew two.txt
2751 augroup aucmd_win_test
2752 au!
2753 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2754 augroup END
2755
2756 call assert_fails('doautoall BufEnter', 'E813:')
2757
2758 tabonly
2759 augroup aucmd_win_test
2760 au!
2761 augroup END
2762 augroup! aucmd_win_test
2763 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002764endfunc
2765
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01002766" This was using freed memory.
2767func Test_BufNew_arglocal()
2768 arglocal
2769 au BufNew * arglocal
2770 call assert_fails('drop xx', 'E1156:')
2771
2772 au! BufNew
2773endfunc
2774
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002775func Test_autocmd_closes_window()
2776 au BufNew,BufWinLeave * e %e
2777 file yyy
2778 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002779 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002780
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002781 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002782 au! BufNew
2783 au! BufWinLeave
2784endfunc
2785
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002786func Test_autocmd_quit_psearch()
2787 sn aa bb
2788 augroup aucmd_win_test
2789 au!
2790 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
2791 augroup END
2792 ps /
2793
2794 augroup aucmd_win_test
2795 au!
2796 augroup END
2797endfunc
2798
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002799" Fuzzer found some strange combination that caused a crash.
2800func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002801 " For unknown reason this hangs on MS-Windows
2802 CheckNotMSWindows
2803
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002804 augroup aucmd_normal_test
2805 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
2806 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002807 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002808 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002809 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002810 normal G
2811
2812 augroup aucmd_normal_test
2813 au!
2814 augroup END
2815endfunc
2816
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002817func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002818 " For unknown reason this hangs on MS-Windows
2819 CheckNotMSWindows
2820
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002821 au BufWinLeave * nested q
2822 call assert_fails("norm 7q?\n", 'E855:')
2823
2824 au! BufWinLeave
2825 new
2826 only
2827endfunc
2828
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002829func Test_autocmd_vimgrep()
2830 augroup aucmd_vimgrep
2831 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * sb
2832 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * q9�
2833 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002834 %bwipe!
Bram Moolenaardd07c022021-02-07 13:32:46 +01002835 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002836
2837 augroup aucmd_vimgrep
2838 au!
2839 augroup END
2840endfunc
2841
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002842func Test_autocmd_with_block()
2843 augroup block_testing
2844 au BufReadPost *.xml {
2845 setlocal matchpairs+=<:>
2846 /<start
2847 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02002848 au CursorHold * {
2849 autocmd BufReadPre * ++once echo 'one' | echo 'two'
2850 g:gotSafeState = 77
2851 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002852 augroup END
2853
2854 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
2855 call assert_equal(expected, execute('au BufReadPost *.xml'))
2856
Bram Moolenaar63b91732021-08-05 20:40:03 +02002857 doautocmd CursorHold
2858 call assert_equal(77, g:gotSafeState)
2859 unlet g:gotSafeState
2860
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002861 augroup block_testing
2862 au!
2863 augroup END
2864endfunc
2865
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002866
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002867" vim: shiftwidth=2 sts=2 expandtab