blob: d3c9b89024dbee329847d3a22c7a7b96f13e1a19 [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
Bram Moolenaarf6246f52022-02-11 16:30:12 +00001902func Test_autocmd_user()
1903 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
1904 doautocmd User MyEvent
1905 call assert_equal(['MyEvent', 'MyEvent'], s:res)
1906 au! User
1907 unlet s:res
1908endfunc
1909
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001910function s:Before_test_dirchanged()
1911 augroup test_dirchanged
1912 autocmd!
1913 augroup END
1914 let s:li = []
1915 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001916 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001917 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001918 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001919 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001920endfunc
1921
1922function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001923 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001924 call delete(s:dir_foo, 'd')
1925 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001926 augroup test_dirchanged
1927 autocmd!
1928 augroup END
1929endfunc
1930
1931function Test_dirchanged_global()
1932 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00001933 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001934 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1935 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001936 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00001937 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001938 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001939 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001940 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001941 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001942 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001943 call s:After_test_dirchanged()
1944endfunc
1945
1946function Test_dirchanged_local()
1947 call s:Before_test_dirchanged()
1948 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1949 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001950 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001951 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001952 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001953 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001954 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001955 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001956 call s:After_test_dirchanged()
1957endfunc
1958
1959function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001960 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001961 call s:Before_test_dirchanged()
1962 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001963 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001964 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1965 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1966 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001967 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001968 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001969 exe 'edit ' . s:dir_foo . '/Xfile'
1970 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001971 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
1972 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001973 set noacd
1974 bwipe!
1975 call s:After_test_dirchanged()
1976endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001977
1978" Test TextChangedI and TextChangedP
1979func Test_ChangedP()
1980 new
1981 call setline(1, ['foo', 'bar', 'foobar'])
1982 call test_override("char_avail", 1)
1983 set complete=. completeopt=menuone
1984
1985 func! TextChangedAutocmd(char)
1986 let g:autocmd .= a:char
1987 endfunc
1988
Christian Brabandtdb3b4462021-10-16 11:58:55 +01001989 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01001990 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1991 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1992 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1993
1994 call cursor(3, 1)
1995 let g:autocmd = ''
1996 call feedkeys("o\<esc>", 'tnix')
1997 call assert_equal('I', g:autocmd)
1998
1999 let g:autocmd = ''
2000 call feedkeys("Sf", 'tnix')
2001 call assert_equal('II', g:autocmd)
2002
2003 let g:autocmd = ''
2004 call feedkeys("Sf\<C-N>", 'tnix')
2005 call assert_equal('IIP', g:autocmd)
2006
2007 let g:autocmd = ''
2008 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2009 call assert_equal('IIPP', g:autocmd)
2010
2011 let g:autocmd = ''
2012 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2013 call assert_equal('IIPPP', g:autocmd)
2014
2015 let g:autocmd = ''
2016 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2017 call assert_equal('IIPPPP', g:autocmd)
2018
2019 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2020 " TODO: how should it handle completeopt=noinsert,noselect?
2021
2022 " CleanUp
2023 call test_override("char_avail", 0)
2024 au! TextChanged
2025 au! TextChangedI
2026 au! TextChangedP
2027 delfu TextChangedAutocmd
2028 unlet! g:autocmd
2029 set complete&vim completeopt&vim
2030
2031 bw!
2032endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002033
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002034let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002035func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002036 if !g:setline_handled
2037 call setline(1, "(x)")
2038 let g:setline_handled = v:true
2039 endif
2040endfunc
2041
2042func Test_TextChangedI_with_setline()
2043 new
2044 call test_override('char_avail', 1)
2045 autocmd TextChangedI <buffer> call SetLineOne()
2046 call feedkeys("i(\<CR>\<Esc>", 'tx')
2047 call assert_equal('(', getline(1))
2048 call assert_equal('x)', getline(2))
2049 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002050 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002051 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002052
Bram Moolenaarca34db32022-01-20 11:17:18 +00002053 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002054 bwipe!
2055endfunc
2056
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002057func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002058 CheckFeature terminal
2059 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002060 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002061 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002062
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002063 " Prepare file for TextChanged event.
2064 call writefile([''], 'Xchanged.txt')
2065 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2066 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002067 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002068 " In ConPTY, there is additional character which is drawn up to the width of
2069 " the screen.
2070 if has('conpty')
2071 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2072 else
2073 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2074 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002075 " It's only adding autocmd, so that no event occurs.
2076 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2077 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002078 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002079 call assert_equal([''], readfile('Xchanged.txt'))
2080
2081 " clean up
2082 call delete('Xchanged.txt')
2083 bwipe!
2084endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002085
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002086func Test_autocmd_nested()
2087 let g:did_nested = 0
2088 augroup Testing
2089 au WinNew * edit somefile
2090 au BufNew * let g:did_nested = 1
2091 augroup END
2092 split
2093 call assert_equal(0, g:did_nested)
2094 close
2095 bwipe! somefile
2096
2097 " old nested argument still works
2098 augroup Testing
2099 au!
2100 au WinNew * nested edit somefile
2101 au BufNew * let g:did_nested = 1
2102 augroup END
2103 split
2104 call assert_equal(1, g:did_nested)
2105 close
2106 bwipe! somefile
2107
2108 " New ++nested argument works
2109 augroup Testing
2110 au!
2111 au WinNew * ++nested edit somefile
2112 au BufNew * let g:did_nested = 1
2113 augroup END
2114 split
2115 call assert_equal(1, g:did_nested)
2116 close
2117 bwipe! somefile
2118
2119 augroup Testing
2120 au!
2121 augroup END
2122
2123 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2124 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2125endfunc
2126
2127func Test_autocmd_once()
2128 " Without ++once WinNew triggers twice
2129 let g:did_split = 0
2130 augroup Testing
2131 au WinNew * let g:did_split += 1
2132 augroup END
2133 split
2134 split
2135 call assert_equal(2, g:did_split)
2136 call assert_true(exists('#WinNew'))
2137 close
2138 close
2139
2140 " With ++once WinNew triggers once
2141 let g:did_split = 0
2142 augroup Testing
2143 au!
2144 au WinNew * ++once let g:did_split += 1
2145 augroup END
2146 split
2147 split
2148 call assert_equal(1, g:did_split)
2149 call assert_false(exists('#WinNew'))
2150 close
2151 close
2152
2153 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2154endfunc
2155
Bram Moolenaara68e5952019-04-25 22:22:01 +02002156func Test_autocmd_bufreadpre()
2157 new
2158 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002159 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002160 w! XAutocmdBufReadPre.txt
2161 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002162 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002163 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002164 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002165 wincmd p
2166 let g:wsv1 = winsaveview()
2167 wincmd p
2168 let g:wsv2 = winsaveview()
2169 " triggers BufReadPre, should not move the cursor in either window
2170 " The topline may change one line in a large window.
2171 edit
2172 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2173 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2174 call assert_equal(2, b:bufreadpre)
2175 wincmd p
2176 call assert_equal(g:wsv1.topline, winsaveview().topline)
2177 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2178 call assert_equal(2, b:bufreadpre)
2179 " Now set the cursor position in an BufReadPre autocommand
2180 " (even though the position will be invalid, this should make Vim reset the
2181 " cursor position in the other window.
2182 wincmd p
2183 set cpo+=g
2184 " won't do anything, but try to set the cursor on an invalid lnum
2185 autocmd BufReadPre <buffer> :norm! 70gg
2186 " triggers BufReadPre, should not move the cursor in either window
2187 e
2188 call assert_equal(1, winsaveview().topline)
2189 call assert_equal(1, winsaveview().lnum)
2190 call assert_equal(3, b:bufreadpre)
2191 wincmd p
2192 call assert_equal(g:wsv1.topline, winsaveview().topline)
2193 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2194 call assert_equal(3, b:bufreadpre)
2195 close
2196 close
2197 call delete('XAutocmdBufReadPre.txt')
2198 set cpo-=g
2199endfunc
2200
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002201" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002202
2203" Tests for the following autocommands:
2204" - FileWritePre writing a compressed file
2205" - FileReadPost reading a compressed file
2206" - BufNewFile reading a file template
2207" - BufReadPre decompressing the file to be read
2208" - FilterReadPre substituting characters in the temp file
2209" - FilterReadPost substituting characters after filtering
2210" - FileReadPre set options for decompression
2211" - FileReadPost decompress the file
2212func Test_ReadWrite_Autocmds()
2213 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002214 CheckUnix
2215 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002216
2217 " Make $GZIP empty, "-v" would cause trouble.
2218 let $GZIP = ""
2219
2220 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2221 " being modified outside of Vim (noticed on Solaris).
2222 au FileChangedShell * echo 'caught FileChangedShell'
2223
2224 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2225 augroup Test1
2226 au!
2227 au FileWritePre *.gz '[,']!gzip
2228 au FileWritePost *.gz undo
2229 au FileReadPost *.gz '[,']!gzip -d
2230 augroup END
2231
2232 new
2233 set bin
2234 call append(0, [
2235 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2236 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2237 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2238 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2239 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2240 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2241 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2242 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2243 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2244 \ ])
2245 1,9write! Xtestfile.gz
2246 enew! | close
2247
2248 new
2249 " Read and decompress the testfile
2250 0read Xtestfile.gz
2251 call assert_equal([
2252 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2253 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2254 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2255 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2256 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2257 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2258 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2259 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2260 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2261 \ ], getline(1, 9))
2262 enew! | close
2263
2264 augroup Test1
2265 au!
2266 augroup END
2267
2268 " Test for the FileAppendPre and FileAppendPost autocmds
2269 augroup Test2
2270 au!
2271 au BufNewFile *.c read Xtest.c
2272 au FileAppendPre *.out '[,']s/new/NEW/
2273 au FileAppendPost *.out !cat Xtest.c >> test.out
2274 augroup END
2275
2276 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2277 new foo.c " should load Xtest.c
2278 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2279 w! >> test.out " append it to the output file
2280
2281 let contents = readfile('test.out')
2282 call assert_equal(' * Here is a NEW .c file', contents[2])
2283 call assert_equal(' * Here is a new .c file', contents[5])
2284
2285 call delete('test.out')
2286 enew! | close
2287 augroup Test2
2288 au!
2289 augroup END
2290
2291 " Test for the BufReadPre and BufReadPost autocmds
2292 augroup Test3
2293 au!
2294 " setup autocommands to decompress before reading and re-compress
2295 " afterwards
2296 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2297 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2298 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2299 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2300 augroup END
2301
2302 e! Xtestfile.gz " Edit compressed file
2303 call assert_equal([
2304 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2305 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2306 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2307 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2308 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2309 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2310 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2311 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2312 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2313 \ ], getline(1, 9))
2314
2315 w! >> test.out " Append it to the output file
2316
2317 augroup Test3
2318 au!
2319 augroup END
2320
2321 " Test for the FilterReadPre and FilterReadPost autocmds.
2322 set shelltemp " need temp files here
2323 augroup Test4
2324 au!
2325 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2326 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2327 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2328 au FilterReadPost *.out '[,']s/x/X/g
2329 augroup END
2330
2331 e! test.out " Edit the output file
2332 1,$!cat
2333 call assert_equal([
2334 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2335 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2336 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2337 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2338 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2339 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2340 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2341 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2342 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2343 \ ], getline(1, 9))
2344 call assert_equal([
2345 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2346 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2347 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2348 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2349 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2350 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2351 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2352 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2353 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2354 \ ], readfile('test.out'))
2355
2356 augroup Test4
2357 au!
2358 augroup END
2359 set shelltemp&vim
2360
2361 " Test for the FileReadPre and FileReadPost autocmds.
2362 augroup Test5
2363 au!
2364 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2365 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2366 au FileReadPost *.gz '[,']s/l/L/
2367 augroup END
2368
2369 new
2370 0r Xtestfile.gz " Read compressed file
2371 call assert_equal([
2372 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2373 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2374 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2375 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2376 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2377 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2378 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2379 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2380 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2381 \ ], getline(1, 9))
2382 call assert_equal([
2383 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2384 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2385 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2386 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2387 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2388 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2389 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2390 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2391 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2392 \ ], readfile('Xtestfile.gz'))
2393
2394 augroup Test5
2395 au!
2396 augroup END
2397
2398 au! FileChangedShell
2399 call delete('Xtestfile.gz')
2400 call delete('Xtest.c')
2401 call delete('test.out')
2402endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002403
2404func Test_throw_in_BufWritePre()
2405 new
2406 call setline(1, ['one', 'two', 'three'])
2407 call assert_false(filereadable('Xthefile'))
2408 augroup throwing
2409 au BufWritePre X* throw 'do not write'
2410 augroup END
2411 try
2412 w Xthefile
2413 catch
2414 let caught = 1
2415 endtry
2416 call assert_equal(1, caught)
2417 call assert_false(filereadable('Xthefile'))
2418
2419 bwipe!
2420 au! throwing
2421endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002422
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002423func Test_autocmd_in_try_block()
2424 call mkdir('Xdir')
2425 au BufEnter * let g:fname = expand('%')
2426 try
2427 edit Xdir/
2428 endtry
2429 call assert_match('Xdir', g:fname)
2430
2431 unlet g:fname
2432 au! BufEnter
2433 call delete('Xdir', 'rf')
2434endfunc
2435
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002436func Test_autocmd_SafeState()
2437 CheckRunVimInTerminal
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002438 let g:test_is_flaky = 1
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002439
2440 let lines =<< trim END
2441 let g:safe = 0
2442 let g:again = ''
2443 au SafeState * let g:safe += 1
2444 au SafeStateAgain * let g:again ..= 'x'
2445 func CallTimer()
2446 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2447 endfunc
2448 END
2449 call writefile(lines, 'XSafeState')
2450 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2451
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002452 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002453 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002454 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002455 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002456
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002457 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002458 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002459 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002460
2461 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002462 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002463 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002464 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002465 call term_sendkeys(buf, ":echo g:again\<CR>")
2466 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2467
2468 call StopVimInTerminal(buf)
2469 call delete('XSafeState')
2470endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002471
2472func Test_autocmd_CmdWinEnter()
2473 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002474 CheckFeature cmdwin
2475
Bram Moolenaar23324a02019-10-01 17:39:04 +02002476 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002477 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002478 let b:dummy_var = 'This is a dummy'
2479 autocmd CmdWinEnter * quit
2480 let winnr = winnr('$')
2481 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002482 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002483 call writefile(lines, filename)
2484 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2485
2486 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002487 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002488 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002489 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002490 call term_sendkeys(buf, ":echo &buftype\<cr>")
2491 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2492 call term_sendkeys(buf, ":echo winnr\<cr>")
2493 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2494
2495 " clean up
2496 call StopVimInTerminal(buf)
2497 call delete(filename)
2498endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002499
2500func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002501 CheckFeature quickfix
2502
Bram Moolenaarec66c412019-10-11 21:19:13 +02002503 pedit xx
2504 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002505 augroup winenter
2506 au WinEnter * if winnr('$') > 2 | quit | endif
2507 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002508 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002509
2510 augroup winenter
2511 au! WinEnter
2512 augroup END
2513
2514 bwipe xx
2515 bwipe x
2516 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002517endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002518
2519func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002520 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002521 edit! Xtest
2522 call setline(1, ['a', 'b', 'c', 'd'])
2523
2524 " :lockmarks preserves the marks
2525 call SetChangeMarks(2, 3)
2526 lockmarks write
2527 call assert_equal([2, 3], [line("'["), line("']")])
2528
2529 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2530 " original values for the user
2531 augroup lockmarks
2532 au!
2533 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2534 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2535 augroup END
2536
2537 lockmarks write
2538 call assert_equal([2, 3], [line("'["), line("']")])
2539
2540 if executable('cat')
2541 lockmarks %!cat
2542 call assert_equal([2, 3], [line("'["), line("']")])
2543 endif
2544
2545 lockmarks 3,4write Xtest2
2546 call assert_equal([2, 3], [line("'["), line("']")])
2547
2548 au! lockmarks
2549 augroup! lockmarks
2550 call delete('Xtest')
2551 call delete('Xtest2')
2552endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002553
2554func Test_FileType_spell()
2555 if !isdirectory('/tmp')
2556 throw "Skipped: requires /tmp directory"
2557 endif
2558
2559 " this was crashing with an invalid free()
2560 setglobal spellfile=/tmp/en.utf-8.add
2561 augroup crash
2562 autocmd!
2563 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2564 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2565 autocmd FileType anotherfiletype setlocal spell
2566 augroup END
2567 func! NoCrash() abort
2568 edit /tmp/crashfile
2569 endfunc
2570 call NoCrash()
2571
2572 au! crash
2573 setglobal spellfile=
2574endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002575
Bram Moolenaar406cd902020-02-18 21:54:41 +01002576" Test closing a window or editing another buffer from a FileChangedRO handler
2577" in a readonly buffer
2578func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002579 call test_override('ui_delay', 10)
2580
Bram Moolenaar406cd902020-02-18 21:54:41 +01002581 augroup FileChangedROTest
2582 au!
2583 autocmd FileChangedRO * quit
2584 augroup END
2585 new
2586 set readonly
2587 call assert_fails('normal i', 'E788:')
2588 close
2589 augroup! FileChangedROTest
2590
2591 augroup FileChangedROTest
2592 au!
2593 autocmd FileChangedRO * edit Xfile
2594 augroup END
2595 new
2596 set readonly
2597 call assert_fails('normal i', 'E788:')
2598 close
2599 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002600 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002601endfunc
2602
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002603func LogACmd()
2604 call add(g:logged, line('$'))
2605endfunc
2606
2607func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002608 CheckNotGui
2609
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002610 enew!
2611 tabnew
2612 call setline(1, ['a', 'b', 'c', 'd'])
2613 $
2614 au TermChanged * call LogACmd()
2615 let g:logged = []
2616 let term_save = &term
2617 set term=xterm
2618 call assert_equal([1, 4], g:logged)
2619
2620 au! TermChanged
2621 let &term = term_save
2622 bwipe!
2623endfunc
2624
Bram Moolenaare3284872020-03-19 13:55:03 +01002625" Test for FileReadCmd autocmd
2626func Test_autocmd_FileReadCmd()
2627 func ReadFileCmd()
2628 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2629 endfunc
2630 augroup FileReadCmdTest
2631 au!
2632 au FileReadCmd Xtest call ReadFileCmd()
2633 augroup END
2634
2635 new
2636 read ++bin Xtest
2637 read ++nobin Xtest
2638 read ++edit Xtest
2639 read ++bad=keep Xtest
2640 read ++bad=drop Xtest
2641 read ++bad=- Xtest
2642 read ++ff=unix Xtest
2643 read ++ff=dos Xtest
2644 read ++ff=mac Xtest
2645 read ++enc=utf-8 Xtest
2646
2647 call assert_equal(['',
2648 \ 'v:cmdarg = ++bin',
2649 \ 'v:cmdarg = ++nobin',
2650 \ 'v:cmdarg = ++edit',
2651 \ 'v:cmdarg = ++bad=keep',
2652 \ 'v:cmdarg = ++bad=drop',
2653 \ 'v:cmdarg = ++bad=-',
2654 \ 'v:cmdarg = ++ff=unix',
2655 \ 'v:cmdarg = ++ff=dos',
2656 \ 'v:cmdarg = ++ff=mac',
2657 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2658
2659 close!
2660 augroup FileReadCmdTest
2661 au!
2662 augroup END
2663 delfunc ReadFileCmd
2664endfunc
2665
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002666" Test for passing invalid arguments to autocmd
2667func Test_autocmd_invalid_args()
2668 " Additional character after * for event
2669 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2670 augroup Test
2671 augroup END
2672 " Invalid autocmd event
2673 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2674 " Invalid autocmd event in a autocmd group
2675 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2676 augroup! Test
2677 " Execute all autocmds
2678 call assert_fails('doautocmd * BufEnter', 'E217:')
2679 call assert_fails('augroup! x1a2b3', 'E367:')
2680 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002681 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002682endfunc
2683
2684" Test for deep nesting of autocmds
2685func Test_autocmd_deep_nesting()
2686 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2687 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2688 autocmd! BufEnter Xfile
2689endfunc
2690
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002691" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2692func Test_autocmd_sigusr1()
2693 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002694 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002695
2696 let g:sigusr1_passed = 0
2697 au SigUSR1 * let g:sigusr1_passed = 1
2698 call system('/bin/kill -s usr1 ' . getpid())
2699 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2700
2701 au! SigUSR1
2702 unlet g:sigusr1_passed
2703endfunc
2704
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002705" Test for BufReadPre autocmd deleting the file
2706func Test_BufReadPre_delfile()
2707 augroup TestAuCmd
2708 au!
2709 autocmd BufReadPre Xfile call delete('Xfile')
2710 augroup END
2711 call writefile([], 'Xfile')
2712 call assert_fails('new Xfile', 'E200:')
2713 call assert_equal('Xfile', @%)
2714 call assert_equal(1, &readonly)
2715 call delete('Xfile')
2716 augroup TestAuCmd
2717 au!
2718 augroup END
2719 close!
2720endfunc
2721
2722" Test for BufReadPre autocmd changing the current buffer
2723func Test_BufReadPre_changebuf()
2724 augroup TestAuCmd
2725 au!
2726 autocmd BufReadPre Xfile edit Xsomeotherfile
2727 augroup END
2728 call writefile([], 'Xfile')
2729 call assert_fails('new Xfile', 'E201:')
2730 call assert_equal('Xsomeotherfile', @%)
2731 call assert_equal(1, &readonly)
2732 call delete('Xfile')
2733 augroup TestAuCmd
2734 au!
2735 augroup END
2736 close!
2737endfunc
2738
2739" Test for BufWipeouti autocmd changing the current buffer when reading a file
2740" in an empty buffer with 'f' flag in 'cpo'
2741func Test_BufDelete_changebuf()
2742 new
2743 augroup TestAuCmd
2744 au!
2745 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2746 augroup END
2747 let save_cpo = &cpo
2748 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002749 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002750 call assert_equal('somefile', @%)
2751 let &cpo = save_cpo
2752 augroup TestAuCmd
2753 au!
2754 augroup END
2755 close!
2756endfunc
2757
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002758" Test for the temporary internal window used to execute autocmds
2759func Test_autocmd_window()
2760 %bw!
2761 edit one.txt
2762 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002763 vnew three.txt
2764 tabnew four.txt
2765 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002766 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002767 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002768 au!
2769 au BufEnter * call add(g:blist, [expand('<afile>'),
2770 \ win_gettype(bufwinnr(expand('<afile>')))])
2771 augroup END
2772
2773 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002774 call assert_equal([
2775 \ ['one.txt', 'autocmd'],
2776 \ ['two.txt', ''],
2777 \ ['four.txt', 'autocmd'],
2778 \ ['three.txt', ''],
2779 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002780
Bram Moolenaar832adf92020-06-25 19:01:36 +02002781 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002782 au!
2783 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002784 augroup! aucmd_win_test1
2785 %bw!
2786endfunc
2787
2788" Test for trying to close the temporary window used for executing an autocmd
2789func Test_close_autocmd_window()
2790 %bw!
2791 edit one.txt
2792 tabnew two.txt
2793 augroup aucmd_win_test2
2794 au!
2795 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2796 augroup END
2797
2798 call assert_fails('doautoall BufEnter', 'E813:')
2799
2800 augroup aucmd_win_test2
2801 au!
2802 augroup END
2803 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002804 %bwipe!
2805endfunc
2806
2807" Test for trying to close the tab that has the temporary window for exeucing
2808" an autocmd.
2809func Test_close_autocmd_tab()
2810 edit one.txt
2811 tabnew two.txt
2812 augroup aucmd_win_test
2813 au!
2814 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2815 augroup END
2816
2817 call assert_fails('doautoall BufEnter', 'E813:')
2818
2819 tabonly
2820 augroup aucmd_win_test
2821 au!
2822 augroup END
2823 augroup! aucmd_win_test
2824 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002825endfunc
2826
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00002827func Test_Visual_doautoall_redraw()
2828 call setline(1, ['a', 'b'])
2829 new
2830 wincmd p
2831 call feedkeys("G\<C-V>", 'txn')
2832 autocmd User Explode ++once redraw
2833 doautoall User Explode
2834 %bwipe!
2835endfunc
2836
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01002837" This was using freed memory.
2838func Test_BufNew_arglocal()
2839 arglocal
2840 au BufNew * arglocal
2841 call assert_fails('drop xx', 'E1156:')
2842
2843 au! BufNew
2844endfunc
2845
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002846func Test_autocmd_closes_window()
2847 au BufNew,BufWinLeave * e %e
2848 file yyy
2849 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002850 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002851
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002852 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002853 au! BufNew
2854 au! BufWinLeave
2855endfunc
2856
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002857func Test_autocmd_quit_psearch()
2858 sn aa bb
2859 augroup aucmd_win_test
2860 au!
2861 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
2862 augroup END
2863 ps /
2864
2865 augroup aucmd_win_test
2866 au!
2867 augroup END
2868endfunc
2869
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002870" Fuzzer found some strange combination that caused a crash.
2871func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002872 " For unknown reason this hangs on MS-Windows
2873 CheckNotMSWindows
2874
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002875 augroup aucmd_normal_test
2876 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
2877 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002878 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002879 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002880 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002881 normal G
2882
2883 augroup aucmd_normal_test
2884 au!
2885 augroup END
2886endfunc
2887
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002888func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002889 " For unknown reason this hangs on MS-Windows
2890 CheckNotMSWindows
2891
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002892 au BufWinLeave * nested q
2893 call assert_fails("norm 7q?\n", 'E855:')
2894
2895 au! BufWinLeave
2896 new
2897 only
2898endfunc
2899
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002900func Test_autocmd_vimgrep()
2901 augroup aucmd_vimgrep
2902 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * sb
2903 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * q9�
2904 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002905 %bwipe!
Bram Moolenaardd07c022021-02-07 13:32:46 +01002906 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002907
2908 augroup aucmd_vimgrep
2909 au!
2910 augroup END
2911endfunc
2912
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002913func Test_autocmd_with_block()
2914 augroup block_testing
2915 au BufReadPost *.xml {
2916 setlocal matchpairs+=<:>
2917 /<start
2918 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02002919 au CursorHold * {
2920 autocmd BufReadPre * ++once echo 'one' | echo 'two'
2921 g:gotSafeState = 77
2922 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002923 augroup END
2924
2925 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
2926 call assert_equal(expected, execute('au BufReadPost *.xml'))
2927
Bram Moolenaar63b91732021-08-05 20:40:03 +02002928 doautocmd CursorHold
2929 call assert_equal(77, g:gotSafeState)
2930 unlet g:gotSafeState
2931
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002932 augroup block_testing
2933 au!
2934 augroup END
2935endfunc
2936
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002937" Test TextChangedI and TextChanged
2938func Test_Changed_ChangedI()
2939 new
2940 call test_override("char_avail", 1)
2941 let [g:autocmd_i, g:autocmd_n] = ['','']
2942
2943 func! TextChangedAutocmdI(char)
2944 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
2945 endfunc
2946
2947 augroup Test_TextChanged
2948 au!
2949 au TextChanged <buffer> :call TextChangedAutocmdI('N')
2950 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
2951 augroup END
2952
2953 call feedkeys("ifoo\<esc>", 'tnix')
2954 " TODO: Test test does not seem to trigger TextChanged autocommand, this
2955 " requires running Vim in a terminal window.
2956 " call assert_equal('N3', g:autocmd_n)
2957 call assert_equal('I3', g:autocmd_i)
2958
2959 call feedkeys("yyp", 'tnix')
2960 " TODO: Test test does not seem to trigger TextChanged autocommand.
2961 " call assert_equal('N4', g:autocmd_n)
2962 call assert_equal('I3', g:autocmd_i)
2963
2964 " CleanUp
2965 call test_override("char_avail", 0)
2966 au! TextChanged <buffer>
2967 au! TextChangedI <buffer>
2968 augroup! Test_TextChanged
2969 delfu TextChangedAutocmdI
2970 unlet! g:autocmd_i g:autocmd_n
2971
2972 bw!
2973endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002974
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002975" vim: shiftwidth=2 sts=2 expandtab