blob: 39ba9af7dcb42dd0d9b8a9de9992d2c2fa9f0f38 [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 Moolenaare99e8442016-07-26 20:43:40 +0200351func s:AddAnAutocmd()
352 augroup vimBarTest
353 au BufReadCmd * echo 'hello'
354 augroup END
355 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
356endfunc
357
358func Test_early_bar()
359 " test that a bar is recognized before the {event}
360 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000361 augroup vimBarTest | au! | let done = 77 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200362 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000363 call assert_equal(77, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200364
365 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000366 augroup vimBarTest| au!| let done = 88 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200367 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000368 call assert_equal(88, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200369
370 " test that a bar is recognized after the {event}
371 call s:AddAnAutocmd()
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000372 augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END
Bram Moolenaare99e8442016-07-26 20:43:40 +0200373 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
Bram Moolenaarb8e642f2021-11-20 10:38:25 +0000374 call assert_equal(99, done)
Bram Moolenaare99e8442016-07-26 20:43:40 +0200375
376 " test that a bar is recognized after the {group}
377 call s:AddAnAutocmd()
378 au! vimBarTest|echo 'hello'
379 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
380endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200381
Bram Moolenaar5c809082016-09-01 16:21:48 +0200382func RemoveGroup()
383 autocmd! StartOK
384 augroup! StartOK
385endfunc
386
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200387func Test_augroup_warning()
388 augroup TheWarning
389 au VimEnter * echo 'entering'
390 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100391 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200392 redir => res
393 augroup! TheWarning
394 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100395 call assert_match("W19:", res)
396 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200397
398 " check "Another" does not take the pace of the deleted entry
399 augroup Another
400 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100401 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200402 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200403
404 " no warning for postpone aucmd delete
405 augroup StartOK
406 au VimEnter * call RemoveGroup()
407 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100408 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200409 redir => res
410 doautocmd VimEnter
411 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100412 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200413 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200414
415 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200416endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200417
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200418func Test_BufReadCmdHelp()
419 " This used to cause access to free memory
420 au BufReadCmd * e +h
421 help
422
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200423 au! BufReadCmd
424endfunc
425
426func Test_BufReadCmdHelpJump()
427 " This used to cause access to free memory
428 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200429 " } to fix highlighting
430 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200431
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200432 au! BufReadCmd
433endfunc
434
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200435func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200436 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200437 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200438 call assert_fails('augroup! x', 'E936:')
439 au VimEnter * echo
440 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200441 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100442 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200443 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200444endfunc
445
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200446" Tests for autocommands on :close command.
447" This used to be in test13.
448func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200449 " Clean up buffers, because in some cases this function fails.
450 call s:cleanup_buffers()
451
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200452 " Write three files and open them, each in a window.
453 " Then go to next window, with autocommand that deletes the previous one.
454 " Do this twice, writing the file.
455 e! Xtestje1
456 call setline(1, 'testje1')
457 w
458 sp Xtestje2
459 call setline(1, 'testje2')
460 w
461 sp Xtestje3
462 call setline(1, 'testje3')
463 w
464 wincmd w
465 au WinLeave Xtestje2 bwipe
466 wincmd w
467 call assert_equal('Xtestje1', expand('%'))
468
469 au WinLeave Xtestje1 bwipe Xtestje3
470 close
471 call assert_equal('Xtestje1', expand('%'))
472
473 " Test deleting the buffer on a Unload event. If this goes wrong there
474 " will be the ATTENTION prompt.
475 e Xtestje1
476 au!
477 au! BufUnload Xtestje1 bwipe
478 call assert_fails('e Xtestje3', 'E937:')
479 call assert_equal('Xtestje3', expand('%'))
480
481 e Xtestje2
482 sp Xtestje1
483 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200484 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200485
486 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
487 " there are ml_line errors and/or a Crash.
488 au!
489 only
490 e Xanother
491 e Xtestje1
492 bwipe Xtestje2
493 bwipe Xtestje3
494 au BufWipeout Xtestje1 buf Xtestje1
495 bwipe
496 call assert_equal('Xanother', expand('%'))
497
498 only
499 help
500 wincmd w
501 1quit
502 call assert_equal('Xanother', expand('%'))
503
504 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100505 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200506 call delete('Xtestje1')
507 call delete('Xtestje2')
508 call delete('Xtestje3')
509endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100510
511func Test_BufEnter()
512 au! BufEnter
513 au Bufenter * let val = val . '+'
514 let g:val = ''
515 split NewFile
516 call assert_equal('+', g:val)
517 bwipe!
518 call assert_equal('++', g:val)
519
520 " Also get BufEnter when editing a directory
521 call mkdir('Xdir')
522 split Xdir
523 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100524
525 " On MS-Windows we can't edit the directory, make sure we wipe the right
526 " buffer.
527 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100528
529 call delete('Xdir', 'd')
530 au! BufEnter
531endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100532
533" Closing a window might cause an endless loop
534" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200535func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200536 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100537 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200538 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100539 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100540 mksession!
541
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200542 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200543 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200544 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100545 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200546 augroup test_autocmd_sessionload
547 autocmd!
548 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
549 augroup END
550
551 func WriteErrors()
552 call writefile([execute("messages")], "Xerrors")
553 endfunc
554 au VimLeave * call WriteErrors()
555 [CODE]
556
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100557 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200558 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100559 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200560 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100561
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100562 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100563 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100564 call delete(file)
565 endfor
566endfunc
567
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100568" Using :blast and :ball for many events caused a crash, because b_nwindows was
569" not incremented correctly.
570func Test_autocmd_blast_badd()
571 let content =<< trim [CODE]
572 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
573 edit foo1
574 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
575 edit foo2
576 call writefile(['OK'], 'Xerrors')
577 qall
578 [CODE]
579
580 call writefile(content, 'XblastBall')
581 call system(GetVimCommand() .. ' --clean -S XblastBall')
582 call assert_match('OK', readfile('Xerrors')->join())
583
584 call delete('XblastBall')
585 call delete('Xerrors')
586endfunc
587
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100588" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200589func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100590 tabnew
591 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100592 mksession!
593
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200594 let content =<< trim [CODE]
595 set nocp noswapfile
596 function! DeleteInactiveBufs()
597 tabfirst
598 let tabblist = []
599 for i in range(1, tabpagenr(''$''))
600 call extend(tabblist, tabpagebuflist(i))
601 endfor
602 for b in range(1, bufnr(''$''))
603 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
604 exec ''bwipeout '' . b
605 endif
606 endfor
607 echomsg "SessionLoadPost DONE"
608 endfunction
609 au SessionLoadPost * call DeleteInactiveBufs()
610
611 func WriteErrors()
612 call writefile([execute("messages")], "Xerrors")
613 endfunc
614 au VimLeave * call WriteErrors()
615 [CODE]
616
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100617 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200618 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100619 let errors = join(readfile('Xerrors'))
620 " This probably only ever matches on unix.
621 call assert_notmatch('Caught deadly signal SEGV', errors)
622 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100623
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100624 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100625 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100626 call delete(file)
627 endfor
628endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200629
630func Test_empty_doau()
631 doau \|
632endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200633
634func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200635 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200636 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200637 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
638 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 +0200639 let g:opt = [expected, actual]
640 "call assert_equal(expected, actual)
641endfunc
642
643func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200644 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200645
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200646 badd test_autocmd.vim
647
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200648 call test_override('starting', 1)
649 set nocp
650 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
651
652 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100653 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200654 set nu
655 call assert_equal([], g:options)
656 call assert_equal(g:opt[0], g:opt[1])
657
658 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100659 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200660 setlocal nonu
661 call assert_equal([], g:options)
662 call assert_equal(g:opt[0], g:opt[1])
663
664 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100665 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200666 setglobal nonu
667 call assert_equal([], g:options)
668 call assert_equal(g:opt[0], g:opt[1])
669
670 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100671 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200672 setlocal ai
673 call assert_equal([], g:options)
674 call assert_equal(g:opt[0], g:opt[1])
675
676 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100677 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200678 setglobal ai
679 call assert_equal([], g:options)
680 call assert_equal(g:opt[0], g:opt[1])
681
682 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100683 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200684 set ai!
685 call assert_equal([], g:options)
686 call assert_equal(g:opt[0], g:opt[1])
687
688 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100689 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200690 noa setlocal ai
691 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200692 set ai!
693 call assert_equal([], g:options)
694 call assert_equal(g:opt[0], g:opt[1])
695
696 " Should not print anything, use :noa
697 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100698 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200699 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200700 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200701 call assert_equal(g:opt[0], g:opt[1])
702
703 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100704 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200705 set list nu
706 call assert_equal([], g:options)
707 call assert_equal(g:opt[0], g:opt[1])
708
709 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100710 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200711 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200712 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 +0200713 call assert_equal(g:opt[0], g:opt[1])
714
715 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100716 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200717 setlocal acd
718 call assert_equal([], g:options)
719 call assert_equal(g:opt[0], g:opt[1])
720
721 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100722 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200723 set ar
724 call assert_equal([], g:options)
725 call assert_equal(g:opt[0], g:opt[1])
726
727 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100728 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200729 setlocal ar
730 call assert_equal([], g:options)
731 call assert_equal(g:opt[0], g:opt[1])
732
733 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100734 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200735 setglobal invar
736 call assert_equal([], g:options)
737 call assert_equal(g:opt[0], g:opt[1])
738
739 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100740 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
741 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200742 call assert_equal([], g:options)
743 call assert_equal(g:opt[0], g:opt[1])
744
745 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100746 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200747 " try twice, first time, shouldn't trigger because option name is invalid,
748 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200749 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200750 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200751 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200752 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200753 call assert_equal([], g:options)
754 call assert_equal(g:opt[0], g:opt[1])
755
756 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100757 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200758 call setwinvar(0, '&number', 1)
759 call assert_equal([], g:options)
760 call assert_equal(g:opt[0], g:opt[1])
761
762 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100763 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200764 setlocal key=blah
765 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200766 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200767 call assert_equal(g:opt[0], g:opt[1])
768
Bram Moolenaard7c96872019-06-15 17:12:48 +0200769
770 " 18a: Setting string global option"
771 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100772 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200773 set backupext=foo
774 call assert_equal([], g:options)
775 call assert_equal(g:opt[0], g:opt[1])
776
777 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100778 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200779 set backupext&
780 call assert_equal([], g:options)
781 call assert_equal(g:opt[0], g:opt[1])
782
783 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100784 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200785 setglobal backupext=bar
786 call assert_equal([], g:options)
787 call assert_equal(g:opt[0], g:opt[1])
788
789 " 18d: Setting local string global option"
790 " As this is a global option this sets the global value even though
791 " :setlocal is used!
792 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100793 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200794 setlocal backupext=baz
795 call assert_equal([], g:options)
796 call assert_equal(g:opt[0], g:opt[1])
797
798 " 18e: Setting again string global option"
799 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
800 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100801 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200802 set backupext=fuu
803 call assert_equal([], g:options)
804 call assert_equal(g:opt[0], g:opt[1])
805
806
zeertzjqb811de52021-10-21 10:50:44 +0100807 " 19a: Setting string global-local (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200808 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100809 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200810 set tags=tagpath
811 call assert_equal([], g:options)
812 call assert_equal(g:opt[0], g:opt[1])
813
zeertzjqb811de52021-10-21 10:50:44 +0100814 " 19b: Resetting string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100815 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200816 set tags&
817 call assert_equal([], g:options)
818 call assert_equal(g:opt[0], g:opt[1])
819
zeertzjqb811de52021-10-21 10:50:44 +0100820 " 19c: Setting global string global-local (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100821 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200822 setglobal tags=tagpath1
823 call assert_equal([], g:options)
824 call assert_equal(g:opt[0], g:opt[1])
825
zeertzjqb811de52021-10-21 10:50:44 +0100826 " 19d: Setting local string global-local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100827 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200828 setlocal tags=tagpath2
829 call assert_equal([], g:options)
830 call assert_equal(g:opt[0], g:opt[1])
831
zeertzjqb811de52021-10-21 10:50:44 +0100832 " 19e: Setting again string global-local (to buffer) option"
833 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200834 " but the old local value for all other kinds of options.
835 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
836 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100837 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200838 set tags=tagpath
839 call assert_equal([], g:options)
840 call assert_equal(g:opt[0], g:opt[1])
841
zeertzjqb811de52021-10-21 10:50:44 +0100842 " 19f: Setting string global-local (to buffer) option to an empty string"
843 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200844 " but the old local value for all other kinds of options.
845 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
846 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100847 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200848 set tags=tagpath
849 call assert_equal([], g:options)
850 call assert_equal(g:opt[0], g:opt[1])
851
852
853 " 20a: Setting string local (to buffer) option"
854 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100855 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200856 set spelllang=elvish,klingon
857 call assert_equal([], g:options)
858 call assert_equal(g:opt[0], g:opt[1])
859
860 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100861 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200862 set spelllang&
863 call assert_equal([], g:options)
864 call assert_equal(g:opt[0], g:opt[1])
865
866 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100867 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200868 setglobal spelllang=elvish
869 call assert_equal([], g:options)
870 call assert_equal(g:opt[0], g:opt[1])
871
872 " 20d: Setting local string local (to buffer) option"
873 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100874 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200875 setlocal spelllang=klingon
876 call assert_equal([], g:options)
877 call assert_equal(g:opt[0], g:opt[1])
878
879 " 20e: Setting again string local (to buffer) option"
zeertzjqb811de52021-10-21 10:50:44 +0100880 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200881 " but the old local value for all other kinds of options.
882 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
883 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100884 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200885 set spelllang=foo
886 call assert_equal([], g:options)
887 call assert_equal(g:opt[0], g:opt[1])
888
889
zeertzjqb811de52021-10-21 10:50:44 +0100890 " 21a: Setting string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200891 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100892 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200893 set statusline=foo
894 call assert_equal([], g:options)
895 call assert_equal(g:opt[0], g:opt[1])
896
zeertzjqb811de52021-10-21 10:50:44 +0100897 " 21b: Resetting string global-local (to window) option"
898 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200899 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100900 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200901 set statusline&
902 call assert_equal([], g:options)
903 call assert_equal(g:opt[0], g:opt[1])
904
zeertzjqb811de52021-10-21 10:50:44 +0100905 " 21c: Setting global string global-local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100906 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200907 setglobal statusline=bar
908 call assert_equal([], g:options)
909 call assert_equal(g:opt[0], g:opt[1])
910
zeertzjqb811de52021-10-21 10:50:44 +0100911 " 21d: Setting local string global-local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200912 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100913 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200914 setlocal statusline=baz
915 call assert_equal([], g:options)
916 call assert_equal(g:opt[0], g:opt[1])
917
zeertzjqb811de52021-10-21 10:50:44 +0100918 " 21e: Setting again string global-local (to window) option"
919 " Note: v:option_old is the old global value for global-local string options
Bram Moolenaard7c96872019-06-15 17:12:48 +0200920 " but the old local value for all other kinds of options.
921 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
922 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100923 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200924 set statusline=foo
925 call assert_equal([], g:options)
926 call assert_equal(g:opt[0], g:opt[1])
927
928
929 " 22a: Setting string local (to window) option"
930 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100931 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200932 set foldignore=fo
933 call assert_equal([], g:options)
934 call assert_equal(g:opt[0], g:opt[1])
935
936 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100937 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200938 set foldignore&
939 call assert_equal([], g:options)
940 call assert_equal(g:opt[0], g:opt[1])
941
942 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100943 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200944 setglobal foldignore=bar
945 call assert_equal([], g:options)
946 call assert_equal(g:opt[0], g:opt[1])
947
948 " 22d: Setting local string local (to window) option"
949 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100950 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200951 setlocal foldignore=baz
952 call assert_equal([], g:options)
953 call assert_equal(g:opt[0], g:opt[1])
954
955 " 22e: Setting again string local (to window) option"
956 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
957 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100958 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200959 set foldignore=fo
960 call assert_equal([], g:options)
961 call assert_equal(g:opt[0], g:opt[1])
962
963
zeertzjqb811de52021-10-21 10:50:44 +0100964 " 23a: Setting global number global option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200965 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
966 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100967 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200968 setglobal cmdheight=2
969 call assert_equal([], g:options)
970 call assert_equal(g:opt[0], g:opt[1])
971
972 " 23b: Setting local number global option"
973 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
974 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100975 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200976 setlocal cmdheight=2
977 call assert_equal([], g:options)
978 call assert_equal(g:opt[0], g:opt[1])
979
980 " 23c: Setting again number global option"
981 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
982 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100983 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200984 set cmdheight=2
985 call assert_equal([], g:options)
986 call assert_equal(g:opt[0], g:opt[1])
987
988 " 23d: Setting again number global option"
989 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100990 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200991 set cmdheight=2
992 call assert_equal([], g:options)
993 call assert_equal(g:opt[0], g:opt[1])
994
995
996 " 24a: Setting global number global-local (to buffer) option"
997 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
998 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100999 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001000 setglobal undolevels=2
1001 call assert_equal([], g:options)
1002 call assert_equal(g:opt[0], g:opt[1])
1003
1004 " 24b: Setting local number global-local (to buffer) option"
1005 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1006 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001007 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001008 setlocal undolevels=2
1009 call assert_equal([], g:options)
1010 call assert_equal(g:opt[0], g:opt[1])
1011
1012 " 24c: Setting again number global-local (to buffer) option"
1013 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
1014 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001015 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001016 set undolevels=2
1017 call assert_equal([], g:options)
1018 call assert_equal(g:opt[0], g:opt[1])
1019
1020 " 24d: Setting again global number global-local (to buffer) option"
1021 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001022 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001023 set undolevels=2
1024 call assert_equal([], g:options)
1025 call assert_equal(g:opt[0], g:opt[1])
1026
1027
1028 " 25a: Setting global number local (to buffer) option"
1029 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1030 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001031 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001032 setglobal wrapmargin=2
1033 call assert_equal([], g:options)
1034 call assert_equal(g:opt[0], g:opt[1])
1035
1036 " 25b: Setting local number local (to buffer) option"
1037 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1038 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001039 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001040 setlocal wrapmargin=2
1041 call assert_equal([], g:options)
1042 call assert_equal(g:opt[0], g:opt[1])
1043
1044 " 25c: Setting again number local (to buffer) option"
1045 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
1046 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001047 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001048 set wrapmargin=2
1049 call assert_equal([], g:options)
1050 call assert_equal(g:opt[0], g:opt[1])
1051
1052 " 25d: Setting again global number local (to buffer) option"
1053 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001054 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001055 set wrapmargin=2
1056 call assert_equal([], g:options)
1057 call assert_equal(g:opt[0], g:opt[1])
1058
1059
1060 " 26: Setting number global-local (to window) option.
1061 " Such option does currently not exist.
1062
1063
1064 " 27a: Setting global number local (to window) option"
1065 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1066 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001067 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001068 setglobal foldcolumn=2
1069 call assert_equal([], g:options)
1070 call assert_equal(g:opt[0], g:opt[1])
1071
1072 " 27b: Setting local number local (to window) option"
1073 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1074 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001075 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001076 setlocal foldcolumn=2
1077 call assert_equal([], g:options)
1078 call assert_equal(g:opt[0], g:opt[1])
1079
1080 " 27c: Setting again number local (to window) option"
1081 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1082 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001083 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001084 set foldcolumn=2
1085 call assert_equal([], g:options)
1086 call assert_equal(g:opt[0], g:opt[1])
1087
zeertzjqb811de52021-10-21 10:50:44 +01001088 " 27d: Setting again global number local (to window) option"
Bram Moolenaard7c96872019-06-15 17:12:48 +02001089 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001090 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001091 set foldcolumn=2
1092 call assert_equal([], g:options)
1093 call assert_equal(g:opt[0], g:opt[1])
1094
1095
1096 " 28a: Setting global boolean global option"
1097 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1098 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001099 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001100 setglobal nowrapscan
1101 call assert_equal([], g:options)
1102 call assert_equal(g:opt[0], g:opt[1])
1103
1104 " 28b: Setting local boolean global option"
1105 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1106 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001107 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001108 setlocal nowrapscan
1109 call assert_equal([], g:options)
1110 call assert_equal(g:opt[0], g:opt[1])
1111
1112 " 28c: Setting again boolean global option"
1113 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1114 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001115 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001116 set nowrapscan
1117 call assert_equal([], g:options)
1118 call assert_equal(g:opt[0], g:opt[1])
1119
1120 " 28d: Setting again global boolean global option"
1121 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001122 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001123 set wrapscan
1124 call assert_equal([], g:options)
1125 call assert_equal(g:opt[0], g:opt[1])
1126
1127
1128 " 29a: Setting global boolean global-local (to buffer) option"
1129 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1130 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001131 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001132 setglobal autoread
1133 call assert_equal([], g:options)
1134 call assert_equal(g:opt[0], g:opt[1])
1135
1136 " 29b: Setting local boolean global-local (to buffer) option"
1137 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1138 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001139 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001140 setlocal noautoread
1141 call assert_equal([], g:options)
1142 call assert_equal(g:opt[0], g:opt[1])
1143
1144 " 29c: Setting again boolean global-local (to buffer) option"
1145 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1146 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001147 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001148 set autoread
1149 call assert_equal([], g:options)
1150 call assert_equal(g:opt[0], g:opt[1])
1151
1152 " 29d: Setting again global boolean global-local (to buffer) option"
1153 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001154 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001155 set autoread
1156 call assert_equal([], g:options)
1157 call assert_equal(g:opt[0], g:opt[1])
1158
1159
1160 " 30a: Setting global boolean local (to buffer) option"
1161 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1162 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001163 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001164 setglobal cindent
1165 call assert_equal([], g:options)
1166 call assert_equal(g:opt[0], g:opt[1])
1167
1168 " 30b: Setting local boolean local (to buffer) option"
1169 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1170 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001171 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001172 setlocal nocindent
1173 call assert_equal([], g:options)
1174 call assert_equal(g:opt[0], g:opt[1])
1175
1176 " 30c: Setting again boolean local (to buffer) option"
1177 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1178 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001179 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001180 set cindent
1181 call assert_equal([], g:options)
1182 call assert_equal(g:opt[0], g:opt[1])
1183
1184 " 30d: Setting again global boolean local (to buffer) option"
1185 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001186 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001187 set cindent
1188 call assert_equal([], g:options)
1189 call assert_equal(g:opt[0], g:opt[1])
1190
1191
1192 " 31: Setting boolean global-local (to window) option
1193 " Currently no such option exists.
1194
1195
1196 " 32a: Setting global boolean local (to window) option"
1197 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1198 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001199 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001200 setglobal cursorcolumn
1201 call assert_equal([], g:options)
1202 call assert_equal(g:opt[0], g:opt[1])
1203
1204 " 32b: Setting local boolean local (to window) option"
1205 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1206 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001207 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001208 setlocal nocursorcolumn
1209 call assert_equal([], g:options)
1210 call assert_equal(g:opt[0], g:opt[1])
1211
1212 " 32c: Setting again boolean local (to window) option"
1213 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1214 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001215 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001216 set cursorcolumn
1217 call assert_equal([], g:options)
1218 call assert_equal(g:opt[0], g:opt[1])
1219
1220 " 32d: Setting again global boolean local (to window) option"
1221 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001222 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001223 set cursorcolumn
1224 call assert_equal([], g:options)
1225 call assert_equal(g:opt[0], g:opt[1])
1226
1227
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001228 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001229 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001230 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001231 set backspace=2
1232 call assert_equal([], g:options)
1233 call assert_equal(g:opt[0], g:opt[1])
1234
1235
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001236 " Cleanup
1237 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001238 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001239 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 +02001240 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001241 endfor
1242 call test_override('starting', 0)
1243 delfunc! AutoCommandOptionSet
1244endfunc
1245
1246func Test_OptionSet_diffmode()
1247 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001248 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001249 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001250 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001251
1252 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1253 call assert_equal(0, &l:cul)
1254 diffthis
1255 call assert_equal(1, &l:cul)
1256
1257 vnew
1258 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1259 call assert_equal(0, &l:cul)
1260 diffthis
1261 call assert_equal(1, &l:cul)
1262
1263 diffoff
1264 call assert_equal(0, &l:cul)
1265 call assert_equal(1, getwinvar(2, '&l:cul'))
1266 bw!
1267
1268 call assert_equal(1, &l:cul)
1269 diffoff!
1270 call assert_equal(0, &l:cul)
1271 call assert_equal(0, getwinvar(1, '&l:cul'))
1272 bw!
1273
1274 " Cleanup
1275 au! OptionSet
1276 call test_override('starting', 0)
1277endfunc
1278
1279func Test_OptionSet_diffmode_close()
1280 call test_override('starting', 1)
1281 " 19: Try to close the current window when entering diff mode
1282 " should not segfault
1283 new
1284 au OptionSet diff close
1285
1286 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001287 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001288 call assert_equal(1, &diff)
1289 vnew
1290 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001291 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001292 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001293 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001294 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001295 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001296 bw!
1297
1298 " Cleanup
1299 au! OptionSet
1300 call test_override('starting', 0)
1301 "delfunc! AutoCommandOptionSet
1302endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001303
1304" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1305func Test_BufleaveWithDelete()
1306 new | edit Xfile1
1307
1308 augroup test_bufleavewithdelete
1309 autocmd!
1310 autocmd BufLeave Xfile1 bwipe Xfile2
1311 augroup END
1312
1313 call assert_fails('edit Xfile2', 'E143:')
1314 call assert_equal('Xfile1', bufname('%'))
1315
1316 autocmd! test_bufleavewithdelete BufLeave Xfile1
1317 augroup! test_bufleavewithdelete
1318
1319 new
1320 bwipe! Xfile1
1321endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001322
1323" Test for autocommand that changes the buffer list, when doing ":ball".
1324func Test_Acmd_BufAll()
1325 enew!
1326 %bwipe!
1327 call writefile(['Test file Xxx1'], 'Xxx1')
1328 call writefile(['Test file Xxx2'], 'Xxx2')
1329 call writefile(['Test file Xxx3'], 'Xxx3')
1330
1331 " Add three files to the buffer list
1332 split Xxx1
1333 close
1334 split Xxx2
1335 close
1336 split Xxx3
1337 close
1338
1339 " Wipe the buffer when the buffer is opened
1340 au BufReadPost Xxx2 bwipe
1341
1342 call append(0, 'Test file Xxx4')
1343 ball
1344
1345 call assert_equal(2, winnr('$'))
1346 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1347 wincmd t
1348
1349 au! BufReadPost
1350 %bwipe!
1351 call delete('Xxx1')
1352 call delete('Xxx2')
1353 call delete('Xxx3')
1354 enew! | only
1355endfunc
1356
1357" Test for autocommand that changes current buffer on BufEnter event.
1358" Check if modelines are interpreted for the correct buffer.
1359func Test_Acmd_BufEnter()
1360 %bwipe!
1361 call writefile(['start of test file Xxx1',
1362 \ "\<Tab>this is a test",
1363 \ 'end of test file Xxx1'], 'Xxx1')
1364 call writefile(['start of test file Xxx2',
1365 \ 'vim: set noai :',
1366 \ "\<Tab>this is a test",
1367 \ 'end of test file Xxx2'], 'Xxx2')
1368
1369 au BufEnter Xxx2 brew
1370 set ai modeline modelines=3
1371 edit Xxx1
1372 " edit Xxx2, autocmd will do :brew
1373 edit Xxx2
1374 exe "normal G?this is a\<CR>"
1375 " Append text with autoindent to this file
1376 normal othis should be auto-indented
1377 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1378 call assert_equal(3, line('.'))
1379 " Remove autocmd and edit Xxx2 again
1380 au! BufEnter Xxx2
1381 buf! Xxx2
1382 exe "normal G?this is a\<CR>"
1383 " append text without autoindent to Xxx
1384 normal othis should be in column 1
1385 call assert_equal("this should be in column 1", getline('.'))
1386 call assert_equal(4, line('.'))
1387
1388 %bwipe!
1389 call delete('Xxx1')
1390 call delete('Xxx2')
1391 set ai&vim modeline&vim modelines&vim
1392endfunc
1393
1394" Test for issue #57
1395" do not move cursor on <c-o> when autoindent is set
1396func Test_ai_CTRL_O()
1397 enew!
1398 set ai
1399 let save_fo = &fo
1400 set fo+=r
1401 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1402 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1403 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1404
1405 set ai&vim
1406 let &fo = save_fo
1407 enew!
1408endfunc
1409
1410" Test for autocommand that deletes the current buffer on BufLeave event.
1411" Also test deleting the last buffer, should give a new, empty buffer.
1412func Test_BufLeave_Wipe()
1413 %bwipe!
1414 let content = ['start of test file Xxx',
1415 \ 'this is a test',
1416 \ 'end of test file Xxx']
1417 call writefile(content, 'Xxx1')
1418 call writefile(content, 'Xxx2')
1419
1420 au BufLeave Xxx2 bwipe
1421 edit Xxx1
1422 split Xxx2
1423 " delete buffer Xxx2, we should be back to Xxx1
1424 bwipe
1425 call assert_equal('Xxx1', bufname('%'))
1426 call assert_equal(1, winnr('$'))
1427
1428 " Create an alternate buffer
1429 %write! test.out
1430 call assert_equal('test.out', bufname('#'))
1431 " delete alternate buffer
1432 bwipe test.out
1433 call assert_equal('Xxx1', bufname('%'))
1434 call assert_equal('', bufname('#'))
1435
1436 au BufLeave Xxx1 bwipe
1437 " delete current buffer, get an empty one
1438 bwipe!
1439 call assert_equal(1, line('$'))
1440 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001441 let g:bufinfo = getbufinfo()
1442 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001443
1444 call delete('Xxx1')
1445 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001446 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001447 %bwipe
1448 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001449
1450 " check that bufinfo doesn't contain a pointer to freed memory
1451 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001452endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001453
1454func Test_QuitPre()
1455 edit Xfoo
1456 let winid = win_getid(winnr())
1457 split Xbar
1458 au! QuitPre * let g:afile = expand('<afile>')
1459 " Close the other window, <afile> should be correct.
1460 exe win_id2win(winid) . 'q'
1461 call assert_equal('Xfoo', g:afile)
1462
1463 unlet g:afile
1464 bwipe Xfoo
1465 bwipe Xbar
1466endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001467
1468func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001469 au! CmdlineChanged : let g:text = getcmdline()
1470 let g:text = 0
1471 call feedkeys(":echom 'hello'\<CR>", 'xt')
1472 call assert_equal("echom 'hello'", g:text)
1473 au! CmdlineChanged
1474
1475 au! CmdlineChanged : let g:entered = expand('<afile>')
1476 let g:entered = 0
1477 call feedkeys(":echom 'hello'\<CR>", 'xt')
1478 call assert_equal(':', g:entered)
1479 au! CmdlineChanged
1480
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001481 au! CmdlineEnter : let g:entered = expand('<afile>')
1482 au! CmdlineLeave : let g:left = expand('<afile>')
1483 let g:entered = 0
1484 let g:left = 0
1485 call feedkeys(":echo 'hello'\<CR>", 'xt')
1486 call assert_equal(':', g:entered)
1487 call assert_equal(':', g:left)
1488 au! CmdlineEnter
1489 au! CmdlineLeave
1490
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001491 let save_shellslash = &shellslash
1492 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001493 au! CmdlineEnter / let g:entered = expand('<afile>')
1494 au! CmdlineLeave / let g:left = expand('<afile>')
1495 let g:entered = 0
1496 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001497 new
1498 call setline(1, 'hello')
1499 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001500 call assert_equal('/', g:entered)
1501 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001502 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001503 au! CmdlineEnter
1504 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001505 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001506endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001507
1508" Test for BufWritePre autocommand that deletes or unloads the buffer.
1509func Test_BufWritePre()
1510 %bwipe
1511 au BufWritePre Xxx1 bunload
1512 au BufWritePre Xxx2 bwipe
1513
1514 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1515 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1516
1517 edit Xtest
1518 e! Xxx2
1519 bdel Xtest
1520 e Xxx1
1521 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001522 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001523 call assert_equal('Xxx2', bufname('%'))
1524 edit Xtest
1525 e! Xxx2
1526 bwipe Xtest
1527 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001528 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001529 call assert_equal('Xxx1', bufname('%'))
1530 au! BufWritePre
1531 call delete('Xxx1')
1532 call delete('Xxx2')
1533endfunc
1534
1535" Test for BufUnload autocommand that unloads all the other buffers
1536func Test_bufunload_all()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01001537 let g:test_is_flaky = 1
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001538 call writefile(['Test file Xxx1'], 'Xxx1')"
1539 call writefile(['Test file Xxx2'], 'Xxx2')"
1540
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001541 let content =<< trim [CODE]
1542 func UnloadAllBufs()
1543 let i = 1
1544 while i <= bufnr('$')
1545 if i != bufnr('%') && bufloaded(i)
1546 exe i . 'bunload'
1547 endif
1548 let i += 1
1549 endwhile
1550 endfunc
1551 au BufUnload * call UnloadAllBufs()
1552 au VimLeave * call writefile(['Test Finished'], 'Xout')
1553 edit Xxx1
1554 split Xxx2
1555 q
1556 [CODE]
1557
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001558 call writefile(content, 'Xtest')
1559
1560 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001561 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001562 call assert_true(filereadable('Xout'))
1563
1564 call delete('Xxx1')
1565 call delete('Xxx2')
1566 call delete('Xtest')
1567 call delete('Xout')
1568endfunc
1569
1570" Some tests for buffer-local autocommands
1571func Test_buflocal_autocmd()
1572 let g:bname = ''
1573 edit xx
1574 au BufLeave <buffer> let g:bname = expand("%")
1575 " here, autocommand for xx should trigger.
1576 " but autocommand shall not apply to buffer named <buffer>.
1577 edit somefile
1578 call assert_equal('xx', g:bname)
1579 let g:bname = ''
1580 " here, autocommand shall be auto-deleted
1581 bwipe xx
1582 " autocmd should not trigger
1583 edit xx
1584 call assert_equal('', g:bname)
1585 " autocmd should not trigger
1586 edit somefile
1587 call assert_equal('', g:bname)
1588 enew
1589 unlet g:bname
1590endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001591
1592" Test for "*Cmd" autocommands
1593func Test_Cmd_Autocmds()
1594 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1595
1596 enew!
1597 au BufReadCmd XtestA 0r Xxx|$del
1598 edit XtestA " will read text of Xxd instead
1599 call assert_equal('start of Xxx', getline(1))
1600
1601 au BufWriteCmd XtestA call append(line("$"), "write")
1602 write " will append a line to the file
1603 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001604 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001605 call assert_equal('write', getline(4))
1606
1607 " now we have:
1608 " 1 start of Xxx
1609 " 2 abc2
1610 " 3 end of Xxx
1611 " 4 write
1612
1613 au FileReadCmd XtestB '[r Xxx
1614 2r XtestB " will read Xxx below line 2 instead
1615 call assert_equal('start of Xxx', getline(3))
1616
1617 " now we have:
1618 " 1 start of Xxx
1619 " 2 abc2
1620 " 3 start of Xxx
1621 " 4 abc2
1622 " 5 end of Xxx
1623 " 6 end of Xxx
1624 " 7 write
1625
1626 au FileWriteCmd XtestC '[,']copy $
1627 normal 4GA1
1628 4,5w XtestC " will copy lines 4 and 5 to the end
1629 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001630 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001631 call assert_equal("end of Xxx", getline(9))
1632
1633 " now we have:
1634 " 1 start of Xxx
1635 " 2 abc2
1636 " 3 start of Xxx
1637 " 4 abc21
1638 " 5 end of Xxx
1639 " 6 end of Xxx
1640 " 7 write
1641 " 8 abc21
1642 " 9 end of Xxx
1643
1644 let g:lines = []
1645 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1646 w >>XtestD " will add lines to 'lines'
1647 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001648 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001649 call assert_equal(9, line('$'))
1650 call assert_equal('end of Xxx', getline('$'))
1651
1652 au BufReadCmd XtestE 0r Xxx|$del
1653 sp XtestE " split window with test.out
1654 call assert_equal('end of Xxx', getline(3))
1655
1656 let g:lines = []
1657 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1658 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1659 wall " will write other window to 'lines'
1660 call assert_equal(4, len(g:lines), g:lines)
1661 call assert_equal('asdf', g:lines[2])
1662
1663 au! BufReadCmd
1664 au! BufWriteCmd
1665 au! FileReadCmd
1666 au! FileWriteCmd
1667 au! FileAppendCmd
1668 %bwipe!
1669 call delete('Xxx')
1670 enew!
1671endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001672
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001673func s:ReadFile()
1674 setl noswapfile nomodified
1675 let filename = resolve(expand("<afile>:p"))
1676 execute 'read' fnameescape(filename)
1677 1d_
1678 exe 'file' fnameescape(filename)
1679 setl buftype=acwrite
1680endfunc
1681
1682func s:WriteFile()
1683 let filename = resolve(expand("<afile>:p"))
1684 setl buftype=
1685 noautocmd execute 'write' fnameescape(filename)
1686 setl buftype=acwrite
1687 setl nomodified
1688endfunc
1689
1690func Test_BufReadCmd()
1691 autocmd BufReadCmd *.test call s:ReadFile()
1692 autocmd BufWriteCmd *.test call s:WriteFile()
1693
1694 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1695 edit Xcmd.test
1696 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1697 normal! Gofour
1698 write
1699 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1700
1701 bwipe!
1702 call delete('Xcmd.test')
1703 au! BufReadCmd
1704 au! BufWriteCmd
1705endfunc
1706
Bram Moolenaaraace2152017-11-05 16:23:10 +01001707func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001708 exe a:start .. 'mark ['
1709 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001710endfunc
1711
1712" Verify the effects of autocmds on '[ and ']
1713func Test_change_mark_in_autocmds()
1714 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001715 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001716
1717 call SetChangeMarks(2, 3)
1718 write
1719 call assert_equal([1, 4], [line("'["), line("']")])
1720
1721 call SetChangeMarks(2, 3)
1722 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1723 write
1724 au! BufWritePre
1725
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001726 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001727 write XtestFilter
1728 write >> XtestFilter
1729
1730 call SetChangeMarks(2, 3)
1731 " Marks are set to the entire range of the write
1732 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1733 " '[ is adjusted to just before the line that will receive the filtered
1734 " data
1735 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1736 " The filtered data is read into the buffer, and the source lines are
1737 " still present, so the range is after the source lines
1738 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1739 %!cat XtestFilter
1740 " After the filtered data is read, the original lines are deleted
1741 call assert_equal([1, 8], [line("'["), line("']")])
1742 au! FilterWritePre,FilterReadPre,FilterReadPost
1743 undo
1744
1745 call SetChangeMarks(1, 4)
1746 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1747 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1748 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1749 2,3!cat XtestFilter
1750 call assert_equal([2, 9], [line("'["), line("']")])
1751 au! FilterWritePre,FilterReadPre,FilterReadPost
1752 undo
1753
1754 call delete('XtestFilter')
1755 endif
1756
1757 call SetChangeMarks(1, 4)
1758 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1759 2,3write Xtest2
1760 au! FileWritePre
1761
1762 call SetChangeMarks(2, 3)
1763 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1764 write >> Xtest2
1765 au! FileAppendPre
1766
1767 call SetChangeMarks(1, 4)
1768 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1769 2,3write >> Xtest2
1770 au! FileAppendPre
1771
1772 call SetChangeMarks(1, 1)
1773 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1774 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1775 3read Xtest2
1776 au! FileReadPre,FileReadPost
1777 undo
1778
1779 call SetChangeMarks(4, 4)
1780 " When the line is 0, it's adjusted to 1
1781 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1782 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1783 0read Xtest2
1784 au! FileReadPre,FileReadPost
1785 undo
1786
1787 call SetChangeMarks(4, 4)
1788 " When the line is 0, it's adjusted to 1
1789 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1790 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1791 1read Xtest2
1792 au! FileReadPre,FileReadPost
1793 undo
1794
1795 bwipe!
1796 call delete('Xtest')
1797 call delete('Xtest2')
1798endfunc
1799
1800func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01001801 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01001802
1803 enew!
1804 call setline(1, ['a', 'b', 'c', 'd'])
1805
1806 let shelltemp = &shelltemp
1807 set shelltemp
1808
1809 let g:filter_au = 0
1810 au FilterWritePre * let g:filter_au += 1
1811 au FilterReadPre * let g:filter_au += 1
1812 au FilterReadPost * let g:filter_au += 1
1813 %!cat
1814 call assert_equal(3, g:filter_au)
1815
1816 if has('filterpipe')
1817 set noshelltemp
1818
1819 let g:filter_au = 0
1820 au FilterWritePre * let g:filter_au += 1
1821 au FilterReadPre * let g:filter_au += 1
1822 au FilterReadPost * let g:filter_au += 1
1823 %!cat
1824 call assert_equal(0, g:filter_au)
1825 endif
1826
1827 au! FilterWritePre,FilterReadPre,FilterReadPost
1828 let &shelltemp = shelltemp
1829 bwipe!
1830endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001831
1832func Test_TextYankPost()
1833 enew!
1834 call setline(1, ['foo'])
1835
1836 let g:event = []
1837 au TextYankPost * let g:event = copy(v:event)
1838
1839 call assert_equal({}, v:event)
1840 call assert_fails('let v:event = {}', 'E46:')
1841 call assert_fails('let v:event.mykey = 0', 'E742:')
1842
1843 norm "ayiw
1844 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001845 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001846 \g:event)
1847 norm y_
1848 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001849 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false},
1850 \g:event)
1851 norm Vy
1852 call assert_equal(
1853 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001854 \g:event)
1855 call feedkeys("\<C-V>y", 'x')
1856 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001857 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001858 \g:event)
1859 norm "xciwbar
1860 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001861 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001862 \g:event)
1863 norm "bdiw
1864 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001865 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001866 \g:event)
1867
1868 call assert_equal({}, v:event)
1869
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001870 if has('clipboard_working') && !has('gui_running')
1871 " Test that when the visual selection is automatically copied to clipboard
1872 " register a TextYankPost is emitted
1873 call setline(1, ['foobar'])
1874
1875 let @* = ''
1876 set clipboard=autoselect
1877 exe "norm! ggviw\<Esc>"
1878 call assert_equal(
1879 \{'regcontents': ['foobar'], 'regname': '*', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1880 \g:event)
1881
1882 let @+ = ''
1883 set clipboard=autoselectplus
1884 exe "norm! ggviw\<Esc>"
1885 call assert_equal(
1886 \{'regcontents': ['foobar'], 'regname': '+', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1887 \g:event)
1888
1889 set clipboard&vim
1890 endif
1891
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001892 au! TextYankPost
1893 unlet g:event
1894 bwipe!
1895endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001896
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01001897func Test_autocommand_all_events()
1898 call assert_fails('au * * bwipe', 'E1155:')
1899 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaarb6db1462021-12-24 19:24:47 +00001900 call assert_fails('au! * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001901endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001902
Bram Moolenaarf6246f52022-02-11 16:30:12 +00001903func Test_autocmd_user()
1904 au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")]
1905 doautocmd User MyEvent
1906 call assert_equal(['MyEvent', 'MyEvent'], s:res)
1907 au! User
1908 unlet s:res
1909endfunc
1910
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001911function s:Before_test_dirchanged()
1912 augroup test_dirchanged
1913 autocmd!
1914 augroup END
1915 let s:li = []
1916 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001917 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001918 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001919 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001920 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001921endfunc
1922
1923function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001924 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001925 call delete(s:dir_foo, 'd')
1926 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001927 augroup test_dirchanged
1928 autocmd!
1929 augroup END
1930endfunc
1931
1932function Test_dirchanged_global()
1933 call s:Before_test_dirchanged()
Bram Moolenaarf6246f52022-02-11 16:30:12 +00001934 autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001935 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1936 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001937 call chdir(s:dir_foo)
Bram Moolenaarf6246f52022-02-11 16:30:12 +00001938 let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo]
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001939 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001940 call chdir(s:dir_foo)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001941 call assert_equal(expected, s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001942 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001943 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001944 call s:After_test_dirchanged()
1945endfunc
1946
1947function Test_dirchanged_local()
1948 call s:Before_test_dirchanged()
1949 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1950 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001951 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001952 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001953 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001954 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001955 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001956 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001957 call s:After_test_dirchanged()
1958endfunc
1959
1960function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001961 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001962 call s:Before_test_dirchanged()
1963 call test_autochdir()
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001964 autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001965 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1966 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1967 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001968 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001969 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001970 exe 'edit ' . s:dir_foo . '/Xfile'
1971 call assert_equal(s:dir_foo, getcwd())
Bram Moolenaar28e8f732022-02-09 12:58:20 +00001972 let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
1973 call assert_equal(expected, s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001974 set noacd
1975 bwipe!
1976 call s:After_test_dirchanged()
1977endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001978
1979" Test TextChangedI and TextChangedP
1980func Test_ChangedP()
1981 new
1982 call setline(1, ['foo', 'bar', 'foobar'])
1983 call test_override("char_avail", 1)
1984 set complete=. completeopt=menuone
1985
1986 func! TextChangedAutocmd(char)
1987 let g:autocmd .= a:char
1988 endfunc
1989
Christian Brabandtdb3b4462021-10-16 11:58:55 +01001990 " TextChanged will not be triggered, only check that it isn't.
Bram Moolenaar5a093432018-02-10 18:15:19 +01001991 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1992 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1993 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1994
1995 call cursor(3, 1)
1996 let g:autocmd = ''
1997 call feedkeys("o\<esc>", 'tnix')
1998 call assert_equal('I', g:autocmd)
1999
2000 let g:autocmd = ''
2001 call feedkeys("Sf", 'tnix')
2002 call assert_equal('II', g:autocmd)
2003
2004 let g:autocmd = ''
2005 call feedkeys("Sf\<C-N>", 'tnix')
2006 call assert_equal('IIP', g:autocmd)
2007
2008 let g:autocmd = ''
2009 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
2010 call assert_equal('IIPP', g:autocmd)
2011
2012 let g:autocmd = ''
2013 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
2014 call assert_equal('IIPPP', g:autocmd)
2015
2016 let g:autocmd = ''
2017 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
2018 call assert_equal('IIPPPP', g:autocmd)
2019
2020 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
2021 " TODO: how should it handle completeopt=noinsert,noselect?
2022
2023 " CleanUp
2024 call test_override("char_avail", 0)
2025 au! TextChanged
2026 au! TextChangedI
2027 au! TextChangedP
2028 delfu TextChangedAutocmd
2029 unlet! g:autocmd
2030 set complete&vim completeopt&vim
2031
2032 bw!
2033endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002034
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002035let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01002036func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002037 if !g:setline_handled
2038 call setline(1, "(x)")
2039 let g:setline_handled = v:true
2040 endif
2041endfunc
2042
2043func Test_TextChangedI_with_setline()
2044 new
2045 call test_override('char_avail', 1)
2046 autocmd TextChangedI <buffer> call SetLineOne()
2047 call feedkeys("i(\<CR>\<Esc>", 'tx')
2048 call assert_equal('(', getline(1))
2049 call assert_equal('x)', getline(2))
2050 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002051 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02002052 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002053
Bram Moolenaarca34db32022-01-20 11:17:18 +00002054 call test_override('char_avail', 0)
Bram Moolenaar91d2e782018-08-07 19:05:01 +02002055 bwipe!
2056endfunc
2057
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002058func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002059 CheckFeature terminal
2060 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002061 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002062 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002063
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002064 " Prepare file for TextChanged event.
2065 call writefile([''], 'Xchanged.txt')
2066 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2067 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002068 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002069 " In ConPTY, there is additional character which is drawn up to the width of
2070 " the screen.
2071 if has('conpty')
2072 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2073 else
2074 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2075 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002076 " It's only adding autocmd, so that no event occurs.
2077 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2078 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002079 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002080 call assert_equal([''], readfile('Xchanged.txt'))
2081
2082 " clean up
2083 call delete('Xchanged.txt')
2084 bwipe!
2085endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002086
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002087func Test_autocmd_nested()
2088 let g:did_nested = 0
2089 augroup Testing
2090 au WinNew * edit somefile
2091 au BufNew * let g:did_nested = 1
2092 augroup END
2093 split
2094 call assert_equal(0, g:did_nested)
2095 close
2096 bwipe! somefile
2097
2098 " old nested argument still works
2099 augroup Testing
2100 au!
2101 au WinNew * nested edit somefile
2102 au BufNew * let g:did_nested = 1
2103 augroup END
2104 split
2105 call assert_equal(1, g:did_nested)
2106 close
2107 bwipe! somefile
2108
2109 " New ++nested argument works
2110 augroup Testing
2111 au!
2112 au WinNew * ++nested edit somefile
2113 au BufNew * let g:did_nested = 1
2114 augroup END
2115 split
2116 call assert_equal(1, g:did_nested)
2117 close
2118 bwipe! somefile
2119
Bram Moolenaarf0775142022-03-04 20:10:38 +00002120 " nested without ++ does not work in Vim9 script
2121 call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:')
2122
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002123 augroup Testing
2124 au!
2125 augroup END
2126
2127 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2128 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2129endfunc
2130
2131func Test_autocmd_once()
2132 " Without ++once WinNew triggers twice
2133 let g:did_split = 0
2134 augroup Testing
2135 au WinNew * let g:did_split += 1
2136 augroup END
2137 split
2138 split
2139 call assert_equal(2, g:did_split)
2140 call assert_true(exists('#WinNew'))
2141 close
2142 close
2143
2144 " With ++once WinNew triggers once
2145 let g:did_split = 0
2146 augroup Testing
2147 au!
2148 au WinNew * ++once let g:did_split += 1
2149 augroup END
2150 split
2151 split
2152 call assert_equal(1, g:did_split)
2153 call assert_false(exists('#WinNew'))
2154 close
2155 close
2156
2157 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2158endfunc
2159
Bram Moolenaara68e5952019-04-25 22:22:01 +02002160func Test_autocmd_bufreadpre()
2161 new
2162 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002163 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002164 w! XAutocmdBufReadPre.txt
2165 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002166 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002167 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002168 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002169 wincmd p
2170 let g:wsv1 = winsaveview()
2171 wincmd p
2172 let g:wsv2 = winsaveview()
2173 " triggers BufReadPre, should not move the cursor in either window
2174 " The topline may change one line in a large window.
2175 edit
2176 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2177 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2178 call assert_equal(2, b:bufreadpre)
2179 wincmd p
2180 call assert_equal(g:wsv1.topline, winsaveview().topline)
2181 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2182 call assert_equal(2, b:bufreadpre)
2183 " Now set the cursor position in an BufReadPre autocommand
2184 " (even though the position will be invalid, this should make Vim reset the
2185 " cursor position in the other window.
2186 wincmd p
2187 set cpo+=g
2188 " won't do anything, but try to set the cursor on an invalid lnum
2189 autocmd BufReadPre <buffer> :norm! 70gg
2190 " triggers BufReadPre, should not move the cursor in either window
2191 e
2192 call assert_equal(1, winsaveview().topline)
2193 call assert_equal(1, winsaveview().lnum)
2194 call assert_equal(3, b:bufreadpre)
2195 wincmd p
2196 call assert_equal(g:wsv1.topline, winsaveview().topline)
2197 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2198 call assert_equal(3, b:bufreadpre)
2199 close
2200 close
2201 call delete('XAutocmdBufReadPre.txt')
2202 set cpo-=g
2203endfunc
2204
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002205" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002206
2207" Tests for the following autocommands:
2208" - FileWritePre writing a compressed file
2209" - FileReadPost reading a compressed file
2210" - BufNewFile reading a file template
2211" - BufReadPre decompressing the file to be read
2212" - FilterReadPre substituting characters in the temp file
2213" - FilterReadPost substituting characters after filtering
2214" - FileReadPre set options for decompression
2215" - FileReadPost decompress the file
2216func Test_ReadWrite_Autocmds()
2217 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002218 CheckUnix
2219 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002220
2221 " Make $GZIP empty, "-v" would cause trouble.
2222 let $GZIP = ""
2223
2224 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2225 " being modified outside of Vim (noticed on Solaris).
2226 au FileChangedShell * echo 'caught FileChangedShell'
2227
2228 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2229 augroup Test1
2230 au!
2231 au FileWritePre *.gz '[,']!gzip
2232 au FileWritePost *.gz undo
2233 au FileReadPost *.gz '[,']!gzip -d
2234 augroup END
2235
2236 new
2237 set bin
2238 call append(0, [
2239 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2240 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2241 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2242 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2243 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2244 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2245 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2246 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2247 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2248 \ ])
2249 1,9write! Xtestfile.gz
2250 enew! | close
2251
2252 new
2253 " Read and decompress the testfile
2254 0read Xtestfile.gz
2255 call assert_equal([
2256 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2257 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2258 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2259 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2260 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2261 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2262 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2263 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2264 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2265 \ ], getline(1, 9))
2266 enew! | close
2267
2268 augroup Test1
2269 au!
2270 augroup END
2271
2272 " Test for the FileAppendPre and FileAppendPost autocmds
2273 augroup Test2
2274 au!
2275 au BufNewFile *.c read Xtest.c
2276 au FileAppendPre *.out '[,']s/new/NEW/
2277 au FileAppendPost *.out !cat Xtest.c >> test.out
2278 augroup END
2279
2280 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2281 new foo.c " should load Xtest.c
2282 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2283 w! >> test.out " append it to the output file
2284
2285 let contents = readfile('test.out')
2286 call assert_equal(' * Here is a NEW .c file', contents[2])
2287 call assert_equal(' * Here is a new .c file', contents[5])
2288
2289 call delete('test.out')
2290 enew! | close
2291 augroup Test2
2292 au!
2293 augroup END
2294
2295 " Test for the BufReadPre and BufReadPost autocmds
2296 augroup Test3
2297 au!
2298 " setup autocommands to decompress before reading and re-compress
2299 " afterwards
2300 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2301 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2302 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2303 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2304 augroup END
2305
2306 e! Xtestfile.gz " Edit compressed file
2307 call assert_equal([
2308 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2309 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2310 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2311 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2312 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2313 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2314 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2315 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2316 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2317 \ ], getline(1, 9))
2318
2319 w! >> test.out " Append it to the output file
2320
2321 augroup Test3
2322 au!
2323 augroup END
2324
2325 " Test for the FilterReadPre and FilterReadPost autocmds.
2326 set shelltemp " need temp files here
2327 augroup Test4
2328 au!
2329 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2330 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2331 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2332 au FilterReadPost *.out '[,']s/x/X/g
2333 augroup END
2334
2335 e! test.out " Edit the output file
2336 1,$!cat
2337 call assert_equal([
2338 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2339 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2340 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2341 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2342 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2343 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2344 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2345 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2346 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2347 \ ], getline(1, 9))
2348 call assert_equal([
2349 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2350 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2351 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2352 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2353 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2354 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2355 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2356 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2357 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2358 \ ], readfile('test.out'))
2359
2360 augroup Test4
2361 au!
2362 augroup END
2363 set shelltemp&vim
2364
2365 " Test for the FileReadPre and FileReadPost autocmds.
2366 augroup Test5
2367 au!
2368 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2369 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2370 au FileReadPost *.gz '[,']s/l/L/
2371 augroup END
2372
2373 new
2374 0r Xtestfile.gz " Read compressed file
2375 call assert_equal([
2376 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2377 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2378 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2379 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2380 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2381 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2382 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2383 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2384 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2385 \ ], getline(1, 9))
2386 call assert_equal([
2387 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2388 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2389 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2390 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2391 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2392 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2393 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2394 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2395 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2396 \ ], readfile('Xtestfile.gz'))
2397
2398 augroup Test5
2399 au!
2400 augroup END
2401
2402 au! FileChangedShell
2403 call delete('Xtestfile.gz')
2404 call delete('Xtest.c')
2405 call delete('test.out')
2406endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002407
2408func Test_throw_in_BufWritePre()
2409 new
2410 call setline(1, ['one', 'two', 'three'])
2411 call assert_false(filereadable('Xthefile'))
2412 augroup throwing
2413 au BufWritePre X* throw 'do not write'
2414 augroup END
2415 try
2416 w Xthefile
2417 catch
2418 let caught = 1
2419 endtry
2420 call assert_equal(1, caught)
2421 call assert_false(filereadable('Xthefile'))
2422
2423 bwipe!
2424 au! throwing
2425endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002426
Bram Moolenaar40fa12a2021-09-22 14:18:13 +02002427func Test_autocmd_in_try_block()
2428 call mkdir('Xdir')
2429 au BufEnter * let g:fname = expand('%')
2430 try
2431 edit Xdir/
2432 endtry
2433 call assert_match('Xdir', g:fname)
2434
2435 unlet g:fname
2436 au! BufEnter
2437 call delete('Xdir', 'rf')
2438endfunc
2439
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002440func Test_autocmd_SafeState()
2441 CheckRunVimInTerminal
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002442 let g:test_is_flaky = 1
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002443
2444 let lines =<< trim END
2445 let g:safe = 0
2446 let g:again = ''
2447 au SafeState * let g:safe += 1
2448 au SafeStateAgain * let g:again ..= 'x'
2449 func CallTimer()
2450 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2451 endfunc
2452 END
2453 call writefile(lines, 'XSafeState')
2454 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2455
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002456 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002457 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002458 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002459 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002460
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002461 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002462 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002463 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002464
2465 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002466 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002467 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002468 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002469 call term_sendkeys(buf, ":echo g:again\<CR>")
2470 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2471
2472 call StopVimInTerminal(buf)
2473 call delete('XSafeState')
2474endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002475
2476func Test_autocmd_CmdWinEnter()
2477 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002478 CheckFeature cmdwin
2479
Bram Moolenaar23324a02019-10-01 17:39:04 +02002480 let lines =<< trim END
Egor Zvorykin125ffd22021-11-17 14:01:14 +00002481 augroup vimHints | au! | augroup END
Bram Moolenaar23324a02019-10-01 17:39:04 +02002482 let b:dummy_var = 'This is a dummy'
2483 autocmd CmdWinEnter * quit
2484 let winnr = winnr('$')
2485 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002486 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002487 call writefile(lines, filename)
2488 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2489
2490 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002491 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002492 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002493 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002494 call term_sendkeys(buf, ":echo &buftype\<cr>")
2495 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2496 call term_sendkeys(buf, ":echo winnr\<cr>")
2497 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2498
2499 " clean up
2500 call StopVimInTerminal(buf)
2501 call delete(filename)
2502endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002503
2504func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002505 CheckFeature quickfix
2506
Bram Moolenaarec66c412019-10-11 21:19:13 +02002507 pedit xx
2508 n x
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002509 augroup winenter
2510 au WinEnter * if winnr('$') > 2 | quit | endif
2511 augroup END
Bram Moolenaarec66c412019-10-11 21:19:13 +02002512 split
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002513
2514 augroup winenter
2515 au! WinEnter
2516 augroup END
2517
2518 bwipe xx
2519 bwipe x
2520 pclose
Bram Moolenaarec66c412019-10-11 21:19:13 +02002521endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002522
2523func Test_BufWrite_lockmarks()
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +01002524 let g:test_is_flaky = 1
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002525 edit! Xtest
2526 call setline(1, ['a', 'b', 'c', 'd'])
2527
2528 " :lockmarks preserves the marks
2529 call SetChangeMarks(2, 3)
2530 lockmarks write
2531 call assert_equal([2, 3], [line("'["), line("']")])
2532
2533 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2534 " original values for the user
2535 augroup lockmarks
2536 au!
2537 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2538 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2539 augroup END
2540
2541 lockmarks write
2542 call assert_equal([2, 3], [line("'["), line("']")])
2543
2544 if executable('cat')
2545 lockmarks %!cat
2546 call assert_equal([2, 3], [line("'["), line("']")])
2547 endif
2548
2549 lockmarks 3,4write Xtest2
2550 call assert_equal([2, 3], [line("'["), line("']")])
2551
2552 au! lockmarks
2553 augroup! lockmarks
2554 call delete('Xtest')
2555 call delete('Xtest2')
2556endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002557
2558func Test_FileType_spell()
2559 if !isdirectory('/tmp')
2560 throw "Skipped: requires /tmp directory"
2561 endif
2562
2563 " this was crashing with an invalid free()
2564 setglobal spellfile=/tmp/en.utf-8.add
2565 augroup crash
2566 autocmd!
2567 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2568 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2569 autocmd FileType anotherfiletype setlocal spell
2570 augroup END
2571 func! NoCrash() abort
2572 edit /tmp/crashfile
2573 endfunc
2574 call NoCrash()
2575
2576 au! crash
2577 setglobal spellfile=
2578endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002579
Bram Moolenaar406cd902020-02-18 21:54:41 +01002580" Test closing a window or editing another buffer from a FileChangedRO handler
2581" in a readonly buffer
2582func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002583 call test_override('ui_delay', 10)
2584
Bram Moolenaar406cd902020-02-18 21:54:41 +01002585 augroup FileChangedROTest
2586 au!
2587 autocmd FileChangedRO * quit
2588 augroup END
2589 new
2590 set readonly
2591 call assert_fails('normal i', 'E788:')
2592 close
2593 augroup! FileChangedROTest
2594
2595 augroup FileChangedROTest
2596 au!
2597 autocmd FileChangedRO * edit Xfile
2598 augroup END
2599 new
2600 set readonly
2601 call assert_fails('normal i', 'E788:')
2602 close
2603 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002604 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002605endfunc
2606
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002607func LogACmd()
2608 call add(g:logged, line('$'))
2609endfunc
2610
2611func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002612 CheckNotGui
2613
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002614 enew!
2615 tabnew
2616 call setline(1, ['a', 'b', 'c', 'd'])
2617 $
2618 au TermChanged * call LogACmd()
2619 let g:logged = []
2620 let term_save = &term
2621 set term=xterm
2622 call assert_equal([1, 4], g:logged)
2623
2624 au! TermChanged
2625 let &term = term_save
2626 bwipe!
2627endfunc
2628
Bram Moolenaare3284872020-03-19 13:55:03 +01002629" Test for FileReadCmd autocmd
2630func Test_autocmd_FileReadCmd()
2631 func ReadFileCmd()
2632 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2633 endfunc
2634 augroup FileReadCmdTest
2635 au!
2636 au FileReadCmd Xtest call ReadFileCmd()
2637 augroup END
2638
2639 new
2640 read ++bin Xtest
2641 read ++nobin Xtest
2642 read ++edit Xtest
2643 read ++bad=keep Xtest
2644 read ++bad=drop Xtest
2645 read ++bad=- Xtest
2646 read ++ff=unix Xtest
2647 read ++ff=dos Xtest
2648 read ++ff=mac Xtest
2649 read ++enc=utf-8 Xtest
2650
2651 call assert_equal(['',
2652 \ 'v:cmdarg = ++bin',
2653 \ 'v:cmdarg = ++nobin',
2654 \ 'v:cmdarg = ++edit',
2655 \ 'v:cmdarg = ++bad=keep',
2656 \ 'v:cmdarg = ++bad=drop',
2657 \ 'v:cmdarg = ++bad=-',
2658 \ 'v:cmdarg = ++ff=unix',
2659 \ 'v:cmdarg = ++ff=dos',
2660 \ 'v:cmdarg = ++ff=mac',
2661 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2662
2663 close!
2664 augroup FileReadCmdTest
2665 au!
2666 augroup END
2667 delfunc ReadFileCmd
2668endfunc
2669
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002670" Test for passing invalid arguments to autocmd
2671func Test_autocmd_invalid_args()
2672 " Additional character after * for event
2673 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2674 augroup Test
2675 augroup END
2676 " Invalid autocmd event
2677 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2678 " Invalid autocmd event in a autocmd group
2679 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2680 augroup! Test
2681 " Execute all autocmds
2682 call assert_fails('doautocmd * BufEnter', 'E217:')
2683 call assert_fails('augroup! x1a2b3', 'E367:')
2684 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002685 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002686endfunc
2687
2688" Test for deep nesting of autocmds
2689func Test_autocmd_deep_nesting()
2690 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2691 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2692 autocmd! BufEnter Xfile
2693endfunc
2694
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002695" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2696func Test_autocmd_sigusr1()
2697 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002698 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002699
2700 let g:sigusr1_passed = 0
2701 au SigUSR1 * let g:sigusr1_passed = 1
2702 call system('/bin/kill -s usr1 ' . getpid())
2703 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2704
2705 au! SigUSR1
2706 unlet g:sigusr1_passed
2707endfunc
2708
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002709" Test for BufReadPre autocmd deleting the file
2710func Test_BufReadPre_delfile()
2711 augroup TestAuCmd
2712 au!
2713 autocmd BufReadPre Xfile call delete('Xfile')
2714 augroup END
2715 call writefile([], 'Xfile')
2716 call assert_fails('new Xfile', 'E200:')
2717 call assert_equal('Xfile', @%)
2718 call assert_equal(1, &readonly)
2719 call delete('Xfile')
2720 augroup TestAuCmd
2721 au!
2722 augroup END
2723 close!
2724endfunc
2725
2726" Test for BufReadPre autocmd changing the current buffer
2727func Test_BufReadPre_changebuf()
2728 augroup TestAuCmd
2729 au!
2730 autocmd BufReadPre Xfile edit Xsomeotherfile
2731 augroup END
2732 call writefile([], 'Xfile')
2733 call assert_fails('new Xfile', 'E201:')
2734 call assert_equal('Xsomeotherfile', @%)
2735 call assert_equal(1, &readonly)
2736 call delete('Xfile')
2737 augroup TestAuCmd
2738 au!
2739 augroup END
2740 close!
2741endfunc
2742
2743" Test for BufWipeouti autocmd changing the current buffer when reading a file
2744" in an empty buffer with 'f' flag in 'cpo'
2745func Test_BufDelete_changebuf()
2746 new
2747 augroup TestAuCmd
2748 au!
2749 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2750 augroup END
2751 let save_cpo = &cpo
2752 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002753 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002754 call assert_equal('somefile', @%)
2755 let &cpo = save_cpo
2756 augroup TestAuCmd
2757 au!
2758 augroup END
2759 close!
2760endfunc
2761
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002762" Test for the temporary internal window used to execute autocmds
2763func Test_autocmd_window()
2764 %bw!
2765 edit one.txt
2766 tabnew two.txt
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002767 vnew three.txt
2768 tabnew four.txt
2769 tabprevious
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002770 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002771 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002772 au!
2773 au BufEnter * call add(g:blist, [expand('<afile>'),
2774 \ win_gettype(bufwinnr(expand('<afile>')))])
2775 augroup END
2776
2777 doautoall BufEnter
Bram Moolenaar41cd8032021-03-13 15:47:56 +01002778 call assert_equal([
2779 \ ['one.txt', 'autocmd'],
2780 \ ['two.txt', ''],
2781 \ ['four.txt', 'autocmd'],
2782 \ ['three.txt', ''],
2783 \ ], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002784
Bram Moolenaar832adf92020-06-25 19:01:36 +02002785 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002786 au!
2787 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002788 augroup! aucmd_win_test1
2789 %bw!
2790endfunc
2791
2792" Test for trying to close the temporary window used for executing an autocmd
2793func Test_close_autocmd_window()
2794 %bw!
2795 edit one.txt
2796 tabnew two.txt
2797 augroup aucmd_win_test2
2798 au!
2799 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2800 augroup END
2801
2802 call assert_fails('doautoall BufEnter', 'E813:')
2803
2804 augroup aucmd_win_test2
2805 au!
2806 augroup END
2807 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002808 %bwipe!
2809endfunc
2810
2811" Test for trying to close the tab that has the temporary window for exeucing
2812" an autocmd.
2813func Test_close_autocmd_tab()
2814 edit one.txt
2815 tabnew two.txt
2816 augroup aucmd_win_test
2817 au!
2818 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2819 augroup END
2820
2821 call assert_fails('doautoall BufEnter', 'E813:')
2822
2823 tabonly
2824 augroup aucmd_win_test
2825 au!
2826 augroup END
2827 augroup! aucmd_win_test
2828 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002829endfunc
2830
Bram Moolenaarcb1956d2022-01-07 15:45:18 +00002831func Test_Visual_doautoall_redraw()
2832 call setline(1, ['a', 'b'])
2833 new
2834 wincmd p
2835 call feedkeys("G\<C-V>", 'txn')
2836 autocmd User Explode ++once redraw
2837 doautoall User Explode
2838 %bwipe!
2839endfunc
2840
Bram Moolenaar6bcb8772021-02-03 21:23:29 +01002841" This was using freed memory.
2842func Test_BufNew_arglocal()
2843 arglocal
2844 au BufNew * arglocal
2845 call assert_fails('drop xx', 'E1156:')
2846
2847 au! BufNew
2848endfunc
2849
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002850func Test_autocmd_closes_window()
2851 au BufNew,BufWinLeave * e %e
2852 file yyy
2853 au BufNew,BufWinLeave * ball
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002854 n xxx
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002855
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002856 %bwipe
Bram Moolenaar8ab37572021-02-03 21:56:59 +01002857 au! BufNew
2858 au! BufWinLeave
2859endfunc
2860
Bram Moolenaar92bb83e2021-02-03 23:04:46 +01002861func Test_autocmd_quit_psearch()
2862 sn aa bb
2863 augroup aucmd_win_test
2864 au!
2865 au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif
2866 augroup END
2867 ps /
2868
2869 augroup aucmd_win_test
2870 au!
2871 augroup END
2872endfunc
2873
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002874" Fuzzer found some strange combination that caused a crash.
2875func Test_autocmd_normal_mess()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002876 " For unknown reason this hangs on MS-Windows
2877 CheckNotMSWindows
2878
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002879 augroup aucmd_normal_test
2880 au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc
2881 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002882 call assert_fails('o4', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002883 silent! H
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002884 call assert_fails('e xx', 'E1159')
Bram Moolenaaraad5f9d2021-02-06 17:30:31 +01002885 normal G
2886
2887 augroup aucmd_normal_test
2888 au!
2889 augroup END
2890endfunc
2891
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002892func Test_autocmd_closing_cmdwin()
Bram Moolenaardd07c022021-02-07 13:32:46 +01002893 " For unknown reason this hangs on MS-Windows
2894 CheckNotMSWindows
2895
Bram Moolenaar8c6951f2021-02-06 18:08:45 +01002896 au BufWinLeave * nested q
2897 call assert_fails("norm 7q?\n", 'E855:')
2898
2899 au! BufWinLeave
2900 new
2901 only
2902endfunc
2903
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002904func Test_autocmd_vimgrep()
2905 augroup aucmd_vimgrep
2906 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * sb
2907 au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * q9�
2908 augroup END
Bram Moolenaar983d83f2021-02-07 12:12:43 +01002909 %bwipe!
Bram Moolenaardd07c022021-02-07 13:32:46 +01002910 call assert_fails('lv ?a? foo', 'E926:')
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002911
2912 augroup aucmd_vimgrep
2913 au!
2914 augroup END
2915endfunc
2916
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002917func Test_autocmd_with_block()
2918 augroup block_testing
2919 au BufReadPost *.xml {
2920 setlocal matchpairs+=<:>
2921 /<start
2922 }
Bram Moolenaar63b91732021-08-05 20:40:03 +02002923 au CursorHold * {
2924 autocmd BufReadPre * ++once echo 'one' | echo 'two'
2925 g:gotSafeState = 77
2926 }
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002927 augroup END
2928
2929 let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }"
2930 call assert_equal(expected, execute('au BufReadPost *.xml'))
2931
Bram Moolenaar63b91732021-08-05 20:40:03 +02002932 doautocmd CursorHold
2933 call assert_equal(77, g:gotSafeState)
2934 unlet g:gotSafeState
2935
Bram Moolenaar73b8b0a2021-08-01 14:52:32 +02002936 augroup block_testing
2937 au!
2938 augroup END
2939endfunc
2940
Christian Brabandtdb3b4462021-10-16 11:58:55 +01002941" Test TextChangedI and TextChanged
2942func Test_Changed_ChangedI()
2943 new
2944 call test_override("char_avail", 1)
2945 let [g:autocmd_i, g:autocmd_n] = ['','']
2946
2947 func! TextChangedAutocmdI(char)
2948 let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick
2949 endfunc
2950
2951 augroup Test_TextChanged
2952 au!
2953 au TextChanged <buffer> :call TextChangedAutocmdI('N')
2954 au TextChangedI <buffer> :call TextChangedAutocmdI('I')
2955 augroup END
2956
2957 call feedkeys("ifoo\<esc>", 'tnix')
2958 " TODO: Test test does not seem to trigger TextChanged autocommand, this
2959 " requires running Vim in a terminal window.
2960 " call assert_equal('N3', g:autocmd_n)
2961 call assert_equal('I3', g:autocmd_i)
2962
2963 call feedkeys("yyp", 'tnix')
2964 " TODO: Test test does not seem to trigger TextChanged autocommand.
2965 " call assert_equal('N4', g:autocmd_n)
2966 call assert_equal('I3', g:autocmd_i)
2967
2968 " CleanUp
2969 call test_override("char_avail", 0)
2970 au! TextChanged <buffer>
2971 au! TextChangedI <buffer>
2972 augroup! Test_TextChanged
2973 delfu TextChangedAutocmdI
2974 unlet! g:autocmd_i g:autocmd_n
2975
2976 bw!
2977endfunc
Bram Moolenaar2c7080b2021-02-06 19:19:42 +01002978
Bram Moolenaar6f2465d2022-03-22 18:13:01 +00002979func Test_closing_autocmd_window()
2980 let lines =<< trim END
2981 edit Xa.txt
2982 tabnew Xb.txt
2983 autocmd BufEnter Xa.txt unhide 1
2984 doautoall BufEnter
2985 END
2986 call v9.CheckScriptFailure(lines, 'E814:')
2987 au! BufEnter
2988 only!
2989 bwipe Xa.txt
2990 bwipe Xb.txt
2991endfunc
2992
Bram Moolenaar347538f2022-03-26 16:28:06 +00002993func Test_bufwipeout_changes_window()
2994 " This should not crash, but we don't have any expectations about what
2995 " happens, changing window in BufWipeout has unpredictable results.
2996 tabedit
2997 let g:window_id = win_getid()
2998 topleft new
2999 setlocal bufhidden=wipe
3000 autocmd BufWipeout <buffer> call win_gotoid(g:window_id)
3001 tabprevious
3002 +tabclose
3003
3004 unlet g:window_id
3005 au! BufWipeout
3006 %bwipe!
3007endfunc
3008
3009
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01003010" vim: shiftwidth=2 sts=2 expandtab