blob: 1ecb8830797a2586710f361f7bffb7f69dba8a02 [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()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000360 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200361 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000362 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200363
364 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000365 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200366 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000367 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200368
369 " test that a bar is recognized after the {event}
370 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000371 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200372 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000373 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200374
375 " test that a bar is recognized after the {group}
376 call s:AddAnAutocmd()
377 au! vimBarTest|echo 'hello'
378 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
379endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200380
Bram Moolenaar5c809082016-09-01 16:21:48 +0200381func RemoveGroup()
382 autocmd! StartOK
383 augroup! StartOK
384endfunc
385
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200386func Test_augroup_warning()
387 augroup TheWarning
388 au VimEnter * echo 'entering'
389 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100390 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200391 redir => res
392 augroup! TheWarning
393 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100394 call assert_match("W19:", res)
395 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200396
397 " check "Another" does not take the pace of the deleted entry
398 augroup Another
399 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100400 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200401 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200402
403 " no warning for postpone aucmd delete
404 augroup StartOK
405 au VimEnter * call RemoveGroup()
406 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100407 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200408 redir => res
409 doautocmd VimEnter
410 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100411 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200412 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200413
414 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200415endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200416
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200417func Test_BufReadCmdHelp()
418 " This used to cause access to free memory
419 au BufReadCmd * e +h
420 help
421
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200422 au! BufReadCmd
423endfunc
424
425func Test_BufReadCmdHelpJump()
426 " This used to cause access to free memory
427 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200428 " } to fix highlighting
429 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200430
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200431 au! BufReadCmd
432endfunc
433
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200434func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200435 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200436 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200437 call assert_fails('augroup! x', 'E936:')
438 au VimEnter * echo
439 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200440 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100441 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200442 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200443endfunc
444
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200445" Tests for autocommands on :close command.
446" This used to be in test13.
447func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200448 " Clean up buffers, because in some cases this function fails.
449 call s:cleanup_buffers()
450
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200451 " Write three files and open them, each in a window.
452 " Then go to next window, with autocommand that deletes the previous one.
453 " Do this twice, writing the file.
454 e! Xtestje1
455 call setline(1, 'testje1')
456 w
457 sp Xtestje2
458 call setline(1, 'testje2')
459 w
460 sp Xtestje3
461 call setline(1, 'testje3')
462 w
463 wincmd w
464 au WinLeave Xtestje2 bwipe
465 wincmd w
466 call assert_equal('Xtestje1', expand('%'))
467
468 au WinLeave Xtestje1 bwipe Xtestje3
469 close
470 call assert_equal('Xtestje1', expand('%'))
471
472 " Test deleting the buffer on a Unload event. If this goes wrong there
473 " will be the ATTENTION prompt.
474 e Xtestje1
475 au!
476 au! BufUnload Xtestje1 bwipe
477 call assert_fails('e Xtestje3', 'E937:')
478 call assert_equal('Xtestje3', expand('%'))
479
480 e Xtestje2
481 sp Xtestje1
482 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200483 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200484
485 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
486 " there are ml_line errors and/or a Crash.
487 au!
488 only
489 e Xanother
490 e Xtestje1
491 bwipe Xtestje2
492 bwipe Xtestje3
493 au BufWipeout Xtestje1 buf Xtestje1
494 bwipe
495 call assert_equal('Xanother', expand('%'))
496
497 only
498 help
499 wincmd w
500 1quit
501 call assert_equal('Xanother', expand('%'))
502
503 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100504 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200505 call delete('Xtestje1')
506 call delete('Xtestje2')
507 call delete('Xtestje3')
508endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100509
510func Test_BufEnter()
511 au! BufEnter
512 au Bufenter * let val = val . '+'
513 let g:val = ''
514 split NewFile
515 call assert_equal('+', g:val)
516 bwipe!
517 call assert_equal('++', g:val)
518
519 " Also get BufEnter when editing a directory
520 call mkdir('Xdir')
521 split Xdir
522 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100523
524 " On MS-Windows we can't edit the directory, make sure we wipe the right
525 " buffer.
526 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100527
528 call delete('Xdir', 'd')
529 au! BufEnter
530endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100531
532" Closing a window might cause an endless loop
533" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200534func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200535 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100536 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200537 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100538 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100539 mksession!
540
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200541 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200542 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200543 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100544 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200545 augroup test_autocmd_sessionload
546 autocmd!
547 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
548 augroup END
549
550 func WriteErrors()
551 call writefile([execute("messages")], "Xerrors")
552 endfunc
553 au VimLeave * call WriteErrors()
554 [CODE]
555
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100556 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200557 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100558 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200559 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100560
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100561 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100562 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100563 call delete(file)
564 endfor
565endfunc
566
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100567" Using :blast and :ball for many events caused a crash, because b_nwindows was
568" not incremented correctly.
569func Test_autocmd_blast_badd()
570 let content =<< trim [CODE]
571 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
572 edit foo1
573 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
574 edit foo2
575 call writefile(['OK'], 'Xerrors')
576 qall
577 [CODE]
578
579 call writefile(content, 'XblastBall')
580 call system(GetVimCommand() .. ' --clean -S XblastBall')
581 call assert_match('OK', readfile('Xerrors')->join())
582
583 call delete('XblastBall')
584 call delete('Xerrors')
585endfunc
586
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100587" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200588func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100589 tabnew
590 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100591 mksession!
592
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200593 let content =<< trim [CODE]
594 set nocp noswapfile
595 function! DeleteInactiveBufs()
596 tabfirst
597 let tabblist = []
598 for i in range(1, tabpagenr(''$''))
599 call extend(tabblist, tabpagebuflist(i))
600 endfor
601 for b in range(1, bufnr(''$''))
602 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
603 exec ''bwipeout '' . b
604 endif
605 endfor
606 echomsg "SessionLoadPost DONE"
607 endfunction
608 au SessionLoadPost * call DeleteInactiveBufs()
609
610 func WriteErrors()
611 call writefile([execute("messages")], "Xerrors")
612 endfunc
613 au VimLeave * call WriteErrors()
614 [CODE]
615
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100616 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200617 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100618 let errors = join(readfile('Xerrors'))
619 " This probably only ever matches on unix.
620 call assert_notmatch('Caught deadly signal SEGV', errors)
621 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100622
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100623 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100624 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100625 call delete(file)
626 endfor
627endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200628
629func Test_empty_doau()
630 doau \|
631endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200632
633func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200634 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200635 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200636 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
637 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 +0200638 let g:opt = [expected, actual]
639 "call assert_equal(expected, actual)
640endfunc
641
642func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200643 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200644
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200645 badd test_autocmd.vim
646
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200647 call test_override('starting', 1)
648 set nocp
649 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
650
651 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100652 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200653 set nu
654 call assert_equal([], g:options)
655 call assert_equal(g:opt[0], g:opt[1])
656
657 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100658 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200659 setlocal nonu
660 call assert_equal([], g:options)
661 call assert_equal(g:opt[0], g:opt[1])
662
663 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100664 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200665 setglobal nonu
666 call assert_equal([], g:options)
667 call assert_equal(g:opt[0], g:opt[1])
668
669 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100670 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200671 setlocal ai
672 call assert_equal([], g:options)
673 call assert_equal(g:opt[0], g:opt[1])
674
675 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100676 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200677 setglobal ai
678 call assert_equal([], g:options)
679 call assert_equal(g:opt[0], g:opt[1])
680
681 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100682 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200683 set ai!
684 call assert_equal([], g:options)
685 call assert_equal(g:opt[0], g:opt[1])
686
687 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100688 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200689 noa setlocal ai
690 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200691 set ai!
692 call assert_equal([], g:options)
693 call assert_equal(g:opt[0], g:opt[1])
694
695 " Should not print anything, use :noa
696 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100697 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200698 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200699 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200700 call assert_equal(g:opt[0], g:opt[1])
701
702 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100703 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200704 set list nu
705 call assert_equal([], g:options)
706 call assert_equal(g:opt[0], g:opt[1])
707
708 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100709 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200710 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200711 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 +0200712 call assert_equal(g:opt[0], g:opt[1])
713
714 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100715 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200716 setlocal acd
717 call assert_equal([], g:options)
718 call assert_equal(g:opt[0], g:opt[1])
719
720 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100721 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200722 set ar
723 call assert_equal([], g:options)
724 call assert_equal(g:opt[0], g:opt[1])
725
726 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100727 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200728 setlocal ar
729 call assert_equal([], g:options)
730 call assert_equal(g:opt[0], g:opt[1])
731
732 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100733 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200734 setglobal invar
735 call assert_equal([], g:options)
736 call assert_equal(g:opt[0], g:opt[1])
737
738 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100739 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
740 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200741 call assert_equal([], g:options)
742 call assert_equal(g:opt[0], g:opt[1])
743
744 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100745 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200746 " try twice, first time, shouldn't trigger because option name is invalid,
747 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200748 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200749 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200750 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200751 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200752 call assert_equal([], g:options)
753 call assert_equal(g:opt[0], g:opt[1])
754
755 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100756 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200757 call setwinvar(0, '&number', 1)
758 call assert_equal([], g:options)
759 call assert_equal(g:opt[0], g:opt[1])
760
761 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100762 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200763 setlocal key=blah
764 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200765 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200766 call assert_equal(g:opt[0], g:opt[1])
767
Bram Moolenaard7c96872019-06-15 17:12:48 +0200768
769 " 18a: Setting string global option"
770 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100771 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200772 set backupext=foo
773 call assert_equal([], g:options)
774 call assert_equal(g:opt[0], g:opt[1])
775
776 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100777 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200778 set backupext&
779 call assert_equal([], g:options)
780 call assert_equal(g:opt[0], g:opt[1])
781
782 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100783 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200784 setglobal backupext=bar
785 call assert_equal([], g:options)
786 call assert_equal(g:opt[0], g:opt[1])
787
788 " 18d: Setting local string global option"
789 " As this is a global option this sets the global value even though
790 " :setlocal is used!
791 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100792 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200793 setlocal backupext=baz
794 call assert_equal([], g:options)
795 call assert_equal(g:opt[0], g:opt[1])
796
797 " 18e: Setting again string global option"
798 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
799 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100800 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200801 set backupext=fuu
802 call assert_equal([], g:options)
803 call assert_equal(g:opt[0], g:opt[1])
804
805
zeertzjqb811de52021-10-21 10:50:44 +0100806 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200807 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100808 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200809 set tags=tagpath
810 call assert_equal([], g:options)
811 call assert_equal(g:opt[0], g:opt[1])
812
zeertzjqb811de52021-10-21 10:50:44 +0100813 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100814 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200815 set tags&
816 call assert_equal([], g:options)
817 call assert_equal(g:opt[0], g:opt[1])
818
zeertzjqb811de52021-10-21 10:50:44 +0100819 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100820 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200821 setglobal tags=tagpath1
822 call assert_equal([], g:options)
823 call assert_equal(g:opt[0], g:opt[1])
824
zeertzjqb811de52021-10-21 10:50:44 +0100825 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100826 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200827 setlocal tags=tagpath2
828 call assert_equal([], g:options)
829 call assert_equal(g:opt[0], g:opt[1])
830
zeertzjqb811de52021-10-21 10:50:44 +0100831 " 19e: Setting again string global-local (to buffer) option"
832 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200833 " but the old local value for all other kinds of options.
834 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
835 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100836 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200837 set tags=tagpath
838 call assert_equal([], g:options)
839 call assert_equal(g:opt[0], g:opt[1])
840
zeertzjqb811de52021-10-21 10:50:44 +0100841 " 19f: Setting string global-local (to buffer) option to an empty string"
842 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200843 " but the old local value for all other kinds of options.
844 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
845 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100846 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200847 set tags=tagpath
848 call assert_equal([], g:options)
849 call assert_equal(g:opt[0], g:opt[1])
850
851
852 " 20a: Setting string local (to buffer) option"
853 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100854 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200855 set spelllang=elvish,klingon
856 call assert_equal([], g:options)
857 call assert_equal(g:opt[0], g:opt[1])
858
859 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100860 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200861 set spelllang&
862 call assert_equal([], g:options)
863 call assert_equal(g:opt[0], g:opt[1])
864
865 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100866 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200867 setglobal spelllang=elvish
868 call assert_equal([], g:options)
869 call assert_equal(g:opt[0], g:opt[1])
870
871 " 20d: Setting local string local (to buffer) option"
872 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100873 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200874 setlocal spelllang=klingon
875 call assert_equal([], g:options)
876 call assert_equal(g:opt[0], g:opt[1])
877
878 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +0100879 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200880 " but the old local value for all other kinds of options.
881 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
882 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100883 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200884 set spelllang=foo
885 call assert_equal([], g:options)
886 call assert_equal(g:opt[0], g:opt[1])
887
888
zeertzjqb811de52021-10-21 10:50:44 +0100889 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200890 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100891 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200892 set statusline=foo
893 call assert_equal([], g:options)
894 call assert_equal(g:opt[0], g:opt[1])
895
zeertzjqb811de52021-10-21 10:50:44 +0100896 " 21b: Resetting string global-local (to window) option"
897 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200898 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100899 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200900 set statusline&
901 call assert_equal([], g:options)
902 call assert_equal(g:opt[0], g:opt[1])
903
zeertzjqb811de52021-10-21 10:50:44 +0100904 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100905 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200906 setglobal statusline=bar
907 call assert_equal([], g:options)
908 call assert_equal(g:opt[0], g:opt[1])
909
zeertzjqb811de52021-10-21 10:50:44 +0100910 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200911 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100912 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200913 setlocal statusline=baz
914 call assert_equal([], g:options)
915 call assert_equal(g:opt[0], g:opt[1])
916
zeertzjqb811de52021-10-21 10:50:44 +0100917 " 21e: Setting again string global-local (to window) option"
918 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200919 " but the old local value for all other kinds of options.
920 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
921 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100922 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200923 set statusline=foo
924 call assert_equal([], g:options)
925 call assert_equal(g:opt[0], g:opt[1])
926
927
928 " 22a: Setting string local (to window) option"
929 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100930 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200931 set foldignore=fo
932 call assert_equal([], g:options)
933 call assert_equal(g:opt[0], g:opt[1])
934
935 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100936 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200937 set foldignore&
938 call assert_equal([], g:options)
939 call assert_equal(g:opt[0], g:opt[1])
940
941 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100942 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200943 setglobal foldignore=bar
944 call assert_equal([], g:options)
945 call assert_equal(g:opt[0], g:opt[1])
946
947 " 22d: Setting local string local (to window) option"
948 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100949 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200950 setlocal foldignore=baz
951 call assert_equal([], g:options)
952 call assert_equal(g:opt[0], g:opt[1])
953
954 " 22e: Setting again string local (to window) option"
955 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
956 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100957 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200958 set foldignore=fo
959 call assert_equal([], g:options)
960 call assert_equal(g:opt[0], g:opt[1])
961
962
zeertzjqb811de52021-10-21 10:50:44 +0100963 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200964 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
965 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100966 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200967 setglobal cmdheight=2
968 call assert_equal([], g:options)
969 call assert_equal(g:opt[0], g:opt[1])
970
971 " 23b: Setting local number global option"
972 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
973 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100974 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200975 setlocal cmdheight=2
976 call assert_equal([], g:options)
977 call assert_equal(g:opt[0], g:opt[1])
978
979 " 23c: Setting again number global option"
980 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
981 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100982 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200983 set cmdheight=2
984 call assert_equal([], g:options)
985 call assert_equal(g:opt[0], g:opt[1])
986
987 " 23d: Setting again number global option"
988 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100989 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200990 set cmdheight=2
991 call assert_equal([], g:options)
992 call assert_equal(g:opt[0], g:opt[1])
993
994
995 " 24a: Setting global number global-local (to buffer) option"
996 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
997 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100998 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200999 setglobal undolevels=2
1000 call assert_equal([], g:options)
1001 call assert_equal(g:opt[0], g:opt[1])
1002
1003 " 24b: Setting local number global-local (to buffer) option"
1004 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1005 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001006 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001007 setlocal undolevels=2
1008 call assert_equal([], g:options)
1009 call assert_equal(g:opt[0], g:opt[1])
1010
1011 " 24c: Setting again number global-local (to buffer) option"
1012 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1013 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001014 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001015 set undolevels=2
1016 call assert_equal([], g:options)
1017 call assert_equal(g:opt[0], g:opt[1])
1018
1019 " 24d: Setting again global number global-local (to buffer) option"
1020 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001021 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001022 set undolevels=2
1023 call assert_equal([], g:options)
1024 call assert_equal(g:opt[0], g:opt[1])
1025
1026
1027 " 25a: Setting global number local (to buffer) option"
1028 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1029 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001030 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001031 setglobal wrapmargin=2
1032 call assert_equal([], g:options)
1033 call assert_equal(g:opt[0], g:opt[1])
1034
1035 " 25b: Setting local number local (to buffer) option"
1036 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1037 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001038 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001039 setlocal wrapmargin=2
1040 call assert_equal([], g:options)
1041 call assert_equal(g:opt[0], g:opt[1])
1042
1043 " 25c: Setting again number local (to buffer) option"
1044 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1045 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001046 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001047 set wrapmargin=2
1048 call assert_equal([], g:options)
1049 call assert_equal(g:opt[0], g:opt[1])
1050
1051 " 25d: Setting again global number local (to buffer) option"
1052 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001053 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001054 set wrapmargin=2
1055 call assert_equal([], g:options)
1056 call assert_equal(g:opt[0], g:opt[1])
1057
1058
1059 " 26: Setting number global-local (to window) option.
1060 " Such option does currently not exist.
1061
1062
1063 " 27a: Setting global number local (to window) option"
1064 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1065 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001066 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001067 setglobal foldcolumn=2
1068 call assert_equal([], g:options)
1069 call assert_equal(g:opt[0], g:opt[1])
1070
1071 " 27b: Setting local number local (to window) option"
1072 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1073 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001074 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001075 setlocal foldcolumn=2
1076 call assert_equal([], g:options)
1077 call assert_equal(g:opt[0], g:opt[1])
1078
1079 " 27c: Setting again number local (to window) option"
1080 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1081 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001082 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001083 set foldcolumn=2
1084 call assert_equal([], g:options)
1085 call assert_equal(g:opt[0], g:opt[1])
1086
zeertzjqb811de52021-10-21 10:50:44 +01001087 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001088 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001089 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001090 set foldcolumn=2
1091 call assert_equal([], g:options)
1092 call assert_equal(g:opt[0], g:opt[1])
1093
1094
1095 " 28a: Setting global boolean global option"
1096 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1097 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001098 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001099 setglobal nowrapscan
1100 call assert_equal([], g:options)
1101 call assert_equal(g:opt[0], g:opt[1])
1102
1103 " 28b: Setting local boolean global option"
1104 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1105 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001106 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001107 setlocal nowrapscan
1108 call assert_equal([], g:options)
1109 call assert_equal(g:opt[0], g:opt[1])
1110
1111 " 28c: Setting again boolean global option"
1112 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1113 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001114 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001115 set nowrapscan
1116 call assert_equal([], g:options)
1117 call assert_equal(g:opt[0], g:opt[1])
1118
1119 " 28d: Setting again global boolean global option"
1120 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001121 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001122 set wrapscan
1123 call assert_equal([], g:options)
1124 call assert_equal(g:opt[0], g:opt[1])
1125
1126
1127 " 29a: Setting global boolean global-local (to buffer) option"
1128 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1129 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001130 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001131 setglobal autoread
1132 call assert_equal([], g:options)
1133 call assert_equal(g:opt[0], g:opt[1])
1134
1135 " 29b: Setting local boolean global-local (to buffer) option"
1136 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1137 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001138 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001139 setlocal noautoread
1140 call assert_equal([], g:options)
1141 call assert_equal(g:opt[0], g:opt[1])
1142
1143 " 29c: Setting again boolean global-local (to buffer) option"
1144 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1145 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001146 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001147 set autoread
1148 call assert_equal([], g:options)
1149 call assert_equal(g:opt[0], g:opt[1])
1150
1151 " 29d: Setting again global boolean global-local (to buffer) option"
1152 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001153 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001154 set autoread
1155 call assert_equal([], g:options)
1156 call assert_equal(g:opt[0], g:opt[1])
1157
1158
1159 " 30a: Setting global boolean local (to buffer) option"
1160 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1161 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001162 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001163 setglobal cindent
1164 call assert_equal([], g:options)
1165 call assert_equal(g:opt[0], g:opt[1])
1166
1167 " 30b: Setting local boolean local (to buffer) option"
1168 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1169 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001170 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001171 setlocal nocindent
1172 call assert_equal([], g:options)
1173 call assert_equal(g:opt[0], g:opt[1])
1174
1175 " 30c: Setting again boolean local (to buffer) option"
1176 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1177 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001178 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001179 set cindent
1180 call assert_equal([], g:options)
1181 call assert_equal(g:opt[0], g:opt[1])
1182
1183 " 30d: Setting again global boolean local (to buffer) option"
1184 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001185 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001186 set cindent
1187 call assert_equal([], g:options)
1188 call assert_equal(g:opt[0], g:opt[1])
1189
1190
1191 " 31: Setting boolean global-local (to window) option
1192 " Currently no such option exists.
1193
1194
1195 " 32a: Setting global boolean local (to window) option"
1196 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1197 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001198 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001199 setglobal cursorcolumn
1200 call assert_equal([], g:options)
1201 call assert_equal(g:opt[0], g:opt[1])
1202
1203 " 32b: Setting local boolean local (to window) option"
1204 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1205 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001206 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001207 setlocal nocursorcolumn
1208 call assert_equal([], g:options)
1209 call assert_equal(g:opt[0], g:opt[1])
1210
1211 " 32c: Setting again boolean local (to window) option"
1212 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1213 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001214 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001215 set cursorcolumn
1216 call assert_equal([], g:options)
1217 call assert_equal(g:opt[0], g:opt[1])
1218
1219 " 32d: Setting again global boolean local (to window) option"
1220 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001221 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001222 set cursorcolumn
1223 call assert_equal([], g:options)
1224 call assert_equal(g:opt[0], g:opt[1])
1225
1226
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001227 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001228 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001229 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001230 set backspace=2
1231 call assert_equal([], g:options)
1232 call assert_equal(g:opt[0], g:opt[1])
1233
1234
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001235 " Cleanup
1236 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001237 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001238 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 +02001239 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001240 endfor
1241 call test_override('starting', 0)
1242 delfunc! AutoCommandOptionSet
1243endfunc
1244
1245func Test_OptionSet_diffmode()
1246 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001247 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001248 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001249 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001250
1251 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1252 call assert_equal(0, &l:cul)
1253 diffthis
1254 call assert_equal(1, &l:cul)
1255
1256 vnew
1257 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1258 call assert_equal(0, &l:cul)
1259 diffthis
1260 call assert_equal(1, &l:cul)
1261
1262 diffoff
1263 call assert_equal(0, &l:cul)
1264 call assert_equal(1, getwinvar(2, '&l:cul'))
1265 bw!
1266
1267 call assert_equal(1, &l:cul)
1268 diffoff!
1269 call assert_equal(0, &l:cul)
1270 call assert_equal(0, getwinvar(1, '&l:cul'))
1271 bw!
1272
1273 " Cleanup
1274 au! OptionSet
1275 call test_override('starting', 0)
1276endfunc
1277
1278func Test_OptionSet_diffmode_close()
1279 call test_override('starting', 1)
1280 " 19: Try to close the current window when entering diff mode
1281 " should not segfault
1282 new
1283 au OptionSet diff close
1284
1285 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001286 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001287 call assert_equal(1, &diff)
1288 vnew
1289 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001290 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001291 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001292 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001293 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001294 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001295 bw!
1296
1297 " Cleanup
1298 au! OptionSet
1299 call test_override('starting', 0)
1300 "delfunc! AutoCommandOptionSet
1301endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001302
1303" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1304func Test_BufleaveWithDelete()
1305 new | edit Xfile1
1306
1307 augroup test_bufleavewithdelete
1308 autocmd!
1309 autocmd BufLeave Xfile1 bwipe Xfile2
1310 augroup END
1311
1312 call assert_fails('edit Xfile2', 'E143:')
1313 call assert_equal('Xfile1', bufname('%'))
1314
1315 autocmd! test_bufleavewithdelete BufLeave Xfile1
1316 augroup! test_bufleavewithdelete
1317
1318 new
1319 bwipe! Xfile1
1320endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001321
1322" Test for autocommand that changes the buffer list, when doing ":ball".
1323func Test_Acmd_BufAll()
1324 enew!
1325 %bwipe!
1326 call writefile(['Test file Xxx1'], 'Xxx1')
1327 call writefile(['Test file Xxx2'], 'Xxx2')
1328 call writefile(['Test file Xxx3'], 'Xxx3')
1329
1330 " Add three files to the buffer list
1331 split Xxx1
1332 close
1333 split Xxx2
1334 close
1335 split Xxx3
1336 close
1337
1338 " Wipe the buffer when the buffer is opened
1339 au BufReadPost Xxx2 bwipe
1340
1341 call append(0, 'Test file Xxx4')
1342 ball
1343
1344 call assert_equal(2, winnr('$'))
1345 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1346 wincmd t
1347
1348 au! BufReadPost
1349 %bwipe!
1350 call delete('Xxx1')
1351 call delete('Xxx2')
1352 call delete('Xxx3')
1353 enew! | only
1354endfunc
1355
1356" Test for autocommand that changes current buffer on BufEnter event.
1357" Check if modelines are interpreted for the correct buffer.
1358func Test_Acmd_BufEnter()
1359 %bwipe!
1360 call writefile(['start of test file Xxx1',
1361 \ "\<Tab>this is a test",
1362 \ 'end of test file Xxx1'], 'Xxx1')
1363 call writefile(['start of test file Xxx2',
1364 \ 'vim: set noai :',
1365 \ "\<Tab>this is a test",
1366 \ 'end of test file Xxx2'], 'Xxx2')
1367
1368 au BufEnter Xxx2 brew
1369 set ai modeline modelines=3
1370 edit Xxx1
1371 " edit Xxx2, autocmd will do :brew
1372 edit Xxx2
1373 exe "normal G?this is a\<CR>"
1374 " Append text with autoindent to this file
1375 normal othis should be auto-indented
1376 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1377 call assert_equal(3, line('.'))
1378 " Remove autocmd and edit Xxx2 again
1379 au! BufEnter Xxx2
1380 buf! Xxx2
1381 exe "normal G?this is a\<CR>"
1382 " append text without autoindent to Xxx
1383 normal othis should be in column 1
1384 call assert_equal("this should be in column 1", getline('.'))
1385 call assert_equal(4, line('.'))
1386
1387 %bwipe!
1388 call delete('Xxx1')
1389 call delete('Xxx2')
1390 set ai&vim modeline&vim modelines&vim
1391endfunc
1392
1393" Test for issue #57
1394" do not move cursor on <c-o> when autoindent is set
1395func Test_ai_CTRL_O()
1396 enew!
1397 set ai
1398 let save_fo = &fo
1399 set fo+=r
1400 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1401 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1402 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1403
1404 set ai&vim
1405 let &fo = save_fo
1406 enew!
1407endfunc
1408
1409" Test for autocommand that deletes the current buffer on BufLeave event.
1410" Also test deleting the last buffer, should give a new, empty buffer.
1411func Test_BufLeave_Wipe()
1412 %bwipe!
1413 let content = ['start of test file Xxx',
1414 \ 'this is a test',
1415 \ 'end of test file Xxx']
1416 call writefile(content, 'Xxx1')
1417 call writefile(content, 'Xxx2')
1418
1419 au BufLeave Xxx2 bwipe
1420 edit Xxx1
1421 split Xxx2
1422 " delete buffer Xxx2, we should be back to Xxx1
1423 bwipe
1424 call assert_equal('Xxx1', bufname('%'))
1425 call assert_equal(1, winnr('$'))
1426
1427 " Create an alternate buffer
1428 %write! test.out
1429 call assert_equal('test.out', bufname('#'))
1430 " delete alternate buffer
1431 bwipe test.out
1432 call assert_equal('Xxx1', bufname('%'))
1433 call assert_equal('', bufname('#'))
1434
1435 au BufLeave Xxx1 bwipe
1436 " delete current buffer, get an empty one
1437 bwipe!
1438 call assert_equal(1, line('$'))
1439 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001440 let g:bufinfo = getbufinfo()
1441 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001442
1443 call delete('Xxx1')
1444 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001445 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001446 %bwipe
1447 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001448
1449 " check that bufinfo doesn't contain a pointer to freed memory
1450 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001451endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001452
1453func Test_QuitPre()
1454 edit Xfoo
1455 let winid = win_getid(winnr())
1456 split Xbar
1457 au! QuitPre * let g:afile = expand('<afile>')
1458 " Close the other window, <afile> should be correct.
1459 exe win_id2win(winid) . 'q'
1460 call assert_equal('Xfoo', g:afile)
1461
1462 unlet g:afile
1463 bwipe Xfoo
1464 bwipe Xbar
1465endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001466
1467func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001468 au! CmdlineChanged : let g:text = getcmdline()
1469 let g:text = 0
1470 call feedkeys(":echom 'hello'\<CR>", 'xt')
1471 call assert_equal("echom 'hello'", g:text)
1472 au! CmdlineChanged
1473
1474 au! CmdlineChanged : let g:entered = expand('<afile>')
1475 let g:entered = 0
1476 call feedkeys(":echom 'hello'\<CR>", 'xt')
1477 call assert_equal(':', g:entered)
1478 au! CmdlineChanged
1479
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001480 au! CmdlineEnter : let g:entered = expand('<afile>')
1481 au! CmdlineLeave : let g:left = expand('<afile>')
1482 let g:entered = 0
1483 let g:left = 0
1484 call feedkeys(":echo 'hello'\<CR>", 'xt')
1485 call assert_equal(':', g:entered)
1486 call assert_equal(':', g:left)
1487 au! CmdlineEnter
1488 au! CmdlineLeave
1489
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001490 let save_shellslash = &shellslash
1491 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001492 au! CmdlineEnter / let g:entered = expand('<afile>')
1493 au! CmdlineLeave / let g:left = expand('<afile>')
1494 let g:entered = 0
1495 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001496 new
1497 call setline(1, 'hello')
1498 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001499 call assert_equal('/', g:entered)
1500 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001501 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001502 au! CmdlineEnter
1503 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001504 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001505endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001506
1507" Test for BufWritePre autocommand that deletes or unloads the buffer.
1508func Test_BufWritePre()
1509 %bwipe
1510 au BufWritePre Xxx1 bunload
1511 au BufWritePre Xxx2 bwipe
1512
1513 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1514 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1515
1516 edit Xtest
1517 e! Xxx2
1518 bdel Xtest
1519 e Xxx1
1520 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001521 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001522 call assert_equal('Xxx2', bufname('%'))
1523 edit Xtest
1524 e! Xxx2
1525 bwipe Xtest
1526 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001527 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001528 call assert_equal('Xxx1', bufname('%'))
1529 au! BufWritePre
1530 call delete('Xxx1')
1531 call delete('Xxx2')
1532endfunc
1533
1534" Test for BufUnload autocommand that unloads all the other buffers
1535func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001536 let g:test_is_flaky = 1
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001537 call writefile(['Test file Xxx1'], 'Xxx1')"
1538 call writefile(['Test file Xxx2'], 'Xxx2')"
1539
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001540 let content =<< trim [CODE]
1541 func UnloadAllBufs()
1542 let i = 1
1543 while i <= bufnr('$')
1544 if i != bufnr('%') && bufloaded(i)
1545 exe i . 'bunload'
1546 endif
1547 let i += 1
1548 endwhile
1549 endfunc
1550 au BufUnload * call UnloadAllBufs()
1551 au VimLeave * call writefile(['Test Finished'], 'Xout')
1552 edit Xxx1
1553 split Xxx2
1554 q
1555 [CODE]
1556
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001557 call writefile(content, 'Xtest')
1558
1559 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001560 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001561 call assert_true(filereadable('Xout'))
1562
1563 call delete('Xxx1')
1564 call delete('Xxx2')
1565 call delete('Xtest')
1566 call delete('Xout')
1567endfunc
1568
1569" Some tests for buffer-local autocommands
1570func Test_buflocal_autocmd()
1571 let g:bname = ''
1572 edit xx
1573 au BufLeave <buffer> let g:bname = expand("%")
1574 " here, autocommand for xx should trigger.
1575 " but autocommand shall not apply to buffer named <buffer>.
1576 edit somefile
1577 call assert_equal('xx', g:bname)
1578 let g:bname = ''
1579 " here, autocommand shall be auto-deleted
1580 bwipe xx
1581 " autocmd should not trigger
1582 edit xx
1583 call assert_equal('', g:bname)
1584 " autocmd should not trigger
1585 edit somefile
1586 call assert_equal('', g:bname)
1587 enew
1588 unlet g:bname
1589endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001590
1591" Test for "*Cmd" autocommands
1592func Test_Cmd_Autocmds()
1593 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1594
1595 enew!
1596 au BufReadCmd XtestA 0r Xxx|$del
1597 edit XtestA " will read text of Xxd instead
1598 call assert_equal('start of Xxx', getline(1))
1599
1600 au BufWriteCmd XtestA call append(line("$"), "write")
1601 write " will append a line to the file
1602 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001603 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001604 call assert_equal('write', getline(4))
1605
1606 " now we have:
1607 " 1 start of Xxx
1608 " 2 abc2
1609 " 3 end of Xxx
1610 " 4 write
1611
1612 au FileReadCmd XtestB '[r Xxx
1613 2r XtestB " will read Xxx below line 2 instead
1614 call assert_equal('start of Xxx', getline(3))
1615
1616 " now we have:
1617 " 1 start of Xxx
1618 " 2 abc2
1619 " 3 start of Xxx
1620 " 4 abc2
1621 " 5 end of Xxx
1622 " 6 end of Xxx
1623 " 7 write
1624
1625 au FileWriteCmd XtestC '[,']copy $
1626 normal 4GA1
1627 4,5w XtestC " will copy lines 4 and 5 to the end
1628 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001629 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001630 call assert_equal("end of Xxx", getline(9))
1631
1632 " now we have:
1633 " 1 start of Xxx
1634 " 2 abc2
1635 " 3 start of Xxx
1636 " 4 abc21
1637 " 5 end of Xxx
1638 " 6 end of Xxx
1639 " 7 write
1640 " 8 abc21
1641 " 9 end of Xxx
1642
1643 let g:lines = []
1644 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1645 w >>XtestD " will add lines to 'lines'
1646 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001647 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001648 call assert_equal(9, line('$'))
1649 call assert_equal('end of Xxx', getline('$'))
1650
1651 au BufReadCmd XtestE 0r Xxx|$del
1652 sp XtestE " split window with test.out
1653 call assert_equal('end of Xxx', getline(3))
1654
1655 let g:lines = []
1656 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1657 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1658 wall " will write other window to 'lines'
1659 call assert_equal(4, len(g:lines), g:lines)
1660 call assert_equal('asdf', g:lines[2])
1661
1662 au! BufReadCmd
1663 au! BufWriteCmd
1664 au! FileReadCmd
1665 au! FileWriteCmd
1666 au! FileAppendCmd
1667 %bwipe!
1668 call delete('Xxx')
1669 enew!
1670endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001671
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001672func s:ReadFile()
1673 setl noswapfile nomodified
1674 let filename = resolve(expand("<afile>:p"))
1675 execute 'read' fnameescape(filename)
1676 1d_
1677 exe 'file' fnameescape(filename)
1678 setl buftype=acwrite
1679endfunc
1680
1681func s:WriteFile()
1682 let filename = resolve(expand("<afile>:p"))
1683 setl buftype=
1684 noautocmd execute 'write' fnameescape(filename)
1685 setl buftype=acwrite
1686 setl nomodified
1687endfunc
1688
1689func Test_BufReadCmd()
1690 autocmd BufReadCmd *.test call s:ReadFile()
1691 autocmd BufWriteCmd *.test call s:WriteFile()
1692
1693 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1694 edit Xcmd.test
1695 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1696 normal! Gofour
1697 write
1698 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1699
1700 bwipe!
1701 call delete('Xcmd.test')
1702 au! BufReadCmd
1703 au! BufWriteCmd
1704endfunc
1705
Bram Moolenaaraace2152017-11-05 16:23:10 +01001706func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001707 exe a:start .. 'mark ['
1708 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001709endfunc
1710
1711" Verify the effects of autocmds on '[ and ']
1712func Test_change_mark_in_autocmds()
1713 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001714 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001715
1716 call SetChangeMarks(2, 3)
1717 write
1718 call assert_equal([1, 4], [line("'["), line("']")])
1719
1720 call SetChangeMarks(2, 3)
1721 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1722 write
1723 au! BufWritePre
1724
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001725 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001726 write XtestFilter
1727 write >> XtestFilter
1728
1729 call SetChangeMarks(2, 3)
1730 " Marks are set to the entire range of the write
1731 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1732 " '[ is adjusted to just before the line that will receive the filtered
1733 " data
1734 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1735 " The filtered data is read into the buffer, and the source lines are
1736 " still present, so the range is after the source lines
1737 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1738 %!cat XtestFilter
1739 " After the filtered data is read, the original lines are deleted
1740 call assert_equal([1, 8], [line("'["), line("']")])
1741 au! FilterWritePre,FilterReadPre,FilterReadPost
1742 undo
1743
1744 call SetChangeMarks(1, 4)
1745 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1746 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1747 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1748 2,3!cat XtestFilter
1749 call assert_equal([2, 9], [line("'["), line("']")])
1750 au! FilterWritePre,FilterReadPre,FilterReadPost
1751 undo
1752
1753 call delete('XtestFilter')
1754 endif
1755
1756 call SetChangeMarks(1, 4)
1757 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1758 2,3write Xtest2
1759 au! FileWritePre
1760
1761 call SetChangeMarks(2, 3)
1762 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1763 write >> Xtest2
1764 au! FileAppendPre
1765
1766 call SetChangeMarks(1, 4)
1767 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1768 2,3write >> Xtest2
1769 au! FileAppendPre
1770
1771 call SetChangeMarks(1, 1)
1772 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1773 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1774 3read Xtest2
1775 au! FileReadPre,FileReadPost
1776 undo
1777
1778 call SetChangeMarks(4, 4)
1779 " When the line is 0, it's adjusted to 1
1780 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1781 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1782 0read Xtest2
1783 au! FileReadPre,FileReadPost
1784 undo
1785
1786 call SetChangeMarks(4, 4)
1787 " When the line is 0, it's adjusted to 1
1788 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1789 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1790 1read Xtest2
1791 au! FileReadPre,FileReadPost
1792 undo
1793
1794 bwipe!
1795 call delete('Xtest')
1796 call delete('Xtest2')
1797endfunc
1798
1799func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01001800 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01001801
1802 enew!
1803 call setline(1, ['a', 'b', 'c', 'd'])
1804
1805 let shelltemp = &shelltemp
1806 set shelltemp
1807
1808 let g:filter_au = 0
1809 au FilterWritePre * let g:filter_au += 1
1810 au FilterReadPre * let g:filter_au += 1
1811 au FilterReadPost * let g:filter_au += 1
1812 %!cat
1813 call assert_equal(3, g:filter_au)
1814
1815 if has('filterpipe')
1816 set noshelltemp
1817
1818 let g:filter_au = 0
1819 au FilterWritePre * let g:filter_au += 1
1820 au FilterReadPre * let g:filter_au += 1
1821 au FilterReadPost * let g:filter_au += 1
1822 %!cat
1823 call assert_equal(0, g:filter_au)
1824 endif
1825
1826 au! FilterWritePre,FilterReadPre,FilterReadPost
1827 let &shelltemp = shelltemp
1828 bwipe!
1829endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001830
1831func Test_TextYankPost()
1832 enew!
1833 call setline(1, ['foo'])
1834
1835 let g:event = []
1836 au TextYankPost * let g:event = copy(v:event)
1837
1838 call assert_equal({}, v:event)
1839 call assert_fails('let v:event = {}', 'E46:')
1840 call assert_fails('let v:event.mykey = 0', 'E742:')
1841
1842 norm "ayiw
1843 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001844 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001845 \g:event)
1846 norm y_
1847 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001848 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false},
1849 \g:event)
1850 norm Vy
1851 call assert_equal(
1852 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001853 \g:event)
1854 call feedkeys("\<C-V>y", 'x')
1855 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001856 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001857 \g:event)
1858 norm "xciwbar
1859 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001860 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001861 \g:event)
1862 norm "bdiw
1863 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001864 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001865 \g:event)
1866
1867 call assert_equal({}, v:event)
1868
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001869 if has('clipboard_working') && !has('gui_running')
1870 " Test that when the visual selection is automatically copied to clipboard
1871 " register a TextYankPost is emitted
1872 call setline(1, ['foobar'])
1873
1874 let @* = ''
1875 set clipboard=autoselect
1876 exe "norm! ggviw\<Esc>"
1877 call assert_equal(
1878 \{'regcontents': ['foobar'], 'regname': '*', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1879 \g:event)
1880
1881 let @+ = ''
1882 set clipboard=autoselectplus
1883 exe "norm! ggviw\<Esc>"
1884 call assert_equal(
1885 \{'regcontents': ['foobar'], 'regname': '+', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1886 \g:event)
1887
1888 set clipboard&vim
1889 endif
1890
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001891 au! TextYankPost
1892 unlet g:event
1893 bwipe!
1894endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001895
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01001896func Test_autocommand_all_events()
1897 call assert_fails('au * * bwipe', 'E1155:')
1898 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00001899 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001900endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001901
1902function s:Before_test_dirchanged()
1903 augroup test_dirchanged
1904 autocmd!
1905 augroup END
1906 let s:li = []
1907 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001908 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001909 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001910 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001911 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001912endfunc
1913
1914function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001915 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001916 call delete(s:dir_foo, 'd')
1917 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001918 augroup test_dirchanged
1919 autocmd!
1920 augroup END
1921endfunc
1922
1923function Test_dirchanged_global()
1924 call s:Before_test_dirchanged()
1925 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1926 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001927 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001928 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001929 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001930 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001931 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001932 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001933 call s:After_test_dirchanged()
1934endfunc
1935
1936function Test_dirchanged_local()
1937 call s:Before_test_dirchanged()
1938 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1939 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001940 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001941 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001942 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001943 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001944 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001945 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001946 call s:After_test_dirchanged()
1947endfunc
1948
1949function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001950 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001951 call s:Before_test_dirchanged()
1952 call test_autochdir()
1953 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1954 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1955 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001956 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001957 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001958 exe 'edit ' . s:dir_foo . '/Xfile'
1959 call assert_equal(s:dir_foo, getcwd())
1960 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001961 set noacd
1962 bwipe!
1963 call s:After_test_dirchanged()
1964endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001965
1966" Test TextChangedI and TextChangedP
1967func Test_ChangedP()
1968 new
1969 call setline(1, ['foo', 'bar', 'foobar'])
1970 call test_override("char_avail", 1)
1971 set complete=. completeopt=menuone
1972
1973 func! TextChangedAutocmd(char)
1974 let g:autocmd .= a:char
1975 endfunc
1976
Christian Brabandtdb3b4462021-10-16 11:58:55 +01001977 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01001978 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1979 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1980 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1981
1982 call cursor(3, 1)
1983 let g:autocmd = ''
1984 call feedkeys("o\<esc>", 'tnix')
1985 call assert_equal('I', g:autocmd)
1986
1987 let g:autocmd = ''
1988 call feedkeys("Sf", 'tnix')
1989 call assert_equal('II', g:autocmd)
1990
1991 let g:autocmd = ''
1992 call feedkeys("Sf\<C-N>", 'tnix')
1993 call assert_equal('IIP', g:autocmd)
1994
1995 let g:autocmd = ''
1996 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1997 call assert_equal('IIPP', g:autocmd)
1998
1999 let g:autocmd = ''
2000 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2001 call assert_equal('IIPPP', g:autocmd)
2002
2003 let g:autocmd = ''
2004 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2005 call assert_equal('IIPPPP', g:autocmd)
2006
2007 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2008 " TODO: how should it handle completeopt=noinsert,noselect?
2009
2010 " CleanUp
2011 call test_override("char_avail", 0)
2012 au! TextChanged
2013 au! TextChangedI
2014 au! TextChangedP
2015 delfu TextChangedAutocmd
2016 unlet! g:autocmd
2017 set complete&vim completeopt&vim
2018
2019 bw!
2020endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002021
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002022let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002023func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002024 if !g:setline_handled
2025 call setline(1, "(x)")
2026 let g:setline_handled = v:true
2027 endif
2028endfunc
2029
2030func Test_TextChangedI_with_setline()
2031 new
2032 call test_override('char_avail', 1)
2033 autocmd TextChangedI <buffer> call SetLineOne()
2034 call feedkeys("i(\<CR>\<Esc>", 'tx')
2035 call assert_equal('(', getline(1))
2036 call assert_equal('x)', getline(2))
2037 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002038 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002039 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002040
Bram Moolenaarca34db32022-01-20 11:17:18 +00002041 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002042 bwipe!
2043endfunc
2044
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002045func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002046 CheckFeature terminal
2047 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002048 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002049 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002050
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002051 " Prepare file for TextChanged event.
2052 call writefile([''], 'Xchanged.txt')
2053 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2054 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002055 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002056 " In ConPTY, there is additional character which is drawn up to the width of
2057 " the screen.
2058 if has('conpty')
2059 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2060 else
2061 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2062 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002063 " It's only adding autocmd, so that no event occurs.
2064 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2065 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002066 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002067 call assert_equal([''], readfile('Xchanged.txt'))
2068
2069 " clean up
2070 call delete('Xchanged.txt')
2071 bwipe!
2072endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002073
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002074func Test_autocmd_nested()
2075 let g:did_nested = 0
2076 augroup Testing
2077 au WinNew * edit somefile
2078 au BufNew * let g:did_nested = 1
2079 augroup END
2080 split
2081 call assert_equal(0, g:did_nested)
2082 close
2083 bwipe! somefile
2084
2085 " old nested argument still works
2086 augroup Testing
2087 au!
2088 au WinNew * nested edit somefile
2089 au BufNew * let g:did_nested = 1
2090 augroup END
2091 split
2092 call assert_equal(1, g:did_nested)
2093 close
2094 bwipe! somefile
2095
2096 " New ++nested argument works
2097 augroup Testing
2098 au!
2099 au WinNew * ++nested edit somefile
2100 au BufNew * let g:did_nested = 1
2101 augroup END
2102 split
2103 call assert_equal(1, g:did_nested)
2104 close
2105 bwipe! somefile
2106
2107 augroup Testing
2108 au!
2109 augroup END
2110
2111 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2112 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2113endfunc
2114
2115func Test_autocmd_once()
2116 " Without ++once WinNew triggers twice
2117 let g:did_split = 0
2118 augroup Testing
2119 au WinNew * let g:did_split += 1
2120 augroup END
2121 split
2122 split
2123 call assert_equal(2, g:did_split)
2124 call assert_true(exists('#WinNew'))
2125 close
2126 close
2127
2128 " With ++once WinNew triggers once
2129 let g:did_split = 0
2130 augroup Testing
2131 au!
2132 au WinNew * ++once let g:did_split += 1
2133 augroup END
2134 split
2135 split
2136 call assert_equal(1, g:did_split)
2137 call assert_false(exists('#WinNew'))
2138 close
2139 close
2140
2141 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2142endfunc
2143
Bram Moolenaara68e5952019-04-25 22:22:01 +02002144func Test_autocmd_bufreadpre()
2145 new
2146 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002147 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002148 w! XAutocmdBufReadPre.txt
2149 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002150 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002151 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002152 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002153 wincmd p
2154 let g:wsv1 = winsaveview()
2155 wincmd p
2156 let g:wsv2 = winsaveview()
2157 " triggers BufReadPre, should not move the cursor in either window
2158 " The topline may change one line in a large window.
2159 edit
2160 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2161 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2162 call assert_equal(2, b:bufreadpre)
2163 wincmd p
2164 call assert_equal(g:wsv1.topline, winsaveview().topline)
2165 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2166 call assert_equal(2, b:bufreadpre)
2167 " Now set the cursor position in an BufReadPre autocommand
2168 " (even though the position will be invalid, this should make Vim reset the
2169 " cursor position in the other window.
2170 wincmd p
2171 set cpo+=g
2172 " won't do anything, but try to set the cursor on an invalid lnum
2173 autocmd BufReadPre <buffer> :norm! 70gg
2174 " triggers BufReadPre, should not move the cursor in either window
2175 e
2176 call assert_equal(1, winsaveview().topline)
2177 call assert_equal(1, winsaveview().lnum)
2178 call assert_equal(3, b:bufreadpre)
2179 wincmd p
2180 call assert_equal(g:wsv1.topline, winsaveview().topline)
2181 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2182 call assert_equal(3, b:bufreadpre)
2183 close
2184 close
2185 call delete('XAutocmdBufReadPre.txt')
2186 set cpo-=g
2187endfunc
2188
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002189" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002190
2191" Tests for the following autocommands:
2192" - FileWritePre writing a compressed file
2193" - FileReadPost reading a compressed file
2194" - BufNewFile reading a file template
2195" - BufReadPre decompressing the file to be read
2196" - FilterReadPre substituting characters in the temp file
2197" - FilterReadPost substituting characters after filtering
2198" - FileReadPre set options for decompression
2199" - FileReadPost decompress the file
2200func Test_ReadWrite_Autocmds()
2201 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002202 CheckUnix
2203 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002204
2205 " Make $GZIP empty, "-v" would cause trouble.
2206 let $GZIP = ""
2207
2208 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2209 " being modified outside of Vim (noticed on Solaris).
2210 au FileChangedShell * echo 'caught FileChangedShell'
2211
2212 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2213 augroup Test1
2214 au!
2215 au FileWritePre *.gz '[,']!gzip
2216 au FileWritePost *.gz undo
2217 au FileReadPost *.gz '[,']!gzip -d
2218 augroup END
2219
2220 new
2221 set bin
2222 call append(0, [
2223 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2224 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2225 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2226 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2227 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2228 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2229 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2230 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2231 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2232 \ ])
2233 1,9write! Xtestfile.gz
2234 enew! | close
2235
2236 new
2237 " Read and decompress the testfile
2238 0read Xtestfile.gz
2239 call assert_equal([
2240 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2241 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2242 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2243 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2244 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2245 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2246 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2247 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2248 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2249 \ ], getline(1, 9))
2250 enew! | close
2251
2252 augroup Test1
2253 au!
2254 augroup END
2255
2256 " Test for the FileAppendPre and FileAppendPost autocmds
2257 augroup Test2
2258 au!
2259 au BufNewFile *.c read Xtest.c
2260 au FileAppendPre *.out '[,']s/new/NEW/
2261 au FileAppendPost *.out !cat Xtest.c >> test.out
2262 augroup END
2263
2264 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2265 new foo.c " should load Xtest.c
2266 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2267 w! >> test.out " append it to the output file
2268
2269 let contents = readfile('test.out')
2270 call assert_equal(' * Here is a NEW .c file', contents[2])
2271 call assert_equal(' * Here is a new .c file', contents[5])
2272
2273 call delete('test.out')
2274 enew! | close
2275 augroup Test2
2276 au!
2277 augroup END
2278
2279 " Test for the BufReadPre and BufReadPost autocmds
2280 augroup Test3
2281 au!
2282 " setup autocommands to decompress before reading and re-compress
2283 " afterwards
2284 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2285 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2286 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2287 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2288 augroup END
2289
2290 e! Xtestfile.gz " Edit compressed file
2291 call assert_equal([
2292 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2293 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2294 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2295 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2296 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2297 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2298 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2299 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2300 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2301 \ ], getline(1, 9))
2302
2303 w! >> test.out " Append it to the output file
2304
2305 augroup Test3
2306 au!
2307 augroup END
2308
2309 " Test for the FilterReadPre and FilterReadPost autocmds.
2310 set shelltemp " need temp files here
2311 augroup Test4
2312 au!
2313 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2314 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2315 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2316 au FilterReadPost *.out '[,']s/x/X/g
2317 augroup END
2318
2319 e! test.out " Edit the output file
2320 1,$!cat
2321 call assert_equal([
2322 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2323 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2324 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2325 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2326 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2327 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2328 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2329 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2330 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2331 \ ], getline(1, 9))
2332 call assert_equal([
2333 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2334 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2335 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2336 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2337 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2338 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2339 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2340 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2341 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2342 \ ], readfile('test.out'))
2343
2344 augroup Test4
2345 au!
2346 augroup END
2347 set shelltemp&vim
2348
2349 " Test for the FileReadPre and FileReadPost autocmds.
2350 augroup Test5
2351 au!
2352 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2353 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2354 au FileReadPost *.gz '[,']s/l/L/
2355 augroup END
2356
2357 new
2358 0r Xtestfile.gz " Read compressed file
2359 call assert_equal([
2360 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2361 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2362 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2363 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2364 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2365 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2366 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2367 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2368 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2369 \ ], getline(1, 9))
2370 call assert_equal([
2371 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2372 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2373 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2374 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2375 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2376 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2377 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2378 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2379 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2380 \ ], readfile('Xtestfile.gz'))
2381
2382 augroup Test5
2383 au!
2384 augroup END
2385
2386 au! FileChangedShell
2387 call delete('Xtestfile.gz')
2388 call delete('Xtest.c')
2389 call delete('test.out')
2390endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002391
2392func Test_throw_in_BufWritePre()
2393 new
2394 call setline(1, ['one', 'two', 'three'])
2395 call assert_false(filereadable('Xthefile'))
2396 augroup throwing
2397 au BufWritePre X* throw 'do not write'
2398 augroup END
2399 try
2400 w Xthefile
2401 catch
2402 let caught = 1
2403 endtry
2404 call assert_equal(1, caught)
2405 call assert_false(filereadable('Xthefile'))
2406
2407 bwipe!
2408 au! throwing
2409endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002410
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002411func Test_autocmd_in_try_block()
2412 call mkdir('Xdir')
2413 au BufEnter * let g:fname = expand('%')
2414 try
2415 edit Xdir/
2416 endtry
2417 call assert_match('Xdir', g:fname)
2418
2419 unlet g:fname
2420 au! BufEnter
2421 call delete('Xdir', 'rf')
2422endfunc
2423
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002424func Test_autocmd_SafeState()
2425 CheckRunVimInTerminal
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002426 let g:test_is_flaky = 1
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002427
2428 let lines =<< trim END
2429 let g:safe = 0
2430 let g:again = ''
2431 au SafeState * let g:safe += 1
2432 au SafeStateAgain * let g:again ..= 'x'
2433 func CallTimer()
2434 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2435 endfunc
2436 END
2437 call writefile(lines, 'XSafeState')
2438 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2439
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002440 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002441 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002442 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002443 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002444
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002445 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002446 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002447 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002448
2449 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002450 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002451 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002452 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002453 call term_sendkeys(buf, ":echo g:again\<CR>")
2454 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2455
2456 call StopVimInTerminal(buf)
2457 call delete('XSafeState')
2458endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002459
2460func Test_autocmd_CmdWinEnter()
2461 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002462 CheckFeature cmdwin
2463
Bram Moolenaar23324a02019-10-01 17:39:04 +02002464 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002465 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002466 let b:dummy_var = 'This is a dummy'
2467 autocmd CmdWinEnter * quit
2468 let winnr = winnr('$')
2469 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002470 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002471 call writefile(lines, filename)
2472 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2473
2474 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002475 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002476 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002477 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002478 call term_sendkeys(buf, ":echo &buftype\<cr>")
2479 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2480 call term_sendkeys(buf, ":echo winnr\<cr>")
2481 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2482
2483 " clean up
2484 call StopVimInTerminal(buf)
2485 call delete(filename)
2486endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002487
2488func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002489 CheckFeature quickfix
2490
Bram Moolenaarec66c412019-10-11 21:19:13 +02002491 pedit xx
2492 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002493 augroup winenter
2494 au WinEnter * if winnr('$') > 2 | quit | endif
2495 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002496 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002497
2498 augroup winenter
2499 au! WinEnter
2500 augroup END
2501
2502 bwipe xx
2503 bwipe x
2504 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002505endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002506
2507func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002508 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002509 edit! Xtest
2510 call setline(1, ['a', 'b', 'c', 'd'])
2511
2512 " :lockmarks preserves the marks
2513 call SetChangeMarks(2, 3)
2514 lockmarks write
2515 call assert_equal([2, 3], [line("'["), line("']")])
2516
2517 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2518 " original values for the user
2519 augroup lockmarks
2520 au!
2521 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2522 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2523 augroup END
2524
2525 lockmarks write
2526 call assert_equal([2, 3], [line("'["), line("']")])
2527
2528 if executable('cat')
2529 lockmarks %!cat
2530 call assert_equal([2, 3], [line("'["), line("']")])
2531 endif
2532
2533 lockmarks 3,4write Xtest2
2534 call assert_equal([2, 3], [line("'["), line("']")])
2535
2536 au! lockmarks
2537 augroup! lockmarks
2538 call delete('Xtest')
2539 call delete('Xtest2')
2540endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002541
2542func Test_FileType_spell()
2543 if !isdirectory('/tmp')
2544 throw "Skipped: requires /tmp directory"
2545 endif
2546
2547 " this was crashing with an invalid free()
2548 setglobal spellfile=/tmp/en.utf-8.add
2549 augroup crash
2550 autocmd!
2551 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2552 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2553 autocmd FileType anotherfiletype setlocal spell
2554 augroup END
2555 func! NoCrash() abort
2556 edit /tmp/crashfile
2557 endfunc
2558 call NoCrash()
2559
2560 au! crash
2561 setglobal spellfile=
2562endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002563
Bram Moolenaar406cd902020-02-18 21:54:41 +01002564" Test closing a window or editing another buffer from a FileChangedRO handler
2565" in a readonly buffer
2566func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002567 call test_override('ui_delay', 10)
2568
Bram Moolenaar406cd902020-02-18 21:54:41 +01002569 augroup FileChangedROTest
2570 au!
2571 autocmd FileChangedRO * quit
2572 augroup END
2573 new
2574 set readonly
2575 call assert_fails('normal i', 'E788:')
2576 close
2577 augroup! FileChangedROTest
2578
2579 augroup FileChangedROTest
2580 au!
2581 autocmd FileChangedRO * edit Xfile
2582 augroup END
2583 new
2584 set readonly
2585 call assert_fails('normal i', 'E788:')
2586 close
2587 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002588 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002589endfunc
2590
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002591func LogACmd()
2592 call add(g:logged, line('$'))
2593endfunc
2594
2595func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002596 CheckNotGui
2597
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002598 enew!
2599 tabnew
2600 call setline(1, ['a', 'b', 'c', 'd'])
2601 $
2602 au TermChanged * call LogACmd()
2603 let g:logged = []
2604 let term_save = &term
2605 set term=xterm
2606 call assert_equal([1, 4], g:logged)
2607
2608 au! TermChanged
2609 let &term = term_save
2610 bwipe!
2611endfunc
2612
Bram Moolenaare3284872020-03-19 13:55:03 +01002613" Test for FileReadCmd autocmd
2614func Test_autocmd_FileReadCmd()
2615 func ReadFileCmd()
2616 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2617 endfunc
2618 augroup FileReadCmdTest
2619 au!
2620 au FileReadCmd Xtest call ReadFileCmd()
2621 augroup END
2622
2623 new
2624 read ++bin Xtest
2625 read ++nobin Xtest
2626 read ++edit Xtest
2627 read ++bad=keep Xtest
2628 read ++bad=drop Xtest
2629 read ++bad=- Xtest
2630 read ++ff=unix Xtest
2631 read ++ff=dos Xtest
2632 read ++ff=mac Xtest
2633 read ++enc=utf-8 Xtest
2634
2635 call assert_equal(['',
2636 \ 'v:cmdarg = ++bin',
2637 \ 'v:cmdarg = ++nobin',
2638 \ 'v:cmdarg = ++edit',
2639 \ 'v:cmdarg = ++bad=keep',
2640 \ 'v:cmdarg = ++bad=drop',
2641 \ 'v:cmdarg = ++bad=-',
2642 \ 'v:cmdarg = ++ff=unix',
2643 \ 'v:cmdarg = ++ff=dos',
2644 \ 'v:cmdarg = ++ff=mac',
2645 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2646
2647 close!
2648 augroup FileReadCmdTest
2649 au!
2650 augroup END
2651 delfunc ReadFileCmd
2652endfunc
2653
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002654" Test for passing invalid arguments to autocmd
2655func Test_autocmd_invalid_args()
2656 " Additional character after * for event
2657 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2658 augroup Test
2659 augroup END
2660 " Invalid autocmd event
2661 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2662 " Invalid autocmd event in a autocmd group
2663 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2664 augroup! Test
2665 " Execute all autocmds
2666 call assert_fails('doautocmd * BufEnter', 'E217:')
2667 call assert_fails('augroup! x1a2b3', 'E367:')
2668 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002669 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002670endfunc
2671
2672" Test for deep nesting of autocmds
2673func Test_autocmd_deep_nesting()
2674 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2675 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2676 autocmd! BufEnter Xfile
2677endfunc
2678
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002679" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2680func Test_autocmd_sigusr1()
2681 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002682 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002683
2684 let g:sigusr1_passed = 0
2685 au SigUSR1 * let g:sigusr1_passed = 1
2686 call system('/bin/kill -s usr1 ' . getpid())
2687 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2688
2689 au! SigUSR1
2690 unlet g:sigusr1_passed
2691endfunc
2692
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002693" Test for BufReadPre autocmd deleting the file
2694func Test_BufReadPre_delfile()
2695 augroup TestAuCmd
2696 au!
2697 autocmd BufReadPre Xfile call delete('Xfile')
2698 augroup END
2699 call writefile([], 'Xfile')
2700 call assert_fails('new Xfile', 'E200:')
2701 call assert_equal('Xfile', @%)
2702 call assert_equal(1, &readonly)
2703 call delete('Xfile')
2704 augroup TestAuCmd
2705 au!
2706 augroup END
2707 close!
2708endfunc
2709
2710" Test for BufReadPre autocmd changing the current buffer
2711func Test_BufReadPre_changebuf()
2712 augroup TestAuCmd
2713 au!
2714 autocmd BufReadPre Xfile edit Xsomeotherfile
2715 augroup END
2716 call writefile([], 'Xfile')
2717 call assert_fails('new Xfile', 'E201:')
2718 call assert_equal('Xsomeotherfile', @%)
2719 call assert_equal(1, &readonly)
2720 call delete('Xfile')
2721 augroup TestAuCmd
2722 au!
2723 augroup END
2724 close!
2725endfunc
2726
2727" Test for BufWipeouti autocmd changing the current buffer when reading a file
2728" in an empty buffer with 'f' flag in 'cpo'
2729func Test_BufDelete_changebuf()
2730 new
2731 augroup TestAuCmd
2732 au!
2733 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2734 augroup END
2735 let save_cpo = &cpo
2736 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002737 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002738 call assert_equal('somefile', @%)
2739 let &cpo = save_cpo
2740 augroup TestAuCmd
2741 au!
2742 augroup END
2743 close!
2744endfunc
2745
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002746" Test for the temporary internal window used to execute autocmds
2747func Test_autocmd_window()
2748 %bw!
2749 edit one.txt
2750 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002751 vnew three.txt
2752 tabnew four.txt
2753 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002754 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002755 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002756 au!
2757 au BufEnter * call add(g:blist, [expand('<afile>'),
2758 \ win_gettype(bufwinnr(expand('<afile>')))])
2759 augroup END
2760
2761 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002762 call assert_equal([
2763 \ ['one.txt', 'autocmd'],
2764 \ ['two.txt', ''],
2765 \ ['four.txt', 'autocmd'],
2766 \ ['three.txt', ''],
2767 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002768
Bram Moolenaar832adf92020-06-25 19:01:36 +02002769 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002770 au!
2771 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002772 augroup! aucmd_win_test1
2773 %bw!
2774endfunc
2775
2776" Test for trying to close the temporary window used for executing an autocmd
2777func Test_close_autocmd_window()
2778 %bw!
2779 edit one.txt
2780 tabnew two.txt
2781 augroup aucmd_win_test2
2782 au!
2783 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2784 augroup END
2785
2786 call assert_fails('doautoall BufEnter', 'E813:')
2787
2788 augroup aucmd_win_test2
2789 au!
2790 augroup END
2791 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002792 %bwipe!
2793endfunc
2794
2795" Test for trying to close the tab that has the temporary window for exeucing
2796" an autocmd.
2797func Test_close_autocmd_tab()
2798 edit one.txt
2799 tabnew two.txt
2800 augroup aucmd_win_test
2801 au!
2802 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2803 augroup END
2804
2805 call assert_fails('doautoall BufEnter', 'E813:')
2806
2807 tabonly
2808 augroup aucmd_win_test
2809 au!
2810 augroup END
2811 augroup! aucmd_win_test
2812 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002813endfunc
2814
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00002815func Test_Visual_doautoall_redraw()
2816 call setline(1, ['a', 'b'])
2817 new
2818 wincmd p
2819 call feedkeys("G\<C-V>", 'txn')
2820 autocmd User Explode ++once redraw
2821 doautoall User Explode
2822 %bwipe!
2823endfunc
2824
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01002825" This was using freed memory.
2826func Test_BufNew_arglocal()
2827 arglocal
2828 au BufNew * arglocal
2829 call assert_fails('drop xx', 'E1156:')
2830
2831 au! BufNew
2832endfunc
2833
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002834func Test_autocmd_closes_window()
2835 au BufNew,BufWinLeave * e %e
2836 file yyy
2837 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002838 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002839
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002840 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002841 au! BufNew
2842 au! BufWinLeave
2843endfunc
2844
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002845func Test_autocmd_quit_psearch()
2846 sn aa bb
2847 augroup aucmd_win_test
2848 au!
2849 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
2850 augroup END
2851 ps /
2852
2853 augroup aucmd_win_test
2854 au!
2855 augroup END
2856endfunc
2857
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002858" Fuzzer found some strange combination that caused a crash.
2859func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002860 " For unknown reason this hangs on MS-Windows
2861 CheckNotMSWindows
2862
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002863 augroup aucmd_normal_test
2864 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
2865 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002866 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002867 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002868 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002869 normal G
2870
2871 augroup aucmd_normal_test
2872 au!
2873 augroup END
2874endfunc
2875
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002876func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002877 " For unknown reason this hangs on MS-Windows
2878 CheckNotMSWindows
2879
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002880 au BufWinLeave * nested q
2881 call assert_fails("norm 7q?\n", 'E855:')
2882
2883 au! BufWinLeave
2884 new
2885 only
2886endfunc
2887
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002888func Test_autocmd_vimgrep()
2889 augroup aucmd_vimgrep
2890 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * sb
2891 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * q9�
2892 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002893 %bwipe!
Bram Moolenaardd07c022021-02-07 13:32:46 +01002894 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002895
2896 augroup aucmd_vimgrep
2897 au!
2898 augroup END
2899endfunc
2900
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002901func Test_autocmd_with_block()
2902 augroup block_testing
2903 au BufReadPost *.xml {
2904 setlocal matchpairs+=<:>
2905 /<start
2906 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02002907 au CursorHold * {
2908 autocmd BufReadPre * ++once echo 'one' | echo 'two'
2909 g:gotSafeState = 77
2910 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002911 augroup END
2912
2913 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
2914 call assert_equal(expected, execute('au BufReadPost *.xml'))
2915
Bram Moolenaar63b91732021-08-05 20:40:03 +02002916 doautocmd CursorHold
2917 call assert_equal(77, g:gotSafeState)
2918 unlet g:gotSafeState
2919
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002920 augroup block_testing
2921 au!
2922 augroup END
2923endfunc
2924
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002925" Test TextChangedI and TextChanged
2926func Test_Changed_ChangedI()
2927 new
2928 call test_override("char_avail", 1)
2929 let [g:autocmd_i, g:autocmd_n] = ['','']
2930
2931 func! TextChangedAutocmdI(char)
2932 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
2933 endfunc
2934
2935 augroup Test_TextChanged
2936 au!
2937 au TextChanged <buffer> :call TextChangedAutocmdI('N')
2938 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
2939 augroup END
2940
2941 call feedkeys("ifoo\<esc>", 'tnix')
2942 " TODO: Test test does not seem to trigger TextChanged autocommand, this
2943 " requires running Vim in a terminal window.
2944 " call assert_equal('N3', g:autocmd_n)
2945 call assert_equal('I3', g:autocmd_i)
2946
2947 call feedkeys("yyp", 'tnix')
2948 " TODO: Test test does not seem to trigger TextChanged autocommand.
2949 " call assert_equal('N4', g:autocmd_n)
2950 call assert_equal('I3', g:autocmd_i)
2951
2952 " CleanUp
2953 call test_override("char_avail", 0)
2954 au! TextChanged <buffer>
2955 au! TextChangedI <buffer>
2956 augroup! Test_TextChanged
2957 delfu TextChangedAutocmdI
2958 unlet! g:autocmd_i g:autocmd_n
2959
2960 bw!
2961endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002962
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002963" vim: shiftwidth=2 sts=2 expandtab