blob: f7963259b98f70cd140066fc9c888155cfd69226 [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
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100303 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200304 redir => res
305 augroup! TheWarning
306 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100307 call assert_match("W19:", res)
308 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200309
310 " check "Another" does not take the pace of the deleted entry
311 augroup Another
312 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100313 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
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
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100320 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200321 redir => res
322 doautocmd VimEnter
323 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100324 call assert_notmatch("W19:", res)
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 Moolenaar5dc4e2f2020-11-25 14:15:12 +0100354 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200355 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()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01001692 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01001693
1694 enew!
1695 call setline(1, ['a', 'b', 'c', 'd'])
1696
1697 let shelltemp = &shelltemp
1698 set shelltemp
1699
1700 let g:filter_au = 0
1701 au FilterWritePre * let g:filter_au += 1
1702 au FilterReadPre * let g:filter_au += 1
1703 au FilterReadPost * let g:filter_au += 1
1704 %!cat
1705 call assert_equal(3, g:filter_au)
1706
1707 if has('filterpipe')
1708 set noshelltemp
1709
1710 let g:filter_au = 0
1711 au FilterWritePre * let g:filter_au += 1
1712 au FilterReadPre * let g:filter_au += 1
1713 au FilterReadPost * let g:filter_au += 1
1714 %!cat
1715 call assert_equal(0, g:filter_au)
1716 endif
1717
1718 au! FilterWritePre,FilterReadPre,FilterReadPost
1719 let &shelltemp = shelltemp
1720 bwipe!
1721endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001722
1723func Test_TextYankPost()
1724 enew!
1725 call setline(1, ['foo'])
1726
1727 let g:event = []
1728 au TextYankPost * let g:event = copy(v:event)
1729
1730 call assert_equal({}, v:event)
1731 call assert_fails('let v:event = {}', 'E46:')
1732 call assert_fails('let v:event.mykey = 0', 'E742:')
1733
1734 norm "ayiw
1735 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001736 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001737 \g:event)
1738 norm y_
1739 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001740 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false},
1741 \g:event)
1742 norm Vy
1743 call assert_equal(
1744 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001745 \g:event)
1746 call feedkeys("\<C-V>y", 'x')
1747 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001748 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001749 \g:event)
1750 norm "xciwbar
1751 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001752 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001753 \g:event)
1754 norm "bdiw
1755 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001756 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001757 \g:event)
1758
1759 call assert_equal({}, v:event)
1760
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001761 if has('clipboard_working') && !has('gui_running')
1762 " Test that when the visual selection is automatically copied to clipboard
1763 " register a TextYankPost is emitted
1764 call setline(1, ['foobar'])
1765
1766 let @* = ''
1767 set clipboard=autoselect
1768 exe "norm! ggviw\<Esc>"
1769 call assert_equal(
1770 \{'regcontents': ['foobar'], 'regname': '*', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1771 \g:event)
1772
1773 let @+ = ''
1774 set clipboard=autoselectplus
1775 exe "norm! ggviw\<Esc>"
1776 call assert_equal(
1777 \{'regcontents': ['foobar'], 'regname': '+', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1778 \g:event)
1779
1780 set clipboard&vim
1781 endif
1782
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001783 au! TextYankPost
1784 unlet g:event
1785 bwipe!
1786endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001787
1788func Test_nocatch_wipe_all_buffers()
1789 " Real nasty autocommand: wipe all buffers on any event.
1790 au * * bwipe *
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001791 call assert_fails('next x', ['E94:', 'E937:'])
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001792 bwipe
1793 au!
1794endfunc
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001795
1796func Test_nocatch_wipe_dummy_buffer()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001797 CheckFeature quickfix
1798 " Nasty autocommand: wipe buffer on any event.
1799 au * x bwipe
Bram Moolenaare2e40752020-09-04 21:18:46 +02001800 call assert_fails('lv½ /x', 'E937:')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001801 au!
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001802endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001803
1804function s:Before_test_dirchanged()
1805 augroup test_dirchanged
1806 autocmd!
1807 augroup END
1808 let s:li = []
1809 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001810 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001811 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001812 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001813 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001814endfunc
1815
1816function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001817 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001818 call delete(s:dir_foo, 'd')
1819 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001820 augroup test_dirchanged
1821 autocmd!
1822 augroup END
1823endfunc
1824
1825function Test_dirchanged_global()
1826 call s:Before_test_dirchanged()
1827 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1828 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001829 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001830 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001831 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001832 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001833 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001834 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001835 call s:After_test_dirchanged()
1836endfunc
1837
1838function Test_dirchanged_local()
1839 call s:Before_test_dirchanged()
1840 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1841 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001842 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001843 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001844 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001845 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001846 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001847 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001848 call s:After_test_dirchanged()
1849endfunc
1850
1851function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001852 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001853 call s:Before_test_dirchanged()
1854 call test_autochdir()
1855 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1856 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1857 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001858 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001859 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001860 exe 'edit ' . s:dir_foo . '/Xfile'
1861 call assert_equal(s:dir_foo, getcwd())
1862 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001863 set noacd
1864 bwipe!
1865 call s:After_test_dirchanged()
1866endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001867
1868" Test TextChangedI and TextChangedP
1869func Test_ChangedP()
1870 new
1871 call setline(1, ['foo', 'bar', 'foobar'])
1872 call test_override("char_avail", 1)
1873 set complete=. completeopt=menuone
1874
1875 func! TextChangedAutocmd(char)
1876 let g:autocmd .= a:char
1877 endfunc
1878
1879 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1880 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1881 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1882
1883 call cursor(3, 1)
1884 let g:autocmd = ''
1885 call feedkeys("o\<esc>", 'tnix')
1886 call assert_equal('I', g:autocmd)
1887
1888 let g:autocmd = ''
1889 call feedkeys("Sf", 'tnix')
1890 call assert_equal('II', g:autocmd)
1891
1892 let g:autocmd = ''
1893 call feedkeys("Sf\<C-N>", 'tnix')
1894 call assert_equal('IIP', g:autocmd)
1895
1896 let g:autocmd = ''
1897 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1898 call assert_equal('IIPP', g:autocmd)
1899
1900 let g:autocmd = ''
1901 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1902 call assert_equal('IIPPP', g:autocmd)
1903
1904 let g:autocmd = ''
1905 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1906 call assert_equal('IIPPPP', g:autocmd)
1907
1908 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1909 " TODO: how should it handle completeopt=noinsert,noselect?
1910
1911 " CleanUp
1912 call test_override("char_avail", 0)
1913 au! TextChanged
1914 au! TextChangedI
1915 au! TextChangedP
1916 delfu TextChangedAutocmd
1917 unlet! g:autocmd
1918 set complete&vim completeopt&vim
1919
1920 bw!
1921endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001922
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001923let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001924func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001925 if !g:setline_handled
1926 call setline(1, "(x)")
1927 let g:setline_handled = v:true
1928 endif
1929endfunc
1930
1931func Test_TextChangedI_with_setline()
1932 new
1933 call test_override('char_avail', 1)
1934 autocmd TextChangedI <buffer> call SetLineOne()
1935 call feedkeys("i(\<CR>\<Esc>", 'tx')
1936 call assert_equal('(', getline(1))
1937 call assert_equal('x)', getline(2))
1938 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001939 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001940 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001941
1942 call test_override('starting', 0)
1943 bwipe!
1944endfunc
1945
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001946func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001947 CheckFeature terminal
1948 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01001949 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01001950 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001951
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001952 " Prepare file for TextChanged event.
1953 call writefile([''], 'Xchanged.txt')
1954 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1955 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001956 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001957 " In ConPTY, there is additional character which is drawn up to the width of
1958 " the screen.
1959 if has('conpty')
1960 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
1961 else
1962 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
1963 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001964 " It's only adding autocmd, so that no event occurs.
1965 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1966 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001967 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001968 call assert_equal([''], readfile('Xchanged.txt'))
1969
1970 " clean up
1971 call delete('Xchanged.txt')
1972 bwipe!
1973endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01001974
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001975func Test_autocmd_nested()
1976 let g:did_nested = 0
1977 augroup Testing
1978 au WinNew * edit somefile
1979 au BufNew * let g:did_nested = 1
1980 augroup END
1981 split
1982 call assert_equal(0, g:did_nested)
1983 close
1984 bwipe! somefile
1985
1986 " old nested argument still works
1987 augroup Testing
1988 au!
1989 au WinNew * nested edit somefile
1990 au BufNew * let g:did_nested = 1
1991 augroup END
1992 split
1993 call assert_equal(1, g:did_nested)
1994 close
1995 bwipe! somefile
1996
1997 " New ++nested argument works
1998 augroup Testing
1999 au!
2000 au WinNew * ++nested edit somefile
2001 au BufNew * let g:did_nested = 1
2002 augroup END
2003 split
2004 call assert_equal(1, g:did_nested)
2005 close
2006 bwipe! somefile
2007
2008 augroup Testing
2009 au!
2010 augroup END
2011
2012 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2013 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2014endfunc
2015
2016func Test_autocmd_once()
2017 " Without ++once WinNew triggers twice
2018 let g:did_split = 0
2019 augroup Testing
2020 au WinNew * let g:did_split += 1
2021 augroup END
2022 split
2023 split
2024 call assert_equal(2, g:did_split)
2025 call assert_true(exists('#WinNew'))
2026 close
2027 close
2028
2029 " With ++once WinNew triggers once
2030 let g:did_split = 0
2031 augroup Testing
2032 au!
2033 au WinNew * ++once let g:did_split += 1
2034 augroup END
2035 split
2036 split
2037 call assert_equal(1, g:did_split)
2038 call assert_false(exists('#WinNew'))
2039 close
2040 close
2041
2042 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2043endfunc
2044
Bram Moolenaara68e5952019-04-25 22:22:01 +02002045func Test_autocmd_bufreadpre()
2046 new
2047 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002048 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002049 w! XAutocmdBufReadPre.txt
2050 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002051 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002052 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002053 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002054 wincmd p
2055 let g:wsv1 = winsaveview()
2056 wincmd p
2057 let g:wsv2 = winsaveview()
2058 " triggers BufReadPre, should not move the cursor in either window
2059 " The topline may change one line in a large window.
2060 edit
2061 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2062 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2063 call assert_equal(2, b:bufreadpre)
2064 wincmd p
2065 call assert_equal(g:wsv1.topline, winsaveview().topline)
2066 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2067 call assert_equal(2, b:bufreadpre)
2068 " Now set the cursor position in an BufReadPre autocommand
2069 " (even though the position will be invalid, this should make Vim reset the
2070 " cursor position in the other window.
2071 wincmd p
2072 set cpo+=g
2073 " won't do anything, but try to set the cursor on an invalid lnum
2074 autocmd BufReadPre <buffer> :norm! 70gg
2075 " triggers BufReadPre, should not move the cursor in either window
2076 e
2077 call assert_equal(1, winsaveview().topline)
2078 call assert_equal(1, winsaveview().lnum)
2079 call assert_equal(3, b:bufreadpre)
2080 wincmd p
2081 call assert_equal(g:wsv1.topline, winsaveview().topline)
2082 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2083 call assert_equal(3, b:bufreadpre)
2084 close
2085 close
2086 call delete('XAutocmdBufReadPre.txt')
2087 set cpo-=g
2088endfunc
2089
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002090" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002091
2092" Tests for the following autocommands:
2093" - FileWritePre writing a compressed file
2094" - FileReadPost reading a compressed file
2095" - BufNewFile reading a file template
2096" - BufReadPre decompressing the file to be read
2097" - FilterReadPre substituting characters in the temp file
2098" - FilterReadPost substituting characters after filtering
2099" - FileReadPre set options for decompression
2100" - FileReadPost decompress the file
2101func Test_ReadWrite_Autocmds()
2102 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002103 CheckUnix
2104 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002105
2106 " Make $GZIP empty, "-v" would cause trouble.
2107 let $GZIP = ""
2108
2109 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2110 " being modified outside of Vim (noticed on Solaris).
2111 au FileChangedShell * echo 'caught FileChangedShell'
2112
2113 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2114 augroup Test1
2115 au!
2116 au FileWritePre *.gz '[,']!gzip
2117 au FileWritePost *.gz undo
2118 au FileReadPost *.gz '[,']!gzip -d
2119 augroup END
2120
2121 new
2122 set bin
2123 call append(0, [
2124 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2125 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2126 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2127 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2128 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2129 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2130 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2131 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2132 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2133 \ ])
2134 1,9write! Xtestfile.gz
2135 enew! | close
2136
2137 new
2138 " Read and decompress the testfile
2139 0read Xtestfile.gz
2140 call assert_equal([
2141 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2142 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2143 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2144 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2145 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2146 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2147 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2148 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2149 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2150 \ ], getline(1, 9))
2151 enew! | close
2152
2153 augroup Test1
2154 au!
2155 augroup END
2156
2157 " Test for the FileAppendPre and FileAppendPost autocmds
2158 augroup Test2
2159 au!
2160 au BufNewFile *.c read Xtest.c
2161 au FileAppendPre *.out '[,']s/new/NEW/
2162 au FileAppendPost *.out !cat Xtest.c >> test.out
2163 augroup END
2164
2165 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2166 new foo.c " should load Xtest.c
2167 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2168 w! >> test.out " append it to the output file
2169
2170 let contents = readfile('test.out')
2171 call assert_equal(' * Here is a NEW .c file', contents[2])
2172 call assert_equal(' * Here is a new .c file', contents[5])
2173
2174 call delete('test.out')
2175 enew! | close
2176 augroup Test2
2177 au!
2178 augroup END
2179
2180 " Test for the BufReadPre and BufReadPost autocmds
2181 augroup Test3
2182 au!
2183 " setup autocommands to decompress before reading and re-compress
2184 " afterwards
2185 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2186 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2187 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2188 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2189 augroup END
2190
2191 e! Xtestfile.gz " Edit compressed file
2192 call assert_equal([
2193 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2194 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2195 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2196 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2197 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2198 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2199 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2200 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2201 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2202 \ ], getline(1, 9))
2203
2204 w! >> test.out " Append it to the output file
2205
2206 augroup Test3
2207 au!
2208 augroup END
2209
2210 " Test for the FilterReadPre and FilterReadPost autocmds.
2211 set shelltemp " need temp files here
2212 augroup Test4
2213 au!
2214 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2215 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2216 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2217 au FilterReadPost *.out '[,']s/x/X/g
2218 augroup END
2219
2220 e! test.out " Edit the output file
2221 1,$!cat
2222 call assert_equal([
2223 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2224 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2225 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2226 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2227 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2228 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2229 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2230 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2231 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2232 \ ], getline(1, 9))
2233 call assert_equal([
2234 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2235 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2236 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2237 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2238 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2239 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2240 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2241 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2242 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2243 \ ], readfile('test.out'))
2244
2245 augroup Test4
2246 au!
2247 augroup END
2248 set shelltemp&vim
2249
2250 " Test for the FileReadPre and FileReadPost autocmds.
2251 augroup Test5
2252 au!
2253 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2254 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2255 au FileReadPost *.gz '[,']s/l/L/
2256 augroup END
2257
2258 new
2259 0r Xtestfile.gz " Read compressed file
2260 call assert_equal([
2261 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2262 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2263 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2264 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2265 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2266 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2267 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2268 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2269 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2270 \ ], getline(1, 9))
2271 call assert_equal([
2272 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2273 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2274 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2275 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2276 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2277 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2278 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2279 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2280 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2281 \ ], readfile('Xtestfile.gz'))
2282
2283 augroup Test5
2284 au!
2285 augroup END
2286
2287 au! FileChangedShell
2288 call delete('Xtestfile.gz')
2289 call delete('Xtest.c')
2290 call delete('test.out')
2291endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002292
2293func Test_throw_in_BufWritePre()
2294 new
2295 call setline(1, ['one', 'two', 'three'])
2296 call assert_false(filereadable('Xthefile'))
2297 augroup throwing
2298 au BufWritePre X* throw 'do not write'
2299 augroup END
2300 try
2301 w Xthefile
2302 catch
2303 let caught = 1
2304 endtry
2305 call assert_equal(1, caught)
2306 call assert_false(filereadable('Xthefile'))
2307
2308 bwipe!
2309 au! throwing
2310endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002311
2312func Test_autocmd_SafeState()
2313 CheckRunVimInTerminal
2314
2315 let lines =<< trim END
2316 let g:safe = 0
2317 let g:again = ''
2318 au SafeState * let g:safe += 1
2319 au SafeStateAgain * let g:again ..= 'x'
2320 func CallTimer()
2321 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2322 endfunc
2323 END
2324 call writefile(lines, 'XSafeState')
2325 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2326
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002327 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002328 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002329 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002330 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002331
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002332 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002333 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002334 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002335
2336 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002337 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002338 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002339 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002340 call term_sendkeys(buf, ":echo g:again\<CR>")
2341 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2342
2343 call StopVimInTerminal(buf)
2344 call delete('XSafeState')
2345endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002346
2347func Test_autocmd_CmdWinEnter()
2348 CheckRunVimInTerminal
2349 " There is not cmdwin switch, so
2350 " test for cmdline_hist
2351 " (both are available with small builds)
2352 CheckFeature cmdline_hist
2353 let lines =<< trim END
2354 let b:dummy_var = 'This is a dummy'
2355 autocmd CmdWinEnter * quit
2356 let winnr = winnr('$')
2357 END
2358 let filename='XCmdWinEnter'
2359 call writefile(lines, filename)
2360 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2361
2362 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002363 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002364 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002365 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002366 call term_sendkeys(buf, ":echo &buftype\<cr>")
2367 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2368 call term_sendkeys(buf, ":echo winnr\<cr>")
2369 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2370
2371 " clean up
2372 call StopVimInTerminal(buf)
2373 call delete(filename)
2374endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002375
2376func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002377 CheckFeature quickfix
2378
Bram Moolenaarec66c412019-10-11 21:19:13 +02002379 pedit xx
2380 n x
2381 au WinEnter * quit
2382 split
2383 au! WinEnter
2384endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002385
2386func Test_BufWrite_lockmarks()
2387 edit! Xtest
2388 call setline(1, ['a', 'b', 'c', 'd'])
2389
2390 " :lockmarks preserves the marks
2391 call SetChangeMarks(2, 3)
2392 lockmarks write
2393 call assert_equal([2, 3], [line("'["), line("']")])
2394
2395 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2396 " original values for the user
2397 augroup lockmarks
2398 au!
2399 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2400 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2401 augroup END
2402
2403 lockmarks write
2404 call assert_equal([2, 3], [line("'["), line("']")])
2405
2406 if executable('cat')
2407 lockmarks %!cat
2408 call assert_equal([2, 3], [line("'["), line("']")])
2409 endif
2410
2411 lockmarks 3,4write Xtest2
2412 call assert_equal([2, 3], [line("'["), line("']")])
2413
2414 au! lockmarks
2415 augroup! lockmarks
2416 call delete('Xtest')
2417 call delete('Xtest2')
2418endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002419
2420func Test_FileType_spell()
2421 if !isdirectory('/tmp')
2422 throw "Skipped: requires /tmp directory"
2423 endif
2424
2425 " this was crashing with an invalid free()
2426 setglobal spellfile=/tmp/en.utf-8.add
2427 augroup crash
2428 autocmd!
2429 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2430 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2431 autocmd FileType anotherfiletype setlocal spell
2432 augroup END
2433 func! NoCrash() abort
2434 edit /tmp/crashfile
2435 endfunc
2436 call NoCrash()
2437
2438 au! crash
2439 setglobal spellfile=
2440endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002441
Bram Moolenaar406cd902020-02-18 21:54:41 +01002442" Test closing a window or editing another buffer from a FileChangedRO handler
2443" in a readonly buffer
2444func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002445 call test_override('ui_delay', 10)
2446
Bram Moolenaar406cd902020-02-18 21:54:41 +01002447 augroup FileChangedROTest
2448 au!
2449 autocmd FileChangedRO * quit
2450 augroup END
2451 new
2452 set readonly
2453 call assert_fails('normal i', 'E788:')
2454 close
2455 augroup! FileChangedROTest
2456
2457 augroup FileChangedROTest
2458 au!
2459 autocmd FileChangedRO * edit Xfile
2460 augroup END
2461 new
2462 set readonly
2463 call assert_fails('normal i', 'E788:')
2464 close
2465 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002466 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002467endfunc
2468
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002469func LogACmd()
2470 call add(g:logged, line('$'))
2471endfunc
2472
2473func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002474 CheckNotGui
2475
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002476 enew!
2477 tabnew
2478 call setline(1, ['a', 'b', 'c', 'd'])
2479 $
2480 au TermChanged * call LogACmd()
2481 let g:logged = []
2482 let term_save = &term
2483 set term=xterm
2484 call assert_equal([1, 4], g:logged)
2485
2486 au! TermChanged
2487 let &term = term_save
2488 bwipe!
2489endfunc
2490
Bram Moolenaare3284872020-03-19 13:55:03 +01002491" Test for FileReadCmd autocmd
2492func Test_autocmd_FileReadCmd()
2493 func ReadFileCmd()
2494 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2495 endfunc
2496 augroup FileReadCmdTest
2497 au!
2498 au FileReadCmd Xtest call ReadFileCmd()
2499 augroup END
2500
2501 new
2502 read ++bin Xtest
2503 read ++nobin Xtest
2504 read ++edit Xtest
2505 read ++bad=keep Xtest
2506 read ++bad=drop Xtest
2507 read ++bad=- Xtest
2508 read ++ff=unix Xtest
2509 read ++ff=dos Xtest
2510 read ++ff=mac Xtest
2511 read ++enc=utf-8 Xtest
2512
2513 call assert_equal(['',
2514 \ 'v:cmdarg = ++bin',
2515 \ 'v:cmdarg = ++nobin',
2516 \ 'v:cmdarg = ++edit',
2517 \ 'v:cmdarg = ++bad=keep',
2518 \ 'v:cmdarg = ++bad=drop',
2519 \ 'v:cmdarg = ++bad=-',
2520 \ 'v:cmdarg = ++ff=unix',
2521 \ 'v:cmdarg = ++ff=dos',
2522 \ 'v:cmdarg = ++ff=mac',
2523 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2524
2525 close!
2526 augroup FileReadCmdTest
2527 au!
2528 augroup END
2529 delfunc ReadFileCmd
2530endfunc
2531
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002532" Test for passing invalid arguments to autocmd
2533func Test_autocmd_invalid_args()
2534 " Additional character after * for event
2535 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2536 augroup Test
2537 augroup END
2538 " Invalid autocmd event
2539 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2540 " Invalid autocmd event in a autocmd group
2541 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2542 augroup! Test
2543 " Execute all autocmds
2544 call assert_fails('doautocmd * BufEnter', 'E217:')
2545 call assert_fails('augroup! x1a2b3', 'E367:')
2546 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002547 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002548endfunc
2549
2550" Test for deep nesting of autocmds
2551func Test_autocmd_deep_nesting()
2552 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2553 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2554 autocmd! BufEnter Xfile
2555endfunc
2556
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002557" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2558func Test_autocmd_sigusr1()
2559 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002560 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002561
2562 let g:sigusr1_passed = 0
2563 au SigUSR1 * let g:sigusr1_passed = 1
2564 call system('/bin/kill -s usr1 ' . getpid())
2565 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2566
2567 au! SigUSR1
2568 unlet g:sigusr1_passed
2569endfunc
2570
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002571" Test for BufReadPre autocmd deleting the file
2572func Test_BufReadPre_delfile()
2573 augroup TestAuCmd
2574 au!
2575 autocmd BufReadPre Xfile call delete('Xfile')
2576 augroup END
2577 call writefile([], 'Xfile')
2578 call assert_fails('new Xfile', 'E200:')
2579 call assert_equal('Xfile', @%)
2580 call assert_equal(1, &readonly)
2581 call delete('Xfile')
2582 augroup TestAuCmd
2583 au!
2584 augroup END
2585 close!
2586endfunc
2587
2588" Test for BufReadPre autocmd changing the current buffer
2589func Test_BufReadPre_changebuf()
2590 augroup TestAuCmd
2591 au!
2592 autocmd BufReadPre Xfile edit Xsomeotherfile
2593 augroup END
2594 call writefile([], 'Xfile')
2595 call assert_fails('new Xfile', 'E201:')
2596 call assert_equal('Xsomeotherfile', @%)
2597 call assert_equal(1, &readonly)
2598 call delete('Xfile')
2599 augroup TestAuCmd
2600 au!
2601 augroup END
2602 close!
2603endfunc
2604
2605" Test for BufWipeouti autocmd changing the current buffer when reading a file
2606" in an empty buffer with 'f' flag in 'cpo'
2607func Test_BufDelete_changebuf()
2608 new
2609 augroup TestAuCmd
2610 au!
2611 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2612 augroup END
2613 let save_cpo = &cpo
2614 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002615 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002616 call assert_equal('somefile', @%)
2617 let &cpo = save_cpo
2618 augroup TestAuCmd
2619 au!
2620 augroup END
2621 close!
2622endfunc
2623
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002624" Test for the temporary internal window used to execute autocmds
2625func Test_autocmd_window()
2626 %bw!
2627 edit one.txt
2628 tabnew two.txt
2629 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002630 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002631 au!
2632 au BufEnter * call add(g:blist, [expand('<afile>'),
2633 \ win_gettype(bufwinnr(expand('<afile>')))])
2634 augroup END
2635
2636 doautoall BufEnter
Bram Moolenaar40a019f2020-06-17 21:41:35 +02002637 call assert_equal([['one.txt', 'autocmd'], ['two.txt', '']], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002638
Bram Moolenaar832adf92020-06-25 19:01:36 +02002639 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002640 au!
2641 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002642 augroup! aucmd_win_test1
2643 %bw!
2644endfunc
2645
2646" Test for trying to close the temporary window used for executing an autocmd
2647func Test_close_autocmd_window()
2648 %bw!
2649 edit one.txt
2650 tabnew two.txt
2651 augroup aucmd_win_test2
2652 au!
2653 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2654 augroup END
2655
2656 call assert_fails('doautoall BufEnter', 'E813:')
2657
2658 augroup aucmd_win_test2
2659 au!
2660 augroup END
2661 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002662 %bwipe!
2663endfunc
2664
2665" Test for trying to close the tab that has the temporary window for exeucing
2666" an autocmd.
2667func Test_close_autocmd_tab()
2668 edit one.txt
2669 tabnew two.txt
2670 augroup aucmd_win_test
2671 au!
2672 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2673 augroup END
2674
2675 call assert_fails('doautoall BufEnter', 'E813:')
2676
2677 tabonly
2678 augroup aucmd_win_test
2679 au!
2680 augroup END
2681 augroup! aucmd_win_test
2682 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002683endfunc
2684
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002685" vim: shiftwidth=2 sts=2 expandtab