blob: 724d0733fc958f73c93734e6d132b730c2aa55ce [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
LemonBoy09371822022-04-08 15:18:45 +01006source screendump.vim
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00007import './vim9.vim' as v9
Bram Moolenaar8c64a362018-03-23 22:39:31 +01008
Bram Moolenaar1e115362019-01-09 23:01:02 +01009func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +020010 for bnr in range(1, bufnr('$'))
11 if bufloaded(bnr) && bufnr('%') != bnr
12 execute 'bd! ' . bnr
13 endif
14 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020015endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020016
Bram Moolenaar14735512016-03-26 21:00:08 +010017func Test_vim_did_enter()
18 call assert_false(v:vim_did_enter)
19
20 " This script will never reach the main loop, can't check if v:vim_did_enter
21 " becomes one.
22endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020023
Bram Moolenaar75911162020-07-21 19:44:47 +020024" Test for the CursorHold autocmd
25func Test_CursorHold_autocmd()
26 CheckRunVimInTerminal
27 call writefile(['one', 'two', 'three'], 'Xfile')
28 let before =<< trim END
29 set updatetime=10
30 au CursorHold * call writefile([line('.')], 'Xoutput', 'a')
31 END
32 call writefile(before, 'Xinit')
33 let buf = RunVimInTerminal('-S Xinit Xfile', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020034 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020035 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020036 call term_sendkeys(buf, "gg")
37 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020038 call WaitForAssert({-> assert_equal(['1'], readfile('Xoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020039 call term_sendkeys(buf, "j")
40 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020041 call WaitForAssert({-> assert_equal(['1', '2'], readfile('Xoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020042 call term_sendkeys(buf, "j")
43 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020044 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('Xoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call StopVimInTerminal(buf)
46
Bram Moolenaar75911162020-07-21 19:44:47 +020047 call delete('Xinit')
48 call delete('Xoutput')
49 call delete('Xfile')
50endfunc
51
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020053
Bram Moolenaarc67e8922016-05-24 16:07:40 +020054 func ExitInsertMode(id)
55 call feedkeys("\<Esc>")
56 endfunc
57
58 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020059 " Need to move the cursor.
60 call feedkeys("ggG", "xt")
61
Bram Moolenaarc67e8922016-05-24 16:07:40 +020062 let g:triggered = 0
63 au CursorHoldI * let g:triggered += 1
64 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010065 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020066 call feedkeys('a', 'x!')
67 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010068 unlet g:triggered
69 au! CursorHoldI
70 set updatetime&
71 endfunc
72
73 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020074 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010075 " Need to move the cursor.
76 call feedkeys("ggG", "xt")
77
78 " Confirm the timer invoked in exit_cb of the job doesn't disturb
79 " CursorHoldI event.
80 let g:triggered = 0
81 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020082 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010083 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020084 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010085 call feedkeys('a', 'x!')
86 call assert_equal(1, g:triggered)
87 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020088 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020089 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020090 endfunc
91
92 func Test_cursorhold_insert_ctrl_x()
93 let g:triggered = 0
94 au CursorHoldI * let g:triggered += 1
95 set updatetime=20
96 call timer_start(100, 'ExitInsertMode')
97 " CursorHoldI does not trigger after CTRL-X
98 call feedkeys("a\<C-X>", 'x!')
99 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +0100100 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200101 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200102 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200103 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200104
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100105 func Test_cursorhold_insert_ctrl_g_U()
106 au CursorHoldI * :
107 set updatetime=20
108 new
109 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
110 call feedkeys("i()\<C-g>U", 'tx!')
111 sleep 200m
112 call assert_equal('(foo)', getline(1))
113 undo
114 call assert_equal('', getline(1))
115
116 bwipe!
117 au! CursorHoldI
118 set updatetime&
119 endfunc
120
Bram Moolenaar97b00752019-05-12 13:07:14 +0200121 func Test_OptionSet_modeline()
122 call test_override('starting', 1)
123 au! OptionSet
124 augroup set_tabstop
125 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
126 augroup END
127 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline')
128 set modeline
129 let v:errmsg = ''
130 call assert_fails('split XoptionsetModeline', 'E12:')
131 call assert_equal(7, &ts)
132 call assert_equal('', v:errmsg)
133
134 augroup set_tabstop
135 au!
136 augroup END
137 bwipe!
138 set ts&
139 call delete('XoptionsetModeline')
140 call test_override('starting', 0)
141 endfunc
142
143endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200144
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200145func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200146 augroup test_bufunload_group
147 autocmd!
148 autocmd BufUnload * call add(s:li, "bufunload")
149 autocmd BufDelete * call add(s:li, "bufdelete")
150 autocmd BufWipeout * call add(s:li, "bufwipeout")
151 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200152
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100153 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200154 new
155 setlocal bufhidden=
156 bunload
157 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200158
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100159 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200160 new
161 setlocal bufhidden=delete
162 bunload
163 call assert_equal(["bufunload", "bufdelete"], s:li)
164
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100165 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200166 new
167 setlocal bufhidden=unload
168 bwipeout
169 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
170
Bram Moolenaare99e8442016-07-26 20:43:40 +0200171 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200172 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200173endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200174
175" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200176func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200177 tabedit
178 tabfirst
179
180 augroup test_autocmd_bufunload_with_tabnext_group
181 autocmd!
182 autocmd BufUnload <buffer> tabnext
183 augroup END
184
185 quit
186 call assert_equal(2, tabpagenr('$'))
187
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200188 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200189 augroup! test_autocmd_bufunload_with_tabnext_group
190 tablast
191 quit
192endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200193
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100194func Test_argdelete_in_next()
195 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
196 call assert_fails('next a b', 'E1156:')
197 au! BufNew,BufEnter,BufLeave,BufWinEnter *
198endfunc
199
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200200func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200201 tabedit
202 augroup sample
203 autocmd!
204 autocmd BufWinLeave <buffer> tabfirst
205 augroup END
206 call setline(1, ['a', 'b', 'c'])
207 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200208 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200209endfunc
210
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200211" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200212func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200213 split aa.txt
214 let lastbuf = bufnr('$')
215
216 augroup test_autocmd_bufunload
217 autocmd!
218 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
219 augroup END
220
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100221 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200222
223 autocmd! test_autocmd_bufunload
224 augroup! test_autocmd_bufunload
225 bwipe! aa.txt
226 bwipe! bb.txt
227endfunc
228
229" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200230func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200231 setlocal buftype=nowrite
232 let lastbuf = bufnr('$')
233
234 augroup test_autocmd_bufunload
235 autocmd!
236 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
237 augroup END
238
239 normal! i1
240 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200241
242 autocmd! test_autocmd_bufunload
243 augroup! test_autocmd_bufunload
244 bwipe! a.txt
245endfunc
246
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100247func Test_autocmd_dummy_wipeout()
248 " prepare files
249 call writefile([''], 'Xdummywipetest1.txt')
250 call writefile([''], 'Xdummywipetest2.txt')
251 augroup test_bufunload_group
252 autocmd!
253 autocmd BufUnload * call add(s:li, "bufunload")
254 autocmd BufDelete * call add(s:li, "bufdelete")
255 autocmd BufWipeout * call add(s:li, "bufwipeout")
256 augroup END
257
258 let s:li = []
259 split Xdummywipetest1.txt
260 silent! vimgrep /notmatched/ Xdummywipetest*
261 call assert_equal(["bufunload", "bufwipeout"], s:li)
262
263 bwipeout
264 call delete('Xdummywipetest1.txt')
265 call delete('Xdummywipetest2.txt')
266 au! test_bufunload_group
267 augroup! test_bufunload_group
268endfunc
269
Bram Moolenaarc917da42016-07-19 22:31:36 +0200270func Test_win_tab_autocmd()
271 let g:record = []
272
273 augroup testing
274 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000275 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200276 au WinEnter * call add(g:record, 'WinEnter')
277 au WinLeave * call add(g:record, 'WinLeave')
278 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200279 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200280 au TabEnter * call add(g:record, 'TabEnter')
281 au TabLeave * call add(g:record, 'TabLeave')
282 augroup END
283
284 split
285 tabnew
286 close
287 close
288
289 call assert_equal([
290 \ 'WinLeave', 'WinNew', 'WinEnter',
291 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000292 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
293 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200294 \ ], g:record)
295
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200296 let g:record = []
297 tabnew somefile
298 tabnext
299 bwipe somefile
300
301 call assert_equal([
302 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
303 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000304 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200305 \ ], g:record)
306
Bram Moolenaarc917da42016-07-19 22:31:36 +0200307 augroup testing
308 au!
309 augroup END
310 unlet g:record
311endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200312
LemonBoy09371822022-04-08 15:18:45 +0100313func Test_WinScrolled()
314 CheckRunVimInTerminal
315
316 let lines =<< trim END
zeertzjqd58862d2022-04-12 11:32:48 +0100317 set nowrap scrolloff=0
318 for ii in range(1, 18)
319 call setline(ii, repeat(nr2char(96 + ii), ii * 2))
320 endfor
321 let win_id = win_getid()
322 let g:matched = v:false
323 execute 'au WinScrolled' win_id 'let g:matched = v:true'
324 let g:scrolled = 0
325 au WinScrolled * let g:scrolled += 1
326 au WinScrolled * let g:amatch = str2nr(expand('<amatch>'))
327 au WinScrolled * let g:afile = str2nr(expand('<afile>'))
LemonBoy09371822022-04-08 15:18:45 +0100328 END
329 call writefile(lines, 'Xtest_winscrolled')
330 let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6})
331
332 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
333 call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000)
334
335 " Scroll left/right in Normal mode.
336 call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>")
337 call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000)
338
339 " Scroll up/down in Normal mode.
340 call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>")
341 call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000)
342
343 " Scroll up/down in Insert mode.
344 call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>")
345 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
346 call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000)
347
348 " Scroll the window horizontally to focus the last letter of the third line
349 " containing only six characters. Moving to the previous and shorter lines
350 " should trigger another autocommand as Vim has to make them visible.
351 call term_sendkeys(buf, "5zl2k")
352 call term_sendkeys(buf, ":echo g:scrolled\<CR>")
353 call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000)
354
355 " Ensure the command was triggered for the specified window ID.
356 call term_sendkeys(buf, ":echo g:matched\<CR>")
357 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
358
359 " Ensure the expansion of <amatch> and <afile> matches the window ID.
360 call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>")
361 call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000)
362
363 call StopVimInTerminal(buf)
364 call delete('Xtest_winscrolled')
365endfunc
366
zeertzjqd58862d2022-04-12 11:32:48 +0100367func Test_WinScrolled_close_curwin()
368 CheckRunVimInTerminal
369
370 let lines =<< trim END
371 set nowrap scrolloff=0
372 call setline(1, ['aaa', 'bbb'])
373 vsplit
374 au WinScrolled * close
375 au VimLeave * call writefile(['123456'], 'Xtestout')
376 END
377 call writefile(lines, 'Xtest_winscrolled_close_curwin')
378 let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6})
379
380 " This was using freed memory
381 call term_sendkeys(buf, "\<C-E>")
382 call TermWait(buf)
383 call StopVimInTerminal(buf)
384
385 call assert_equal(['123456'], readfile('Xtestout'))
386
387 call delete('Xtest_winscrolled_close_curwin')
388 call delete('Xtestout')
389endfunc
390
naohiro ono23beefe2021-11-13 12:38:49 +0000391func Test_WinClosed()
392 " Test that the pattern is matched against the closed window's ID, and both
393 " <amatch> and <afile> are set to it.
394 new
395 let winid = win_getid()
396 let g:matched = v:false
397 augroup test-WinClosed
398 autocmd!
399 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
400 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
401 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
402 augroup END
403 close
404 call assert_true(g:matched)
405 call assert_equal(winid, g:amatch)
406 call assert_equal(winid, g:afile)
407
408 " Test that WinClosed is non-recursive.
409 new
410 new
411 call assert_equal(3, winnr('$'))
412 let g:triggered = 0
413 augroup test-WinClosed
414 autocmd!
415 autocmd WinClosed * let g:triggered += 1
416 autocmd WinClosed * 2 wincmd c
417 augroup END
418 close
419 call assert_equal(1, winnr('$'))
420 call assert_equal(1, g:triggered)
421
422 autocmd! test-WinClosed
423 augroup! test-WinClosed
424 unlet g:matched
425 unlet g:amatch
426 unlet g:afile
427 unlet g:triggered
428endfunc
429
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100430func Test_WinClosed_throws()
431 vnew
432 let bnr = bufnr()
433 call assert_equal(1, bufloaded(bnr))
434 augroup test-WinClosed
435 autocmd WinClosed * throw 'foo'
436 augroup END
437 try
438 close
439 catch /.*/
440 endtry
441 call assert_equal(0, bufloaded(bnr))
442
443 autocmd! test-WinClosed
444 augroup! test-WinClosed
445endfunc
446
zeertzjq6a069402022-04-07 14:08:29 +0100447func Test_WinClosed_throws_with_tabs()
448 tabnew
449 let bnr = bufnr()
450 call assert_equal(1, bufloaded(bnr))
451 augroup test-WinClosed
452 autocmd WinClosed * throw 'foo'
453 augroup END
454 try
455 close
456 catch /.*/
457 endtry
458 call assert_equal(0, bufloaded(bnr))
459
460 autocmd! test-WinClosed
461 augroup! test-WinClosed
462endfunc
463
Bram Moolenaare99e8442016-07-26 20:43:40 +0200464func s:AddAnAutocmd()
465 augroup vimBarTest
466 au BufReadCmd * echo 'hello'
467 augroup END
468 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
469endfunc
470
471func Test_early_bar()
472 " test that a bar is recognized before the {event}
473 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000474 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200475 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000476 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200477
478 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000479 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200480 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000481 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200482
483 " test that a bar is recognized after the {event}
484 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000485 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200486 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000487 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200488
489 " test that a bar is recognized after the {group}
490 call s:AddAnAutocmd()
491 au! vimBarTest|echo 'hello'
492 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
493endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200494
Bram Moolenaar5c809082016-09-01 16:21:48 +0200495func RemoveGroup()
496 autocmd! StartOK
497 augroup! StartOK
498endfunc
499
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200500func Test_augroup_warning()
501 augroup TheWarning
502 au VimEnter * echo 'entering'
503 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100504 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200505 redir => res
506 augroup! TheWarning
507 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100508 call assert_match("W19:", res)
509 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200510
511 " check "Another" does not take the pace of the deleted entry
512 augroup Another
513 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100514 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200515 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200516
517 " no warning for postpone aucmd delete
518 augroup StartOK
519 au VimEnter * call RemoveGroup()
520 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100521 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200522 redir => res
523 doautocmd VimEnter
524 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100525 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200526 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200527
528 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200529endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200530
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200531func Test_BufReadCmdHelp()
532 " This used to cause access to free memory
533 au BufReadCmd * e +h
534 help
535
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200536 au! BufReadCmd
537endfunc
538
539func Test_BufReadCmdHelpJump()
540 " This used to cause access to free memory
541 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200542 " } to fix highlighting
543 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200544
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200545 au! BufReadCmd
546endfunc
547
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200548func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200549 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200550 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200551 call assert_fails('augroup! x', 'E936:')
552 au VimEnter * echo
553 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200554 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100555 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200556 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200557endfunc
558
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200559" Tests for autocommands on :close command.
560" This used to be in test13.
561func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200562 " Clean up buffers, because in some cases this function fails.
563 call s:cleanup_buffers()
564
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200565 " Write three files and open them, each in a window.
566 " Then go to next window, with autocommand that deletes the previous one.
567 " Do this twice, writing the file.
568 e! Xtestje1
569 call setline(1, 'testje1')
570 w
571 sp Xtestje2
572 call setline(1, 'testje2')
573 w
574 sp Xtestje3
575 call setline(1, 'testje3')
576 w
577 wincmd w
578 au WinLeave Xtestje2 bwipe
579 wincmd w
580 call assert_equal('Xtestje1', expand('%'))
581
582 au WinLeave Xtestje1 bwipe Xtestje3
583 close
584 call assert_equal('Xtestje1', expand('%'))
585
586 " Test deleting the buffer on a Unload event. If this goes wrong there
587 " will be the ATTENTION prompt.
588 e Xtestje1
589 au!
590 au! BufUnload Xtestje1 bwipe
591 call assert_fails('e Xtestje3', 'E937:')
592 call assert_equal('Xtestje3', expand('%'))
593
594 e Xtestje2
595 sp Xtestje1
596 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200597 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200598
599 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
600 " there are ml_line errors and/or a Crash.
601 au!
602 only
603 e Xanother
604 e Xtestje1
605 bwipe Xtestje2
606 bwipe Xtestje3
607 au BufWipeout Xtestje1 buf Xtestje1
608 bwipe
609 call assert_equal('Xanother', expand('%'))
610
611 only
612 help
613 wincmd w
614 1quit
615 call assert_equal('Xanother', expand('%'))
616
617 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100618 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200619 call delete('Xtestje1')
620 call delete('Xtestje2')
621 call delete('Xtestje3')
622endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100623
624func Test_BufEnter()
625 au! BufEnter
626 au Bufenter * let val = val . '+'
627 let g:val = ''
628 split NewFile
629 call assert_equal('+', g:val)
630 bwipe!
631 call assert_equal('++', g:val)
632
633 " Also get BufEnter when editing a directory
634 call mkdir('Xdir')
635 split Xdir
636 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100637
638 " On MS-Windows we can't edit the directory, make sure we wipe the right
639 " buffer.
640 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100641
642 call delete('Xdir', 'd')
643 au! BufEnter
644endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100645
646" Closing a window might cause an endless loop
647" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200648func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200649 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100650 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200651 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100652 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100653 mksession!
654
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200655 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200656 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200657 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100658 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200659 augroup test_autocmd_sessionload
660 autocmd!
661 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
662 augroup END
663
664 func WriteErrors()
665 call writefile([execute("messages")], "Xerrors")
666 endfunc
667 au VimLeave * call WriteErrors()
668 [CODE]
669
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100670 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200671 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100672 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200673 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100674
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100675 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100676 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100677 call delete(file)
678 endfor
679endfunc
680
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100681" Using :blast and :ball for many events caused a crash, because b_nwindows was
682" not incremented correctly.
683func Test_autocmd_blast_badd()
684 let content =<< trim [CODE]
685 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
686 edit foo1
687 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
688 edit foo2
689 call writefile(['OK'], 'Xerrors')
690 qall
691 [CODE]
692
693 call writefile(content, 'XblastBall')
694 call system(GetVimCommand() .. ' --clean -S XblastBall')
695 call assert_match('OK', readfile('Xerrors')->join())
696
697 call delete('XblastBall')
698 call delete('Xerrors')
699endfunc
700
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100701" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200702func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100703 tabnew
704 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100705 mksession!
706
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200707 let content =<< trim [CODE]
708 set nocp noswapfile
709 function! DeleteInactiveBufs()
710 tabfirst
711 let tabblist = []
712 for i in range(1, tabpagenr(''$''))
713 call extend(tabblist, tabpagebuflist(i))
714 endfor
715 for b in range(1, bufnr(''$''))
716 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
717 exec ''bwipeout '' . b
718 endif
719 endfor
720 echomsg "SessionLoadPost DONE"
721 endfunction
722 au SessionLoadPost * call DeleteInactiveBufs()
723
724 func WriteErrors()
725 call writefile([execute("messages")], "Xerrors")
726 endfunc
727 au VimLeave * call WriteErrors()
728 [CODE]
729
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100730 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200731 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100732 let errors = join(readfile('Xerrors'))
733 " This probably only ever matches on unix.
734 call assert_notmatch('Caught deadly signal SEGV', errors)
735 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100736
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100737 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100738 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100739 call delete(file)
740 endfor
741endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200742
743func Test_empty_doau()
744 doau \|
745endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200746
747func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200748 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200749 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200750 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
751 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 +0200752 let g:opt = [expected, actual]
753 "call assert_equal(expected, actual)
754endfunc
755
756func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200757 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200758
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200759 badd test_autocmd.vim
760
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200761 call test_override('starting', 1)
762 set nocp
763 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
764
765 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100766 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200767 set nu
768 call assert_equal([], g:options)
769 call assert_equal(g:opt[0], g:opt[1])
770
771 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100772 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200773 setlocal nonu
774 call assert_equal([], g:options)
775 call assert_equal(g:opt[0], g:opt[1])
776
777 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100778 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200779 setglobal nonu
780 call assert_equal([], g:options)
781 call assert_equal(g:opt[0], g:opt[1])
782
783 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100784 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200785 setlocal ai
786 call assert_equal([], g:options)
787 call assert_equal(g:opt[0], g:opt[1])
788
789 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100790 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200791 setglobal ai
792 call assert_equal([], g:options)
793 call assert_equal(g:opt[0], g:opt[1])
794
795 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100796 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200797 set ai!
798 call assert_equal([], g:options)
799 call assert_equal(g:opt[0], g:opt[1])
800
801 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100802 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200803 noa setlocal ai
804 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200805 set ai!
806 call assert_equal([], g:options)
807 call assert_equal(g:opt[0], g:opt[1])
808
809 " Should not print anything, use :noa
810 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100811 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200812 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200813 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200814 call assert_equal(g:opt[0], g:opt[1])
815
816 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100817 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200818 set list nu
819 call assert_equal([], g:options)
820 call assert_equal(g:opt[0], g:opt[1])
821
822 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100823 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200824 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200825 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 +0200826 call assert_equal(g:opt[0], g:opt[1])
827
828 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100829 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200830 setlocal acd
831 call assert_equal([], g:options)
832 call assert_equal(g:opt[0], g:opt[1])
833
834 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100835 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200836 set ar
837 call assert_equal([], g:options)
838 call assert_equal(g:opt[0], g:opt[1])
839
840 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100841 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200842 setlocal ar
843 call assert_equal([], g:options)
844 call assert_equal(g:opt[0], g:opt[1])
845
846 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100847 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200848 setglobal invar
849 call assert_equal([], g:options)
850 call assert_equal(g:opt[0], g:opt[1])
851
852 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100853 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
854 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200855 call assert_equal([], g:options)
856 call assert_equal(g:opt[0], g:opt[1])
857
858 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100859 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200860 " try twice, first time, shouldn't trigger because option name is invalid,
861 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200862 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200863 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200864 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200865 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200866 call assert_equal([], g:options)
867 call assert_equal(g:opt[0], g:opt[1])
868
869 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100870 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200871 call setwinvar(0, '&number', 1)
872 call assert_equal([], g:options)
873 call assert_equal(g:opt[0], g:opt[1])
874
875 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100876 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200877 setlocal key=blah
878 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200879 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200880 call assert_equal(g:opt[0], g:opt[1])
881
Bram Moolenaard7c96872019-06-15 17:12:48 +0200882
883 " 18a: Setting string global option"
884 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100885 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200886 set backupext=foo
887 call assert_equal([], g:options)
888 call assert_equal(g:opt[0], g:opt[1])
889
890 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100891 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200892 set backupext&
893 call assert_equal([], g:options)
894 call assert_equal(g:opt[0], g:opt[1])
895
896 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100897 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200898 setglobal backupext=bar
899 call assert_equal([], g:options)
900 call assert_equal(g:opt[0], g:opt[1])
901
902 " 18d: Setting local string global option"
903 " As this is a global option this sets the global value even though
904 " :setlocal is used!
905 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100906 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200907 setlocal backupext=baz
908 call assert_equal([], g:options)
909 call assert_equal(g:opt[0], g:opt[1])
910
911 " 18e: Setting again string global option"
912 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
913 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100914 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200915 set backupext=fuu
916 call assert_equal([], g:options)
917 call assert_equal(g:opt[0], g:opt[1])
918
919
zeertzjqb811de52021-10-21 10:50:44 +0100920 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200921 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100922 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200923 set tags=tagpath
924 call assert_equal([], g:options)
925 call assert_equal(g:opt[0], g:opt[1])
926
zeertzjqb811de52021-10-21 10:50:44 +0100927 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100928 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200929 set tags&
930 call assert_equal([], g:options)
931 call assert_equal(g:opt[0], g:opt[1])
932
zeertzjqb811de52021-10-21 10:50:44 +0100933 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100934 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200935 setglobal tags=tagpath1
936 call assert_equal([], g:options)
937 call assert_equal(g:opt[0], g:opt[1])
938
zeertzjqb811de52021-10-21 10:50:44 +0100939 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100940 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200941 setlocal tags=tagpath2
942 call assert_equal([], g:options)
943 call assert_equal(g:opt[0], g:opt[1])
944
zeertzjqb811de52021-10-21 10:50:44 +0100945 " 19e: Setting again string global-local (to buffer) option"
946 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200947 " but the old local value for all other kinds of options.
948 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
949 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100950 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200951 set tags=tagpath
952 call assert_equal([], g:options)
953 call assert_equal(g:opt[0], g:opt[1])
954
zeertzjqb811de52021-10-21 10:50:44 +0100955 " 19f: Setting string global-local (to buffer) option to an empty string"
956 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200957 " but the old local value for all other kinds of options.
958 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
959 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100960 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200961 set tags=tagpath
962 call assert_equal([], g:options)
963 call assert_equal(g:opt[0], g:opt[1])
964
965
966 " 20a: Setting string local (to buffer) option"
967 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100968 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200969 set spelllang=elvish,klingon
970 call assert_equal([], g:options)
971 call assert_equal(g:opt[0], g:opt[1])
972
973 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100974 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200975 set spelllang&
976 call assert_equal([], g:options)
977 call assert_equal(g:opt[0], g:opt[1])
978
979 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100980 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200981 setglobal spelllang=elvish
982 call assert_equal([], g:options)
983 call assert_equal(g:opt[0], g:opt[1])
984
985 " 20d: Setting local string local (to buffer) option"
986 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100987 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200988 setlocal spelllang=klingon
989 call assert_equal([], g:options)
990 call assert_equal(g:opt[0], g:opt[1])
991
992 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +0100993 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200994 " but the old local value for all other kinds of options.
995 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
996 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100997 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200998 set spelllang=foo
999 call assert_equal([], g:options)
1000 call assert_equal(g:opt[0], g:opt[1])
1001
1002
zeertzjqb811de52021-10-21 10:50:44 +01001003 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001004 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001005 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001006 set statusline=foo
1007 call assert_equal([], g:options)
1008 call assert_equal(g:opt[0], g:opt[1])
1009
zeertzjqb811de52021-10-21 10:50:44 +01001010 " 21b: Resetting string global-local (to window) option"
1011 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001012 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001013 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001014 set statusline&
1015 call assert_equal([], g:options)
1016 call assert_equal(g:opt[0], g:opt[1])
1017
zeertzjqb811de52021-10-21 10:50:44 +01001018 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001019 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001020 setglobal statusline=bar
1021 call assert_equal([], g:options)
1022 call assert_equal(g:opt[0], g:opt[1])
1023
zeertzjqb811de52021-10-21 10:50:44 +01001024 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001025 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001026 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001027 setlocal statusline=baz
1028 call assert_equal([], g:options)
1029 call assert_equal(g:opt[0], g:opt[1])
1030
zeertzjqb811de52021-10-21 10:50:44 +01001031 " 21e: Setting again string global-local (to window) option"
1032 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +02001033 " but the old local value for all other kinds of options.
1034 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
1035 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001036 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001037 set statusline=foo
1038 call assert_equal([], g:options)
1039 call assert_equal(g:opt[0], g:opt[1])
1040
1041
1042 " 22a: Setting string local (to window) option"
1043 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001044 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001045 set foldignore=fo
1046 call assert_equal([], g:options)
1047 call assert_equal(g:opt[0], g:opt[1])
1048
1049 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001050 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001051 set foldignore&
1052 call assert_equal([], g:options)
1053 call assert_equal(g:opt[0], g:opt[1])
1054
1055 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001056 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001057 setglobal foldignore=bar
1058 call assert_equal([], g:options)
1059 call assert_equal(g:opt[0], g:opt[1])
1060
1061 " 22d: Setting local string local (to window) option"
1062 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001063 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001064 setlocal foldignore=baz
1065 call assert_equal([], g:options)
1066 call assert_equal(g:opt[0], g:opt[1])
1067
1068 " 22e: Setting again string local (to window) option"
1069 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
1070 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001071 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001072 set foldignore=fo
1073 call assert_equal([], g:options)
1074 call assert_equal(g:opt[0], g:opt[1])
1075
1076
zeertzjqb811de52021-10-21 10:50:44 +01001077 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001078 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1079 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001080 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001081 setglobal cmdheight=2
1082 call assert_equal([], g:options)
1083 call assert_equal(g:opt[0], g:opt[1])
1084
1085 " 23b: Setting local number global option"
1086 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1087 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001088 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001089 setlocal cmdheight=2
1090 call assert_equal([], g:options)
1091 call assert_equal(g:opt[0], g:opt[1])
1092
1093 " 23c: Setting again number global option"
1094 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1095 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001096 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001097 set cmdheight=2
1098 call assert_equal([], g:options)
1099 call assert_equal(g:opt[0], g:opt[1])
1100
1101 " 23d: Setting again number global option"
1102 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001103 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001104 set cmdheight=2
1105 call assert_equal([], g:options)
1106 call assert_equal(g:opt[0], g:opt[1])
1107
1108
1109 " 24a: Setting global number global-local (to buffer) option"
1110 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1111 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001112 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001113 setglobal undolevels=2
1114 call assert_equal([], g:options)
1115 call assert_equal(g:opt[0], g:opt[1])
1116
1117 " 24b: Setting local number global-local (to buffer) option"
1118 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1119 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001120 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001121 setlocal undolevels=2
1122 call assert_equal([], g:options)
1123 call assert_equal(g:opt[0], g:opt[1])
1124
1125 " 24c: Setting again number global-local (to buffer) option"
1126 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1127 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001128 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001129 set undolevels=2
1130 call assert_equal([], g:options)
1131 call assert_equal(g:opt[0], g:opt[1])
1132
1133 " 24d: Setting again global number global-local (to buffer) option"
1134 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001135 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001136 set undolevels=2
1137 call assert_equal([], g:options)
1138 call assert_equal(g:opt[0], g:opt[1])
1139
1140
1141 " 25a: Setting global number local (to buffer) option"
1142 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1143 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001144 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001145 setglobal wrapmargin=2
1146 call assert_equal([], g:options)
1147 call assert_equal(g:opt[0], g:opt[1])
1148
1149 " 25b: Setting local number local (to buffer) option"
1150 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1151 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001152 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001153 setlocal wrapmargin=2
1154 call assert_equal([], g:options)
1155 call assert_equal(g:opt[0], g:opt[1])
1156
1157 " 25c: Setting again number local (to buffer) option"
1158 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1159 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001160 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001161 set wrapmargin=2
1162 call assert_equal([], g:options)
1163 call assert_equal(g:opt[0], g:opt[1])
1164
1165 " 25d: Setting again global number local (to buffer) option"
1166 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001167 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001168 set wrapmargin=2
1169 call assert_equal([], g:options)
1170 call assert_equal(g:opt[0], g:opt[1])
1171
1172
1173 " 26: Setting number global-local (to window) option.
1174 " Such option does currently not exist.
1175
1176
1177 " 27a: Setting global number local (to window) option"
1178 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1179 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001180 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001181 setglobal foldcolumn=2
1182 call assert_equal([], g:options)
1183 call assert_equal(g:opt[0], g:opt[1])
1184
1185 " 27b: Setting local number local (to window) option"
1186 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1187 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001188 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001189 setlocal foldcolumn=2
1190 call assert_equal([], g:options)
1191 call assert_equal(g:opt[0], g:opt[1])
1192
1193 " 27c: Setting again number local (to window) option"
1194 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1195 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001196 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001197 set foldcolumn=2
1198 call assert_equal([], g:options)
1199 call assert_equal(g:opt[0], g:opt[1])
1200
zeertzjqb811de52021-10-21 10:50:44 +01001201 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001202 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001203 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001204 set foldcolumn=2
1205 call assert_equal([], g:options)
1206 call assert_equal(g:opt[0], g:opt[1])
1207
1208
1209 " 28a: Setting global boolean global option"
1210 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1211 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001212 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001213 setglobal nowrapscan
1214 call assert_equal([], g:options)
1215 call assert_equal(g:opt[0], g:opt[1])
1216
1217 " 28b: Setting local boolean global option"
1218 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1219 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001220 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001221 setlocal nowrapscan
1222 call assert_equal([], g:options)
1223 call assert_equal(g:opt[0], g:opt[1])
1224
1225 " 28c: Setting again boolean global option"
1226 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1227 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001228 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001229 set nowrapscan
1230 call assert_equal([], g:options)
1231 call assert_equal(g:opt[0], g:opt[1])
1232
1233 " 28d: Setting again global boolean global option"
1234 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001235 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001236 set wrapscan
1237 call assert_equal([], g:options)
1238 call assert_equal(g:opt[0], g:opt[1])
1239
1240
1241 " 29a: Setting global boolean global-local (to buffer) option"
1242 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1243 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001244 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001245 setglobal autoread
1246 call assert_equal([], g:options)
1247 call assert_equal(g:opt[0], g:opt[1])
1248
1249 " 29b: Setting local boolean global-local (to buffer) option"
1250 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1251 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001252 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001253 setlocal noautoread
1254 call assert_equal([], g:options)
1255 call assert_equal(g:opt[0], g:opt[1])
1256
1257 " 29c: Setting again boolean global-local (to buffer) option"
1258 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1259 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001260 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001261 set autoread
1262 call assert_equal([], g:options)
1263 call assert_equal(g:opt[0], g:opt[1])
1264
1265 " 29d: Setting again global boolean global-local (to buffer) option"
1266 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001267 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001268 set autoread
1269 call assert_equal([], g:options)
1270 call assert_equal(g:opt[0], g:opt[1])
1271
1272
1273 " 30a: Setting global boolean local (to buffer) option"
1274 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1275 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001276 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001277 setglobal cindent
1278 call assert_equal([], g:options)
1279 call assert_equal(g:opt[0], g:opt[1])
1280
1281 " 30b: Setting local boolean local (to buffer) option"
1282 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1283 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001284 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001285 setlocal nocindent
1286 call assert_equal([], g:options)
1287 call assert_equal(g:opt[0], g:opt[1])
1288
1289 " 30c: Setting again boolean local (to buffer) option"
1290 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1291 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001292 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001293 set cindent
1294 call assert_equal([], g:options)
1295 call assert_equal(g:opt[0], g:opt[1])
1296
1297 " 30d: Setting again global boolean local (to buffer) option"
1298 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001299 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001300 set cindent
1301 call assert_equal([], g:options)
1302 call assert_equal(g:opt[0], g:opt[1])
1303
1304
1305 " 31: Setting boolean global-local (to window) option
1306 " Currently no such option exists.
1307
1308
1309 " 32a: Setting global boolean local (to window) option"
1310 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1311 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001312 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001313 setglobal cursorcolumn
1314 call assert_equal([], g:options)
1315 call assert_equal(g:opt[0], g:opt[1])
1316
1317 " 32b: Setting local boolean local (to window) option"
1318 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1319 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001320 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001321 setlocal nocursorcolumn
1322 call assert_equal([], g:options)
1323 call assert_equal(g:opt[0], g:opt[1])
1324
1325 " 32c: Setting again boolean local (to window) option"
1326 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1327 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001328 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001329 set cursorcolumn
1330 call assert_equal([], g:options)
1331 call assert_equal(g:opt[0], g:opt[1])
1332
1333 " 32d: Setting again global boolean local (to window) option"
1334 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001335 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001336 set cursorcolumn
1337 call assert_equal([], g:options)
1338 call assert_equal(g:opt[0], g:opt[1])
1339
1340
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001341 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001342 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001343 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001344 set backspace=2
1345 call assert_equal([], g:options)
1346 call assert_equal(g:opt[0], g:opt[1])
1347
1348
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001349 " Cleanup
1350 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001351 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001352 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 +02001353 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001354 endfor
1355 call test_override('starting', 0)
1356 delfunc! AutoCommandOptionSet
1357endfunc
1358
1359func Test_OptionSet_diffmode()
1360 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001361 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001362 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001363 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001364
1365 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1366 call assert_equal(0, &l:cul)
1367 diffthis
1368 call assert_equal(1, &l:cul)
1369
1370 vnew
1371 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1372 call assert_equal(0, &l:cul)
1373 diffthis
1374 call assert_equal(1, &l:cul)
1375
1376 diffoff
1377 call assert_equal(0, &l:cul)
1378 call assert_equal(1, getwinvar(2, '&l:cul'))
1379 bw!
1380
1381 call assert_equal(1, &l:cul)
1382 diffoff!
1383 call assert_equal(0, &l:cul)
1384 call assert_equal(0, getwinvar(1, '&l:cul'))
1385 bw!
1386
1387 " Cleanup
1388 au! OptionSet
1389 call test_override('starting', 0)
1390endfunc
1391
1392func Test_OptionSet_diffmode_close()
1393 call test_override('starting', 1)
1394 " 19: Try to close the current window when entering diff mode
1395 " should not segfault
1396 new
1397 au OptionSet diff close
1398
1399 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001400 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001401 call assert_equal(1, &diff)
1402 vnew
1403 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001404 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001405 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001406 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001407 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001408 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001409 bw!
1410
1411 " Cleanup
1412 au! OptionSet
1413 call test_override('starting', 0)
1414 "delfunc! AutoCommandOptionSet
1415endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001416
1417" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1418func Test_BufleaveWithDelete()
1419 new | edit Xfile1
1420
1421 augroup test_bufleavewithdelete
1422 autocmd!
1423 autocmd BufLeave Xfile1 bwipe Xfile2
1424 augroup END
1425
1426 call assert_fails('edit Xfile2', 'E143:')
1427 call assert_equal('Xfile1', bufname('%'))
1428
1429 autocmd! test_bufleavewithdelete BufLeave Xfile1
1430 augroup! test_bufleavewithdelete
1431
1432 new
1433 bwipe! Xfile1
1434endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001435
1436" Test for autocommand that changes the buffer list, when doing ":ball".
1437func Test_Acmd_BufAll()
1438 enew!
1439 %bwipe!
1440 call writefile(['Test file Xxx1'], 'Xxx1')
1441 call writefile(['Test file Xxx2'], 'Xxx2')
1442 call writefile(['Test file Xxx3'], 'Xxx3')
1443
1444 " Add three files to the buffer list
1445 split Xxx1
1446 close
1447 split Xxx2
1448 close
1449 split Xxx3
1450 close
1451
1452 " Wipe the buffer when the buffer is opened
1453 au BufReadPost Xxx2 bwipe
1454
1455 call append(0, 'Test file Xxx4')
1456 ball
1457
1458 call assert_equal(2, winnr('$'))
1459 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1460 wincmd t
1461
1462 au! BufReadPost
1463 %bwipe!
1464 call delete('Xxx1')
1465 call delete('Xxx2')
1466 call delete('Xxx3')
1467 enew! | only
1468endfunc
1469
1470" Test for autocommand that changes current buffer on BufEnter event.
1471" Check if modelines are interpreted for the correct buffer.
1472func Test_Acmd_BufEnter()
1473 %bwipe!
1474 call writefile(['start of test file Xxx1',
1475 \ "\<Tab>this is a test",
1476 \ 'end of test file Xxx1'], 'Xxx1')
1477 call writefile(['start of test file Xxx2',
1478 \ 'vim: set noai :',
1479 \ "\<Tab>this is a test",
1480 \ 'end of test file Xxx2'], 'Xxx2')
1481
1482 au BufEnter Xxx2 brew
1483 set ai modeline modelines=3
1484 edit Xxx1
1485 " edit Xxx2, autocmd will do :brew
1486 edit Xxx2
1487 exe "normal G?this is a\<CR>"
1488 " Append text with autoindent to this file
1489 normal othis should be auto-indented
1490 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1491 call assert_equal(3, line('.'))
1492 " Remove autocmd and edit Xxx2 again
1493 au! BufEnter Xxx2
1494 buf! Xxx2
1495 exe "normal G?this is a\<CR>"
1496 " append text without autoindent to Xxx
1497 normal othis should be in column 1
1498 call assert_equal("this should be in column 1", getline('.'))
1499 call assert_equal(4, line('.'))
1500
1501 %bwipe!
1502 call delete('Xxx1')
1503 call delete('Xxx2')
1504 set ai&vim modeline&vim modelines&vim
1505endfunc
1506
1507" Test for issue #57
1508" do not move cursor on <c-o> when autoindent is set
1509func Test_ai_CTRL_O()
1510 enew!
1511 set ai
1512 let save_fo = &fo
1513 set fo+=r
1514 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1515 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1516 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1517
1518 set ai&vim
1519 let &fo = save_fo
1520 enew!
1521endfunc
1522
1523" Test for autocommand that deletes the current buffer on BufLeave event.
1524" Also test deleting the last buffer, should give a new, empty buffer.
1525func Test_BufLeave_Wipe()
1526 %bwipe!
1527 let content = ['start of test file Xxx',
1528 \ 'this is a test',
1529 \ 'end of test file Xxx']
1530 call writefile(content, 'Xxx1')
1531 call writefile(content, 'Xxx2')
1532
1533 au BufLeave Xxx2 bwipe
1534 edit Xxx1
1535 split Xxx2
1536 " delete buffer Xxx2, we should be back to Xxx1
1537 bwipe
1538 call assert_equal('Xxx1', bufname('%'))
1539 call assert_equal(1, winnr('$'))
1540
1541 " Create an alternate buffer
1542 %write! test.out
1543 call assert_equal('test.out', bufname('#'))
1544 " delete alternate buffer
1545 bwipe test.out
1546 call assert_equal('Xxx1', bufname('%'))
1547 call assert_equal('', bufname('#'))
1548
1549 au BufLeave Xxx1 bwipe
1550 " delete current buffer, get an empty one
1551 bwipe!
1552 call assert_equal(1, line('$'))
1553 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001554 let g:bufinfo = getbufinfo()
1555 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001556
1557 call delete('Xxx1')
1558 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001559 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001560 %bwipe
1561 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001562
1563 " check that bufinfo doesn't contain a pointer to freed memory
1564 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001565endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001566
1567func Test_QuitPre()
1568 edit Xfoo
1569 let winid = win_getid(winnr())
1570 split Xbar
1571 au! QuitPre * let g:afile = expand('<afile>')
1572 " Close the other window, <afile> should be correct.
1573 exe win_id2win(winid) . 'q'
1574 call assert_equal('Xfoo', g:afile)
1575
1576 unlet g:afile
1577 bwipe Xfoo
1578 bwipe Xbar
1579endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001580
1581func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001582 au! CmdlineChanged : let g:text = getcmdline()
1583 let g:text = 0
1584 call feedkeys(":echom 'hello'\<CR>", 'xt')
1585 call assert_equal("echom 'hello'", g:text)
1586 au! CmdlineChanged
1587
1588 au! CmdlineChanged : let g:entered = expand('<afile>')
1589 let g:entered = 0
1590 call feedkeys(":echom 'hello'\<CR>", 'xt')
1591 call assert_equal(':', g:entered)
1592 au! CmdlineChanged
1593
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001594 au! CmdlineEnter : let g:entered = expand('<afile>')
1595 au! CmdlineLeave : let g:left = expand('<afile>')
1596 let g:entered = 0
1597 let g:left = 0
1598 call feedkeys(":echo 'hello'\<CR>", 'xt')
1599 call assert_equal(':', g:entered)
1600 call assert_equal(':', g:left)
1601 au! CmdlineEnter
1602 au! CmdlineLeave
1603
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001604 let save_shellslash = &shellslash
1605 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001606 au! CmdlineEnter / let g:entered = expand('<afile>')
1607 au! CmdlineLeave / let g:left = expand('<afile>')
1608 let g:entered = 0
1609 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001610 new
1611 call setline(1, 'hello')
1612 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001613 call assert_equal('/', g:entered)
1614 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001615 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001616 au! CmdlineEnter
1617 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001618 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001619endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001620
1621" Test for BufWritePre autocommand that deletes or unloads the buffer.
1622func Test_BufWritePre()
1623 %bwipe
1624 au BufWritePre Xxx1 bunload
1625 au BufWritePre Xxx2 bwipe
1626
1627 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1628 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1629
1630 edit Xtest
1631 e! Xxx2
1632 bdel Xtest
1633 e Xxx1
1634 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001635 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001636 call assert_equal('Xxx2', bufname('%'))
1637 edit Xtest
1638 e! Xxx2
1639 bwipe Xtest
1640 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001641 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001642 call assert_equal('Xxx1', bufname('%'))
1643 au! BufWritePre
1644 call delete('Xxx1')
1645 call delete('Xxx2')
1646endfunc
1647
1648" Test for BufUnload autocommand that unloads all the other buffers
1649func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001650 let g:test_is_flaky = 1
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001651 call writefile(['Test file Xxx1'], 'Xxx1')"
1652 call writefile(['Test file Xxx2'], 'Xxx2')"
1653
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001654 let content =<< trim [CODE]
1655 func UnloadAllBufs()
1656 let i = 1
1657 while i <= bufnr('$')
1658 if i != bufnr('%') && bufloaded(i)
1659 exe i . 'bunload'
1660 endif
1661 let i += 1
1662 endwhile
1663 endfunc
1664 au BufUnload * call UnloadAllBufs()
1665 au VimLeave * call writefile(['Test Finished'], 'Xout')
1666 edit Xxx1
1667 split Xxx2
1668 q
1669 [CODE]
1670
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001671 call writefile(content, 'Xtest')
1672
1673 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001674 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001675 call assert_true(filereadable('Xout'))
1676
1677 call delete('Xxx1')
1678 call delete('Xxx2')
1679 call delete('Xtest')
1680 call delete('Xout')
1681endfunc
1682
1683" Some tests for buffer-local autocommands
1684func Test_buflocal_autocmd()
1685 let g:bname = ''
1686 edit xx
1687 au BufLeave <buffer> let g:bname = expand("%")
1688 " here, autocommand for xx should trigger.
1689 " but autocommand shall not apply to buffer named <buffer>.
1690 edit somefile
1691 call assert_equal('xx', g:bname)
1692 let g:bname = ''
1693 " here, autocommand shall be auto-deleted
1694 bwipe xx
1695 " autocmd should not trigger
1696 edit xx
1697 call assert_equal('', g:bname)
1698 " autocmd should not trigger
1699 edit somefile
1700 call assert_equal('', g:bname)
1701 enew
1702 unlet g:bname
1703endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001704
1705" Test for "*Cmd" autocommands
1706func Test_Cmd_Autocmds()
1707 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1708
1709 enew!
1710 au BufReadCmd XtestA 0r Xxx|$del
1711 edit XtestA " will read text of Xxd instead
1712 call assert_equal('start of Xxx', getline(1))
1713
1714 au BufWriteCmd XtestA call append(line("$"), "write")
1715 write " will append a line to the file
1716 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001717 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001718 call assert_equal('write', getline(4))
1719
1720 " now we have:
1721 " 1 start of Xxx
1722 " 2 abc2
1723 " 3 end of Xxx
1724 " 4 write
1725
1726 au FileReadCmd XtestB '[r Xxx
1727 2r XtestB " will read Xxx below line 2 instead
1728 call assert_equal('start of Xxx', getline(3))
1729
1730 " now we have:
1731 " 1 start of Xxx
1732 " 2 abc2
1733 " 3 start of Xxx
1734 " 4 abc2
1735 " 5 end of Xxx
1736 " 6 end of Xxx
1737 " 7 write
1738
1739 au FileWriteCmd XtestC '[,']copy $
1740 normal 4GA1
1741 4,5w XtestC " will copy lines 4 and 5 to the end
1742 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001743 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001744 call assert_equal("end of Xxx", getline(9))
1745
1746 " now we have:
1747 " 1 start of Xxx
1748 " 2 abc2
1749 " 3 start of Xxx
1750 " 4 abc21
1751 " 5 end of Xxx
1752 " 6 end of Xxx
1753 " 7 write
1754 " 8 abc21
1755 " 9 end of Xxx
1756
1757 let g:lines = []
1758 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1759 w >>XtestD " will add lines to 'lines'
1760 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001761 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001762 call assert_equal(9, line('$'))
1763 call assert_equal('end of Xxx', getline('$'))
1764
1765 au BufReadCmd XtestE 0r Xxx|$del
1766 sp XtestE " split window with test.out
1767 call assert_equal('end of Xxx', getline(3))
1768
1769 let g:lines = []
1770 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1771 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1772 wall " will write other window to 'lines'
1773 call assert_equal(4, len(g:lines), g:lines)
1774 call assert_equal('asdf', g:lines[2])
1775
1776 au! BufReadCmd
1777 au! BufWriteCmd
1778 au! FileReadCmd
1779 au! FileWriteCmd
1780 au! FileAppendCmd
1781 %bwipe!
1782 call delete('Xxx')
1783 enew!
1784endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001785
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001786func s:ReadFile()
1787 setl noswapfile nomodified
1788 let filename = resolve(expand("<afile>:p"))
1789 execute 'read' fnameescape(filename)
1790 1d_
1791 exe 'file' fnameescape(filename)
1792 setl buftype=acwrite
1793endfunc
1794
1795func s:WriteFile()
1796 let filename = resolve(expand("<afile>:p"))
1797 setl buftype=
1798 noautocmd execute 'write' fnameescape(filename)
1799 setl buftype=acwrite
1800 setl nomodified
1801endfunc
1802
1803func Test_BufReadCmd()
1804 autocmd BufReadCmd *.test call s:ReadFile()
1805 autocmd BufWriteCmd *.test call s:WriteFile()
1806
1807 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1808 edit Xcmd.test
1809 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1810 normal! Gofour
1811 write
1812 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1813
1814 bwipe!
1815 call delete('Xcmd.test')
1816 au! BufReadCmd
1817 au! BufWriteCmd
1818endfunc
1819
Bram Moolenaaraace2152017-11-05 16:23:10 +01001820func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001821 exe a:start .. 'mark ['
1822 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001823endfunc
1824
1825" Verify the effects of autocmds on '[ and ']
1826func Test_change_mark_in_autocmds()
1827 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001828 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001829
1830 call SetChangeMarks(2, 3)
1831 write
1832 call assert_equal([1, 4], [line("'["), line("']")])
1833
1834 call SetChangeMarks(2, 3)
1835 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1836 write
1837 au! BufWritePre
1838
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001839 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001840 write XtestFilter
1841 write >> XtestFilter
1842
1843 call SetChangeMarks(2, 3)
1844 " Marks are set to the entire range of the write
1845 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1846 " '[ is adjusted to just before the line that will receive the filtered
1847 " data
1848 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1849 " The filtered data is read into the buffer, and the source lines are
1850 " still present, so the range is after the source lines
1851 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1852 %!cat XtestFilter
1853 " After the filtered data is read, the original lines are deleted
1854 call assert_equal([1, 8], [line("'["), line("']")])
1855 au! FilterWritePre,FilterReadPre,FilterReadPost
1856 undo
1857
1858 call SetChangeMarks(1, 4)
1859 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1860 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1861 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1862 2,3!cat XtestFilter
1863 call assert_equal([2, 9], [line("'["), line("']")])
1864 au! FilterWritePre,FilterReadPre,FilterReadPost
1865 undo
1866
1867 call delete('XtestFilter')
1868 endif
1869
1870 call SetChangeMarks(1, 4)
1871 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1872 2,3write Xtest2
1873 au! FileWritePre
1874
1875 call SetChangeMarks(2, 3)
1876 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1877 write >> Xtest2
1878 au! FileAppendPre
1879
1880 call SetChangeMarks(1, 4)
1881 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1882 2,3write >> Xtest2
1883 au! FileAppendPre
1884
1885 call SetChangeMarks(1, 1)
1886 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1887 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1888 3read Xtest2
1889 au! FileReadPre,FileReadPost
1890 undo
1891
1892 call SetChangeMarks(4, 4)
1893 " When the line is 0, it's adjusted to 1
1894 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1895 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1896 0read Xtest2
1897 au! FileReadPre,FileReadPost
1898 undo
1899
1900 call SetChangeMarks(4, 4)
1901 " When the line is 0, it's adjusted to 1
1902 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1903 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1904 1read Xtest2
1905 au! FileReadPre,FileReadPost
1906 undo
1907
1908 bwipe!
1909 call delete('Xtest')
1910 call delete('Xtest2')
1911endfunc
1912
1913func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01001914 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01001915
1916 enew!
1917 call setline(1, ['a', 'b', 'c', 'd'])
1918
1919 let shelltemp = &shelltemp
1920 set shelltemp
1921
1922 let g:filter_au = 0
1923 au FilterWritePre * let g:filter_au += 1
1924 au FilterReadPre * let g:filter_au += 1
1925 au FilterReadPost * let g:filter_au += 1
1926 %!cat
1927 call assert_equal(3, g:filter_au)
1928
1929 if has('filterpipe')
1930 set noshelltemp
1931
1932 let g:filter_au = 0
1933 au FilterWritePre * let g:filter_au += 1
1934 au FilterReadPre * let g:filter_au += 1
1935 au FilterReadPost * let g:filter_au += 1
1936 %!cat
1937 call assert_equal(0, g:filter_au)
1938 endif
1939
1940 au! FilterWritePre,FilterReadPre,FilterReadPost
1941 let &shelltemp = shelltemp
1942 bwipe!
1943endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001944
1945func Test_TextYankPost()
1946 enew!
1947 call setline(1, ['foo'])
1948
1949 let g:event = []
1950 au TextYankPost * let g:event = copy(v:event)
1951
1952 call assert_equal({}, v:event)
1953 call assert_fails('let v:event = {}', 'E46:')
1954 call assert_fails('let v:event.mykey = 0', 'E742:')
1955
1956 norm "ayiw
1957 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01001958 \ #{regcontents: ['foo'], regname: 'a', operator: 'y',
1959 \ regtype: 'v', visual: v:false, inclusive: v:true},
1960 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001961 norm y_
1962 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01001963 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
1964 \ visual: v:false, inclusive: v:false},
1965 \ g:event)
Bram Moolenaar37d16732020-06-12 22:09:01 +02001966 norm Vy
1967 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01001968 \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V',
1969 \ visual: v:true, inclusive: v:true},
1970 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001971 call feedkeys("\<C-V>y", 'x')
1972 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01001973 \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161",
1974 \ visual: v:true, inclusive: v:true},
1975 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001976 norm "xciwbar
1977 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01001978 \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v',
1979 \ visual: v:false, inclusive: v:true},
1980 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001981 norm "bdiw
1982 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01001983 \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v',
1984 \ visual: v:false, inclusive: v:true},
1985 \ g:event)
1986
1987 call setline(1, 'foobar')
1988 " exclusive motion
1989 norm $"ay0
1990 call assert_equal(
1991 \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v',
1992 \ visual: v:false, inclusive: v:false},
1993 \ g:event)
1994 " inclusive motion
1995 norm 0"ay$
1996 call assert_equal(
1997 \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v',
1998 \ visual: v:false, inclusive: v:true},
1999 \ g:event)
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002000
2001 call assert_equal({}, v:event)
2002
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002003 if has('clipboard_working') && !has('gui_running')
2004 " Test that when the visual selection is automatically copied to clipboard
2005 " register a TextYankPost is emitted
2006 call setline(1, ['foobar'])
2007
2008 let @* = ''
2009 set clipboard=autoselect
2010 exe "norm! ggviw\<Esc>"
2011 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002012 \ #{regcontents: ['foobar'], regname: '*', operator: 'y',
2013 \ regtype: 'v', visual: v:true, inclusive: v:false},
2014 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002015
2016 let @+ = ''
2017 set clipboard=autoselectplus
2018 exe "norm! ggviw\<Esc>"
2019 call assert_equal(
Bram Moolenaara016eeb2022-04-09 11:37:38 +01002020 \ #{regcontents: ['foobar'], regname: '+', operator: 'y',
2021 \ regtype: 'v', visual: v:true, inclusive: v:false},
2022 \ g:event)
Bram Moolenaarfccbf062020-11-26 20:34:00 +01002023
2024 set clipboard&vim
2025 endif
2026
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01002027 au! TextYankPost
2028 unlet g:event
2029 bwipe!
2030endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01002031
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01002032func Test_autocommand_all_events()
2033 call assert_fails('au * * bwipe', 'E1155:')
2034 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00002035 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01002036endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002037
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002038func Test_autocmd_user()
2039 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
2040 doautocmd User MyEvent
2041 call assert_equal(['MyEvent', 'MyEvent'], s:res)
2042 au! User
2043 unlet s:res
2044endfunc
2045
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002046function s:Before_test_dirchanged()
2047 augroup test_dirchanged
2048 autocmd!
2049 augroup END
2050 let s:li = []
2051 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002052 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002053 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002054 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002055 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002056endfunc
2057
2058function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002059 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002060 call delete(s:dir_foo, 'd')
2061 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002062 augroup test_dirchanged
2063 autocmd!
2064 augroup END
2065endfunc
2066
2067function Test_dirchanged_global()
2068 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002069 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002070 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
2071 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002072 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00002073 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002074 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002075 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002076 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002077 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002078 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002079 call s:After_test_dirchanged()
2080endfunc
2081
2082function Test_dirchanged_local()
2083 call s:Before_test_dirchanged()
2084 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
2085 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002086 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002087 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002088 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002089 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002090 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002091 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002092 call s:After_test_dirchanged()
2093endfunc
2094
2095function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002096 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002097 call s:Before_test_dirchanged()
2098 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002099 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002100 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2101 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2102 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002103 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002104 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002105 exe 'edit ' . s:dir_foo . '/Xfile'
2106 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002107 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2108 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002109 set noacd
2110 bwipe!
2111 call s:After_test_dirchanged()
2112endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002113
2114" Test TextChangedI and TextChangedP
2115func Test_ChangedP()
2116 new
2117 call setline(1, ['foo', 'bar', 'foobar'])
2118 call test_override("char_avail", 1)
2119 set complete=. completeopt=menuone
2120
2121 func! TextChangedAutocmd(char)
2122 let g:autocmd .= a:char
2123 endfunc
2124
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002125 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002126 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2127 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2128 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2129
2130 call cursor(3, 1)
2131 let g:autocmd = ''
2132 call feedkeys("o\<esc>", 'tnix')
2133 call assert_equal('I', g:autocmd)
2134
2135 let g:autocmd = ''
2136 call feedkeys("Sf", 'tnix')
2137 call assert_equal('II', g:autocmd)
2138
2139 let g:autocmd = ''
2140 call feedkeys("Sf\<C-N>", 'tnix')
2141 call assert_equal('IIP', g:autocmd)
2142
2143 let g:autocmd = ''
2144 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2145 call assert_equal('IIPP', g:autocmd)
2146
2147 let g:autocmd = ''
2148 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2149 call assert_equal('IIPPP', g:autocmd)
2150
2151 let g:autocmd = ''
2152 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2153 call assert_equal('IIPPPP', g:autocmd)
2154
2155 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2156 " TODO: how should it handle completeopt=noinsert,noselect?
2157
2158 " CleanUp
2159 call test_override("char_avail", 0)
2160 au! TextChanged
2161 au! TextChangedI
2162 au! TextChangedP
2163 delfu TextChangedAutocmd
2164 unlet! g:autocmd
2165 set complete&vim completeopt&vim
2166
2167 bw!
2168endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002169
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002170let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002171func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002172 if !g:setline_handled
2173 call setline(1, "(x)")
2174 let g:setline_handled = v:true
2175 endif
2176endfunc
2177
2178func Test_TextChangedI_with_setline()
2179 new
2180 call test_override('char_avail', 1)
2181 autocmd TextChangedI <buffer> call SetLineOne()
2182 call feedkeys("i(\<CR>\<Esc>", 'tx')
2183 call assert_equal('(', getline(1))
2184 call assert_equal('x)', getline(2))
2185 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002186 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002187 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002188
Bram Moolenaarca34db32022-01-20 11:17:18 +00002189 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002190 bwipe!
2191endfunc
2192
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002193func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002194 CheckFeature terminal
2195 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002196 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002197 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002198
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002199 " Prepare file for TextChanged event.
2200 call writefile([''], 'Xchanged.txt')
2201 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2202 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002203 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002204 " In ConPTY, there is additional character which is drawn up to the width of
2205 " the screen.
2206 if has('conpty')
2207 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2208 else
2209 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2210 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002211 " It's only adding autocmd, so that no event occurs.
2212 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2213 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002214 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002215 call assert_equal([''], readfile('Xchanged.txt'))
2216
2217 " clean up
2218 call delete('Xchanged.txt')
2219 bwipe!
2220endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002221
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002222func Test_autocmd_nested()
2223 let g:did_nested = 0
2224 augroup Testing
2225 au WinNew * edit somefile
2226 au BufNew * let g:did_nested = 1
2227 augroup END
2228 split
2229 call assert_equal(0, g:did_nested)
2230 close
2231 bwipe! somefile
2232
2233 " old nested argument still works
2234 augroup Testing
2235 au!
2236 au WinNew * nested edit somefile
2237 au BufNew * let g:did_nested = 1
2238 augroup END
2239 split
2240 call assert_equal(1, g:did_nested)
2241 close
2242 bwipe! somefile
2243
2244 " New ++nested argument works
2245 augroup Testing
2246 au!
2247 au WinNew * ++nested edit somefile
2248 au BufNew * let g:did_nested = 1
2249 augroup END
2250 split
2251 call assert_equal(1, g:did_nested)
2252 close
2253 bwipe! somefile
2254
Bram Moolenaarf0775142022-03-04 20:10:38 +00002255 " nested without ++ does not work in Vim9 script
2256 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2257
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002258 augroup Testing
2259 au!
2260 augroup END
2261
2262 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2263 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2264endfunc
2265
2266func Test_autocmd_once()
2267 " Without ++once WinNew triggers twice
2268 let g:did_split = 0
2269 augroup Testing
2270 au WinNew * let g:did_split += 1
2271 augroup END
2272 split
2273 split
2274 call assert_equal(2, g:did_split)
2275 call assert_true(exists('#WinNew'))
2276 close
2277 close
2278
2279 " With ++once WinNew triggers once
2280 let g:did_split = 0
2281 augroup Testing
2282 au!
2283 au WinNew * ++once let g:did_split += 1
2284 augroup END
2285 split
2286 split
2287 call assert_equal(1, g:did_split)
2288 call assert_false(exists('#WinNew'))
2289 close
2290 close
2291
2292 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2293endfunc
2294
Bram Moolenaara68e5952019-04-25 22:22:01 +02002295func Test_autocmd_bufreadpre()
2296 new
2297 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002298 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002299 w! XAutocmdBufReadPre.txt
2300 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002301 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002302 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002303 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002304 wincmd p
2305 let g:wsv1 = winsaveview()
2306 wincmd p
2307 let g:wsv2 = winsaveview()
2308 " triggers BufReadPre, should not move the cursor in either window
2309 " The topline may change one line in a large window.
2310 edit
2311 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2312 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2313 call assert_equal(2, b:bufreadpre)
2314 wincmd p
2315 call assert_equal(g:wsv1.topline, winsaveview().topline)
2316 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2317 call assert_equal(2, b:bufreadpre)
2318 " Now set the cursor position in an BufReadPre autocommand
2319 " (even though the position will be invalid, this should make Vim reset the
2320 " cursor position in the other window.
2321 wincmd p
2322 set cpo+=g
2323 " won't do anything, but try to set the cursor on an invalid lnum
2324 autocmd BufReadPre <buffer> :norm! 70gg
2325 " triggers BufReadPre, should not move the cursor in either window
2326 e
2327 call assert_equal(1, winsaveview().topline)
2328 call assert_equal(1, winsaveview().lnum)
2329 call assert_equal(3, b:bufreadpre)
2330 wincmd p
2331 call assert_equal(g:wsv1.topline, winsaveview().topline)
2332 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2333 call assert_equal(3, b:bufreadpre)
2334 close
2335 close
2336 call delete('XAutocmdBufReadPre.txt')
2337 set cpo-=g
2338endfunc
2339
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002340" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002341
2342" Tests for the following autocommands:
2343" - FileWritePre writing a compressed file
2344" - FileReadPost reading a compressed file
2345" - BufNewFile reading a file template
2346" - BufReadPre decompressing the file to be read
2347" - FilterReadPre substituting characters in the temp file
2348" - FilterReadPost substituting characters after filtering
2349" - FileReadPre set options for decompression
2350" - FileReadPost decompress the file
2351func Test_ReadWrite_Autocmds()
2352 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002353 CheckUnix
2354 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002355
2356 " Make $GZIP empty, "-v" would cause trouble.
2357 let $GZIP = ""
2358
2359 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2360 " being modified outside of Vim (noticed on Solaris).
2361 au FileChangedShell * echo 'caught FileChangedShell'
2362
2363 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2364 augroup Test1
2365 au!
2366 au FileWritePre *.gz '[,']!gzip
2367 au FileWritePost *.gz undo
2368 au FileReadPost *.gz '[,']!gzip -d
2369 augroup END
2370
2371 new
2372 set bin
2373 call append(0, [
2374 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2375 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2376 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2377 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2378 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2379 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2380 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2381 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2382 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2383 \ ])
2384 1,9write! Xtestfile.gz
2385 enew! | close
2386
2387 new
2388 " Read and decompress the testfile
2389 0read Xtestfile.gz
2390 call assert_equal([
2391 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2392 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2393 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2394 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2395 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2396 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2397 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2398 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2399 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2400 \ ], getline(1, 9))
2401 enew! | close
2402
2403 augroup Test1
2404 au!
2405 augroup END
2406
2407 " Test for the FileAppendPre and FileAppendPost autocmds
2408 augroup Test2
2409 au!
2410 au BufNewFile *.c read Xtest.c
2411 au FileAppendPre *.out '[,']s/new/NEW/
2412 au FileAppendPost *.out !cat Xtest.c >> test.out
2413 augroup END
2414
2415 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2416 new foo.c " should load Xtest.c
2417 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2418 w! >> test.out " append it to the output file
2419
2420 let contents = readfile('test.out')
2421 call assert_equal(' * Here is a NEW .c file', contents[2])
2422 call assert_equal(' * Here is a new .c file', contents[5])
2423
2424 call delete('test.out')
2425 enew! | close
2426 augroup Test2
2427 au!
2428 augroup END
2429
2430 " Test for the BufReadPre and BufReadPost autocmds
2431 augroup Test3
2432 au!
2433 " setup autocommands to decompress before reading and re-compress
2434 " afterwards
2435 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2436 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2437 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2438 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2439 augroup END
2440
2441 e! Xtestfile.gz " Edit compressed file
2442 call assert_equal([
2443 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2444 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2445 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2446 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2447 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2448 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2449 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2450 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2451 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2452 \ ], getline(1, 9))
2453
2454 w! >> test.out " Append it to the output file
2455
2456 augroup Test3
2457 au!
2458 augroup END
2459
2460 " Test for the FilterReadPre and FilterReadPost autocmds.
2461 set shelltemp " need temp files here
2462 augroup Test4
2463 au!
2464 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2465 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2466 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2467 au FilterReadPost *.out '[,']s/x/X/g
2468 augroup END
2469
2470 e! test.out " Edit the output file
2471 1,$!cat
2472 call assert_equal([
2473 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2474 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2475 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2476 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2477 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2478 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2479 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2480 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2481 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2482 \ ], getline(1, 9))
2483 call assert_equal([
2484 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2485 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2486 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2487 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2488 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2489 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2490 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2491 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2492 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2493 \ ], readfile('test.out'))
2494
2495 augroup Test4
2496 au!
2497 augroup END
2498 set shelltemp&vim
2499
2500 " Test for the FileReadPre and FileReadPost autocmds.
2501 augroup Test5
2502 au!
2503 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2504 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2505 au FileReadPost *.gz '[,']s/l/L/
2506 augroup END
2507
2508 new
2509 0r Xtestfile.gz " Read compressed file
2510 call assert_equal([
2511 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2512 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2513 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2514 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2515 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2516 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2517 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2518 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2519 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2520 \ ], getline(1, 9))
2521 call assert_equal([
2522 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2523 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2524 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2525 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2526 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2527 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2528 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2529 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2530 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2531 \ ], readfile('Xtestfile.gz'))
2532
2533 augroup Test5
2534 au!
2535 augroup END
2536
2537 au! FileChangedShell
2538 call delete('Xtestfile.gz')
2539 call delete('Xtest.c')
2540 call delete('test.out')
2541endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002542
2543func Test_throw_in_BufWritePre()
2544 new
2545 call setline(1, ['one', 'two', 'three'])
2546 call assert_false(filereadable('Xthefile'))
2547 augroup throwing
2548 au BufWritePre X* throw 'do not write'
2549 augroup END
2550 try
2551 w Xthefile
2552 catch
2553 let caught = 1
2554 endtry
2555 call assert_equal(1, caught)
2556 call assert_false(filereadable('Xthefile'))
2557
2558 bwipe!
2559 au! throwing
2560endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002561
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002562func Test_autocmd_in_try_block()
2563 call mkdir('Xdir')
2564 au BufEnter * let g:fname = expand('%')
2565 try
2566 edit Xdir/
2567 endtry
2568 call assert_match('Xdir', g:fname)
2569
2570 unlet g:fname
2571 au! BufEnter
2572 call delete('Xdir', 'rf')
2573endfunc
2574
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002575func Test_autocmd_SafeState()
2576 CheckRunVimInTerminal
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002577 let g:test_is_flaky = 1
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002578
2579 let lines =<< trim END
2580 let g:safe = 0
2581 let g:again = ''
2582 au SafeState * let g:safe += 1
2583 au SafeStateAgain * let g:again ..= 'x'
2584 func CallTimer()
2585 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2586 endfunc
2587 END
2588 call writefile(lines, 'XSafeState')
2589 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2590
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002591 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002592 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002593 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002594 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002595
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002596 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002597 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002598 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002599
2600 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002601 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002602 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002603 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002604 call term_sendkeys(buf, ":echo g:again\<CR>")
2605 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2606
2607 call StopVimInTerminal(buf)
2608 call delete('XSafeState')
2609endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002610
2611func Test_autocmd_CmdWinEnter()
2612 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002613 CheckFeature cmdwin
2614
Bram Moolenaar23324a02019-10-01 17:39:04 +02002615 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002616 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002617 let b:dummy_var = 'This is a dummy'
2618 autocmd CmdWinEnter * quit
2619 let winnr = winnr('$')
2620 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002621 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002622 call writefile(lines, filename)
2623 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2624
2625 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002626 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002627 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002628 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002629 call term_sendkeys(buf, ":echo &buftype\<cr>")
2630 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2631 call term_sendkeys(buf, ":echo winnr\<cr>")
2632 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2633
2634 " clean up
2635 call StopVimInTerminal(buf)
2636 call delete(filename)
2637endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002638
2639func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002640 CheckFeature quickfix
2641
Bram Moolenaarec66c412019-10-11 21:19:13 +02002642 pedit xx
2643 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002644 augroup winenter
2645 au WinEnter * if winnr('$') > 2 | quit | endif
2646 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002647 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002648
2649 augroup winenter
2650 au! WinEnter
2651 augroup END
2652
2653 bwipe xx
2654 bwipe x
2655 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002656endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002657
2658func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002659 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002660 edit! Xtest
2661 call setline(1, ['a', 'b', 'c', 'd'])
2662
2663 " :lockmarks preserves the marks
2664 call SetChangeMarks(2, 3)
2665 lockmarks write
2666 call assert_equal([2, 3], [line("'["), line("']")])
2667
2668 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2669 " original values for the user
2670 augroup lockmarks
2671 au!
2672 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2673 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2674 augroup END
2675
2676 lockmarks write
2677 call assert_equal([2, 3], [line("'["), line("']")])
2678
2679 if executable('cat')
2680 lockmarks %!cat
2681 call assert_equal([2, 3], [line("'["), line("']")])
2682 endif
2683
2684 lockmarks 3,4write Xtest2
2685 call assert_equal([2, 3], [line("'["), line("']")])
2686
2687 au! lockmarks
2688 augroup! lockmarks
2689 call delete('Xtest')
2690 call delete('Xtest2')
2691endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002692
2693func Test_FileType_spell()
2694 if !isdirectory('/tmp')
2695 throw "Skipped: requires /tmp directory"
2696 endif
2697
2698 " this was crashing with an invalid free()
2699 setglobal spellfile=/tmp/en.utf-8.add
2700 augroup crash
2701 autocmd!
2702 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2703 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2704 autocmd FileType anotherfiletype setlocal spell
2705 augroup END
2706 func! NoCrash() abort
2707 edit /tmp/crashfile
2708 endfunc
2709 call NoCrash()
2710
2711 au! crash
2712 setglobal spellfile=
2713endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002714
Bram Moolenaar406cd902020-02-18 21:54:41 +01002715" Test closing a window or editing another buffer from a FileChangedRO handler
2716" in a readonly buffer
2717func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002718 call test_override('ui_delay', 10)
2719
Bram Moolenaar406cd902020-02-18 21:54:41 +01002720 augroup FileChangedROTest
2721 au!
2722 autocmd FileChangedRO * quit
2723 augroup END
2724 new
2725 set readonly
2726 call assert_fails('normal i', 'E788:')
2727 close
2728 augroup! FileChangedROTest
2729
2730 augroup FileChangedROTest
2731 au!
2732 autocmd FileChangedRO * edit Xfile
2733 augroup END
2734 new
2735 set readonly
2736 call assert_fails('normal i', 'E788:')
2737 close
2738 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002739 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002740endfunc
2741
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002742func LogACmd()
2743 call add(g:logged, line('$'))
2744endfunc
2745
2746func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002747 CheckNotGui
2748
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002749 enew!
2750 tabnew
2751 call setline(1, ['a', 'b', 'c', 'd'])
2752 $
2753 au TermChanged * call LogACmd()
2754 let g:logged = []
2755 let term_save = &term
2756 set term=xterm
2757 call assert_equal([1, 4], g:logged)
2758
2759 au! TermChanged
2760 let &term = term_save
2761 bwipe!
2762endfunc
2763
Bram Moolenaare3284872020-03-19 13:55:03 +01002764" Test for FileReadCmd autocmd
2765func Test_autocmd_FileReadCmd()
2766 func ReadFileCmd()
2767 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2768 endfunc
2769 augroup FileReadCmdTest
2770 au!
2771 au FileReadCmd Xtest call ReadFileCmd()
2772 augroup END
2773
2774 new
2775 read ++bin Xtest
2776 read ++nobin Xtest
2777 read ++edit Xtest
2778 read ++bad=keep Xtest
2779 read ++bad=drop Xtest
2780 read ++bad=- Xtest
2781 read ++ff=unix Xtest
2782 read ++ff=dos Xtest
2783 read ++ff=mac Xtest
2784 read ++enc=utf-8 Xtest
2785
2786 call assert_equal(['',
2787 \ 'v:cmdarg = ++bin',
2788 \ 'v:cmdarg = ++nobin',
2789 \ 'v:cmdarg = ++edit',
2790 \ 'v:cmdarg = ++bad=keep',
2791 \ 'v:cmdarg = ++bad=drop',
2792 \ 'v:cmdarg = ++bad=-',
2793 \ 'v:cmdarg = ++ff=unix',
2794 \ 'v:cmdarg = ++ff=dos',
2795 \ 'v:cmdarg = ++ff=mac',
2796 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2797
2798 close!
2799 augroup FileReadCmdTest
2800 au!
2801 augroup END
2802 delfunc ReadFileCmd
2803endfunc
2804
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002805" Test for passing invalid arguments to autocmd
2806func Test_autocmd_invalid_args()
2807 " Additional character after * for event
2808 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2809 augroup Test
2810 augroup END
2811 " Invalid autocmd event
2812 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2813 " Invalid autocmd event in a autocmd group
2814 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2815 augroup! Test
2816 " Execute all autocmds
2817 call assert_fails('doautocmd * BufEnter', 'E217:')
2818 call assert_fails('augroup! x1a2b3', 'E367:')
2819 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002820 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002821endfunc
2822
2823" Test for deep nesting of autocmds
2824func Test_autocmd_deep_nesting()
2825 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2826 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2827 autocmd! BufEnter Xfile
2828endfunc
2829
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002830" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2831func Test_autocmd_sigusr1()
2832 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002833 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002834
2835 let g:sigusr1_passed = 0
2836 au SigUSR1 * let g:sigusr1_passed = 1
2837 call system('/bin/kill -s usr1 ' . getpid())
2838 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2839
2840 au! SigUSR1
2841 unlet g:sigusr1_passed
2842endfunc
2843
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002844" Test for BufReadPre autocmd deleting the file
2845func Test_BufReadPre_delfile()
2846 augroup TestAuCmd
2847 au!
2848 autocmd BufReadPre Xfile call delete('Xfile')
2849 augroup END
2850 call writefile([], 'Xfile')
2851 call assert_fails('new Xfile', 'E200:')
2852 call assert_equal('Xfile', @%)
2853 call assert_equal(1, &readonly)
2854 call delete('Xfile')
2855 augroup TestAuCmd
2856 au!
2857 augroup END
2858 close!
2859endfunc
2860
2861" Test for BufReadPre autocmd changing the current buffer
2862func Test_BufReadPre_changebuf()
2863 augroup TestAuCmd
2864 au!
2865 autocmd BufReadPre Xfile edit Xsomeotherfile
2866 augroup END
2867 call writefile([], 'Xfile')
2868 call assert_fails('new Xfile', 'E201:')
2869 call assert_equal('Xsomeotherfile', @%)
2870 call assert_equal(1, &readonly)
2871 call delete('Xfile')
2872 augroup TestAuCmd
2873 au!
2874 augroup END
2875 close!
2876endfunc
2877
2878" Test for BufWipeouti autocmd changing the current buffer when reading a file
2879" in an empty buffer with 'f' flag in 'cpo'
2880func Test_BufDelete_changebuf()
2881 new
2882 augroup TestAuCmd
2883 au!
2884 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2885 augroup END
2886 let save_cpo = &cpo
2887 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002888 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002889 call assert_equal('somefile', @%)
2890 let &cpo = save_cpo
2891 augroup TestAuCmd
2892 au!
2893 augroup END
2894 close!
2895endfunc
2896
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002897" Test for the temporary internal window used to execute autocmds
2898func Test_autocmd_window()
2899 %bw!
2900 edit one.txt
2901 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002902 vnew three.txt
2903 tabnew four.txt
2904 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002905 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002906 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002907 au!
2908 au BufEnter * call add(g:blist, [expand('<afile>'),
2909 \ win_gettype(bufwinnr(expand('<afile>')))])
2910 augroup END
2911
2912 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002913 call assert_equal([
2914 \ ['one.txt', 'autocmd'],
2915 \ ['two.txt', ''],
2916 \ ['four.txt', 'autocmd'],
2917 \ ['three.txt', ''],
2918 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002919
Bram Moolenaar832adf92020-06-25 19:01:36 +02002920 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002921 au!
2922 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002923 augroup! aucmd_win_test1
2924 %bw!
2925endfunc
2926
2927" Test for trying to close the temporary window used for executing an autocmd
2928func Test_close_autocmd_window()
2929 %bw!
2930 edit one.txt
2931 tabnew two.txt
2932 augroup aucmd_win_test2
2933 au!
2934 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2935 augroup END
2936
2937 call assert_fails('doautoall BufEnter', 'E813:')
2938
2939 augroup aucmd_win_test2
2940 au!
2941 augroup END
2942 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002943 %bwipe!
2944endfunc
2945
2946" Test for trying to close the tab that has the temporary window for exeucing
2947" an autocmd.
2948func Test_close_autocmd_tab()
2949 edit one.txt
2950 tabnew two.txt
2951 augroup aucmd_win_test
2952 au!
2953 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2954 augroup END
2955
2956 call assert_fails('doautoall BufEnter', 'E813:')
2957
2958 tabonly
2959 augroup aucmd_win_test
2960 au!
2961 augroup END
2962 augroup! aucmd_win_test
2963 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002964endfunc
2965
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00002966func Test_Visual_doautoall_redraw()
2967 call setline(1, ['a', 'b'])
2968 new
2969 wincmd p
2970 call feedkeys("G\<C-V>", 'txn')
2971 autocmd User Explode ++once redraw
2972 doautoall User Explode
2973 %bwipe!
2974endfunc
2975
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01002976" This was using freed memory.
2977func Test_BufNew_arglocal()
2978 arglocal
2979 au BufNew * arglocal
2980 call assert_fails('drop xx', 'E1156:')
2981
2982 au! BufNew
2983endfunc
2984
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002985func Test_autocmd_closes_window()
2986 au BufNew,BufWinLeave * e %e
2987 file yyy
2988 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002989 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002990
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002991 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002992 au! BufNew
2993 au! BufWinLeave
2994endfunc
2995
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002996func Test_autocmd_quit_psearch()
2997 sn aa bb
2998 augroup aucmd_win_test
2999 au!
3000 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
3001 augroup END
3002 ps /
3003
3004 augroup aucmd_win_test
3005 au!
3006 augroup END
3007endfunc
3008
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003009" Fuzzer found some strange combination that caused a crash.
3010func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003011 " For unknown reason this hangs on MS-Windows
3012 CheckNotMSWindows
3013
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003014 augroup aucmd_normal_test
3015 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
3016 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003017 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003018 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003019 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01003020 normal G
3021
3022 augroup aucmd_normal_test
3023 au!
3024 augroup END
3025endfunc
3026
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003027func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01003028 " For unknown reason this hangs on MS-Windows
3029 CheckNotMSWindows
3030
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01003031 au BufWinLeave * nested q
3032 call assert_fails("norm 7q?\n", 'E855:')
3033
3034 au! BufWinLeave
3035 new
3036 only
3037endfunc
3038
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003039func Test_autocmd_vimgrep()
3040 augroup aucmd_vimgrep
3041 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * sb
3042 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * q9�
3043 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01003044 %bwipe!
Bram Moolenaardd07c022021-02-07 13:32:46 +01003045 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003046
3047 augroup aucmd_vimgrep
3048 au!
3049 augroup END
3050endfunc
3051
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003052func Test_autocmd_with_block()
3053 augroup block_testing
3054 au BufReadPost *.xml {
3055 setlocal matchpairs+=<:>
3056 /<start
3057 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02003058 au CursorHold * {
3059 autocmd BufReadPre * ++once echo 'one' | echo 'two'
3060 g:gotSafeState = 77
3061 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003062 augroup END
3063
3064 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
3065 call assert_equal(expected, execute('au BufReadPost *.xml'))
3066
Bram Moolenaar63b91732021-08-05 20:40:03 +02003067 doautocmd CursorHold
3068 call assert_equal(77, g:gotSafeState)
3069 unlet g:gotSafeState
3070
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02003071 augroup block_testing
3072 au!
3073 augroup END
3074endfunc
3075
Christian Brabandtdb3b4462021-10-16 11:58:55 +01003076" Test TextChangedI and TextChanged
3077func Test_Changed_ChangedI()
3078 new
3079 call test_override("char_avail", 1)
3080 let [g:autocmd_i, g:autocmd_n] = ['','']
3081
3082 func! TextChangedAutocmdI(char)
3083 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
3084 endfunc
3085
3086 augroup Test_TextChanged
3087 au!
3088 au TextChanged <buffer> :call TextChangedAutocmdI('N')
3089 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
3090 augroup END
3091
3092 call feedkeys("ifoo\<esc>", 'tnix')
3093 " TODO: Test test does not seem to trigger TextChanged autocommand, this
3094 " requires running Vim in a terminal window.
3095 " call assert_equal('N3', g:autocmd_n)
3096 call assert_equal('I3', g:autocmd_i)
3097
3098 call feedkeys("yyp", 'tnix')
3099 " TODO: Test test does not seem to trigger TextChanged autocommand.
3100 " call assert_equal('N4', g:autocmd_n)
3101 call assert_equal('I3', g:autocmd_i)
3102
3103 " CleanUp
3104 call test_override("char_avail", 0)
3105 au! TextChanged <buffer>
3106 au! TextChangedI <buffer>
3107 augroup! Test_TextChanged
3108 delfu TextChangedAutocmdI
3109 unlet! g:autocmd_i g:autocmd_n
3110
3111 bw!
3112endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003113
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003114func Test_closing_autocmd_window()
3115 let lines =<< trim END
3116 edit Xa.txt
3117 tabnew Xb.txt
3118 autocmd BufEnter Xa.txt unhide 1
3119 doautoall BufEnter
3120 END
3121 call v9.CheckScriptFailure(lines, 'E814:')
3122 au! BufEnter
3123 only!
3124 bwipe Xa.txt
3125 bwipe Xb.txt
3126endfunc
3127
Bram Moolenaar347538f2022-03-26 16:28:06 +00003128func Test_bufwipeout_changes_window()
3129 " This should not crash, but we don't have any expectations about what
3130 " happens, changing window in BufWipeout has unpredictable results.
3131 tabedit
3132 let g:window_id = win_getid()
3133 topleft new
3134 setlocal bufhidden=wipe
3135 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3136 tabprevious
3137 +tabclose
3138
3139 unlet g:window_id
3140 au! BufWipeout
3141 %bwipe!
3142endfunc
3143
zeertzjq021996f2022-04-10 11:44:04 +01003144func Test_v_event_readonly()
3145 autocmd CompleteChanged * let v:event.width = 0
3146 call assert_fails("normal! i\<C-X>\<C-V>", 'E46:')
3147 au! CompleteChanged
3148
3149 autocmd DirChangedPre * let v:event.directory = ''
3150 call assert_fails('cd .', 'E46:')
3151 au! DirChangedPre
3152
3153 autocmd ModeChanged * let v:event.new_mode = ''
3154 call assert_fails('normal! cc', 'E46:')
3155 au! ModeChanged
3156
3157 autocmd TextYankPost * let v:event.operator = ''
3158 call assert_fails('normal! yy', 'E46:')
3159 au! TextYankPost
3160endfunc
3161
Bram Moolenaar347538f2022-03-26 16:28:06 +00003162
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003163" vim: shiftwidth=2 sts=2 expandtab