blob: 1e8711c6b13fee62f34429aad04ab86f735c0bc7 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02005source term_util.vim
Bram Moolenaar8c64a362018-03-23 22:39:31 +01006
Bram Moolenaar1e115362019-01-09 23:01:02 +01007func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +02008 for bnr in range(1, bufnr('$'))
9 if bufloaded(bnr) && bufnr('%') != bnr
10 execute 'bd! ' . bnr
11 endif
12 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020013endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020014
Bram Moolenaar14735512016-03-26 21:00:08 +010015func Test_vim_did_enter()
16 call assert_false(v:vim_did_enter)
17
18 " This script will never reach the main loop, can't check if v:vim_did_enter
19 " becomes one.
20endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020021
Bram Moolenaar75911162020-07-21 19:44:47 +020022" Test for the CursorHold autocmd
23func Test_CursorHold_autocmd()
24 CheckRunVimInTerminal
25 call writefile(['one', 'two', 'three'], 'Xfile')
26 let before =<< trim END
27 set updatetime=10
28 au CursorHold * call writefile([line('.')], 'Xoutput', 'a')
29 END
30 call writefile(before, 'Xinit')
31 let buf = RunVimInTerminal('-S Xinit Xfile', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020032 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020033 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020034 call term_sendkeys(buf, "gg")
35 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020036 call WaitForAssert({-> assert_equal(['1'], readfile('Xoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020037 call term_sendkeys(buf, "j")
38 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020039 call WaitForAssert({-> assert_equal(['1', '2'], readfile('Xoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020040 call term_sendkeys(buf, "j")
41 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020042 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('Xoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020043 call StopVimInTerminal(buf)
44
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call delete('Xinit')
46 call delete('Xoutput')
47 call delete('Xfile')
48endfunc
49
Bram Moolenaarc67e8922016-05-24 16:07:40 +020050if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020051
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052 func ExitInsertMode(id)
53 call feedkeys("\<Esc>")
54 endfunc
55
56 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020057 " Need to move the cursor.
58 call feedkeys("ggG", "xt")
59
Bram Moolenaarc67e8922016-05-24 16:07:40 +020060 let g:triggered = 0
61 au CursorHoldI * let g:triggered += 1
62 set updatetime=20
63 call timer_start(100, 'ExitInsertMode')
64 call feedkeys('a', 'x!')
65 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010066 unlet g:triggered
67 au! CursorHoldI
68 set updatetime&
69 endfunc
70
71 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020072 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010073 " Need to move the cursor.
74 call feedkeys("ggG", "xt")
75
76 " Confirm the timer invoked in exit_cb of the job doesn't disturb
77 " CursorHoldI event.
78 let g:triggered = 0
79 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020080 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010081 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020082 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010083 call feedkeys('a', 'x!')
84 call assert_equal(1, g:triggered)
85 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020086 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020087 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020088 endfunc
89
90 func Test_cursorhold_insert_ctrl_x()
91 let g:triggered = 0
92 au CursorHoldI * let g:triggered += 1
93 set updatetime=20
94 call timer_start(100, 'ExitInsertMode')
95 " CursorHoldI does not trigger after CTRL-X
96 call feedkeys("a\<C-X>", 'x!')
97 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010098 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020099 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200100 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200101 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200102
103 func Test_OptionSet_modeline()
104 call test_override('starting', 1)
105 au! OptionSet
106 augroup set_tabstop
107 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
108 augroup END
109 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline')
110 set modeline
111 let v:errmsg = ''
112 call assert_fails('split XoptionsetModeline', 'E12:')
113 call assert_equal(7, &ts)
114 call assert_equal('', v:errmsg)
115
116 augroup set_tabstop
117 au!
118 augroup END
119 bwipe!
120 set ts&
121 call delete('XoptionsetModeline')
122 call test_override('starting', 0)
123 endfunc
124
125endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200126
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200127func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200128 augroup test_bufunload_group
129 autocmd!
130 autocmd BufUnload * call add(s:li, "bufunload")
131 autocmd BufDelete * call add(s:li, "bufdelete")
132 autocmd BufWipeout * call add(s:li, "bufwipeout")
133 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200134
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200135 let s:li=[]
136 new
137 setlocal bufhidden=
138 bunload
139 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200140
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200141 let s:li=[]
142 new
143 setlocal bufhidden=delete
144 bunload
145 call assert_equal(["bufunload", "bufdelete"], s:li)
146
147 let s:li=[]
148 new
149 setlocal bufhidden=unload
150 bwipeout
151 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
152
Bram Moolenaare99e8442016-07-26 20:43:40 +0200153 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200154 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200155endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200156
157" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200158func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200159 tabedit
160 tabfirst
161
162 augroup test_autocmd_bufunload_with_tabnext_group
163 autocmd!
164 autocmd BufUnload <buffer> tabnext
165 augroup END
166
167 quit
168 call assert_equal(2, tabpagenr('$'))
169
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200170 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200171 augroup! test_autocmd_bufunload_with_tabnext_group
172 tablast
173 quit
174endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200175
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200176func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200177 tabedit
178 augroup sample
179 autocmd!
180 autocmd BufWinLeave <buffer> tabfirst
181 augroup END
182 call setline(1, ['a', 'b', 'c'])
183 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200184 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200185endfunc
186
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200187" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200188func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200189 split aa.txt
190 let lastbuf = bufnr('$')
191
192 augroup test_autocmd_bufunload
193 autocmd!
194 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
195 augroup END
196
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100197 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200198
199 autocmd! test_autocmd_bufunload
200 augroup! test_autocmd_bufunload
201 bwipe! aa.txt
202 bwipe! bb.txt
203endfunc
204
205" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200206func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200207 setlocal buftype=nowrite
208 let lastbuf = bufnr('$')
209
210 augroup test_autocmd_bufunload
211 autocmd!
212 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
213 augroup END
214
215 normal! i1
216 call assert_fails('edit a.txt', 'E517:')
217 call feedkeys("\<CR>")
218
219 autocmd! test_autocmd_bufunload
220 augroup! test_autocmd_bufunload
221 bwipe! a.txt
222endfunc
223
Bram Moolenaarc917da42016-07-19 22:31:36 +0200224func Test_win_tab_autocmd()
225 let g:record = []
226
227 augroup testing
228 au WinNew * call add(g:record, 'WinNew')
229 au WinEnter * call add(g:record, 'WinEnter')
230 au WinLeave * call add(g:record, 'WinLeave')
231 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200232 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200233 au TabEnter * call add(g:record, 'TabEnter')
234 au TabLeave * call add(g:record, 'TabLeave')
235 augroup END
236
237 split
238 tabnew
239 close
240 close
241
242 call assert_equal([
243 \ 'WinLeave', 'WinNew', 'WinEnter',
244 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200245 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200246 \ 'WinLeave', 'WinEnter'
247 \ ], g:record)
248
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200249 let g:record = []
250 tabnew somefile
251 tabnext
252 bwipe somefile
253
254 call assert_equal([
255 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
256 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
257 \ 'TabClosed'
258 \ ], g:record)
259
Bram Moolenaarc917da42016-07-19 22:31:36 +0200260 augroup testing
261 au!
262 augroup END
263 unlet g:record
264endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200265
266func s:AddAnAutocmd()
267 augroup vimBarTest
268 au BufReadCmd * echo 'hello'
269 augroup END
270 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
271endfunc
272
273func Test_early_bar()
274 " test that a bar is recognized before the {event}
275 call s:AddAnAutocmd()
276 augroup vimBarTest | au! | augroup END
277 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
278
279 call s:AddAnAutocmd()
280 augroup vimBarTest| au!| augroup END
281 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
282
283 " test that a bar is recognized after the {event}
284 call s:AddAnAutocmd()
285 augroup vimBarTest| au!BufReadCmd| augroup END
286 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
287
288 " test that a bar is recognized after the {group}
289 call s:AddAnAutocmd()
290 au! vimBarTest|echo 'hello'
291 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
292endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200293
Bram Moolenaar5c809082016-09-01 16:21:48 +0200294func RemoveGroup()
295 autocmd! StartOK
296 augroup! StartOK
297endfunc
298
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200299func Test_augroup_warning()
300 augroup TheWarning
301 au VimEnter * echo 'entering'
302 augroup END
303 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
304 redir => res
305 augroup! TheWarning
306 redir END
307 call assert_true(match(res, "W19:") >= 0)
308 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
309
310 " check "Another" does not take the pace of the deleted entry
311 augroup Another
312 augroup END
313 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200314 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200315
316 " no warning for postpone aucmd delete
317 augroup StartOK
318 au VimEnter * call RemoveGroup()
319 augroup END
320 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
321 redir => res
322 doautocmd VimEnter
323 redir END
324 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200325 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200326
327 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200328endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200329
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200330func Test_BufReadCmdHelp()
331 " This used to cause access to free memory
332 au BufReadCmd * e +h
333 help
334
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200335 au! BufReadCmd
336endfunc
337
338func Test_BufReadCmdHelpJump()
339 " This used to cause access to free memory
340 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200341 " } to fix highlighting
342 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200343
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200344 au! BufReadCmd
345endfunc
346
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200347func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200348 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200349 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200350 call assert_fails('augroup! x', 'E936:')
351 au VimEnter * echo
352 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200353 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200354 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
355 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200356endfunc
357
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200358" Tests for autocommands on :close command.
359" This used to be in test13.
360func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200361 " Clean up buffers, because in some cases this function fails.
362 call s:cleanup_buffers()
363
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200364 " Write three files and open them, each in a window.
365 " Then go to next window, with autocommand that deletes the previous one.
366 " Do this twice, writing the file.
367 e! Xtestje1
368 call setline(1, 'testje1')
369 w
370 sp Xtestje2
371 call setline(1, 'testje2')
372 w
373 sp Xtestje3
374 call setline(1, 'testje3')
375 w
376 wincmd w
377 au WinLeave Xtestje2 bwipe
378 wincmd w
379 call assert_equal('Xtestje1', expand('%'))
380
381 au WinLeave Xtestje1 bwipe Xtestje3
382 close
383 call assert_equal('Xtestje1', expand('%'))
384
385 " Test deleting the buffer on a Unload event. If this goes wrong there
386 " will be the ATTENTION prompt.
387 e Xtestje1
388 au!
389 au! BufUnload Xtestje1 bwipe
390 call assert_fails('e Xtestje3', 'E937:')
391 call assert_equal('Xtestje3', expand('%'))
392
393 e Xtestje2
394 sp Xtestje1
395 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200396 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200397
398 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
399 " there are ml_line errors and/or a Crash.
400 au!
401 only
402 e Xanother
403 e Xtestje1
404 bwipe Xtestje2
405 bwipe Xtestje3
406 au BufWipeout Xtestje1 buf Xtestje1
407 bwipe
408 call assert_equal('Xanother', expand('%'))
409
410 only
411 help
412 wincmd w
413 1quit
414 call assert_equal('Xanother', expand('%'))
415
416 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100417 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200418 call delete('Xtestje1')
419 call delete('Xtestje2')
420 call delete('Xtestje3')
421endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100422
423func Test_BufEnter()
424 au! BufEnter
425 au Bufenter * let val = val . '+'
426 let g:val = ''
427 split NewFile
428 call assert_equal('+', g:val)
429 bwipe!
430 call assert_equal('++', g:val)
431
432 " Also get BufEnter when editing a directory
433 call mkdir('Xdir')
434 split Xdir
435 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100436
437 " On MS-Windows we can't edit the directory, make sure we wipe the right
438 " buffer.
439 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100440
441 call delete('Xdir', 'd')
442 au! BufEnter
443endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100444
445" Closing a window might cause an endless loop
446" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200447func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200448 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100449 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200450 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100451 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100452 mksession!
453
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200454 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200455 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200456 set nocp noswapfile
457 let v:swapchoice="e"
458 augroup test_autocmd_sessionload
459 autocmd!
460 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
461 augroup END
462
463 func WriteErrors()
464 call writefile([execute("messages")], "Xerrors")
465 endfunc
466 au VimLeave * call WriteErrors()
467 [CODE]
468
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100469 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200470 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100471 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200472 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100473
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100474 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100475 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100476 call delete(file)
477 endfor
478endfunc
479
480" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200481func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100482 tabnew
483 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100484 mksession!
485
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200486 let content =<< trim [CODE]
487 set nocp noswapfile
488 function! DeleteInactiveBufs()
489 tabfirst
490 let tabblist = []
491 for i in range(1, tabpagenr(''$''))
492 call extend(tabblist, tabpagebuflist(i))
493 endfor
494 for b in range(1, bufnr(''$''))
495 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
496 exec ''bwipeout '' . b
497 endif
498 endfor
499 echomsg "SessionLoadPost DONE"
500 endfunction
501 au SessionLoadPost * call DeleteInactiveBufs()
502
503 func WriteErrors()
504 call writefile([execute("messages")], "Xerrors")
505 endfunc
506 au VimLeave * call WriteErrors()
507 [CODE]
508
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100509 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200510 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100511 let errors = join(readfile('Xerrors'))
512 " This probably only ever matches on unix.
513 call assert_notmatch('Caught deadly signal SEGV', errors)
514 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100515
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100516 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100517 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100518 call delete(file)
519 endfor
520endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200521
522func Test_empty_doau()
523 doau \|
524endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200525
526func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200527 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200528 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200529 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
530 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 +0200531 let g:opt = [expected, actual]
532 "call assert_equal(expected, actual)
533endfunc
534
535func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200536 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200537
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200538 badd test_autocmd.vim
539
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200540 call test_override('starting', 1)
541 set nocp
542 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
543
544 " 1: Setting number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200545 let g:options=[['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200546 set nu
547 call assert_equal([], g:options)
548 call assert_equal(g:opt[0], g:opt[1])
549
550 " 2: Setting local number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200551 let g:options=[['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200552 setlocal nonu
553 call assert_equal([], g:options)
554 call assert_equal(g:opt[0], g:opt[1])
555
556 " 3: Setting global number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200557 let g:options=[['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200558 setglobal nonu
559 call assert_equal([], g:options)
560 call assert_equal(g:opt[0], g:opt[1])
561
562 " 4: Setting local autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200563 let g:options=[['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200564 setlocal ai
565 call assert_equal([], g:options)
566 call assert_equal(g:opt[0], g:opt[1])
567
568 " 5: Setting global autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200569 let g:options=[['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200570 setglobal ai
571 call assert_equal([], g:options)
572 call assert_equal(g:opt[0], g:opt[1])
573
574 " 6: Setting global autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200575 let g:options=[['autoindent', 1, 1, 1, 0, 'global', 'set']]
576 set ai!
577 call assert_equal([], g:options)
578 call assert_equal(g:opt[0], g:opt[1])
579
580 " 6a: Setting global autoindent option"
581 let g:options=[['autoindent', 1, 1, 0, 0, 'global', 'set']]
582 noa setlocal ai
583 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200584 set ai!
585 call assert_equal([], g:options)
586 call assert_equal(g:opt[0], g:opt[1])
587
588 " Should not print anything, use :noa
589 " 7: don't trigger OptionSet"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200590 let g:options=[['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200591 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200592 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200593 call assert_equal(g:opt[0], g:opt[1])
594
595 " 8: Setting several global list and number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200596 let g:options=[['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200597 set list nu
598 call assert_equal([], g:options)
599 call assert_equal(g:opt[0], g:opt[1])
600
601 " 9: don't trigger OptionSet"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200602 let g:options=[['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200603 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200604 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 +0200605 call assert_equal(g:opt[0], g:opt[1])
606
607 " 10: Setting global acd"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200608 let g:options=[['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200609 setlocal acd
610 call assert_equal([], g:options)
611 call assert_equal(g:opt[0], g:opt[1])
612
613 " 11: Setting global autoread (also sets local value)"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200614 let g:options=[['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200615 set ar
616 call assert_equal([], g:options)
617 call assert_equal(g:opt[0], g:opt[1])
618
619 " 12: Setting local autoread"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200620 let g:options=[['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200621 setlocal ar
622 call assert_equal([], g:options)
623 call assert_equal(g:opt[0], g:opt[1])
624
625 " 13: Setting global autoread"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200626 let g:options=[['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200627 setglobal invar
628 call assert_equal([], g:options)
629 call assert_equal(g:opt[0], g:opt[1])
630
631 " 14: Setting option backspace through :let"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200632 let g:options=[['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200633 let &bs="eol,indent,start"
634 call assert_equal([], g:options)
635 call assert_equal(g:opt[0], g:opt[1])
636
637 " 15: Setting option backspace through setbufvar()"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200638 let g:options=[['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200639 " try twice, first time, shouldn't trigger because option name is invalid,
640 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200641 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200642 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200643 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200644 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200645 call assert_equal([], g:options)
646 call assert_equal(g:opt[0], g:opt[1])
647
648 " 16: Setting number option using setwinvar"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200649 let g:options=[['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200650 call setwinvar(0, '&number', 1)
651 call assert_equal([], g:options)
652 call assert_equal(g:opt[0], g:opt[1])
653
654 " 17: Setting key option, shouldn't trigger"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200655 let g:options=[['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200656 setlocal key=blah
657 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200658 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200659 call assert_equal(g:opt[0], g:opt[1])
660
Bram Moolenaard7c96872019-06-15 17:12:48 +0200661
662 " 18a: Setting string global option"
663 let oldval = &backupext
664 let g:options=[['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
665 set backupext=foo
666 call assert_equal([], g:options)
667 call assert_equal(g:opt[0], g:opt[1])
668
669 " 18b: Resetting string global option"
670 let g:options=[['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
671 set backupext&
672 call assert_equal([], g:options)
673 call assert_equal(g:opt[0], g:opt[1])
674
675 " 18c: Setting global string global option"
676 let g:options=[['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
677 setglobal backupext=bar
678 call assert_equal([], g:options)
679 call assert_equal(g:opt[0], g:opt[1])
680
681 " 18d: Setting local string global option"
682 " As this is a global option this sets the global value even though
683 " :setlocal is used!
684 noa set backupext& " Reset global and local value (without triggering autocmd)
685 let g:options=[['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
686 setlocal backupext=baz
687 call assert_equal([], g:options)
688 call assert_equal(g:opt[0], g:opt[1])
689
690 " 18e: Setting again string global option"
691 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
692 noa setlocal backupext=ext_local " Sets the global(!) value!
693 let g:options=[['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
694 set backupext=fuu
695 call assert_equal([], g:options)
696 call assert_equal(g:opt[0], g:opt[1])
697
698
699 " 19a: Setting string local-global (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200700 let oldval = &tags
Bram Moolenaard7c96872019-06-15 17:12:48 +0200701 let g:options=[['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200702 set tags=tagpath
703 call assert_equal([], g:options)
704 call assert_equal(g:opt[0], g:opt[1])
705
Bram Moolenaard7c96872019-06-15 17:12:48 +0200706 " 19b: Resetting string local-global (to buffer) option"
707 let g:options=[['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200708 set tags&
709 call assert_equal([], g:options)
710 call assert_equal(g:opt[0], g:opt[1])
711
Bram Moolenaard7c96872019-06-15 17:12:48 +0200712 " 19c: Setting global string local-global (to buffer) option "
713 let g:options=[['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
714 setglobal tags=tagpath1
715 call assert_equal([], g:options)
716 call assert_equal(g:opt[0], g:opt[1])
717
718 " 19d: Setting local string local-global (to buffer) option"
719 let g:options=[['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
720 setlocal tags=tagpath2
721 call assert_equal([], g:options)
722 call assert_equal(g:opt[0], g:opt[1])
723
724 " 19e: Setting again string local-global (to buffer) option"
725 " Note: v:option_old is the old global value for local-global string options
726 " but the old local value for all other kinds of options.
727 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
728 noa setlocal tags=tag_local
729 let g:options=[['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
730 set tags=tagpath
731 call assert_equal([], g:options)
732 call assert_equal(g:opt[0], g:opt[1])
733
734 " 19f: Setting string local-global (to buffer) option to an empty string"
735 " Note: v:option_old is the old global value for local-global string options
736 " but the old local value for all other kinds of options.
737 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
738 noa setlocal tags= " empty string
739 let g:options=[['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
740 set tags=tagpath
741 call assert_equal([], g:options)
742 call assert_equal(g:opt[0], g:opt[1])
743
744
745 " 20a: Setting string local (to buffer) option"
746 let oldval = &spelllang
747 let g:options=[['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
748 set spelllang=elvish,klingon
749 call assert_equal([], g:options)
750 call assert_equal(g:opt[0], g:opt[1])
751
752 " 20b: Resetting string local (to buffer) option"
753 let g:options=[['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
754 set spelllang&
755 call assert_equal([], g:options)
756 call assert_equal(g:opt[0], g:opt[1])
757
758 " 20c: Setting global string local (to buffer) option"
759 let g:options=[['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
760 setglobal spelllang=elvish
761 call assert_equal([], g:options)
762 call assert_equal(g:opt[0], g:opt[1])
763
764 " 20d: Setting local string local (to buffer) option"
765 noa set spelllang& " Reset global and local value (without triggering autocmd)
766 let g:options=[['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
767 setlocal spelllang=klingon
768 call assert_equal([], g:options)
769 call assert_equal(g:opt[0], g:opt[1])
770
771 " 20e: Setting again string local (to buffer) option"
772 " Note: v:option_old is the old global value for local-global string options
773 " but the old local value for all other kinds of options.
774 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
775 noa setlocal spelllang=spelllocal
776 let g:options=[['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
777 set spelllang=foo
778 call assert_equal([], g:options)
779 call assert_equal(g:opt[0], g:opt[1])
780
781
782 " 21a: Setting string local-global (to window) option"
783 let oldval = &statusline
784 let g:options=[['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
785 set statusline=foo
786 call assert_equal([], g:options)
787 call assert_equal(g:opt[0], g:opt[1])
788
789 " 21b: Resetting string local-global (to window) option"
790 " Note: v:option_old is the old global value for local-global string options
791 " but the old local value for all other kinds of options.
792 let g:options=[['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
793 set statusline&
794 call assert_equal([], g:options)
795 call assert_equal(g:opt[0], g:opt[1])
796
797 " 21c: Setting global string local-global (to window) option"
798 let g:options=[['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
799 setglobal statusline=bar
800 call assert_equal([], g:options)
801 call assert_equal(g:opt[0], g:opt[1])
802
803 " 21d: Setting local string local-global (to window) option"
804 noa set statusline& " Reset global and local value (without triggering autocmd)
805 let g:options=[['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
806 setlocal statusline=baz
807 call assert_equal([], g:options)
808 call assert_equal(g:opt[0], g:opt[1])
809
810 " 21e: Setting again string local-global (to window) option"
811 " Note: v:option_old is the old global value for local-global string options
812 " but the old local value for all other kinds of options.
813 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
814 noa setlocal statusline=baz
815 let g:options=[['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
816 set statusline=foo
817 call assert_equal([], g:options)
818 call assert_equal(g:opt[0], g:opt[1])
819
820
821 " 22a: Setting string local (to window) option"
822 let oldval = &foldignore
823 let g:options=[['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
824 set foldignore=fo
825 call assert_equal([], g:options)
826 call assert_equal(g:opt[0], g:opt[1])
827
828 " 22b: Resetting string local (to window) option"
829 let g:options=[['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
830 set foldignore&
831 call assert_equal([], g:options)
832 call assert_equal(g:opt[0], g:opt[1])
833
834 " 22c: Setting global string local (to window) option"
835 let g:options=[['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
836 setglobal foldignore=bar
837 call assert_equal([], g:options)
838 call assert_equal(g:opt[0], g:opt[1])
839
840 " 22d: Setting local string local (to window) option"
841 noa set foldignore& " Reset global and local value (without triggering autocmd)
842 let g:options=[['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
843 setlocal foldignore=baz
844 call assert_equal([], g:options)
845 call assert_equal(g:opt[0], g:opt[1])
846
847 " 22e: Setting again string local (to window) option"
848 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
849 noa setlocal foldignore=loc
850 let g:options=[['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
851 set foldignore=fo
852 call assert_equal([], g:options)
853 call assert_equal(g:opt[0], g:opt[1])
854
855
856 " 23a: Setting global number local option"
857 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
858 noa setlocal cmdheight=1 " Sets the global(!) value!
859 let g:options=[['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
860 setglobal cmdheight=2
861 call assert_equal([], g:options)
862 call assert_equal(g:opt[0], g:opt[1])
863
864 " 23b: Setting local number global option"
865 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
866 noa setlocal cmdheight=1 " Sets the global(!) value!
867 let g:options=[['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
868 setlocal cmdheight=2
869 call assert_equal([], g:options)
870 call assert_equal(g:opt[0], g:opt[1])
871
872 " 23c: Setting again number global option"
873 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
874 noa setlocal cmdheight=1 " Sets the global(!) value!
875 let g:options=[['cmdheight', '1', '1', '1', '2', 'global', 'set']]
876 set cmdheight=2
877 call assert_equal([], g:options)
878 call assert_equal(g:opt[0], g:opt[1])
879
880 " 23d: Setting again number global option"
881 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
882 let g:options=[['cmdheight', '8', '8', '8', '2', 'global', 'set']]
883 set cmdheight=2
884 call assert_equal([], g:options)
885 call assert_equal(g:opt[0], g:opt[1])
886
887
888 " 24a: Setting global number global-local (to buffer) option"
889 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
890 noa setlocal undolevels=1
891 let g:options=[['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
892 setglobal undolevels=2
893 call assert_equal([], g:options)
894 call assert_equal(g:opt[0], g:opt[1])
895
896 " 24b: Setting local number global-local (to buffer) option"
897 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
898 noa setlocal undolevels=1
899 let g:options=[['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
900 setlocal undolevels=2
901 call assert_equal([], g:options)
902 call assert_equal(g:opt[0], g:opt[1])
903
904 " 24c: Setting again number global-local (to buffer) option"
905 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
906 noa setlocal undolevels=1
907 let g:options=[['undolevels', '1', '1', '8', '2', 'global', 'set']]
908 set undolevels=2
909 call assert_equal([], g:options)
910 call assert_equal(g:opt[0], g:opt[1])
911
912 " 24d: Setting again global number global-local (to buffer) option"
913 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
914 let g:options=[['undolevels', '8', '8', '8', '2', 'global', 'set']]
915 set undolevels=2
916 call assert_equal([], g:options)
917 call assert_equal(g:opt[0], g:opt[1])
918
919
920 " 25a: Setting global number local (to buffer) option"
921 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
922 noa setlocal wrapmargin=1
923 let g:options=[['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
924 setglobal wrapmargin=2
925 call assert_equal([], g:options)
926 call assert_equal(g:opt[0], g:opt[1])
927
928 " 25b: Setting local number local (to buffer) option"
929 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
930 noa setlocal wrapmargin=1
931 let g:options=[['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
932 setlocal wrapmargin=2
933 call assert_equal([], g:options)
934 call assert_equal(g:opt[0], g:opt[1])
935
936 " 25c: Setting again number local (to buffer) option"
937 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
938 noa setlocal wrapmargin=1
939 let g:options=[['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
940 set wrapmargin=2
941 call assert_equal([], g:options)
942 call assert_equal(g:opt[0], g:opt[1])
943
944 " 25d: Setting again global number local (to buffer) option"
945 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
946 let g:options=[['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
947 set wrapmargin=2
948 call assert_equal([], g:options)
949 call assert_equal(g:opt[0], g:opt[1])
950
951
952 " 26: Setting number global-local (to window) option.
953 " Such option does currently not exist.
954
955
956 " 27a: Setting global number local (to window) option"
957 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
958 noa setlocal foldcolumn=1
959 let g:options=[['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
960 setglobal foldcolumn=2
961 call assert_equal([], g:options)
962 call assert_equal(g:opt[0], g:opt[1])
963
964 " 27b: Setting local number local (to window) option"
965 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
966 noa setlocal foldcolumn=1
967 let g:options=[['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
968 setlocal foldcolumn=2
969 call assert_equal([], g:options)
970 call assert_equal(g:opt[0], g:opt[1])
971
972 " 27c: Setting again number local (to window) option"
973 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
974 noa setlocal foldcolumn=1
975 let g:options=[['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
976 set foldcolumn=2
977 call assert_equal([], g:options)
978 call assert_equal(g:opt[0], g:opt[1])
979
980 " 27d: Ssettin again global number local (to window) option"
981 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
982 let g:options=[['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
983 set foldcolumn=2
984 call assert_equal([], g:options)
985 call assert_equal(g:opt[0], g:opt[1])
986
987
988 " 28a: Setting global boolean global option"
989 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
990 noa setlocal wrapscan " Sets the global(!) value!
991 let g:options=[['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
992 setglobal nowrapscan
993 call assert_equal([], g:options)
994 call assert_equal(g:opt[0], g:opt[1])
995
996 " 28b: Setting local boolean global option"
997 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
998 noa setlocal wrapscan " Sets the global(!) value!
999 let g:options=[['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
1000 setlocal nowrapscan
1001 call assert_equal([], g:options)
1002 call assert_equal(g:opt[0], g:opt[1])
1003
1004 " 28c: Setting again boolean global option"
1005 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1006 noa setlocal wrapscan " Sets the global(!) value!
1007 let g:options=[['wrapscan', '1', '1', '1', '0', 'global', 'set']]
1008 set nowrapscan
1009 call assert_equal([], g:options)
1010 call assert_equal(g:opt[0], g:opt[1])
1011
1012 " 28d: Setting again global boolean global option"
1013 noa set nowrapscan " Reset global and local value (without triggering autocmd)
1014 let g:options=[['wrapscan', '0', '0', '0', '1', 'global', 'set']]
1015 set wrapscan
1016 call assert_equal([], g:options)
1017 call assert_equal(g:opt[0], g:opt[1])
1018
1019
1020 " 29a: Setting global boolean global-local (to buffer) option"
1021 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1022 noa setlocal autoread
1023 let g:options=[['autoread', '0', '', '0', '1', 'global', 'setglobal']]
1024 setglobal autoread
1025 call assert_equal([], g:options)
1026 call assert_equal(g:opt[0], g:opt[1])
1027
1028 " 29b: Setting local boolean global-local (to buffer) option"
1029 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1030 noa setlocal autoread
1031 let g:options=[['autoread', '1', '1', '', '0', 'local', 'setlocal']]
1032 setlocal noautoread
1033 call assert_equal([], g:options)
1034 call assert_equal(g:opt[0], g:opt[1])
1035
1036 " 29c: Setting again boolean global-local (to buffer) option"
1037 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1038 noa setlocal autoread
1039 let g:options=[['autoread', '1', '1', '0', '1', 'global', 'set']]
1040 set autoread
1041 call assert_equal([], g:options)
1042 call assert_equal(g:opt[0], g:opt[1])
1043
1044 " 29d: Setting again global boolean global-local (to buffer) option"
1045 noa set noautoread " Reset global and local value (without triggering autocmd)
1046 let g:options=[['autoread', '0', '0', '0', '1', 'global', 'set']]
1047 set autoread
1048 call assert_equal([], g:options)
1049 call assert_equal(g:opt[0], g:opt[1])
1050
1051
1052 " 30a: Setting global boolean local (to buffer) option"
1053 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1054 noa setlocal cindent
1055 let g:options=[['cindent', '0', '', '0', '1', 'global', 'setglobal']]
1056 setglobal cindent
1057 call assert_equal([], g:options)
1058 call assert_equal(g:opt[0], g:opt[1])
1059
1060 " 30b: Setting local boolean local (to buffer) option"
1061 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1062 noa setlocal cindent
1063 let g:options=[['cindent', '1', '1', '', '0', 'local', 'setlocal']]
1064 setlocal nocindent
1065 call assert_equal([], g:options)
1066 call assert_equal(g:opt[0], g:opt[1])
1067
1068 " 30c: Setting again boolean local (to buffer) option"
1069 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1070 noa setlocal cindent
1071 let g:options=[['cindent', '1', '1', '0', '1', 'global', 'set']]
1072 set cindent
1073 call assert_equal([], g:options)
1074 call assert_equal(g:opt[0], g:opt[1])
1075
1076 " 30d: Setting again global boolean local (to buffer) option"
1077 noa set nocindent " Reset global and local value (without triggering autocmd)
1078 let g:options=[['cindent', '0', '0', '0', '1', 'global', 'set']]
1079 set cindent
1080 call assert_equal([], g:options)
1081 call assert_equal(g:opt[0], g:opt[1])
1082
1083
1084 " 31: Setting boolean global-local (to window) option
1085 " Currently no such option exists.
1086
1087
1088 " 32a: Setting global boolean local (to window) option"
1089 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1090 noa setlocal cursorcolumn
1091 let g:options=[['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
1092 setglobal cursorcolumn
1093 call assert_equal([], g:options)
1094 call assert_equal(g:opt[0], g:opt[1])
1095
1096 " 32b: Setting local boolean local (to window) option"
1097 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1098 noa setlocal cursorcolumn
1099 let g:options=[['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
1100 setlocal nocursorcolumn
1101 call assert_equal([], g:options)
1102 call assert_equal(g:opt[0], g:opt[1])
1103
1104 " 32c: Setting again boolean local (to window) option"
1105 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1106 noa setlocal cursorcolumn
1107 let g:options=[['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
1108 set cursorcolumn
1109 call assert_equal([], g:options)
1110 call assert_equal(g:opt[0], g:opt[1])
1111
1112 " 32d: Setting again global boolean local (to window) option"
1113 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
1114 let g:options=[['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
1115 set cursorcolumn
1116 call assert_equal([], g:options)
1117 call assert_equal(g:opt[0], g:opt[1])
1118
1119
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001120 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001121 noa set backspace=1 " Reset global and local value (without triggering autocmd)
1122 let g:options=[['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
1123 set backspace=2
1124 call assert_equal([], g:options)
1125 call assert_equal(g:opt[0], g:opt[1])
1126
1127
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001128 " Cleanup
1129 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001130 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001131 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 +02001132 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001133 endfor
1134 call test_override('starting', 0)
1135 delfunc! AutoCommandOptionSet
1136endfunc
1137
1138func Test_OptionSet_diffmode()
1139 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001140 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001141 new
1142 au OptionSet diff :let &l:cul=v:option_new
1143
1144 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1145 call assert_equal(0, &l:cul)
1146 diffthis
1147 call assert_equal(1, &l:cul)
1148
1149 vnew
1150 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1151 call assert_equal(0, &l:cul)
1152 diffthis
1153 call assert_equal(1, &l:cul)
1154
1155 diffoff
1156 call assert_equal(0, &l:cul)
1157 call assert_equal(1, getwinvar(2, '&l:cul'))
1158 bw!
1159
1160 call assert_equal(1, &l:cul)
1161 diffoff!
1162 call assert_equal(0, &l:cul)
1163 call assert_equal(0, getwinvar(1, '&l:cul'))
1164 bw!
1165
1166 " Cleanup
1167 au! OptionSet
1168 call test_override('starting', 0)
1169endfunc
1170
1171func Test_OptionSet_diffmode_close()
1172 call test_override('starting', 1)
1173 " 19: Try to close the current window when entering diff mode
1174 " should not segfault
1175 new
1176 au OptionSet diff close
1177
1178 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001179 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001180 call assert_equal(1, &diff)
1181 vnew
1182 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001183 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001184 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001185 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001186 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001187 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001188 bw!
1189
1190 " Cleanup
1191 au! OptionSet
1192 call test_override('starting', 0)
1193 "delfunc! AutoCommandOptionSet
1194endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001195
1196" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1197func Test_BufleaveWithDelete()
1198 new | edit Xfile1
1199
1200 augroup test_bufleavewithdelete
1201 autocmd!
1202 autocmd BufLeave Xfile1 bwipe Xfile2
1203 augroup END
1204
1205 call assert_fails('edit Xfile2', 'E143:')
1206 call assert_equal('Xfile1', bufname('%'))
1207
1208 autocmd! test_bufleavewithdelete BufLeave Xfile1
1209 augroup! test_bufleavewithdelete
1210
1211 new
1212 bwipe! Xfile1
1213endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001214
1215" Test for autocommand that changes the buffer list, when doing ":ball".
1216func Test_Acmd_BufAll()
1217 enew!
1218 %bwipe!
1219 call writefile(['Test file Xxx1'], 'Xxx1')
1220 call writefile(['Test file Xxx2'], 'Xxx2')
1221 call writefile(['Test file Xxx3'], 'Xxx3')
1222
1223 " Add three files to the buffer list
1224 split Xxx1
1225 close
1226 split Xxx2
1227 close
1228 split Xxx3
1229 close
1230
1231 " Wipe the buffer when the buffer is opened
1232 au BufReadPost Xxx2 bwipe
1233
1234 call append(0, 'Test file Xxx4')
1235 ball
1236
1237 call assert_equal(2, winnr('$'))
1238 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1239 wincmd t
1240
1241 au! BufReadPost
1242 %bwipe!
1243 call delete('Xxx1')
1244 call delete('Xxx2')
1245 call delete('Xxx3')
1246 enew! | only
1247endfunc
1248
1249" Test for autocommand that changes current buffer on BufEnter event.
1250" Check if modelines are interpreted for the correct buffer.
1251func Test_Acmd_BufEnter()
1252 %bwipe!
1253 call writefile(['start of test file Xxx1',
1254 \ "\<Tab>this is a test",
1255 \ 'end of test file Xxx1'], 'Xxx1')
1256 call writefile(['start of test file Xxx2',
1257 \ 'vim: set noai :',
1258 \ "\<Tab>this is a test",
1259 \ 'end of test file Xxx2'], 'Xxx2')
1260
1261 au BufEnter Xxx2 brew
1262 set ai modeline modelines=3
1263 edit Xxx1
1264 " edit Xxx2, autocmd will do :brew
1265 edit Xxx2
1266 exe "normal G?this is a\<CR>"
1267 " Append text with autoindent to this file
1268 normal othis should be auto-indented
1269 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1270 call assert_equal(3, line('.'))
1271 " Remove autocmd and edit Xxx2 again
1272 au! BufEnter Xxx2
1273 buf! Xxx2
1274 exe "normal G?this is a\<CR>"
1275 " append text without autoindent to Xxx
1276 normal othis should be in column 1
1277 call assert_equal("this should be in column 1", getline('.'))
1278 call assert_equal(4, line('.'))
1279
1280 %bwipe!
1281 call delete('Xxx1')
1282 call delete('Xxx2')
1283 set ai&vim modeline&vim modelines&vim
1284endfunc
1285
1286" Test for issue #57
1287" do not move cursor on <c-o> when autoindent is set
1288func Test_ai_CTRL_O()
1289 enew!
1290 set ai
1291 let save_fo = &fo
1292 set fo+=r
1293 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1294 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1295 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1296
1297 set ai&vim
1298 let &fo = save_fo
1299 enew!
1300endfunc
1301
1302" Test for autocommand that deletes the current buffer on BufLeave event.
1303" Also test deleting the last buffer, should give a new, empty buffer.
1304func Test_BufLeave_Wipe()
1305 %bwipe!
1306 let content = ['start of test file Xxx',
1307 \ 'this is a test',
1308 \ 'end of test file Xxx']
1309 call writefile(content, 'Xxx1')
1310 call writefile(content, 'Xxx2')
1311
1312 au BufLeave Xxx2 bwipe
1313 edit Xxx1
1314 split Xxx2
1315 " delete buffer Xxx2, we should be back to Xxx1
1316 bwipe
1317 call assert_equal('Xxx1', bufname('%'))
1318 call assert_equal(1, winnr('$'))
1319
1320 " Create an alternate buffer
1321 %write! test.out
1322 call assert_equal('test.out', bufname('#'))
1323 " delete alternate buffer
1324 bwipe test.out
1325 call assert_equal('Xxx1', bufname('%'))
1326 call assert_equal('', bufname('#'))
1327
1328 au BufLeave Xxx1 bwipe
1329 " delete current buffer, get an empty one
1330 bwipe!
1331 call assert_equal(1, line('$'))
1332 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001333 let g:bufinfo = getbufinfo()
1334 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001335
1336 call delete('Xxx1')
1337 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001338 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001339 %bwipe
1340 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001341
1342 " check that bufinfo doesn't contain a pointer to freed memory
1343 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001344endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001345
1346func Test_QuitPre()
1347 edit Xfoo
1348 let winid = win_getid(winnr())
1349 split Xbar
1350 au! QuitPre * let g:afile = expand('<afile>')
1351 " Close the other window, <afile> should be correct.
1352 exe win_id2win(winid) . 'q'
1353 call assert_equal('Xfoo', g:afile)
1354
1355 unlet g:afile
1356 bwipe Xfoo
1357 bwipe Xbar
1358endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001359
1360func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001361 au! CmdlineChanged : let g:text = getcmdline()
1362 let g:text = 0
1363 call feedkeys(":echom 'hello'\<CR>", 'xt')
1364 call assert_equal("echom 'hello'", g:text)
1365 au! CmdlineChanged
1366
1367 au! CmdlineChanged : let g:entered = expand('<afile>')
1368 let g:entered = 0
1369 call feedkeys(":echom 'hello'\<CR>", 'xt')
1370 call assert_equal(':', g:entered)
1371 au! CmdlineChanged
1372
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001373 au! CmdlineEnter : let g:entered = expand('<afile>')
1374 au! CmdlineLeave : let g:left = expand('<afile>')
1375 let g:entered = 0
1376 let g:left = 0
1377 call feedkeys(":echo 'hello'\<CR>", 'xt')
1378 call assert_equal(':', g:entered)
1379 call assert_equal(':', g:left)
1380 au! CmdlineEnter
1381 au! CmdlineLeave
1382
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001383 let save_shellslash = &shellslash
1384 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001385 au! CmdlineEnter / let g:entered = expand('<afile>')
1386 au! CmdlineLeave / let g:left = expand('<afile>')
1387 let g:entered = 0
1388 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001389 new
1390 call setline(1, 'hello')
1391 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001392 call assert_equal('/', g:entered)
1393 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001394 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001395 au! CmdlineEnter
1396 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001397 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001398endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001399
1400" Test for BufWritePre autocommand that deletes or unloads the buffer.
1401func Test_BufWritePre()
1402 %bwipe
1403 au BufWritePre Xxx1 bunload
1404 au BufWritePre Xxx2 bwipe
1405
1406 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1407 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1408
1409 edit Xtest
1410 e! Xxx2
1411 bdel Xtest
1412 e Xxx1
1413 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001414 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001415 call assert_equal('Xxx2', bufname('%'))
1416 edit Xtest
1417 e! Xxx2
1418 bwipe Xtest
1419 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001420 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001421 call assert_equal('Xxx1', bufname('%'))
1422 au! BufWritePre
1423 call delete('Xxx1')
1424 call delete('Xxx2')
1425endfunc
1426
1427" Test for BufUnload autocommand that unloads all the other buffers
1428func Test_bufunload_all()
1429 call writefile(['Test file Xxx1'], 'Xxx1')"
1430 call writefile(['Test file Xxx2'], 'Xxx2')"
1431
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001432 let content =<< trim [CODE]
1433 func UnloadAllBufs()
1434 let i = 1
1435 while i <= bufnr('$')
1436 if i != bufnr('%') && bufloaded(i)
1437 exe i . 'bunload'
1438 endif
1439 let i += 1
1440 endwhile
1441 endfunc
1442 au BufUnload * call UnloadAllBufs()
1443 au VimLeave * call writefile(['Test Finished'], 'Xout')
1444 edit Xxx1
1445 split Xxx2
1446 q
1447 [CODE]
1448
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001449 call writefile(content, 'Xtest')
1450
1451 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001452 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001453 call assert_true(filereadable('Xout'))
1454
1455 call delete('Xxx1')
1456 call delete('Xxx2')
1457 call delete('Xtest')
1458 call delete('Xout')
1459endfunc
1460
1461" Some tests for buffer-local autocommands
1462func Test_buflocal_autocmd()
1463 let g:bname = ''
1464 edit xx
1465 au BufLeave <buffer> let g:bname = expand("%")
1466 " here, autocommand for xx should trigger.
1467 " but autocommand shall not apply to buffer named <buffer>.
1468 edit somefile
1469 call assert_equal('xx', g:bname)
1470 let g:bname = ''
1471 " here, autocommand shall be auto-deleted
1472 bwipe xx
1473 " autocmd should not trigger
1474 edit xx
1475 call assert_equal('', g:bname)
1476 " autocmd should not trigger
1477 edit somefile
1478 call assert_equal('', g:bname)
1479 enew
1480 unlet g:bname
1481endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001482
1483" Test for "*Cmd" autocommands
1484func Test_Cmd_Autocmds()
1485 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1486
1487 enew!
1488 au BufReadCmd XtestA 0r Xxx|$del
1489 edit XtestA " will read text of Xxd instead
1490 call assert_equal('start of Xxx', getline(1))
1491
1492 au BufWriteCmd XtestA call append(line("$"), "write")
1493 write " will append a line to the file
1494 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001495 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001496 call assert_equal('write', getline(4))
1497
1498 " now we have:
1499 " 1 start of Xxx
1500 " 2 abc2
1501 " 3 end of Xxx
1502 " 4 write
1503
1504 au FileReadCmd XtestB '[r Xxx
1505 2r XtestB " will read Xxx below line 2 instead
1506 call assert_equal('start of Xxx', getline(3))
1507
1508 " now we have:
1509 " 1 start of Xxx
1510 " 2 abc2
1511 " 3 start of Xxx
1512 " 4 abc2
1513 " 5 end of Xxx
1514 " 6 end of Xxx
1515 " 7 write
1516
1517 au FileWriteCmd XtestC '[,']copy $
1518 normal 4GA1
1519 4,5w XtestC " will copy lines 4 and 5 to the end
1520 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001521 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001522 call assert_equal("end of Xxx", getline(9))
1523
1524 " now we have:
1525 " 1 start of Xxx
1526 " 2 abc2
1527 " 3 start of Xxx
1528 " 4 abc21
1529 " 5 end of Xxx
1530 " 6 end of Xxx
1531 " 7 write
1532 " 8 abc21
1533 " 9 end of Xxx
1534
1535 let g:lines = []
1536 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1537 w >>XtestD " will add lines to 'lines'
1538 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001539 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001540 call assert_equal(9, line('$'))
1541 call assert_equal('end of Xxx', getline('$'))
1542
1543 au BufReadCmd XtestE 0r Xxx|$del
1544 sp XtestE " split window with test.out
1545 call assert_equal('end of Xxx', getline(3))
1546
1547 let g:lines = []
1548 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1549 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1550 wall " will write other window to 'lines'
1551 call assert_equal(4, len(g:lines), g:lines)
1552 call assert_equal('asdf', g:lines[2])
1553
1554 au! BufReadCmd
1555 au! BufWriteCmd
1556 au! FileReadCmd
1557 au! FileWriteCmd
1558 au! FileAppendCmd
1559 %bwipe!
1560 call delete('Xxx')
1561 enew!
1562endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001563
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001564func s:ReadFile()
1565 setl noswapfile nomodified
1566 let filename = resolve(expand("<afile>:p"))
1567 execute 'read' fnameescape(filename)
1568 1d_
1569 exe 'file' fnameescape(filename)
1570 setl buftype=acwrite
1571endfunc
1572
1573func s:WriteFile()
1574 let filename = resolve(expand("<afile>:p"))
1575 setl buftype=
1576 noautocmd execute 'write' fnameescape(filename)
1577 setl buftype=acwrite
1578 setl nomodified
1579endfunc
1580
1581func Test_BufReadCmd()
1582 autocmd BufReadCmd *.test call s:ReadFile()
1583 autocmd BufWriteCmd *.test call s:WriteFile()
1584
1585 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1586 edit Xcmd.test
1587 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1588 normal! Gofour
1589 write
1590 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1591
1592 bwipe!
1593 call delete('Xcmd.test')
1594 au! BufReadCmd
1595 au! BufWriteCmd
1596endfunc
1597
Bram Moolenaaraace2152017-11-05 16:23:10 +01001598func SetChangeMarks(start, end)
1599 exe a:start. 'mark ['
1600 exe a:end. 'mark ]'
1601endfunc
1602
1603" Verify the effects of autocmds on '[ and ']
1604func Test_change_mark_in_autocmds()
1605 edit! Xtest
1606 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u", 'xtn')
1607
1608 call SetChangeMarks(2, 3)
1609 write
1610 call assert_equal([1, 4], [line("'["), line("']")])
1611
1612 call SetChangeMarks(2, 3)
1613 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1614 write
1615 au! BufWritePre
1616
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001617 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001618 write XtestFilter
1619 write >> XtestFilter
1620
1621 call SetChangeMarks(2, 3)
1622 " Marks are set to the entire range of the write
1623 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1624 " '[ is adjusted to just before the line that will receive the filtered
1625 " data
1626 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1627 " The filtered data is read into the buffer, and the source lines are
1628 " still present, so the range is after the source lines
1629 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1630 %!cat XtestFilter
1631 " After the filtered data is read, the original lines are deleted
1632 call assert_equal([1, 8], [line("'["), line("']")])
1633 au! FilterWritePre,FilterReadPre,FilterReadPost
1634 undo
1635
1636 call SetChangeMarks(1, 4)
1637 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1638 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1639 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1640 2,3!cat XtestFilter
1641 call assert_equal([2, 9], [line("'["), line("']")])
1642 au! FilterWritePre,FilterReadPre,FilterReadPost
1643 undo
1644
1645 call delete('XtestFilter')
1646 endif
1647
1648 call SetChangeMarks(1, 4)
1649 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1650 2,3write Xtest2
1651 au! FileWritePre
1652
1653 call SetChangeMarks(2, 3)
1654 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1655 write >> Xtest2
1656 au! FileAppendPre
1657
1658 call SetChangeMarks(1, 4)
1659 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1660 2,3write >> Xtest2
1661 au! FileAppendPre
1662
1663 call SetChangeMarks(1, 1)
1664 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1665 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1666 3read Xtest2
1667 au! FileReadPre,FileReadPost
1668 undo
1669
1670 call SetChangeMarks(4, 4)
1671 " When the line is 0, it's adjusted to 1
1672 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1673 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1674 0read Xtest2
1675 au! FileReadPre,FileReadPost
1676 undo
1677
1678 call SetChangeMarks(4, 4)
1679 " When the line is 0, it's adjusted to 1
1680 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1681 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1682 1read Xtest2
1683 au! FileReadPre,FileReadPost
1684 undo
1685
1686 bwipe!
1687 call delete('Xtest')
1688 call delete('Xtest2')
1689endfunc
1690
1691func Test_Filter_noshelltemp()
1692 if !executable('cat')
1693 return
1694 endif
1695
1696 enew!
1697 call setline(1, ['a', 'b', 'c', 'd'])
1698
1699 let shelltemp = &shelltemp
1700 set shelltemp
1701
1702 let g:filter_au = 0
1703 au FilterWritePre * let g:filter_au += 1
1704 au FilterReadPre * let g:filter_au += 1
1705 au FilterReadPost * let g:filter_au += 1
1706 %!cat
1707 call assert_equal(3, g:filter_au)
1708
1709 if has('filterpipe')
1710 set noshelltemp
1711
1712 let g:filter_au = 0
1713 au FilterWritePre * let g:filter_au += 1
1714 au FilterReadPre * let g:filter_au += 1
1715 au FilterReadPost * let g:filter_au += 1
1716 %!cat
1717 call assert_equal(0, g:filter_au)
1718 endif
1719
1720 au! FilterWritePre,FilterReadPre,FilterReadPost
1721 let &shelltemp = shelltemp
1722 bwipe!
1723endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001724
1725func Test_TextYankPost()
1726 enew!
1727 call setline(1, ['foo'])
1728
1729 let g:event = []
1730 au TextYankPost * let g:event = copy(v:event)
1731
1732 call assert_equal({}, v:event)
1733 call assert_fails('let v:event = {}', 'E46:')
1734 call assert_fails('let v:event.mykey = 0', 'E742:')
1735
1736 norm "ayiw
1737 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001738 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001739 \g:event)
1740 norm y_
1741 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001742 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false},
1743 \g:event)
1744 norm Vy
1745 call assert_equal(
1746 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001747 \g:event)
1748 call feedkeys("\<C-V>y", 'x')
1749 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001750 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001751 \g:event)
1752 norm "xciwbar
1753 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001754 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001755 \g:event)
1756 norm "bdiw
1757 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001758 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001759 \g:event)
1760
1761 call assert_equal({}, v:event)
1762
1763 au! TextYankPost
1764 unlet g:event
1765 bwipe!
1766endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001767
1768func Test_nocatch_wipe_all_buffers()
1769 " Real nasty autocommand: wipe all buffers on any event.
1770 au * * bwipe *
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001771 call assert_fails('next x', ['E94:', 'E937:'])
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001772 bwipe
1773 au!
1774endfunc
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001775
1776func Test_nocatch_wipe_dummy_buffer()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001777 CheckFeature quickfix
1778 " Nasty autocommand: wipe buffer on any event.
1779 au * x bwipe
Bram Moolenaare2e40752020-09-04 21:18:46 +02001780 call assert_fails('lv½ /x', 'E937:')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001781 au!
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001782endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001783
1784function s:Before_test_dirchanged()
1785 augroup test_dirchanged
1786 autocmd!
1787 augroup END
1788 let s:li = []
1789 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001790 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001791 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001792 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001793 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001794endfunc
1795
1796function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001797 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001798 call delete(s:dir_foo, 'd')
1799 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001800 augroup test_dirchanged
1801 autocmd!
1802 augroup END
1803endfunc
1804
1805function Test_dirchanged_global()
1806 call s:Before_test_dirchanged()
1807 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1808 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001809 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001810 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001811 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001812 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001813 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001814 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001815 call s:After_test_dirchanged()
1816endfunc
1817
1818function Test_dirchanged_local()
1819 call s:Before_test_dirchanged()
1820 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1821 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001822 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001823 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001824 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001825 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001826 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001827 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001828 call s:After_test_dirchanged()
1829endfunc
1830
1831function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001832 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001833 call s:Before_test_dirchanged()
1834 call test_autochdir()
1835 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1836 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1837 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001838 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001839 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001840 exe 'edit ' . s:dir_foo . '/Xfile'
1841 call assert_equal(s:dir_foo, getcwd())
1842 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001843 set noacd
1844 bwipe!
1845 call s:After_test_dirchanged()
1846endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001847
1848" Test TextChangedI and TextChangedP
1849func Test_ChangedP()
1850 new
1851 call setline(1, ['foo', 'bar', 'foobar'])
1852 call test_override("char_avail", 1)
1853 set complete=. completeopt=menuone
1854
1855 func! TextChangedAutocmd(char)
1856 let g:autocmd .= a:char
1857 endfunc
1858
1859 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1860 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1861 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1862
1863 call cursor(3, 1)
1864 let g:autocmd = ''
1865 call feedkeys("o\<esc>", 'tnix')
1866 call assert_equal('I', g:autocmd)
1867
1868 let g:autocmd = ''
1869 call feedkeys("Sf", 'tnix')
1870 call assert_equal('II', g:autocmd)
1871
1872 let g:autocmd = ''
1873 call feedkeys("Sf\<C-N>", 'tnix')
1874 call assert_equal('IIP', g:autocmd)
1875
1876 let g:autocmd = ''
1877 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1878 call assert_equal('IIPP', g:autocmd)
1879
1880 let g:autocmd = ''
1881 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1882 call assert_equal('IIPPP', g:autocmd)
1883
1884 let g:autocmd = ''
1885 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1886 call assert_equal('IIPPPP', g:autocmd)
1887
1888 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1889 " TODO: how should it handle completeopt=noinsert,noselect?
1890
1891 " CleanUp
1892 call test_override("char_avail", 0)
1893 au! TextChanged
1894 au! TextChangedI
1895 au! TextChangedP
1896 delfu TextChangedAutocmd
1897 unlet! g:autocmd
1898 set complete&vim completeopt&vim
1899
1900 bw!
1901endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001902
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001903let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001904func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001905 if !g:setline_handled
1906 call setline(1, "(x)")
1907 let g:setline_handled = v:true
1908 endif
1909endfunc
1910
1911func Test_TextChangedI_with_setline()
1912 new
1913 call test_override('char_avail', 1)
1914 autocmd TextChangedI <buffer> call SetLineOne()
1915 call feedkeys("i(\<CR>\<Esc>", 'tx')
1916 call assert_equal('(', getline(1))
1917 call assert_equal('x)', getline(2))
1918 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001919 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001920 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001921
1922 call test_override('starting', 0)
1923 bwipe!
1924endfunc
1925
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001926func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001927 CheckFeature terminal
1928 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01001929 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01001930 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001931
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001932 " Prepare file for TextChanged event.
1933 call writefile([''], 'Xchanged.txt')
1934 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1935 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001936 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001937 " In ConPTY, there is additional character which is drawn up to the width of
1938 " the screen.
1939 if has('conpty')
1940 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
1941 else
1942 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
1943 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001944 " It's only adding autocmd, so that no event occurs.
1945 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1946 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001947 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001948 call assert_equal([''], readfile('Xchanged.txt'))
1949
1950 " clean up
1951 call delete('Xchanged.txt')
1952 bwipe!
1953endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01001954
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001955func Test_autocmd_nested()
1956 let g:did_nested = 0
1957 augroup Testing
1958 au WinNew * edit somefile
1959 au BufNew * let g:did_nested = 1
1960 augroup END
1961 split
1962 call assert_equal(0, g:did_nested)
1963 close
1964 bwipe! somefile
1965
1966 " old nested argument still works
1967 augroup Testing
1968 au!
1969 au WinNew * nested edit somefile
1970 au BufNew * let g:did_nested = 1
1971 augroup END
1972 split
1973 call assert_equal(1, g:did_nested)
1974 close
1975 bwipe! somefile
1976
1977 " New ++nested argument works
1978 augroup Testing
1979 au!
1980 au WinNew * ++nested edit somefile
1981 au BufNew * let g:did_nested = 1
1982 augroup END
1983 split
1984 call assert_equal(1, g:did_nested)
1985 close
1986 bwipe! somefile
1987
1988 augroup Testing
1989 au!
1990 augroup END
1991
1992 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
1993 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
1994endfunc
1995
1996func Test_autocmd_once()
1997 " Without ++once WinNew triggers twice
1998 let g:did_split = 0
1999 augroup Testing
2000 au WinNew * let g:did_split += 1
2001 augroup END
2002 split
2003 split
2004 call assert_equal(2, g:did_split)
2005 call assert_true(exists('#WinNew'))
2006 close
2007 close
2008
2009 " With ++once WinNew triggers once
2010 let g:did_split = 0
2011 augroup Testing
2012 au!
2013 au WinNew * ++once let g:did_split += 1
2014 augroup END
2015 split
2016 split
2017 call assert_equal(1, g:did_split)
2018 call assert_false(exists('#WinNew'))
2019 close
2020 close
2021
2022 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2023endfunc
2024
Bram Moolenaara68e5952019-04-25 22:22:01 +02002025func Test_autocmd_bufreadpre()
2026 new
2027 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002028 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002029 w! XAutocmdBufReadPre.txt
2030 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002031 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002032 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002033 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002034 wincmd p
2035 let g:wsv1 = winsaveview()
2036 wincmd p
2037 let g:wsv2 = winsaveview()
2038 " triggers BufReadPre, should not move the cursor in either window
2039 " The topline may change one line in a large window.
2040 edit
2041 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2042 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2043 call assert_equal(2, b:bufreadpre)
2044 wincmd p
2045 call assert_equal(g:wsv1.topline, winsaveview().topline)
2046 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2047 call assert_equal(2, b:bufreadpre)
2048 " Now set the cursor position in an BufReadPre autocommand
2049 " (even though the position will be invalid, this should make Vim reset the
2050 " cursor position in the other window.
2051 wincmd p
2052 set cpo+=g
2053 " won't do anything, but try to set the cursor on an invalid lnum
2054 autocmd BufReadPre <buffer> :norm! 70gg
2055 " triggers BufReadPre, should not move the cursor in either window
2056 e
2057 call assert_equal(1, winsaveview().topline)
2058 call assert_equal(1, winsaveview().lnum)
2059 call assert_equal(3, b:bufreadpre)
2060 wincmd p
2061 call assert_equal(g:wsv1.topline, winsaveview().topline)
2062 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2063 call assert_equal(3, b:bufreadpre)
2064 close
2065 close
2066 call delete('XAutocmdBufReadPre.txt')
2067 set cpo-=g
2068endfunc
2069
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002070" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002071
2072" Tests for the following autocommands:
2073" - FileWritePre writing a compressed file
2074" - FileReadPost reading a compressed file
2075" - BufNewFile reading a file template
2076" - BufReadPre decompressing the file to be read
2077" - FilterReadPre substituting characters in the temp file
2078" - FilterReadPost substituting characters after filtering
2079" - FileReadPre set options for decompression
2080" - FileReadPost decompress the file
2081func Test_ReadWrite_Autocmds()
2082 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002083 CheckUnix
2084 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002085
2086 " Make $GZIP empty, "-v" would cause trouble.
2087 let $GZIP = ""
2088
2089 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2090 " being modified outside of Vim (noticed on Solaris).
2091 au FileChangedShell * echo 'caught FileChangedShell'
2092
2093 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2094 augroup Test1
2095 au!
2096 au FileWritePre *.gz '[,']!gzip
2097 au FileWritePost *.gz undo
2098 au FileReadPost *.gz '[,']!gzip -d
2099 augroup END
2100
2101 new
2102 set bin
2103 call append(0, [
2104 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2105 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2106 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2107 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2108 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2109 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2110 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2111 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2112 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2113 \ ])
2114 1,9write! Xtestfile.gz
2115 enew! | close
2116
2117 new
2118 " Read and decompress the testfile
2119 0read Xtestfile.gz
2120 call assert_equal([
2121 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2122 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2123 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2124 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2125 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2126 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2127 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2128 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2129 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2130 \ ], getline(1, 9))
2131 enew! | close
2132
2133 augroup Test1
2134 au!
2135 augroup END
2136
2137 " Test for the FileAppendPre and FileAppendPost autocmds
2138 augroup Test2
2139 au!
2140 au BufNewFile *.c read Xtest.c
2141 au FileAppendPre *.out '[,']s/new/NEW/
2142 au FileAppendPost *.out !cat Xtest.c >> test.out
2143 augroup END
2144
2145 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2146 new foo.c " should load Xtest.c
2147 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2148 w! >> test.out " append it to the output file
2149
2150 let contents = readfile('test.out')
2151 call assert_equal(' * Here is a NEW .c file', contents[2])
2152 call assert_equal(' * Here is a new .c file', contents[5])
2153
2154 call delete('test.out')
2155 enew! | close
2156 augroup Test2
2157 au!
2158 augroup END
2159
2160 " Test for the BufReadPre and BufReadPost autocmds
2161 augroup Test3
2162 au!
2163 " setup autocommands to decompress before reading and re-compress
2164 " afterwards
2165 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2166 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2167 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2168 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2169 augroup END
2170
2171 e! Xtestfile.gz " Edit compressed file
2172 call assert_equal([
2173 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2174 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2175 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2176 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2177 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2178 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2179 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2180 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2181 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2182 \ ], getline(1, 9))
2183
2184 w! >> test.out " Append it to the output file
2185
2186 augroup Test3
2187 au!
2188 augroup END
2189
2190 " Test for the FilterReadPre and FilterReadPost autocmds.
2191 set shelltemp " need temp files here
2192 augroup Test4
2193 au!
2194 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2195 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2196 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2197 au FilterReadPost *.out '[,']s/x/X/g
2198 augroup END
2199
2200 e! test.out " Edit the output file
2201 1,$!cat
2202 call assert_equal([
2203 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2204 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2205 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2206 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2207 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2208 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2209 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2210 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2211 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2212 \ ], getline(1, 9))
2213 call assert_equal([
2214 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2215 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2216 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2217 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2218 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2219 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2220 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2221 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2222 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2223 \ ], readfile('test.out'))
2224
2225 augroup Test4
2226 au!
2227 augroup END
2228 set shelltemp&vim
2229
2230 " Test for the FileReadPre and FileReadPost autocmds.
2231 augroup Test5
2232 au!
2233 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2234 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2235 au FileReadPost *.gz '[,']s/l/L/
2236 augroup END
2237
2238 new
2239 0r Xtestfile.gz " Read compressed file
2240 call assert_equal([
2241 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2242 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2243 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2244 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2245 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2246 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2247 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2248 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2249 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2250 \ ], getline(1, 9))
2251 call assert_equal([
2252 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2253 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2254 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2255 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2256 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2257 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2258 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2259 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2260 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2261 \ ], readfile('Xtestfile.gz'))
2262
2263 augroup Test5
2264 au!
2265 augroup END
2266
2267 au! FileChangedShell
2268 call delete('Xtestfile.gz')
2269 call delete('Xtest.c')
2270 call delete('test.out')
2271endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002272
2273func Test_throw_in_BufWritePre()
2274 new
2275 call setline(1, ['one', 'two', 'three'])
2276 call assert_false(filereadable('Xthefile'))
2277 augroup throwing
2278 au BufWritePre X* throw 'do not write'
2279 augroup END
2280 try
2281 w Xthefile
2282 catch
2283 let caught = 1
2284 endtry
2285 call assert_equal(1, caught)
2286 call assert_false(filereadable('Xthefile'))
2287
2288 bwipe!
2289 au! throwing
2290endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002291
2292func Test_autocmd_SafeState()
2293 CheckRunVimInTerminal
2294
2295 let lines =<< trim END
2296 let g:safe = 0
2297 let g:again = ''
2298 au SafeState * let g:safe += 1
2299 au SafeStateAgain * let g:again ..= 'x'
2300 func CallTimer()
2301 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2302 endfunc
2303 END
2304 call writefile(lines, 'XSafeState')
2305 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2306
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002307 " Sometimes we loop to handle a K_IGNORE, SafeState may be trigered once or
2308 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002309 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002310 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002311
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002312 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002313 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002314 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002315
2316 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002317 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002318 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002319 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002320 call term_sendkeys(buf, ":echo g:again\<CR>")
2321 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2322
2323 call StopVimInTerminal(buf)
2324 call delete('XSafeState')
2325endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002326
2327func Test_autocmd_CmdWinEnter()
2328 CheckRunVimInTerminal
2329 " There is not cmdwin switch, so
2330 " test for cmdline_hist
2331 " (both are available with small builds)
2332 CheckFeature cmdline_hist
2333 let lines =<< trim END
2334 let b:dummy_var = 'This is a dummy'
2335 autocmd CmdWinEnter * quit
2336 let winnr = winnr('$')
2337 END
2338 let filename='XCmdWinEnter'
2339 call writefile(lines, filename)
2340 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2341
2342 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002343 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002344 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002345 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002346 call term_sendkeys(buf, ":echo &buftype\<cr>")
2347 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2348 call term_sendkeys(buf, ":echo winnr\<cr>")
2349 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2350
2351 " clean up
2352 call StopVimInTerminal(buf)
2353 call delete(filename)
2354endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002355
2356func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002357 CheckFeature quickfix
2358
Bram Moolenaarec66c412019-10-11 21:19:13 +02002359 pedit xx
2360 n x
2361 au WinEnter * quit
2362 split
2363 au! WinEnter
2364endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002365
2366func Test_BufWrite_lockmarks()
2367 edit! Xtest
2368 call setline(1, ['a', 'b', 'c', 'd'])
2369
2370 " :lockmarks preserves the marks
2371 call SetChangeMarks(2, 3)
2372 lockmarks write
2373 call assert_equal([2, 3], [line("'["), line("']")])
2374
2375 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2376 " original values for the user
2377 augroup lockmarks
2378 au!
2379 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2380 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2381 augroup END
2382
2383 lockmarks write
2384 call assert_equal([2, 3], [line("'["), line("']")])
2385
2386 if executable('cat')
2387 lockmarks %!cat
2388 call assert_equal([2, 3], [line("'["), line("']")])
2389 endif
2390
2391 lockmarks 3,4write Xtest2
2392 call assert_equal([2, 3], [line("'["), line("']")])
2393
2394 au! lockmarks
2395 augroup! lockmarks
2396 call delete('Xtest')
2397 call delete('Xtest2')
2398endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002399
2400func Test_FileType_spell()
2401 if !isdirectory('/tmp')
2402 throw "Skipped: requires /tmp directory"
2403 endif
2404
2405 " this was crashing with an invalid free()
2406 setglobal spellfile=/tmp/en.utf-8.add
2407 augroup crash
2408 autocmd!
2409 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2410 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2411 autocmd FileType anotherfiletype setlocal spell
2412 augroup END
2413 func! NoCrash() abort
2414 edit /tmp/crashfile
2415 endfunc
2416 call NoCrash()
2417
2418 au! crash
2419 setglobal spellfile=
2420endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002421
Bram Moolenaar406cd902020-02-18 21:54:41 +01002422" Test closing a window or editing another buffer from a FileChangedRO handler
2423" in a readonly buffer
2424func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002425 call test_override('ui_delay', 10)
2426
Bram Moolenaar406cd902020-02-18 21:54:41 +01002427 augroup FileChangedROTest
2428 au!
2429 autocmd FileChangedRO * quit
2430 augroup END
2431 new
2432 set readonly
2433 call assert_fails('normal i', 'E788:')
2434 close
2435 augroup! FileChangedROTest
2436
2437 augroup FileChangedROTest
2438 au!
2439 autocmd FileChangedRO * edit Xfile
2440 augroup END
2441 new
2442 set readonly
2443 call assert_fails('normal i', 'E788:')
2444 close
2445 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002446 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002447endfunc
2448
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002449func LogACmd()
2450 call add(g:logged, line('$'))
2451endfunc
2452
2453func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002454 CheckNotGui
2455
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002456 enew!
2457 tabnew
2458 call setline(1, ['a', 'b', 'c', 'd'])
2459 $
2460 au TermChanged * call LogACmd()
2461 let g:logged = []
2462 let term_save = &term
2463 set term=xterm
2464 call assert_equal([1, 4], g:logged)
2465
2466 au! TermChanged
2467 let &term = term_save
2468 bwipe!
2469endfunc
2470
Bram Moolenaare3284872020-03-19 13:55:03 +01002471" Test for FileReadCmd autocmd
2472func Test_autocmd_FileReadCmd()
2473 func ReadFileCmd()
2474 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2475 endfunc
2476 augroup FileReadCmdTest
2477 au!
2478 au FileReadCmd Xtest call ReadFileCmd()
2479 augroup END
2480
2481 new
2482 read ++bin Xtest
2483 read ++nobin Xtest
2484 read ++edit Xtest
2485 read ++bad=keep Xtest
2486 read ++bad=drop Xtest
2487 read ++bad=- Xtest
2488 read ++ff=unix Xtest
2489 read ++ff=dos Xtest
2490 read ++ff=mac Xtest
2491 read ++enc=utf-8 Xtest
2492
2493 call assert_equal(['',
2494 \ 'v:cmdarg = ++bin',
2495 \ 'v:cmdarg = ++nobin',
2496 \ 'v:cmdarg = ++edit',
2497 \ 'v:cmdarg = ++bad=keep',
2498 \ 'v:cmdarg = ++bad=drop',
2499 \ 'v:cmdarg = ++bad=-',
2500 \ 'v:cmdarg = ++ff=unix',
2501 \ 'v:cmdarg = ++ff=dos',
2502 \ 'v:cmdarg = ++ff=mac',
2503 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2504
2505 close!
2506 augroup FileReadCmdTest
2507 au!
2508 augroup END
2509 delfunc ReadFileCmd
2510endfunc
2511
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002512" Test for passing invalid arguments to autocmd
2513func Test_autocmd_invalid_args()
2514 " Additional character after * for event
2515 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2516 augroup Test
2517 augroup END
2518 " Invalid autocmd event
2519 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2520 " Invalid autocmd event in a autocmd group
2521 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2522 augroup! Test
2523 " Execute all autocmds
2524 call assert_fails('doautocmd * BufEnter', 'E217:')
2525 call assert_fails('augroup! x1a2b3', 'E367:')
2526 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002527 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002528endfunc
2529
2530" Test for deep nesting of autocmds
2531func Test_autocmd_deep_nesting()
2532 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2533 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2534 autocmd! BufEnter Xfile
2535endfunc
2536
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002537" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2538func Test_autocmd_sigusr1()
2539 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002540 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002541
2542 let g:sigusr1_passed = 0
2543 au SigUSR1 * let g:sigusr1_passed = 1
2544 call system('/bin/kill -s usr1 ' . getpid())
2545 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2546
2547 au! SigUSR1
2548 unlet g:sigusr1_passed
2549endfunc
2550
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002551" Test for BufReadPre autocmd deleting the file
2552func Test_BufReadPre_delfile()
2553 augroup TestAuCmd
2554 au!
2555 autocmd BufReadPre Xfile call delete('Xfile')
2556 augroup END
2557 call writefile([], 'Xfile')
2558 call assert_fails('new Xfile', 'E200:')
2559 call assert_equal('Xfile', @%)
2560 call assert_equal(1, &readonly)
2561 call delete('Xfile')
2562 augroup TestAuCmd
2563 au!
2564 augroup END
2565 close!
2566endfunc
2567
2568" Test for BufReadPre autocmd changing the current buffer
2569func Test_BufReadPre_changebuf()
2570 augroup TestAuCmd
2571 au!
2572 autocmd BufReadPre Xfile edit Xsomeotherfile
2573 augroup END
2574 call writefile([], 'Xfile')
2575 call assert_fails('new Xfile', 'E201:')
2576 call assert_equal('Xsomeotherfile', @%)
2577 call assert_equal(1, &readonly)
2578 call delete('Xfile')
2579 augroup TestAuCmd
2580 au!
2581 augroup END
2582 close!
2583endfunc
2584
2585" Test for BufWipeouti autocmd changing the current buffer when reading a file
2586" in an empty buffer with 'f' flag in 'cpo'
2587func Test_BufDelete_changebuf()
2588 new
2589 augroup TestAuCmd
2590 au!
2591 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2592 augroup END
2593 let save_cpo = &cpo
2594 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002595 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002596 call assert_equal('somefile', @%)
2597 let &cpo = save_cpo
2598 augroup TestAuCmd
2599 au!
2600 augroup END
2601 close!
2602endfunc
2603
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002604" Test for the temporary internal window used to execute autocmds
2605func Test_autocmd_window()
2606 %bw!
2607 edit one.txt
2608 tabnew two.txt
2609 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002610 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002611 au!
2612 au BufEnter * call add(g:blist, [expand('<afile>'),
2613 \ win_gettype(bufwinnr(expand('<afile>')))])
2614 augroup END
2615
2616 doautoall BufEnter
Bram Moolenaar40a019f2020-06-17 21:41:35 +02002617 call assert_equal([['one.txt', 'autocmd'], ['two.txt', '']], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002618
Bram Moolenaar832adf92020-06-25 19:01:36 +02002619 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002620 au!
2621 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002622 augroup! aucmd_win_test1
2623 %bw!
2624endfunc
2625
2626" Test for trying to close the temporary window used for executing an autocmd
2627func Test_close_autocmd_window()
2628 %bw!
2629 edit one.txt
2630 tabnew two.txt
2631 augroup aucmd_win_test2
2632 au!
2633 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2634 augroup END
2635
2636 call assert_fails('doautoall BufEnter', 'E813:')
2637
2638 augroup aucmd_win_test2
2639 au!
2640 augroup END
2641 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002642 %bwipe!
2643endfunc
2644
2645" Test for trying to close the tab that has the temporary window for exeucing
2646" an autocmd.
2647func Test_close_autocmd_tab()
2648 edit one.txt
2649 tabnew two.txt
2650 augroup aucmd_win_test
2651 au!
2652 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2653 augroup END
2654
2655 call assert_fails('doautoall BufEnter', 'E813:')
2656
2657 tabonly
2658 augroup aucmd_win_test
2659 au!
2660 augroup END
2661 augroup! aucmd_win_test
2662 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002663endfunc
2664
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002665" vim: shiftwidth=2 sts=2 expandtab