blob: b7cc0c0bf2d70826c408ee05703fe8ddb9850d40 [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")
33 call term_wait(buf, 20)
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
80 set updatetime=500
81 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar8d4ce562019-01-30 22:01:40 +010082 \ {'exit_cb': {-> timer_start(1000, '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 Moolenaar9b7bf9e2020-07-11 22:14:59 +0200197 call assert_fails('edit bb.txt', ['E937:', 'E517:'])
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200198
199 autocmd! test_autocmd_bufunload
200 augroup! test_autocmd_bufunload
201 bwipe! aa.txt
202 bwipe! bb.txt
203endfunc
204
205" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200206func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200207 setlocal buftype=nowrite
208 let lastbuf = bufnr('$')
209
210 augroup test_autocmd_bufunload
211 autocmd!
212 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
213 augroup END
214
215 normal! i1
216 call assert_fails('edit a.txt', 'E517:')
217 call feedkeys("\<CR>")
218
219 autocmd! test_autocmd_bufunload
220 augroup! test_autocmd_bufunload
221 bwipe! a.txt
222endfunc
223
Bram Moolenaarc917da42016-07-19 22:31:36 +0200224func Test_win_tab_autocmd()
225 let g:record = []
226
227 augroup testing
228 au WinNew * call add(g:record, 'WinNew')
229 au WinEnter * call add(g:record, 'WinEnter')
230 au WinLeave * call add(g:record, 'WinLeave')
231 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200232 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200233 au TabEnter * call add(g:record, 'TabEnter')
234 au TabLeave * call add(g:record, 'TabLeave')
235 augroup END
236
237 split
238 tabnew
239 close
240 close
241
242 call assert_equal([
243 \ 'WinLeave', 'WinNew', 'WinEnter',
244 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200245 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200246 \ 'WinLeave', 'WinEnter'
247 \ ], g:record)
248
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200249 let g:record = []
250 tabnew somefile
251 tabnext
252 bwipe somefile
253
254 call assert_equal([
255 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
256 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
257 \ 'TabClosed'
258 \ ], g:record)
259
Bram Moolenaarc917da42016-07-19 22:31:36 +0200260 augroup testing
261 au!
262 augroup END
263 unlet g:record
264endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200265
266func s:AddAnAutocmd()
267 augroup vimBarTest
268 au BufReadCmd * echo 'hello'
269 augroup END
270 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
271endfunc
272
273func Test_early_bar()
274 " test that a bar is recognized before the {event}
275 call s:AddAnAutocmd()
276 augroup vimBarTest | au! | augroup END
277 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
278
279 call s:AddAnAutocmd()
280 augroup vimBarTest| au!| augroup END
281 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
282
283 " test that a bar is recognized after the {event}
284 call s:AddAnAutocmd()
285 augroup vimBarTest| au!BufReadCmd| augroup END
286 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
287
288 " test that a bar is recognized after the {group}
289 call s:AddAnAutocmd()
290 au! vimBarTest|echo 'hello'
291 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
292endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200293
Bram Moolenaar5c809082016-09-01 16:21:48 +0200294func RemoveGroup()
295 autocmd! StartOK
296 augroup! StartOK
297endfunc
298
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200299func Test_augroup_warning()
300 augroup TheWarning
301 au VimEnter * echo 'entering'
302 augroup END
303 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
304 redir => res
305 augroup! TheWarning
306 redir END
307 call assert_true(match(res, "W19:") >= 0)
308 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
309
310 " check "Another" does not take the pace of the deleted entry
311 augroup Another
312 augroup END
313 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200314 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200315
316 " no warning for postpone aucmd delete
317 augroup StartOK
318 au VimEnter * call RemoveGroup()
319 augroup END
320 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
321 redir => res
322 doautocmd VimEnter
323 redir END
324 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200325 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200326
327 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200328endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200329
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200330func Test_BufReadCmdHelp()
331 " This used to cause access to free memory
332 au BufReadCmd * e +h
333 help
334
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200335 au! BufReadCmd
336endfunc
337
338func Test_BufReadCmdHelpJump()
339 " This used to cause access to free memory
340 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200341 " } to fix highlighting
342 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200343
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200344 au! BufReadCmd
345endfunc
346
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200347func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200348 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200349 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200350 call assert_fails('augroup! x', 'E936:')
351 au VimEnter * echo
352 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200353 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200354 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
355 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200356endfunc
357
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200358" Tests for autocommands on :close command.
359" This used to be in test13.
360func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200361 " Clean up buffers, because in some cases this function fails.
362 call s:cleanup_buffers()
363
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200364 " Write three files and open them, each in a window.
365 " Then go to next window, with autocommand that deletes the previous one.
366 " Do this twice, writing the file.
367 e! Xtestje1
368 call setline(1, 'testje1')
369 w
370 sp Xtestje2
371 call setline(1, 'testje2')
372 w
373 sp Xtestje3
374 call setline(1, 'testje3')
375 w
376 wincmd w
377 au WinLeave Xtestje2 bwipe
378 wincmd w
379 call assert_equal('Xtestje1', expand('%'))
380
381 au WinLeave Xtestje1 bwipe Xtestje3
382 close
383 call assert_equal('Xtestje1', expand('%'))
384
385 " Test deleting the buffer on a Unload event. If this goes wrong there
386 " will be the ATTENTION prompt.
387 e Xtestje1
388 au!
389 au! BufUnload Xtestje1 bwipe
390 call assert_fails('e Xtestje3', 'E937:')
391 call assert_equal('Xtestje3', expand('%'))
392
393 e Xtestje2
394 sp Xtestje1
395 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200396 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200397
398 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
399 " there are ml_line errors and/or a Crash.
400 au!
401 only
402 e Xanother
403 e Xtestje1
404 bwipe Xtestje2
405 bwipe Xtestje3
406 au BufWipeout Xtestje1 buf Xtestje1
407 bwipe
408 call assert_equal('Xanother', expand('%'))
409
410 only
411 help
412 wincmd w
413 1quit
414 call assert_equal('Xanother', expand('%'))
415
416 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100417 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200418 call delete('Xtestje1')
419 call delete('Xtestje2')
420 call delete('Xtestje3')
421endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100422
423func Test_BufEnter()
424 au! BufEnter
425 au Bufenter * let val = val . '+'
426 let g:val = ''
427 split NewFile
428 call assert_equal('+', g:val)
429 bwipe!
430 call assert_equal('++', g:val)
431
432 " Also get BufEnter when editing a directory
433 call mkdir('Xdir')
434 split Xdir
435 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100436
437 " On MS-Windows we can't edit the directory, make sure we wipe the right
438 " buffer.
439 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100440
441 call delete('Xdir', 'd')
442 au! BufEnter
443endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100444
445" Closing a window might cause an endless loop
446" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200447func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200448 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100449 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200450 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100451 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100452 mksession!
453
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200454 let content =<< trim [CODE]
455 set nocp noswapfile
456 let v:swapchoice="e"
457 augroup test_autocmd_sessionload
458 autocmd!
459 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
460 augroup END
461
462 func WriteErrors()
463 call writefile([execute("messages")], "Xerrors")
464 endfunc
465 au VimLeave * call WriteErrors()
466 [CODE]
467
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100468 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200469 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100470 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200471 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100472
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100473 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100474 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100475 call delete(file)
476 endfor
477endfunc
478
479" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200480func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100481 tabnew
482 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100483 mksession!
484
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200485 let content =<< trim [CODE]
486 set nocp noswapfile
487 function! DeleteInactiveBufs()
488 tabfirst
489 let tabblist = []
490 for i in range(1, tabpagenr(''$''))
491 call extend(tabblist, tabpagebuflist(i))
492 endfor
493 for b in range(1, bufnr(''$''))
494 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
495 exec ''bwipeout '' . b
496 endif
497 endfor
498 echomsg "SessionLoadPost DONE"
499 endfunction
500 au SessionLoadPost * call DeleteInactiveBufs()
501
502 func WriteErrors()
503 call writefile([execute("messages")], "Xerrors")
504 endfunc
505 au VimLeave * call WriteErrors()
506 [CODE]
507
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100508 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200509 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100510 let errors = join(readfile('Xerrors'))
511 " This probably only ever matches on unix.
512 call assert_notmatch('Caught deadly signal SEGV', errors)
513 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100514
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100515 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100516 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100517 call delete(file)
518 endfor
519endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200520
521func Test_empty_doau()
522 doau \|
523endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200524
525func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200526 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200527 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200528 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
529 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 +0200530 let g:opt = [expected, actual]
531 "call assert_equal(expected, actual)
532endfunc
533
534func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200535 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200536
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200537 badd test_autocmd.vim
538
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200539 call test_override('starting', 1)
540 set nocp
541 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
542
543 " 1: Setting number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200544 let g:options=[['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200545 set nu
546 call assert_equal([], g:options)
547 call assert_equal(g:opt[0], g:opt[1])
548
549 " 2: Setting local number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200550 let g:options=[['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200551 setlocal nonu
552 call assert_equal([], g:options)
553 call assert_equal(g:opt[0], g:opt[1])
554
555 " 3: Setting global number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200556 let g:options=[['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200557 setglobal nonu
558 call assert_equal([], g:options)
559 call assert_equal(g:opt[0], g:opt[1])
560
561 " 4: Setting local autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200562 let g:options=[['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200563 setlocal ai
564 call assert_equal([], g:options)
565 call assert_equal(g:opt[0], g:opt[1])
566
567 " 5: Setting global autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200568 let g:options=[['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200569 setglobal ai
570 call assert_equal([], g:options)
571 call assert_equal(g:opt[0], g:opt[1])
572
573 " 6: Setting global autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200574 let g:options=[['autoindent', 1, 1, 1, 0, 'global', 'set']]
575 set ai!
576 call assert_equal([], g:options)
577 call assert_equal(g:opt[0], g:opt[1])
578
579 " 6a: Setting global autoindent option"
580 let g:options=[['autoindent', 1, 1, 0, 0, 'global', 'set']]
581 noa setlocal ai
582 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200583 set ai!
584 call assert_equal([], g:options)
585 call assert_equal(g:opt[0], g:opt[1])
586
587 " Should not print anything, use :noa
588 " 7: don't trigger OptionSet"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200589 let g:options=[['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200590 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200591 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200592 call assert_equal(g:opt[0], g:opt[1])
593
594 " 8: Setting several global list and number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200595 let g:options=[['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200596 set list nu
597 call assert_equal([], g:options)
598 call assert_equal(g:opt[0], g:opt[1])
599
600 " 9: don't trigger OptionSet"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200601 let g:options=[['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200602 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200603 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 +0200604 call assert_equal(g:opt[0], g:opt[1])
605
606 " 10: Setting global acd"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200607 let g:options=[['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200608 setlocal acd
609 call assert_equal([], g:options)
610 call assert_equal(g:opt[0], g:opt[1])
611
612 " 11: Setting global autoread (also sets local value)"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200613 let g:options=[['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200614 set ar
615 call assert_equal([], g:options)
616 call assert_equal(g:opt[0], g:opt[1])
617
618 " 12: Setting local autoread"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200619 let g:options=[['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200620 setlocal ar
621 call assert_equal([], g:options)
622 call assert_equal(g:opt[0], g:opt[1])
623
624 " 13: Setting global autoread"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200625 let g:options=[['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200626 setglobal invar
627 call assert_equal([], g:options)
628 call assert_equal(g:opt[0], g:opt[1])
629
630 " 14: Setting option backspace through :let"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200631 let g:options=[['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200632 let &bs="eol,indent,start"
633 call assert_equal([], g:options)
634 call assert_equal(g:opt[0], g:opt[1])
635
636 " 15: Setting option backspace through setbufvar()"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200637 let g:options=[['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200638 " try twice, first time, shouldn't trigger because option name is invalid,
639 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200640 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200641 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200642 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200643 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200644 call assert_equal([], g:options)
645 call assert_equal(g:opt[0], g:opt[1])
646
647 " 16: Setting number option using setwinvar"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200648 let g:options=[['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200649 call setwinvar(0, '&number', 1)
650 call assert_equal([], g:options)
651 call assert_equal(g:opt[0], g:opt[1])
652
653 " 17: Setting key option, shouldn't trigger"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200654 let g:options=[['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200655 setlocal key=blah
656 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200657 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200658 call assert_equal(g:opt[0], g:opt[1])
659
Bram Moolenaard7c96872019-06-15 17:12:48 +0200660
661 " 18a: Setting string global option"
662 let oldval = &backupext
663 let g:options=[['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
664 set backupext=foo
665 call assert_equal([], g:options)
666 call assert_equal(g:opt[0], g:opt[1])
667
668 " 18b: Resetting string global option"
669 let g:options=[['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
670 set backupext&
671 call assert_equal([], g:options)
672 call assert_equal(g:opt[0], g:opt[1])
673
674 " 18c: Setting global string global option"
675 let g:options=[['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
676 setglobal backupext=bar
677 call assert_equal([], g:options)
678 call assert_equal(g:opt[0], g:opt[1])
679
680 " 18d: Setting local string global option"
681 " As this is a global option this sets the global value even though
682 " :setlocal is used!
683 noa set backupext& " Reset global and local value (without triggering autocmd)
684 let g:options=[['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
685 setlocal backupext=baz
686 call assert_equal([], g:options)
687 call assert_equal(g:opt[0], g:opt[1])
688
689 " 18e: Setting again string global option"
690 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
691 noa setlocal backupext=ext_local " Sets the global(!) value!
692 let g:options=[['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
693 set backupext=fuu
694 call assert_equal([], g:options)
695 call assert_equal(g:opt[0], g:opt[1])
696
697
698 " 19a: Setting string local-global (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200699 let oldval = &tags
Bram Moolenaard7c96872019-06-15 17:12:48 +0200700 let g:options=[['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200701 set tags=tagpath
702 call assert_equal([], g:options)
703 call assert_equal(g:opt[0], g:opt[1])
704
Bram Moolenaard7c96872019-06-15 17:12:48 +0200705 " 19b: Resetting string local-global (to buffer) option"
706 let g:options=[['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200707 set tags&
708 call assert_equal([], g:options)
709 call assert_equal(g:opt[0], g:opt[1])
710
Bram Moolenaard7c96872019-06-15 17:12:48 +0200711 " 19c: Setting global string local-global (to buffer) option "
712 let g:options=[['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
713 setglobal tags=tagpath1
714 call assert_equal([], g:options)
715 call assert_equal(g:opt[0], g:opt[1])
716
717 " 19d: Setting local string local-global (to buffer) option"
718 let g:options=[['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
719 setlocal tags=tagpath2
720 call assert_equal([], g:options)
721 call assert_equal(g:opt[0], g:opt[1])
722
723 " 19e: Setting again string local-global (to buffer) option"
724 " Note: v:option_old is the old global value for local-global string options
725 " but the old local value for all other kinds of options.
726 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
727 noa setlocal tags=tag_local
728 let g:options=[['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
729 set tags=tagpath
730 call assert_equal([], g:options)
731 call assert_equal(g:opt[0], g:opt[1])
732
733 " 19f: Setting string local-global (to buffer) option to an empty string"
734 " Note: v:option_old is the old global value for local-global string options
735 " but the old local value for all other kinds of options.
736 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
737 noa setlocal tags= " empty string
738 let g:options=[['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
739 set tags=tagpath
740 call assert_equal([], g:options)
741 call assert_equal(g:opt[0], g:opt[1])
742
743
744 " 20a: Setting string local (to buffer) option"
745 let oldval = &spelllang
746 let g:options=[['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
747 set spelllang=elvish,klingon
748 call assert_equal([], g:options)
749 call assert_equal(g:opt[0], g:opt[1])
750
751 " 20b: Resetting string local (to buffer) option"
752 let g:options=[['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
753 set spelllang&
754 call assert_equal([], g:options)
755 call assert_equal(g:opt[0], g:opt[1])
756
757 " 20c: Setting global string local (to buffer) option"
758 let g:options=[['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
759 setglobal spelllang=elvish
760 call assert_equal([], g:options)
761 call assert_equal(g:opt[0], g:opt[1])
762
763 " 20d: Setting local string local (to buffer) option"
764 noa set spelllang& " Reset global and local value (without triggering autocmd)
765 let g:options=[['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
766 setlocal spelllang=klingon
767 call assert_equal([], g:options)
768 call assert_equal(g:opt[0], g:opt[1])
769
770 " 20e: Setting again string local (to buffer) option"
771 " Note: v:option_old is the old global value for local-global string options
772 " but the old local value for all other kinds of options.
773 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
774 noa setlocal spelllang=spelllocal
775 let g:options=[['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
776 set spelllang=foo
777 call assert_equal([], g:options)
778 call assert_equal(g:opt[0], g:opt[1])
779
780
781 " 21a: Setting string local-global (to window) option"
782 let oldval = &statusline
783 let g:options=[['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
784 set statusline=foo
785 call assert_equal([], g:options)
786 call assert_equal(g:opt[0], g:opt[1])
787
788 " 21b: Resetting string local-global (to window) option"
789 " Note: v:option_old is the old global value for local-global string options
790 " but the old local value for all other kinds of options.
791 let g:options=[['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
792 set statusline&
793 call assert_equal([], g:options)
794 call assert_equal(g:opt[0], g:opt[1])
795
796 " 21c: Setting global string local-global (to window) option"
797 let g:options=[['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
798 setglobal statusline=bar
799 call assert_equal([], g:options)
800 call assert_equal(g:opt[0], g:opt[1])
801
802 " 21d: Setting local string local-global (to window) option"
803 noa set statusline& " Reset global and local value (without triggering autocmd)
804 let g:options=[['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
805 setlocal statusline=baz
806 call assert_equal([], g:options)
807 call assert_equal(g:opt[0], g:opt[1])
808
809 " 21e: Setting again string local-global (to window) option"
810 " Note: v:option_old is the old global value for local-global string options
811 " but the old local value for all other kinds of options.
812 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
813 noa setlocal statusline=baz
814 let g:options=[['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
815 set statusline=foo
816 call assert_equal([], g:options)
817 call assert_equal(g:opt[0], g:opt[1])
818
819
820 " 22a: Setting string local (to window) option"
821 let oldval = &foldignore
822 let g:options=[['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
823 set foldignore=fo
824 call assert_equal([], g:options)
825 call assert_equal(g:opt[0], g:opt[1])
826
827 " 22b: Resetting string local (to window) option"
828 let g:options=[['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
829 set foldignore&
830 call assert_equal([], g:options)
831 call assert_equal(g:opt[0], g:opt[1])
832
833 " 22c: Setting global string local (to window) option"
834 let g:options=[['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
835 setglobal foldignore=bar
836 call assert_equal([], g:options)
837 call assert_equal(g:opt[0], g:opt[1])
838
839 " 22d: Setting local string local (to window) option"
840 noa set foldignore& " Reset global and local value (without triggering autocmd)
841 let g:options=[['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
842 setlocal foldignore=baz
843 call assert_equal([], g:options)
844 call assert_equal(g:opt[0], g:opt[1])
845
846 " 22e: Setting again string local (to window) option"
847 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
848 noa setlocal foldignore=loc
849 let g:options=[['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
850 set foldignore=fo
851 call assert_equal([], g:options)
852 call assert_equal(g:opt[0], g:opt[1])
853
854
855 " 23a: Setting global number local option"
856 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
857 noa setlocal cmdheight=1 " Sets the global(!) value!
858 let g:options=[['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
859 setglobal cmdheight=2
860 call assert_equal([], g:options)
861 call assert_equal(g:opt[0], g:opt[1])
862
863 " 23b: Setting local number global option"
864 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
865 noa setlocal cmdheight=1 " Sets the global(!) value!
866 let g:options=[['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
867 setlocal cmdheight=2
868 call assert_equal([], g:options)
869 call assert_equal(g:opt[0], g:opt[1])
870
871 " 23c: Setting again number global option"
872 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
873 noa setlocal cmdheight=1 " Sets the global(!) value!
874 let g:options=[['cmdheight', '1', '1', '1', '2', 'global', 'set']]
875 set cmdheight=2
876 call assert_equal([], g:options)
877 call assert_equal(g:opt[0], g:opt[1])
878
879 " 23d: Setting again number global option"
880 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
881 let g:options=[['cmdheight', '8', '8', '8', '2', 'global', 'set']]
882 set cmdheight=2
883 call assert_equal([], g:options)
884 call assert_equal(g:opt[0], g:opt[1])
885
886
887 " 24a: Setting global number global-local (to buffer) option"
888 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
889 noa setlocal undolevels=1
890 let g:options=[['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
891 setglobal undolevels=2
892 call assert_equal([], g:options)
893 call assert_equal(g:opt[0], g:opt[1])
894
895 " 24b: Setting local number global-local (to buffer) option"
896 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
897 noa setlocal undolevels=1
898 let g:options=[['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
899 setlocal undolevels=2
900 call assert_equal([], g:options)
901 call assert_equal(g:opt[0], g:opt[1])
902
903 " 24c: Setting again number global-local (to buffer) option"
904 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
905 noa setlocal undolevels=1
906 let g:options=[['undolevels', '1', '1', '8', '2', 'global', 'set']]
907 set undolevels=2
908 call assert_equal([], g:options)
909 call assert_equal(g:opt[0], g:opt[1])
910
911 " 24d: Setting again global number global-local (to buffer) option"
912 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
913 let g:options=[['undolevels', '8', '8', '8', '2', 'global', 'set']]
914 set undolevels=2
915 call assert_equal([], g:options)
916 call assert_equal(g:opt[0], g:opt[1])
917
918
919 " 25a: Setting global number local (to buffer) option"
920 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
921 noa setlocal wrapmargin=1
922 let g:options=[['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
923 setglobal wrapmargin=2
924 call assert_equal([], g:options)
925 call assert_equal(g:opt[0], g:opt[1])
926
927 " 25b: Setting local number local (to buffer) option"
928 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
929 noa setlocal wrapmargin=1
930 let g:options=[['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
931 setlocal wrapmargin=2
932 call assert_equal([], g:options)
933 call assert_equal(g:opt[0], g:opt[1])
934
935 " 25c: Setting again number local (to buffer) option"
936 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
937 noa setlocal wrapmargin=1
938 let g:options=[['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
939 set wrapmargin=2
940 call assert_equal([], g:options)
941 call assert_equal(g:opt[0], g:opt[1])
942
943 " 25d: Setting again global number local (to buffer) option"
944 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
945 let g:options=[['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
946 set wrapmargin=2
947 call assert_equal([], g:options)
948 call assert_equal(g:opt[0], g:opt[1])
949
950
951 " 26: Setting number global-local (to window) option.
952 " Such option does currently not exist.
953
954
955 " 27a: Setting global number local (to window) option"
956 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
957 noa setlocal foldcolumn=1
958 let g:options=[['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
959 setglobal foldcolumn=2
960 call assert_equal([], g:options)
961 call assert_equal(g:opt[0], g:opt[1])
962
963 " 27b: Setting local number local (to window) option"
964 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
965 noa setlocal foldcolumn=1
966 let g:options=[['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
967 setlocal foldcolumn=2
968 call assert_equal([], g:options)
969 call assert_equal(g:opt[0], g:opt[1])
970
971 " 27c: Setting again number local (to window) option"
972 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
973 noa setlocal foldcolumn=1
974 let g:options=[['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
975 set foldcolumn=2
976 call assert_equal([], g:options)
977 call assert_equal(g:opt[0], g:opt[1])
978
979 " 27d: Ssettin again global number local (to window) option"
980 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
981 let g:options=[['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
982 set foldcolumn=2
983 call assert_equal([], g:options)
984 call assert_equal(g:opt[0], g:opt[1])
985
986
987 " 28a: Setting global boolean global option"
988 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
989 noa setlocal wrapscan " Sets the global(!) value!
990 let g:options=[['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
991 setglobal nowrapscan
992 call assert_equal([], g:options)
993 call assert_equal(g:opt[0], g:opt[1])
994
995 " 28b: Setting local boolean global option"
996 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
997 noa setlocal wrapscan " Sets the global(!) value!
998 let g:options=[['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
999 setlocal nowrapscan
1000 call assert_equal([], g:options)
1001 call assert_equal(g:opt[0], g:opt[1])
1002
1003 " 28c: Setting again boolean global option"
1004 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1005 noa setlocal wrapscan " Sets the global(!) value!
1006 let g:options=[['wrapscan', '1', '1', '1', '0', 'global', 'set']]
1007 set nowrapscan
1008 call assert_equal([], g:options)
1009 call assert_equal(g:opt[0], g:opt[1])
1010
1011 " 28d: Setting again global boolean global option"
1012 noa set nowrapscan " Reset global and local value (without triggering autocmd)
1013 let g:options=[['wrapscan', '0', '0', '0', '1', 'global', 'set']]
1014 set wrapscan
1015 call assert_equal([], g:options)
1016 call assert_equal(g:opt[0], g:opt[1])
1017
1018
1019 " 29a: Setting global boolean global-local (to buffer) option"
1020 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1021 noa setlocal autoread
1022 let g:options=[['autoread', '0', '', '0', '1', 'global', 'setglobal']]
1023 setglobal autoread
1024 call assert_equal([], g:options)
1025 call assert_equal(g:opt[0], g:opt[1])
1026
1027 " 29b: Setting local boolean global-local (to buffer) option"
1028 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1029 noa setlocal autoread
1030 let g:options=[['autoread', '1', '1', '', '0', 'local', 'setlocal']]
1031 setlocal noautoread
1032 call assert_equal([], g:options)
1033 call assert_equal(g:opt[0], g:opt[1])
1034
1035 " 29c: Setting again boolean global-local (to buffer) option"
1036 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1037 noa setlocal autoread
1038 let g:options=[['autoread', '1', '1', '0', '1', 'global', 'set']]
1039 set autoread
1040 call assert_equal([], g:options)
1041 call assert_equal(g:opt[0], g:opt[1])
1042
1043 " 29d: Setting again global boolean global-local (to buffer) option"
1044 noa set noautoread " Reset global and local value (without triggering autocmd)
1045 let g:options=[['autoread', '0', '0', '0', '1', 'global', 'set']]
1046 set autoread
1047 call assert_equal([], g:options)
1048 call assert_equal(g:opt[0], g:opt[1])
1049
1050
1051 " 30a: Setting global boolean local (to buffer) option"
1052 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1053 noa setlocal cindent
1054 let g:options=[['cindent', '0', '', '0', '1', 'global', 'setglobal']]
1055 setglobal cindent
1056 call assert_equal([], g:options)
1057 call assert_equal(g:opt[0], g:opt[1])
1058
1059 " 30b: Setting local boolean local (to buffer) option"
1060 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1061 noa setlocal cindent
1062 let g:options=[['cindent', '1', '1', '', '0', 'local', 'setlocal']]
1063 setlocal nocindent
1064 call assert_equal([], g:options)
1065 call assert_equal(g:opt[0], g:opt[1])
1066
1067 " 30c: Setting again boolean local (to buffer) option"
1068 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1069 noa setlocal cindent
1070 let g:options=[['cindent', '1', '1', '0', '1', 'global', 'set']]
1071 set cindent
1072 call assert_equal([], g:options)
1073 call assert_equal(g:opt[0], g:opt[1])
1074
1075 " 30d: Setting again global boolean local (to buffer) option"
1076 noa set nocindent " Reset global and local value (without triggering autocmd)
1077 let g:options=[['cindent', '0', '0', '0', '1', 'global', 'set']]
1078 set cindent
1079 call assert_equal([], g:options)
1080 call assert_equal(g:opt[0], g:opt[1])
1081
1082
1083 " 31: Setting boolean global-local (to window) option
1084 " Currently no such option exists.
1085
1086
1087 " 32a: Setting global boolean local (to window) option"
1088 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1089 noa setlocal cursorcolumn
1090 let g:options=[['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
1091 setglobal cursorcolumn
1092 call assert_equal([], g:options)
1093 call assert_equal(g:opt[0], g:opt[1])
1094
1095 " 32b: Setting local boolean local (to window) option"
1096 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1097 noa setlocal cursorcolumn
1098 let g:options=[['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
1099 setlocal nocursorcolumn
1100 call assert_equal([], g:options)
1101 call assert_equal(g:opt[0], g:opt[1])
1102
1103 " 32c: Setting again boolean local (to window) option"
1104 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1105 noa setlocal cursorcolumn
1106 let g:options=[['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
1107 set cursorcolumn
1108 call assert_equal([], g:options)
1109 call assert_equal(g:opt[0], g:opt[1])
1110
1111 " 32d: Setting again global boolean local (to window) option"
1112 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
1113 let g:options=[['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
1114 set cursorcolumn
1115 call assert_equal([], g:options)
1116 call assert_equal(g:opt[0], g:opt[1])
1117
1118
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001119 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001120 noa set backspace=1 " Reset global and local value (without triggering autocmd)
1121 let g:options=[['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
1122 set backspace=2
1123 call assert_equal([], g:options)
1124 call assert_equal(g:opt[0], g:opt[1])
1125
1126
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001127 " Cleanup
1128 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001129 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001130 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 +02001131 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001132 endfor
1133 call test_override('starting', 0)
1134 delfunc! AutoCommandOptionSet
1135endfunc
1136
1137func Test_OptionSet_diffmode()
1138 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001139 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001140 new
1141 au OptionSet diff :let &l:cul=v:option_new
1142
1143 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1144 call assert_equal(0, &l:cul)
1145 diffthis
1146 call assert_equal(1, &l:cul)
1147
1148 vnew
1149 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1150 call assert_equal(0, &l:cul)
1151 diffthis
1152 call assert_equal(1, &l:cul)
1153
1154 diffoff
1155 call assert_equal(0, &l:cul)
1156 call assert_equal(1, getwinvar(2, '&l:cul'))
1157 bw!
1158
1159 call assert_equal(1, &l:cul)
1160 diffoff!
1161 call assert_equal(0, &l:cul)
1162 call assert_equal(0, getwinvar(1, '&l:cul'))
1163 bw!
1164
1165 " Cleanup
1166 au! OptionSet
1167 call test_override('starting', 0)
1168endfunc
1169
1170func Test_OptionSet_diffmode_close()
1171 call test_override('starting', 1)
1172 " 19: Try to close the current window when entering diff mode
1173 " should not segfault
1174 new
1175 au OptionSet diff close
1176
1177 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001178 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001179 call assert_equal(1, &diff)
1180 vnew
1181 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001182 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001183 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001184 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001185 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001186 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001187 bw!
1188
1189 " Cleanup
1190 au! OptionSet
1191 call test_override('starting', 0)
1192 "delfunc! AutoCommandOptionSet
1193endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001194
1195" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1196func Test_BufleaveWithDelete()
1197 new | edit Xfile1
1198
1199 augroup test_bufleavewithdelete
1200 autocmd!
1201 autocmd BufLeave Xfile1 bwipe Xfile2
1202 augroup END
1203
1204 call assert_fails('edit Xfile2', 'E143:')
1205 call assert_equal('Xfile1', bufname('%'))
1206
1207 autocmd! test_bufleavewithdelete BufLeave Xfile1
1208 augroup! test_bufleavewithdelete
1209
1210 new
1211 bwipe! Xfile1
1212endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001213
1214" Test for autocommand that changes the buffer list, when doing ":ball".
1215func Test_Acmd_BufAll()
1216 enew!
1217 %bwipe!
1218 call writefile(['Test file Xxx1'], 'Xxx1')
1219 call writefile(['Test file Xxx2'], 'Xxx2')
1220 call writefile(['Test file Xxx3'], 'Xxx3')
1221
1222 " Add three files to the buffer list
1223 split Xxx1
1224 close
1225 split Xxx2
1226 close
1227 split Xxx3
1228 close
1229
1230 " Wipe the buffer when the buffer is opened
1231 au BufReadPost Xxx2 bwipe
1232
1233 call append(0, 'Test file Xxx4')
1234 ball
1235
1236 call assert_equal(2, winnr('$'))
1237 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1238 wincmd t
1239
1240 au! BufReadPost
1241 %bwipe!
1242 call delete('Xxx1')
1243 call delete('Xxx2')
1244 call delete('Xxx3')
1245 enew! | only
1246endfunc
1247
1248" Test for autocommand that changes current buffer on BufEnter event.
1249" Check if modelines are interpreted for the correct buffer.
1250func Test_Acmd_BufEnter()
1251 %bwipe!
1252 call writefile(['start of test file Xxx1',
1253 \ "\<Tab>this is a test",
1254 \ 'end of test file Xxx1'], 'Xxx1')
1255 call writefile(['start of test file Xxx2',
1256 \ 'vim: set noai :',
1257 \ "\<Tab>this is a test",
1258 \ 'end of test file Xxx2'], 'Xxx2')
1259
1260 au BufEnter Xxx2 brew
1261 set ai modeline modelines=3
1262 edit Xxx1
1263 " edit Xxx2, autocmd will do :brew
1264 edit Xxx2
1265 exe "normal G?this is a\<CR>"
1266 " Append text with autoindent to this file
1267 normal othis should be auto-indented
1268 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1269 call assert_equal(3, line('.'))
1270 " Remove autocmd and edit Xxx2 again
1271 au! BufEnter Xxx2
1272 buf! Xxx2
1273 exe "normal G?this is a\<CR>"
1274 " append text without autoindent to Xxx
1275 normal othis should be in column 1
1276 call assert_equal("this should be in column 1", getline('.'))
1277 call assert_equal(4, line('.'))
1278
1279 %bwipe!
1280 call delete('Xxx1')
1281 call delete('Xxx2')
1282 set ai&vim modeline&vim modelines&vim
1283endfunc
1284
1285" Test for issue #57
1286" do not move cursor on <c-o> when autoindent is set
1287func Test_ai_CTRL_O()
1288 enew!
1289 set ai
1290 let save_fo = &fo
1291 set fo+=r
1292 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1293 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1294 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1295
1296 set ai&vim
1297 let &fo = save_fo
1298 enew!
1299endfunc
1300
1301" Test for autocommand that deletes the current buffer on BufLeave event.
1302" Also test deleting the last buffer, should give a new, empty buffer.
1303func Test_BufLeave_Wipe()
1304 %bwipe!
1305 let content = ['start of test file Xxx',
1306 \ 'this is a test',
1307 \ 'end of test file Xxx']
1308 call writefile(content, 'Xxx1')
1309 call writefile(content, 'Xxx2')
1310
1311 au BufLeave Xxx2 bwipe
1312 edit Xxx1
1313 split Xxx2
1314 " delete buffer Xxx2, we should be back to Xxx1
1315 bwipe
1316 call assert_equal('Xxx1', bufname('%'))
1317 call assert_equal(1, winnr('$'))
1318
1319 " Create an alternate buffer
1320 %write! test.out
1321 call assert_equal('test.out', bufname('#'))
1322 " delete alternate buffer
1323 bwipe test.out
1324 call assert_equal('Xxx1', bufname('%'))
1325 call assert_equal('', bufname('#'))
1326
1327 au BufLeave Xxx1 bwipe
1328 " delete current buffer, get an empty one
1329 bwipe!
1330 call assert_equal(1, line('$'))
1331 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001332 let g:bufinfo = getbufinfo()
1333 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001334
1335 call delete('Xxx1')
1336 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001337 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001338 %bwipe
1339 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001340
1341 " check that bufinfo doesn't contain a pointer to freed memory
1342 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001343endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001344
1345func Test_QuitPre()
1346 edit Xfoo
1347 let winid = win_getid(winnr())
1348 split Xbar
1349 au! QuitPre * let g:afile = expand('<afile>')
1350 " Close the other window, <afile> should be correct.
1351 exe win_id2win(winid) . 'q'
1352 call assert_equal('Xfoo', g:afile)
1353
1354 unlet g:afile
1355 bwipe Xfoo
1356 bwipe Xbar
1357endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001358
1359func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001360 au! CmdlineChanged : let g:text = getcmdline()
1361 let g:text = 0
1362 call feedkeys(":echom 'hello'\<CR>", 'xt')
1363 call assert_equal("echom 'hello'", g:text)
1364 au! CmdlineChanged
1365
1366 au! CmdlineChanged : let g:entered = expand('<afile>')
1367 let g:entered = 0
1368 call feedkeys(":echom 'hello'\<CR>", 'xt')
1369 call assert_equal(':', g:entered)
1370 au! CmdlineChanged
1371
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001372 au! CmdlineEnter : let g:entered = expand('<afile>')
1373 au! CmdlineLeave : let g:left = expand('<afile>')
1374 let g:entered = 0
1375 let g:left = 0
1376 call feedkeys(":echo 'hello'\<CR>", 'xt')
1377 call assert_equal(':', g:entered)
1378 call assert_equal(':', g:left)
1379 au! CmdlineEnter
1380 au! CmdlineLeave
1381
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001382 let save_shellslash = &shellslash
1383 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001384 au! CmdlineEnter / let g:entered = expand('<afile>')
1385 au! CmdlineLeave / let g:left = expand('<afile>')
1386 let g:entered = 0
1387 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001388 new
1389 call setline(1, 'hello')
1390 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001391 call assert_equal('/', g:entered)
1392 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001393 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001394 au! CmdlineEnter
1395 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001396 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001397endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001398
1399" Test for BufWritePre autocommand that deletes or unloads the buffer.
1400func Test_BufWritePre()
1401 %bwipe
1402 au BufWritePre Xxx1 bunload
1403 au BufWritePre Xxx2 bwipe
1404
1405 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1406 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1407
1408 edit Xtest
1409 e! Xxx2
1410 bdel Xtest
1411 e Xxx1
1412 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001413 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001414 call assert_equal('Xxx2', bufname('%'))
1415 edit Xtest
1416 e! Xxx2
1417 bwipe Xtest
1418 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001419 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001420 call assert_equal('Xxx1', bufname('%'))
1421 au! BufWritePre
1422 call delete('Xxx1')
1423 call delete('Xxx2')
1424endfunc
1425
1426" Test for BufUnload autocommand that unloads all the other buffers
1427func Test_bufunload_all()
1428 call writefile(['Test file Xxx1'], 'Xxx1')"
1429 call writefile(['Test file Xxx2'], 'Xxx2')"
1430
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001431 let content =<< trim [CODE]
1432 func UnloadAllBufs()
1433 let i = 1
1434 while i <= bufnr('$')
1435 if i != bufnr('%') && bufloaded(i)
1436 exe i . 'bunload'
1437 endif
1438 let i += 1
1439 endwhile
1440 endfunc
1441 au BufUnload * call UnloadAllBufs()
1442 au VimLeave * call writefile(['Test Finished'], 'Xout')
1443 edit Xxx1
1444 split Xxx2
1445 q
1446 [CODE]
1447
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001448 call writefile(content, 'Xtest')
1449
1450 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001451 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001452 call assert_true(filereadable('Xout'))
1453
1454 call delete('Xxx1')
1455 call delete('Xxx2')
1456 call delete('Xtest')
1457 call delete('Xout')
1458endfunc
1459
1460" Some tests for buffer-local autocommands
1461func Test_buflocal_autocmd()
1462 let g:bname = ''
1463 edit xx
1464 au BufLeave <buffer> let g:bname = expand("%")
1465 " here, autocommand for xx should trigger.
1466 " but autocommand shall not apply to buffer named <buffer>.
1467 edit somefile
1468 call assert_equal('xx', g:bname)
1469 let g:bname = ''
1470 " here, autocommand shall be auto-deleted
1471 bwipe xx
1472 " autocmd should not trigger
1473 edit xx
1474 call assert_equal('', g:bname)
1475 " autocmd should not trigger
1476 edit somefile
1477 call assert_equal('', g:bname)
1478 enew
1479 unlet g:bname
1480endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001481
1482" Test for "*Cmd" autocommands
1483func Test_Cmd_Autocmds()
1484 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1485
1486 enew!
1487 au BufReadCmd XtestA 0r Xxx|$del
1488 edit XtestA " will read text of Xxd instead
1489 call assert_equal('start of Xxx', getline(1))
1490
1491 au BufWriteCmd XtestA call append(line("$"), "write")
1492 write " will append a line to the file
1493 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001494 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001495 call assert_equal('write', getline(4))
1496
1497 " now we have:
1498 " 1 start of Xxx
1499 " 2 abc2
1500 " 3 end of Xxx
1501 " 4 write
1502
1503 au FileReadCmd XtestB '[r Xxx
1504 2r XtestB " will read Xxx below line 2 instead
1505 call assert_equal('start of Xxx', getline(3))
1506
1507 " now we have:
1508 " 1 start of Xxx
1509 " 2 abc2
1510 " 3 start of Xxx
1511 " 4 abc2
1512 " 5 end of Xxx
1513 " 6 end of Xxx
1514 " 7 write
1515
1516 au FileWriteCmd XtestC '[,']copy $
1517 normal 4GA1
1518 4,5w XtestC " will copy lines 4 and 5 to the end
1519 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001520 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001521 call assert_equal("end of Xxx", getline(9))
1522
1523 " now we have:
1524 " 1 start of Xxx
1525 " 2 abc2
1526 " 3 start of Xxx
1527 " 4 abc21
1528 " 5 end of Xxx
1529 " 6 end of Xxx
1530 " 7 write
1531 " 8 abc21
1532 " 9 end of Xxx
1533
1534 let g:lines = []
1535 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1536 w >>XtestD " will add lines to 'lines'
1537 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001538 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001539 call assert_equal(9, line('$'))
1540 call assert_equal('end of Xxx', getline('$'))
1541
1542 au BufReadCmd XtestE 0r Xxx|$del
1543 sp XtestE " split window with test.out
1544 call assert_equal('end of Xxx', getline(3))
1545
1546 let g:lines = []
1547 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1548 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1549 wall " will write other window to 'lines'
1550 call assert_equal(4, len(g:lines), g:lines)
1551 call assert_equal('asdf', g:lines[2])
1552
1553 au! BufReadCmd
1554 au! BufWriteCmd
1555 au! FileReadCmd
1556 au! FileWriteCmd
1557 au! FileAppendCmd
1558 %bwipe!
1559 call delete('Xxx')
1560 enew!
1561endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001562
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001563func s:ReadFile()
1564 setl noswapfile nomodified
1565 let filename = resolve(expand("<afile>:p"))
1566 execute 'read' fnameescape(filename)
1567 1d_
1568 exe 'file' fnameescape(filename)
1569 setl buftype=acwrite
1570endfunc
1571
1572func s:WriteFile()
1573 let filename = resolve(expand("<afile>:p"))
1574 setl buftype=
1575 noautocmd execute 'write' fnameescape(filename)
1576 setl buftype=acwrite
1577 setl nomodified
1578endfunc
1579
1580func Test_BufReadCmd()
1581 autocmd BufReadCmd *.test call s:ReadFile()
1582 autocmd BufWriteCmd *.test call s:WriteFile()
1583
1584 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1585 edit Xcmd.test
1586 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1587 normal! Gofour
1588 write
1589 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1590
1591 bwipe!
1592 call delete('Xcmd.test')
1593 au! BufReadCmd
1594 au! BufWriteCmd
1595endfunc
1596
Bram Moolenaaraace2152017-11-05 16:23:10 +01001597func SetChangeMarks(start, end)
1598 exe a:start. 'mark ['
1599 exe a:end. 'mark ]'
1600endfunc
1601
1602" Verify the effects of autocmds on '[ and ']
1603func Test_change_mark_in_autocmds()
1604 edit! Xtest
1605 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u", 'xtn')
1606
1607 call SetChangeMarks(2, 3)
1608 write
1609 call assert_equal([1, 4], [line("'["), line("']")])
1610
1611 call SetChangeMarks(2, 3)
1612 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1613 write
1614 au! BufWritePre
1615
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001616 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001617 write XtestFilter
1618 write >> XtestFilter
1619
1620 call SetChangeMarks(2, 3)
1621 " Marks are set to the entire range of the write
1622 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1623 " '[ is adjusted to just before the line that will receive the filtered
1624 " data
1625 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1626 " The filtered data is read into the buffer, and the source lines are
1627 " still present, so the range is after the source lines
1628 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1629 %!cat XtestFilter
1630 " After the filtered data is read, the original lines are deleted
1631 call assert_equal([1, 8], [line("'["), line("']")])
1632 au! FilterWritePre,FilterReadPre,FilterReadPost
1633 undo
1634
1635 call SetChangeMarks(1, 4)
1636 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1637 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1638 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1639 2,3!cat XtestFilter
1640 call assert_equal([2, 9], [line("'["), line("']")])
1641 au! FilterWritePre,FilterReadPre,FilterReadPost
1642 undo
1643
1644 call delete('XtestFilter')
1645 endif
1646
1647 call SetChangeMarks(1, 4)
1648 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1649 2,3write Xtest2
1650 au! FileWritePre
1651
1652 call SetChangeMarks(2, 3)
1653 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1654 write >> Xtest2
1655 au! FileAppendPre
1656
1657 call SetChangeMarks(1, 4)
1658 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1659 2,3write >> Xtest2
1660 au! FileAppendPre
1661
1662 call SetChangeMarks(1, 1)
1663 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1664 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1665 3read Xtest2
1666 au! FileReadPre,FileReadPost
1667 undo
1668
1669 call SetChangeMarks(4, 4)
1670 " When the line is 0, it's adjusted to 1
1671 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1672 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1673 0read Xtest2
1674 au! FileReadPre,FileReadPost
1675 undo
1676
1677 call SetChangeMarks(4, 4)
1678 " When the line is 0, it's adjusted to 1
1679 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1680 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1681 1read Xtest2
1682 au! FileReadPre,FileReadPost
1683 undo
1684
1685 bwipe!
1686 call delete('Xtest')
1687 call delete('Xtest2')
1688endfunc
1689
1690func Test_Filter_noshelltemp()
1691 if !executable('cat')
1692 return
1693 endif
1694
1695 enew!
1696 call setline(1, ['a', 'b', 'c', 'd'])
1697
1698 let shelltemp = &shelltemp
1699 set shelltemp
1700
1701 let g:filter_au = 0
1702 au FilterWritePre * let g:filter_au += 1
1703 au FilterReadPre * let g:filter_au += 1
1704 au FilterReadPost * let g:filter_au += 1
1705 %!cat
1706 call assert_equal(3, g:filter_au)
1707
1708 if has('filterpipe')
1709 set noshelltemp
1710
1711 let g:filter_au = 0
1712 au FilterWritePre * let g:filter_au += 1
1713 au FilterReadPre * let g:filter_au += 1
1714 au FilterReadPost * let g:filter_au += 1
1715 %!cat
1716 call assert_equal(0, g:filter_au)
1717 endif
1718
1719 au! FilterWritePre,FilterReadPre,FilterReadPost
1720 let &shelltemp = shelltemp
1721 bwipe!
1722endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001723
1724func Test_TextYankPost()
1725 enew!
1726 call setline(1, ['foo'])
1727
1728 let g:event = []
1729 au TextYankPost * let g:event = copy(v:event)
1730
1731 call assert_equal({}, v:event)
1732 call assert_fails('let v:event = {}', 'E46:')
1733 call assert_fails('let v:event.mykey = 0', 'E742:')
1734
1735 norm "ayiw
1736 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001737 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001738 \g:event)
1739 norm y_
1740 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001741 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false},
1742 \g:event)
1743 norm Vy
1744 call assert_equal(
1745 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001746 \g:event)
1747 call feedkeys("\<C-V>y", 'x')
1748 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001749 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001750 \g:event)
1751 norm "xciwbar
1752 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001753 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001754 \g:event)
1755 norm "bdiw
1756 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001757 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001758 \g:event)
1759
1760 call assert_equal({}, v:event)
1761
1762 au! TextYankPost
1763 unlet g:event
1764 bwipe!
1765endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001766
1767func Test_nocatch_wipe_all_buffers()
1768 " Real nasty autocommand: wipe all buffers on any event.
1769 au * * bwipe *
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001770 call assert_fails('next x', ['E94:', 'E517:'])
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001771 bwipe
1772 au!
1773endfunc
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001774
1775func Test_nocatch_wipe_dummy_buffer()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001776 CheckFeature quickfix
1777 " Nasty autocommand: wipe buffer on any event.
1778 au * x bwipe
Bram Moolenaare2e40752020-09-04 21:18:46 +02001779 call assert_fails('lv½ /x', 'E937:')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001780 au!
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001781endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001782
1783function s:Before_test_dirchanged()
1784 augroup test_dirchanged
1785 autocmd!
1786 augroup END
1787 let s:li = []
1788 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001789 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001790 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001791 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001792 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001793endfunc
1794
1795function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001796 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001797 call delete(s:dir_foo, 'd')
1798 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001799 augroup test_dirchanged
1800 autocmd!
1801 augroup END
1802endfunc
1803
1804function Test_dirchanged_global()
1805 call s:Before_test_dirchanged()
1806 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1807 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001808 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001809 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001810 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001811 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001812 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001813 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001814 call s:After_test_dirchanged()
1815endfunc
1816
1817function Test_dirchanged_local()
1818 call s:Before_test_dirchanged()
1819 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1820 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001821 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001822 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001823 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001824 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001825 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001826 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001827 call s:After_test_dirchanged()
1828endfunc
1829
1830function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001831 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001832 call s:Before_test_dirchanged()
1833 call test_autochdir()
1834 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1835 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1836 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001837 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001838 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001839 exe 'edit ' . s:dir_foo . '/Xfile'
1840 call assert_equal(s:dir_foo, getcwd())
1841 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001842 set noacd
1843 bwipe!
1844 call s:After_test_dirchanged()
1845endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001846
1847" Test TextChangedI and TextChangedP
1848func Test_ChangedP()
1849 new
1850 call setline(1, ['foo', 'bar', 'foobar'])
1851 call test_override("char_avail", 1)
1852 set complete=. completeopt=menuone
1853
1854 func! TextChangedAutocmd(char)
1855 let g:autocmd .= a:char
1856 endfunc
1857
1858 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1859 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1860 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1861
1862 call cursor(3, 1)
1863 let g:autocmd = ''
1864 call feedkeys("o\<esc>", 'tnix')
1865 call assert_equal('I', g:autocmd)
1866
1867 let g:autocmd = ''
1868 call feedkeys("Sf", 'tnix')
1869 call assert_equal('II', g:autocmd)
1870
1871 let g:autocmd = ''
1872 call feedkeys("Sf\<C-N>", 'tnix')
1873 call assert_equal('IIP', g:autocmd)
1874
1875 let g:autocmd = ''
1876 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1877 call assert_equal('IIPP', g:autocmd)
1878
1879 let g:autocmd = ''
1880 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1881 call assert_equal('IIPPP', g:autocmd)
1882
1883 let g:autocmd = ''
1884 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1885 call assert_equal('IIPPPP', g:autocmd)
1886
1887 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1888 " TODO: how should it handle completeopt=noinsert,noselect?
1889
1890 " CleanUp
1891 call test_override("char_avail", 0)
1892 au! TextChanged
1893 au! TextChangedI
1894 au! TextChangedP
1895 delfu TextChangedAutocmd
1896 unlet! g:autocmd
1897 set complete&vim completeopt&vim
1898
1899 bw!
1900endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001901
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001902let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001903func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001904 if !g:setline_handled
1905 call setline(1, "(x)")
1906 let g:setline_handled = v:true
1907 endif
1908endfunc
1909
1910func Test_TextChangedI_with_setline()
1911 new
1912 call test_override('char_avail', 1)
1913 autocmd TextChangedI <buffer> call SetLineOne()
1914 call feedkeys("i(\<CR>\<Esc>", 'tx')
1915 call assert_equal('(', getline(1))
1916 call assert_equal('x)', getline(2))
1917 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001918 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001919 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001920
1921 call test_override('starting', 0)
1922 bwipe!
1923endfunc
1924
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001925func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001926 CheckFeature terminal
1927 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01001928 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01001929 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001930
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001931 " Prepare file for TextChanged event.
1932 call writefile([''], 'Xchanged.txt')
1933 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1934 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001935 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001936 " In ConPTY, there is additional character which is drawn up to the width of
1937 " the screen.
1938 if has('conpty')
1939 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
1940 else
1941 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
1942 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001943 " It's only adding autocmd, so that no event occurs.
1944 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1945 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001946 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001947 call assert_equal([''], readfile('Xchanged.txt'))
1948
1949 " clean up
1950 call delete('Xchanged.txt')
1951 bwipe!
1952endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01001953
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001954func Test_autocmd_nested()
1955 let g:did_nested = 0
1956 augroup Testing
1957 au WinNew * edit somefile
1958 au BufNew * let g:did_nested = 1
1959 augroup END
1960 split
1961 call assert_equal(0, g:did_nested)
1962 close
1963 bwipe! somefile
1964
1965 " old nested argument still works
1966 augroup Testing
1967 au!
1968 au WinNew * nested edit somefile
1969 au BufNew * let g:did_nested = 1
1970 augroup END
1971 split
1972 call assert_equal(1, g:did_nested)
1973 close
1974 bwipe! somefile
1975
1976 " New ++nested argument works
1977 augroup Testing
1978 au!
1979 au WinNew * ++nested edit somefile
1980 au BufNew * let g:did_nested = 1
1981 augroup END
1982 split
1983 call assert_equal(1, g:did_nested)
1984 close
1985 bwipe! somefile
1986
1987 augroup Testing
1988 au!
1989 augroup END
1990
1991 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
1992 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
1993endfunc
1994
1995func Test_autocmd_once()
1996 " Without ++once WinNew triggers twice
1997 let g:did_split = 0
1998 augroup Testing
1999 au WinNew * let g:did_split += 1
2000 augroup END
2001 split
2002 split
2003 call assert_equal(2, g:did_split)
2004 call assert_true(exists('#WinNew'))
2005 close
2006 close
2007
2008 " With ++once WinNew triggers once
2009 let g:did_split = 0
2010 augroup Testing
2011 au!
2012 au WinNew * ++once let g:did_split += 1
2013 augroup END
2014 split
2015 split
2016 call assert_equal(1, g:did_split)
2017 call assert_false(exists('#WinNew'))
2018 close
2019 close
2020
2021 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2022endfunc
2023
Bram Moolenaara68e5952019-04-25 22:22:01 +02002024func Test_autocmd_bufreadpre()
2025 new
2026 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002027 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002028 w! XAutocmdBufReadPre.txt
2029 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002030 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002031 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002032 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002033 wincmd p
2034 let g:wsv1 = winsaveview()
2035 wincmd p
2036 let g:wsv2 = winsaveview()
2037 " triggers BufReadPre, should not move the cursor in either window
2038 " The topline may change one line in a large window.
2039 edit
2040 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2041 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2042 call assert_equal(2, b:bufreadpre)
2043 wincmd p
2044 call assert_equal(g:wsv1.topline, winsaveview().topline)
2045 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2046 call assert_equal(2, b:bufreadpre)
2047 " Now set the cursor position in an BufReadPre autocommand
2048 " (even though the position will be invalid, this should make Vim reset the
2049 " cursor position in the other window.
2050 wincmd p
2051 set cpo+=g
2052 " won't do anything, but try to set the cursor on an invalid lnum
2053 autocmd BufReadPre <buffer> :norm! 70gg
2054 " triggers BufReadPre, should not move the cursor in either window
2055 e
2056 call assert_equal(1, winsaveview().topline)
2057 call assert_equal(1, winsaveview().lnum)
2058 call assert_equal(3, b:bufreadpre)
2059 wincmd p
2060 call assert_equal(g:wsv1.topline, winsaveview().topline)
2061 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2062 call assert_equal(3, b:bufreadpre)
2063 close
2064 close
2065 call delete('XAutocmdBufReadPre.txt')
2066 set cpo-=g
2067endfunc
2068
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002069" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002070
2071" Tests for the following autocommands:
2072" - FileWritePre writing a compressed file
2073" - FileReadPost reading a compressed file
2074" - BufNewFile reading a file template
2075" - BufReadPre decompressing the file to be read
2076" - FilterReadPre substituting characters in the temp file
2077" - FilterReadPost substituting characters after filtering
2078" - FileReadPre set options for decompression
2079" - FileReadPost decompress the file
2080func Test_ReadWrite_Autocmds()
2081 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002082 CheckUnix
2083 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002084
2085 " Make $GZIP empty, "-v" would cause trouble.
2086 let $GZIP = ""
2087
2088 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2089 " being modified outside of Vim (noticed on Solaris).
2090 au FileChangedShell * echo 'caught FileChangedShell'
2091
2092 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2093 augroup Test1
2094 au!
2095 au FileWritePre *.gz '[,']!gzip
2096 au FileWritePost *.gz undo
2097 au FileReadPost *.gz '[,']!gzip -d
2098 augroup END
2099
2100 new
2101 set bin
2102 call append(0, [
2103 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2104 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2105 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2106 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2107 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2108 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2109 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2110 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2111 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2112 \ ])
2113 1,9write! Xtestfile.gz
2114 enew! | close
2115
2116 new
2117 " Read and decompress the testfile
2118 0read Xtestfile.gz
2119 call assert_equal([
2120 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2121 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2122 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2123 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2124 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2125 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2126 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2127 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2128 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2129 \ ], getline(1, 9))
2130 enew! | close
2131
2132 augroup Test1
2133 au!
2134 augroup END
2135
2136 " Test for the FileAppendPre and FileAppendPost autocmds
2137 augroup Test2
2138 au!
2139 au BufNewFile *.c read Xtest.c
2140 au FileAppendPre *.out '[,']s/new/NEW/
2141 au FileAppendPost *.out !cat Xtest.c >> test.out
2142 augroup END
2143
2144 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2145 new foo.c " should load Xtest.c
2146 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2147 w! >> test.out " append it to the output file
2148
2149 let contents = readfile('test.out')
2150 call assert_equal(' * Here is a NEW .c file', contents[2])
2151 call assert_equal(' * Here is a new .c file', contents[5])
2152
2153 call delete('test.out')
2154 enew! | close
2155 augroup Test2
2156 au!
2157 augroup END
2158
2159 " Test for the BufReadPre and BufReadPost autocmds
2160 augroup Test3
2161 au!
2162 " setup autocommands to decompress before reading and re-compress
2163 " afterwards
2164 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2165 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2166 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2167 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2168 augroup END
2169
2170 e! Xtestfile.gz " Edit compressed file
2171 call assert_equal([
2172 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2173 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2174 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2175 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2176 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2177 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2178 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2179 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2180 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2181 \ ], getline(1, 9))
2182
2183 w! >> test.out " Append it to the output file
2184
2185 augroup Test3
2186 au!
2187 augroup END
2188
2189 " Test for the FilterReadPre and FilterReadPost autocmds.
2190 set shelltemp " need temp files here
2191 augroup Test4
2192 au!
2193 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2194 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2195 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2196 au FilterReadPost *.out '[,']s/x/X/g
2197 augroup END
2198
2199 e! test.out " Edit the output file
2200 1,$!cat
2201 call assert_equal([
2202 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2203 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2204 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2205 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2206 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2207 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2208 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2209 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2210 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2211 \ ], getline(1, 9))
2212 call assert_equal([
2213 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2214 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2215 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2216 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2217 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2218 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2219 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2220 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2221 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2222 \ ], readfile('test.out'))
2223
2224 augroup Test4
2225 au!
2226 augroup END
2227 set shelltemp&vim
2228
2229 " Test for the FileReadPre and FileReadPost autocmds.
2230 augroup Test5
2231 au!
2232 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2233 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2234 au FileReadPost *.gz '[,']s/l/L/
2235 augroup END
2236
2237 new
2238 0r Xtestfile.gz " Read compressed file
2239 call assert_equal([
2240 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2241 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2242 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2243 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2244 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2245 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2246 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2247 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2248 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2249 \ ], getline(1, 9))
2250 call assert_equal([
2251 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2252 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2253 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2254 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2255 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2256 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2257 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2258 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2259 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2260 \ ], readfile('Xtestfile.gz'))
2261
2262 augroup Test5
2263 au!
2264 augroup END
2265
2266 au! FileChangedShell
2267 call delete('Xtestfile.gz')
2268 call delete('Xtest.c')
2269 call delete('test.out')
2270endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002271
2272func Test_throw_in_BufWritePre()
2273 new
2274 call setline(1, ['one', 'two', 'three'])
2275 call assert_false(filereadable('Xthefile'))
2276 augroup throwing
2277 au BufWritePre X* throw 'do not write'
2278 augroup END
2279 try
2280 w Xthefile
2281 catch
2282 let caught = 1
2283 endtry
2284 call assert_equal(1, caught)
2285 call assert_false(filereadable('Xthefile'))
2286
2287 bwipe!
2288 au! throwing
2289endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002290
2291func Test_autocmd_SafeState()
2292 CheckRunVimInTerminal
2293
2294 let lines =<< trim END
2295 let g:safe = 0
2296 let g:again = ''
2297 au SafeState * let g:safe += 1
2298 au SafeStateAgain * let g:again ..= 'x'
2299 func CallTimer()
2300 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2301 endfunc
2302 END
2303 call writefile(lines, 'XSafeState')
2304 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2305
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002306 " Sometimes we loop to handle a K_IGNORE, SafeState may be trigered once or
2307 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002308 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002309 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002310
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002311 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002312 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002313 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002314
2315 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002316 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002317 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002318 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002319 call term_sendkeys(buf, ":echo g:again\<CR>")
2320 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2321
2322 call StopVimInTerminal(buf)
2323 call delete('XSafeState')
2324endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002325
2326func Test_autocmd_CmdWinEnter()
2327 CheckRunVimInTerminal
2328 " There is not cmdwin switch, so
2329 " test for cmdline_hist
2330 " (both are available with small builds)
2331 CheckFeature cmdline_hist
2332 let lines =<< trim END
2333 let b:dummy_var = 'This is a dummy'
2334 autocmd CmdWinEnter * quit
2335 let winnr = winnr('$')
2336 END
2337 let filename='XCmdWinEnter'
2338 call writefile(lines, filename)
2339 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2340
2341 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002342 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002343 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002344 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002345 call term_sendkeys(buf, ":echo &buftype\<cr>")
2346 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2347 call term_sendkeys(buf, ":echo winnr\<cr>")
2348 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2349
2350 " clean up
2351 call StopVimInTerminal(buf)
2352 call delete(filename)
2353endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002354
2355func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002356 CheckFeature quickfix
2357
Bram Moolenaarec66c412019-10-11 21:19:13 +02002358 pedit xx
2359 n x
2360 au WinEnter * quit
2361 split
2362 au! WinEnter
2363endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002364
2365func Test_BufWrite_lockmarks()
2366 edit! Xtest
2367 call setline(1, ['a', 'b', 'c', 'd'])
2368
2369 " :lockmarks preserves the marks
2370 call SetChangeMarks(2, 3)
2371 lockmarks write
2372 call assert_equal([2, 3], [line("'["), line("']")])
2373
2374 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2375 " original values for the user
2376 augroup lockmarks
2377 au!
2378 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2379 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2380 augroup END
2381
2382 lockmarks write
2383 call assert_equal([2, 3], [line("'["), line("']")])
2384
2385 if executable('cat')
2386 lockmarks %!cat
2387 call assert_equal([2, 3], [line("'["), line("']")])
2388 endif
2389
2390 lockmarks 3,4write Xtest2
2391 call assert_equal([2, 3], [line("'["), line("']")])
2392
2393 au! lockmarks
2394 augroup! lockmarks
2395 call delete('Xtest')
2396 call delete('Xtest2')
2397endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002398
2399func Test_FileType_spell()
2400 if !isdirectory('/tmp')
2401 throw "Skipped: requires /tmp directory"
2402 endif
2403
2404 " this was crashing with an invalid free()
2405 setglobal spellfile=/tmp/en.utf-8.add
2406 augroup crash
2407 autocmd!
2408 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2409 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2410 autocmd FileType anotherfiletype setlocal spell
2411 augroup END
2412 func! NoCrash() abort
2413 edit /tmp/crashfile
2414 endfunc
2415 call NoCrash()
2416
2417 au! crash
2418 setglobal spellfile=
2419endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002420
Bram Moolenaar406cd902020-02-18 21:54:41 +01002421" Test closing a window or editing another buffer from a FileChangedRO handler
2422" in a readonly buffer
2423func Test_FileChangedRO_winclose()
2424 augroup FileChangedROTest
2425 au!
2426 autocmd FileChangedRO * quit
2427 augroup END
2428 new
2429 set readonly
2430 call assert_fails('normal i', 'E788:')
2431 close
2432 augroup! FileChangedROTest
2433
2434 augroup FileChangedROTest
2435 au!
2436 autocmd FileChangedRO * edit Xfile
2437 augroup END
2438 new
2439 set readonly
2440 call assert_fails('normal i', 'E788:')
2441 close
2442 augroup! FileChangedROTest
2443endfunc
2444
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002445func LogACmd()
2446 call add(g:logged, line('$'))
2447endfunc
2448
2449func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002450 CheckNotGui
2451
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002452 enew!
2453 tabnew
2454 call setline(1, ['a', 'b', 'c', 'd'])
2455 $
2456 au TermChanged * call LogACmd()
2457 let g:logged = []
2458 let term_save = &term
2459 set term=xterm
2460 call assert_equal([1, 4], g:logged)
2461
2462 au! TermChanged
2463 let &term = term_save
2464 bwipe!
2465endfunc
2466
Bram Moolenaare3284872020-03-19 13:55:03 +01002467" Test for FileReadCmd autocmd
2468func Test_autocmd_FileReadCmd()
2469 func ReadFileCmd()
2470 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2471 endfunc
2472 augroup FileReadCmdTest
2473 au!
2474 au FileReadCmd Xtest call ReadFileCmd()
2475 augroup END
2476
2477 new
2478 read ++bin Xtest
2479 read ++nobin Xtest
2480 read ++edit Xtest
2481 read ++bad=keep Xtest
2482 read ++bad=drop Xtest
2483 read ++bad=- Xtest
2484 read ++ff=unix Xtest
2485 read ++ff=dos Xtest
2486 read ++ff=mac Xtest
2487 read ++enc=utf-8 Xtest
2488
2489 call assert_equal(['',
2490 \ 'v:cmdarg = ++bin',
2491 \ 'v:cmdarg = ++nobin',
2492 \ 'v:cmdarg = ++edit',
2493 \ 'v:cmdarg = ++bad=keep',
2494 \ 'v:cmdarg = ++bad=drop',
2495 \ 'v:cmdarg = ++bad=-',
2496 \ 'v:cmdarg = ++ff=unix',
2497 \ 'v:cmdarg = ++ff=dos',
2498 \ 'v:cmdarg = ++ff=mac',
2499 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2500
2501 close!
2502 augroup FileReadCmdTest
2503 au!
2504 augroup END
2505 delfunc ReadFileCmd
2506endfunc
2507
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002508" Test for passing invalid arguments to autocmd
2509func Test_autocmd_invalid_args()
2510 " Additional character after * for event
2511 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2512 augroup Test
2513 augroup END
2514 " Invalid autocmd event
2515 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2516 " Invalid autocmd event in a autocmd group
2517 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2518 augroup! Test
2519 " Execute all autocmds
2520 call assert_fails('doautocmd * BufEnter', 'E217:')
2521 call assert_fails('augroup! x1a2b3', 'E367:')
2522 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002523 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002524endfunc
2525
2526" Test for deep nesting of autocmds
2527func Test_autocmd_deep_nesting()
2528 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2529 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2530 autocmd! BufEnter Xfile
2531endfunc
2532
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002533" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2534func Test_autocmd_sigusr1()
2535 CheckUnix
2536
2537 let g:sigusr1_passed = 0
2538 au SigUSR1 * let g:sigusr1_passed = 1
2539 call system('/bin/kill -s usr1 ' . getpid())
2540 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2541
2542 au! SigUSR1
2543 unlet g:sigusr1_passed
2544endfunc
2545
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002546" Test for BufReadPre autocmd deleting the file
2547func Test_BufReadPre_delfile()
2548 augroup TestAuCmd
2549 au!
2550 autocmd BufReadPre Xfile call delete('Xfile')
2551 augroup END
2552 call writefile([], 'Xfile')
2553 call assert_fails('new Xfile', 'E200:')
2554 call assert_equal('Xfile', @%)
2555 call assert_equal(1, &readonly)
2556 call delete('Xfile')
2557 augroup TestAuCmd
2558 au!
2559 augroup END
2560 close!
2561endfunc
2562
2563" Test for BufReadPre autocmd changing the current buffer
2564func Test_BufReadPre_changebuf()
2565 augroup TestAuCmd
2566 au!
2567 autocmd BufReadPre Xfile edit Xsomeotherfile
2568 augroup END
2569 call writefile([], 'Xfile')
2570 call assert_fails('new Xfile', 'E201:')
2571 call assert_equal('Xsomeotherfile', @%)
2572 call assert_equal(1, &readonly)
2573 call delete('Xfile')
2574 augroup TestAuCmd
2575 au!
2576 augroup END
2577 close!
2578endfunc
2579
2580" Test for BufWipeouti autocmd changing the current buffer when reading a file
2581" in an empty buffer with 'f' flag in 'cpo'
2582func Test_BufDelete_changebuf()
2583 new
2584 augroup TestAuCmd
2585 au!
2586 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2587 augroup END
2588 let save_cpo = &cpo
2589 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002590 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002591 call assert_equal('somefile', @%)
2592 let &cpo = save_cpo
2593 augroup TestAuCmd
2594 au!
2595 augroup END
2596 close!
2597endfunc
2598
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002599" Test for the temporary internal window used to execute autocmds
2600func Test_autocmd_window()
2601 %bw!
2602 edit one.txt
2603 tabnew two.txt
2604 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002605 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002606 au!
2607 au BufEnter * call add(g:blist, [expand('<afile>'),
2608 \ win_gettype(bufwinnr(expand('<afile>')))])
2609 augroup END
2610
2611 doautoall BufEnter
Bram Moolenaar40a019f2020-06-17 21:41:35 +02002612 call assert_equal([['one.txt', 'autocmd'], ['two.txt', '']], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002613
Bram Moolenaar832adf92020-06-25 19:01:36 +02002614 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002615 au!
2616 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002617 augroup! aucmd_win_test1
2618 %bw!
2619endfunc
2620
2621" Test for trying to close the temporary window used for executing an autocmd
2622func Test_close_autocmd_window()
2623 %bw!
2624 edit one.txt
2625 tabnew two.txt
2626 augroup aucmd_win_test2
2627 au!
2628 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2629 augroup END
2630
2631 call assert_fails('doautoall BufEnter', 'E813:')
2632
2633 augroup aucmd_win_test2
2634 au!
2635 augroup END
2636 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002637 %bwipe!
2638endfunc
2639
2640" Test for trying to close the tab that has the temporary window for exeucing
2641" an autocmd.
2642func Test_close_autocmd_tab()
2643 edit one.txt
2644 tabnew two.txt
2645 augroup aucmd_win_test
2646 au!
2647 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2648 augroup END
2649
2650 call assert_fails('doautoall BufEnter', 'E813:')
2651
2652 tabonly
2653 augroup aucmd_win_test
2654 au!
2655 augroup END
2656 augroup! aucmd_win_test
2657 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002658endfunc
2659
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002660" vim: shiftwidth=2 sts=2 expandtab