blob: 89023a1fb707d9c76b0d4844cc83ba7f4bebe1ac [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 Moolenaar6f2465d2022-03-22 18:13:01 +00006import './vim9.vim' as v9
Bram Moolenaar8c64a362018-03-23 22:39:31 +01007
Bram Moolenaar1e115362019-01-09 23:01:02 +01008func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +02009 for bnr in range(1, bufnr('$'))
10 if bufloaded(bnr) && bufnr('%') != bnr
11 execute 'bd! ' . bnr
12 endif
13 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020014endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020015
Bram Moolenaar14735512016-03-26 21:00:08 +010016func Test_vim_did_enter()
17 call assert_false(v:vim_did_enter)
18
19 " This script will never reach the main loop, can't check if v:vim_did_enter
20 " becomes one.
21endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020022
Bram Moolenaar75911162020-07-21 19:44:47 +020023" Test for the CursorHold autocmd
24func Test_CursorHold_autocmd()
25 CheckRunVimInTerminal
26 call writefile(['one', 'two', 'three'], 'Xfile')
27 let before =<< trim END
28 set updatetime=10
29 au CursorHold * call writefile([line('.')], 'Xoutput', 'a')
30 END
31 call writefile(before, 'Xinit')
32 let buf = RunVimInTerminal('-S Xinit Xfile', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020033 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020034 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020035 call term_sendkeys(buf, "gg")
36 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020037 call WaitForAssert({-> assert_equal(['1'], readfile('Xoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020038 call term_sendkeys(buf, "j")
39 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020040 call WaitForAssert({-> assert_equal(['1', '2'], readfile('Xoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020041 call term_sendkeys(buf, "j")
42 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020043 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('Xoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020044 call StopVimInTerminal(buf)
45
Bram Moolenaar75911162020-07-21 19:44:47 +020046 call delete('Xinit')
47 call delete('Xoutput')
48 call delete('Xfile')
49endfunc
50
Bram Moolenaarc67e8922016-05-24 16:07:40 +020051if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020052
Bram Moolenaarc67e8922016-05-24 16:07:40 +020053 func ExitInsertMode(id)
54 call feedkeys("\<Esc>")
55 endfunc
56
57 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020058 " Need to move the cursor.
59 call feedkeys("ggG", "xt")
60
Bram Moolenaarc67e8922016-05-24 16:07:40 +020061 let g:triggered = 0
62 au CursorHoldI * let g:triggered += 1
63 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010064 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020065 call feedkeys('a', 'x!')
66 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010067 unlet g:triggered
68 au! CursorHoldI
69 set updatetime&
70 endfunc
71
72 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020073 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010074 " Need to move the cursor.
75 call feedkeys("ggG", "xt")
76
77 " Confirm the timer invoked in exit_cb of the job doesn't disturb
78 " CursorHoldI event.
79 let g:triggered = 0
80 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020081 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010082 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020083 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010084 call feedkeys('a', 'x!')
85 call assert_equal(1, g:triggered)
86 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020087 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020088 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020089 endfunc
90
91 func Test_cursorhold_insert_ctrl_x()
92 let g:triggered = 0
93 au CursorHoldI * let g:triggered += 1
94 set updatetime=20
95 call timer_start(100, 'ExitInsertMode')
96 " CursorHoldI does not trigger after CTRL-X
97 call feedkeys("a\<C-X>", 'x!')
98 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010099 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200100 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200101 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200102 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200103
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100104 func Test_cursorhold_insert_ctrl_g_U()
105 au CursorHoldI * :
106 set updatetime=20
107 new
108 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
109 call feedkeys("i()\<C-g>U", 'tx!')
110 sleep 200m
111 call assert_equal('(foo)', getline(1))
112 undo
113 call assert_equal('', getline(1))
114
115 bwipe!
116 au! CursorHoldI
117 set updatetime&
118 endfunc
119
Bram Moolenaar97b00752019-05-12 13:07:14 +0200120 func Test_OptionSet_modeline()
121 call test_override('starting', 1)
122 au! OptionSet
123 augroup set_tabstop
124 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
125 augroup END
126 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline')
127 set modeline
128 let v:errmsg = ''
129 call assert_fails('split XoptionsetModeline', 'E12:')
130 call assert_equal(7, &ts)
131 call assert_equal('', v:errmsg)
132
133 augroup set_tabstop
134 au!
135 augroup END
136 bwipe!
137 set ts&
138 call delete('XoptionsetModeline')
139 call test_override('starting', 0)
140 endfunc
141
142endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200143
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200144func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200145 augroup test_bufunload_group
146 autocmd!
147 autocmd BufUnload * call add(s:li, "bufunload")
148 autocmd BufDelete * call add(s:li, "bufdelete")
149 autocmd BufWipeout * call add(s:li, "bufwipeout")
150 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200151
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100152 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200153 new
154 setlocal bufhidden=
155 bunload
156 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200157
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100158 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200159 new
160 setlocal bufhidden=delete
161 bunload
162 call assert_equal(["bufunload", "bufdelete"], s:li)
163
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100164 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200165 new
166 setlocal bufhidden=unload
167 bwipeout
168 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
169
Bram Moolenaare99e8442016-07-26 20:43:40 +0200170 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200171 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200172endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200173
174" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200175func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200176 tabedit
177 tabfirst
178
179 augroup test_autocmd_bufunload_with_tabnext_group
180 autocmd!
181 autocmd BufUnload <buffer> tabnext
182 augroup END
183
184 quit
185 call assert_equal(2, tabpagenr('$'))
186
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200187 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200188 augroup! test_autocmd_bufunload_with_tabnext_group
189 tablast
190 quit
191endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200192
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100193func Test_argdelete_in_next()
194 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
195 call assert_fails('next a b', 'E1156:')
196 au! BufNew,BufEnter,BufLeave,BufWinEnter *
197endfunc
198
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200199func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200200 tabedit
201 augroup sample
202 autocmd!
203 autocmd BufWinLeave <buffer> tabfirst
204 augroup END
205 call setline(1, ['a', 'b', 'c'])
206 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200207 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200208endfunc
209
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200210" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200211func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200212 split aa.txt
213 let lastbuf = bufnr('$')
214
215 augroup test_autocmd_bufunload
216 autocmd!
217 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
218 augroup END
219
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100220 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200221
222 autocmd! test_autocmd_bufunload
223 augroup! test_autocmd_bufunload
224 bwipe! aa.txt
225 bwipe! bb.txt
226endfunc
227
228" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200229func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200230 setlocal buftype=nowrite
231 let lastbuf = bufnr('$')
232
233 augroup test_autocmd_bufunload
234 autocmd!
235 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
236 augroup END
237
238 normal! i1
239 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200240
241 autocmd! test_autocmd_bufunload
242 augroup! test_autocmd_bufunload
243 bwipe! a.txt
244endfunc
245
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100246func Test_autocmd_dummy_wipeout()
247 " prepare files
248 call writefile([''], 'Xdummywipetest1.txt')
249 call writefile([''], 'Xdummywipetest2.txt')
250 augroup test_bufunload_group
251 autocmd!
252 autocmd BufUnload * call add(s:li, "bufunload")
253 autocmd BufDelete * call add(s:li, "bufdelete")
254 autocmd BufWipeout * call add(s:li, "bufwipeout")
255 augroup END
256
257 let s:li = []
258 split Xdummywipetest1.txt
259 silent! vimgrep /notmatched/ Xdummywipetest*
260 call assert_equal(["bufunload", "bufwipeout"], s:li)
261
262 bwipeout
263 call delete('Xdummywipetest1.txt')
264 call delete('Xdummywipetest2.txt')
265 au! test_bufunload_group
266 augroup! test_bufunload_group
267endfunc
268
Bram Moolenaarc917da42016-07-19 22:31:36 +0200269func Test_win_tab_autocmd()
270 let g:record = []
271
272 augroup testing
273 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000274 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200275 au WinEnter * call add(g:record, 'WinEnter')
276 au WinLeave * call add(g:record, 'WinLeave')
277 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200278 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200279 au TabEnter * call add(g:record, 'TabEnter')
280 au TabLeave * call add(g:record, 'TabLeave')
281 augroup END
282
283 split
284 tabnew
285 close
286 close
287
288 call assert_equal([
289 \ 'WinLeave', 'WinNew', 'WinEnter',
290 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000291 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
292 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200293 \ ], g:record)
294
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200295 let g:record = []
296 tabnew somefile
297 tabnext
298 bwipe somefile
299
300 call assert_equal([
301 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
302 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000303 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200304 \ ], g:record)
305
Bram Moolenaarc917da42016-07-19 22:31:36 +0200306 augroup testing
307 au!
308 augroup END
309 unlet g:record
310endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200311
naohiro ono23beefe2021-11-13 12:38:49 +0000312func Test_WinClosed()
313 " Test that the pattern is matched against the closed window's ID, and both
314 " <amatch> and <afile> are set to it.
315 new
316 let winid = win_getid()
317 let g:matched = v:false
318 augroup test-WinClosed
319 autocmd!
320 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
321 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
322 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
323 augroup END
324 close
325 call assert_true(g:matched)
326 call assert_equal(winid, g:amatch)
327 call assert_equal(winid, g:afile)
328
329 " Test that WinClosed is non-recursive.
330 new
331 new
332 call assert_equal(3, winnr('$'))
333 let g:triggered = 0
334 augroup test-WinClosed
335 autocmd!
336 autocmd WinClosed * let g:triggered += 1
337 autocmd WinClosed * 2 wincmd c
338 augroup END
339 close
340 call assert_equal(1, winnr('$'))
341 call assert_equal(1, g:triggered)
342
343 autocmd! test-WinClosed
344 augroup! test-WinClosed
345 unlet g:matched
346 unlet g:amatch
347 unlet g:afile
348 unlet g:triggered
349endfunc
350
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100351func Test_WinClosed_throws()
352 vnew
353 let bnr = bufnr()
354 call assert_equal(1, bufloaded(bnr))
355 augroup test-WinClosed
356 autocmd WinClosed * throw 'foo'
357 augroup END
358 try
359 close
360 catch /.*/
361 endtry
362 call assert_equal(0, bufloaded(bnr))
363
364 autocmd! test-WinClosed
365 augroup! test-WinClosed
366endfunc
367
Bram Moolenaare99e8442016-07-26 20:43:40 +0200368func s:AddAnAutocmd()
369 augroup vimBarTest
370 au BufReadCmd * echo 'hello'
371 augroup END
372 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
373endfunc
374
375func Test_early_bar()
376 " test that a bar is recognized before the {event}
377 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000378 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200379 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000380 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200381
382 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000383 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200384 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000385 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200386
387 " test that a bar is recognized after the {event}
388 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000389 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200390 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000391 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200392
393 " test that a bar is recognized after the {group}
394 call s:AddAnAutocmd()
395 au! vimBarTest|echo 'hello'
396 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
397endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200398
Bram Moolenaar5c809082016-09-01 16:21:48 +0200399func RemoveGroup()
400 autocmd! StartOK
401 augroup! StartOK
402endfunc
403
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200404func Test_augroup_warning()
405 augroup TheWarning
406 au VimEnter * echo 'entering'
407 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100408 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200409 redir => res
410 augroup! TheWarning
411 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100412 call assert_match("W19:", res)
413 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200414
415 " check "Another" does not take the pace of the deleted entry
416 augroup Another
417 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100418 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200419 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200420
421 " no warning for postpone aucmd delete
422 augroup StartOK
423 au VimEnter * call RemoveGroup()
424 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100425 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200426 redir => res
427 doautocmd VimEnter
428 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100429 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200430 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200431
432 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200433endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200434
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200435func Test_BufReadCmdHelp()
436 " This used to cause access to free memory
437 au BufReadCmd * e +h
438 help
439
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200440 au! BufReadCmd
441endfunc
442
443func Test_BufReadCmdHelpJump()
444 " This used to cause access to free memory
445 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200446 " } to fix highlighting
447 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200448
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200449 au! BufReadCmd
450endfunc
451
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200452func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200453 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200454 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200455 call assert_fails('augroup! x', 'E936:')
456 au VimEnter * echo
457 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200458 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100459 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200460 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200461endfunc
462
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200463" Tests for autocommands on :close command.
464" This used to be in test13.
465func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200466 " Clean up buffers, because in some cases this function fails.
467 call s:cleanup_buffers()
468
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200469 " Write three files and open them, each in a window.
470 " Then go to next window, with autocommand that deletes the previous one.
471 " Do this twice, writing the file.
472 e! Xtestje1
473 call setline(1, 'testje1')
474 w
475 sp Xtestje2
476 call setline(1, 'testje2')
477 w
478 sp Xtestje3
479 call setline(1, 'testje3')
480 w
481 wincmd w
482 au WinLeave Xtestje2 bwipe
483 wincmd w
484 call assert_equal('Xtestje1', expand('%'))
485
486 au WinLeave Xtestje1 bwipe Xtestje3
487 close
488 call assert_equal('Xtestje1', expand('%'))
489
490 " Test deleting the buffer on a Unload event. If this goes wrong there
491 " will be the ATTENTION prompt.
492 e Xtestje1
493 au!
494 au! BufUnload Xtestje1 bwipe
495 call assert_fails('e Xtestje3', 'E937:')
496 call assert_equal('Xtestje3', expand('%'))
497
498 e Xtestje2
499 sp Xtestje1
500 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200501 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200502
503 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
504 " there are ml_line errors and/or a Crash.
505 au!
506 only
507 e Xanother
508 e Xtestje1
509 bwipe Xtestje2
510 bwipe Xtestje3
511 au BufWipeout Xtestje1 buf Xtestje1
512 bwipe
513 call assert_equal('Xanother', expand('%'))
514
515 only
516 help
517 wincmd w
518 1quit
519 call assert_equal('Xanother', expand('%'))
520
521 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100522 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200523 call delete('Xtestje1')
524 call delete('Xtestje2')
525 call delete('Xtestje3')
526endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100527
528func Test_BufEnter()
529 au! BufEnter
530 au Bufenter * let val = val . '+'
531 let g:val = ''
532 split NewFile
533 call assert_equal('+', g:val)
534 bwipe!
535 call assert_equal('++', g:val)
536
537 " Also get BufEnter when editing a directory
538 call mkdir('Xdir')
539 split Xdir
540 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100541
542 " On MS-Windows we can't edit the directory, make sure we wipe the right
543 " buffer.
544 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100545
546 call delete('Xdir', 'd')
547 au! BufEnter
548endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100549
550" Closing a window might cause an endless loop
551" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200552func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200553 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100554 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200555 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100556 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100557 mksession!
558
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200559 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200560 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200561 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100562 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200563 augroup test_autocmd_sessionload
564 autocmd!
565 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
566 augroup END
567
568 func WriteErrors()
569 call writefile([execute("messages")], "Xerrors")
570 endfunc
571 au VimLeave * call WriteErrors()
572 [CODE]
573
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100574 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200575 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100576 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200577 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100578
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100579 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100580 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100581 call delete(file)
582 endfor
583endfunc
584
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100585" Using :blast and :ball for many events caused a crash, because b_nwindows was
586" not incremented correctly.
587func Test_autocmd_blast_badd()
588 let content =<< trim [CODE]
589 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
590 edit foo1
591 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
592 edit foo2
593 call writefile(['OK'], 'Xerrors')
594 qall
595 [CODE]
596
597 call writefile(content, 'XblastBall')
598 call system(GetVimCommand() .. ' --clean -S XblastBall')
599 call assert_match('OK', readfile('Xerrors')->join())
600
601 call delete('XblastBall')
602 call delete('Xerrors')
603endfunc
604
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100605" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200606func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100607 tabnew
608 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100609 mksession!
610
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200611 let content =<< trim [CODE]
612 set nocp noswapfile
613 function! DeleteInactiveBufs()
614 tabfirst
615 let tabblist = []
616 for i in range(1, tabpagenr(''$''))
617 call extend(tabblist, tabpagebuflist(i))
618 endfor
619 for b in range(1, bufnr(''$''))
620 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
621 exec ''bwipeout '' . b
622 endif
623 endfor
624 echomsg "SessionLoadPost DONE"
625 endfunction
626 au SessionLoadPost * call DeleteInactiveBufs()
627
628 func WriteErrors()
629 call writefile([execute("messages")], "Xerrors")
630 endfunc
631 au VimLeave * call WriteErrors()
632 [CODE]
633
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100634 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200635 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100636 let errors = join(readfile('Xerrors'))
637 " This probably only ever matches on unix.
638 call assert_notmatch('Caught deadly signal SEGV', errors)
639 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100640
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100641 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100642 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100643 call delete(file)
644 endfor
645endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200646
647func Test_empty_doau()
648 doau \|
649endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200650
651func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200652 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200653 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200654 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
655 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 +0200656 let g:opt = [expected, actual]
657 "call assert_equal(expected, actual)
658endfunc
659
660func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200661 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200662
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200663 badd test_autocmd.vim
664
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200665 call test_override('starting', 1)
666 set nocp
667 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
668
669 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100670 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200671 set nu
672 call assert_equal([], g:options)
673 call assert_equal(g:opt[0], g:opt[1])
674
675 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100676 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200677 setlocal nonu
678 call assert_equal([], g:options)
679 call assert_equal(g:opt[0], g:opt[1])
680
681 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100682 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200683 setglobal nonu
684 call assert_equal([], g:options)
685 call assert_equal(g:opt[0], g:opt[1])
686
687 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100688 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200689 setlocal ai
690 call assert_equal([], g:options)
691 call assert_equal(g:opt[0], g:opt[1])
692
693 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100694 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200695 setglobal ai
696 call assert_equal([], g:options)
697 call assert_equal(g:opt[0], g:opt[1])
698
699 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100700 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200701 set ai!
702 call assert_equal([], g:options)
703 call assert_equal(g:opt[0], g:opt[1])
704
705 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100706 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200707 noa setlocal ai
708 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200709 set ai!
710 call assert_equal([], g:options)
711 call assert_equal(g:opt[0], g:opt[1])
712
713 " Should not print anything, use :noa
714 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100715 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200716 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200717 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200718 call assert_equal(g:opt[0], g:opt[1])
719
720 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100721 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200722 set list nu
723 call assert_equal([], g:options)
724 call assert_equal(g:opt[0], g:opt[1])
725
726 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100727 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200728 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200729 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 +0200730 call assert_equal(g:opt[0], g:opt[1])
731
732 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100733 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200734 setlocal acd
735 call assert_equal([], g:options)
736 call assert_equal(g:opt[0], g:opt[1])
737
738 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100739 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200740 set ar
741 call assert_equal([], g:options)
742 call assert_equal(g:opt[0], g:opt[1])
743
744 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100745 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200746 setlocal ar
747 call assert_equal([], g:options)
748 call assert_equal(g:opt[0], g:opt[1])
749
750 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100751 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200752 setglobal invar
753 call assert_equal([], g:options)
754 call assert_equal(g:opt[0], g:opt[1])
755
756 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100757 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
758 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200759 call assert_equal([], g:options)
760 call assert_equal(g:opt[0], g:opt[1])
761
762 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100763 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200764 " try twice, first time, shouldn't trigger because option name is invalid,
765 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200766 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200767 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200768 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200769 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200770 call assert_equal([], g:options)
771 call assert_equal(g:opt[0], g:opt[1])
772
773 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100774 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200775 call setwinvar(0, '&number', 1)
776 call assert_equal([], g:options)
777 call assert_equal(g:opt[0], g:opt[1])
778
779 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100780 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200781 setlocal key=blah
782 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200783 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200784 call assert_equal(g:opt[0], g:opt[1])
785
Bram Moolenaard7c96872019-06-15 17:12:48 +0200786
787 " 18a: Setting string global option"
788 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100789 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200790 set backupext=foo
791 call assert_equal([], g:options)
792 call assert_equal(g:opt[0], g:opt[1])
793
794 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100795 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200796 set backupext&
797 call assert_equal([], g:options)
798 call assert_equal(g:opt[0], g:opt[1])
799
800 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100801 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200802 setglobal backupext=bar
803 call assert_equal([], g:options)
804 call assert_equal(g:opt[0], g:opt[1])
805
806 " 18d: Setting local string global option"
807 " As this is a global option this sets the global value even though
808 " :setlocal is used!
809 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100810 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200811 setlocal backupext=baz
812 call assert_equal([], g:options)
813 call assert_equal(g:opt[0], g:opt[1])
814
815 " 18e: Setting again string global option"
816 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
817 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100818 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200819 set backupext=fuu
820 call assert_equal([], g:options)
821 call assert_equal(g:opt[0], g:opt[1])
822
823
zeertzjqb811de52021-10-21 10:50:44 +0100824 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200825 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100826 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200827 set tags=tagpath
828 call assert_equal([], g:options)
829 call assert_equal(g:opt[0], g:opt[1])
830
zeertzjqb811de52021-10-21 10:50:44 +0100831 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100832 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200833 set tags&
834 call assert_equal([], g:options)
835 call assert_equal(g:opt[0], g:opt[1])
836
zeertzjqb811de52021-10-21 10:50:44 +0100837 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100838 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200839 setglobal tags=tagpath1
840 call assert_equal([], g:options)
841 call assert_equal(g:opt[0], g:opt[1])
842
zeertzjqb811de52021-10-21 10:50:44 +0100843 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100844 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200845 setlocal tags=tagpath2
846 call assert_equal([], g:options)
847 call assert_equal(g:opt[0], g:opt[1])
848
zeertzjqb811de52021-10-21 10:50:44 +0100849 " 19e: Setting again string global-local (to buffer) option"
850 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200851 " but the old local value for all other kinds of options.
852 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
853 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100854 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200855 set tags=tagpath
856 call assert_equal([], g:options)
857 call assert_equal(g:opt[0], g:opt[1])
858
zeertzjqb811de52021-10-21 10:50:44 +0100859 " 19f: Setting string global-local (to buffer) option to an empty string"
860 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200861 " but the old local value for all other kinds of options.
862 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
863 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100864 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200865 set tags=tagpath
866 call assert_equal([], g:options)
867 call assert_equal(g:opt[0], g:opt[1])
868
869
870 " 20a: Setting string local (to buffer) option"
871 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100872 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200873 set spelllang=elvish,klingon
874 call assert_equal([], g:options)
875 call assert_equal(g:opt[0], g:opt[1])
876
877 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100878 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200879 set spelllang&
880 call assert_equal([], g:options)
881 call assert_equal(g:opt[0], g:opt[1])
882
883 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100884 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200885 setglobal spelllang=elvish
886 call assert_equal([], g:options)
887 call assert_equal(g:opt[0], g:opt[1])
888
889 " 20d: Setting local string local (to buffer) option"
890 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100891 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200892 setlocal spelllang=klingon
893 call assert_equal([], g:options)
894 call assert_equal(g:opt[0], g:opt[1])
895
896 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +0100897 " 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.
899 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
900 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100901 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200902 set spelllang=foo
903 call assert_equal([], g:options)
904 call assert_equal(g:opt[0], g:opt[1])
905
906
zeertzjqb811de52021-10-21 10:50:44 +0100907 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200908 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100909 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200910 set statusline=foo
911 call assert_equal([], g:options)
912 call assert_equal(g:opt[0], g:opt[1])
913
zeertzjqb811de52021-10-21 10:50:44 +0100914 " 21b: Resetting string global-local (to window) option"
915 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200916 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100917 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200918 set statusline&
919 call assert_equal([], g:options)
920 call assert_equal(g:opt[0], g:opt[1])
921
zeertzjqb811de52021-10-21 10:50:44 +0100922 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100923 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200924 setglobal statusline=bar
925 call assert_equal([], g:options)
926 call assert_equal(g:opt[0], g:opt[1])
927
zeertzjqb811de52021-10-21 10:50:44 +0100928 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200929 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100930 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200931 setlocal statusline=baz
932 call assert_equal([], g:options)
933 call assert_equal(g:opt[0], g:opt[1])
934
zeertzjqb811de52021-10-21 10:50:44 +0100935 " 21e: Setting again string global-local (to window) option"
936 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200937 " but the old local value for all other kinds of options.
938 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
939 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100940 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200941 set statusline=foo
942 call assert_equal([], g:options)
943 call assert_equal(g:opt[0], g:opt[1])
944
945
946 " 22a: Setting string local (to window) option"
947 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100948 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200949 set foldignore=fo
950 call assert_equal([], g:options)
951 call assert_equal(g:opt[0], g:opt[1])
952
953 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100954 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200955 set foldignore&
956 call assert_equal([], g:options)
957 call assert_equal(g:opt[0], g:opt[1])
958
959 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100960 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200961 setglobal foldignore=bar
962 call assert_equal([], g:options)
963 call assert_equal(g:opt[0], g:opt[1])
964
965 " 22d: Setting local string local (to window) option"
966 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100967 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200968 setlocal foldignore=baz
969 call assert_equal([], g:options)
970 call assert_equal(g:opt[0], g:opt[1])
971
972 " 22e: Setting again string local (to window) option"
973 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
974 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100975 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200976 set foldignore=fo
977 call assert_equal([], g:options)
978 call assert_equal(g:opt[0], g:opt[1])
979
980
zeertzjqb811de52021-10-21 10:50:44 +0100981 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200982 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
983 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100984 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200985 setglobal cmdheight=2
986 call assert_equal([], g:options)
987 call assert_equal(g:opt[0], g:opt[1])
988
989 " 23b: Setting local number global option"
990 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
991 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100992 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200993 setlocal cmdheight=2
994 call assert_equal([], g:options)
995 call assert_equal(g:opt[0], g:opt[1])
996
997 " 23c: Setting again number global option"
998 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
999 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001000 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001001 set cmdheight=2
1002 call assert_equal([], g:options)
1003 call assert_equal(g:opt[0], g:opt[1])
1004
1005 " 23d: Setting again number global option"
1006 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001007 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001008 set cmdheight=2
1009 call assert_equal([], g:options)
1010 call assert_equal(g:opt[0], g:opt[1])
1011
1012
1013 " 24a: Setting global number global-local (to buffer) option"
1014 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1015 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001016 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001017 setglobal undolevels=2
1018 call assert_equal([], g:options)
1019 call assert_equal(g:opt[0], g:opt[1])
1020
1021 " 24b: Setting local number global-local (to buffer) option"
1022 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1023 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001024 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001025 setlocal undolevels=2
1026 call assert_equal([], g:options)
1027 call assert_equal(g:opt[0], g:opt[1])
1028
1029 " 24c: Setting again number global-local (to buffer) option"
1030 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1031 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001032 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001033 set undolevels=2
1034 call assert_equal([], g:options)
1035 call assert_equal(g:opt[0], g:opt[1])
1036
1037 " 24d: Setting again global number global-local (to buffer) option"
1038 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001039 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001040 set undolevels=2
1041 call assert_equal([], g:options)
1042 call assert_equal(g:opt[0], g:opt[1])
1043
1044
1045 " 25a: Setting global number local (to buffer) option"
1046 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1047 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001048 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001049 setglobal wrapmargin=2
1050 call assert_equal([], g:options)
1051 call assert_equal(g:opt[0], g:opt[1])
1052
1053 " 25b: Setting local number local (to buffer) option"
1054 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1055 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001056 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001057 setlocal wrapmargin=2
1058 call assert_equal([], g:options)
1059 call assert_equal(g:opt[0], g:opt[1])
1060
1061 " 25c: Setting again number local (to buffer) option"
1062 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1063 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001064 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001065 set wrapmargin=2
1066 call assert_equal([], g:options)
1067 call assert_equal(g:opt[0], g:opt[1])
1068
1069 " 25d: Setting again global number local (to buffer) option"
1070 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001071 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001072 set wrapmargin=2
1073 call assert_equal([], g:options)
1074 call assert_equal(g:opt[0], g:opt[1])
1075
1076
1077 " 26: Setting number global-local (to window) option.
1078 " Such option does currently not exist.
1079
1080
1081 " 27a: Setting global number local (to window) option"
1082 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1083 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001084 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001085 setglobal foldcolumn=2
1086 call assert_equal([], g:options)
1087 call assert_equal(g:opt[0], g:opt[1])
1088
1089 " 27b: Setting local number local (to window) option"
1090 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1091 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001092 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001093 setlocal foldcolumn=2
1094 call assert_equal([], g:options)
1095 call assert_equal(g:opt[0], g:opt[1])
1096
1097 " 27c: Setting again number local (to window) option"
1098 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1099 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001100 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001101 set foldcolumn=2
1102 call assert_equal([], g:options)
1103 call assert_equal(g:opt[0], g:opt[1])
1104
zeertzjqb811de52021-10-21 10:50:44 +01001105 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001106 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001107 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001108 set foldcolumn=2
1109 call assert_equal([], g:options)
1110 call assert_equal(g:opt[0], g:opt[1])
1111
1112
1113 " 28a: Setting global boolean global option"
1114 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1115 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001116 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001117 setglobal nowrapscan
1118 call assert_equal([], g:options)
1119 call assert_equal(g:opt[0], g:opt[1])
1120
1121 " 28b: Setting local boolean global option"
1122 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1123 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001124 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001125 setlocal nowrapscan
1126 call assert_equal([], g:options)
1127 call assert_equal(g:opt[0], g:opt[1])
1128
1129 " 28c: Setting again boolean global option"
1130 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1131 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001132 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001133 set nowrapscan
1134 call assert_equal([], g:options)
1135 call assert_equal(g:opt[0], g:opt[1])
1136
1137 " 28d: Setting again global boolean global option"
1138 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001139 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001140 set wrapscan
1141 call assert_equal([], g:options)
1142 call assert_equal(g:opt[0], g:opt[1])
1143
1144
1145 " 29a: Setting global boolean global-local (to buffer) option"
1146 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1147 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001148 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001149 setglobal autoread
1150 call assert_equal([], g:options)
1151 call assert_equal(g:opt[0], g:opt[1])
1152
1153 " 29b: Setting local boolean global-local (to buffer) option"
1154 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1155 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001156 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001157 setlocal noautoread
1158 call assert_equal([], g:options)
1159 call assert_equal(g:opt[0], g:opt[1])
1160
1161 " 29c: Setting again boolean global-local (to buffer) option"
1162 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1163 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001164 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001165 set autoread
1166 call assert_equal([], g:options)
1167 call assert_equal(g:opt[0], g:opt[1])
1168
1169 " 29d: Setting again global boolean global-local (to buffer) option"
1170 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001171 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001172 set autoread
1173 call assert_equal([], g:options)
1174 call assert_equal(g:opt[0], g:opt[1])
1175
1176
1177 " 30a: Setting global boolean local (to buffer) option"
1178 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1179 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001180 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001181 setglobal cindent
1182 call assert_equal([], g:options)
1183 call assert_equal(g:opt[0], g:opt[1])
1184
1185 " 30b: Setting local boolean local (to buffer) option"
1186 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1187 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001188 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001189 setlocal nocindent
1190 call assert_equal([], g:options)
1191 call assert_equal(g:opt[0], g:opt[1])
1192
1193 " 30c: Setting again boolean local (to buffer) option"
1194 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1195 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001196 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001197 set cindent
1198 call assert_equal([], g:options)
1199 call assert_equal(g:opt[0], g:opt[1])
1200
1201 " 30d: Setting again global boolean local (to buffer) option"
1202 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001203 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001204 set cindent
1205 call assert_equal([], g:options)
1206 call assert_equal(g:opt[0], g:opt[1])
1207
1208
1209 " 31: Setting boolean global-local (to window) option
1210 " Currently no such option exists.
1211
1212
1213 " 32a: Setting global boolean local (to window) option"
1214 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1215 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001216 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001217 setglobal cursorcolumn
1218 call assert_equal([], g:options)
1219 call assert_equal(g:opt[0], g:opt[1])
1220
1221 " 32b: Setting local boolean local (to window) option"
1222 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1223 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001224 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001225 setlocal nocursorcolumn
1226 call assert_equal([], g:options)
1227 call assert_equal(g:opt[0], g:opt[1])
1228
1229 " 32c: Setting again boolean local (to window) option"
1230 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1231 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001232 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001233 set cursorcolumn
1234 call assert_equal([], g:options)
1235 call assert_equal(g:opt[0], g:opt[1])
1236
1237 " 32d: Setting again global boolean local (to window) option"
1238 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001239 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001240 set cursorcolumn
1241 call assert_equal([], g:options)
1242 call assert_equal(g:opt[0], g:opt[1])
1243
1244
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001245 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001246 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001247 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001248 set backspace=2
1249 call assert_equal([], g:options)
1250 call assert_equal(g:opt[0], g:opt[1])
1251
1252
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001253 " Cleanup
1254 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001255 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001256 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 +02001257 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001258 endfor
1259 call test_override('starting', 0)
1260 delfunc! AutoCommandOptionSet
1261endfunc
1262
1263func Test_OptionSet_diffmode()
1264 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001265 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001266 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001267 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001268
1269 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1270 call assert_equal(0, &l:cul)
1271 diffthis
1272 call assert_equal(1, &l:cul)
1273
1274 vnew
1275 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1276 call assert_equal(0, &l:cul)
1277 diffthis
1278 call assert_equal(1, &l:cul)
1279
1280 diffoff
1281 call assert_equal(0, &l:cul)
1282 call assert_equal(1, getwinvar(2, '&l:cul'))
1283 bw!
1284
1285 call assert_equal(1, &l:cul)
1286 diffoff!
1287 call assert_equal(0, &l:cul)
1288 call assert_equal(0, getwinvar(1, '&l:cul'))
1289 bw!
1290
1291 " Cleanup
1292 au! OptionSet
1293 call test_override('starting', 0)
1294endfunc
1295
1296func Test_OptionSet_diffmode_close()
1297 call test_override('starting', 1)
1298 " 19: Try to close the current window when entering diff mode
1299 " should not segfault
1300 new
1301 au OptionSet diff close
1302
1303 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001304 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001305 call assert_equal(1, &diff)
1306 vnew
1307 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001308 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001309 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001310 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001311 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001312 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001313 bw!
1314
1315 " Cleanup
1316 au! OptionSet
1317 call test_override('starting', 0)
1318 "delfunc! AutoCommandOptionSet
1319endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001320
1321" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1322func Test_BufleaveWithDelete()
1323 new | edit Xfile1
1324
1325 augroup test_bufleavewithdelete
1326 autocmd!
1327 autocmd BufLeave Xfile1 bwipe Xfile2
1328 augroup END
1329
1330 call assert_fails('edit Xfile2', 'E143:')
1331 call assert_equal('Xfile1', bufname('%'))
1332
1333 autocmd! test_bufleavewithdelete BufLeave Xfile1
1334 augroup! test_bufleavewithdelete
1335
1336 new
1337 bwipe! Xfile1
1338endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001339
1340" Test for autocommand that changes the buffer list, when doing ":ball".
1341func Test_Acmd_BufAll()
1342 enew!
1343 %bwipe!
1344 call writefile(['Test file Xxx1'], 'Xxx1')
1345 call writefile(['Test file Xxx2'], 'Xxx2')
1346 call writefile(['Test file Xxx3'], 'Xxx3')
1347
1348 " Add three files to the buffer list
1349 split Xxx1
1350 close
1351 split Xxx2
1352 close
1353 split Xxx3
1354 close
1355
1356 " Wipe the buffer when the buffer is opened
1357 au BufReadPost Xxx2 bwipe
1358
1359 call append(0, 'Test file Xxx4')
1360 ball
1361
1362 call assert_equal(2, winnr('$'))
1363 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1364 wincmd t
1365
1366 au! BufReadPost
1367 %bwipe!
1368 call delete('Xxx1')
1369 call delete('Xxx2')
1370 call delete('Xxx3')
1371 enew! | only
1372endfunc
1373
1374" Test for autocommand that changes current buffer on BufEnter event.
1375" Check if modelines are interpreted for the correct buffer.
1376func Test_Acmd_BufEnter()
1377 %bwipe!
1378 call writefile(['start of test file Xxx1',
1379 \ "\<Tab>this is a test",
1380 \ 'end of test file Xxx1'], 'Xxx1')
1381 call writefile(['start of test file Xxx2',
1382 \ 'vim: set noai :',
1383 \ "\<Tab>this is a test",
1384 \ 'end of test file Xxx2'], 'Xxx2')
1385
1386 au BufEnter Xxx2 brew
1387 set ai modeline modelines=3
1388 edit Xxx1
1389 " edit Xxx2, autocmd will do :brew
1390 edit Xxx2
1391 exe "normal G?this is a\<CR>"
1392 " Append text with autoindent to this file
1393 normal othis should be auto-indented
1394 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1395 call assert_equal(3, line('.'))
1396 " Remove autocmd and edit Xxx2 again
1397 au! BufEnter Xxx2
1398 buf! Xxx2
1399 exe "normal G?this is a\<CR>"
1400 " append text without autoindent to Xxx
1401 normal othis should be in column 1
1402 call assert_equal("this should be in column 1", getline('.'))
1403 call assert_equal(4, line('.'))
1404
1405 %bwipe!
1406 call delete('Xxx1')
1407 call delete('Xxx2')
1408 set ai&vim modeline&vim modelines&vim
1409endfunc
1410
1411" Test for issue #57
1412" do not move cursor on <c-o> when autoindent is set
1413func Test_ai_CTRL_O()
1414 enew!
1415 set ai
1416 let save_fo = &fo
1417 set fo+=r
1418 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1419 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1420 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1421
1422 set ai&vim
1423 let &fo = save_fo
1424 enew!
1425endfunc
1426
1427" Test for autocommand that deletes the current buffer on BufLeave event.
1428" Also test deleting the last buffer, should give a new, empty buffer.
1429func Test_BufLeave_Wipe()
1430 %bwipe!
1431 let content = ['start of test file Xxx',
1432 \ 'this is a test',
1433 \ 'end of test file Xxx']
1434 call writefile(content, 'Xxx1')
1435 call writefile(content, 'Xxx2')
1436
1437 au BufLeave Xxx2 bwipe
1438 edit Xxx1
1439 split Xxx2
1440 " delete buffer Xxx2, we should be back to Xxx1
1441 bwipe
1442 call assert_equal('Xxx1', bufname('%'))
1443 call assert_equal(1, winnr('$'))
1444
1445 " Create an alternate buffer
1446 %write! test.out
1447 call assert_equal('test.out', bufname('#'))
1448 " delete alternate buffer
1449 bwipe test.out
1450 call assert_equal('Xxx1', bufname('%'))
1451 call assert_equal('', bufname('#'))
1452
1453 au BufLeave Xxx1 bwipe
1454 " delete current buffer, get an empty one
1455 bwipe!
1456 call assert_equal(1, line('$'))
1457 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001458 let g:bufinfo = getbufinfo()
1459 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001460
1461 call delete('Xxx1')
1462 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001463 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001464 %bwipe
1465 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001466
1467 " check that bufinfo doesn't contain a pointer to freed memory
1468 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001469endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001470
1471func Test_QuitPre()
1472 edit Xfoo
1473 let winid = win_getid(winnr())
1474 split Xbar
1475 au! QuitPre * let g:afile = expand('<afile>')
1476 " Close the other window, <afile> should be correct.
1477 exe win_id2win(winid) . 'q'
1478 call assert_equal('Xfoo', g:afile)
1479
1480 unlet g:afile
1481 bwipe Xfoo
1482 bwipe Xbar
1483endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001484
1485func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001486 au! CmdlineChanged : let g:text = getcmdline()
1487 let g:text = 0
1488 call feedkeys(":echom 'hello'\<CR>", 'xt')
1489 call assert_equal("echom 'hello'", g:text)
1490 au! CmdlineChanged
1491
1492 au! CmdlineChanged : let g:entered = expand('<afile>')
1493 let g:entered = 0
1494 call feedkeys(":echom 'hello'\<CR>", 'xt')
1495 call assert_equal(':', g:entered)
1496 au! CmdlineChanged
1497
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001498 au! CmdlineEnter : let g:entered = expand('<afile>')
1499 au! CmdlineLeave : let g:left = expand('<afile>')
1500 let g:entered = 0
1501 let g:left = 0
1502 call feedkeys(":echo 'hello'\<CR>", 'xt')
1503 call assert_equal(':', g:entered)
1504 call assert_equal(':', g:left)
1505 au! CmdlineEnter
1506 au! CmdlineLeave
1507
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001508 let save_shellslash = &shellslash
1509 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001510 au! CmdlineEnter / let g:entered = expand('<afile>')
1511 au! CmdlineLeave / let g:left = expand('<afile>')
1512 let g:entered = 0
1513 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001514 new
1515 call setline(1, 'hello')
1516 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001517 call assert_equal('/', g:entered)
1518 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001519 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001520 au! CmdlineEnter
1521 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001522 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001523endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001524
1525" Test for BufWritePre autocommand that deletes or unloads the buffer.
1526func Test_BufWritePre()
1527 %bwipe
1528 au BufWritePre Xxx1 bunload
1529 au BufWritePre Xxx2 bwipe
1530
1531 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1532 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1533
1534 edit Xtest
1535 e! Xxx2
1536 bdel Xtest
1537 e Xxx1
1538 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001539 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001540 call assert_equal('Xxx2', bufname('%'))
1541 edit Xtest
1542 e! Xxx2
1543 bwipe Xtest
1544 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001545 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001546 call assert_equal('Xxx1', bufname('%'))
1547 au! BufWritePre
1548 call delete('Xxx1')
1549 call delete('Xxx2')
1550endfunc
1551
1552" Test for BufUnload autocommand that unloads all the other buffers
1553func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001554 let g:test_is_flaky = 1
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001555 call writefile(['Test file Xxx1'], 'Xxx1')"
1556 call writefile(['Test file Xxx2'], 'Xxx2')"
1557
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001558 let content =<< trim [CODE]
1559 func UnloadAllBufs()
1560 let i = 1
1561 while i <= bufnr('$')
1562 if i != bufnr('%') && bufloaded(i)
1563 exe i . 'bunload'
1564 endif
1565 let i += 1
1566 endwhile
1567 endfunc
1568 au BufUnload * call UnloadAllBufs()
1569 au VimLeave * call writefile(['Test Finished'], 'Xout')
1570 edit Xxx1
1571 split Xxx2
1572 q
1573 [CODE]
1574
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001575 call writefile(content, 'Xtest')
1576
1577 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001578 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001579 call assert_true(filereadable('Xout'))
1580
1581 call delete('Xxx1')
1582 call delete('Xxx2')
1583 call delete('Xtest')
1584 call delete('Xout')
1585endfunc
1586
1587" Some tests for buffer-local autocommands
1588func Test_buflocal_autocmd()
1589 let g:bname = ''
1590 edit xx
1591 au BufLeave <buffer> let g:bname = expand("%")
1592 " here, autocommand for xx should trigger.
1593 " but autocommand shall not apply to buffer named <buffer>.
1594 edit somefile
1595 call assert_equal('xx', g:bname)
1596 let g:bname = ''
1597 " here, autocommand shall be auto-deleted
1598 bwipe xx
1599 " autocmd should not trigger
1600 edit xx
1601 call assert_equal('', g:bname)
1602 " autocmd should not trigger
1603 edit somefile
1604 call assert_equal('', g:bname)
1605 enew
1606 unlet g:bname
1607endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001608
1609" Test for "*Cmd" autocommands
1610func Test_Cmd_Autocmds()
1611 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1612
1613 enew!
1614 au BufReadCmd XtestA 0r Xxx|$del
1615 edit XtestA " will read text of Xxd instead
1616 call assert_equal('start of Xxx', getline(1))
1617
1618 au BufWriteCmd XtestA call append(line("$"), "write")
1619 write " will append a line to the file
1620 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001621 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001622 call assert_equal('write', getline(4))
1623
1624 " now we have:
1625 " 1 start of Xxx
1626 " 2 abc2
1627 " 3 end of Xxx
1628 " 4 write
1629
1630 au FileReadCmd XtestB '[r Xxx
1631 2r XtestB " will read Xxx below line 2 instead
1632 call assert_equal('start of Xxx', getline(3))
1633
1634 " now we have:
1635 " 1 start of Xxx
1636 " 2 abc2
1637 " 3 start of Xxx
1638 " 4 abc2
1639 " 5 end of Xxx
1640 " 6 end of Xxx
1641 " 7 write
1642
1643 au FileWriteCmd XtestC '[,']copy $
1644 normal 4GA1
1645 4,5w XtestC " will copy lines 4 and 5 to the end
1646 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001647 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001648 call assert_equal("end of Xxx", getline(9))
1649
1650 " now we have:
1651 " 1 start of Xxx
1652 " 2 abc2
1653 " 3 start of Xxx
1654 " 4 abc21
1655 " 5 end of Xxx
1656 " 6 end of Xxx
1657 " 7 write
1658 " 8 abc21
1659 " 9 end of Xxx
1660
1661 let g:lines = []
1662 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1663 w >>XtestD " will add lines to 'lines'
1664 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001665 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001666 call assert_equal(9, line('$'))
1667 call assert_equal('end of Xxx', getline('$'))
1668
1669 au BufReadCmd XtestE 0r Xxx|$del
1670 sp XtestE " split window with test.out
1671 call assert_equal('end of Xxx', getline(3))
1672
1673 let g:lines = []
1674 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1675 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1676 wall " will write other window to 'lines'
1677 call assert_equal(4, len(g:lines), g:lines)
1678 call assert_equal('asdf', g:lines[2])
1679
1680 au! BufReadCmd
1681 au! BufWriteCmd
1682 au! FileReadCmd
1683 au! FileWriteCmd
1684 au! FileAppendCmd
1685 %bwipe!
1686 call delete('Xxx')
1687 enew!
1688endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001689
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001690func s:ReadFile()
1691 setl noswapfile nomodified
1692 let filename = resolve(expand("<afile>:p"))
1693 execute 'read' fnameescape(filename)
1694 1d_
1695 exe 'file' fnameescape(filename)
1696 setl buftype=acwrite
1697endfunc
1698
1699func s:WriteFile()
1700 let filename = resolve(expand("<afile>:p"))
1701 setl buftype=
1702 noautocmd execute 'write' fnameescape(filename)
1703 setl buftype=acwrite
1704 setl nomodified
1705endfunc
1706
1707func Test_BufReadCmd()
1708 autocmd BufReadCmd *.test call s:ReadFile()
1709 autocmd BufWriteCmd *.test call s:WriteFile()
1710
1711 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1712 edit Xcmd.test
1713 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1714 normal! Gofour
1715 write
1716 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1717
1718 bwipe!
1719 call delete('Xcmd.test')
1720 au! BufReadCmd
1721 au! BufWriteCmd
1722endfunc
1723
Bram Moolenaaraace2152017-11-05 16:23:10 +01001724func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001725 exe a:start .. 'mark ['
1726 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001727endfunc
1728
1729" Verify the effects of autocmds on '[ and ']
1730func Test_change_mark_in_autocmds()
1731 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001732 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001733
1734 call SetChangeMarks(2, 3)
1735 write
1736 call assert_equal([1, 4], [line("'["), line("']")])
1737
1738 call SetChangeMarks(2, 3)
1739 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1740 write
1741 au! BufWritePre
1742
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001743 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001744 write XtestFilter
1745 write >> XtestFilter
1746
1747 call SetChangeMarks(2, 3)
1748 " Marks are set to the entire range of the write
1749 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1750 " '[ is adjusted to just before the line that will receive the filtered
1751 " data
1752 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1753 " The filtered data is read into the buffer, and the source lines are
1754 " still present, so the range is after the source lines
1755 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1756 %!cat XtestFilter
1757 " After the filtered data is read, the original lines are deleted
1758 call assert_equal([1, 8], [line("'["), line("']")])
1759 au! FilterWritePre,FilterReadPre,FilterReadPost
1760 undo
1761
1762 call SetChangeMarks(1, 4)
1763 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1764 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1765 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1766 2,3!cat XtestFilter
1767 call assert_equal([2, 9], [line("'["), line("']")])
1768 au! FilterWritePre,FilterReadPre,FilterReadPost
1769 undo
1770
1771 call delete('XtestFilter')
1772 endif
1773
1774 call SetChangeMarks(1, 4)
1775 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1776 2,3write Xtest2
1777 au! FileWritePre
1778
1779 call SetChangeMarks(2, 3)
1780 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1781 write >> Xtest2
1782 au! FileAppendPre
1783
1784 call SetChangeMarks(1, 4)
1785 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1786 2,3write >> Xtest2
1787 au! FileAppendPre
1788
1789 call SetChangeMarks(1, 1)
1790 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1791 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1792 3read Xtest2
1793 au! FileReadPre,FileReadPost
1794 undo
1795
1796 call SetChangeMarks(4, 4)
1797 " When the line is 0, it's adjusted to 1
1798 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1799 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1800 0read Xtest2
1801 au! FileReadPre,FileReadPost
1802 undo
1803
1804 call SetChangeMarks(4, 4)
1805 " When the line is 0, it's adjusted to 1
1806 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1807 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1808 1read Xtest2
1809 au! FileReadPre,FileReadPost
1810 undo
1811
1812 bwipe!
1813 call delete('Xtest')
1814 call delete('Xtest2')
1815endfunc
1816
1817func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01001818 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01001819
1820 enew!
1821 call setline(1, ['a', 'b', 'c', 'd'])
1822
1823 let shelltemp = &shelltemp
1824 set shelltemp
1825
1826 let g:filter_au = 0
1827 au FilterWritePre * let g:filter_au += 1
1828 au FilterReadPre * let g:filter_au += 1
1829 au FilterReadPost * let g:filter_au += 1
1830 %!cat
1831 call assert_equal(3, g:filter_au)
1832
1833 if has('filterpipe')
1834 set noshelltemp
1835
1836 let g:filter_au = 0
1837 au FilterWritePre * let g:filter_au += 1
1838 au FilterReadPre * let g:filter_au += 1
1839 au FilterReadPost * let g:filter_au += 1
1840 %!cat
1841 call assert_equal(0, g:filter_au)
1842 endif
1843
1844 au! FilterWritePre,FilterReadPre,FilterReadPost
1845 let &shelltemp = shelltemp
1846 bwipe!
1847endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001848
1849func Test_TextYankPost()
1850 enew!
1851 call setline(1, ['foo'])
1852
1853 let g:event = []
1854 au TextYankPost * let g:event = copy(v:event)
1855
1856 call assert_equal({}, v:event)
1857 call assert_fails('let v:event = {}', 'E46:')
1858 call assert_fails('let v:event.mykey = 0', 'E742:')
1859
1860 norm "ayiw
1861 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001862 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001863 \g:event)
1864 norm y_
1865 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001866 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false},
1867 \g:event)
1868 norm Vy
1869 call assert_equal(
1870 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001871 \g:event)
1872 call feedkeys("\<C-V>y", 'x')
1873 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001874 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001875 \g:event)
1876 norm "xciwbar
1877 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001878 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001879 \g:event)
1880 norm "bdiw
1881 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001882 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001883 \g:event)
1884
1885 call assert_equal({}, v:event)
1886
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001887 if has('clipboard_working') && !has('gui_running')
1888 " Test that when the visual selection is automatically copied to clipboard
1889 " register a TextYankPost is emitted
1890 call setline(1, ['foobar'])
1891
1892 let @* = ''
1893 set clipboard=autoselect
1894 exe "norm! ggviw\<Esc>"
1895 call assert_equal(
1896 \{'regcontents': ['foobar'], 'regname': '*', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1897 \g:event)
1898
1899 let @+ = ''
1900 set clipboard=autoselectplus
1901 exe "norm! ggviw\<Esc>"
1902 call assert_equal(
1903 \{'regcontents': ['foobar'], 'regname': '+', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1904 \g:event)
1905
1906 set clipboard&vim
1907 endif
1908
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001909 au! TextYankPost
1910 unlet g:event
1911 bwipe!
1912endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001913
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01001914func Test_autocommand_all_events()
1915 call assert_fails('au * * bwipe', 'E1155:')
1916 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00001917 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001918endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001919
Bram Moolenaarf6246f52022-02-11 16:30:12 +00001920func Test_autocmd_user()
1921 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
1922 doautocmd User MyEvent
1923 call assert_equal(['MyEvent', 'MyEvent'], s:res)
1924 au! User
1925 unlet s:res
1926endfunc
1927
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001928function s:Before_test_dirchanged()
1929 augroup test_dirchanged
1930 autocmd!
1931 augroup END
1932 let s:li = []
1933 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001934 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001935 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001936 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001937 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001938endfunc
1939
1940function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001941 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001942 call delete(s:dir_foo, 'd')
1943 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001944 augroup test_dirchanged
1945 autocmd!
1946 augroup END
1947endfunc
1948
1949function Test_dirchanged_global()
1950 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00001951 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001952 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1953 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001954 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00001955 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001956 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001957 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001958 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001959 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001960 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001961 call s:After_test_dirchanged()
1962endfunc
1963
1964function Test_dirchanged_local()
1965 call s:Before_test_dirchanged()
1966 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1967 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001968 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001969 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001970 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001971 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001972 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001973 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001974 call s:After_test_dirchanged()
1975endfunc
1976
1977function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001978 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001979 call s:Before_test_dirchanged()
1980 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001981 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001982 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1983 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1984 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001985 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001986 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001987 exe 'edit ' . s:dir_foo . '/Xfile'
1988 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001989 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
1990 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001991 set noacd
1992 bwipe!
1993 call s:After_test_dirchanged()
1994endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001995
1996" Test TextChangedI and TextChangedP
1997func Test_ChangedP()
1998 new
1999 call setline(1, ['foo', 'bar', 'foobar'])
2000 call test_override("char_avail", 1)
2001 set complete=. completeopt=menuone
2002
2003 func! TextChangedAutocmd(char)
2004 let g:autocmd .= a:char
2005 endfunc
2006
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002007 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002008 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2009 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2010 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2011
2012 call cursor(3, 1)
2013 let g:autocmd = ''
2014 call feedkeys("o\<esc>", 'tnix')
2015 call assert_equal('I', g:autocmd)
2016
2017 let g:autocmd = ''
2018 call feedkeys("Sf", 'tnix')
2019 call assert_equal('II', g:autocmd)
2020
2021 let g:autocmd = ''
2022 call feedkeys("Sf\<C-N>", 'tnix')
2023 call assert_equal('IIP', g:autocmd)
2024
2025 let g:autocmd = ''
2026 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2027 call assert_equal('IIPP', g:autocmd)
2028
2029 let g:autocmd = ''
2030 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2031 call assert_equal('IIPPP', g:autocmd)
2032
2033 let g:autocmd = ''
2034 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2035 call assert_equal('IIPPPP', g:autocmd)
2036
2037 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2038 " TODO: how should it handle completeopt=noinsert,noselect?
2039
2040 " CleanUp
2041 call test_override("char_avail", 0)
2042 au! TextChanged
2043 au! TextChangedI
2044 au! TextChangedP
2045 delfu TextChangedAutocmd
2046 unlet! g:autocmd
2047 set complete&vim completeopt&vim
2048
2049 bw!
2050endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002051
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002052let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002053func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002054 if !g:setline_handled
2055 call setline(1, "(x)")
2056 let g:setline_handled = v:true
2057 endif
2058endfunc
2059
2060func Test_TextChangedI_with_setline()
2061 new
2062 call test_override('char_avail', 1)
2063 autocmd TextChangedI <buffer> call SetLineOne()
2064 call feedkeys("i(\<CR>\<Esc>", 'tx')
2065 call assert_equal('(', getline(1))
2066 call assert_equal('x)', getline(2))
2067 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002068 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002069 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002070
Bram Moolenaarca34db32022-01-20 11:17:18 +00002071 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002072 bwipe!
2073endfunc
2074
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002075func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002076 CheckFeature terminal
2077 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002078 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002079 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002080
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002081 " Prepare file for TextChanged event.
2082 call writefile([''], 'Xchanged.txt')
2083 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2084 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002085 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002086 " In ConPTY, there is additional character which is drawn up to the width of
2087 " the screen.
2088 if has('conpty')
2089 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2090 else
2091 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2092 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002093 " It's only adding autocmd, so that no event occurs.
2094 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2095 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002096 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002097 call assert_equal([''], readfile('Xchanged.txt'))
2098
2099 " clean up
2100 call delete('Xchanged.txt')
2101 bwipe!
2102endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002103
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002104func Test_autocmd_nested()
2105 let g:did_nested = 0
2106 augroup Testing
2107 au WinNew * edit somefile
2108 au BufNew * let g:did_nested = 1
2109 augroup END
2110 split
2111 call assert_equal(0, g:did_nested)
2112 close
2113 bwipe! somefile
2114
2115 " old nested argument still works
2116 augroup Testing
2117 au!
2118 au WinNew * nested edit somefile
2119 au BufNew * let g:did_nested = 1
2120 augroup END
2121 split
2122 call assert_equal(1, g:did_nested)
2123 close
2124 bwipe! somefile
2125
2126 " New ++nested argument works
2127 augroup Testing
2128 au!
2129 au WinNew * ++nested edit somefile
2130 au BufNew * let g:did_nested = 1
2131 augroup END
2132 split
2133 call assert_equal(1, g:did_nested)
2134 close
2135 bwipe! somefile
2136
Bram Moolenaarf0775142022-03-04 20:10:38 +00002137 " nested without ++ does not work in Vim9 script
2138 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2139
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002140 augroup Testing
2141 au!
2142 augroup END
2143
2144 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2145 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2146endfunc
2147
2148func Test_autocmd_once()
2149 " Without ++once WinNew triggers twice
2150 let g:did_split = 0
2151 augroup Testing
2152 au WinNew * let g:did_split += 1
2153 augroup END
2154 split
2155 split
2156 call assert_equal(2, g:did_split)
2157 call assert_true(exists('#WinNew'))
2158 close
2159 close
2160
2161 " With ++once WinNew triggers once
2162 let g:did_split = 0
2163 augroup Testing
2164 au!
2165 au WinNew * ++once let g:did_split += 1
2166 augroup END
2167 split
2168 split
2169 call assert_equal(1, g:did_split)
2170 call assert_false(exists('#WinNew'))
2171 close
2172 close
2173
2174 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2175endfunc
2176
Bram Moolenaara68e5952019-04-25 22:22:01 +02002177func Test_autocmd_bufreadpre()
2178 new
2179 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002180 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002181 w! XAutocmdBufReadPre.txt
2182 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002183 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002184 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002185 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002186 wincmd p
2187 let g:wsv1 = winsaveview()
2188 wincmd p
2189 let g:wsv2 = winsaveview()
2190 " triggers BufReadPre, should not move the cursor in either window
2191 " The topline may change one line in a large window.
2192 edit
2193 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2194 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2195 call assert_equal(2, b:bufreadpre)
2196 wincmd p
2197 call assert_equal(g:wsv1.topline, winsaveview().topline)
2198 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2199 call assert_equal(2, b:bufreadpre)
2200 " Now set the cursor position in an BufReadPre autocommand
2201 " (even though the position will be invalid, this should make Vim reset the
2202 " cursor position in the other window.
2203 wincmd p
2204 set cpo+=g
2205 " won't do anything, but try to set the cursor on an invalid lnum
2206 autocmd BufReadPre <buffer> :norm! 70gg
2207 " triggers BufReadPre, should not move the cursor in either window
2208 e
2209 call assert_equal(1, winsaveview().topline)
2210 call assert_equal(1, winsaveview().lnum)
2211 call assert_equal(3, b:bufreadpre)
2212 wincmd p
2213 call assert_equal(g:wsv1.topline, winsaveview().topline)
2214 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2215 call assert_equal(3, b:bufreadpre)
2216 close
2217 close
2218 call delete('XAutocmdBufReadPre.txt')
2219 set cpo-=g
2220endfunc
2221
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002222" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002223
2224" Tests for the following autocommands:
2225" - FileWritePre writing a compressed file
2226" - FileReadPost reading a compressed file
2227" - BufNewFile reading a file template
2228" - BufReadPre decompressing the file to be read
2229" - FilterReadPre substituting characters in the temp file
2230" - FilterReadPost substituting characters after filtering
2231" - FileReadPre set options for decompression
2232" - FileReadPost decompress the file
2233func Test_ReadWrite_Autocmds()
2234 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002235 CheckUnix
2236 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002237
2238 " Make $GZIP empty, "-v" would cause trouble.
2239 let $GZIP = ""
2240
2241 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2242 " being modified outside of Vim (noticed on Solaris).
2243 au FileChangedShell * echo 'caught FileChangedShell'
2244
2245 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2246 augroup Test1
2247 au!
2248 au FileWritePre *.gz '[,']!gzip
2249 au FileWritePost *.gz undo
2250 au FileReadPost *.gz '[,']!gzip -d
2251 augroup END
2252
2253 new
2254 set bin
2255 call append(0, [
2256 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2257 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2258 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2259 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2260 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2261 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2262 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2263 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2264 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2265 \ ])
2266 1,9write! Xtestfile.gz
2267 enew! | close
2268
2269 new
2270 " Read and decompress the testfile
2271 0read Xtestfile.gz
2272 call assert_equal([
2273 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2274 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2275 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2276 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2277 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2278 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2279 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2280 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2281 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2282 \ ], getline(1, 9))
2283 enew! | close
2284
2285 augroup Test1
2286 au!
2287 augroup END
2288
2289 " Test for the FileAppendPre and FileAppendPost autocmds
2290 augroup Test2
2291 au!
2292 au BufNewFile *.c read Xtest.c
2293 au FileAppendPre *.out '[,']s/new/NEW/
2294 au FileAppendPost *.out !cat Xtest.c >> test.out
2295 augroup END
2296
2297 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2298 new foo.c " should load Xtest.c
2299 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2300 w! >> test.out " append it to the output file
2301
2302 let contents = readfile('test.out')
2303 call assert_equal(' * Here is a NEW .c file', contents[2])
2304 call assert_equal(' * Here is a new .c file', contents[5])
2305
2306 call delete('test.out')
2307 enew! | close
2308 augroup Test2
2309 au!
2310 augroup END
2311
2312 " Test for the BufReadPre and BufReadPost autocmds
2313 augroup Test3
2314 au!
2315 " setup autocommands to decompress before reading and re-compress
2316 " afterwards
2317 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2318 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2319 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2320 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2321 augroup END
2322
2323 e! Xtestfile.gz " Edit compressed file
2324 call assert_equal([
2325 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2326 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2327 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2328 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2329 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2330 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2331 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2332 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2333 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2334 \ ], getline(1, 9))
2335
2336 w! >> test.out " Append it to the output file
2337
2338 augroup Test3
2339 au!
2340 augroup END
2341
2342 " Test for the FilterReadPre and FilterReadPost autocmds.
2343 set shelltemp " need temp files here
2344 augroup Test4
2345 au!
2346 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2347 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2348 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2349 au FilterReadPost *.out '[,']s/x/X/g
2350 augroup END
2351
2352 e! test.out " Edit the output file
2353 1,$!cat
2354 call assert_equal([
2355 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2356 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2357 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2358 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2359 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2360 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2361 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2362 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2363 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2364 \ ], getline(1, 9))
2365 call assert_equal([
2366 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2367 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2368 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2369 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2370 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2371 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2372 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2373 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2374 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2375 \ ], readfile('test.out'))
2376
2377 augroup Test4
2378 au!
2379 augroup END
2380 set shelltemp&vim
2381
2382 " Test for the FileReadPre and FileReadPost autocmds.
2383 augroup Test5
2384 au!
2385 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2386 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2387 au FileReadPost *.gz '[,']s/l/L/
2388 augroup END
2389
2390 new
2391 0r Xtestfile.gz " Read compressed file
2392 call assert_equal([
2393 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2394 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2395 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2396 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2397 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2398 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2399 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2400 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2401 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2402 \ ], getline(1, 9))
2403 call assert_equal([
2404 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2405 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2406 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2407 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2408 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2409 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2410 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2411 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2412 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2413 \ ], readfile('Xtestfile.gz'))
2414
2415 augroup Test5
2416 au!
2417 augroup END
2418
2419 au! FileChangedShell
2420 call delete('Xtestfile.gz')
2421 call delete('Xtest.c')
2422 call delete('test.out')
2423endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002424
2425func Test_throw_in_BufWritePre()
2426 new
2427 call setline(1, ['one', 'two', 'three'])
2428 call assert_false(filereadable('Xthefile'))
2429 augroup throwing
2430 au BufWritePre X* throw 'do not write'
2431 augroup END
2432 try
2433 w Xthefile
2434 catch
2435 let caught = 1
2436 endtry
2437 call assert_equal(1, caught)
2438 call assert_false(filereadable('Xthefile'))
2439
2440 bwipe!
2441 au! throwing
2442endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002443
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002444func Test_autocmd_in_try_block()
2445 call mkdir('Xdir')
2446 au BufEnter * let g:fname = expand('%')
2447 try
2448 edit Xdir/
2449 endtry
2450 call assert_match('Xdir', g:fname)
2451
2452 unlet g:fname
2453 au! BufEnter
2454 call delete('Xdir', 'rf')
2455endfunc
2456
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002457func Test_autocmd_SafeState()
2458 CheckRunVimInTerminal
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002459 let g:test_is_flaky = 1
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002460
2461 let lines =<< trim END
2462 let g:safe = 0
2463 let g:again = ''
2464 au SafeState * let g:safe += 1
2465 au SafeStateAgain * let g:again ..= 'x'
2466 func CallTimer()
2467 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2468 endfunc
2469 END
2470 call writefile(lines, 'XSafeState')
2471 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2472
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002473 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002474 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002475 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002476 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002477
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002478 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002479 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002480 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002481
2482 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002483 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002484 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002485 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002486 call term_sendkeys(buf, ":echo g:again\<CR>")
2487 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2488
2489 call StopVimInTerminal(buf)
2490 call delete('XSafeState')
2491endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002492
2493func Test_autocmd_CmdWinEnter()
2494 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002495 CheckFeature cmdwin
2496
Bram Moolenaar23324a02019-10-01 17:39:04 +02002497 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002498 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002499 let b:dummy_var = 'This is a dummy'
2500 autocmd CmdWinEnter * quit
2501 let winnr = winnr('$')
2502 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002503 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002504 call writefile(lines, filename)
2505 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2506
2507 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002508 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002509 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002510 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002511 call term_sendkeys(buf, ":echo &buftype\<cr>")
2512 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2513 call term_sendkeys(buf, ":echo winnr\<cr>")
2514 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2515
2516 " clean up
2517 call StopVimInTerminal(buf)
2518 call delete(filename)
2519endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002520
2521func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002522 CheckFeature quickfix
2523
Bram Moolenaarec66c412019-10-11 21:19:13 +02002524 pedit xx
2525 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002526 augroup winenter
2527 au WinEnter * if winnr('$') > 2 | quit | endif
2528 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002529 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002530
2531 augroup winenter
2532 au! WinEnter
2533 augroup END
2534
2535 bwipe xx
2536 bwipe x
2537 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002538endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002539
2540func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002541 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002542 edit! Xtest
2543 call setline(1, ['a', 'b', 'c', 'd'])
2544
2545 " :lockmarks preserves the marks
2546 call SetChangeMarks(2, 3)
2547 lockmarks write
2548 call assert_equal([2, 3], [line("'["), line("']")])
2549
2550 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2551 " original values for the user
2552 augroup lockmarks
2553 au!
2554 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2555 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2556 augroup END
2557
2558 lockmarks write
2559 call assert_equal([2, 3], [line("'["), line("']")])
2560
2561 if executable('cat')
2562 lockmarks %!cat
2563 call assert_equal([2, 3], [line("'["), line("']")])
2564 endif
2565
2566 lockmarks 3,4write Xtest2
2567 call assert_equal([2, 3], [line("'["), line("']")])
2568
2569 au! lockmarks
2570 augroup! lockmarks
2571 call delete('Xtest')
2572 call delete('Xtest2')
2573endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002574
2575func Test_FileType_spell()
2576 if !isdirectory('/tmp')
2577 throw "Skipped: requires /tmp directory"
2578 endif
2579
2580 " this was crashing with an invalid free()
2581 setglobal spellfile=/tmp/en.utf-8.add
2582 augroup crash
2583 autocmd!
2584 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2585 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2586 autocmd FileType anotherfiletype setlocal spell
2587 augroup END
2588 func! NoCrash() abort
2589 edit /tmp/crashfile
2590 endfunc
2591 call NoCrash()
2592
2593 au! crash
2594 setglobal spellfile=
2595endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002596
Bram Moolenaar406cd902020-02-18 21:54:41 +01002597" Test closing a window or editing another buffer from a FileChangedRO handler
2598" in a readonly buffer
2599func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002600 call test_override('ui_delay', 10)
2601
Bram Moolenaar406cd902020-02-18 21:54:41 +01002602 augroup FileChangedROTest
2603 au!
2604 autocmd FileChangedRO * quit
2605 augroup END
2606 new
2607 set readonly
2608 call assert_fails('normal i', 'E788:')
2609 close
2610 augroup! FileChangedROTest
2611
2612 augroup FileChangedROTest
2613 au!
2614 autocmd FileChangedRO * edit Xfile
2615 augroup END
2616 new
2617 set readonly
2618 call assert_fails('normal i', 'E788:')
2619 close
2620 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002621 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002622endfunc
2623
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002624func LogACmd()
2625 call add(g:logged, line('$'))
2626endfunc
2627
2628func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002629 CheckNotGui
2630
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002631 enew!
2632 tabnew
2633 call setline(1, ['a', 'b', 'c', 'd'])
2634 $
2635 au TermChanged * call LogACmd()
2636 let g:logged = []
2637 let term_save = &term
2638 set term=xterm
2639 call assert_equal([1, 4], g:logged)
2640
2641 au! TermChanged
2642 let &term = term_save
2643 bwipe!
2644endfunc
2645
Bram Moolenaare3284872020-03-19 13:55:03 +01002646" Test for FileReadCmd autocmd
2647func Test_autocmd_FileReadCmd()
2648 func ReadFileCmd()
2649 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2650 endfunc
2651 augroup FileReadCmdTest
2652 au!
2653 au FileReadCmd Xtest call ReadFileCmd()
2654 augroup END
2655
2656 new
2657 read ++bin Xtest
2658 read ++nobin Xtest
2659 read ++edit Xtest
2660 read ++bad=keep Xtest
2661 read ++bad=drop Xtest
2662 read ++bad=- Xtest
2663 read ++ff=unix Xtest
2664 read ++ff=dos Xtest
2665 read ++ff=mac Xtest
2666 read ++enc=utf-8 Xtest
2667
2668 call assert_equal(['',
2669 \ 'v:cmdarg = ++bin',
2670 \ 'v:cmdarg = ++nobin',
2671 \ 'v:cmdarg = ++edit',
2672 \ 'v:cmdarg = ++bad=keep',
2673 \ 'v:cmdarg = ++bad=drop',
2674 \ 'v:cmdarg = ++bad=-',
2675 \ 'v:cmdarg = ++ff=unix',
2676 \ 'v:cmdarg = ++ff=dos',
2677 \ 'v:cmdarg = ++ff=mac',
2678 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2679
2680 close!
2681 augroup FileReadCmdTest
2682 au!
2683 augroup END
2684 delfunc ReadFileCmd
2685endfunc
2686
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002687" Test for passing invalid arguments to autocmd
2688func Test_autocmd_invalid_args()
2689 " Additional character after * for event
2690 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2691 augroup Test
2692 augroup END
2693 " Invalid autocmd event
2694 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2695 " Invalid autocmd event in a autocmd group
2696 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2697 augroup! Test
2698 " Execute all autocmds
2699 call assert_fails('doautocmd * BufEnter', 'E217:')
2700 call assert_fails('augroup! x1a2b3', 'E367:')
2701 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002702 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002703endfunc
2704
2705" Test for deep nesting of autocmds
2706func Test_autocmd_deep_nesting()
2707 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2708 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2709 autocmd! BufEnter Xfile
2710endfunc
2711
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002712" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2713func Test_autocmd_sigusr1()
2714 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002715 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002716
2717 let g:sigusr1_passed = 0
2718 au SigUSR1 * let g:sigusr1_passed = 1
2719 call system('/bin/kill -s usr1 ' . getpid())
2720 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2721
2722 au! SigUSR1
2723 unlet g:sigusr1_passed
2724endfunc
2725
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002726" Test for BufReadPre autocmd deleting the file
2727func Test_BufReadPre_delfile()
2728 augroup TestAuCmd
2729 au!
2730 autocmd BufReadPre Xfile call delete('Xfile')
2731 augroup END
2732 call writefile([], 'Xfile')
2733 call assert_fails('new Xfile', 'E200:')
2734 call assert_equal('Xfile', @%)
2735 call assert_equal(1, &readonly)
2736 call delete('Xfile')
2737 augroup TestAuCmd
2738 au!
2739 augroup END
2740 close!
2741endfunc
2742
2743" Test for BufReadPre autocmd changing the current buffer
2744func Test_BufReadPre_changebuf()
2745 augroup TestAuCmd
2746 au!
2747 autocmd BufReadPre Xfile edit Xsomeotherfile
2748 augroup END
2749 call writefile([], 'Xfile')
2750 call assert_fails('new Xfile', 'E201:')
2751 call assert_equal('Xsomeotherfile', @%)
2752 call assert_equal(1, &readonly)
2753 call delete('Xfile')
2754 augroup TestAuCmd
2755 au!
2756 augroup END
2757 close!
2758endfunc
2759
2760" Test for BufWipeouti autocmd changing the current buffer when reading a file
2761" in an empty buffer with 'f' flag in 'cpo'
2762func Test_BufDelete_changebuf()
2763 new
2764 augroup TestAuCmd
2765 au!
2766 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2767 augroup END
2768 let save_cpo = &cpo
2769 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002770 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002771 call assert_equal('somefile', @%)
2772 let &cpo = save_cpo
2773 augroup TestAuCmd
2774 au!
2775 augroup END
2776 close!
2777endfunc
2778
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002779" Test for the temporary internal window used to execute autocmds
2780func Test_autocmd_window()
2781 %bw!
2782 edit one.txt
2783 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002784 vnew three.txt
2785 tabnew four.txt
2786 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002787 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002788 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002789 au!
2790 au BufEnter * call add(g:blist, [expand('<afile>'),
2791 \ win_gettype(bufwinnr(expand('<afile>')))])
2792 augroup END
2793
2794 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002795 call assert_equal([
2796 \ ['one.txt', 'autocmd'],
2797 \ ['two.txt', ''],
2798 \ ['four.txt', 'autocmd'],
2799 \ ['three.txt', ''],
2800 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002801
Bram Moolenaar832adf92020-06-25 19:01:36 +02002802 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002803 au!
2804 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002805 augroup! aucmd_win_test1
2806 %bw!
2807endfunc
2808
2809" Test for trying to close the temporary window used for executing an autocmd
2810func Test_close_autocmd_window()
2811 %bw!
2812 edit one.txt
2813 tabnew two.txt
2814 augroup aucmd_win_test2
2815 au!
2816 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2817 augroup END
2818
2819 call assert_fails('doautoall BufEnter', 'E813:')
2820
2821 augroup aucmd_win_test2
2822 au!
2823 augroup END
2824 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002825 %bwipe!
2826endfunc
2827
2828" Test for trying to close the tab that has the temporary window for exeucing
2829" an autocmd.
2830func Test_close_autocmd_tab()
2831 edit one.txt
2832 tabnew two.txt
2833 augroup aucmd_win_test
2834 au!
2835 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2836 augroup END
2837
2838 call assert_fails('doautoall BufEnter', 'E813:')
2839
2840 tabonly
2841 augroup aucmd_win_test
2842 au!
2843 augroup END
2844 augroup! aucmd_win_test
2845 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002846endfunc
2847
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00002848func Test_Visual_doautoall_redraw()
2849 call setline(1, ['a', 'b'])
2850 new
2851 wincmd p
2852 call feedkeys("G\<C-V>", 'txn')
2853 autocmd User Explode ++once redraw
2854 doautoall User Explode
2855 %bwipe!
2856endfunc
2857
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01002858" This was using freed memory.
2859func Test_BufNew_arglocal()
2860 arglocal
2861 au BufNew * arglocal
2862 call assert_fails('drop xx', 'E1156:')
2863
2864 au! BufNew
2865endfunc
2866
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002867func Test_autocmd_closes_window()
2868 au BufNew,BufWinLeave * e %e
2869 file yyy
2870 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002871 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002872
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002873 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002874 au! BufNew
2875 au! BufWinLeave
2876endfunc
2877
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002878func Test_autocmd_quit_psearch()
2879 sn aa bb
2880 augroup aucmd_win_test
2881 au!
2882 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
2883 augroup END
2884 ps /
2885
2886 augroup aucmd_win_test
2887 au!
2888 augroup END
2889endfunc
2890
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002891" Fuzzer found some strange combination that caused a crash.
2892func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002893 " For unknown reason this hangs on MS-Windows
2894 CheckNotMSWindows
2895
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002896 augroup aucmd_normal_test
2897 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
2898 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002899 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002900 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002901 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002902 normal G
2903
2904 augroup aucmd_normal_test
2905 au!
2906 augroup END
2907endfunc
2908
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002909func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002910 " For unknown reason this hangs on MS-Windows
2911 CheckNotMSWindows
2912
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002913 au BufWinLeave * nested q
2914 call assert_fails("norm 7q?\n", 'E855:')
2915
2916 au! BufWinLeave
2917 new
2918 only
2919endfunc
2920
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002921func Test_autocmd_vimgrep()
2922 augroup aucmd_vimgrep
2923 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * sb
2924 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * q9�
2925 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002926 %bwipe!
Bram Moolenaardd07c022021-02-07 13:32:46 +01002927 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002928
2929 augroup aucmd_vimgrep
2930 au!
2931 augroup END
2932endfunc
2933
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002934func Test_autocmd_with_block()
2935 augroup block_testing
2936 au BufReadPost *.xml {
2937 setlocal matchpairs+=<:>
2938 /<start
2939 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02002940 au CursorHold * {
2941 autocmd BufReadPre * ++once echo 'one' | echo 'two'
2942 g:gotSafeState = 77
2943 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002944 augroup END
2945
2946 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
2947 call assert_equal(expected, execute('au BufReadPost *.xml'))
2948
Bram Moolenaar63b91732021-08-05 20:40:03 +02002949 doautocmd CursorHold
2950 call assert_equal(77, g:gotSafeState)
2951 unlet g:gotSafeState
2952
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002953 augroup block_testing
2954 au!
2955 augroup END
2956endfunc
2957
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002958" Test TextChangedI and TextChanged
2959func Test_Changed_ChangedI()
2960 new
2961 call test_override("char_avail", 1)
2962 let [g:autocmd_i, g:autocmd_n] = ['','']
2963
2964 func! TextChangedAutocmdI(char)
2965 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
2966 endfunc
2967
2968 augroup Test_TextChanged
2969 au!
2970 au TextChanged <buffer> :call TextChangedAutocmdI('N')
2971 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
2972 augroup END
2973
2974 call feedkeys("ifoo\<esc>", 'tnix')
2975 " TODO: Test test does not seem to trigger TextChanged autocommand, this
2976 " requires running Vim in a terminal window.
2977 " call assert_equal('N3', g:autocmd_n)
2978 call assert_equal('I3', g:autocmd_i)
2979
2980 call feedkeys("yyp", 'tnix')
2981 " TODO: Test test does not seem to trigger TextChanged autocommand.
2982 " call assert_equal('N4', g:autocmd_n)
2983 call assert_equal('I3', g:autocmd_i)
2984
2985 " CleanUp
2986 call test_override("char_avail", 0)
2987 au! TextChanged <buffer>
2988 au! TextChangedI <buffer>
2989 augroup! Test_TextChanged
2990 delfu TextChangedAutocmdI
2991 unlet! g:autocmd_i g:autocmd_n
2992
2993 bw!
2994endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002995
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00002996func Test_closing_autocmd_window()
2997 let lines =<< trim END
2998 edit Xa.txt
2999 tabnew Xb.txt
3000 autocmd BufEnter Xa.txt unhide 1
3001 doautoall BufEnter
3002 END
3003 call v9.CheckScriptFailure(lines, 'E814:')
3004 au! BufEnter
3005 only!
3006 bwipe Xa.txt
3007 bwipe Xb.txt
3008endfunc
3009
Bram Moolenaar347538f2022-03-26 16:28:06 +00003010func Test_bufwipeout_changes_window()
3011 " This should not crash, but we don't have any expectations about what
3012 " happens, changing window in BufWipeout has unpredictable results.
3013 tabedit
3014 let g:window_id = win_getid()
3015 topleft new
3016 setlocal bufhidden=wipe
3017 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3018 tabprevious
3019 +tabclose
3020
3021 unlet g:window_id
3022 au! BufWipeout
3023 %bwipe!
3024endfunc
3025
3026
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003027" vim: shiftwidth=2 sts=2 expandtab