blob: 045320e5f59ee3b64dc0d6835ba7827aff1a2b10 [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')
naohiro ono23beefe2021-11-13 12:38:49 +0000273 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200274 au WinEnter * call add(g:record, 'WinEnter')
275 au WinLeave * call add(g:record, 'WinLeave')
276 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200277 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200278 au TabEnter * call add(g:record, 'TabEnter')
279 au TabLeave * call add(g:record, 'TabLeave')
280 augroup END
281
282 split
283 tabnew
284 close
285 close
286
287 call assert_equal([
288 \ 'WinLeave', 'WinNew', 'WinEnter',
289 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000290 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
291 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200292 \ ], g:record)
293
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200294 let g:record = []
295 tabnew somefile
296 tabnext
297 bwipe somefile
298
299 call assert_equal([
300 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
301 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000302 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200303 \ ], g:record)
304
Bram Moolenaarc917da42016-07-19 22:31:36 +0200305 augroup testing
306 au!
307 augroup END
308 unlet g:record
309endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200310
naohiro ono23beefe2021-11-13 12:38:49 +0000311func Test_WinClosed()
312 " Test that the pattern is matched against the closed window's ID, and both
313 " <amatch> and <afile> are set to it.
314 new
315 let winid = win_getid()
316 let g:matched = v:false
317 augroup test-WinClosed
318 autocmd!
319 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
320 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
321 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
322 augroup END
323 close
324 call assert_true(g:matched)
325 call assert_equal(winid, g:amatch)
326 call assert_equal(winid, g:afile)
327
328 " Test that WinClosed is non-recursive.
329 new
330 new
331 call assert_equal(3, winnr('$'))
332 let g:triggered = 0
333 augroup test-WinClosed
334 autocmd!
335 autocmd WinClosed * let g:triggered += 1
336 autocmd WinClosed * 2 wincmd c
337 augroup END
338 close
339 call assert_equal(1, winnr('$'))
340 call assert_equal(1, g:triggered)
341
342 autocmd! test-WinClosed
343 augroup! test-WinClosed
344 unlet g:matched
345 unlet g:amatch
346 unlet g:afile
347 unlet g:triggered
348endfunc
349
Bram Moolenaare99e8442016-07-26 20:43:40 +0200350func s:AddAnAutocmd()
351 augroup vimBarTest
352 au BufReadCmd * echo 'hello'
353 augroup END
354 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
355endfunc
356
357func Test_early_bar()
358 " test that a bar is recognized before the {event}
359 call s:AddAnAutocmd()
360 augroup vimBarTest | au! | augroup END
361 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
362
363 call s:AddAnAutocmd()
364 augroup vimBarTest| au!| augroup END
365 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
366
367 " test that a bar is recognized after the {event}
368 call s:AddAnAutocmd()
369 augroup vimBarTest| au!BufReadCmd| augroup END
370 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
371
372 " test that a bar is recognized after the {group}
373 call s:AddAnAutocmd()
374 au! vimBarTest|echo 'hello'
375 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
376endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200377
Bram Moolenaar5c809082016-09-01 16:21:48 +0200378func RemoveGroup()
379 autocmd! StartOK
380 augroup! StartOK
381endfunc
382
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200383func Test_augroup_warning()
384 augroup TheWarning
385 au VimEnter * echo 'entering'
386 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100387 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200388 redir => res
389 augroup! TheWarning
390 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100391 call assert_match("W19:", res)
392 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200393
394 " check "Another" does not take the pace of the deleted entry
395 augroup Another
396 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100397 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200398 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200399
400 " no warning for postpone aucmd delete
401 augroup StartOK
402 au VimEnter * call RemoveGroup()
403 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100404 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200405 redir => res
406 doautocmd VimEnter
407 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100408 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200409 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200410
411 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200412endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200413
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200414func Test_BufReadCmdHelp()
415 " This used to cause access to free memory
416 au BufReadCmd * e +h
417 help
418
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200419 au! BufReadCmd
420endfunc
421
422func Test_BufReadCmdHelpJump()
423 " This used to cause access to free memory
424 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200425 " } to fix highlighting
426 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200427
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200428 au! BufReadCmd
429endfunc
430
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200431func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200432 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200433 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200434 call assert_fails('augroup! x', 'E936:')
435 au VimEnter * echo
436 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200437 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100438 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200439 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200440endfunc
441
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200442" Tests for autocommands on :close command.
443" This used to be in test13.
444func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200445 " Clean up buffers, because in some cases this function fails.
446 call s:cleanup_buffers()
447
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200448 " Write three files and open them, each in a window.
449 " Then go to next window, with autocommand that deletes the previous one.
450 " Do this twice, writing the file.
451 e! Xtestje1
452 call setline(1, 'testje1')
453 w
454 sp Xtestje2
455 call setline(1, 'testje2')
456 w
457 sp Xtestje3
458 call setline(1, 'testje3')
459 w
460 wincmd w
461 au WinLeave Xtestje2 bwipe
462 wincmd w
463 call assert_equal('Xtestje1', expand('%'))
464
465 au WinLeave Xtestje1 bwipe Xtestje3
466 close
467 call assert_equal('Xtestje1', expand('%'))
468
469 " Test deleting the buffer on a Unload event. If this goes wrong there
470 " will be the ATTENTION prompt.
471 e Xtestje1
472 au!
473 au! BufUnload Xtestje1 bwipe
474 call assert_fails('e Xtestje3', 'E937:')
475 call assert_equal('Xtestje3', expand('%'))
476
477 e Xtestje2
478 sp Xtestje1
479 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200480 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200481
482 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
483 " there are ml_line errors and/or a Crash.
484 au!
485 only
486 e Xanother
487 e Xtestje1
488 bwipe Xtestje2
489 bwipe Xtestje3
490 au BufWipeout Xtestje1 buf Xtestje1
491 bwipe
492 call assert_equal('Xanother', expand('%'))
493
494 only
495 help
496 wincmd w
497 1quit
498 call assert_equal('Xanother', expand('%'))
499
500 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100501 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200502 call delete('Xtestje1')
503 call delete('Xtestje2')
504 call delete('Xtestje3')
505endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100506
507func Test_BufEnter()
508 au! BufEnter
509 au Bufenter * let val = val . '+'
510 let g:val = ''
511 split NewFile
512 call assert_equal('+', g:val)
513 bwipe!
514 call assert_equal('++', g:val)
515
516 " Also get BufEnter when editing a directory
517 call mkdir('Xdir')
518 split Xdir
519 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100520
521 " On MS-Windows we can't edit the directory, make sure we wipe the right
522 " buffer.
523 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100524
525 call delete('Xdir', 'd')
526 au! BufEnter
527endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100528
529" Closing a window might cause an endless loop
530" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200531func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200532 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100533 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200534 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100535 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100536 mksession!
537
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200538 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200539 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200540 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100541 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200542 augroup test_autocmd_sessionload
543 autocmd!
544 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
545 augroup END
546
547 func WriteErrors()
548 call writefile([execute("messages")], "Xerrors")
549 endfunc
550 au VimLeave * call WriteErrors()
551 [CODE]
552
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100553 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200554 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100555 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200556 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100557
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100558 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100559 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100560 call delete(file)
561 endfor
562endfunc
563
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100564" Using :blast and :ball for many events caused a crash, because b_nwindows was
565" not incremented correctly.
566func Test_autocmd_blast_badd()
567 let content =<< trim [CODE]
568 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
569 edit foo1
570 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
571 edit foo2
572 call writefile(['OK'], 'Xerrors')
573 qall
574 [CODE]
575
576 call writefile(content, 'XblastBall')
577 call system(GetVimCommand() .. ' --clean -S XblastBall')
578 call assert_match('OK', readfile('Xerrors')->join())
579
580 call delete('XblastBall')
581 call delete('Xerrors')
582endfunc
583
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100584" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200585func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100586 tabnew
587 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100588 mksession!
589
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200590 let content =<< trim [CODE]
591 set nocp noswapfile
592 function! DeleteInactiveBufs()
593 tabfirst
594 let tabblist = []
595 for i in range(1, tabpagenr(''$''))
596 call extend(tabblist, tabpagebuflist(i))
597 endfor
598 for b in range(1, bufnr(''$''))
599 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
600 exec ''bwipeout '' . b
601 endif
602 endfor
603 echomsg "SessionLoadPost DONE"
604 endfunction
605 au SessionLoadPost * call DeleteInactiveBufs()
606
607 func WriteErrors()
608 call writefile([execute("messages")], "Xerrors")
609 endfunc
610 au VimLeave * call WriteErrors()
611 [CODE]
612
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100613 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200614 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100615 let errors = join(readfile('Xerrors'))
616 " This probably only ever matches on unix.
617 call assert_notmatch('Caught deadly signal SEGV', errors)
618 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100619
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100620 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100621 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100622 call delete(file)
623 endfor
624endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200625
626func Test_empty_doau()
627 doau \|
628endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200629
630func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200631 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200632 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200633 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
634 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 +0200635 let g:opt = [expected, actual]
636 "call assert_equal(expected, actual)
637endfunc
638
639func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200640 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200641
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200642 badd test_autocmd.vim
643
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200644 call test_override('starting', 1)
645 set nocp
646 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
647
648 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100649 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200650 set nu
651 call assert_equal([], g:options)
652 call assert_equal(g:opt[0], g:opt[1])
653
654 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100655 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200656 setlocal nonu
657 call assert_equal([], g:options)
658 call assert_equal(g:opt[0], g:opt[1])
659
660 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100661 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200662 setglobal nonu
663 call assert_equal([], g:options)
664 call assert_equal(g:opt[0], g:opt[1])
665
666 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100667 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200668 setlocal ai
669 call assert_equal([], g:options)
670 call assert_equal(g:opt[0], g:opt[1])
671
672 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100673 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200674 setglobal ai
675 call assert_equal([], g:options)
676 call assert_equal(g:opt[0], g:opt[1])
677
678 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100679 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200680 set ai!
681 call assert_equal([], g:options)
682 call assert_equal(g:opt[0], g:opt[1])
683
684 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100685 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200686 noa setlocal ai
687 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200688 set ai!
689 call assert_equal([], g:options)
690 call assert_equal(g:opt[0], g:opt[1])
691
692 " Should not print anything, use :noa
693 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100694 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200695 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200696 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200697 call assert_equal(g:opt[0], g:opt[1])
698
699 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100700 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200701 set list nu
702 call assert_equal([], g:options)
703 call assert_equal(g:opt[0], g:opt[1])
704
705 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100706 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200707 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200708 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 +0200709 call assert_equal(g:opt[0], g:opt[1])
710
711 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100712 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200713 setlocal acd
714 call assert_equal([], g:options)
715 call assert_equal(g:opt[0], g:opt[1])
716
717 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100718 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200719 set ar
720 call assert_equal([], g:options)
721 call assert_equal(g:opt[0], g:opt[1])
722
723 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100724 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200725 setlocal ar
726 call assert_equal([], g:options)
727 call assert_equal(g:opt[0], g:opt[1])
728
729 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100730 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200731 setglobal invar
732 call assert_equal([], g:options)
733 call assert_equal(g:opt[0], g:opt[1])
734
735 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100736 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
737 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200738 call assert_equal([], g:options)
739 call assert_equal(g:opt[0], g:opt[1])
740
741 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100742 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200743 " try twice, first time, shouldn't trigger because option name is invalid,
744 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200745 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200746 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200747 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200748 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200749 call assert_equal([], g:options)
750 call assert_equal(g:opt[0], g:opt[1])
751
752 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100753 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200754 call setwinvar(0, '&number', 1)
755 call assert_equal([], g:options)
756 call assert_equal(g:opt[0], g:opt[1])
757
758 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100759 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200760 setlocal key=blah
761 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200762 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200763 call assert_equal(g:opt[0], g:opt[1])
764
Bram Moolenaard7c96872019-06-15 17:12:48 +0200765
766 " 18a: Setting string global option"
767 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100768 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200769 set backupext=foo
770 call assert_equal([], g:options)
771 call assert_equal(g:opt[0], g:opt[1])
772
773 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100774 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200775 set backupext&
776 call assert_equal([], g:options)
777 call assert_equal(g:opt[0], g:opt[1])
778
779 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100780 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200781 setglobal backupext=bar
782 call assert_equal([], g:options)
783 call assert_equal(g:opt[0], g:opt[1])
784
785 " 18d: Setting local string global option"
786 " As this is a global option this sets the global value even though
787 " :setlocal is used!
788 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100789 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200790 setlocal backupext=baz
791 call assert_equal([], g:options)
792 call assert_equal(g:opt[0], g:opt[1])
793
794 " 18e: Setting again string global option"
795 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
796 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100797 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200798 set backupext=fuu
799 call assert_equal([], g:options)
800 call assert_equal(g:opt[0], g:opt[1])
801
802
zeertzjqb811de52021-10-21 10:50:44 +0100803 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200804 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100805 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200806 set tags=tagpath
807 call assert_equal([], g:options)
808 call assert_equal(g:opt[0], g:opt[1])
809
zeertzjqb811de52021-10-21 10:50:44 +0100810 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100811 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200812 set tags&
813 call assert_equal([], g:options)
814 call assert_equal(g:opt[0], g:opt[1])
815
zeertzjqb811de52021-10-21 10:50:44 +0100816 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100817 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200818 setglobal tags=tagpath1
819 call assert_equal([], g:options)
820 call assert_equal(g:opt[0], g:opt[1])
821
zeertzjqb811de52021-10-21 10:50:44 +0100822 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100823 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200824 setlocal tags=tagpath2
825 call assert_equal([], g:options)
826 call assert_equal(g:opt[0], g:opt[1])
827
zeertzjqb811de52021-10-21 10:50:44 +0100828 " 19e: Setting again string global-local (to buffer) option"
829 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200830 " but the old local value for all other kinds of options.
831 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
832 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100833 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200834 set tags=tagpath
835 call assert_equal([], g:options)
836 call assert_equal(g:opt[0], g:opt[1])
837
zeertzjqb811de52021-10-21 10:50:44 +0100838 " 19f: Setting string global-local (to buffer) option to an empty string"
839 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200840 " but the old local value for all other kinds of options.
841 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
842 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100843 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200844 set tags=tagpath
845 call assert_equal([], g:options)
846 call assert_equal(g:opt[0], g:opt[1])
847
848
849 " 20a: Setting string local (to buffer) option"
850 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100851 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200852 set spelllang=elvish,klingon
853 call assert_equal([], g:options)
854 call assert_equal(g:opt[0], g:opt[1])
855
856 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100857 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200858 set spelllang&
859 call assert_equal([], g:options)
860 call assert_equal(g:opt[0], g:opt[1])
861
862 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100863 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200864 setglobal spelllang=elvish
865 call assert_equal([], g:options)
866 call assert_equal(g:opt[0], g:opt[1])
867
868 " 20d: Setting local string local (to buffer) option"
869 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100870 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200871 setlocal spelllang=klingon
872 call assert_equal([], g:options)
873 call assert_equal(g:opt[0], g:opt[1])
874
875 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +0100876 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200877 " but the old local value for all other kinds of options.
878 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
879 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100880 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200881 set spelllang=foo
882 call assert_equal([], g:options)
883 call assert_equal(g:opt[0], g:opt[1])
884
885
zeertzjqb811de52021-10-21 10:50:44 +0100886 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200887 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100888 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200889 set statusline=foo
890 call assert_equal([], g:options)
891 call assert_equal(g:opt[0], g:opt[1])
892
zeertzjqb811de52021-10-21 10:50:44 +0100893 " 21b: Resetting string global-local (to window) option"
894 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200895 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100896 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200897 set statusline&
898 call assert_equal([], g:options)
899 call assert_equal(g:opt[0], g:opt[1])
900
zeertzjqb811de52021-10-21 10:50:44 +0100901 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100902 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200903 setglobal statusline=bar
904 call assert_equal([], g:options)
905 call assert_equal(g:opt[0], g:opt[1])
906
zeertzjqb811de52021-10-21 10:50:44 +0100907 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200908 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100909 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200910 setlocal statusline=baz
911 call assert_equal([], g:options)
912 call assert_equal(g:opt[0], g:opt[1])
913
zeertzjqb811de52021-10-21 10:50:44 +0100914 " 21e: Setting again string global-local (to window) option"
915 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200916 " but the old local value for all other kinds of options.
917 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
918 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100919 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200920 set statusline=foo
921 call assert_equal([], g:options)
922 call assert_equal(g:opt[0], g:opt[1])
923
924
925 " 22a: Setting string local (to window) option"
926 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100927 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200928 set foldignore=fo
929 call assert_equal([], g:options)
930 call assert_equal(g:opt[0], g:opt[1])
931
932 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100933 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200934 set foldignore&
935 call assert_equal([], g:options)
936 call assert_equal(g:opt[0], g:opt[1])
937
938 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100939 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200940 setglobal foldignore=bar
941 call assert_equal([], g:options)
942 call assert_equal(g:opt[0], g:opt[1])
943
944 " 22d: Setting local string local (to window) option"
945 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100946 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200947 setlocal foldignore=baz
948 call assert_equal([], g:options)
949 call assert_equal(g:opt[0], g:opt[1])
950
951 " 22e: Setting again string local (to window) option"
952 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
953 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100954 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200955 set foldignore=fo
956 call assert_equal([], g:options)
957 call assert_equal(g:opt[0], g:opt[1])
958
959
zeertzjqb811de52021-10-21 10:50:44 +0100960 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200961 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
962 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100963 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200964 setglobal cmdheight=2
965 call assert_equal([], g:options)
966 call assert_equal(g:opt[0], g:opt[1])
967
968 " 23b: Setting local number global option"
969 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
970 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100971 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200972 setlocal cmdheight=2
973 call assert_equal([], g:options)
974 call assert_equal(g:opt[0], g:opt[1])
975
976 " 23c: Setting again number global option"
977 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
978 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100979 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200980 set cmdheight=2
981 call assert_equal([], g:options)
982 call assert_equal(g:opt[0], g:opt[1])
983
984 " 23d: Setting again number global option"
985 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100986 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200987 set cmdheight=2
988 call assert_equal([], g:options)
989 call assert_equal(g:opt[0], g:opt[1])
990
991
992 " 24a: Setting global number global-local (to buffer) option"
993 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
994 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100995 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200996 setglobal undolevels=2
997 call assert_equal([], g:options)
998 call assert_equal(g:opt[0], g:opt[1])
999
1000 " 24b: Setting local number global-local (to buffer) option"
1001 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1002 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001003 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001004 setlocal undolevels=2
1005 call assert_equal([], g:options)
1006 call assert_equal(g:opt[0], g:opt[1])
1007
1008 " 24c: Setting again number global-local (to buffer) option"
1009 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1010 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001011 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001012 set undolevels=2
1013 call assert_equal([], g:options)
1014 call assert_equal(g:opt[0], g:opt[1])
1015
1016 " 24d: Setting again global number global-local (to buffer) option"
1017 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001018 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001019 set undolevels=2
1020 call assert_equal([], g:options)
1021 call assert_equal(g:opt[0], g:opt[1])
1022
1023
1024 " 25a: Setting global number local (to buffer) option"
1025 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1026 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001027 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001028 setglobal wrapmargin=2
1029 call assert_equal([], g:options)
1030 call assert_equal(g:opt[0], g:opt[1])
1031
1032 " 25b: Setting local number local (to buffer) option"
1033 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1034 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001035 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001036 setlocal wrapmargin=2
1037 call assert_equal([], g:options)
1038 call assert_equal(g:opt[0], g:opt[1])
1039
1040 " 25c: Setting again number local (to buffer) option"
1041 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1042 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001043 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001044 set wrapmargin=2
1045 call assert_equal([], g:options)
1046 call assert_equal(g:opt[0], g:opt[1])
1047
1048 " 25d: Setting again global number local (to buffer) option"
1049 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001050 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001051 set wrapmargin=2
1052 call assert_equal([], g:options)
1053 call assert_equal(g:opt[0], g:opt[1])
1054
1055
1056 " 26: Setting number global-local (to window) option.
1057 " Such option does currently not exist.
1058
1059
1060 " 27a: Setting global number local (to window) option"
1061 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1062 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001063 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001064 setglobal foldcolumn=2
1065 call assert_equal([], g:options)
1066 call assert_equal(g:opt[0], g:opt[1])
1067
1068 " 27b: Setting local number local (to window) option"
1069 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1070 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001071 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001072 setlocal foldcolumn=2
1073 call assert_equal([], g:options)
1074 call assert_equal(g:opt[0], g:opt[1])
1075
1076 " 27c: Setting again number local (to window) option"
1077 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1078 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001079 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001080 set foldcolumn=2
1081 call assert_equal([], g:options)
1082 call assert_equal(g:opt[0], g:opt[1])
1083
zeertzjqb811de52021-10-21 10:50:44 +01001084 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001085 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001086 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001087 set foldcolumn=2
1088 call assert_equal([], g:options)
1089 call assert_equal(g:opt[0], g:opt[1])
1090
1091
1092 " 28a: Setting global boolean global option"
1093 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1094 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001095 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001096 setglobal nowrapscan
1097 call assert_equal([], g:options)
1098 call assert_equal(g:opt[0], g:opt[1])
1099
1100 " 28b: Setting local boolean global option"
1101 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1102 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001103 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001104 setlocal nowrapscan
1105 call assert_equal([], g:options)
1106 call assert_equal(g:opt[0], g:opt[1])
1107
1108 " 28c: Setting again boolean global option"
1109 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1110 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001111 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001112 set nowrapscan
1113 call assert_equal([], g:options)
1114 call assert_equal(g:opt[0], g:opt[1])
1115
1116 " 28d: Setting again global boolean global option"
1117 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001118 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001119 set wrapscan
1120 call assert_equal([], g:options)
1121 call assert_equal(g:opt[0], g:opt[1])
1122
1123
1124 " 29a: Setting global boolean global-local (to buffer) option"
1125 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1126 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001127 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001128 setglobal autoread
1129 call assert_equal([], g:options)
1130 call assert_equal(g:opt[0], g:opt[1])
1131
1132 " 29b: Setting local boolean global-local (to buffer) option"
1133 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1134 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001135 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001136 setlocal noautoread
1137 call assert_equal([], g:options)
1138 call assert_equal(g:opt[0], g:opt[1])
1139
1140 " 29c: Setting again boolean global-local (to buffer) option"
1141 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1142 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001143 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001144 set autoread
1145 call assert_equal([], g:options)
1146 call assert_equal(g:opt[0], g:opt[1])
1147
1148 " 29d: Setting again global boolean global-local (to buffer) option"
1149 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001150 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001151 set autoread
1152 call assert_equal([], g:options)
1153 call assert_equal(g:opt[0], g:opt[1])
1154
1155
1156 " 30a: Setting global boolean local (to buffer) option"
1157 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1158 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001159 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001160 setglobal cindent
1161 call assert_equal([], g:options)
1162 call assert_equal(g:opt[0], g:opt[1])
1163
1164 " 30b: Setting local boolean local (to buffer) option"
1165 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1166 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001167 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001168 setlocal nocindent
1169 call assert_equal([], g:options)
1170 call assert_equal(g:opt[0], g:opt[1])
1171
1172 " 30c: Setting again boolean local (to buffer) option"
1173 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1174 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001175 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001176 set cindent
1177 call assert_equal([], g:options)
1178 call assert_equal(g:opt[0], g:opt[1])
1179
1180 " 30d: Setting again global boolean local (to buffer) option"
1181 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001182 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001183 set cindent
1184 call assert_equal([], g:options)
1185 call assert_equal(g:opt[0], g:opt[1])
1186
1187
1188 " 31: Setting boolean global-local (to window) option
1189 " Currently no such option exists.
1190
1191
1192 " 32a: Setting global boolean local (to window) option"
1193 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1194 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001195 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001196 setglobal cursorcolumn
1197 call assert_equal([], g:options)
1198 call assert_equal(g:opt[0], g:opt[1])
1199
1200 " 32b: Setting local boolean local (to window) option"
1201 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1202 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001203 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001204 setlocal nocursorcolumn
1205 call assert_equal([], g:options)
1206 call assert_equal(g:opt[0], g:opt[1])
1207
1208 " 32c: Setting again boolean local (to window) option"
1209 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1210 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001211 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001212 set cursorcolumn
1213 call assert_equal([], g:options)
1214 call assert_equal(g:opt[0], g:opt[1])
1215
1216 " 32d: Setting again global boolean local (to window) option"
1217 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001218 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001219 set cursorcolumn
1220 call assert_equal([], g:options)
1221 call assert_equal(g:opt[0], g:opt[1])
1222
1223
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001224 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001225 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001226 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001227 set backspace=2
1228 call assert_equal([], g:options)
1229 call assert_equal(g:opt[0], g:opt[1])
1230
1231
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001232 " Cleanup
1233 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001234 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001235 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 +02001236 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001237 endfor
1238 call test_override('starting', 0)
1239 delfunc! AutoCommandOptionSet
1240endfunc
1241
1242func Test_OptionSet_diffmode()
1243 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001244 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001245 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001246 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001247
1248 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1249 call assert_equal(0, &l:cul)
1250 diffthis
1251 call assert_equal(1, &l:cul)
1252
1253 vnew
1254 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1255 call assert_equal(0, &l:cul)
1256 diffthis
1257 call assert_equal(1, &l:cul)
1258
1259 diffoff
1260 call assert_equal(0, &l:cul)
1261 call assert_equal(1, getwinvar(2, '&l:cul'))
1262 bw!
1263
1264 call assert_equal(1, &l:cul)
1265 diffoff!
1266 call assert_equal(0, &l:cul)
1267 call assert_equal(0, getwinvar(1, '&l:cul'))
1268 bw!
1269
1270 " Cleanup
1271 au! OptionSet
1272 call test_override('starting', 0)
1273endfunc
1274
1275func Test_OptionSet_diffmode_close()
1276 call test_override('starting', 1)
1277 " 19: Try to close the current window when entering diff mode
1278 " should not segfault
1279 new
1280 au OptionSet diff close
1281
1282 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001283 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001284 call assert_equal(1, &diff)
1285 vnew
1286 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001287 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001288 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001289 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001290 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001291 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001292 bw!
1293
1294 " Cleanup
1295 au! OptionSet
1296 call test_override('starting', 0)
1297 "delfunc! AutoCommandOptionSet
1298endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001299
1300" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1301func Test_BufleaveWithDelete()
1302 new | edit Xfile1
1303
1304 augroup test_bufleavewithdelete
1305 autocmd!
1306 autocmd BufLeave Xfile1 bwipe Xfile2
1307 augroup END
1308
1309 call assert_fails('edit Xfile2', 'E143:')
1310 call assert_equal('Xfile1', bufname('%'))
1311
1312 autocmd! test_bufleavewithdelete BufLeave Xfile1
1313 augroup! test_bufleavewithdelete
1314
1315 new
1316 bwipe! Xfile1
1317endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001318
1319" Test for autocommand that changes the buffer list, when doing ":ball".
1320func Test_Acmd_BufAll()
1321 enew!
1322 %bwipe!
1323 call writefile(['Test file Xxx1'], 'Xxx1')
1324 call writefile(['Test file Xxx2'], 'Xxx2')
1325 call writefile(['Test file Xxx3'], 'Xxx3')
1326
1327 " Add three files to the buffer list
1328 split Xxx1
1329 close
1330 split Xxx2
1331 close
1332 split Xxx3
1333 close
1334
1335 " Wipe the buffer when the buffer is opened
1336 au BufReadPost Xxx2 bwipe
1337
1338 call append(0, 'Test file Xxx4')
1339 ball
1340
1341 call assert_equal(2, winnr('$'))
1342 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1343 wincmd t
1344
1345 au! BufReadPost
1346 %bwipe!
1347 call delete('Xxx1')
1348 call delete('Xxx2')
1349 call delete('Xxx3')
1350 enew! | only
1351endfunc
1352
1353" Test for autocommand that changes current buffer on BufEnter event.
1354" Check if modelines are interpreted for the correct buffer.
1355func Test_Acmd_BufEnter()
1356 %bwipe!
1357 call writefile(['start of test file Xxx1',
1358 \ "\<Tab>this is a test",
1359 \ 'end of test file Xxx1'], 'Xxx1')
1360 call writefile(['start of test file Xxx2',
1361 \ 'vim: set noai :',
1362 \ "\<Tab>this is a test",
1363 \ 'end of test file Xxx2'], 'Xxx2')
1364
1365 au BufEnter Xxx2 brew
1366 set ai modeline modelines=3
1367 edit Xxx1
1368 " edit Xxx2, autocmd will do :brew
1369 edit Xxx2
1370 exe "normal G?this is a\<CR>"
1371 " Append text with autoindent to this file
1372 normal othis should be auto-indented
1373 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1374 call assert_equal(3, line('.'))
1375 " Remove autocmd and edit Xxx2 again
1376 au! BufEnter Xxx2
1377 buf! Xxx2
1378 exe "normal G?this is a\<CR>"
1379 " append text without autoindent to Xxx
1380 normal othis should be in column 1
1381 call assert_equal("this should be in column 1", getline('.'))
1382 call assert_equal(4, line('.'))
1383
1384 %bwipe!
1385 call delete('Xxx1')
1386 call delete('Xxx2')
1387 set ai&vim modeline&vim modelines&vim
1388endfunc
1389
1390" Test for issue #57
1391" do not move cursor on <c-o> when autoindent is set
1392func Test_ai_CTRL_O()
1393 enew!
1394 set ai
1395 let save_fo = &fo
1396 set fo+=r
1397 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1398 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1399 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1400
1401 set ai&vim
1402 let &fo = save_fo
1403 enew!
1404endfunc
1405
1406" Test for autocommand that deletes the current buffer on BufLeave event.
1407" Also test deleting the last buffer, should give a new, empty buffer.
1408func Test_BufLeave_Wipe()
1409 %bwipe!
1410 let content = ['start of test file Xxx',
1411 \ 'this is a test',
1412 \ 'end of test file Xxx']
1413 call writefile(content, 'Xxx1')
1414 call writefile(content, 'Xxx2')
1415
1416 au BufLeave Xxx2 bwipe
1417 edit Xxx1
1418 split Xxx2
1419 " delete buffer Xxx2, we should be back to Xxx1
1420 bwipe
1421 call assert_equal('Xxx1', bufname('%'))
1422 call assert_equal(1, winnr('$'))
1423
1424 " Create an alternate buffer
1425 %write! test.out
1426 call assert_equal('test.out', bufname('#'))
1427 " delete alternate buffer
1428 bwipe test.out
1429 call assert_equal('Xxx1', bufname('%'))
1430 call assert_equal('', bufname('#'))
1431
1432 au BufLeave Xxx1 bwipe
1433 " delete current buffer, get an empty one
1434 bwipe!
1435 call assert_equal(1, line('$'))
1436 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001437 let g:bufinfo = getbufinfo()
1438 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001439
1440 call delete('Xxx1')
1441 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001442 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001443 %bwipe
1444 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001445
1446 " check that bufinfo doesn't contain a pointer to freed memory
1447 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001448endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001449
1450func Test_QuitPre()
1451 edit Xfoo
1452 let winid = win_getid(winnr())
1453 split Xbar
1454 au! QuitPre * let g:afile = expand('<afile>')
1455 " Close the other window, <afile> should be correct.
1456 exe win_id2win(winid) . 'q'
1457 call assert_equal('Xfoo', g:afile)
1458
1459 unlet g:afile
1460 bwipe Xfoo
1461 bwipe Xbar
1462endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001463
1464func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001465 au! CmdlineChanged : let g:text = getcmdline()
1466 let g:text = 0
1467 call feedkeys(":echom 'hello'\<CR>", 'xt')
1468 call assert_equal("echom 'hello'", g:text)
1469 au! CmdlineChanged
1470
1471 au! CmdlineChanged : let g:entered = expand('<afile>')
1472 let g:entered = 0
1473 call feedkeys(":echom 'hello'\<CR>", 'xt')
1474 call assert_equal(':', g:entered)
1475 au! CmdlineChanged
1476
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001477 au! CmdlineEnter : let g:entered = expand('<afile>')
1478 au! CmdlineLeave : let g:left = expand('<afile>')
1479 let g:entered = 0
1480 let g:left = 0
1481 call feedkeys(":echo 'hello'\<CR>", 'xt')
1482 call assert_equal(':', g:entered)
1483 call assert_equal(':', g:left)
1484 au! CmdlineEnter
1485 au! CmdlineLeave
1486
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001487 let save_shellslash = &shellslash
1488 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001489 au! CmdlineEnter / let g:entered = expand('<afile>')
1490 au! CmdlineLeave / let g:left = expand('<afile>')
1491 let g:entered = 0
1492 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001493 new
1494 call setline(1, 'hello')
1495 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001496 call assert_equal('/', g:entered)
1497 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001498 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001499 au! CmdlineEnter
1500 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001501 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001502endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001503
1504" Test for BufWritePre autocommand that deletes or unloads the buffer.
1505func Test_BufWritePre()
1506 %bwipe
1507 au BufWritePre Xxx1 bunload
1508 au BufWritePre Xxx2 bwipe
1509
1510 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1511 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1512
1513 edit Xtest
1514 e! Xxx2
1515 bdel Xtest
1516 e Xxx1
1517 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001518 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001519 call assert_equal('Xxx2', bufname('%'))
1520 edit Xtest
1521 e! Xxx2
1522 bwipe Xtest
1523 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001524 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001525 call assert_equal('Xxx1', bufname('%'))
1526 au! BufWritePre
1527 call delete('Xxx1')
1528 call delete('Xxx2')
1529endfunc
1530
1531" Test for BufUnload autocommand that unloads all the other buffers
1532func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001533 let g:test_is_flaky = 1
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001534 call writefile(['Test file Xxx1'], 'Xxx1')"
1535 call writefile(['Test file Xxx2'], 'Xxx2')"
1536
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001537 let content =<< trim [CODE]
1538 func UnloadAllBufs()
1539 let i = 1
1540 while i <= bufnr('$')
1541 if i != bufnr('%') && bufloaded(i)
1542 exe i . 'bunload'
1543 endif
1544 let i += 1
1545 endwhile
1546 endfunc
1547 au BufUnload * call UnloadAllBufs()
1548 au VimLeave * call writefile(['Test Finished'], 'Xout')
1549 edit Xxx1
1550 split Xxx2
1551 q
1552 [CODE]
1553
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001554 call writefile(content, 'Xtest')
1555
1556 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001557 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001558 call assert_true(filereadable('Xout'))
1559
1560 call delete('Xxx1')
1561 call delete('Xxx2')
1562 call delete('Xtest')
1563 call delete('Xout')
1564endfunc
1565
1566" Some tests for buffer-local autocommands
1567func Test_buflocal_autocmd()
1568 let g:bname = ''
1569 edit xx
1570 au BufLeave <buffer> let g:bname = expand("%")
1571 " here, autocommand for xx should trigger.
1572 " but autocommand shall not apply to buffer named <buffer>.
1573 edit somefile
1574 call assert_equal('xx', g:bname)
1575 let g:bname = ''
1576 " here, autocommand shall be auto-deleted
1577 bwipe xx
1578 " autocmd should not trigger
1579 edit xx
1580 call assert_equal('', g:bname)
1581 " autocmd should not trigger
1582 edit somefile
1583 call assert_equal('', g:bname)
1584 enew
1585 unlet g:bname
1586endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001587
1588" Test for "*Cmd" autocommands
1589func Test_Cmd_Autocmds()
1590 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1591
1592 enew!
1593 au BufReadCmd XtestA 0r Xxx|$del
1594 edit XtestA " will read text of Xxd instead
1595 call assert_equal('start of Xxx', getline(1))
1596
1597 au BufWriteCmd XtestA call append(line("$"), "write")
1598 write " will append a line to the file
1599 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001600 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001601 call assert_equal('write', getline(4))
1602
1603 " now we have:
1604 " 1 start of Xxx
1605 " 2 abc2
1606 " 3 end of Xxx
1607 " 4 write
1608
1609 au FileReadCmd XtestB '[r Xxx
1610 2r XtestB " will read Xxx below line 2 instead
1611 call assert_equal('start of Xxx', getline(3))
1612
1613 " now we have:
1614 " 1 start of Xxx
1615 " 2 abc2
1616 " 3 start of Xxx
1617 " 4 abc2
1618 " 5 end of Xxx
1619 " 6 end of Xxx
1620 " 7 write
1621
1622 au FileWriteCmd XtestC '[,']copy $
1623 normal 4GA1
1624 4,5w XtestC " will copy lines 4 and 5 to the end
1625 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001626 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001627 call assert_equal("end of Xxx", getline(9))
1628
1629 " now we have:
1630 " 1 start of Xxx
1631 " 2 abc2
1632 " 3 start of Xxx
1633 " 4 abc21
1634 " 5 end of Xxx
1635 " 6 end of Xxx
1636 " 7 write
1637 " 8 abc21
1638 " 9 end of Xxx
1639
1640 let g:lines = []
1641 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1642 w >>XtestD " will add lines to 'lines'
1643 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001644 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001645 call assert_equal(9, line('$'))
1646 call assert_equal('end of Xxx', getline('$'))
1647
1648 au BufReadCmd XtestE 0r Xxx|$del
1649 sp XtestE " split window with test.out
1650 call assert_equal('end of Xxx', getline(3))
1651
1652 let g:lines = []
1653 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1654 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1655 wall " will write other window to 'lines'
1656 call assert_equal(4, len(g:lines), g:lines)
1657 call assert_equal('asdf', g:lines[2])
1658
1659 au! BufReadCmd
1660 au! BufWriteCmd
1661 au! FileReadCmd
1662 au! FileWriteCmd
1663 au! FileAppendCmd
1664 %bwipe!
1665 call delete('Xxx')
1666 enew!
1667endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001668
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001669func s:ReadFile()
1670 setl noswapfile nomodified
1671 let filename = resolve(expand("<afile>:p"))
1672 execute 'read' fnameescape(filename)
1673 1d_
1674 exe 'file' fnameescape(filename)
1675 setl buftype=acwrite
1676endfunc
1677
1678func s:WriteFile()
1679 let filename = resolve(expand("<afile>:p"))
1680 setl buftype=
1681 noautocmd execute 'write' fnameescape(filename)
1682 setl buftype=acwrite
1683 setl nomodified
1684endfunc
1685
1686func Test_BufReadCmd()
1687 autocmd BufReadCmd *.test call s:ReadFile()
1688 autocmd BufWriteCmd *.test call s:WriteFile()
1689
1690 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1691 edit Xcmd.test
1692 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1693 normal! Gofour
1694 write
1695 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1696
1697 bwipe!
1698 call delete('Xcmd.test')
1699 au! BufReadCmd
1700 au! BufWriteCmd
1701endfunc
1702
Bram Moolenaaraace2152017-11-05 16:23:10 +01001703func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001704 exe a:start .. 'mark ['
1705 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001706endfunc
1707
1708" Verify the effects of autocmds on '[ and ']
1709func Test_change_mark_in_autocmds()
1710 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001711 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001712
1713 call SetChangeMarks(2, 3)
1714 write
1715 call assert_equal([1, 4], [line("'["), line("']")])
1716
1717 call SetChangeMarks(2, 3)
1718 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1719 write
1720 au! BufWritePre
1721
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001722 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001723 write XtestFilter
1724 write >> XtestFilter
1725
1726 call SetChangeMarks(2, 3)
1727 " Marks are set to the entire range of the write
1728 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1729 " '[ is adjusted to just before the line that will receive the filtered
1730 " data
1731 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1732 " The filtered data is read into the buffer, and the source lines are
1733 " still present, so the range is after the source lines
1734 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1735 %!cat XtestFilter
1736 " After the filtered data is read, the original lines are deleted
1737 call assert_equal([1, 8], [line("'["), line("']")])
1738 au! FilterWritePre,FilterReadPre,FilterReadPost
1739 undo
1740
1741 call SetChangeMarks(1, 4)
1742 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1743 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1744 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1745 2,3!cat XtestFilter
1746 call assert_equal([2, 9], [line("'["), line("']")])
1747 au! FilterWritePre,FilterReadPre,FilterReadPost
1748 undo
1749
1750 call delete('XtestFilter')
1751 endif
1752
1753 call SetChangeMarks(1, 4)
1754 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1755 2,3write Xtest2
1756 au! FileWritePre
1757
1758 call SetChangeMarks(2, 3)
1759 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1760 write >> Xtest2
1761 au! FileAppendPre
1762
1763 call SetChangeMarks(1, 4)
1764 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1765 2,3write >> Xtest2
1766 au! FileAppendPre
1767
1768 call SetChangeMarks(1, 1)
1769 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1770 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1771 3read Xtest2
1772 au! FileReadPre,FileReadPost
1773 undo
1774
1775 call SetChangeMarks(4, 4)
1776 " When the line is 0, it's adjusted to 1
1777 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1778 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1779 0read Xtest2
1780 au! FileReadPre,FileReadPost
1781 undo
1782
1783 call SetChangeMarks(4, 4)
1784 " When the line is 0, it's adjusted to 1
1785 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1786 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1787 1read Xtest2
1788 au! FileReadPre,FileReadPost
1789 undo
1790
1791 bwipe!
1792 call delete('Xtest')
1793 call delete('Xtest2')
1794endfunc
1795
1796func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01001797 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01001798
1799 enew!
1800 call setline(1, ['a', 'b', 'c', 'd'])
1801
1802 let shelltemp = &shelltemp
1803 set shelltemp
1804
1805 let g:filter_au = 0
1806 au FilterWritePre * let g:filter_au += 1
1807 au FilterReadPre * let g:filter_au += 1
1808 au FilterReadPost * let g:filter_au += 1
1809 %!cat
1810 call assert_equal(3, g:filter_au)
1811
1812 if has('filterpipe')
1813 set noshelltemp
1814
1815 let g:filter_au = 0
1816 au FilterWritePre * let g:filter_au += 1
1817 au FilterReadPre * let g:filter_au += 1
1818 au FilterReadPost * let g:filter_au += 1
1819 %!cat
1820 call assert_equal(0, g:filter_au)
1821 endif
1822
1823 au! FilterWritePre,FilterReadPre,FilterReadPost
1824 let &shelltemp = shelltemp
1825 bwipe!
1826endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001827
1828func Test_TextYankPost()
1829 enew!
1830 call setline(1, ['foo'])
1831
1832 let g:event = []
1833 au TextYankPost * let g:event = copy(v:event)
1834
1835 call assert_equal({}, v:event)
1836 call assert_fails('let v:event = {}', 'E46:')
1837 call assert_fails('let v:event.mykey = 0', 'E742:')
1838
1839 norm "ayiw
1840 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001841 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001842 \g:event)
1843 norm y_
1844 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001845 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false},
1846 \g:event)
1847 norm Vy
1848 call assert_equal(
1849 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001850 \g:event)
1851 call feedkeys("\<C-V>y", 'x')
1852 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001853 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001854 \g:event)
1855 norm "xciwbar
1856 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001857 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001858 \g:event)
1859 norm "bdiw
1860 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001861 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001862 \g:event)
1863
1864 call assert_equal({}, v:event)
1865
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001866 if has('clipboard_working') && !has('gui_running')
1867 " Test that when the visual selection is automatically copied to clipboard
1868 " register a TextYankPost is emitted
1869 call setline(1, ['foobar'])
1870
1871 let @* = ''
1872 set clipboard=autoselect
1873 exe "norm! ggviw\<Esc>"
1874 call assert_equal(
1875 \{'regcontents': ['foobar'], 'regname': '*', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1876 \g:event)
1877
1878 let @+ = ''
1879 set clipboard=autoselectplus
1880 exe "norm! ggviw\<Esc>"
1881 call assert_equal(
1882 \{'regcontents': ['foobar'], 'regname': '+', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1883 \g:event)
1884
1885 set clipboard&vim
1886 endif
1887
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001888 au! TextYankPost
1889 unlet g:event
1890 bwipe!
1891endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001892
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01001893func Test_autocommand_all_events()
1894 call assert_fails('au * * bwipe', 'E1155:')
1895 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001896endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001897
1898function s:Before_test_dirchanged()
1899 augroup test_dirchanged
1900 autocmd!
1901 augroup END
1902 let s:li = []
1903 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001904 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001905 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001906 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001907 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001908endfunc
1909
1910function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001911 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001912 call delete(s:dir_foo, 'd')
1913 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001914 augroup test_dirchanged
1915 autocmd!
1916 augroup END
1917endfunc
1918
1919function Test_dirchanged_global()
1920 call s:Before_test_dirchanged()
1921 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1922 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001923 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001924 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001925 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001926 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001927 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001928 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001929 call s:After_test_dirchanged()
1930endfunc
1931
1932function Test_dirchanged_local()
1933 call s:Before_test_dirchanged()
1934 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1935 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001936 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001937 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001938 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001939 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001940 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001941 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001942 call s:After_test_dirchanged()
1943endfunc
1944
1945function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001946 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001947 call s:Before_test_dirchanged()
1948 call test_autochdir()
1949 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1950 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1951 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001952 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001953 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001954 exe 'edit ' . s:dir_foo . '/Xfile'
1955 call assert_equal(s:dir_foo, getcwd())
1956 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001957 set noacd
1958 bwipe!
1959 call s:After_test_dirchanged()
1960endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001961
1962" Test TextChangedI and TextChangedP
1963func Test_ChangedP()
1964 new
1965 call setline(1, ['foo', 'bar', 'foobar'])
1966 call test_override("char_avail", 1)
1967 set complete=. completeopt=menuone
1968
1969 func! TextChangedAutocmd(char)
1970 let g:autocmd .= a:char
1971 endfunc
1972
Christian Brabandtdb3b4462021-10-16 11:58:55 +01001973 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01001974 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1975 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1976 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1977
1978 call cursor(3, 1)
1979 let g:autocmd = ''
1980 call feedkeys("o\<esc>", 'tnix')
1981 call assert_equal('I', g:autocmd)
1982
1983 let g:autocmd = ''
1984 call feedkeys("Sf", 'tnix')
1985 call assert_equal('II', g:autocmd)
1986
1987 let g:autocmd = ''
1988 call feedkeys("Sf\<C-N>", 'tnix')
1989 call assert_equal('IIP', g:autocmd)
1990
1991 let g:autocmd = ''
1992 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1993 call assert_equal('IIPP', g:autocmd)
1994
1995 let g:autocmd = ''
1996 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1997 call assert_equal('IIPPP', g:autocmd)
1998
1999 let g:autocmd = ''
2000 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2001 call assert_equal('IIPPPP', g:autocmd)
2002
2003 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2004 " TODO: how should it handle completeopt=noinsert,noselect?
2005
2006 " CleanUp
2007 call test_override("char_avail", 0)
2008 au! TextChanged
2009 au! TextChangedI
2010 au! TextChangedP
2011 delfu TextChangedAutocmd
2012 unlet! g:autocmd
2013 set complete&vim completeopt&vim
2014
2015 bw!
2016endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002017
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002018let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002019func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002020 if !g:setline_handled
2021 call setline(1, "(x)")
2022 let g:setline_handled = v:true
2023 endif
2024endfunc
2025
2026func Test_TextChangedI_with_setline()
2027 new
2028 call test_override('char_avail', 1)
2029 autocmd TextChangedI <buffer> call SetLineOne()
2030 call feedkeys("i(\<CR>\<Esc>", 'tx')
2031 call assert_equal('(', getline(1))
2032 call assert_equal('x)', getline(2))
2033 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002034 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002035 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002036
2037 call test_override('starting', 0)
2038 bwipe!
2039endfunc
2040
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002041func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002042 CheckFeature terminal
2043 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002044 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002045 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002046
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002047 " Prepare file for TextChanged event.
2048 call writefile([''], 'Xchanged.txt')
2049 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2050 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002051 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002052 " In ConPTY, there is additional character which is drawn up to the width of
2053 " the screen.
2054 if has('conpty')
2055 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2056 else
2057 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2058 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002059 " It's only adding autocmd, so that no event occurs.
2060 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2061 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002062 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002063 call assert_equal([''], readfile('Xchanged.txt'))
2064
2065 " clean up
2066 call delete('Xchanged.txt')
2067 bwipe!
2068endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002069
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002070func Test_autocmd_nested()
2071 let g:did_nested = 0
2072 augroup Testing
2073 au WinNew * edit somefile
2074 au BufNew * let g:did_nested = 1
2075 augroup END
2076 split
2077 call assert_equal(0, g:did_nested)
2078 close
2079 bwipe! somefile
2080
2081 " old nested argument still works
2082 augroup Testing
2083 au!
2084 au WinNew * nested edit somefile
2085 au BufNew * let g:did_nested = 1
2086 augroup END
2087 split
2088 call assert_equal(1, g:did_nested)
2089 close
2090 bwipe! somefile
2091
2092 " New ++nested argument works
2093 augroup Testing
2094 au!
2095 au WinNew * ++nested edit somefile
2096 au BufNew * let g:did_nested = 1
2097 augroup END
2098 split
2099 call assert_equal(1, g:did_nested)
2100 close
2101 bwipe! somefile
2102
2103 augroup Testing
2104 au!
2105 augroup END
2106
2107 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2108 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2109endfunc
2110
2111func Test_autocmd_once()
2112 " Without ++once WinNew triggers twice
2113 let g:did_split = 0
2114 augroup Testing
2115 au WinNew * let g:did_split += 1
2116 augroup END
2117 split
2118 split
2119 call assert_equal(2, g:did_split)
2120 call assert_true(exists('#WinNew'))
2121 close
2122 close
2123
2124 " With ++once WinNew triggers once
2125 let g:did_split = 0
2126 augroup Testing
2127 au!
2128 au WinNew * ++once let g:did_split += 1
2129 augroup END
2130 split
2131 split
2132 call assert_equal(1, g:did_split)
2133 call assert_false(exists('#WinNew'))
2134 close
2135 close
2136
2137 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2138endfunc
2139
Bram Moolenaara68e5952019-04-25 22:22:01 +02002140func Test_autocmd_bufreadpre()
2141 new
2142 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002143 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002144 w! XAutocmdBufReadPre.txt
2145 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002146 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002147 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002148 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002149 wincmd p
2150 let g:wsv1 = winsaveview()
2151 wincmd p
2152 let g:wsv2 = winsaveview()
2153 " triggers BufReadPre, should not move the cursor in either window
2154 " The topline may change one line in a large window.
2155 edit
2156 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2157 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2158 call assert_equal(2, b:bufreadpre)
2159 wincmd p
2160 call assert_equal(g:wsv1.topline, winsaveview().topline)
2161 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2162 call assert_equal(2, b:bufreadpre)
2163 " Now set the cursor position in an BufReadPre autocommand
2164 " (even though the position will be invalid, this should make Vim reset the
2165 " cursor position in the other window.
2166 wincmd p
2167 set cpo+=g
2168 " won't do anything, but try to set the cursor on an invalid lnum
2169 autocmd BufReadPre <buffer> :norm! 70gg
2170 " triggers BufReadPre, should not move the cursor in either window
2171 e
2172 call assert_equal(1, winsaveview().topline)
2173 call assert_equal(1, winsaveview().lnum)
2174 call assert_equal(3, b:bufreadpre)
2175 wincmd p
2176 call assert_equal(g:wsv1.topline, winsaveview().topline)
2177 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2178 call assert_equal(3, b:bufreadpre)
2179 close
2180 close
2181 call delete('XAutocmdBufReadPre.txt')
2182 set cpo-=g
2183endfunc
2184
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002185" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002186
2187" Tests for the following autocommands:
2188" - FileWritePre writing a compressed file
2189" - FileReadPost reading a compressed file
2190" - BufNewFile reading a file template
2191" - BufReadPre decompressing the file to be read
2192" - FilterReadPre substituting characters in the temp file
2193" - FilterReadPost substituting characters after filtering
2194" - FileReadPre set options for decompression
2195" - FileReadPost decompress the file
2196func Test_ReadWrite_Autocmds()
2197 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002198 CheckUnix
2199 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002200
2201 " Make $GZIP empty, "-v" would cause trouble.
2202 let $GZIP = ""
2203
2204 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2205 " being modified outside of Vim (noticed on Solaris).
2206 au FileChangedShell * echo 'caught FileChangedShell'
2207
2208 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2209 augroup Test1
2210 au!
2211 au FileWritePre *.gz '[,']!gzip
2212 au FileWritePost *.gz undo
2213 au FileReadPost *.gz '[,']!gzip -d
2214 augroup END
2215
2216 new
2217 set bin
2218 call append(0, [
2219 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2220 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2221 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2222 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2223 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2224 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2225 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2226 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2227 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2228 \ ])
2229 1,9write! Xtestfile.gz
2230 enew! | close
2231
2232 new
2233 " Read and decompress the testfile
2234 0read Xtestfile.gz
2235 call assert_equal([
2236 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2237 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2238 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2239 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2240 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2241 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2242 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2243 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2244 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2245 \ ], getline(1, 9))
2246 enew! | close
2247
2248 augroup Test1
2249 au!
2250 augroup END
2251
2252 " Test for the FileAppendPre and FileAppendPost autocmds
2253 augroup Test2
2254 au!
2255 au BufNewFile *.c read Xtest.c
2256 au FileAppendPre *.out '[,']s/new/NEW/
2257 au FileAppendPost *.out !cat Xtest.c >> test.out
2258 augroup END
2259
2260 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2261 new foo.c " should load Xtest.c
2262 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2263 w! >> test.out " append it to the output file
2264
2265 let contents = readfile('test.out')
2266 call assert_equal(' * Here is a NEW .c file', contents[2])
2267 call assert_equal(' * Here is a new .c file', contents[5])
2268
2269 call delete('test.out')
2270 enew! | close
2271 augroup Test2
2272 au!
2273 augroup END
2274
2275 " Test for the BufReadPre and BufReadPost autocmds
2276 augroup Test3
2277 au!
2278 " setup autocommands to decompress before reading and re-compress
2279 " afterwards
2280 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2281 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2282 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2283 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2284 augroup END
2285
2286 e! Xtestfile.gz " Edit compressed file
2287 call assert_equal([
2288 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2289 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2290 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2291 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2292 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2293 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2294 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2295 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2296 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2297 \ ], getline(1, 9))
2298
2299 w! >> test.out " Append it to the output file
2300
2301 augroup Test3
2302 au!
2303 augroup END
2304
2305 " Test for the FilterReadPre and FilterReadPost autocmds.
2306 set shelltemp " need temp files here
2307 augroup Test4
2308 au!
2309 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2310 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2311 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2312 au FilterReadPost *.out '[,']s/x/X/g
2313 augroup END
2314
2315 e! test.out " Edit the output file
2316 1,$!cat
2317 call assert_equal([
2318 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2319 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2320 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2321 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2322 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2323 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2324 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2325 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2326 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2327 \ ], getline(1, 9))
2328 call assert_equal([
2329 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2330 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2331 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2332 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2333 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2334 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2335 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2336 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2337 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2338 \ ], readfile('test.out'))
2339
2340 augroup Test4
2341 au!
2342 augroup END
2343 set shelltemp&vim
2344
2345 " Test for the FileReadPre and FileReadPost autocmds.
2346 augroup Test5
2347 au!
2348 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2349 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2350 au FileReadPost *.gz '[,']s/l/L/
2351 augroup END
2352
2353 new
2354 0r Xtestfile.gz " Read compressed file
2355 call assert_equal([
2356 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2357 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2358 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2359 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2360 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2361 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2362 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2363 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2364 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2365 \ ], getline(1, 9))
2366 call assert_equal([
2367 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2368 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2369 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2370 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2371 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2372 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2373 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2374 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2375 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2376 \ ], readfile('Xtestfile.gz'))
2377
2378 augroup Test5
2379 au!
2380 augroup END
2381
2382 au! FileChangedShell
2383 call delete('Xtestfile.gz')
2384 call delete('Xtest.c')
2385 call delete('test.out')
2386endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002387
2388func Test_throw_in_BufWritePre()
2389 new
2390 call setline(1, ['one', 'two', 'three'])
2391 call assert_false(filereadable('Xthefile'))
2392 augroup throwing
2393 au BufWritePre X* throw 'do not write'
2394 augroup END
2395 try
2396 w Xthefile
2397 catch
2398 let caught = 1
2399 endtry
2400 call assert_equal(1, caught)
2401 call assert_false(filereadable('Xthefile'))
2402
2403 bwipe!
2404 au! throwing
2405endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002406
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002407func Test_autocmd_in_try_block()
2408 call mkdir('Xdir')
2409 au BufEnter * let g:fname = expand('%')
2410 try
2411 edit Xdir/
2412 endtry
2413 call assert_match('Xdir', g:fname)
2414
2415 unlet g:fname
2416 au! BufEnter
2417 call delete('Xdir', 'rf')
2418endfunc
2419
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002420func Test_autocmd_SafeState()
2421 CheckRunVimInTerminal
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002422 let g:test_is_flaky = 1
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002423
2424 let lines =<< trim END
2425 let g:safe = 0
2426 let g:again = ''
2427 au SafeState * let g:safe += 1
2428 au SafeStateAgain * let g:again ..= 'x'
2429 func CallTimer()
2430 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2431 endfunc
2432 END
2433 call writefile(lines, 'XSafeState')
2434 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2435
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002436 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002437 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002438 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002439 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002440
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002441 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002442 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002443 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002444
2445 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002446 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002447 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002448 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002449 call term_sendkeys(buf, ":echo g:again\<CR>")
2450 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2451
2452 call StopVimInTerminal(buf)
2453 call delete('XSafeState')
2454endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002455
2456func Test_autocmd_CmdWinEnter()
2457 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002458 CheckFeature cmdwin
2459
Bram Moolenaar23324a02019-10-01 17:39:04 +02002460 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002461 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002462 let b:dummy_var = 'This is a dummy'
2463 autocmd CmdWinEnter * quit
2464 let winnr = winnr('$')
2465 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002466 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002467 call writefile(lines, filename)
2468 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2469
2470 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002471 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002472 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002473 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002474 call term_sendkeys(buf, ":echo &buftype\<cr>")
2475 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2476 call term_sendkeys(buf, ":echo winnr\<cr>")
2477 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2478
2479 " clean up
2480 call StopVimInTerminal(buf)
2481 call delete(filename)
2482endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002483
2484func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002485 CheckFeature quickfix
2486
Bram Moolenaarec66c412019-10-11 21:19:13 +02002487 pedit xx
2488 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002489 augroup winenter
2490 au WinEnter * if winnr('$') > 2 | quit | endif
2491 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002492 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002493
2494 augroup winenter
2495 au! WinEnter
2496 augroup END
2497
2498 bwipe xx
2499 bwipe x
2500 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002501endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002502
2503func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002504 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002505 edit! Xtest
2506 call setline(1, ['a', 'b', 'c', 'd'])
2507
2508 " :lockmarks preserves the marks
2509 call SetChangeMarks(2, 3)
2510 lockmarks write
2511 call assert_equal([2, 3], [line("'["), line("']")])
2512
2513 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2514 " original values for the user
2515 augroup lockmarks
2516 au!
2517 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2518 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2519 augroup END
2520
2521 lockmarks write
2522 call assert_equal([2, 3], [line("'["), line("']")])
2523
2524 if executable('cat')
2525 lockmarks %!cat
2526 call assert_equal([2, 3], [line("'["), line("']")])
2527 endif
2528
2529 lockmarks 3,4write Xtest2
2530 call assert_equal([2, 3], [line("'["), line("']")])
2531
2532 au! lockmarks
2533 augroup! lockmarks
2534 call delete('Xtest')
2535 call delete('Xtest2')
2536endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002537
2538func Test_FileType_spell()
2539 if !isdirectory('/tmp')
2540 throw "Skipped: requires /tmp directory"
2541 endif
2542
2543 " this was crashing with an invalid free()
2544 setglobal spellfile=/tmp/en.utf-8.add
2545 augroup crash
2546 autocmd!
2547 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2548 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2549 autocmd FileType anotherfiletype setlocal spell
2550 augroup END
2551 func! NoCrash() abort
2552 edit /tmp/crashfile
2553 endfunc
2554 call NoCrash()
2555
2556 au! crash
2557 setglobal spellfile=
2558endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002559
Bram Moolenaar406cd902020-02-18 21:54:41 +01002560" Test closing a window or editing another buffer from a FileChangedRO handler
2561" in a readonly buffer
2562func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002563 call test_override('ui_delay', 10)
2564
Bram Moolenaar406cd902020-02-18 21:54:41 +01002565 augroup FileChangedROTest
2566 au!
2567 autocmd FileChangedRO * quit
2568 augroup END
2569 new
2570 set readonly
2571 call assert_fails('normal i', 'E788:')
2572 close
2573 augroup! FileChangedROTest
2574
2575 augroup FileChangedROTest
2576 au!
2577 autocmd FileChangedRO * edit Xfile
2578 augroup END
2579 new
2580 set readonly
2581 call assert_fails('normal i', 'E788:')
2582 close
2583 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002584 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002585endfunc
2586
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002587func LogACmd()
2588 call add(g:logged, line('$'))
2589endfunc
2590
2591func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002592 CheckNotGui
2593
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002594 enew!
2595 tabnew
2596 call setline(1, ['a', 'b', 'c', 'd'])
2597 $
2598 au TermChanged * call LogACmd()
2599 let g:logged = []
2600 let term_save = &term
2601 set term=xterm
2602 call assert_equal([1, 4], g:logged)
2603
2604 au! TermChanged
2605 let &term = term_save
2606 bwipe!
2607endfunc
2608
Bram Moolenaare3284872020-03-19 13:55:03 +01002609" Test for FileReadCmd autocmd
2610func Test_autocmd_FileReadCmd()
2611 func ReadFileCmd()
2612 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2613 endfunc
2614 augroup FileReadCmdTest
2615 au!
2616 au FileReadCmd Xtest call ReadFileCmd()
2617 augroup END
2618
2619 new
2620 read ++bin Xtest
2621 read ++nobin Xtest
2622 read ++edit Xtest
2623 read ++bad=keep Xtest
2624 read ++bad=drop Xtest
2625 read ++bad=- Xtest
2626 read ++ff=unix Xtest
2627 read ++ff=dos Xtest
2628 read ++ff=mac Xtest
2629 read ++enc=utf-8 Xtest
2630
2631 call assert_equal(['',
2632 \ 'v:cmdarg = ++bin',
2633 \ 'v:cmdarg = ++nobin',
2634 \ 'v:cmdarg = ++edit',
2635 \ 'v:cmdarg = ++bad=keep',
2636 \ 'v:cmdarg = ++bad=drop',
2637 \ 'v:cmdarg = ++bad=-',
2638 \ 'v:cmdarg = ++ff=unix',
2639 \ 'v:cmdarg = ++ff=dos',
2640 \ 'v:cmdarg = ++ff=mac',
2641 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2642
2643 close!
2644 augroup FileReadCmdTest
2645 au!
2646 augroup END
2647 delfunc ReadFileCmd
2648endfunc
2649
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002650" Test for passing invalid arguments to autocmd
2651func Test_autocmd_invalid_args()
2652 " Additional character after * for event
2653 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2654 augroup Test
2655 augroup END
2656 " Invalid autocmd event
2657 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2658 " Invalid autocmd event in a autocmd group
2659 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2660 augroup! Test
2661 " Execute all autocmds
2662 call assert_fails('doautocmd * BufEnter', 'E217:')
2663 call assert_fails('augroup! x1a2b3', 'E367:')
2664 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002665 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002666endfunc
2667
2668" Test for deep nesting of autocmds
2669func Test_autocmd_deep_nesting()
2670 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2671 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2672 autocmd! BufEnter Xfile
2673endfunc
2674
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002675" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2676func Test_autocmd_sigusr1()
2677 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002678 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002679
2680 let g:sigusr1_passed = 0
2681 au SigUSR1 * let g:sigusr1_passed = 1
2682 call system('/bin/kill -s usr1 ' . getpid())
2683 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2684
2685 au! SigUSR1
2686 unlet g:sigusr1_passed
2687endfunc
2688
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002689" Test for BufReadPre autocmd deleting the file
2690func Test_BufReadPre_delfile()
2691 augroup TestAuCmd
2692 au!
2693 autocmd BufReadPre Xfile call delete('Xfile')
2694 augroup END
2695 call writefile([], 'Xfile')
2696 call assert_fails('new Xfile', 'E200:')
2697 call assert_equal('Xfile', @%)
2698 call assert_equal(1, &readonly)
2699 call delete('Xfile')
2700 augroup TestAuCmd
2701 au!
2702 augroup END
2703 close!
2704endfunc
2705
2706" Test for BufReadPre autocmd changing the current buffer
2707func Test_BufReadPre_changebuf()
2708 augroup TestAuCmd
2709 au!
2710 autocmd BufReadPre Xfile edit Xsomeotherfile
2711 augroup END
2712 call writefile([], 'Xfile')
2713 call assert_fails('new Xfile', 'E201:')
2714 call assert_equal('Xsomeotherfile', @%)
2715 call assert_equal(1, &readonly)
2716 call delete('Xfile')
2717 augroup TestAuCmd
2718 au!
2719 augroup END
2720 close!
2721endfunc
2722
2723" Test for BufWipeouti autocmd changing the current buffer when reading a file
2724" in an empty buffer with 'f' flag in 'cpo'
2725func Test_BufDelete_changebuf()
2726 new
2727 augroup TestAuCmd
2728 au!
2729 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2730 augroup END
2731 let save_cpo = &cpo
2732 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002733 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002734 call assert_equal('somefile', @%)
2735 let &cpo = save_cpo
2736 augroup TestAuCmd
2737 au!
2738 augroup END
2739 close!
2740endfunc
2741
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002742" Test for the temporary internal window used to execute autocmds
2743func Test_autocmd_window()
2744 %bw!
2745 edit one.txt
2746 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002747 vnew three.txt
2748 tabnew four.txt
2749 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002750 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002751 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002752 au!
2753 au BufEnter * call add(g:blist, [expand('<afile>'),
2754 \ win_gettype(bufwinnr(expand('<afile>')))])
2755 augroup END
2756
2757 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002758 call assert_equal([
2759 \ ['one.txt', 'autocmd'],
2760 \ ['two.txt', ''],
2761 \ ['four.txt', 'autocmd'],
2762 \ ['three.txt', ''],
2763 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002764
Bram Moolenaar832adf92020-06-25 19:01:36 +02002765 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002766 au!
2767 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002768 augroup! aucmd_win_test1
2769 %bw!
2770endfunc
2771
2772" Test for trying to close the temporary window used for executing an autocmd
2773func Test_close_autocmd_window()
2774 %bw!
2775 edit one.txt
2776 tabnew two.txt
2777 augroup aucmd_win_test2
2778 au!
2779 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2780 augroup END
2781
2782 call assert_fails('doautoall BufEnter', 'E813:')
2783
2784 augroup aucmd_win_test2
2785 au!
2786 augroup END
2787 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002788 %bwipe!
2789endfunc
2790
2791" Test for trying to close the tab that has the temporary window for exeucing
2792" an autocmd.
2793func Test_close_autocmd_tab()
2794 edit one.txt
2795 tabnew two.txt
2796 augroup aucmd_win_test
2797 au!
2798 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2799 augroup END
2800
2801 call assert_fails('doautoall BufEnter', 'E813:')
2802
2803 tabonly
2804 augroup aucmd_win_test
2805 au!
2806 augroup END
2807 augroup! aucmd_win_test
2808 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002809endfunc
2810
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01002811" This was using freed memory.
2812func Test_BufNew_arglocal()
2813 arglocal
2814 au BufNew * arglocal
2815 call assert_fails('drop xx', 'E1156:')
2816
2817 au! BufNew
2818endfunc
2819
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002820func Test_autocmd_closes_window()
2821 au BufNew,BufWinLeave * e %e
2822 file yyy
2823 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002824 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002825
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002826 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002827 au! BufNew
2828 au! BufWinLeave
2829endfunc
2830
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002831func Test_autocmd_quit_psearch()
2832 sn aa bb
2833 augroup aucmd_win_test
2834 au!
2835 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
2836 augroup END
2837 ps /
2838
2839 augroup aucmd_win_test
2840 au!
2841 augroup END
2842endfunc
2843
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002844" Fuzzer found some strange combination that caused a crash.
2845func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002846 " For unknown reason this hangs on MS-Windows
2847 CheckNotMSWindows
2848
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002849 augroup aucmd_normal_test
2850 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
2851 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002852 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002853 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002854 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002855 normal G
2856
2857 augroup aucmd_normal_test
2858 au!
2859 augroup END
2860endfunc
2861
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002862func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002863 " For unknown reason this hangs on MS-Windows
2864 CheckNotMSWindows
2865
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002866 au BufWinLeave * nested q
2867 call assert_fails("norm 7q?\n", 'E855:')
2868
2869 au! BufWinLeave
2870 new
2871 only
2872endfunc
2873
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002874func Test_autocmd_vimgrep()
2875 augroup aucmd_vimgrep
2876 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * sb
2877 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * q9�
2878 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002879 %bwipe!
Bram Moolenaardd07c022021-02-07 13:32:46 +01002880 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002881
2882 augroup aucmd_vimgrep
2883 au!
2884 augroup END
2885endfunc
2886
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002887func Test_autocmd_with_block()
2888 augroup block_testing
2889 au BufReadPost *.xml {
2890 setlocal matchpairs+=<:>
2891 /<start
2892 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02002893 au CursorHold * {
2894 autocmd BufReadPre * ++once echo 'one' | echo 'two'
2895 g:gotSafeState = 77
2896 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002897 augroup END
2898
2899 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
2900 call assert_equal(expected, execute('au BufReadPost *.xml'))
2901
Bram Moolenaar63b91732021-08-05 20:40:03 +02002902 doautocmd CursorHold
2903 call assert_equal(77, g:gotSafeState)
2904 unlet g:gotSafeState
2905
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002906 augroup block_testing
2907 au!
2908 augroup END
2909endfunc
2910
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002911" Test TextChangedI and TextChanged
2912func Test_Changed_ChangedI()
2913 new
2914 call test_override("char_avail", 1)
2915 let [g:autocmd_i, g:autocmd_n] = ['','']
2916
2917 func! TextChangedAutocmdI(char)
2918 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
2919 endfunc
2920
2921 augroup Test_TextChanged
2922 au!
2923 au TextChanged <buffer> :call TextChangedAutocmdI('N')
2924 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
2925 augroup END
2926
2927 call feedkeys("ifoo\<esc>", 'tnix')
2928 " TODO: Test test does not seem to trigger TextChanged autocommand, this
2929 " requires running Vim in a terminal window.
2930 " call assert_equal('N3', g:autocmd_n)
2931 call assert_equal('I3', g:autocmd_i)
2932
2933 call feedkeys("yyp", 'tnix')
2934 " TODO: Test test does not seem to trigger TextChanged autocommand.
2935 " call assert_equal('N4', g:autocmd_n)
2936 call assert_equal('I3', g:autocmd_i)
2937
2938 " CleanUp
2939 call test_override("char_avail", 0)
2940 au! TextChanged <buffer>
2941 au! TextChangedI <buffer>
2942 augroup! Test_TextChanged
2943 delfu TextChangedAutocmdI
2944 unlet! g:autocmd_i g:autocmd_n
2945
2946 bw!
2947endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002948
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002949" vim: shiftwidth=2 sts=2 expandtab