blob: eb1fa046c59575b5130d4d7f037e13a72a073810 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02005source term_util.vim
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00006import './vim9.vim' as v9
Bram Moolenaar8c64a362018-03-23 22:39:31 +01007
Bram Moolenaar1e115362019-01-09 23:01:02 +01008func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +02009 for bnr in range(1, bufnr('$'))
10 if bufloaded(bnr) && bufnr('%') != bnr
11 execute 'bd! ' . bnr
12 endif
13 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020014endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020015
Bram Moolenaar14735512016-03-26 21:00:08 +010016func Test_vim_did_enter()
17 call assert_false(v:vim_did_enter)
18
19 " This script will never reach the main loop, can't check if v:vim_did_enter
20 " becomes one.
21endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020022
Bram Moolenaar75911162020-07-21 19:44:47 +020023" Test for the CursorHold autocmd
24func Test_CursorHold_autocmd()
25 CheckRunVimInTerminal
26 call writefile(['one', 'two', 'three'], 'Xfile')
27 let before =<< trim END
28 set updatetime=10
29 au CursorHold * call writefile([line('.')], 'Xoutput', 'a')
30 END
31 call writefile(before, 'Xinit')
32 let buf = RunVimInTerminal('-S Xinit Xfile', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020033 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020034 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020035 call term_sendkeys(buf, "gg")
36 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020037 call WaitForAssert({-> assert_equal(['1'], readfile('Xoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020038 call term_sendkeys(buf, "j")
39 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020040 call WaitForAssert({-> assert_equal(['1', '2'], readfile('Xoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020041 call term_sendkeys(buf, "j")
42 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020043 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('Xoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020044 call StopVimInTerminal(buf)
45
Bram Moolenaar75911162020-07-21 19:44:47 +020046 call delete('Xinit')
47 call delete('Xoutput')
48 call delete('Xfile')
49endfunc
50
Bram Moolenaarc67e8922016-05-24 16:07:40 +020051if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020052
Bram Moolenaarc67e8922016-05-24 16:07:40 +020053 func ExitInsertMode(id)
54 call feedkeys("\<Esc>")
55 endfunc
56
57 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020058 " Need to move the cursor.
59 call feedkeys("ggG", "xt")
60
Bram Moolenaarc67e8922016-05-24 16:07:40 +020061 let g:triggered = 0
62 au CursorHoldI * let g:triggered += 1
63 set updatetime=20
Bram Moolenaar92bb83e2021-02-03 23:04:46 +010064 call timer_start(200, 'ExitInsertMode')
Bram Moolenaarc67e8922016-05-24 16:07:40 +020065 call feedkeys('a', 'x!')
66 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010067 unlet g:triggered
68 au! CursorHoldI
69 set updatetime&
70 endfunc
71
72 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020073 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010074 " Need to move the cursor.
75 call feedkeys("ggG", "xt")
76
77 " Confirm the timer invoked in exit_cb of the job doesn't disturb
78 " CursorHoldI event.
79 let g:triggered = 0
80 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020081 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010082 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020083 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010084 call feedkeys('a', 'x!')
85 call assert_equal(1, g:triggered)
86 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020087 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020088 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020089 endfunc
90
91 func Test_cursorhold_insert_ctrl_x()
92 let g:triggered = 0
93 au CursorHoldI * let g:triggered += 1
94 set updatetime=20
95 call timer_start(100, 'ExitInsertMode')
96 " CursorHoldI does not trigger after CTRL-X
97 call feedkeys("a\<C-X>", 'x!')
98 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010099 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +0200100 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200101 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200102 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200103
Bram Moolenaar5a9357d2021-10-03 16:22:05 +0100104 func Test_cursorhold_insert_ctrl_g_U()
105 au CursorHoldI * :
106 set updatetime=20
107 new
108 call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') })
109 call feedkeys("i()\<C-g>U", 'tx!')
110 sleep 200m
111 call assert_equal('(foo)', getline(1))
112 undo
113 call assert_equal('', getline(1))
114
115 bwipe!
116 au! CursorHoldI
117 set updatetime&
118 endfunc
119
Bram Moolenaar97b00752019-05-12 13:07:14 +0200120 func Test_OptionSet_modeline()
121 call test_override('starting', 1)
122 au! OptionSet
123 augroup set_tabstop
124 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
125 augroup END
126 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline')
127 set modeline
128 let v:errmsg = ''
129 call assert_fails('split XoptionsetModeline', 'E12:')
130 call assert_equal(7, &ts)
131 call assert_equal('', v:errmsg)
132
133 augroup set_tabstop
134 au!
135 augroup END
136 bwipe!
137 set ts&
138 call delete('XoptionsetModeline')
139 call test_override('starting', 0)
140 endfunc
141
142endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200143
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200144func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200145 augroup test_bufunload_group
146 autocmd!
147 autocmd BufUnload * call add(s:li, "bufunload")
148 autocmd BufDelete * call add(s:li, "bufdelete")
149 autocmd BufWipeout * call add(s:li, "bufwipeout")
150 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200151
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100152 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200153 new
154 setlocal bufhidden=
155 bunload
156 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200157
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100158 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200159 new
160 setlocal bufhidden=delete
161 bunload
162 call assert_equal(["bufunload", "bufdelete"], s:li)
163
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100164 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200165 new
166 setlocal bufhidden=unload
167 bwipeout
168 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
169
Bram Moolenaare99e8442016-07-26 20:43:40 +0200170 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200171 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200172endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200173
174" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200175func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200176 tabedit
177 tabfirst
178
179 augroup test_autocmd_bufunload_with_tabnext_group
180 autocmd!
181 autocmd BufUnload <buffer> tabnext
182 augroup END
183
184 quit
185 call assert_equal(2, tabpagenr('$'))
186
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200187 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200188 augroup! test_autocmd_bufunload_with_tabnext_group
189 tablast
190 quit
191endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200192
Bram Moolenaar5ed58c72021-01-28 14:24:55 +0100193func Test_argdelete_in_next()
194 au BufNew,BufEnter,BufLeave,BufWinEnter * argdel
195 call assert_fails('next a b', 'E1156:')
196 au! BufNew,BufEnter,BufLeave,BufWinEnter *
197endfunc
198
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200199func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200200 tabedit
201 augroup sample
202 autocmd!
203 autocmd BufWinLeave <buffer> tabfirst
204 augroup END
205 call setline(1, ['a', 'b', 'c'])
206 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200207 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200208endfunc
209
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200210" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200211func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200212 split aa.txt
213 let lastbuf = bufnr('$')
214
215 augroup test_autocmd_bufunload
216 autocmd!
217 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
218 augroup END
219
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100220 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200221
222 autocmd! test_autocmd_bufunload
223 augroup! test_autocmd_bufunload
224 bwipe! aa.txt
225 bwipe! bb.txt
226endfunc
227
228" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200229func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200230 setlocal buftype=nowrite
231 let lastbuf = bufnr('$')
232
233 augroup test_autocmd_bufunload
234 autocmd!
235 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
236 augroup END
237
238 normal! i1
239 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200240
241 autocmd! test_autocmd_bufunload
242 augroup! test_autocmd_bufunload
243 bwipe! a.txt
244endfunc
245
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100246func Test_autocmd_dummy_wipeout()
247 " prepare files
248 call writefile([''], 'Xdummywipetest1.txt')
249 call writefile([''], 'Xdummywipetest2.txt')
250 augroup test_bufunload_group
251 autocmd!
252 autocmd BufUnload * call add(s:li, "bufunload")
253 autocmd BufDelete * call add(s:li, "bufdelete")
254 autocmd BufWipeout * call add(s:li, "bufwipeout")
255 augroup END
256
257 let s:li = []
258 split Xdummywipetest1.txt
259 silent! vimgrep /notmatched/ Xdummywipetest*
260 call assert_equal(["bufunload", "bufwipeout"], s:li)
261
262 bwipeout
263 call delete('Xdummywipetest1.txt')
264 call delete('Xdummywipetest2.txt')
265 au! test_bufunload_group
266 augroup! test_bufunload_group
267endfunc
268
Bram Moolenaarc917da42016-07-19 22:31:36 +0200269func Test_win_tab_autocmd()
270 let g:record = []
271
272 augroup testing
273 au WinNew * call add(g:record, 'WinNew')
naohiro ono23beefe2021-11-13 12:38:49 +0000274 au WinClosed * call add(g:record, 'WinClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200275 au WinEnter * call add(g:record, 'WinEnter')
276 au WinLeave * call add(g:record, 'WinLeave')
277 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200278 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200279 au TabEnter * call add(g:record, 'TabEnter')
280 au TabLeave * call add(g:record, 'TabLeave')
281 augroup END
282
283 split
284 tabnew
285 close
286 close
287
288 call assert_equal([
289 \ 'WinLeave', 'WinNew', 'WinEnter',
290 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000291 \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter',
292 \ 'WinLeave', 'WinClosed', 'WinEnter'
Bram Moolenaarc917da42016-07-19 22:31:36 +0200293 \ ], g:record)
294
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200295 let g:record = []
296 tabnew somefile
297 tabnext
298 bwipe somefile
299
300 call assert_equal([
301 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
302 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
naohiro ono23beefe2021-11-13 12:38:49 +0000303 \ 'WinClosed', 'TabClosed'
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200304 \ ], g:record)
305
Bram Moolenaarc917da42016-07-19 22:31:36 +0200306 augroup testing
307 au!
308 augroup END
309 unlet g:record
310endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200311
naohiro ono23beefe2021-11-13 12:38:49 +0000312func Test_WinClosed()
313 " Test that the pattern is matched against the closed window's ID, and both
314 " <amatch> and <afile> are set to it.
315 new
316 let winid = win_getid()
317 let g:matched = v:false
318 augroup test-WinClosed
319 autocmd!
320 execute 'autocmd WinClosed' winid 'let g:matched = v:true'
321 autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>'))
322 autocmd WinClosed * let g:afile = str2nr(expand('<afile>'))
323 augroup END
324 close
325 call assert_true(g:matched)
326 call assert_equal(winid, g:amatch)
327 call assert_equal(winid, g:afile)
328
329 " Test that WinClosed is non-recursive.
330 new
331 new
332 call assert_equal(3, winnr('$'))
333 let g:triggered = 0
334 augroup test-WinClosed
335 autocmd!
336 autocmd WinClosed * let g:triggered += 1
337 autocmd WinClosed * 2 wincmd c
338 augroup END
339 close
340 call assert_equal(1, winnr('$'))
341 call assert_equal(1, g:triggered)
342
343 autocmd! test-WinClosed
344 augroup! test-WinClosed
345 unlet g:matched
346 unlet g:amatch
347 unlet g:afile
348 unlet g:triggered
349endfunc
350
Bram Moolenaarc947b9a2022-04-06 17:59:21 +0100351func Test_WinClosed_throws()
352 vnew
353 let bnr = bufnr()
354 call assert_equal(1, bufloaded(bnr))
355 augroup test-WinClosed
356 autocmd WinClosed * throw 'foo'
357 augroup END
358 try
359 close
360 catch /.*/
361 endtry
362 call assert_equal(0, bufloaded(bnr))
363
364 autocmd! test-WinClosed
365 augroup! test-WinClosed
366endfunc
367
zeertzjq6a069402022-04-07 14:08:29 +0100368func Test_WinClosed_throws_with_tabs()
369 tabnew
370 let bnr = bufnr()
371 call assert_equal(1, bufloaded(bnr))
372 augroup test-WinClosed
373 autocmd WinClosed * throw 'foo'
374 augroup END
375 try
376 close
377 catch /.*/
378 endtry
379 call assert_equal(0, bufloaded(bnr))
380
381 autocmd! test-WinClosed
382 augroup! test-WinClosed
383endfunc
384
Bram Moolenaare99e8442016-07-26 20:43:40 +0200385func s:AddAnAutocmd()
386 augroup vimBarTest
387 au BufReadCmd * echo 'hello'
388 augroup END
389 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
390endfunc
391
392func Test_early_bar()
393 " test that a bar is recognized before the {event}
394 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000395 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200396 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000397 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200398
399 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000400 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200401 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000402 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200403
404 " test that a bar is recognized after the {event}
405 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000406 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200407 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000408 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200409
410 " test that a bar is recognized after the {group}
411 call s:AddAnAutocmd()
412 au! vimBarTest|echo 'hello'
413 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
414endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200415
Bram Moolenaar5c809082016-09-01 16:21:48 +0200416func RemoveGroup()
417 autocmd! StartOK
418 augroup! StartOK
419endfunc
420
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200421func Test_augroup_warning()
422 augroup TheWarning
423 au VimEnter * echo 'entering'
424 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100425 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200426 redir => res
427 augroup! TheWarning
428 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100429 call assert_match("W19:", res)
430 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200431
432 " check "Another" does not take the pace of the deleted entry
433 augroup Another
434 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100435 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200436 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200437
438 " no warning for postpone aucmd delete
439 augroup StartOK
440 au VimEnter * call RemoveGroup()
441 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100442 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200443 redir => res
444 doautocmd VimEnter
445 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100446 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200447 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200448
449 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200450endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200451
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200452func Test_BufReadCmdHelp()
453 " This used to cause access to free memory
454 au BufReadCmd * e +h
455 help
456
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200457 au! BufReadCmd
458endfunc
459
460func Test_BufReadCmdHelpJump()
461 " This used to cause access to free memory
462 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200463 " } to fix highlighting
464 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200465
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200466 au! BufReadCmd
467endfunc
468
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200469func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200470 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200471 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200472 call assert_fails('augroup! x', 'E936:')
473 au VimEnter * echo
474 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200475 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100476 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200477 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200478endfunc
479
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200480" Tests for autocommands on :close command.
481" This used to be in test13.
482func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200483 " Clean up buffers, because in some cases this function fails.
484 call s:cleanup_buffers()
485
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200486 " Write three files and open them, each in a window.
487 " Then go to next window, with autocommand that deletes the previous one.
488 " Do this twice, writing the file.
489 e! Xtestje1
490 call setline(1, 'testje1')
491 w
492 sp Xtestje2
493 call setline(1, 'testje2')
494 w
495 sp Xtestje3
496 call setline(1, 'testje3')
497 w
498 wincmd w
499 au WinLeave Xtestje2 bwipe
500 wincmd w
501 call assert_equal('Xtestje1', expand('%'))
502
503 au WinLeave Xtestje1 bwipe Xtestje3
504 close
505 call assert_equal('Xtestje1', expand('%'))
506
507 " Test deleting the buffer on a Unload event. If this goes wrong there
508 " will be the ATTENTION prompt.
509 e Xtestje1
510 au!
511 au! BufUnload Xtestje1 bwipe
512 call assert_fails('e Xtestje3', 'E937:')
513 call assert_equal('Xtestje3', expand('%'))
514
515 e Xtestje2
516 sp Xtestje1
517 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200518 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200519
520 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
521 " there are ml_line errors and/or a Crash.
522 au!
523 only
524 e Xanother
525 e Xtestje1
526 bwipe Xtestje2
527 bwipe Xtestje3
528 au BufWipeout Xtestje1 buf Xtestje1
529 bwipe
530 call assert_equal('Xanother', expand('%'))
531
532 only
533 help
534 wincmd w
535 1quit
536 call assert_equal('Xanother', expand('%'))
537
538 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100539 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200540 call delete('Xtestje1')
541 call delete('Xtestje2')
542 call delete('Xtestje3')
543endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100544
545func Test_BufEnter()
546 au! BufEnter
547 au Bufenter * let val = val . '+'
548 let g:val = ''
549 split NewFile
550 call assert_equal('+', g:val)
551 bwipe!
552 call assert_equal('++', g:val)
553
554 " Also get BufEnter when editing a directory
555 call mkdir('Xdir')
556 split Xdir
557 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100558
559 " On MS-Windows we can't edit the directory, make sure we wipe the right
560 " buffer.
561 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100562
563 call delete('Xdir', 'd')
564 au! BufEnter
565endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100566
567" Closing a window might cause an endless loop
568" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200569func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200570 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100571 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200572 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100573 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100574 mksession!
575
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200576 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200577 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200578 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100579 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200580 augroup test_autocmd_sessionload
581 autocmd!
582 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
583 augroup END
584
585 func WriteErrors()
586 call writefile([execute("messages")], "Xerrors")
587 endfunc
588 au VimLeave * call WriteErrors()
589 [CODE]
590
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100591 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200592 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100593 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200594 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100595
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100596 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100597 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100598 call delete(file)
599 endfor
600endfunc
601
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100602" Using :blast and :ball for many events caused a crash, because b_nwindows was
603" not incremented correctly.
604func Test_autocmd_blast_badd()
605 let content =<< trim [CODE]
606 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
607 edit foo1
608 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
609 edit foo2
610 call writefile(['OK'], 'Xerrors')
611 qall
612 [CODE]
613
614 call writefile(content, 'XblastBall')
615 call system(GetVimCommand() .. ' --clean -S XblastBall')
616 call assert_match('OK', readfile('Xerrors')->join())
617
618 call delete('XblastBall')
619 call delete('Xerrors')
620endfunc
621
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100622" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200623func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100624 tabnew
625 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100626 mksession!
627
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200628 let content =<< trim [CODE]
629 set nocp noswapfile
630 function! DeleteInactiveBufs()
631 tabfirst
632 let tabblist = []
633 for i in range(1, tabpagenr(''$''))
634 call extend(tabblist, tabpagebuflist(i))
635 endfor
636 for b in range(1, bufnr(''$''))
637 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
638 exec ''bwipeout '' . b
639 endif
640 endfor
641 echomsg "SessionLoadPost DONE"
642 endfunction
643 au SessionLoadPost * call DeleteInactiveBufs()
644
645 func WriteErrors()
646 call writefile([execute("messages")], "Xerrors")
647 endfunc
648 au VimLeave * call WriteErrors()
649 [CODE]
650
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100651 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200652 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100653 let errors = join(readfile('Xerrors'))
654 " This probably only ever matches on unix.
655 call assert_notmatch('Caught deadly signal SEGV', errors)
656 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100657
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100658 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100659 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100660 call delete(file)
661 endfor
662endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200663
664func Test_empty_doau()
665 doau \|
666endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200667
668func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200669 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200670 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200671 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
672 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 +0200673 let g:opt = [expected, actual]
674 "call assert_equal(expected, actual)
675endfunc
676
677func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200678 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200679
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200680 badd test_autocmd.vim
681
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200682 call test_override('starting', 1)
683 set nocp
684 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
685
686 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100687 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200688 set nu
689 call assert_equal([], g:options)
690 call assert_equal(g:opt[0], g:opt[1])
691
692 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100693 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200694 setlocal nonu
695 call assert_equal([], g:options)
696 call assert_equal(g:opt[0], g:opt[1])
697
698 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100699 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200700 setglobal nonu
701 call assert_equal([], g:options)
702 call assert_equal(g:opt[0], g:opt[1])
703
704 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100705 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200706 setlocal ai
707 call assert_equal([], g:options)
708 call assert_equal(g:opt[0], g:opt[1])
709
710 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100711 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200712 setglobal ai
713 call assert_equal([], g:options)
714 call assert_equal(g:opt[0], g:opt[1])
715
716 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100717 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200718 set ai!
719 call assert_equal([], g:options)
720 call assert_equal(g:opt[0], g:opt[1])
721
722 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100723 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200724 noa setlocal ai
725 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200726 set ai!
727 call assert_equal([], g:options)
728 call assert_equal(g:opt[0], g:opt[1])
729
730 " Should not print anything, use :noa
731 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100732 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200733 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200734 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200735 call assert_equal(g:opt[0], g:opt[1])
736
737 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100738 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200739 set list nu
740 call assert_equal([], g:options)
741 call assert_equal(g:opt[0], g:opt[1])
742
743 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100744 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200745 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200746 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 +0200747 call assert_equal(g:opt[0], g:opt[1])
748
749 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100750 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200751 setlocal acd
752 call assert_equal([], g:options)
753 call assert_equal(g:opt[0], g:opt[1])
754
755 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100756 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200757 set ar
758 call assert_equal([], g:options)
759 call assert_equal(g:opt[0], g:opt[1])
760
761 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100762 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200763 setlocal ar
764 call assert_equal([], g:options)
765 call assert_equal(g:opt[0], g:opt[1])
766
767 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100768 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200769 setglobal invar
770 call assert_equal([], g:options)
771 call assert_equal(g:opt[0], g:opt[1])
772
773 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100774 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
775 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200776 call assert_equal([], g:options)
777 call assert_equal(g:opt[0], g:opt[1])
778
779 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100780 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200781 " try twice, first time, shouldn't trigger because option name is invalid,
782 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200783 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200784 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200785 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200786 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200787 call assert_equal([], g:options)
788 call assert_equal(g:opt[0], g:opt[1])
789
790 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100791 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200792 call setwinvar(0, '&number', 1)
793 call assert_equal([], g:options)
794 call assert_equal(g:opt[0], g:opt[1])
795
796 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100797 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200798 setlocal key=blah
799 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200800 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200801 call assert_equal(g:opt[0], g:opt[1])
802
Bram Moolenaard7c96872019-06-15 17:12:48 +0200803
804 " 18a: Setting string global option"
805 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100806 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200807 set backupext=foo
808 call assert_equal([], g:options)
809 call assert_equal(g:opt[0], g:opt[1])
810
811 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100812 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200813 set backupext&
814 call assert_equal([], g:options)
815 call assert_equal(g:opt[0], g:opt[1])
816
817 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100818 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200819 setglobal backupext=bar
820 call assert_equal([], g:options)
821 call assert_equal(g:opt[0], g:opt[1])
822
823 " 18d: Setting local string global option"
824 " As this is a global option this sets the global value even though
825 " :setlocal is used!
826 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100827 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200828 setlocal backupext=baz
829 call assert_equal([], g:options)
830 call assert_equal(g:opt[0], g:opt[1])
831
832 " 18e: Setting again string global option"
833 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
834 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100835 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200836 set backupext=fuu
837 call assert_equal([], g:options)
838 call assert_equal(g:opt[0], g:opt[1])
839
840
zeertzjqb811de52021-10-21 10:50:44 +0100841 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200842 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100843 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200844 set tags=tagpath
845 call assert_equal([], g:options)
846 call assert_equal(g:opt[0], g:opt[1])
847
zeertzjqb811de52021-10-21 10:50:44 +0100848 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100849 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200850 set tags&
851 call assert_equal([], g:options)
852 call assert_equal(g:opt[0], g:opt[1])
853
zeertzjqb811de52021-10-21 10:50:44 +0100854 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100855 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200856 setglobal tags=tagpath1
857 call assert_equal([], g:options)
858 call assert_equal(g:opt[0], g:opt[1])
859
zeertzjqb811de52021-10-21 10:50:44 +0100860 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100861 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200862 setlocal tags=tagpath2
863 call assert_equal([], g:options)
864 call assert_equal(g:opt[0], g:opt[1])
865
zeertzjqb811de52021-10-21 10:50:44 +0100866 " 19e: Setting again string global-local (to buffer) option"
867 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200868 " but the old local value for all other kinds of options.
869 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
870 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100871 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200872 set tags=tagpath
873 call assert_equal([], g:options)
874 call assert_equal(g:opt[0], g:opt[1])
875
zeertzjqb811de52021-10-21 10:50:44 +0100876 " 19f: Setting string global-local (to buffer) option to an empty string"
877 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200878 " but the old local value for all other kinds of options.
879 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
880 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100881 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200882 set tags=tagpath
883 call assert_equal([], g:options)
884 call assert_equal(g:opt[0], g:opt[1])
885
886
887 " 20a: Setting string local (to buffer) option"
888 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100889 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200890 set spelllang=elvish,klingon
891 call assert_equal([], g:options)
892 call assert_equal(g:opt[0], g:opt[1])
893
894 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100895 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200896 set spelllang&
897 call assert_equal([], g:options)
898 call assert_equal(g:opt[0], g:opt[1])
899
900 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100901 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200902 setglobal spelllang=elvish
903 call assert_equal([], g:options)
904 call assert_equal(g:opt[0], g:opt[1])
905
906 " 20d: Setting local string local (to buffer) option"
907 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100908 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200909 setlocal spelllang=klingon
910 call assert_equal([], g:options)
911 call assert_equal(g:opt[0], g:opt[1])
912
913 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +0100914 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200915 " but the old local value for all other kinds of options.
916 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
917 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100918 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200919 set spelllang=foo
920 call assert_equal([], g:options)
921 call assert_equal(g:opt[0], g:opt[1])
922
923
zeertzjqb811de52021-10-21 10:50:44 +0100924 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200925 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100926 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200927 set statusline=foo
928 call assert_equal([], g:options)
929 call assert_equal(g:opt[0], g:opt[1])
930
zeertzjqb811de52021-10-21 10:50:44 +0100931 " 21b: Resetting string global-local (to window) option"
932 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200933 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100934 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200935 set statusline&
936 call assert_equal([], g:options)
937 call assert_equal(g:opt[0], g:opt[1])
938
zeertzjqb811de52021-10-21 10:50:44 +0100939 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100940 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200941 setglobal statusline=bar
942 call assert_equal([], g:options)
943 call assert_equal(g:opt[0], g:opt[1])
944
zeertzjqb811de52021-10-21 10:50:44 +0100945 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200946 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100947 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200948 setlocal statusline=baz
949 call assert_equal([], g:options)
950 call assert_equal(g:opt[0], g:opt[1])
951
zeertzjqb811de52021-10-21 10:50:44 +0100952 " 21e: Setting again string global-local (to window) option"
953 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200954 " but the old local value for all other kinds of options.
955 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
956 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100957 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200958 set statusline=foo
959 call assert_equal([], g:options)
960 call assert_equal(g:opt[0], g:opt[1])
961
962
963 " 22a: Setting string local (to window) option"
964 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100965 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200966 set foldignore=fo
967 call assert_equal([], g:options)
968 call assert_equal(g:opt[0], g:opt[1])
969
970 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100971 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200972 set foldignore&
973 call assert_equal([], g:options)
974 call assert_equal(g:opt[0], g:opt[1])
975
976 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100977 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200978 setglobal foldignore=bar
979 call assert_equal([], g:options)
980 call assert_equal(g:opt[0], g:opt[1])
981
982 " 22d: Setting local string local (to window) option"
983 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100984 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200985 setlocal foldignore=baz
986 call assert_equal([], g:options)
987 call assert_equal(g:opt[0], g:opt[1])
988
989 " 22e: Setting again string local (to window) option"
990 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
991 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100992 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200993 set foldignore=fo
994 call assert_equal([], g:options)
995 call assert_equal(g:opt[0], g:opt[1])
996
997
zeertzjqb811de52021-10-21 10:50:44 +0100998 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200999 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1000 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001001 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001002 setglobal cmdheight=2
1003 call assert_equal([], g:options)
1004 call assert_equal(g:opt[0], g:opt[1])
1005
1006 " 23b: Setting local number global option"
1007 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1008 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001009 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001010 setlocal cmdheight=2
1011 call assert_equal([], g:options)
1012 call assert_equal(g:opt[0], g:opt[1])
1013
1014 " 23c: Setting again number global option"
1015 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
1016 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001017 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001018 set cmdheight=2
1019 call assert_equal([], g:options)
1020 call assert_equal(g:opt[0], g:opt[1])
1021
1022 " 23d: Setting again number global option"
1023 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001024 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001025 set cmdheight=2
1026 call assert_equal([], g:options)
1027 call assert_equal(g:opt[0], g:opt[1])
1028
1029
1030 " 24a: Setting global number global-local (to buffer) option"
1031 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1032 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001033 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001034 setglobal undolevels=2
1035 call assert_equal([], g:options)
1036 call assert_equal(g:opt[0], g:opt[1])
1037
1038 " 24b: Setting local number global-local (to buffer) option"
1039 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1040 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001041 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001042 setlocal undolevels=2
1043 call assert_equal([], g:options)
1044 call assert_equal(g:opt[0], g:opt[1])
1045
1046 " 24c: Setting again number global-local (to buffer) option"
1047 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1048 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001049 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001050 set undolevels=2
1051 call assert_equal([], g:options)
1052 call assert_equal(g:opt[0], g:opt[1])
1053
1054 " 24d: Setting again global number global-local (to buffer) option"
1055 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001056 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001057 set undolevels=2
1058 call assert_equal([], g:options)
1059 call assert_equal(g:opt[0], g:opt[1])
1060
1061
1062 " 25a: Setting global number local (to buffer) option"
1063 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1064 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001065 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001066 setglobal wrapmargin=2
1067 call assert_equal([], g:options)
1068 call assert_equal(g:opt[0], g:opt[1])
1069
1070 " 25b: Setting local number local (to buffer) option"
1071 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1072 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001073 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001074 setlocal wrapmargin=2
1075 call assert_equal([], g:options)
1076 call assert_equal(g:opt[0], g:opt[1])
1077
1078 " 25c: Setting again number local (to buffer) option"
1079 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1080 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001081 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001082 set wrapmargin=2
1083 call assert_equal([], g:options)
1084 call assert_equal(g:opt[0], g:opt[1])
1085
1086 " 25d: Setting again global number local (to buffer) option"
1087 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001088 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001089 set wrapmargin=2
1090 call assert_equal([], g:options)
1091 call assert_equal(g:opt[0], g:opt[1])
1092
1093
1094 " 26: Setting number global-local (to window) option.
1095 " Such option does currently not exist.
1096
1097
1098 " 27a: Setting global number local (to window) option"
1099 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1100 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001101 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001102 setglobal foldcolumn=2
1103 call assert_equal([], g:options)
1104 call assert_equal(g:opt[0], g:opt[1])
1105
1106 " 27b: Setting local number local (to window) option"
1107 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1108 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001109 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001110 setlocal foldcolumn=2
1111 call assert_equal([], g:options)
1112 call assert_equal(g:opt[0], g:opt[1])
1113
1114 " 27c: Setting again number local (to window) option"
1115 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1116 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001117 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001118 set foldcolumn=2
1119 call assert_equal([], g:options)
1120 call assert_equal(g:opt[0], g:opt[1])
1121
zeertzjqb811de52021-10-21 10:50:44 +01001122 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001123 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001124 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001125 set foldcolumn=2
1126 call assert_equal([], g:options)
1127 call assert_equal(g:opt[0], g:opt[1])
1128
1129
1130 " 28a: Setting global boolean global option"
1131 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1132 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001133 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001134 setglobal nowrapscan
1135 call assert_equal([], g:options)
1136 call assert_equal(g:opt[0], g:opt[1])
1137
1138 " 28b: Setting local boolean global option"
1139 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1140 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001141 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001142 setlocal nowrapscan
1143 call assert_equal([], g:options)
1144 call assert_equal(g:opt[0], g:opt[1])
1145
1146 " 28c: Setting again boolean global option"
1147 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1148 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001149 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001150 set nowrapscan
1151 call assert_equal([], g:options)
1152 call assert_equal(g:opt[0], g:opt[1])
1153
1154 " 28d: Setting again global boolean global option"
1155 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001156 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001157 set wrapscan
1158 call assert_equal([], g:options)
1159 call assert_equal(g:opt[0], g:opt[1])
1160
1161
1162 " 29a: Setting global boolean global-local (to buffer) option"
1163 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1164 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001165 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001166 setglobal autoread
1167 call assert_equal([], g:options)
1168 call assert_equal(g:opt[0], g:opt[1])
1169
1170 " 29b: Setting local boolean global-local (to buffer) option"
1171 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1172 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001173 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001174 setlocal noautoread
1175 call assert_equal([], g:options)
1176 call assert_equal(g:opt[0], g:opt[1])
1177
1178 " 29c: Setting again boolean global-local (to buffer) option"
1179 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1180 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001181 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001182 set autoread
1183 call assert_equal([], g:options)
1184 call assert_equal(g:opt[0], g:opt[1])
1185
1186 " 29d: Setting again global boolean global-local (to buffer) option"
1187 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001188 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001189 set autoread
1190 call assert_equal([], g:options)
1191 call assert_equal(g:opt[0], g:opt[1])
1192
1193
1194 " 30a: Setting global boolean local (to buffer) option"
1195 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1196 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001197 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001198 setglobal cindent
1199 call assert_equal([], g:options)
1200 call assert_equal(g:opt[0], g:opt[1])
1201
1202 " 30b: Setting local boolean local (to buffer) option"
1203 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1204 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001205 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001206 setlocal nocindent
1207 call assert_equal([], g:options)
1208 call assert_equal(g:opt[0], g:opt[1])
1209
1210 " 30c: Setting again boolean local (to buffer) option"
1211 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1212 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001213 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001214 set cindent
1215 call assert_equal([], g:options)
1216 call assert_equal(g:opt[0], g:opt[1])
1217
1218 " 30d: Setting again global boolean local (to buffer) option"
1219 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001220 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001221 set cindent
1222 call assert_equal([], g:options)
1223 call assert_equal(g:opt[0], g:opt[1])
1224
1225
1226 " 31: Setting boolean global-local (to window) option
1227 " Currently no such option exists.
1228
1229
1230 " 32a: Setting global boolean local (to window) option"
1231 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1232 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001233 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001234 setglobal cursorcolumn
1235 call assert_equal([], g:options)
1236 call assert_equal(g:opt[0], g:opt[1])
1237
1238 " 32b: Setting local boolean local (to window) option"
1239 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1240 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001241 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001242 setlocal nocursorcolumn
1243 call assert_equal([], g:options)
1244 call assert_equal(g:opt[0], g:opt[1])
1245
1246 " 32c: Setting again boolean local (to window) option"
1247 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1248 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001249 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001250 set cursorcolumn
1251 call assert_equal([], g:options)
1252 call assert_equal(g:opt[0], g:opt[1])
1253
1254 " 32d: Setting again global boolean local (to window) option"
1255 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001256 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001257 set cursorcolumn
1258 call assert_equal([], g:options)
1259 call assert_equal(g:opt[0], g:opt[1])
1260
1261
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001262 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001263 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001264 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001265 set backspace=2
1266 call assert_equal([], g:options)
1267 call assert_equal(g:opt[0], g:opt[1])
1268
1269
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001270 " Cleanup
1271 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001272 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001273 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 +02001274 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001275 endfor
1276 call test_override('starting', 0)
1277 delfunc! AutoCommandOptionSet
1278endfunc
1279
1280func Test_OptionSet_diffmode()
1281 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001282 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001283 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001284 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001285
1286 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1287 call assert_equal(0, &l:cul)
1288 diffthis
1289 call assert_equal(1, &l:cul)
1290
1291 vnew
1292 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1293 call assert_equal(0, &l:cul)
1294 diffthis
1295 call assert_equal(1, &l:cul)
1296
1297 diffoff
1298 call assert_equal(0, &l:cul)
1299 call assert_equal(1, getwinvar(2, '&l:cul'))
1300 bw!
1301
1302 call assert_equal(1, &l:cul)
1303 diffoff!
1304 call assert_equal(0, &l:cul)
1305 call assert_equal(0, getwinvar(1, '&l:cul'))
1306 bw!
1307
1308 " Cleanup
1309 au! OptionSet
1310 call test_override('starting', 0)
1311endfunc
1312
1313func Test_OptionSet_diffmode_close()
1314 call test_override('starting', 1)
1315 " 19: Try to close the current window when entering diff mode
1316 " should not segfault
1317 new
1318 au OptionSet diff close
1319
1320 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001321 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001322 call assert_equal(1, &diff)
1323 vnew
1324 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001325 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001326 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001327 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001328 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001329 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001330 bw!
1331
1332 " Cleanup
1333 au! OptionSet
1334 call test_override('starting', 0)
1335 "delfunc! AutoCommandOptionSet
1336endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001337
1338" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1339func Test_BufleaveWithDelete()
1340 new | edit Xfile1
1341
1342 augroup test_bufleavewithdelete
1343 autocmd!
1344 autocmd BufLeave Xfile1 bwipe Xfile2
1345 augroup END
1346
1347 call assert_fails('edit Xfile2', 'E143:')
1348 call assert_equal('Xfile1', bufname('%'))
1349
1350 autocmd! test_bufleavewithdelete BufLeave Xfile1
1351 augroup! test_bufleavewithdelete
1352
1353 new
1354 bwipe! Xfile1
1355endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001356
1357" Test for autocommand that changes the buffer list, when doing ":ball".
1358func Test_Acmd_BufAll()
1359 enew!
1360 %bwipe!
1361 call writefile(['Test file Xxx1'], 'Xxx1')
1362 call writefile(['Test file Xxx2'], 'Xxx2')
1363 call writefile(['Test file Xxx3'], 'Xxx3')
1364
1365 " Add three files to the buffer list
1366 split Xxx1
1367 close
1368 split Xxx2
1369 close
1370 split Xxx3
1371 close
1372
1373 " Wipe the buffer when the buffer is opened
1374 au BufReadPost Xxx2 bwipe
1375
1376 call append(0, 'Test file Xxx4')
1377 ball
1378
1379 call assert_equal(2, winnr('$'))
1380 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1381 wincmd t
1382
1383 au! BufReadPost
1384 %bwipe!
1385 call delete('Xxx1')
1386 call delete('Xxx2')
1387 call delete('Xxx3')
1388 enew! | only
1389endfunc
1390
1391" Test for autocommand that changes current buffer on BufEnter event.
1392" Check if modelines are interpreted for the correct buffer.
1393func Test_Acmd_BufEnter()
1394 %bwipe!
1395 call writefile(['start of test file Xxx1',
1396 \ "\<Tab>this is a test",
1397 \ 'end of test file Xxx1'], 'Xxx1')
1398 call writefile(['start of test file Xxx2',
1399 \ 'vim: set noai :',
1400 \ "\<Tab>this is a test",
1401 \ 'end of test file Xxx2'], 'Xxx2')
1402
1403 au BufEnter Xxx2 brew
1404 set ai modeline modelines=3
1405 edit Xxx1
1406 " edit Xxx2, autocmd will do :brew
1407 edit Xxx2
1408 exe "normal G?this is a\<CR>"
1409 " Append text with autoindent to this file
1410 normal othis should be auto-indented
1411 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1412 call assert_equal(3, line('.'))
1413 " Remove autocmd and edit Xxx2 again
1414 au! BufEnter Xxx2
1415 buf! Xxx2
1416 exe "normal G?this is a\<CR>"
1417 " append text without autoindent to Xxx
1418 normal othis should be in column 1
1419 call assert_equal("this should be in column 1", getline('.'))
1420 call assert_equal(4, line('.'))
1421
1422 %bwipe!
1423 call delete('Xxx1')
1424 call delete('Xxx2')
1425 set ai&vim modeline&vim modelines&vim
1426endfunc
1427
1428" Test for issue #57
1429" do not move cursor on <c-o> when autoindent is set
1430func Test_ai_CTRL_O()
1431 enew!
1432 set ai
1433 let save_fo = &fo
1434 set fo+=r
1435 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1436 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1437 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1438
1439 set ai&vim
1440 let &fo = save_fo
1441 enew!
1442endfunc
1443
1444" Test for autocommand that deletes the current buffer on BufLeave event.
1445" Also test deleting the last buffer, should give a new, empty buffer.
1446func Test_BufLeave_Wipe()
1447 %bwipe!
1448 let content = ['start of test file Xxx',
1449 \ 'this is a test',
1450 \ 'end of test file Xxx']
1451 call writefile(content, 'Xxx1')
1452 call writefile(content, 'Xxx2')
1453
1454 au BufLeave Xxx2 bwipe
1455 edit Xxx1
1456 split Xxx2
1457 " delete buffer Xxx2, we should be back to Xxx1
1458 bwipe
1459 call assert_equal('Xxx1', bufname('%'))
1460 call assert_equal(1, winnr('$'))
1461
1462 " Create an alternate buffer
1463 %write! test.out
1464 call assert_equal('test.out', bufname('#'))
1465 " delete alternate buffer
1466 bwipe test.out
1467 call assert_equal('Xxx1', bufname('%'))
1468 call assert_equal('', bufname('#'))
1469
1470 au BufLeave Xxx1 bwipe
1471 " delete current buffer, get an empty one
1472 bwipe!
1473 call assert_equal(1, line('$'))
1474 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001475 let g:bufinfo = getbufinfo()
1476 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001477
1478 call delete('Xxx1')
1479 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001480 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001481 %bwipe
1482 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001483
1484 " check that bufinfo doesn't contain a pointer to freed memory
1485 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001486endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001487
1488func Test_QuitPre()
1489 edit Xfoo
1490 let winid = win_getid(winnr())
1491 split Xbar
1492 au! QuitPre * let g:afile = expand('<afile>')
1493 " Close the other window, <afile> should be correct.
1494 exe win_id2win(winid) . 'q'
1495 call assert_equal('Xfoo', g:afile)
1496
1497 unlet g:afile
1498 bwipe Xfoo
1499 bwipe Xbar
1500endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001501
1502func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001503 au! CmdlineChanged : let g:text = getcmdline()
1504 let g:text = 0
1505 call feedkeys(":echom 'hello'\<CR>", 'xt')
1506 call assert_equal("echom 'hello'", g:text)
1507 au! CmdlineChanged
1508
1509 au! CmdlineChanged : let g:entered = expand('<afile>')
1510 let g:entered = 0
1511 call feedkeys(":echom 'hello'\<CR>", 'xt')
1512 call assert_equal(':', g:entered)
1513 au! CmdlineChanged
1514
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001515 au! CmdlineEnter : let g:entered = expand('<afile>')
1516 au! CmdlineLeave : let g:left = expand('<afile>')
1517 let g:entered = 0
1518 let g:left = 0
1519 call feedkeys(":echo 'hello'\<CR>", 'xt')
1520 call assert_equal(':', g:entered)
1521 call assert_equal(':', g:left)
1522 au! CmdlineEnter
1523 au! CmdlineLeave
1524
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001525 let save_shellslash = &shellslash
1526 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001527 au! CmdlineEnter / let g:entered = expand('<afile>')
1528 au! CmdlineLeave / let g:left = expand('<afile>')
1529 let g:entered = 0
1530 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001531 new
1532 call setline(1, 'hello')
1533 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001534 call assert_equal('/', g:entered)
1535 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001536 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001537 au! CmdlineEnter
1538 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001539 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001540endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001541
1542" Test for BufWritePre autocommand that deletes or unloads the buffer.
1543func Test_BufWritePre()
1544 %bwipe
1545 au BufWritePre Xxx1 bunload
1546 au BufWritePre Xxx2 bwipe
1547
1548 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1549 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1550
1551 edit Xtest
1552 e! Xxx2
1553 bdel Xtest
1554 e Xxx1
1555 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001556 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001557 call assert_equal('Xxx2', bufname('%'))
1558 edit Xtest
1559 e! Xxx2
1560 bwipe Xtest
1561 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001562 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001563 call assert_equal('Xxx1', bufname('%'))
1564 au! BufWritePre
1565 call delete('Xxx1')
1566 call delete('Xxx2')
1567endfunc
1568
1569" Test for BufUnload autocommand that unloads all the other buffers
1570func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001571 let g:test_is_flaky = 1
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001572 call writefile(['Test file Xxx1'], 'Xxx1')"
1573 call writefile(['Test file Xxx2'], 'Xxx2')"
1574
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001575 let content =<< trim [CODE]
1576 func UnloadAllBufs()
1577 let i = 1
1578 while i <= bufnr('$')
1579 if i != bufnr('%') && bufloaded(i)
1580 exe i . 'bunload'
1581 endif
1582 let i += 1
1583 endwhile
1584 endfunc
1585 au BufUnload * call UnloadAllBufs()
1586 au VimLeave * call writefile(['Test Finished'], 'Xout')
1587 edit Xxx1
1588 split Xxx2
1589 q
1590 [CODE]
1591
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001592 call writefile(content, 'Xtest')
1593
1594 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001595 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001596 call assert_true(filereadable('Xout'))
1597
1598 call delete('Xxx1')
1599 call delete('Xxx2')
1600 call delete('Xtest')
1601 call delete('Xout')
1602endfunc
1603
1604" Some tests for buffer-local autocommands
1605func Test_buflocal_autocmd()
1606 let g:bname = ''
1607 edit xx
1608 au BufLeave <buffer> let g:bname = expand("%")
1609 " here, autocommand for xx should trigger.
1610 " but autocommand shall not apply to buffer named <buffer>.
1611 edit somefile
1612 call assert_equal('xx', g:bname)
1613 let g:bname = ''
1614 " here, autocommand shall be auto-deleted
1615 bwipe xx
1616 " autocmd should not trigger
1617 edit xx
1618 call assert_equal('', g:bname)
1619 " autocmd should not trigger
1620 edit somefile
1621 call assert_equal('', g:bname)
1622 enew
1623 unlet g:bname
1624endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001625
1626" Test for "*Cmd" autocommands
1627func Test_Cmd_Autocmds()
1628 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1629
1630 enew!
1631 au BufReadCmd XtestA 0r Xxx|$del
1632 edit XtestA " will read text of Xxd instead
1633 call assert_equal('start of Xxx', getline(1))
1634
1635 au BufWriteCmd XtestA call append(line("$"), "write")
1636 write " will append a line to the file
1637 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001638 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001639 call assert_equal('write', getline(4))
1640
1641 " now we have:
1642 " 1 start of Xxx
1643 " 2 abc2
1644 " 3 end of Xxx
1645 " 4 write
1646
1647 au FileReadCmd XtestB '[r Xxx
1648 2r XtestB " will read Xxx below line 2 instead
1649 call assert_equal('start of Xxx', getline(3))
1650
1651 " now we have:
1652 " 1 start of Xxx
1653 " 2 abc2
1654 " 3 start of Xxx
1655 " 4 abc2
1656 " 5 end of Xxx
1657 " 6 end of Xxx
1658 " 7 write
1659
1660 au FileWriteCmd XtestC '[,']copy $
1661 normal 4GA1
1662 4,5w XtestC " will copy lines 4 and 5 to the end
1663 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001664 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001665 call assert_equal("end of Xxx", getline(9))
1666
1667 " now we have:
1668 " 1 start of Xxx
1669 " 2 abc2
1670 " 3 start of Xxx
1671 " 4 abc21
1672 " 5 end of Xxx
1673 " 6 end of Xxx
1674 " 7 write
1675 " 8 abc21
1676 " 9 end of Xxx
1677
1678 let g:lines = []
1679 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1680 w >>XtestD " will add lines to 'lines'
1681 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001682 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001683 call assert_equal(9, line('$'))
1684 call assert_equal('end of Xxx', getline('$'))
1685
1686 au BufReadCmd XtestE 0r Xxx|$del
1687 sp XtestE " split window with test.out
1688 call assert_equal('end of Xxx', getline(3))
1689
1690 let g:lines = []
1691 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1692 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1693 wall " will write other window to 'lines'
1694 call assert_equal(4, len(g:lines), g:lines)
1695 call assert_equal('asdf', g:lines[2])
1696
1697 au! BufReadCmd
1698 au! BufWriteCmd
1699 au! FileReadCmd
1700 au! FileWriteCmd
1701 au! FileAppendCmd
1702 %bwipe!
1703 call delete('Xxx')
1704 enew!
1705endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001706
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001707func s:ReadFile()
1708 setl noswapfile nomodified
1709 let filename = resolve(expand("<afile>:p"))
1710 execute 'read' fnameescape(filename)
1711 1d_
1712 exe 'file' fnameescape(filename)
1713 setl buftype=acwrite
1714endfunc
1715
1716func s:WriteFile()
1717 let filename = resolve(expand("<afile>:p"))
1718 setl buftype=
1719 noautocmd execute 'write' fnameescape(filename)
1720 setl buftype=acwrite
1721 setl nomodified
1722endfunc
1723
1724func Test_BufReadCmd()
1725 autocmd BufReadCmd *.test call s:ReadFile()
1726 autocmd BufWriteCmd *.test call s:WriteFile()
1727
1728 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1729 edit Xcmd.test
1730 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1731 normal! Gofour
1732 write
1733 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1734
1735 bwipe!
1736 call delete('Xcmd.test')
1737 au! BufReadCmd
1738 au! BufWriteCmd
1739endfunc
1740
Bram Moolenaaraace2152017-11-05 16:23:10 +01001741func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001742 exe a:start .. 'mark ['
1743 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001744endfunc
1745
1746" Verify the effects of autocmds on '[ and ']
1747func Test_change_mark_in_autocmds()
1748 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001749 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001750
1751 call SetChangeMarks(2, 3)
1752 write
1753 call assert_equal([1, 4], [line("'["), line("']")])
1754
1755 call SetChangeMarks(2, 3)
1756 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1757 write
1758 au! BufWritePre
1759
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001760 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001761 write XtestFilter
1762 write >> XtestFilter
1763
1764 call SetChangeMarks(2, 3)
1765 " Marks are set to the entire range of the write
1766 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1767 " '[ is adjusted to just before the line that will receive the filtered
1768 " data
1769 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1770 " The filtered data is read into the buffer, and the source lines are
1771 " still present, so the range is after the source lines
1772 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1773 %!cat XtestFilter
1774 " After the filtered data is read, the original lines are deleted
1775 call assert_equal([1, 8], [line("'["), line("']")])
1776 au! FilterWritePre,FilterReadPre,FilterReadPost
1777 undo
1778
1779 call SetChangeMarks(1, 4)
1780 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1781 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1782 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1783 2,3!cat XtestFilter
1784 call assert_equal([2, 9], [line("'["), line("']")])
1785 au! FilterWritePre,FilterReadPre,FilterReadPost
1786 undo
1787
1788 call delete('XtestFilter')
1789 endif
1790
1791 call SetChangeMarks(1, 4)
1792 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1793 2,3write Xtest2
1794 au! FileWritePre
1795
1796 call SetChangeMarks(2, 3)
1797 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1798 write >> Xtest2
1799 au! FileAppendPre
1800
1801 call SetChangeMarks(1, 4)
1802 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1803 2,3write >> Xtest2
1804 au! FileAppendPre
1805
1806 call SetChangeMarks(1, 1)
1807 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1808 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1809 3read Xtest2
1810 au! FileReadPre,FileReadPost
1811 undo
1812
1813 call SetChangeMarks(4, 4)
1814 " When the line is 0, it's adjusted to 1
1815 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1816 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1817 0read Xtest2
1818 au! FileReadPre,FileReadPost
1819 undo
1820
1821 call SetChangeMarks(4, 4)
1822 " When the line is 0, it's adjusted to 1
1823 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1824 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1825 1read Xtest2
1826 au! FileReadPre,FileReadPost
1827 undo
1828
1829 bwipe!
1830 call delete('Xtest')
1831 call delete('Xtest2')
1832endfunc
1833
1834func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01001835 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01001836
1837 enew!
1838 call setline(1, ['a', 'b', 'c', 'd'])
1839
1840 let shelltemp = &shelltemp
1841 set shelltemp
1842
1843 let g:filter_au = 0
1844 au FilterWritePre * let g:filter_au += 1
1845 au FilterReadPre * let g:filter_au += 1
1846 au FilterReadPost * let g:filter_au += 1
1847 %!cat
1848 call assert_equal(3, g:filter_au)
1849
1850 if has('filterpipe')
1851 set noshelltemp
1852
1853 let g:filter_au = 0
1854 au FilterWritePre * let g:filter_au += 1
1855 au FilterReadPre * let g:filter_au += 1
1856 au FilterReadPost * let g:filter_au += 1
1857 %!cat
1858 call assert_equal(0, g:filter_au)
1859 endif
1860
1861 au! FilterWritePre,FilterReadPre,FilterReadPost
1862 let &shelltemp = shelltemp
1863 bwipe!
1864endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001865
1866func Test_TextYankPost()
1867 enew!
1868 call setline(1, ['foo'])
1869
1870 let g:event = []
1871 au TextYankPost * let g:event = copy(v:event)
1872
1873 call assert_equal({}, v:event)
1874 call assert_fails('let v:event = {}', 'E46:')
1875 call assert_fails('let v:event.mykey = 0', 'E742:')
1876
1877 norm "ayiw
1878 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001879 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001880 \g:event)
1881 norm y_
1882 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001883 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false},
1884 \g:event)
1885 norm Vy
1886 call assert_equal(
1887 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001888 \g:event)
1889 call feedkeys("\<C-V>y", 'x')
1890 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001891 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001892 \g:event)
1893 norm "xciwbar
1894 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001895 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001896 \g:event)
1897 norm "bdiw
1898 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001899 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001900 \g:event)
1901
1902 call assert_equal({}, v:event)
1903
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001904 if has('clipboard_working') && !has('gui_running')
1905 " Test that when the visual selection is automatically copied to clipboard
1906 " register a TextYankPost is emitted
1907 call setline(1, ['foobar'])
1908
1909 let @* = ''
1910 set clipboard=autoselect
1911 exe "norm! ggviw\<Esc>"
1912 call assert_equal(
1913 \{'regcontents': ['foobar'], 'regname': '*', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1914 \g:event)
1915
1916 let @+ = ''
1917 set clipboard=autoselectplus
1918 exe "norm! ggviw\<Esc>"
1919 call assert_equal(
1920 \{'regcontents': ['foobar'], 'regname': '+', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1921 \g:event)
1922
1923 set clipboard&vim
1924 endif
1925
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001926 au! TextYankPost
1927 unlet g:event
1928 bwipe!
1929endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001930
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01001931func Test_autocommand_all_events()
1932 call assert_fails('au * * bwipe', 'E1155:')
1933 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00001934 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001935endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001936
Bram Moolenaarf6246f52022-02-11 16:30:12 +00001937func Test_autocmd_user()
1938 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
1939 doautocmd User MyEvent
1940 call assert_equal(['MyEvent', 'MyEvent'], s:res)
1941 au! User
1942 unlet s:res
1943endfunc
1944
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001945function s:Before_test_dirchanged()
1946 augroup test_dirchanged
1947 autocmd!
1948 augroup END
1949 let s:li = []
1950 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001951 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001952 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001953 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001954 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001955endfunc
1956
1957function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001958 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001959 call delete(s:dir_foo, 'd')
1960 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001961 augroup test_dirchanged
1962 autocmd!
1963 augroup END
1964endfunc
1965
1966function Test_dirchanged_global()
1967 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00001968 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001969 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1970 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001971 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00001972 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001973 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001974 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001975 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001976 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001977 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001978 call s:After_test_dirchanged()
1979endfunc
1980
1981function Test_dirchanged_local()
1982 call s:Before_test_dirchanged()
1983 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1984 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001985 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001986 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001987 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001988 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001989 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001990 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001991 call s:After_test_dirchanged()
1992endfunc
1993
1994function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001995 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001996 call s:Before_test_dirchanged()
1997 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001998 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001999 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
2000 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
2001 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01002002 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002003 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01002004 exe 'edit ' . s:dir_foo . '/Xfile'
2005 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00002006 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
2007 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01002008 set noacd
2009 bwipe!
2010 call s:After_test_dirchanged()
2011endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01002012
2013" Test TextChangedI and TextChangedP
2014func Test_ChangedP()
2015 new
2016 call setline(1, ['foo', 'bar', 'foobar'])
2017 call test_override("char_avail", 1)
2018 set complete=. completeopt=menuone
2019
2020 func! TextChangedAutocmd(char)
2021 let g:autocmd .= a:char
2022 endfunc
2023
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002024 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01002025 au! TextChanged <buffer> :call TextChangedAutocmd('N')
2026 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
2027 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
2028
2029 call cursor(3, 1)
2030 let g:autocmd = ''
2031 call feedkeys("o\<esc>", 'tnix')
2032 call assert_equal('I', g:autocmd)
2033
2034 let g:autocmd = ''
2035 call feedkeys("Sf", 'tnix')
2036 call assert_equal('II', g:autocmd)
2037
2038 let g:autocmd = ''
2039 call feedkeys("Sf\<C-N>", 'tnix')
2040 call assert_equal('IIP', g:autocmd)
2041
2042 let g:autocmd = ''
2043 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2044 call assert_equal('IIPP', g:autocmd)
2045
2046 let g:autocmd = ''
2047 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2048 call assert_equal('IIPPP', g:autocmd)
2049
2050 let g:autocmd = ''
2051 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2052 call assert_equal('IIPPPP', g:autocmd)
2053
2054 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2055 " TODO: how should it handle completeopt=noinsert,noselect?
2056
2057 " CleanUp
2058 call test_override("char_avail", 0)
2059 au! TextChanged
2060 au! TextChangedI
2061 au! TextChangedP
2062 delfu TextChangedAutocmd
2063 unlet! g:autocmd
2064 set complete&vim completeopt&vim
2065
2066 bw!
2067endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002068
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002069let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002070func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002071 if !g:setline_handled
2072 call setline(1, "(x)")
2073 let g:setline_handled = v:true
2074 endif
2075endfunc
2076
2077func Test_TextChangedI_with_setline()
2078 new
2079 call test_override('char_avail', 1)
2080 autocmd TextChangedI <buffer> call SetLineOne()
2081 call feedkeys("i(\<CR>\<Esc>", 'tx')
2082 call assert_equal('(', getline(1))
2083 call assert_equal('x)', getline(2))
2084 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002085 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002086 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002087
Bram Moolenaarca34db32022-01-20 11:17:18 +00002088 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002089 bwipe!
2090endfunc
2091
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002092func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002093 CheckFeature terminal
2094 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002095 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002096 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002097
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002098 " Prepare file for TextChanged event.
2099 call writefile([''], 'Xchanged.txt')
2100 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2101 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002102 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002103 " In ConPTY, there is additional character which is drawn up to the width of
2104 " the screen.
2105 if has('conpty')
2106 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2107 else
2108 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2109 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002110 " It's only adding autocmd, so that no event occurs.
2111 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2112 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002113 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002114 call assert_equal([''], readfile('Xchanged.txt'))
2115
2116 " clean up
2117 call delete('Xchanged.txt')
2118 bwipe!
2119endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002120
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002121func Test_autocmd_nested()
2122 let g:did_nested = 0
2123 augroup Testing
2124 au WinNew * edit somefile
2125 au BufNew * let g:did_nested = 1
2126 augroup END
2127 split
2128 call assert_equal(0, g:did_nested)
2129 close
2130 bwipe! somefile
2131
2132 " old nested argument still works
2133 augroup Testing
2134 au!
2135 au WinNew * nested edit somefile
2136 au BufNew * let g:did_nested = 1
2137 augroup END
2138 split
2139 call assert_equal(1, g:did_nested)
2140 close
2141 bwipe! somefile
2142
2143 " New ++nested argument works
2144 augroup Testing
2145 au!
2146 au WinNew * ++nested edit somefile
2147 au BufNew * let g:did_nested = 1
2148 augroup END
2149 split
2150 call assert_equal(1, g:did_nested)
2151 close
2152 bwipe! somefile
2153
Bram Moolenaarf0775142022-03-04 20:10:38 +00002154 " nested without ++ does not work in Vim9 script
2155 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2156
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002157 augroup Testing
2158 au!
2159 augroup END
2160
2161 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2162 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2163endfunc
2164
2165func Test_autocmd_once()
2166 " Without ++once WinNew triggers twice
2167 let g:did_split = 0
2168 augroup Testing
2169 au WinNew * let g:did_split += 1
2170 augroup END
2171 split
2172 split
2173 call assert_equal(2, g:did_split)
2174 call assert_true(exists('#WinNew'))
2175 close
2176 close
2177
2178 " With ++once WinNew triggers once
2179 let g:did_split = 0
2180 augroup Testing
2181 au!
2182 au WinNew * ++once let g:did_split += 1
2183 augroup END
2184 split
2185 split
2186 call assert_equal(1, g:did_split)
2187 call assert_false(exists('#WinNew'))
2188 close
2189 close
2190
2191 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2192endfunc
2193
Bram Moolenaara68e5952019-04-25 22:22:01 +02002194func Test_autocmd_bufreadpre()
2195 new
2196 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002197 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002198 w! XAutocmdBufReadPre.txt
2199 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002200 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002201 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002202 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002203 wincmd p
2204 let g:wsv1 = winsaveview()
2205 wincmd p
2206 let g:wsv2 = winsaveview()
2207 " triggers BufReadPre, should not move the cursor in either window
2208 " The topline may change one line in a large window.
2209 edit
2210 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2211 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2212 call assert_equal(2, b:bufreadpre)
2213 wincmd p
2214 call assert_equal(g:wsv1.topline, winsaveview().topline)
2215 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2216 call assert_equal(2, b:bufreadpre)
2217 " Now set the cursor position in an BufReadPre autocommand
2218 " (even though the position will be invalid, this should make Vim reset the
2219 " cursor position in the other window.
2220 wincmd p
2221 set cpo+=g
2222 " won't do anything, but try to set the cursor on an invalid lnum
2223 autocmd BufReadPre <buffer> :norm! 70gg
2224 " triggers BufReadPre, should not move the cursor in either window
2225 e
2226 call assert_equal(1, winsaveview().topline)
2227 call assert_equal(1, winsaveview().lnum)
2228 call assert_equal(3, b:bufreadpre)
2229 wincmd p
2230 call assert_equal(g:wsv1.topline, winsaveview().topline)
2231 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2232 call assert_equal(3, b:bufreadpre)
2233 close
2234 close
2235 call delete('XAutocmdBufReadPre.txt')
2236 set cpo-=g
2237endfunc
2238
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002239" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002240
2241" Tests for the following autocommands:
2242" - FileWritePre writing a compressed file
2243" - FileReadPost reading a compressed file
2244" - BufNewFile reading a file template
2245" - BufReadPre decompressing the file to be read
2246" - FilterReadPre substituting characters in the temp file
2247" - FilterReadPost substituting characters after filtering
2248" - FileReadPre set options for decompression
2249" - FileReadPost decompress the file
2250func Test_ReadWrite_Autocmds()
2251 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002252 CheckUnix
2253 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002254
2255 " Make $GZIP empty, "-v" would cause trouble.
2256 let $GZIP = ""
2257
2258 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2259 " being modified outside of Vim (noticed on Solaris).
2260 au FileChangedShell * echo 'caught FileChangedShell'
2261
2262 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2263 augroup Test1
2264 au!
2265 au FileWritePre *.gz '[,']!gzip
2266 au FileWritePost *.gz undo
2267 au FileReadPost *.gz '[,']!gzip -d
2268 augroup END
2269
2270 new
2271 set bin
2272 call append(0, [
2273 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2274 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2275 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2276 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2277 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2278 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2279 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2280 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2281 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2282 \ ])
2283 1,9write! Xtestfile.gz
2284 enew! | close
2285
2286 new
2287 " Read and decompress the testfile
2288 0read Xtestfile.gz
2289 call assert_equal([
2290 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2291 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2292 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2293 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2294 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2295 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2296 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2297 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2298 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2299 \ ], getline(1, 9))
2300 enew! | close
2301
2302 augroup Test1
2303 au!
2304 augroup END
2305
2306 " Test for the FileAppendPre and FileAppendPost autocmds
2307 augroup Test2
2308 au!
2309 au BufNewFile *.c read Xtest.c
2310 au FileAppendPre *.out '[,']s/new/NEW/
2311 au FileAppendPost *.out !cat Xtest.c >> test.out
2312 augroup END
2313
2314 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2315 new foo.c " should load Xtest.c
2316 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2317 w! >> test.out " append it to the output file
2318
2319 let contents = readfile('test.out')
2320 call assert_equal(' * Here is a NEW .c file', contents[2])
2321 call assert_equal(' * Here is a new .c file', contents[5])
2322
2323 call delete('test.out')
2324 enew! | close
2325 augroup Test2
2326 au!
2327 augroup END
2328
2329 " Test for the BufReadPre and BufReadPost autocmds
2330 augroup Test3
2331 au!
2332 " setup autocommands to decompress before reading and re-compress
2333 " afterwards
2334 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2335 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2336 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2337 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2338 augroup END
2339
2340 e! Xtestfile.gz " Edit compressed file
2341 call assert_equal([
2342 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2343 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2344 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2345 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2346 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2347 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2348 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2349 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2350 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2351 \ ], getline(1, 9))
2352
2353 w! >> test.out " Append it to the output file
2354
2355 augroup Test3
2356 au!
2357 augroup END
2358
2359 " Test for the FilterReadPre and FilterReadPost autocmds.
2360 set shelltemp " need temp files here
2361 augroup Test4
2362 au!
2363 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2364 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2365 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2366 au FilterReadPost *.out '[,']s/x/X/g
2367 augroup END
2368
2369 e! test.out " Edit the output file
2370 1,$!cat
2371 call assert_equal([
2372 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2373 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2374 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2375 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2376 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2377 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2378 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2379 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2380 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2381 \ ], getline(1, 9))
2382 call assert_equal([
2383 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2384 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2385 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2386 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2387 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2388 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2389 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2390 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2391 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2392 \ ], readfile('test.out'))
2393
2394 augroup Test4
2395 au!
2396 augroup END
2397 set shelltemp&vim
2398
2399 " Test for the FileReadPre and FileReadPost autocmds.
2400 augroup Test5
2401 au!
2402 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2403 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2404 au FileReadPost *.gz '[,']s/l/L/
2405 augroup END
2406
2407 new
2408 0r Xtestfile.gz " Read compressed file
2409 call assert_equal([
2410 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2411 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2412 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2413 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2414 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2415 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2416 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2417 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2418 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2419 \ ], getline(1, 9))
2420 call assert_equal([
2421 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2422 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2423 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2424 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2425 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2426 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2427 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2428 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2429 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2430 \ ], readfile('Xtestfile.gz'))
2431
2432 augroup Test5
2433 au!
2434 augroup END
2435
2436 au! FileChangedShell
2437 call delete('Xtestfile.gz')
2438 call delete('Xtest.c')
2439 call delete('test.out')
2440endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002441
2442func Test_throw_in_BufWritePre()
2443 new
2444 call setline(1, ['one', 'two', 'three'])
2445 call assert_false(filereadable('Xthefile'))
2446 augroup throwing
2447 au BufWritePre X* throw 'do not write'
2448 augroup END
2449 try
2450 w Xthefile
2451 catch
2452 let caught = 1
2453 endtry
2454 call assert_equal(1, caught)
2455 call assert_false(filereadable('Xthefile'))
2456
2457 bwipe!
2458 au! throwing
2459endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002460
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002461func Test_autocmd_in_try_block()
2462 call mkdir('Xdir')
2463 au BufEnter * let g:fname = expand('%')
2464 try
2465 edit Xdir/
2466 endtry
2467 call assert_match('Xdir', g:fname)
2468
2469 unlet g:fname
2470 au! BufEnter
2471 call delete('Xdir', 'rf')
2472endfunc
2473
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002474func Test_autocmd_SafeState()
2475 CheckRunVimInTerminal
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002476 let g:test_is_flaky = 1
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002477
2478 let lines =<< trim END
2479 let g:safe = 0
2480 let g:again = ''
2481 au SafeState * let g:safe += 1
2482 au SafeStateAgain * let g:again ..= 'x'
2483 func CallTimer()
2484 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2485 endfunc
2486 END
2487 call writefile(lines, 'XSafeState')
2488 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2489
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002490 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002491 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002492 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002493 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002494
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002495 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002496 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002497 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002498
2499 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002500 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002501 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002502 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002503 call term_sendkeys(buf, ":echo g:again\<CR>")
2504 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2505
2506 call StopVimInTerminal(buf)
2507 call delete('XSafeState')
2508endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002509
2510func Test_autocmd_CmdWinEnter()
2511 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002512 CheckFeature cmdwin
2513
Bram Moolenaar23324a02019-10-01 17:39:04 +02002514 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002515 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002516 let b:dummy_var = 'This is a dummy'
2517 autocmd CmdWinEnter * quit
2518 let winnr = winnr('$')
2519 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002520 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002521 call writefile(lines, filename)
2522 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2523
2524 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002525 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002526 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002527 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002528 call term_sendkeys(buf, ":echo &buftype\<cr>")
2529 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2530 call term_sendkeys(buf, ":echo winnr\<cr>")
2531 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2532
2533 " clean up
2534 call StopVimInTerminal(buf)
2535 call delete(filename)
2536endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002537
2538func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002539 CheckFeature quickfix
2540
Bram Moolenaarec66c412019-10-11 21:19:13 +02002541 pedit xx
2542 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002543 augroup winenter
2544 au WinEnter * if winnr('$') > 2 | quit | endif
2545 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002546 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002547
2548 augroup winenter
2549 au! WinEnter
2550 augroup END
2551
2552 bwipe xx
2553 bwipe x
2554 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002555endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002556
2557func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002558 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002559 edit! Xtest
2560 call setline(1, ['a', 'b', 'c', 'd'])
2561
2562 " :lockmarks preserves the marks
2563 call SetChangeMarks(2, 3)
2564 lockmarks write
2565 call assert_equal([2, 3], [line("'["), line("']")])
2566
2567 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2568 " original values for the user
2569 augroup lockmarks
2570 au!
2571 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2572 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2573 augroup END
2574
2575 lockmarks write
2576 call assert_equal([2, 3], [line("'["), line("']")])
2577
2578 if executable('cat')
2579 lockmarks %!cat
2580 call assert_equal([2, 3], [line("'["), line("']")])
2581 endif
2582
2583 lockmarks 3,4write Xtest2
2584 call assert_equal([2, 3], [line("'["), line("']")])
2585
2586 au! lockmarks
2587 augroup! lockmarks
2588 call delete('Xtest')
2589 call delete('Xtest2')
2590endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002591
2592func Test_FileType_spell()
2593 if !isdirectory('/tmp')
2594 throw "Skipped: requires /tmp directory"
2595 endif
2596
2597 " this was crashing with an invalid free()
2598 setglobal spellfile=/tmp/en.utf-8.add
2599 augroup crash
2600 autocmd!
2601 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2602 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2603 autocmd FileType anotherfiletype setlocal spell
2604 augroup END
2605 func! NoCrash() abort
2606 edit /tmp/crashfile
2607 endfunc
2608 call NoCrash()
2609
2610 au! crash
2611 setglobal spellfile=
2612endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002613
Bram Moolenaar406cd902020-02-18 21:54:41 +01002614" Test closing a window or editing another buffer from a FileChangedRO handler
2615" in a readonly buffer
2616func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002617 call test_override('ui_delay', 10)
2618
Bram Moolenaar406cd902020-02-18 21:54:41 +01002619 augroup FileChangedROTest
2620 au!
2621 autocmd FileChangedRO * quit
2622 augroup END
2623 new
2624 set readonly
2625 call assert_fails('normal i', 'E788:')
2626 close
2627 augroup! FileChangedROTest
2628
2629 augroup FileChangedROTest
2630 au!
2631 autocmd FileChangedRO * edit Xfile
2632 augroup END
2633 new
2634 set readonly
2635 call assert_fails('normal i', 'E788:')
2636 close
2637 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002638 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002639endfunc
2640
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002641func LogACmd()
2642 call add(g:logged, line('$'))
2643endfunc
2644
2645func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002646 CheckNotGui
2647
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002648 enew!
2649 tabnew
2650 call setline(1, ['a', 'b', 'c', 'd'])
2651 $
2652 au TermChanged * call LogACmd()
2653 let g:logged = []
2654 let term_save = &term
2655 set term=xterm
2656 call assert_equal([1, 4], g:logged)
2657
2658 au! TermChanged
2659 let &term = term_save
2660 bwipe!
2661endfunc
2662
Bram Moolenaare3284872020-03-19 13:55:03 +01002663" Test for FileReadCmd autocmd
2664func Test_autocmd_FileReadCmd()
2665 func ReadFileCmd()
2666 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2667 endfunc
2668 augroup FileReadCmdTest
2669 au!
2670 au FileReadCmd Xtest call ReadFileCmd()
2671 augroup END
2672
2673 new
2674 read ++bin Xtest
2675 read ++nobin Xtest
2676 read ++edit Xtest
2677 read ++bad=keep Xtest
2678 read ++bad=drop Xtest
2679 read ++bad=- Xtest
2680 read ++ff=unix Xtest
2681 read ++ff=dos Xtest
2682 read ++ff=mac Xtest
2683 read ++enc=utf-8 Xtest
2684
2685 call assert_equal(['',
2686 \ 'v:cmdarg = ++bin',
2687 \ 'v:cmdarg = ++nobin',
2688 \ 'v:cmdarg = ++edit',
2689 \ 'v:cmdarg = ++bad=keep',
2690 \ 'v:cmdarg = ++bad=drop',
2691 \ 'v:cmdarg = ++bad=-',
2692 \ 'v:cmdarg = ++ff=unix',
2693 \ 'v:cmdarg = ++ff=dos',
2694 \ 'v:cmdarg = ++ff=mac',
2695 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2696
2697 close!
2698 augroup FileReadCmdTest
2699 au!
2700 augroup END
2701 delfunc ReadFileCmd
2702endfunc
2703
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002704" Test for passing invalid arguments to autocmd
2705func Test_autocmd_invalid_args()
2706 " Additional character after * for event
2707 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2708 augroup Test
2709 augroup END
2710 " Invalid autocmd event
2711 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2712 " Invalid autocmd event in a autocmd group
2713 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2714 augroup! Test
2715 " Execute all autocmds
2716 call assert_fails('doautocmd * BufEnter', 'E217:')
2717 call assert_fails('augroup! x1a2b3', 'E367:')
2718 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002719 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002720endfunc
2721
2722" Test for deep nesting of autocmds
2723func Test_autocmd_deep_nesting()
2724 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2725 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2726 autocmd! BufEnter Xfile
2727endfunc
2728
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002729" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2730func Test_autocmd_sigusr1()
2731 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002732 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002733
2734 let g:sigusr1_passed = 0
2735 au SigUSR1 * let g:sigusr1_passed = 1
2736 call system('/bin/kill -s usr1 ' . getpid())
2737 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2738
2739 au! SigUSR1
2740 unlet g:sigusr1_passed
2741endfunc
2742
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002743" Test for BufReadPre autocmd deleting the file
2744func Test_BufReadPre_delfile()
2745 augroup TestAuCmd
2746 au!
2747 autocmd BufReadPre Xfile call delete('Xfile')
2748 augroup END
2749 call writefile([], 'Xfile')
2750 call assert_fails('new Xfile', 'E200:')
2751 call assert_equal('Xfile', @%)
2752 call assert_equal(1, &readonly)
2753 call delete('Xfile')
2754 augroup TestAuCmd
2755 au!
2756 augroup END
2757 close!
2758endfunc
2759
2760" Test for BufReadPre autocmd changing the current buffer
2761func Test_BufReadPre_changebuf()
2762 augroup TestAuCmd
2763 au!
2764 autocmd BufReadPre Xfile edit Xsomeotherfile
2765 augroup END
2766 call writefile([], 'Xfile')
2767 call assert_fails('new Xfile', 'E201:')
2768 call assert_equal('Xsomeotherfile', @%)
2769 call assert_equal(1, &readonly)
2770 call delete('Xfile')
2771 augroup TestAuCmd
2772 au!
2773 augroup END
2774 close!
2775endfunc
2776
2777" Test for BufWipeouti autocmd changing the current buffer when reading a file
2778" in an empty buffer with 'f' flag in 'cpo'
2779func Test_BufDelete_changebuf()
2780 new
2781 augroup TestAuCmd
2782 au!
2783 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2784 augroup END
2785 let save_cpo = &cpo
2786 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002787 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002788 call assert_equal('somefile', @%)
2789 let &cpo = save_cpo
2790 augroup TestAuCmd
2791 au!
2792 augroup END
2793 close!
2794endfunc
2795
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002796" Test for the temporary internal window used to execute autocmds
2797func Test_autocmd_window()
2798 %bw!
2799 edit one.txt
2800 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002801 vnew three.txt
2802 tabnew four.txt
2803 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002804 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002805 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002806 au!
2807 au BufEnter * call add(g:blist, [expand('<afile>'),
2808 \ win_gettype(bufwinnr(expand('<afile>')))])
2809 augroup END
2810
2811 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002812 call assert_equal([
2813 \ ['one.txt', 'autocmd'],
2814 \ ['two.txt', ''],
2815 \ ['four.txt', 'autocmd'],
2816 \ ['three.txt', ''],
2817 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002818
Bram Moolenaar832adf92020-06-25 19:01:36 +02002819 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002820 au!
2821 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002822 augroup! aucmd_win_test1
2823 %bw!
2824endfunc
2825
2826" Test for trying to close the temporary window used for executing an autocmd
2827func Test_close_autocmd_window()
2828 %bw!
2829 edit one.txt
2830 tabnew two.txt
2831 augroup aucmd_win_test2
2832 au!
2833 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2834 augroup END
2835
2836 call assert_fails('doautoall BufEnter', 'E813:')
2837
2838 augroup aucmd_win_test2
2839 au!
2840 augroup END
2841 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002842 %bwipe!
2843endfunc
2844
2845" Test for trying to close the tab that has the temporary window for exeucing
2846" an autocmd.
2847func Test_close_autocmd_tab()
2848 edit one.txt
2849 tabnew two.txt
2850 augroup aucmd_win_test
2851 au!
2852 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2853 augroup END
2854
2855 call assert_fails('doautoall BufEnter', 'E813:')
2856
2857 tabonly
2858 augroup aucmd_win_test
2859 au!
2860 augroup END
2861 augroup! aucmd_win_test
2862 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002863endfunc
2864
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00002865func Test_Visual_doautoall_redraw()
2866 call setline(1, ['a', 'b'])
2867 new
2868 wincmd p
2869 call feedkeys("G\<C-V>", 'txn')
2870 autocmd User Explode ++once redraw
2871 doautoall User Explode
2872 %bwipe!
2873endfunc
2874
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01002875" This was using freed memory.
2876func Test_BufNew_arglocal()
2877 arglocal
2878 au BufNew * arglocal
2879 call assert_fails('drop xx', 'E1156:')
2880
2881 au! BufNew
2882endfunc
2883
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002884func Test_autocmd_closes_window()
2885 au BufNew,BufWinLeave * e %e
2886 file yyy
2887 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002888 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002889
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002890 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002891 au! BufNew
2892 au! BufWinLeave
2893endfunc
2894
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002895func Test_autocmd_quit_psearch()
2896 sn aa bb
2897 augroup aucmd_win_test
2898 au!
2899 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
2900 augroup END
2901 ps /
2902
2903 augroup aucmd_win_test
2904 au!
2905 augroup END
2906endfunc
2907
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002908" Fuzzer found some strange combination that caused a crash.
2909func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002910 " For unknown reason this hangs on MS-Windows
2911 CheckNotMSWindows
2912
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002913 augroup aucmd_normal_test
2914 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
2915 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002916 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002917 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002918 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002919 normal G
2920
2921 augroup aucmd_normal_test
2922 au!
2923 augroup END
2924endfunc
2925
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002926func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002927 " For unknown reason this hangs on MS-Windows
2928 CheckNotMSWindows
2929
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002930 au BufWinLeave * nested q
2931 call assert_fails("norm 7q?\n", 'E855:')
2932
2933 au! BufWinLeave
2934 new
2935 only
2936endfunc
2937
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002938func Test_autocmd_vimgrep()
2939 augroup aucmd_vimgrep
2940 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * sb
2941 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * q9�
2942 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002943 %bwipe!
Bram Moolenaardd07c022021-02-07 13:32:46 +01002944 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002945
2946 augroup aucmd_vimgrep
2947 au!
2948 augroup END
2949endfunc
2950
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002951func Test_autocmd_with_block()
2952 augroup block_testing
2953 au BufReadPost *.xml {
2954 setlocal matchpairs+=<:>
2955 /<start
2956 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02002957 au CursorHold * {
2958 autocmd BufReadPre * ++once echo 'one' | echo 'two'
2959 g:gotSafeState = 77
2960 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002961 augroup END
2962
2963 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
2964 call assert_equal(expected, execute('au BufReadPost *.xml'))
2965
Bram Moolenaar63b91732021-08-05 20:40:03 +02002966 doautocmd CursorHold
2967 call assert_equal(77, g:gotSafeState)
2968 unlet g:gotSafeState
2969
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002970 augroup block_testing
2971 au!
2972 augroup END
2973endfunc
2974
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002975" Test TextChangedI and TextChanged
2976func Test_Changed_ChangedI()
2977 new
2978 call test_override("char_avail", 1)
2979 let [g:autocmd_i, g:autocmd_n] = ['','']
2980
2981 func! TextChangedAutocmdI(char)
2982 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
2983 endfunc
2984
2985 augroup Test_TextChanged
2986 au!
2987 au TextChanged <buffer> :call TextChangedAutocmdI('N')
2988 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
2989 augroup END
2990
2991 call feedkeys("ifoo\<esc>", 'tnix')
2992 " TODO: Test test does not seem to trigger TextChanged autocommand, this
2993 " requires running Vim in a terminal window.
2994 " call assert_equal('N3', g:autocmd_n)
2995 call assert_equal('I3', g:autocmd_i)
2996
2997 call feedkeys("yyp", 'tnix')
2998 " TODO: Test test does not seem to trigger TextChanged autocommand.
2999 " call assert_equal('N4', g:autocmd_n)
3000 call assert_equal('I3', g:autocmd_i)
3001
3002 " CleanUp
3003 call test_override("char_avail", 0)
3004 au! TextChanged <buffer>
3005 au! TextChangedI <buffer>
3006 augroup! Test_TextChanged
3007 delfu TextChangedAutocmdI
3008 unlet! g:autocmd_i g:autocmd_n
3009
3010 bw!
3011endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01003012
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00003013func Test_closing_autocmd_window()
3014 let lines =<< trim END
3015 edit Xa.txt
3016 tabnew Xb.txt
3017 autocmd BufEnter Xa.txt unhide 1
3018 doautoall BufEnter
3019 END
3020 call v9.CheckScriptFailure(lines, 'E814:')
3021 au! BufEnter
3022 only!
3023 bwipe Xa.txt
3024 bwipe Xb.txt
3025endfunc
3026
Bram Moolenaar347538f2022-03-26 16:28:06 +00003027func Test_bufwipeout_changes_window()
3028 " This should not crash, but we don't have any expectations about what
3029 " happens, changing window in BufWipeout has unpredictable results.
3030 tabedit
3031 let g:window_id = win_getid()
3032 topleft new
3033 setlocal bufhidden=wipe
3034 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3035 tabprevious
3036 +tabclose
3037
3038 unlet g:window_id
3039 au! BufWipeout
3040 %bwipe!
3041endfunc
3042
3043
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003044" vim: shiftwidth=2 sts=2 expandtab