blob: c167ed951b537dc64c643df6449046f33e292314 [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 Moolenaarc67e8922016-05-24 16:07:40 +020022if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020023
Bram Moolenaarc67e8922016-05-24 16:07:40 +020024 func ExitInsertMode(id)
25 call feedkeys("\<Esc>")
26 endfunc
27
28 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020029 " Need to move the cursor.
30 call feedkeys("ggG", "xt")
31
Bram Moolenaarc67e8922016-05-24 16:07:40 +020032 let g:triggered = 0
33 au CursorHoldI * let g:triggered += 1
34 set updatetime=20
35 call timer_start(100, 'ExitInsertMode')
36 call feedkeys('a', 'x!')
37 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010038 unlet g:triggered
39 au! CursorHoldI
40 set updatetime&
41 endfunc
42
43 func Test_cursorhold_insert_with_timer_interrupt()
44 if !has('job')
45 return
46 endif
47 " Need to move the cursor.
48 call feedkeys("ggG", "xt")
49
50 " Confirm the timer invoked in exit_cb of the job doesn't disturb
51 " CursorHoldI event.
52 let g:triggered = 0
53 au CursorHoldI * let g:triggered += 1
54 set updatetime=500
55 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar8d4ce562019-01-30 22:01:40 +010056 \ {'exit_cb': {-> timer_start(1000, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010057 call feedkeys('a', 'x!')
58 call assert_equal(1, g:triggered)
59 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020060 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020061 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020062 endfunc
63
64 func Test_cursorhold_insert_ctrl_x()
65 let g:triggered = 0
66 au CursorHoldI * let g:triggered += 1
67 set updatetime=20
68 call timer_start(100, 'ExitInsertMode')
69 " CursorHoldI does not trigger after CTRL-X
70 call feedkeys("a\<C-X>", 'x!')
71 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010072 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020073 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020074 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020075 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +020076
77 func Test_OptionSet_modeline()
78 call test_override('starting', 1)
79 au! OptionSet
80 augroup set_tabstop
81 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
82 augroup END
83 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline')
84 set modeline
85 let v:errmsg = ''
86 call assert_fails('split XoptionsetModeline', 'E12:')
87 call assert_equal(7, &ts)
88 call assert_equal('', v:errmsg)
89
90 augroup set_tabstop
91 au!
92 augroup END
93 bwipe!
94 set ts&
95 call delete('XoptionsetModeline')
96 call test_override('starting', 0)
97 endfunc
98
99endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200100
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200101func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200102 augroup test_bufunload_group
103 autocmd!
104 autocmd BufUnload * call add(s:li, "bufunload")
105 autocmd BufDelete * call add(s:li, "bufdelete")
106 autocmd BufWipeout * call add(s:li, "bufwipeout")
107 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200108
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200109 let s:li=[]
110 new
111 setlocal bufhidden=
112 bunload
113 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200114
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200115 let s:li=[]
116 new
117 setlocal bufhidden=delete
118 bunload
119 call assert_equal(["bufunload", "bufdelete"], s:li)
120
121 let s:li=[]
122 new
123 setlocal bufhidden=unload
124 bwipeout
125 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
126
Bram Moolenaare99e8442016-07-26 20:43:40 +0200127 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200128 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200129endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200130
131" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200132func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200133 tabedit
134 tabfirst
135
136 augroup test_autocmd_bufunload_with_tabnext_group
137 autocmd!
138 autocmd BufUnload <buffer> tabnext
139 augroup END
140
141 quit
142 call assert_equal(2, tabpagenr('$'))
143
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200144 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200145 augroup! test_autocmd_bufunload_with_tabnext_group
146 tablast
147 quit
148endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200149
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200150func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200151 tabedit
152 augroup sample
153 autocmd!
154 autocmd BufWinLeave <buffer> tabfirst
155 augroup END
156 call setline(1, ['a', 'b', 'c'])
157 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200158 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200159endfunc
160
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200161" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200162func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200163 split aa.txt
164 let lastbuf = bufnr('$')
165
166 augroup test_autocmd_bufunload
167 autocmd!
168 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
169 augroup END
170
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +0200171 call assert_fails('edit bb.txt', ['E937:', 'E517:'])
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200172
173 autocmd! test_autocmd_bufunload
174 augroup! test_autocmd_bufunload
175 bwipe! aa.txt
176 bwipe! bb.txt
177endfunc
178
179" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200180func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200181 setlocal buftype=nowrite
182 let lastbuf = bufnr('$')
183
184 augroup test_autocmd_bufunload
185 autocmd!
186 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
187 augroup END
188
189 normal! i1
190 call assert_fails('edit a.txt', 'E517:')
191 call feedkeys("\<CR>")
192
193 autocmd! test_autocmd_bufunload
194 augroup! test_autocmd_bufunload
195 bwipe! a.txt
196endfunc
197
Bram Moolenaarc917da42016-07-19 22:31:36 +0200198func Test_win_tab_autocmd()
199 let g:record = []
200
201 augroup testing
202 au WinNew * call add(g:record, 'WinNew')
203 au WinEnter * call add(g:record, 'WinEnter')
204 au WinLeave * call add(g:record, 'WinLeave')
205 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200206 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200207 au TabEnter * call add(g:record, 'TabEnter')
208 au TabLeave * call add(g:record, 'TabLeave')
209 augroup END
210
211 split
212 tabnew
213 close
214 close
215
216 call assert_equal([
217 \ 'WinLeave', 'WinNew', 'WinEnter',
218 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200219 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200220 \ 'WinLeave', 'WinEnter'
221 \ ], g:record)
222
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200223 let g:record = []
224 tabnew somefile
225 tabnext
226 bwipe somefile
227
228 call assert_equal([
229 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
230 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
231 \ 'TabClosed'
232 \ ], g:record)
233
Bram Moolenaarc917da42016-07-19 22:31:36 +0200234 augroup testing
235 au!
236 augroup END
237 unlet g:record
238endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200239
240func s:AddAnAutocmd()
241 augroup vimBarTest
242 au BufReadCmd * echo 'hello'
243 augroup END
244 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
245endfunc
246
247func Test_early_bar()
248 " test that a bar is recognized before the {event}
249 call s:AddAnAutocmd()
250 augroup vimBarTest | au! | augroup END
251 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
252
253 call s:AddAnAutocmd()
254 augroup vimBarTest| au!| augroup END
255 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
256
257 " test that a bar is recognized after the {event}
258 call s:AddAnAutocmd()
259 augroup vimBarTest| au!BufReadCmd| augroup END
260 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
261
262 " test that a bar is recognized after the {group}
263 call s:AddAnAutocmd()
264 au! vimBarTest|echo 'hello'
265 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
266endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200267
Bram Moolenaar5c809082016-09-01 16:21:48 +0200268func RemoveGroup()
269 autocmd! StartOK
270 augroup! StartOK
271endfunc
272
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200273func Test_augroup_warning()
274 augroup TheWarning
275 au VimEnter * echo 'entering'
276 augroup END
277 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
278 redir => res
279 augroup! TheWarning
280 redir END
281 call assert_true(match(res, "W19:") >= 0)
282 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
283
284 " check "Another" does not take the pace of the deleted entry
285 augroup Another
286 augroup END
287 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200288 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200289
290 " no warning for postpone aucmd delete
291 augroup StartOK
292 au VimEnter * call RemoveGroup()
293 augroup END
294 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
295 redir => res
296 doautocmd VimEnter
297 redir END
298 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200299 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200300
301 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200302endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200303
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200304func Test_BufReadCmdHelp()
305 " This used to cause access to free memory
306 au BufReadCmd * e +h
307 help
308
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200309 au! BufReadCmd
310endfunc
311
312func Test_BufReadCmdHelpJump()
313 " This used to cause access to free memory
314 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200315 " } to fix highlighting
316 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200317
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200318 au! BufReadCmd
319endfunc
320
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200321func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200322 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200323 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200324 call assert_fails('augroup! x', 'E936:')
325 au VimEnter * echo
326 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200327 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200328 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
329 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200330endfunc
331
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200332" Tests for autocommands on :close command.
333" This used to be in test13.
334func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200335 " Clean up buffers, because in some cases this function fails.
336 call s:cleanup_buffers()
337
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200338 " Write three files and open them, each in a window.
339 " Then go to next window, with autocommand that deletes the previous one.
340 " Do this twice, writing the file.
341 e! Xtestje1
342 call setline(1, 'testje1')
343 w
344 sp Xtestje2
345 call setline(1, 'testje2')
346 w
347 sp Xtestje3
348 call setline(1, 'testje3')
349 w
350 wincmd w
351 au WinLeave Xtestje2 bwipe
352 wincmd w
353 call assert_equal('Xtestje1', expand('%'))
354
355 au WinLeave Xtestje1 bwipe Xtestje3
356 close
357 call assert_equal('Xtestje1', expand('%'))
358
359 " Test deleting the buffer on a Unload event. If this goes wrong there
360 " will be the ATTENTION prompt.
361 e Xtestje1
362 au!
363 au! BufUnload Xtestje1 bwipe
364 call assert_fails('e Xtestje3', 'E937:')
365 call assert_equal('Xtestje3', expand('%'))
366
367 e Xtestje2
368 sp Xtestje1
369 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200370 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200371
372 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
373 " there are ml_line errors and/or a Crash.
374 au!
375 only
376 e Xanother
377 e Xtestje1
378 bwipe Xtestje2
379 bwipe Xtestje3
380 au BufWipeout Xtestje1 buf Xtestje1
381 bwipe
382 call assert_equal('Xanother', expand('%'))
383
384 only
385 help
386 wincmd w
387 1quit
388 call assert_equal('Xanother', expand('%'))
389
390 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100391 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200392 call delete('Xtestje1')
393 call delete('Xtestje2')
394 call delete('Xtestje3')
395endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100396
397func Test_BufEnter()
398 au! BufEnter
399 au Bufenter * let val = val . '+'
400 let g:val = ''
401 split NewFile
402 call assert_equal('+', g:val)
403 bwipe!
404 call assert_equal('++', g:val)
405
406 " Also get BufEnter when editing a directory
407 call mkdir('Xdir')
408 split Xdir
409 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100410
411 " On MS-Windows we can't edit the directory, make sure we wipe the right
412 " buffer.
413 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100414
415 call delete('Xdir', 'd')
416 au! BufEnter
417endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100418
419" Closing a window might cause an endless loop
420" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200421func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200422 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100423 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200424 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100425 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100426 mksession!
427
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200428 let content =<< trim [CODE]
429 set nocp noswapfile
430 let v:swapchoice="e"
431 augroup test_autocmd_sessionload
432 autocmd!
433 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
434 augroup END
435
436 func WriteErrors()
437 call writefile([execute("messages")], "Xerrors")
438 endfunc
439 au VimLeave * call WriteErrors()
440 [CODE]
441
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100442 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200443 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100444 let errors = join(readfile('Xerrors'))
445 call assert_match('E814', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100446
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100447 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100448 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100449 call delete(file)
450 endfor
451endfunc
452
453" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200454func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100455 tabnew
456 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100457 mksession!
458
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200459 let content =<< trim [CODE]
460 set nocp noswapfile
461 function! DeleteInactiveBufs()
462 tabfirst
463 let tabblist = []
464 for i in range(1, tabpagenr(''$''))
465 call extend(tabblist, tabpagebuflist(i))
466 endfor
467 for b in range(1, bufnr(''$''))
468 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
469 exec ''bwipeout '' . b
470 endif
471 endfor
472 echomsg "SessionLoadPost DONE"
473 endfunction
474 au SessionLoadPost * call DeleteInactiveBufs()
475
476 func WriteErrors()
477 call writefile([execute("messages")], "Xerrors")
478 endfunc
479 au VimLeave * call WriteErrors()
480 [CODE]
481
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100482 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200483 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100484 let errors = join(readfile('Xerrors'))
485 " This probably only ever matches on unix.
486 call assert_notmatch('Caught deadly signal SEGV', errors)
487 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100488
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100489 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100490 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100491 call delete(file)
492 endfor
493endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200494
495func Test_empty_doau()
496 doau \|
497endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200498
499func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200500 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200501 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200502 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
503 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 +0200504 let g:opt = [expected, actual]
505 "call assert_equal(expected, actual)
506endfunc
507
508func Test_OptionSet()
Bram Moolenaar26d98212019-01-27 22:32:55 +0100509 if !has("eval") || !exists("+autochdir")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200510 return
511 endif
512
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200513 badd test_autocmd.vim
514
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200515 call test_override('starting', 1)
516 set nocp
517 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
518
519 " 1: Setting number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200520 let g:options=[['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200521 set nu
522 call assert_equal([], g:options)
523 call assert_equal(g:opt[0], g:opt[1])
524
525 " 2: Setting local number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200526 let g:options=[['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200527 setlocal nonu
528 call assert_equal([], g:options)
529 call assert_equal(g:opt[0], g:opt[1])
530
531 " 3: Setting global number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200532 let g:options=[['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200533 setglobal nonu
534 call assert_equal([], g:options)
535 call assert_equal(g:opt[0], g:opt[1])
536
537 " 4: Setting local autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200538 let g:options=[['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200539 setlocal ai
540 call assert_equal([], g:options)
541 call assert_equal(g:opt[0], g:opt[1])
542
543 " 5: Setting global autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200544 let g:options=[['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200545 setglobal ai
546 call assert_equal([], g:options)
547 call assert_equal(g:opt[0], g:opt[1])
548
549 " 6: Setting global autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200550 let g:options=[['autoindent', 1, 1, 1, 0, 'global', 'set']]
551 set ai!
552 call assert_equal([], g:options)
553 call assert_equal(g:opt[0], g:opt[1])
554
555 " 6a: Setting global autoindent option"
556 let g:options=[['autoindent', 1, 1, 0, 0, 'global', 'set']]
557 noa setlocal ai
558 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200559 set ai!
560 call assert_equal([], g:options)
561 call assert_equal(g:opt[0], g:opt[1])
562
563 " Should not print anything, use :noa
564 " 7: don't trigger OptionSet"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200565 let g:options=[['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200566 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200567 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200568 call assert_equal(g:opt[0], g:opt[1])
569
570 " 8: Setting several global list and number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200571 let g:options=[['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200572 set list nu
573 call assert_equal([], g:options)
574 call assert_equal(g:opt[0], g:opt[1])
575
576 " 9: don't trigger OptionSet"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200577 let g:options=[['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200578 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200579 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 +0200580 call assert_equal(g:opt[0], g:opt[1])
581
582 " 10: Setting global acd"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200583 let g:options=[['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200584 setlocal acd
585 call assert_equal([], g:options)
586 call assert_equal(g:opt[0], g:opt[1])
587
588 " 11: Setting global autoread (also sets local value)"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200589 let g:options=[['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200590 set ar
591 call assert_equal([], g:options)
592 call assert_equal(g:opt[0], g:opt[1])
593
594 " 12: Setting local autoread"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200595 let g:options=[['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200596 setlocal ar
597 call assert_equal([], g:options)
598 call assert_equal(g:opt[0], g:opt[1])
599
600 " 13: Setting global autoread"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200601 let g:options=[['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200602 setglobal invar
603 call assert_equal([], g:options)
604 call assert_equal(g:opt[0], g:opt[1])
605
606 " 14: Setting option backspace through :let"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200607 let g:options=[['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200608 let &bs="eol,indent,start"
609 call assert_equal([], g:options)
610 call assert_equal(g:opt[0], g:opt[1])
611
612 " 15: Setting option backspace through setbufvar()"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200613 let g:options=[['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200614 " try twice, first time, shouldn't trigger because option name is invalid,
615 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200616 let bnum = bufnr('%')
617 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200618 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200619 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200620 call assert_equal([], g:options)
621 call assert_equal(g:opt[0], g:opt[1])
622
623 " 16: Setting number option using setwinvar"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200624 let g:options=[['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200625 call setwinvar(0, '&number', 1)
626 call assert_equal([], g:options)
627 call assert_equal(g:opt[0], g:opt[1])
628
629 " 17: Setting key option, shouldn't trigger"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200630 let g:options=[['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200631 setlocal key=blah
632 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200633 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200634 call assert_equal(g:opt[0], g:opt[1])
635
Bram Moolenaard7c96872019-06-15 17:12:48 +0200636
637 " 18a: Setting string global option"
638 let oldval = &backupext
639 let g:options=[['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
640 set backupext=foo
641 call assert_equal([], g:options)
642 call assert_equal(g:opt[0], g:opt[1])
643
644 " 18b: Resetting string global option"
645 let g:options=[['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
646 set backupext&
647 call assert_equal([], g:options)
648 call assert_equal(g:opt[0], g:opt[1])
649
650 " 18c: Setting global string global option"
651 let g:options=[['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
652 setglobal backupext=bar
653 call assert_equal([], g:options)
654 call assert_equal(g:opt[0], g:opt[1])
655
656 " 18d: Setting local string global option"
657 " As this is a global option this sets the global value even though
658 " :setlocal is used!
659 noa set backupext& " Reset global and local value (without triggering autocmd)
660 let g:options=[['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
661 setlocal backupext=baz
662 call assert_equal([], g:options)
663 call assert_equal(g:opt[0], g:opt[1])
664
665 " 18e: Setting again string global option"
666 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
667 noa setlocal backupext=ext_local " Sets the global(!) value!
668 let g:options=[['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
669 set backupext=fuu
670 call assert_equal([], g:options)
671 call assert_equal(g:opt[0], g:opt[1])
672
673
674 " 19a: Setting string local-global (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200675 let oldval = &tags
Bram Moolenaard7c96872019-06-15 17:12:48 +0200676 let g:options=[['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200677 set tags=tagpath
678 call assert_equal([], g:options)
679 call assert_equal(g:opt[0], g:opt[1])
680
Bram Moolenaard7c96872019-06-15 17:12:48 +0200681 " 19b: Resetting string local-global (to buffer) option"
682 let g:options=[['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200683 set tags&
684 call assert_equal([], g:options)
685 call assert_equal(g:opt[0], g:opt[1])
686
Bram Moolenaard7c96872019-06-15 17:12:48 +0200687 " 19c: Setting global string local-global (to buffer) option "
688 let g:options=[['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
689 setglobal tags=tagpath1
690 call assert_equal([], g:options)
691 call assert_equal(g:opt[0], g:opt[1])
692
693 " 19d: Setting local string local-global (to buffer) option"
694 let g:options=[['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
695 setlocal tags=tagpath2
696 call assert_equal([], g:options)
697 call assert_equal(g:opt[0], g:opt[1])
698
699 " 19e: Setting again string local-global (to buffer) option"
700 " Note: v:option_old is the old global value for local-global string options
701 " but the old local value for all other kinds of options.
702 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
703 noa setlocal tags=tag_local
704 let g:options=[['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
705 set tags=tagpath
706 call assert_equal([], g:options)
707 call assert_equal(g:opt[0], g:opt[1])
708
709 " 19f: Setting string local-global (to buffer) option to an empty string"
710 " Note: v:option_old is the old global value for local-global string options
711 " but the old local value for all other kinds of options.
712 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
713 noa setlocal tags= " empty string
714 let g:options=[['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
715 set tags=tagpath
716 call assert_equal([], g:options)
717 call assert_equal(g:opt[0], g:opt[1])
718
719
720 " 20a: Setting string local (to buffer) option"
721 let oldval = &spelllang
722 let g:options=[['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
723 set spelllang=elvish,klingon
724 call assert_equal([], g:options)
725 call assert_equal(g:opt[0], g:opt[1])
726
727 " 20b: Resetting string local (to buffer) option"
728 let g:options=[['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
729 set spelllang&
730 call assert_equal([], g:options)
731 call assert_equal(g:opt[0], g:opt[1])
732
733 " 20c: Setting global string local (to buffer) option"
734 let g:options=[['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
735 setglobal spelllang=elvish
736 call assert_equal([], g:options)
737 call assert_equal(g:opt[0], g:opt[1])
738
739 " 20d: Setting local string local (to buffer) option"
740 noa set spelllang& " Reset global and local value (without triggering autocmd)
741 let g:options=[['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
742 setlocal spelllang=klingon
743 call assert_equal([], g:options)
744 call assert_equal(g:opt[0], g:opt[1])
745
746 " 20e: Setting again string local (to buffer) option"
747 " Note: v:option_old is the old global value for local-global string options
748 " but the old local value for all other kinds of options.
749 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
750 noa setlocal spelllang=spelllocal
751 let g:options=[['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
752 set spelllang=foo
753 call assert_equal([], g:options)
754 call assert_equal(g:opt[0], g:opt[1])
755
756
757 " 21a: Setting string local-global (to window) option"
758 let oldval = &statusline
759 let g:options=[['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
760 set statusline=foo
761 call assert_equal([], g:options)
762 call assert_equal(g:opt[0], g:opt[1])
763
764 " 21b: Resetting string local-global (to window) option"
765 " Note: v:option_old is the old global value for local-global string options
766 " but the old local value for all other kinds of options.
767 let g:options=[['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
768 set statusline&
769 call assert_equal([], g:options)
770 call assert_equal(g:opt[0], g:opt[1])
771
772 " 21c: Setting global string local-global (to window) option"
773 let g:options=[['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
774 setglobal statusline=bar
775 call assert_equal([], g:options)
776 call assert_equal(g:opt[0], g:opt[1])
777
778 " 21d: Setting local string local-global (to window) option"
779 noa set statusline& " Reset global and local value (without triggering autocmd)
780 let g:options=[['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
781 setlocal statusline=baz
782 call assert_equal([], g:options)
783 call assert_equal(g:opt[0], g:opt[1])
784
785 " 21e: Setting again string local-global (to window) option"
786 " Note: v:option_old is the old global value for local-global string options
787 " but the old local value for all other kinds of options.
788 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
789 noa setlocal statusline=baz
790 let g:options=[['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
791 set statusline=foo
792 call assert_equal([], g:options)
793 call assert_equal(g:opt[0], g:opt[1])
794
795
796 " 22a: Setting string local (to window) option"
797 let oldval = &foldignore
798 let g:options=[['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
799 set foldignore=fo
800 call assert_equal([], g:options)
801 call assert_equal(g:opt[0], g:opt[1])
802
803 " 22b: Resetting string local (to window) option"
804 let g:options=[['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
805 set foldignore&
806 call assert_equal([], g:options)
807 call assert_equal(g:opt[0], g:opt[1])
808
809 " 22c: Setting global string local (to window) option"
810 let g:options=[['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
811 setglobal foldignore=bar
812 call assert_equal([], g:options)
813 call assert_equal(g:opt[0], g:opt[1])
814
815 " 22d: Setting local string local (to window) option"
816 noa set foldignore& " Reset global and local value (without triggering autocmd)
817 let g:options=[['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
818 setlocal foldignore=baz
819 call assert_equal([], g:options)
820 call assert_equal(g:opt[0], g:opt[1])
821
822 " 22e: Setting again string local (to window) option"
823 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
824 noa setlocal foldignore=loc
825 let g:options=[['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
826 set foldignore=fo
827 call assert_equal([], g:options)
828 call assert_equal(g:opt[0], g:opt[1])
829
830
831 " 23a: Setting global number local option"
832 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
833 noa setlocal cmdheight=1 " Sets the global(!) value!
834 let g:options=[['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
835 setglobal cmdheight=2
836 call assert_equal([], g:options)
837 call assert_equal(g:opt[0], g:opt[1])
838
839 " 23b: Setting local number global option"
840 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
841 noa setlocal cmdheight=1 " Sets the global(!) value!
842 let g:options=[['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
843 setlocal cmdheight=2
844 call assert_equal([], g:options)
845 call assert_equal(g:opt[0], g:opt[1])
846
847 " 23c: Setting again number global option"
848 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
849 noa setlocal cmdheight=1 " Sets the global(!) value!
850 let g:options=[['cmdheight', '1', '1', '1', '2', 'global', 'set']]
851 set cmdheight=2
852 call assert_equal([], g:options)
853 call assert_equal(g:opt[0], g:opt[1])
854
855 " 23d: Setting again number global option"
856 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
857 let g:options=[['cmdheight', '8', '8', '8', '2', 'global', 'set']]
858 set cmdheight=2
859 call assert_equal([], g:options)
860 call assert_equal(g:opt[0], g:opt[1])
861
862
863 " 24a: Setting global number global-local (to buffer) option"
864 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
865 noa setlocal undolevels=1
866 let g:options=[['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
867 setglobal undolevels=2
868 call assert_equal([], g:options)
869 call assert_equal(g:opt[0], g:opt[1])
870
871 " 24b: Setting local number global-local (to buffer) option"
872 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
873 noa setlocal undolevels=1
874 let g:options=[['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
875 setlocal undolevels=2
876 call assert_equal([], g:options)
877 call assert_equal(g:opt[0], g:opt[1])
878
879 " 24c: Setting again number global-local (to buffer) option"
880 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
881 noa setlocal undolevels=1
882 let g:options=[['undolevels', '1', '1', '8', '2', 'global', 'set']]
883 set undolevels=2
884 call assert_equal([], g:options)
885 call assert_equal(g:opt[0], g:opt[1])
886
887 " 24d: Setting again global number global-local (to buffer) option"
888 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
889 let g:options=[['undolevels', '8', '8', '8', '2', 'global', 'set']]
890 set undolevels=2
891 call assert_equal([], g:options)
892 call assert_equal(g:opt[0], g:opt[1])
893
894
895 " 25a: Setting global number local (to buffer) option"
896 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
897 noa setlocal wrapmargin=1
898 let g:options=[['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
899 setglobal wrapmargin=2
900 call assert_equal([], g:options)
901 call assert_equal(g:opt[0], g:opt[1])
902
903 " 25b: Setting local number local (to buffer) option"
904 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
905 noa setlocal wrapmargin=1
906 let g:options=[['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
907 setlocal wrapmargin=2
908 call assert_equal([], g:options)
909 call assert_equal(g:opt[0], g:opt[1])
910
911 " 25c: Setting again number local (to buffer) option"
912 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
913 noa setlocal wrapmargin=1
914 let g:options=[['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
915 set wrapmargin=2
916 call assert_equal([], g:options)
917 call assert_equal(g:opt[0], g:opt[1])
918
919 " 25d: Setting again global number local (to buffer) option"
920 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
921 let g:options=[['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
922 set wrapmargin=2
923 call assert_equal([], g:options)
924 call assert_equal(g:opt[0], g:opt[1])
925
926
927 " 26: Setting number global-local (to window) option.
928 " Such option does currently not exist.
929
930
931 " 27a: Setting global number local (to window) option"
932 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
933 noa setlocal foldcolumn=1
934 let g:options=[['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
935 setglobal foldcolumn=2
936 call assert_equal([], g:options)
937 call assert_equal(g:opt[0], g:opt[1])
938
939 " 27b: Setting local number local (to window) option"
940 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
941 noa setlocal foldcolumn=1
942 let g:options=[['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
943 setlocal foldcolumn=2
944 call assert_equal([], g:options)
945 call assert_equal(g:opt[0], g:opt[1])
946
947 " 27c: Setting again number local (to window) option"
948 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
949 noa setlocal foldcolumn=1
950 let g:options=[['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
951 set foldcolumn=2
952 call assert_equal([], g:options)
953 call assert_equal(g:opt[0], g:opt[1])
954
955 " 27d: Ssettin again global number local (to window) option"
956 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
957 let g:options=[['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
958 set foldcolumn=2
959 call assert_equal([], g:options)
960 call assert_equal(g:opt[0], g:opt[1])
961
962
963 " 28a: Setting global boolean global option"
964 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
965 noa setlocal wrapscan " Sets the global(!) value!
966 let g:options=[['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
967 setglobal nowrapscan
968 call assert_equal([], g:options)
969 call assert_equal(g:opt[0], g:opt[1])
970
971 " 28b: Setting local boolean global option"
972 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
973 noa setlocal wrapscan " Sets the global(!) value!
974 let g:options=[['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
975 setlocal nowrapscan
976 call assert_equal([], g:options)
977 call assert_equal(g:opt[0], g:opt[1])
978
979 " 28c: Setting again boolean global option"
980 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
981 noa setlocal wrapscan " Sets the global(!) value!
982 let g:options=[['wrapscan', '1', '1', '1', '0', 'global', 'set']]
983 set nowrapscan
984 call assert_equal([], g:options)
985 call assert_equal(g:opt[0], g:opt[1])
986
987 " 28d: Setting again global boolean global option"
988 noa set nowrapscan " Reset global and local value (without triggering autocmd)
989 let g:options=[['wrapscan', '0', '0', '0', '1', 'global', 'set']]
990 set wrapscan
991 call assert_equal([], g:options)
992 call assert_equal(g:opt[0], g:opt[1])
993
994
995 " 29a: Setting global boolean global-local (to buffer) option"
996 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
997 noa setlocal autoread
998 let g:options=[['autoread', '0', '', '0', '1', 'global', 'setglobal']]
999 setglobal autoread
1000 call assert_equal([], g:options)
1001 call assert_equal(g:opt[0], g:opt[1])
1002
1003 " 29b: Setting local boolean global-local (to buffer) option"
1004 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1005 noa setlocal autoread
1006 let g:options=[['autoread', '1', '1', '', '0', 'local', 'setlocal']]
1007 setlocal noautoread
1008 call assert_equal([], g:options)
1009 call assert_equal(g:opt[0], g:opt[1])
1010
1011 " 29c: Setting again boolean global-local (to buffer) option"
1012 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1013 noa setlocal autoread
1014 let g:options=[['autoread', '1', '1', '0', '1', 'global', 'set']]
1015 set autoread
1016 call assert_equal([], g:options)
1017 call assert_equal(g:opt[0], g:opt[1])
1018
1019 " 29d: Setting again global boolean global-local (to buffer) option"
1020 noa set noautoread " Reset global and local value (without triggering autocmd)
1021 let g:options=[['autoread', '0', '0', '0', '1', 'global', 'set']]
1022 set autoread
1023 call assert_equal([], g:options)
1024 call assert_equal(g:opt[0], g:opt[1])
1025
1026
1027 " 30a: Setting global boolean local (to buffer) option"
1028 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1029 noa setlocal cindent
1030 let g:options=[['cindent', '0', '', '0', '1', 'global', 'setglobal']]
1031 setglobal cindent
1032 call assert_equal([], g:options)
1033 call assert_equal(g:opt[0], g:opt[1])
1034
1035 " 30b: Setting local boolean local (to buffer) option"
1036 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1037 noa setlocal cindent
1038 let g:options=[['cindent', '1', '1', '', '0', 'local', 'setlocal']]
1039 setlocal nocindent
1040 call assert_equal([], g:options)
1041 call assert_equal(g:opt[0], g:opt[1])
1042
1043 " 30c: Setting again boolean local (to buffer) option"
1044 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1045 noa setlocal cindent
1046 let g:options=[['cindent', '1', '1', '0', '1', 'global', 'set']]
1047 set cindent
1048 call assert_equal([], g:options)
1049 call assert_equal(g:opt[0], g:opt[1])
1050
1051 " 30d: Setting again global boolean local (to buffer) option"
1052 noa set nocindent " Reset global and local value (without triggering autocmd)
1053 let g:options=[['cindent', '0', '0', '0', '1', 'global', 'set']]
1054 set cindent
1055 call assert_equal([], g:options)
1056 call assert_equal(g:opt[0], g:opt[1])
1057
1058
1059 " 31: Setting boolean global-local (to window) option
1060 " Currently no such option exists.
1061
1062
1063 " 32a: Setting global boolean local (to window) option"
1064 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1065 noa setlocal cursorcolumn
1066 let g:options=[['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
1067 setglobal cursorcolumn
1068 call assert_equal([], g:options)
1069 call assert_equal(g:opt[0], g:opt[1])
1070
1071 " 32b: Setting local boolean local (to window) option"
1072 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1073 noa setlocal cursorcolumn
1074 let g:options=[['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
1075 setlocal nocursorcolumn
1076 call assert_equal([], g:options)
1077 call assert_equal(g:opt[0], g:opt[1])
1078
1079 " 32c: Setting again boolean local (to window) option"
1080 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1081 noa setlocal cursorcolumn
1082 let g:options=[['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
1083 set cursorcolumn
1084 call assert_equal([], g:options)
1085 call assert_equal(g:opt[0], g:opt[1])
1086
1087 " 32d: Setting again global boolean local (to window) option"
1088 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
1089 let g:options=[['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
1090 set cursorcolumn
1091 call assert_equal([], g:options)
1092 call assert_equal(g:opt[0], g:opt[1])
1093
1094
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001095 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001096 noa set backspace=1 " Reset global and local value (without triggering autocmd)
1097 let g:options=[['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
1098 set backspace=2
1099 call assert_equal([], g:options)
1100 call assert_equal(g:opt[0], g:opt[1])
1101
1102
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001103 " Cleanup
1104 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001105 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001106 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 +02001107 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001108 endfor
1109 call test_override('starting', 0)
1110 delfunc! AutoCommandOptionSet
1111endfunc
1112
1113func Test_OptionSet_diffmode()
1114 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001115 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001116 new
1117 au OptionSet diff :let &l:cul=v:option_new
1118
1119 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1120 call assert_equal(0, &l:cul)
1121 diffthis
1122 call assert_equal(1, &l:cul)
1123
1124 vnew
1125 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1126 call assert_equal(0, &l:cul)
1127 diffthis
1128 call assert_equal(1, &l:cul)
1129
1130 diffoff
1131 call assert_equal(0, &l:cul)
1132 call assert_equal(1, getwinvar(2, '&l:cul'))
1133 bw!
1134
1135 call assert_equal(1, &l:cul)
1136 diffoff!
1137 call assert_equal(0, &l:cul)
1138 call assert_equal(0, getwinvar(1, '&l:cul'))
1139 bw!
1140
1141 " Cleanup
1142 au! OptionSet
1143 call test_override('starting', 0)
1144endfunc
1145
1146func Test_OptionSet_diffmode_close()
1147 call test_override('starting', 1)
1148 " 19: Try to close the current window when entering diff mode
1149 " should not segfault
1150 new
1151 au OptionSet diff close
1152
1153 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1154 call assert_fails(':diffthis', 'E788')
1155 call assert_equal(1, &diff)
1156 vnew
1157 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1158 call assert_fails(':diffthis', 'E788')
1159 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001160 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001161 bw!
1162 call assert_fails(':diffoff!', 'E788')
1163 bw!
1164
1165 " Cleanup
1166 au! OptionSet
1167 call test_override('starting', 0)
1168 "delfunc! AutoCommandOptionSet
1169endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001170
1171" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1172func Test_BufleaveWithDelete()
1173 new | edit Xfile1
1174
1175 augroup test_bufleavewithdelete
1176 autocmd!
1177 autocmd BufLeave Xfile1 bwipe Xfile2
1178 augroup END
1179
1180 call assert_fails('edit Xfile2', 'E143:')
1181 call assert_equal('Xfile1', bufname('%'))
1182
1183 autocmd! test_bufleavewithdelete BufLeave Xfile1
1184 augroup! test_bufleavewithdelete
1185
1186 new
1187 bwipe! Xfile1
1188endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001189
1190" Test for autocommand that changes the buffer list, when doing ":ball".
1191func Test_Acmd_BufAll()
1192 enew!
1193 %bwipe!
1194 call writefile(['Test file Xxx1'], 'Xxx1')
1195 call writefile(['Test file Xxx2'], 'Xxx2')
1196 call writefile(['Test file Xxx3'], 'Xxx3')
1197
1198 " Add three files to the buffer list
1199 split Xxx1
1200 close
1201 split Xxx2
1202 close
1203 split Xxx3
1204 close
1205
1206 " Wipe the buffer when the buffer is opened
1207 au BufReadPost Xxx2 bwipe
1208
1209 call append(0, 'Test file Xxx4')
1210 ball
1211
1212 call assert_equal(2, winnr('$'))
1213 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1214 wincmd t
1215
1216 au! BufReadPost
1217 %bwipe!
1218 call delete('Xxx1')
1219 call delete('Xxx2')
1220 call delete('Xxx3')
1221 enew! | only
1222endfunc
1223
1224" Test for autocommand that changes current buffer on BufEnter event.
1225" Check if modelines are interpreted for the correct buffer.
1226func Test_Acmd_BufEnter()
1227 %bwipe!
1228 call writefile(['start of test file Xxx1',
1229 \ "\<Tab>this is a test",
1230 \ 'end of test file Xxx1'], 'Xxx1')
1231 call writefile(['start of test file Xxx2',
1232 \ 'vim: set noai :',
1233 \ "\<Tab>this is a test",
1234 \ 'end of test file Xxx2'], 'Xxx2')
1235
1236 au BufEnter Xxx2 brew
1237 set ai modeline modelines=3
1238 edit Xxx1
1239 " edit Xxx2, autocmd will do :brew
1240 edit Xxx2
1241 exe "normal G?this is a\<CR>"
1242 " Append text with autoindent to this file
1243 normal othis should be auto-indented
1244 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1245 call assert_equal(3, line('.'))
1246 " Remove autocmd and edit Xxx2 again
1247 au! BufEnter Xxx2
1248 buf! Xxx2
1249 exe "normal G?this is a\<CR>"
1250 " append text without autoindent to Xxx
1251 normal othis should be in column 1
1252 call assert_equal("this should be in column 1", getline('.'))
1253 call assert_equal(4, line('.'))
1254
1255 %bwipe!
1256 call delete('Xxx1')
1257 call delete('Xxx2')
1258 set ai&vim modeline&vim modelines&vim
1259endfunc
1260
1261" Test for issue #57
1262" do not move cursor on <c-o> when autoindent is set
1263func Test_ai_CTRL_O()
1264 enew!
1265 set ai
1266 let save_fo = &fo
1267 set fo+=r
1268 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1269 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1270 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1271
1272 set ai&vim
1273 let &fo = save_fo
1274 enew!
1275endfunc
1276
1277" Test for autocommand that deletes the current buffer on BufLeave event.
1278" Also test deleting the last buffer, should give a new, empty buffer.
1279func Test_BufLeave_Wipe()
1280 %bwipe!
1281 let content = ['start of test file Xxx',
1282 \ 'this is a test',
1283 \ 'end of test file Xxx']
1284 call writefile(content, 'Xxx1')
1285 call writefile(content, 'Xxx2')
1286
1287 au BufLeave Xxx2 bwipe
1288 edit Xxx1
1289 split Xxx2
1290 " delete buffer Xxx2, we should be back to Xxx1
1291 bwipe
1292 call assert_equal('Xxx1', bufname('%'))
1293 call assert_equal(1, winnr('$'))
1294
1295 " Create an alternate buffer
1296 %write! test.out
1297 call assert_equal('test.out', bufname('#'))
1298 " delete alternate buffer
1299 bwipe test.out
1300 call assert_equal('Xxx1', bufname('%'))
1301 call assert_equal('', bufname('#'))
1302
1303 au BufLeave Xxx1 bwipe
1304 " delete current buffer, get an empty one
1305 bwipe!
1306 call assert_equal(1, line('$'))
1307 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001308 let g:bufinfo = getbufinfo()
1309 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001310
1311 call delete('Xxx1')
1312 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001313 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001314 %bwipe
1315 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001316
1317 " check that bufinfo doesn't contain a pointer to freed memory
1318 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001319endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001320
1321func Test_QuitPre()
1322 edit Xfoo
1323 let winid = win_getid(winnr())
1324 split Xbar
1325 au! QuitPre * let g:afile = expand('<afile>')
1326 " Close the other window, <afile> should be correct.
1327 exe win_id2win(winid) . 'q'
1328 call assert_equal('Xfoo', g:afile)
1329
1330 unlet g:afile
1331 bwipe Xfoo
1332 bwipe Xbar
1333endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001334
1335func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001336 au! CmdlineChanged : let g:text = getcmdline()
1337 let g:text = 0
1338 call feedkeys(":echom 'hello'\<CR>", 'xt')
1339 call assert_equal("echom 'hello'", g:text)
1340 au! CmdlineChanged
1341
1342 au! CmdlineChanged : let g:entered = expand('<afile>')
1343 let g:entered = 0
1344 call feedkeys(":echom 'hello'\<CR>", 'xt')
1345 call assert_equal(':', g:entered)
1346 au! CmdlineChanged
1347
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001348 au! CmdlineEnter : let g:entered = expand('<afile>')
1349 au! CmdlineLeave : let g:left = expand('<afile>')
1350 let g:entered = 0
1351 let g:left = 0
1352 call feedkeys(":echo 'hello'\<CR>", 'xt')
1353 call assert_equal(':', g:entered)
1354 call assert_equal(':', g:left)
1355 au! CmdlineEnter
1356 au! CmdlineLeave
1357
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001358 let save_shellslash = &shellslash
1359 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001360 au! CmdlineEnter / let g:entered = expand('<afile>')
1361 au! CmdlineLeave / let g:left = expand('<afile>')
1362 let g:entered = 0
1363 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001364 new
1365 call setline(1, 'hello')
1366 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001367 call assert_equal('/', g:entered)
1368 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001369 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001370 au! CmdlineEnter
1371 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001372 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001373endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001374
1375" Test for BufWritePre autocommand that deletes or unloads the buffer.
1376func Test_BufWritePre()
1377 %bwipe
1378 au BufWritePre Xxx1 bunload
1379 au BufWritePre Xxx2 bwipe
1380
1381 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1382 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1383
1384 edit Xtest
1385 e! Xxx2
1386 bdel Xtest
1387 e Xxx1
1388 " write it, will unload it and give an error msg
1389 call assert_fails('w', 'E203')
1390 call assert_equal('Xxx2', bufname('%'))
1391 edit Xtest
1392 e! Xxx2
1393 bwipe Xtest
1394 " write it, will delete the buffer and give an error msg
1395 call assert_fails('w', 'E203')
1396 call assert_equal('Xxx1', bufname('%'))
1397 au! BufWritePre
1398 call delete('Xxx1')
1399 call delete('Xxx2')
1400endfunc
1401
1402" Test for BufUnload autocommand that unloads all the other buffers
1403func Test_bufunload_all()
1404 call writefile(['Test file Xxx1'], 'Xxx1')"
1405 call writefile(['Test file Xxx2'], 'Xxx2')"
1406
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001407 let content =<< trim [CODE]
1408 func UnloadAllBufs()
1409 let i = 1
1410 while i <= bufnr('$')
1411 if i != bufnr('%') && bufloaded(i)
1412 exe i . 'bunload'
1413 endif
1414 let i += 1
1415 endwhile
1416 endfunc
1417 au BufUnload * call UnloadAllBufs()
1418 au VimLeave * call writefile(['Test Finished'], 'Xout')
1419 edit Xxx1
1420 split Xxx2
1421 q
1422 [CODE]
1423
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001424 call writefile(content, 'Xtest')
1425
1426 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001427 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001428 call assert_true(filereadable('Xout'))
1429
1430 call delete('Xxx1')
1431 call delete('Xxx2')
1432 call delete('Xtest')
1433 call delete('Xout')
1434endfunc
1435
1436" Some tests for buffer-local autocommands
1437func Test_buflocal_autocmd()
1438 let g:bname = ''
1439 edit xx
1440 au BufLeave <buffer> let g:bname = expand("%")
1441 " here, autocommand for xx should trigger.
1442 " but autocommand shall not apply to buffer named <buffer>.
1443 edit somefile
1444 call assert_equal('xx', g:bname)
1445 let g:bname = ''
1446 " here, autocommand shall be auto-deleted
1447 bwipe xx
1448 " autocmd should not trigger
1449 edit xx
1450 call assert_equal('', g:bname)
1451 " autocmd should not trigger
1452 edit somefile
1453 call assert_equal('', g:bname)
1454 enew
1455 unlet g:bname
1456endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001457
1458" Test for "*Cmd" autocommands
1459func Test_Cmd_Autocmds()
1460 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1461
1462 enew!
1463 au BufReadCmd XtestA 0r Xxx|$del
1464 edit XtestA " will read text of Xxd instead
1465 call assert_equal('start of Xxx', getline(1))
1466
1467 au BufWriteCmd XtestA call append(line("$"), "write")
1468 write " will append a line to the file
1469 call assert_equal('write', getline('$'))
1470 call assert_fails('read XtestA', 'E484') " should not read anything
1471 call assert_equal('write', getline(4))
1472
1473 " now we have:
1474 " 1 start of Xxx
1475 " 2 abc2
1476 " 3 end of Xxx
1477 " 4 write
1478
1479 au FileReadCmd XtestB '[r Xxx
1480 2r XtestB " will read Xxx below line 2 instead
1481 call assert_equal('start of Xxx', getline(3))
1482
1483 " now we have:
1484 " 1 start of Xxx
1485 " 2 abc2
1486 " 3 start of Xxx
1487 " 4 abc2
1488 " 5 end of Xxx
1489 " 6 end of Xxx
1490 " 7 write
1491
1492 au FileWriteCmd XtestC '[,']copy $
1493 normal 4GA1
1494 4,5w XtestC " will copy lines 4 and 5 to the end
1495 call assert_equal("\tabc21", getline(8))
1496 call assert_fails('r XtestC', 'E484') " should not read anything
1497 call assert_equal("end of Xxx", getline(9))
1498
1499 " now we have:
1500 " 1 start of Xxx
1501 " 2 abc2
1502 " 3 start of Xxx
1503 " 4 abc21
1504 " 5 end of Xxx
1505 " 6 end of Xxx
1506 " 7 write
1507 " 8 abc21
1508 " 9 end of Xxx
1509
1510 let g:lines = []
1511 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1512 w >>XtestD " will add lines to 'lines'
1513 call assert_equal(9, len(g:lines))
1514 call assert_fails('$r XtestD', 'E484') " should not read anything
1515 call assert_equal(9, line('$'))
1516 call assert_equal('end of Xxx', getline('$'))
1517
1518 au BufReadCmd XtestE 0r Xxx|$del
1519 sp XtestE " split window with test.out
1520 call assert_equal('end of Xxx', getline(3))
1521
1522 let g:lines = []
1523 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1524 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1525 wall " will write other window to 'lines'
1526 call assert_equal(4, len(g:lines), g:lines)
1527 call assert_equal('asdf', g:lines[2])
1528
1529 au! BufReadCmd
1530 au! BufWriteCmd
1531 au! FileReadCmd
1532 au! FileWriteCmd
1533 au! FileAppendCmd
1534 %bwipe!
1535 call delete('Xxx')
1536 enew!
1537endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001538
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001539func s:ReadFile()
1540 setl noswapfile nomodified
1541 let filename = resolve(expand("<afile>:p"))
1542 execute 'read' fnameescape(filename)
1543 1d_
1544 exe 'file' fnameescape(filename)
1545 setl buftype=acwrite
1546endfunc
1547
1548func s:WriteFile()
1549 let filename = resolve(expand("<afile>:p"))
1550 setl buftype=
1551 noautocmd execute 'write' fnameescape(filename)
1552 setl buftype=acwrite
1553 setl nomodified
1554endfunc
1555
1556func Test_BufReadCmd()
1557 autocmd BufReadCmd *.test call s:ReadFile()
1558 autocmd BufWriteCmd *.test call s:WriteFile()
1559
1560 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1561 edit Xcmd.test
1562 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1563 normal! Gofour
1564 write
1565 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1566
1567 bwipe!
1568 call delete('Xcmd.test')
1569 au! BufReadCmd
1570 au! BufWriteCmd
1571endfunc
1572
Bram Moolenaaraace2152017-11-05 16:23:10 +01001573func SetChangeMarks(start, end)
1574 exe a:start. 'mark ['
1575 exe a:end. 'mark ]'
1576endfunc
1577
1578" Verify the effects of autocmds on '[ and ']
1579func Test_change_mark_in_autocmds()
1580 edit! Xtest
1581 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u", 'xtn')
1582
1583 call SetChangeMarks(2, 3)
1584 write
1585 call assert_equal([1, 4], [line("'["), line("']")])
1586
1587 call SetChangeMarks(2, 3)
1588 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1589 write
1590 au! BufWritePre
1591
1592 if executable('cat')
1593 write XtestFilter
1594 write >> XtestFilter
1595
1596 call SetChangeMarks(2, 3)
1597 " Marks are set to the entire range of the write
1598 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1599 " '[ is adjusted to just before the line that will receive the filtered
1600 " data
1601 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1602 " The filtered data is read into the buffer, and the source lines are
1603 " still present, so the range is after the source lines
1604 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1605 %!cat XtestFilter
1606 " After the filtered data is read, the original lines are deleted
1607 call assert_equal([1, 8], [line("'["), line("']")])
1608 au! FilterWritePre,FilterReadPre,FilterReadPost
1609 undo
1610
1611 call SetChangeMarks(1, 4)
1612 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1613 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1614 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1615 2,3!cat XtestFilter
1616 call assert_equal([2, 9], [line("'["), line("']")])
1617 au! FilterWritePre,FilterReadPre,FilterReadPost
1618 undo
1619
1620 call delete('XtestFilter')
1621 endif
1622
1623 call SetChangeMarks(1, 4)
1624 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1625 2,3write Xtest2
1626 au! FileWritePre
1627
1628 call SetChangeMarks(2, 3)
1629 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1630 write >> Xtest2
1631 au! FileAppendPre
1632
1633 call SetChangeMarks(1, 4)
1634 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1635 2,3write >> Xtest2
1636 au! FileAppendPre
1637
1638 call SetChangeMarks(1, 1)
1639 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1640 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1641 3read Xtest2
1642 au! FileReadPre,FileReadPost
1643 undo
1644
1645 call SetChangeMarks(4, 4)
1646 " When the line is 0, it's adjusted to 1
1647 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1648 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1649 0read Xtest2
1650 au! FileReadPre,FileReadPost
1651 undo
1652
1653 call SetChangeMarks(4, 4)
1654 " When the line is 0, it's adjusted to 1
1655 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1656 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1657 1read Xtest2
1658 au! FileReadPre,FileReadPost
1659 undo
1660
1661 bwipe!
1662 call delete('Xtest')
1663 call delete('Xtest2')
1664endfunc
1665
1666func Test_Filter_noshelltemp()
1667 if !executable('cat')
1668 return
1669 endif
1670
1671 enew!
1672 call setline(1, ['a', 'b', 'c', 'd'])
1673
1674 let shelltemp = &shelltemp
1675 set shelltemp
1676
1677 let g:filter_au = 0
1678 au FilterWritePre * let g:filter_au += 1
1679 au FilterReadPre * let g:filter_au += 1
1680 au FilterReadPost * let g:filter_au += 1
1681 %!cat
1682 call assert_equal(3, g:filter_au)
1683
1684 if has('filterpipe')
1685 set noshelltemp
1686
1687 let g:filter_au = 0
1688 au FilterWritePre * let g:filter_au += 1
1689 au FilterReadPre * let g:filter_au += 1
1690 au FilterReadPost * let g:filter_au += 1
1691 %!cat
1692 call assert_equal(0, g:filter_au)
1693 endif
1694
1695 au! FilterWritePre,FilterReadPre,FilterReadPost
1696 let &shelltemp = shelltemp
1697 bwipe!
1698endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001699
1700func Test_TextYankPost()
1701 enew!
1702 call setline(1, ['foo'])
1703
1704 let g:event = []
1705 au TextYankPost * let g:event = copy(v:event)
1706
1707 call assert_equal({}, v:event)
1708 call assert_fails('let v:event = {}', 'E46:')
1709 call assert_fails('let v:event.mykey = 0', 'E742:')
1710
1711 norm "ayiw
1712 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001713 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001714 \g:event)
1715 norm y_
1716 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001717 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false},
1718 \g:event)
1719 norm Vy
1720 call assert_equal(
1721 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001722 \g:event)
1723 call feedkeys("\<C-V>y", 'x')
1724 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001725 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001726 \g:event)
1727 norm "xciwbar
1728 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001729 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001730 \g:event)
1731 norm "bdiw
1732 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001733 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001734 \g:event)
1735
1736 call assert_equal({}, v:event)
1737
1738 au! TextYankPost
1739 unlet g:event
1740 bwipe!
1741endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001742
1743func Test_nocatch_wipe_all_buffers()
1744 " Real nasty autocommand: wipe all buffers on any event.
1745 au * * bwipe *
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001746 call assert_fails('next x', ['E94:', 'E517:'])
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001747 bwipe
1748 au!
1749endfunc
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001750
1751func Test_nocatch_wipe_dummy_buffer()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001752 if has('quickfix')
1753 " Nasty autocommand: wipe buffer on any event.
1754 au * x bwipe
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02001755 call assert_fails('lv½ /x', 'E937')
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001756 au!
1757 endif
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001758endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001759
1760function s:Before_test_dirchanged()
1761 augroup test_dirchanged
1762 autocmd!
1763 augroup END
1764 let s:li = []
1765 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001766 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001767 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001768 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001769 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001770endfunc
1771
1772function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001773 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001774 call delete(s:dir_foo, 'd')
1775 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001776 augroup test_dirchanged
1777 autocmd!
1778 augroup END
1779endfunc
1780
1781function Test_dirchanged_global()
1782 call s:Before_test_dirchanged()
1783 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1784 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001785 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001786 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001787 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001788 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001789 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001790 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001791 call s:After_test_dirchanged()
1792endfunc
1793
1794function Test_dirchanged_local()
1795 call s:Before_test_dirchanged()
1796 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1797 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001798 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001799 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001800 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001801 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001802 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001803 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001804 call s:After_test_dirchanged()
1805endfunc
1806
1807function Test_dirchanged_auto()
Bram Moolenaarec48a9c2018-02-03 20:11:40 +01001808 if !exists('+autochdir')
1809 return
1810 endif
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001811 call s:Before_test_dirchanged()
1812 call test_autochdir()
1813 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1814 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1815 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001816 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001817 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001818 exe 'edit ' . s:dir_foo . '/Xfile'
1819 call assert_equal(s:dir_foo, getcwd())
1820 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001821 set noacd
1822 bwipe!
1823 call s:After_test_dirchanged()
1824endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001825
1826" Test TextChangedI and TextChangedP
1827func Test_ChangedP()
1828 new
1829 call setline(1, ['foo', 'bar', 'foobar'])
1830 call test_override("char_avail", 1)
1831 set complete=. completeopt=menuone
1832
1833 func! TextChangedAutocmd(char)
1834 let g:autocmd .= a:char
1835 endfunc
1836
1837 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1838 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1839 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1840
1841 call cursor(3, 1)
1842 let g:autocmd = ''
1843 call feedkeys("o\<esc>", 'tnix')
1844 call assert_equal('I', g:autocmd)
1845
1846 let g:autocmd = ''
1847 call feedkeys("Sf", 'tnix')
1848 call assert_equal('II', g:autocmd)
1849
1850 let g:autocmd = ''
1851 call feedkeys("Sf\<C-N>", 'tnix')
1852 call assert_equal('IIP', g:autocmd)
1853
1854 let g:autocmd = ''
1855 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1856 call assert_equal('IIPP', g:autocmd)
1857
1858 let g:autocmd = ''
1859 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1860 call assert_equal('IIPPP', g:autocmd)
1861
1862 let g:autocmd = ''
1863 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1864 call assert_equal('IIPPPP', g:autocmd)
1865
1866 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1867 " TODO: how should it handle completeopt=noinsert,noselect?
1868
1869 " CleanUp
1870 call test_override("char_avail", 0)
1871 au! TextChanged
1872 au! TextChangedI
1873 au! TextChangedP
1874 delfu TextChangedAutocmd
1875 unlet! g:autocmd
1876 set complete&vim completeopt&vim
1877
1878 bw!
1879endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001880
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001881let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001882func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001883 if !g:setline_handled
1884 call setline(1, "(x)")
1885 let g:setline_handled = v:true
1886 endif
1887endfunc
1888
1889func Test_TextChangedI_with_setline()
1890 new
1891 call test_override('char_avail', 1)
1892 autocmd TextChangedI <buffer> call SetLineOne()
1893 call feedkeys("i(\<CR>\<Esc>", 'tx')
1894 call assert_equal('(', getline(1))
1895 call assert_equal('x)', getline(2))
1896 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001897 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001898 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001899
1900 call test_override('starting', 0)
1901 bwipe!
1902endfunc
1903
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001904func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001905 CheckFeature terminal
1906 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01001907 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01001908 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001909
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001910 " Prepare file for TextChanged event.
1911 call writefile([''], 'Xchanged.txt')
1912 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1913 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001914 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001915 " In ConPTY, there is additional character which is drawn up to the width of
1916 " the screen.
1917 if has('conpty')
1918 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
1919 else
1920 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
1921 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001922 " It's only adding autocmd, so that no event occurs.
1923 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1924 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001925 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001926 call assert_equal([''], readfile('Xchanged.txt'))
1927
1928 " clean up
1929 call delete('Xchanged.txt')
1930 bwipe!
1931endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01001932
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001933func Test_autocmd_nested()
1934 let g:did_nested = 0
1935 augroup Testing
1936 au WinNew * edit somefile
1937 au BufNew * let g:did_nested = 1
1938 augroup END
1939 split
1940 call assert_equal(0, g:did_nested)
1941 close
1942 bwipe! somefile
1943
1944 " old nested argument still works
1945 augroup Testing
1946 au!
1947 au WinNew * nested edit somefile
1948 au BufNew * let g:did_nested = 1
1949 augroup END
1950 split
1951 call assert_equal(1, g:did_nested)
1952 close
1953 bwipe! somefile
1954
1955 " New ++nested argument works
1956 augroup Testing
1957 au!
1958 au WinNew * ++nested edit somefile
1959 au BufNew * let g:did_nested = 1
1960 augroup END
1961 split
1962 call assert_equal(1, g:did_nested)
1963 close
1964 bwipe! somefile
1965
1966 augroup Testing
1967 au!
1968 augroup END
1969
1970 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
1971 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
1972endfunc
1973
1974func Test_autocmd_once()
1975 " Without ++once WinNew triggers twice
1976 let g:did_split = 0
1977 augroup Testing
1978 au WinNew * let g:did_split += 1
1979 augroup END
1980 split
1981 split
1982 call assert_equal(2, g:did_split)
1983 call assert_true(exists('#WinNew'))
1984 close
1985 close
1986
1987 " With ++once WinNew triggers once
1988 let g:did_split = 0
1989 augroup Testing
1990 au!
1991 au WinNew * ++once let g:did_split += 1
1992 augroup END
1993 split
1994 split
1995 call assert_equal(1, g:did_split)
1996 call assert_false(exists('#WinNew'))
1997 close
1998 close
1999
2000 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2001endfunc
2002
Bram Moolenaara68e5952019-04-25 22:22:01 +02002003func Test_autocmd_bufreadpre()
2004 new
2005 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002006 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002007 w! XAutocmdBufReadPre.txt
2008 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002009 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002010 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002011 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002012 wincmd p
2013 let g:wsv1 = winsaveview()
2014 wincmd p
2015 let g:wsv2 = winsaveview()
2016 " triggers BufReadPre, should not move the cursor in either window
2017 " The topline may change one line in a large window.
2018 edit
2019 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2020 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2021 call assert_equal(2, b:bufreadpre)
2022 wincmd p
2023 call assert_equal(g:wsv1.topline, winsaveview().topline)
2024 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2025 call assert_equal(2, b:bufreadpre)
2026 " Now set the cursor position in an BufReadPre autocommand
2027 " (even though the position will be invalid, this should make Vim reset the
2028 " cursor position in the other window.
2029 wincmd p
2030 set cpo+=g
2031 " won't do anything, but try to set the cursor on an invalid lnum
2032 autocmd BufReadPre <buffer> :norm! 70gg
2033 " triggers BufReadPre, should not move the cursor in either window
2034 e
2035 call assert_equal(1, winsaveview().topline)
2036 call assert_equal(1, winsaveview().lnum)
2037 call assert_equal(3, b:bufreadpre)
2038 wincmd p
2039 call assert_equal(g:wsv1.topline, winsaveview().topline)
2040 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2041 call assert_equal(3, b:bufreadpre)
2042 close
2043 close
2044 call delete('XAutocmdBufReadPre.txt')
2045 set cpo-=g
2046endfunc
2047
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002048" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002049
2050" Tests for the following autocommands:
2051" - FileWritePre writing a compressed file
2052" - FileReadPost reading a compressed file
2053" - BufNewFile reading a file template
2054" - BufReadPre decompressing the file to be read
2055" - FilterReadPre substituting characters in the temp file
2056" - FilterReadPost substituting characters after filtering
2057" - FileReadPre set options for decompression
2058" - FileReadPost decompress the file
2059func Test_ReadWrite_Autocmds()
2060 " Run this test only on Unix-like systems and if gzip is available
2061 if !has('unix') || !executable("gzip")
2062 return
2063 endif
2064
2065 " Make $GZIP empty, "-v" would cause trouble.
2066 let $GZIP = ""
2067
2068 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2069 " being modified outside of Vim (noticed on Solaris).
2070 au FileChangedShell * echo 'caught FileChangedShell'
2071
2072 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2073 augroup Test1
2074 au!
2075 au FileWritePre *.gz '[,']!gzip
2076 au FileWritePost *.gz undo
2077 au FileReadPost *.gz '[,']!gzip -d
2078 augroup END
2079
2080 new
2081 set bin
2082 call append(0, [
2083 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2084 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2085 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2086 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2087 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2088 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2089 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2090 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2091 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2092 \ ])
2093 1,9write! Xtestfile.gz
2094 enew! | close
2095
2096 new
2097 " Read and decompress the testfile
2098 0read Xtestfile.gz
2099 call assert_equal([
2100 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2101 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2102 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2103 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2104 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2105 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2106 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2107 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2108 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2109 \ ], getline(1, 9))
2110 enew! | close
2111
2112 augroup Test1
2113 au!
2114 augroup END
2115
2116 " Test for the FileAppendPre and FileAppendPost autocmds
2117 augroup Test2
2118 au!
2119 au BufNewFile *.c read Xtest.c
2120 au FileAppendPre *.out '[,']s/new/NEW/
2121 au FileAppendPost *.out !cat Xtest.c >> test.out
2122 augroup END
2123
2124 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2125 new foo.c " should load Xtest.c
2126 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2127 w! >> test.out " append it to the output file
2128
2129 let contents = readfile('test.out')
2130 call assert_equal(' * Here is a NEW .c file', contents[2])
2131 call assert_equal(' * Here is a new .c file', contents[5])
2132
2133 call delete('test.out')
2134 enew! | close
2135 augroup Test2
2136 au!
2137 augroup END
2138
2139 " Test for the BufReadPre and BufReadPost autocmds
2140 augroup Test3
2141 au!
2142 " setup autocommands to decompress before reading and re-compress
2143 " afterwards
2144 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2145 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2146 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2147 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2148 augroup END
2149
2150 e! Xtestfile.gz " Edit compressed file
2151 call assert_equal([
2152 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2153 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2154 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2155 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2156 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2157 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2158 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2159 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2160 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2161 \ ], getline(1, 9))
2162
2163 w! >> test.out " Append it to the output file
2164
2165 augroup Test3
2166 au!
2167 augroup END
2168
2169 " Test for the FilterReadPre and FilterReadPost autocmds.
2170 set shelltemp " need temp files here
2171 augroup Test4
2172 au!
2173 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2174 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2175 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2176 au FilterReadPost *.out '[,']s/x/X/g
2177 augroup END
2178
2179 e! test.out " Edit the output file
2180 1,$!cat
2181 call assert_equal([
2182 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2183 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2184 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2185 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2186 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2187 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2188 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2189 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2190 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2191 \ ], getline(1, 9))
2192 call assert_equal([
2193 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2194 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2195 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2196 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2197 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2198 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2199 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2200 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2201 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2202 \ ], readfile('test.out'))
2203
2204 augroup Test4
2205 au!
2206 augroup END
2207 set shelltemp&vim
2208
2209 " Test for the FileReadPre and FileReadPost autocmds.
2210 augroup Test5
2211 au!
2212 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2213 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2214 au FileReadPost *.gz '[,']s/l/L/
2215 augroup END
2216
2217 new
2218 0r Xtestfile.gz " Read compressed file
2219 call assert_equal([
2220 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2221 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2222 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2223 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2224 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2225 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2226 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2227 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2228 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2229 \ ], getline(1, 9))
2230 call assert_equal([
2231 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2232 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2233 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2234 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2235 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2236 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2237 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2238 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2239 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2240 \ ], readfile('Xtestfile.gz'))
2241
2242 augroup Test5
2243 au!
2244 augroup END
2245
2246 au! FileChangedShell
2247 call delete('Xtestfile.gz')
2248 call delete('Xtest.c')
2249 call delete('test.out')
2250endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002251
2252func Test_throw_in_BufWritePre()
2253 new
2254 call setline(1, ['one', 'two', 'three'])
2255 call assert_false(filereadable('Xthefile'))
2256 augroup throwing
2257 au BufWritePre X* throw 'do not write'
2258 augroup END
2259 try
2260 w Xthefile
2261 catch
2262 let caught = 1
2263 endtry
2264 call assert_equal(1, caught)
2265 call assert_false(filereadable('Xthefile'))
2266
2267 bwipe!
2268 au! throwing
2269endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002270
2271func Test_autocmd_SafeState()
2272 CheckRunVimInTerminal
2273
2274 let lines =<< trim END
2275 let g:safe = 0
2276 let g:again = ''
2277 au SafeState * let g:safe += 1
2278 au SafeStateAgain * let g:again ..= 'x'
2279 func CallTimer()
2280 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2281 endfunc
2282 END
2283 call writefile(lines, 'XSafeState')
2284 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2285
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002286 " Sometimes we loop to handle a K_IGNORE, SafeState may be trigered once or
2287 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002288 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002289 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002290
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002291 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002292 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002293 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002294
2295 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002296 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002297 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002298 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002299 call term_sendkeys(buf, ":echo g:again\<CR>")
2300 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2301
2302 call StopVimInTerminal(buf)
2303 call delete('XSafeState')
2304endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002305
2306func Test_autocmd_CmdWinEnter()
2307 CheckRunVimInTerminal
2308 " There is not cmdwin switch, so
2309 " test for cmdline_hist
2310 " (both are available with small builds)
2311 CheckFeature cmdline_hist
2312 let lines =<< trim END
2313 let b:dummy_var = 'This is a dummy'
2314 autocmd CmdWinEnter * quit
2315 let winnr = winnr('$')
2316 END
2317 let filename='XCmdWinEnter'
2318 call writefile(lines, filename)
2319 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2320
2321 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002322 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002323 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002324 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002325 call term_sendkeys(buf, ":echo &buftype\<cr>")
2326 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2327 call term_sendkeys(buf, ":echo winnr\<cr>")
2328 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2329
2330 " clean up
2331 call StopVimInTerminal(buf)
2332 call delete(filename)
2333endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002334
2335func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002336 CheckFeature quickfix
2337
Bram Moolenaarec66c412019-10-11 21:19:13 +02002338 pedit xx
2339 n x
2340 au WinEnter * quit
2341 split
2342 au! WinEnter
2343endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002344
2345func Test_BufWrite_lockmarks()
2346 edit! Xtest
2347 call setline(1, ['a', 'b', 'c', 'd'])
2348
2349 " :lockmarks preserves the marks
2350 call SetChangeMarks(2, 3)
2351 lockmarks write
2352 call assert_equal([2, 3], [line("'["), line("']")])
2353
2354 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2355 " original values for the user
2356 augroup lockmarks
2357 au!
2358 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2359 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2360 augroup END
2361
2362 lockmarks write
2363 call assert_equal([2, 3], [line("'["), line("']")])
2364
2365 if executable('cat')
2366 lockmarks %!cat
2367 call assert_equal([2, 3], [line("'["), line("']")])
2368 endif
2369
2370 lockmarks 3,4write Xtest2
2371 call assert_equal([2, 3], [line("'["), line("']")])
2372
2373 au! lockmarks
2374 augroup! lockmarks
2375 call delete('Xtest')
2376 call delete('Xtest2')
2377endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002378
2379func Test_FileType_spell()
2380 if !isdirectory('/tmp')
2381 throw "Skipped: requires /tmp directory"
2382 endif
2383
2384 " this was crashing with an invalid free()
2385 setglobal spellfile=/tmp/en.utf-8.add
2386 augroup crash
2387 autocmd!
2388 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2389 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2390 autocmd FileType anotherfiletype setlocal spell
2391 augroup END
2392 func! NoCrash() abort
2393 edit /tmp/crashfile
2394 endfunc
2395 call NoCrash()
2396
2397 au! crash
2398 setglobal spellfile=
2399endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002400
Bram Moolenaar406cd902020-02-18 21:54:41 +01002401" Test closing a window or editing another buffer from a FileChangedRO handler
2402" in a readonly buffer
2403func Test_FileChangedRO_winclose()
2404 augroup FileChangedROTest
2405 au!
2406 autocmd FileChangedRO * quit
2407 augroup END
2408 new
2409 set readonly
2410 call assert_fails('normal i', 'E788:')
2411 close
2412 augroup! FileChangedROTest
2413
2414 augroup FileChangedROTest
2415 au!
2416 autocmd FileChangedRO * edit Xfile
2417 augroup END
2418 new
2419 set readonly
2420 call assert_fails('normal i', 'E788:')
2421 close
2422 augroup! FileChangedROTest
2423endfunc
2424
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002425func LogACmd()
2426 call add(g:logged, line('$'))
2427endfunc
2428
2429func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002430 CheckNotGui
2431
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002432 enew!
2433 tabnew
2434 call setline(1, ['a', 'b', 'c', 'd'])
2435 $
2436 au TermChanged * call LogACmd()
2437 let g:logged = []
2438 let term_save = &term
2439 set term=xterm
2440 call assert_equal([1, 4], g:logged)
2441
2442 au! TermChanged
2443 let &term = term_save
2444 bwipe!
2445endfunc
2446
Bram Moolenaare3284872020-03-19 13:55:03 +01002447" Test for FileReadCmd autocmd
2448func Test_autocmd_FileReadCmd()
2449 func ReadFileCmd()
2450 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2451 endfunc
2452 augroup FileReadCmdTest
2453 au!
2454 au FileReadCmd Xtest call ReadFileCmd()
2455 augroup END
2456
2457 new
2458 read ++bin Xtest
2459 read ++nobin Xtest
2460 read ++edit Xtest
2461 read ++bad=keep Xtest
2462 read ++bad=drop Xtest
2463 read ++bad=- Xtest
2464 read ++ff=unix Xtest
2465 read ++ff=dos Xtest
2466 read ++ff=mac Xtest
2467 read ++enc=utf-8 Xtest
2468
2469 call assert_equal(['',
2470 \ 'v:cmdarg = ++bin',
2471 \ 'v:cmdarg = ++nobin',
2472 \ 'v:cmdarg = ++edit',
2473 \ 'v:cmdarg = ++bad=keep',
2474 \ 'v:cmdarg = ++bad=drop',
2475 \ 'v:cmdarg = ++bad=-',
2476 \ 'v:cmdarg = ++ff=unix',
2477 \ 'v:cmdarg = ++ff=dos',
2478 \ 'v:cmdarg = ++ff=mac',
2479 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2480
2481 close!
2482 augroup FileReadCmdTest
2483 au!
2484 augroup END
2485 delfunc ReadFileCmd
2486endfunc
2487
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002488" Test for passing invalid arguments to autocmd
2489func Test_autocmd_invalid_args()
2490 " Additional character after * for event
2491 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2492 augroup Test
2493 augroup END
2494 " Invalid autocmd event
2495 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2496 " Invalid autocmd event in a autocmd group
2497 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2498 augroup! Test
2499 " Execute all autocmds
2500 call assert_fails('doautocmd * BufEnter', 'E217:')
2501 call assert_fails('augroup! x1a2b3', 'E367:')
2502 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
2503endfunc
2504
2505" Test for deep nesting of autocmds
2506func Test_autocmd_deep_nesting()
2507 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2508 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2509 autocmd! BufEnter Xfile
2510endfunc
2511
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002512" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2513func Test_autocmd_sigusr1()
2514 CheckUnix
2515
2516 let g:sigusr1_passed = 0
2517 au SigUSR1 * let g:sigusr1_passed = 1
2518 call system('/bin/kill -s usr1 ' . getpid())
2519 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2520
2521 au! SigUSR1
2522 unlet g:sigusr1_passed
2523endfunc
2524
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002525" Test for BufReadPre autocmd deleting the file
2526func Test_BufReadPre_delfile()
2527 augroup TestAuCmd
2528 au!
2529 autocmd BufReadPre Xfile call delete('Xfile')
2530 augroup END
2531 call writefile([], 'Xfile')
2532 call assert_fails('new Xfile', 'E200:')
2533 call assert_equal('Xfile', @%)
2534 call assert_equal(1, &readonly)
2535 call delete('Xfile')
2536 augroup TestAuCmd
2537 au!
2538 augroup END
2539 close!
2540endfunc
2541
2542" Test for BufReadPre autocmd changing the current buffer
2543func Test_BufReadPre_changebuf()
2544 augroup TestAuCmd
2545 au!
2546 autocmd BufReadPre Xfile edit Xsomeotherfile
2547 augroup END
2548 call writefile([], 'Xfile')
2549 call assert_fails('new Xfile', 'E201:')
2550 call assert_equal('Xsomeotherfile', @%)
2551 call assert_equal(1, &readonly)
2552 call delete('Xfile')
2553 augroup TestAuCmd
2554 au!
2555 augroup END
2556 close!
2557endfunc
2558
2559" Test for BufWipeouti autocmd changing the current buffer when reading a file
2560" in an empty buffer with 'f' flag in 'cpo'
2561func Test_BufDelete_changebuf()
2562 new
2563 augroup TestAuCmd
2564 au!
2565 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2566 augroup END
2567 let save_cpo = &cpo
2568 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002569 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002570 call assert_equal('somefile', @%)
2571 let &cpo = save_cpo
2572 augroup TestAuCmd
2573 au!
2574 augroup END
2575 close!
2576endfunc
2577
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002578" Test for the temporary internal window used to execute autocmds
2579func Test_autocmd_window()
2580 %bw!
2581 edit one.txt
2582 tabnew two.txt
2583 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002584 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002585 au!
2586 au BufEnter * call add(g:blist, [expand('<afile>'),
2587 \ win_gettype(bufwinnr(expand('<afile>')))])
2588 augroup END
2589
2590 doautoall BufEnter
Bram Moolenaar40a019f2020-06-17 21:41:35 +02002591 call assert_equal([['one.txt', 'autocmd'], ['two.txt', '']], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002592
Bram Moolenaar832adf92020-06-25 19:01:36 +02002593 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002594 au!
2595 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002596 augroup! aucmd_win_test1
2597 %bw!
2598endfunc
2599
2600" Test for trying to close the temporary window used for executing an autocmd
2601func Test_close_autocmd_window()
2602 %bw!
2603 edit one.txt
2604 tabnew two.txt
2605 augroup aucmd_win_test2
2606 au!
2607 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2608 augroup END
2609
2610 call assert_fails('doautoall BufEnter', 'E813:')
2611
2612 augroup aucmd_win_test2
2613 au!
2614 augroup END
2615 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002616 %bwipe!
2617endfunc
2618
2619" Test for trying to close the tab that has the temporary window for exeucing
2620" an autocmd.
2621func Test_close_autocmd_tab()
2622 edit one.txt
2623 tabnew two.txt
2624 augroup aucmd_win_test
2625 au!
2626 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2627 augroup END
2628
2629 call assert_fails('doautoall BufEnter', 'E813:')
2630
2631 tabonly
2632 augroup aucmd_win_test
2633 au!
2634 augroup END
2635 augroup! aucmd_win_test
2636 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002637endfunc
2638
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002639" vim: shiftwidth=2 sts=2 expandtab