blob: 1f901a9ffa6a2805a9d976d288d9784e54c77fb5 [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 Moolenaara997b452018-04-17 23:24:06 +0200171 " Todo: check for E937 generated first
172 " call assert_fails('edit bb.txt', 'E937:')
173 call assert_fails('edit bb.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200174
175 autocmd! test_autocmd_bufunload
176 augroup! test_autocmd_bufunload
177 bwipe! aa.txt
178 bwipe! bb.txt
179endfunc
180
181" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200182func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200183 setlocal buftype=nowrite
184 let lastbuf = bufnr('$')
185
186 augroup test_autocmd_bufunload
187 autocmd!
188 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
189 augroup END
190
191 normal! i1
192 call assert_fails('edit a.txt', 'E517:')
193 call feedkeys("\<CR>")
194
195 autocmd! test_autocmd_bufunload
196 augroup! test_autocmd_bufunload
197 bwipe! a.txt
198endfunc
199
Bram Moolenaarc917da42016-07-19 22:31:36 +0200200func Test_win_tab_autocmd()
201 let g:record = []
202
203 augroup testing
204 au WinNew * call add(g:record, 'WinNew')
205 au WinEnter * call add(g:record, 'WinEnter')
206 au WinLeave * call add(g:record, 'WinLeave')
207 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200208 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200209 au TabEnter * call add(g:record, 'TabEnter')
210 au TabLeave * call add(g:record, 'TabLeave')
211 augroup END
212
213 split
214 tabnew
215 close
216 close
217
218 call assert_equal([
219 \ 'WinLeave', 'WinNew', 'WinEnter',
220 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200221 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200222 \ 'WinLeave', 'WinEnter'
223 \ ], g:record)
224
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200225 let g:record = []
226 tabnew somefile
227 tabnext
228 bwipe somefile
229
230 call assert_equal([
231 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
232 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
233 \ 'TabClosed'
234 \ ], g:record)
235
Bram Moolenaarc917da42016-07-19 22:31:36 +0200236 augroup testing
237 au!
238 augroup END
239 unlet g:record
240endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200241
242func s:AddAnAutocmd()
243 augroup vimBarTest
244 au BufReadCmd * echo 'hello'
245 augroup END
246 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
247endfunc
248
249func Test_early_bar()
250 " test that a bar is recognized before the {event}
251 call s:AddAnAutocmd()
252 augroup vimBarTest | au! | augroup END
253 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
254
255 call s:AddAnAutocmd()
256 augroup vimBarTest| au!| augroup END
257 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
258
259 " test that a bar is recognized after the {event}
260 call s:AddAnAutocmd()
261 augroup vimBarTest| au!BufReadCmd| augroup END
262 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
263
264 " test that a bar is recognized after the {group}
265 call s:AddAnAutocmd()
266 au! vimBarTest|echo 'hello'
267 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
268endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200269
Bram Moolenaar5c809082016-09-01 16:21:48 +0200270func RemoveGroup()
271 autocmd! StartOK
272 augroup! StartOK
273endfunc
274
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200275func Test_augroup_warning()
276 augroup TheWarning
277 au VimEnter * echo 'entering'
278 augroup END
279 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
280 redir => res
281 augroup! TheWarning
282 redir END
283 call assert_true(match(res, "W19:") >= 0)
284 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
285
286 " check "Another" does not take the pace of the deleted entry
287 augroup Another
288 augroup END
289 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200290 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200291
292 " no warning for postpone aucmd delete
293 augroup StartOK
294 au VimEnter * call RemoveGroup()
295 augroup END
296 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
297 redir => res
298 doautocmd VimEnter
299 redir END
300 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200301 au! VimEnter
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)
1160 bw!
1161 call assert_fails(':diffoff!', 'E788')
1162 bw!
1163
1164 " Cleanup
1165 au! OptionSet
1166 call test_override('starting', 0)
1167 "delfunc! AutoCommandOptionSet
1168endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001169
1170" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1171func Test_BufleaveWithDelete()
1172 new | edit Xfile1
1173
1174 augroup test_bufleavewithdelete
1175 autocmd!
1176 autocmd BufLeave Xfile1 bwipe Xfile2
1177 augroup END
1178
1179 call assert_fails('edit Xfile2', 'E143:')
1180 call assert_equal('Xfile1', bufname('%'))
1181
1182 autocmd! test_bufleavewithdelete BufLeave Xfile1
1183 augroup! test_bufleavewithdelete
1184
1185 new
1186 bwipe! Xfile1
1187endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001188
1189" Test for autocommand that changes the buffer list, when doing ":ball".
1190func Test_Acmd_BufAll()
1191 enew!
1192 %bwipe!
1193 call writefile(['Test file Xxx1'], 'Xxx1')
1194 call writefile(['Test file Xxx2'], 'Xxx2')
1195 call writefile(['Test file Xxx3'], 'Xxx3')
1196
1197 " Add three files to the buffer list
1198 split Xxx1
1199 close
1200 split Xxx2
1201 close
1202 split Xxx3
1203 close
1204
1205 " Wipe the buffer when the buffer is opened
1206 au BufReadPost Xxx2 bwipe
1207
1208 call append(0, 'Test file Xxx4')
1209 ball
1210
1211 call assert_equal(2, winnr('$'))
1212 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1213 wincmd t
1214
1215 au! BufReadPost
1216 %bwipe!
1217 call delete('Xxx1')
1218 call delete('Xxx2')
1219 call delete('Xxx3')
1220 enew! | only
1221endfunc
1222
1223" Test for autocommand that changes current buffer on BufEnter event.
1224" Check if modelines are interpreted for the correct buffer.
1225func Test_Acmd_BufEnter()
1226 %bwipe!
1227 call writefile(['start of test file Xxx1',
1228 \ "\<Tab>this is a test",
1229 \ 'end of test file Xxx1'], 'Xxx1')
1230 call writefile(['start of test file Xxx2',
1231 \ 'vim: set noai :',
1232 \ "\<Tab>this is a test",
1233 \ 'end of test file Xxx2'], 'Xxx2')
1234
1235 au BufEnter Xxx2 brew
1236 set ai modeline modelines=3
1237 edit Xxx1
1238 " edit Xxx2, autocmd will do :brew
1239 edit Xxx2
1240 exe "normal G?this is a\<CR>"
1241 " Append text with autoindent to this file
1242 normal othis should be auto-indented
1243 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1244 call assert_equal(3, line('.'))
1245 " Remove autocmd and edit Xxx2 again
1246 au! BufEnter Xxx2
1247 buf! Xxx2
1248 exe "normal G?this is a\<CR>"
1249 " append text without autoindent to Xxx
1250 normal othis should be in column 1
1251 call assert_equal("this should be in column 1", getline('.'))
1252 call assert_equal(4, line('.'))
1253
1254 %bwipe!
1255 call delete('Xxx1')
1256 call delete('Xxx2')
1257 set ai&vim modeline&vim modelines&vim
1258endfunc
1259
1260" Test for issue #57
1261" do not move cursor on <c-o> when autoindent is set
1262func Test_ai_CTRL_O()
1263 enew!
1264 set ai
1265 let save_fo = &fo
1266 set fo+=r
1267 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1268 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1269 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1270
1271 set ai&vim
1272 let &fo = save_fo
1273 enew!
1274endfunc
1275
1276" Test for autocommand that deletes the current buffer on BufLeave event.
1277" Also test deleting the last buffer, should give a new, empty buffer.
1278func Test_BufLeave_Wipe()
1279 %bwipe!
1280 let content = ['start of test file Xxx',
1281 \ 'this is a test',
1282 \ 'end of test file Xxx']
1283 call writefile(content, 'Xxx1')
1284 call writefile(content, 'Xxx2')
1285
1286 au BufLeave Xxx2 bwipe
1287 edit Xxx1
1288 split Xxx2
1289 " delete buffer Xxx2, we should be back to Xxx1
1290 bwipe
1291 call assert_equal('Xxx1', bufname('%'))
1292 call assert_equal(1, winnr('$'))
1293
1294 " Create an alternate buffer
1295 %write! test.out
1296 call assert_equal('test.out', bufname('#'))
1297 " delete alternate buffer
1298 bwipe test.out
1299 call assert_equal('Xxx1', bufname('%'))
1300 call assert_equal('', bufname('#'))
1301
1302 au BufLeave Xxx1 bwipe
1303 " delete current buffer, get an empty one
1304 bwipe!
1305 call assert_equal(1, line('$'))
1306 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001307 let g:bufinfo = getbufinfo()
1308 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001309
1310 call delete('Xxx1')
1311 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001312 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001313 %bwipe
1314 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001315
1316 " check that bufinfo doesn't contain a pointer to freed memory
1317 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001318endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001319
1320func Test_QuitPre()
1321 edit Xfoo
1322 let winid = win_getid(winnr())
1323 split Xbar
1324 au! QuitPre * let g:afile = expand('<afile>')
1325 " Close the other window, <afile> should be correct.
1326 exe win_id2win(winid) . 'q'
1327 call assert_equal('Xfoo', g:afile)
1328
1329 unlet g:afile
1330 bwipe Xfoo
1331 bwipe Xbar
1332endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001333
1334func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001335 au! CmdlineChanged : let g:text = getcmdline()
1336 let g:text = 0
1337 call feedkeys(":echom 'hello'\<CR>", 'xt')
1338 call assert_equal("echom 'hello'", g:text)
1339 au! CmdlineChanged
1340
1341 au! CmdlineChanged : let g:entered = expand('<afile>')
1342 let g:entered = 0
1343 call feedkeys(":echom 'hello'\<CR>", 'xt')
1344 call assert_equal(':', g:entered)
1345 au! CmdlineChanged
1346
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001347 au! CmdlineEnter : let g:entered = expand('<afile>')
1348 au! CmdlineLeave : let g:left = expand('<afile>')
1349 let g:entered = 0
1350 let g:left = 0
1351 call feedkeys(":echo 'hello'\<CR>", 'xt')
1352 call assert_equal(':', g:entered)
1353 call assert_equal(':', g:left)
1354 au! CmdlineEnter
1355 au! CmdlineLeave
1356
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001357 let save_shellslash = &shellslash
1358 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001359 au! CmdlineEnter / let g:entered = expand('<afile>')
1360 au! CmdlineLeave / let g:left = expand('<afile>')
1361 let g:entered = 0
1362 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001363 new
1364 call setline(1, 'hello')
1365 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001366 call assert_equal('/', g:entered)
1367 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001368 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001369 au! CmdlineEnter
1370 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001371 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001372endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001373
1374" Test for BufWritePre autocommand that deletes or unloads the buffer.
1375func Test_BufWritePre()
1376 %bwipe
1377 au BufWritePre Xxx1 bunload
1378 au BufWritePre Xxx2 bwipe
1379
1380 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1381 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1382
1383 edit Xtest
1384 e! Xxx2
1385 bdel Xtest
1386 e Xxx1
1387 " write it, will unload it and give an error msg
1388 call assert_fails('w', 'E203')
1389 call assert_equal('Xxx2', bufname('%'))
1390 edit Xtest
1391 e! Xxx2
1392 bwipe Xtest
1393 " write it, will delete the buffer and give an error msg
1394 call assert_fails('w', 'E203')
1395 call assert_equal('Xxx1', bufname('%'))
1396 au! BufWritePre
1397 call delete('Xxx1')
1398 call delete('Xxx2')
1399endfunc
1400
1401" Test for BufUnload autocommand that unloads all the other buffers
1402func Test_bufunload_all()
1403 call writefile(['Test file Xxx1'], 'Xxx1')"
1404 call writefile(['Test file Xxx2'], 'Xxx2')"
1405
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001406 let content =<< trim [CODE]
1407 func UnloadAllBufs()
1408 let i = 1
1409 while i <= bufnr('$')
1410 if i != bufnr('%') && bufloaded(i)
1411 exe i . 'bunload'
1412 endif
1413 let i += 1
1414 endwhile
1415 endfunc
1416 au BufUnload * call UnloadAllBufs()
1417 au VimLeave * call writefile(['Test Finished'], 'Xout')
1418 edit Xxx1
1419 split Xxx2
1420 q
1421 [CODE]
1422
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001423 call writefile(content, 'Xtest')
1424
1425 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001426 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001427 call assert_true(filereadable('Xout'))
1428
1429 call delete('Xxx1')
1430 call delete('Xxx2')
1431 call delete('Xtest')
1432 call delete('Xout')
1433endfunc
1434
1435" Some tests for buffer-local autocommands
1436func Test_buflocal_autocmd()
1437 let g:bname = ''
1438 edit xx
1439 au BufLeave <buffer> let g:bname = expand("%")
1440 " here, autocommand for xx should trigger.
1441 " but autocommand shall not apply to buffer named <buffer>.
1442 edit somefile
1443 call assert_equal('xx', g:bname)
1444 let g:bname = ''
1445 " here, autocommand shall be auto-deleted
1446 bwipe xx
1447 " autocmd should not trigger
1448 edit xx
1449 call assert_equal('', g:bname)
1450 " autocmd should not trigger
1451 edit somefile
1452 call assert_equal('', g:bname)
1453 enew
1454 unlet g:bname
1455endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001456
1457" Test for "*Cmd" autocommands
1458func Test_Cmd_Autocmds()
1459 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1460
1461 enew!
1462 au BufReadCmd XtestA 0r Xxx|$del
1463 edit XtestA " will read text of Xxd instead
1464 call assert_equal('start of Xxx', getline(1))
1465
1466 au BufWriteCmd XtestA call append(line("$"), "write")
1467 write " will append a line to the file
1468 call assert_equal('write', getline('$'))
1469 call assert_fails('read XtestA', 'E484') " should not read anything
1470 call assert_equal('write', getline(4))
1471
1472 " now we have:
1473 " 1 start of Xxx
1474 " 2 abc2
1475 " 3 end of Xxx
1476 " 4 write
1477
1478 au FileReadCmd XtestB '[r Xxx
1479 2r XtestB " will read Xxx below line 2 instead
1480 call assert_equal('start of Xxx', getline(3))
1481
1482 " now we have:
1483 " 1 start of Xxx
1484 " 2 abc2
1485 " 3 start of Xxx
1486 " 4 abc2
1487 " 5 end of Xxx
1488 " 6 end of Xxx
1489 " 7 write
1490
1491 au FileWriteCmd XtestC '[,']copy $
1492 normal 4GA1
1493 4,5w XtestC " will copy lines 4 and 5 to the end
1494 call assert_equal("\tabc21", getline(8))
1495 call assert_fails('r XtestC', 'E484') " should not read anything
1496 call assert_equal("end of Xxx", getline(9))
1497
1498 " now we have:
1499 " 1 start of Xxx
1500 " 2 abc2
1501 " 3 start of Xxx
1502 " 4 abc21
1503 " 5 end of Xxx
1504 " 6 end of Xxx
1505 " 7 write
1506 " 8 abc21
1507 " 9 end of Xxx
1508
1509 let g:lines = []
1510 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1511 w >>XtestD " will add lines to 'lines'
1512 call assert_equal(9, len(g:lines))
1513 call assert_fails('$r XtestD', 'E484') " should not read anything
1514 call assert_equal(9, line('$'))
1515 call assert_equal('end of Xxx', getline('$'))
1516
1517 au BufReadCmd XtestE 0r Xxx|$del
1518 sp XtestE " split window with test.out
1519 call assert_equal('end of Xxx', getline(3))
1520
1521 let g:lines = []
1522 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1523 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1524 wall " will write other window to 'lines'
1525 call assert_equal(4, len(g:lines), g:lines)
1526 call assert_equal('asdf', g:lines[2])
1527
1528 au! BufReadCmd
1529 au! BufWriteCmd
1530 au! FileReadCmd
1531 au! FileWriteCmd
1532 au! FileAppendCmd
1533 %bwipe!
1534 call delete('Xxx')
1535 enew!
1536endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001537
1538func SetChangeMarks(start, end)
1539 exe a:start. 'mark ['
1540 exe a:end. 'mark ]'
1541endfunc
1542
1543" Verify the effects of autocmds on '[ and ']
1544func Test_change_mark_in_autocmds()
1545 edit! Xtest
1546 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u", 'xtn')
1547
1548 call SetChangeMarks(2, 3)
1549 write
1550 call assert_equal([1, 4], [line("'["), line("']")])
1551
1552 call SetChangeMarks(2, 3)
1553 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1554 write
1555 au! BufWritePre
1556
1557 if executable('cat')
1558 write XtestFilter
1559 write >> XtestFilter
1560
1561 call SetChangeMarks(2, 3)
1562 " Marks are set to the entire range of the write
1563 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1564 " '[ is adjusted to just before the line that will receive the filtered
1565 " data
1566 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1567 " The filtered data is read into the buffer, and the source lines are
1568 " still present, so the range is after the source lines
1569 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1570 %!cat XtestFilter
1571 " After the filtered data is read, the original lines are deleted
1572 call assert_equal([1, 8], [line("'["), line("']")])
1573 au! FilterWritePre,FilterReadPre,FilterReadPost
1574 undo
1575
1576 call SetChangeMarks(1, 4)
1577 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1578 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1579 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1580 2,3!cat XtestFilter
1581 call assert_equal([2, 9], [line("'["), line("']")])
1582 au! FilterWritePre,FilterReadPre,FilterReadPost
1583 undo
1584
1585 call delete('XtestFilter')
1586 endif
1587
1588 call SetChangeMarks(1, 4)
1589 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1590 2,3write Xtest2
1591 au! FileWritePre
1592
1593 call SetChangeMarks(2, 3)
1594 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1595 write >> Xtest2
1596 au! FileAppendPre
1597
1598 call SetChangeMarks(1, 4)
1599 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1600 2,3write >> Xtest2
1601 au! FileAppendPre
1602
1603 call SetChangeMarks(1, 1)
1604 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1605 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1606 3read Xtest2
1607 au! FileReadPre,FileReadPost
1608 undo
1609
1610 call SetChangeMarks(4, 4)
1611 " When the line is 0, it's adjusted to 1
1612 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1613 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1614 0read Xtest2
1615 au! FileReadPre,FileReadPost
1616 undo
1617
1618 call SetChangeMarks(4, 4)
1619 " When the line is 0, it's adjusted to 1
1620 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1621 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1622 1read Xtest2
1623 au! FileReadPre,FileReadPost
1624 undo
1625
1626 bwipe!
1627 call delete('Xtest')
1628 call delete('Xtest2')
1629endfunc
1630
1631func Test_Filter_noshelltemp()
1632 if !executable('cat')
1633 return
1634 endif
1635
1636 enew!
1637 call setline(1, ['a', 'b', 'c', 'd'])
1638
1639 let shelltemp = &shelltemp
1640 set shelltemp
1641
1642 let g:filter_au = 0
1643 au FilterWritePre * let g:filter_au += 1
1644 au FilterReadPre * let g:filter_au += 1
1645 au FilterReadPost * let g:filter_au += 1
1646 %!cat
1647 call assert_equal(3, g:filter_au)
1648
1649 if has('filterpipe')
1650 set noshelltemp
1651
1652 let g:filter_au = 0
1653 au FilterWritePre * let g:filter_au += 1
1654 au FilterReadPre * let g:filter_au += 1
1655 au FilterReadPost * let g:filter_au += 1
1656 %!cat
1657 call assert_equal(0, g:filter_au)
1658 endif
1659
1660 au! FilterWritePre,FilterReadPre,FilterReadPost
1661 let &shelltemp = shelltemp
1662 bwipe!
1663endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001664
1665func Test_TextYankPost()
1666 enew!
1667 call setline(1, ['foo'])
1668
1669 let g:event = []
1670 au TextYankPost * let g:event = copy(v:event)
1671
1672 call assert_equal({}, v:event)
1673 call assert_fails('let v:event = {}', 'E46:')
1674 call assert_fails('let v:event.mykey = 0', 'E742:')
1675
1676 norm "ayiw
1677 call assert_equal(
1678 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v'},
1679 \g:event)
1680 norm y_
1681 call assert_equal(
1682 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V'},
1683 \g:event)
1684 call feedkeys("\<C-V>y", 'x')
1685 call assert_equal(
1686 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161"},
1687 \g:event)
1688 norm "xciwbar
1689 call assert_equal(
1690 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v'},
1691 \g:event)
1692 norm "bdiw
1693 call assert_equal(
1694 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v'},
1695 \g:event)
1696
1697 call assert_equal({}, v:event)
1698
1699 au! TextYankPost
1700 unlet g:event
1701 bwipe!
1702endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001703
1704func Test_nocatch_wipe_all_buffers()
1705 " Real nasty autocommand: wipe all buffers on any event.
1706 au * * bwipe *
Bram Moolenaara997b452018-04-17 23:24:06 +02001707 " Get E93 first?
1708 " call assert_fails('next x', 'E93:')
1709 call assert_fails('next x', 'E517:')
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001710 bwipe
1711 au!
1712endfunc
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001713
1714func Test_nocatch_wipe_dummy_buffer()
1715 " Nasty autocommand: wipe buffer on any event.
1716 au * x bwipe
1717 call assert_fails('lv½ /x', 'E480')
1718 au!
1719endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001720
1721function s:Before_test_dirchanged()
1722 augroup test_dirchanged
1723 autocmd!
1724 augroup END
1725 let s:li = []
1726 let s:dir_this = getcwd()
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001727 let s:dir_foo = s:dir_this . '/foo'
1728 call mkdir(s:dir_foo)
1729 let s:dir_bar = s:dir_this . '/bar'
1730 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001731endfunc
1732
1733function s:After_test_dirchanged()
1734 exe 'cd' s:dir_this
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001735 call delete(s:dir_foo, 'd')
1736 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001737 augroup test_dirchanged
1738 autocmd!
1739 augroup END
1740endfunc
1741
1742function Test_dirchanged_global()
1743 call s:Before_test_dirchanged()
1744 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1745 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001746 exe 'cd' s:dir_foo
1747 call assert_equal(["cd:", s:dir_foo], s:li)
1748 exe 'cd' s:dir_foo
1749 call assert_equal(["cd:", s:dir_foo], s:li)
1750 exe 'lcd' s:dir_bar
1751 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001752 call s:After_test_dirchanged()
1753endfunc
1754
1755function Test_dirchanged_local()
1756 call s:Before_test_dirchanged()
1757 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1758 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001759 exe 'cd' s:dir_foo
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001760 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001761 exe 'lcd' s:dir_bar
1762 call assert_equal(["lcd:", s:dir_bar], s:li)
1763 exe 'lcd' s:dir_bar
1764 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001765 call s:After_test_dirchanged()
1766endfunc
1767
1768function Test_dirchanged_auto()
Bram Moolenaarec48a9c2018-02-03 20:11:40 +01001769 if !exists('+autochdir')
1770 return
1771 endif
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001772 call s:Before_test_dirchanged()
1773 call test_autochdir()
1774 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1775 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1776 set acd
1777 exe 'cd ..'
1778 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001779 exe 'edit ' . s:dir_foo . '/Xfile'
1780 call assert_equal(s:dir_foo, getcwd())
1781 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001782 set noacd
1783 bwipe!
1784 call s:After_test_dirchanged()
1785endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001786
1787" Test TextChangedI and TextChangedP
1788func Test_ChangedP()
1789 new
1790 call setline(1, ['foo', 'bar', 'foobar'])
1791 call test_override("char_avail", 1)
1792 set complete=. completeopt=menuone
1793
1794 func! TextChangedAutocmd(char)
1795 let g:autocmd .= a:char
1796 endfunc
1797
1798 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1799 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1800 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1801
1802 call cursor(3, 1)
1803 let g:autocmd = ''
1804 call feedkeys("o\<esc>", 'tnix')
1805 call assert_equal('I', g:autocmd)
1806
1807 let g:autocmd = ''
1808 call feedkeys("Sf", 'tnix')
1809 call assert_equal('II', g:autocmd)
1810
1811 let g:autocmd = ''
1812 call feedkeys("Sf\<C-N>", 'tnix')
1813 call assert_equal('IIP', g:autocmd)
1814
1815 let g:autocmd = ''
1816 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1817 call assert_equal('IIPP', g:autocmd)
1818
1819 let g:autocmd = ''
1820 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1821 call assert_equal('IIPPP', g:autocmd)
1822
1823 let g:autocmd = ''
1824 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1825 call assert_equal('IIPPPP', g:autocmd)
1826
1827 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1828 " TODO: how should it handle completeopt=noinsert,noselect?
1829
1830 " CleanUp
1831 call test_override("char_avail", 0)
1832 au! TextChanged
1833 au! TextChangedI
1834 au! TextChangedP
1835 delfu TextChangedAutocmd
1836 unlet! g:autocmd
1837 set complete&vim completeopt&vim
1838
1839 bw!
1840endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001841
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001842let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001843func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001844 if !g:setline_handled
1845 call setline(1, "(x)")
1846 let g:setline_handled = v:true
1847 endif
1848endfunc
1849
1850func Test_TextChangedI_with_setline()
1851 new
1852 call test_override('char_avail', 1)
1853 autocmd TextChangedI <buffer> call SetLineOne()
1854 call feedkeys("i(\<CR>\<Esc>", 'tx')
1855 call assert_equal('(', getline(1))
1856 call assert_equal('x)', getline(2))
1857 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001858 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001859 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001860
1861 call test_override('starting', 0)
1862 bwipe!
1863endfunc
1864
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001865func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001866 CheckFeature terminal
1867 CheckNotGui
1868
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001869 " Prepare file for TextChanged event.
1870 call writefile([''], 'Xchanged.txt')
1871 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1872 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001873 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001874 " In ConPTY, there is additional character which is drawn up to the width of
1875 " the screen.
1876 if has('conpty')
1877 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
1878 else
1879 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
1880 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001881 " It's only adding autocmd, so that no event occurs.
1882 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1883 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001884 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001885 call assert_equal([''], readfile('Xchanged.txt'))
1886
1887 " clean up
1888 call delete('Xchanged.txt')
1889 bwipe!
1890endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01001891
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001892func Test_autocmd_nested()
1893 let g:did_nested = 0
1894 augroup Testing
1895 au WinNew * edit somefile
1896 au BufNew * let g:did_nested = 1
1897 augroup END
1898 split
1899 call assert_equal(0, g:did_nested)
1900 close
1901 bwipe! somefile
1902
1903 " old nested argument still works
1904 augroup Testing
1905 au!
1906 au WinNew * nested edit somefile
1907 au BufNew * let g:did_nested = 1
1908 augroup END
1909 split
1910 call assert_equal(1, g:did_nested)
1911 close
1912 bwipe! somefile
1913
1914 " New ++nested argument works
1915 augroup Testing
1916 au!
1917 au WinNew * ++nested edit somefile
1918 au BufNew * let g:did_nested = 1
1919 augroup END
1920 split
1921 call assert_equal(1, g:did_nested)
1922 close
1923 bwipe! somefile
1924
1925 augroup Testing
1926 au!
1927 augroup END
1928
1929 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
1930 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
1931endfunc
1932
1933func Test_autocmd_once()
1934 " Without ++once WinNew triggers twice
1935 let g:did_split = 0
1936 augroup Testing
1937 au WinNew * let g:did_split += 1
1938 augroup END
1939 split
1940 split
1941 call assert_equal(2, g:did_split)
1942 call assert_true(exists('#WinNew'))
1943 close
1944 close
1945
1946 " With ++once WinNew triggers once
1947 let g:did_split = 0
1948 augroup Testing
1949 au!
1950 au WinNew * ++once let g:did_split += 1
1951 augroup END
1952 split
1953 split
1954 call assert_equal(1, g:did_split)
1955 call assert_false(exists('#WinNew'))
1956 close
1957 close
1958
1959 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
1960endfunc
1961
Bram Moolenaara68e5952019-04-25 22:22:01 +02001962func Test_autocmd_bufreadpre()
1963 new
1964 let b:bufreadpre = 1
1965 call append(0, range(100))
1966 w! XAutocmdBufReadPre.txt
1967 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
1968 norm! 50gg
1969 sp
1970 norm! 100gg
1971 wincmd p
1972 let g:wsv1 = winsaveview()
1973 wincmd p
1974 let g:wsv2 = winsaveview()
1975 " triggers BufReadPre, should not move the cursor in either window
1976 " The topline may change one line in a large window.
1977 edit
1978 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
1979 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
1980 call assert_equal(2, b:bufreadpre)
1981 wincmd p
1982 call assert_equal(g:wsv1.topline, winsaveview().topline)
1983 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
1984 call assert_equal(2, b:bufreadpre)
1985 " Now set the cursor position in an BufReadPre autocommand
1986 " (even though the position will be invalid, this should make Vim reset the
1987 " cursor position in the other window.
1988 wincmd p
1989 set cpo+=g
1990 " won't do anything, but try to set the cursor on an invalid lnum
1991 autocmd BufReadPre <buffer> :norm! 70gg
1992 " triggers BufReadPre, should not move the cursor in either window
1993 e
1994 call assert_equal(1, winsaveview().topline)
1995 call assert_equal(1, winsaveview().lnum)
1996 call assert_equal(3, b:bufreadpre)
1997 wincmd p
1998 call assert_equal(g:wsv1.topline, winsaveview().topline)
1999 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2000 call assert_equal(3, b:bufreadpre)
2001 close
2002 close
2003 call delete('XAutocmdBufReadPre.txt')
2004 set cpo-=g
2005endfunc
2006
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002007" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002008
2009" Tests for the following autocommands:
2010" - FileWritePre writing a compressed file
2011" - FileReadPost reading a compressed file
2012" - BufNewFile reading a file template
2013" - BufReadPre decompressing the file to be read
2014" - FilterReadPre substituting characters in the temp file
2015" - FilterReadPost substituting characters after filtering
2016" - FileReadPre set options for decompression
2017" - FileReadPost decompress the file
2018func Test_ReadWrite_Autocmds()
2019 " Run this test only on Unix-like systems and if gzip is available
2020 if !has('unix') || !executable("gzip")
2021 return
2022 endif
2023
2024 " Make $GZIP empty, "-v" would cause trouble.
2025 let $GZIP = ""
2026
2027 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2028 " being modified outside of Vim (noticed on Solaris).
2029 au FileChangedShell * echo 'caught FileChangedShell'
2030
2031 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2032 augroup Test1
2033 au!
2034 au FileWritePre *.gz '[,']!gzip
2035 au FileWritePost *.gz undo
2036 au FileReadPost *.gz '[,']!gzip -d
2037 augroup END
2038
2039 new
2040 set bin
2041 call append(0, [
2042 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2043 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2044 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2045 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2046 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2047 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2048 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2049 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2050 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2051 \ ])
2052 1,9write! Xtestfile.gz
2053 enew! | close
2054
2055 new
2056 " Read and decompress the testfile
2057 0read Xtestfile.gz
2058 call assert_equal([
2059 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2060 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2061 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2062 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2063 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2064 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2065 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2066 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2067 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2068 \ ], getline(1, 9))
2069 enew! | close
2070
2071 augroup Test1
2072 au!
2073 augroup END
2074
2075 " Test for the FileAppendPre and FileAppendPost autocmds
2076 augroup Test2
2077 au!
2078 au BufNewFile *.c read Xtest.c
2079 au FileAppendPre *.out '[,']s/new/NEW/
2080 au FileAppendPost *.out !cat Xtest.c >> test.out
2081 augroup END
2082
2083 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2084 new foo.c " should load Xtest.c
2085 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2086 w! >> test.out " append it to the output file
2087
2088 let contents = readfile('test.out')
2089 call assert_equal(' * Here is a NEW .c file', contents[2])
2090 call assert_equal(' * Here is a new .c file', contents[5])
2091
2092 call delete('test.out')
2093 enew! | close
2094 augroup Test2
2095 au!
2096 augroup END
2097
2098 " Test for the BufReadPre and BufReadPost autocmds
2099 augroup Test3
2100 au!
2101 " setup autocommands to decompress before reading and re-compress
2102 " afterwards
2103 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2104 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2105 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2106 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2107 augroup END
2108
2109 e! Xtestfile.gz " Edit compressed file
2110 call assert_equal([
2111 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2112 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2113 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2114 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2115 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2116 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2117 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2118 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2119 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2120 \ ], getline(1, 9))
2121
2122 w! >> test.out " Append it to the output file
2123
2124 augroup Test3
2125 au!
2126 augroup END
2127
2128 " Test for the FilterReadPre and FilterReadPost autocmds.
2129 set shelltemp " need temp files here
2130 augroup Test4
2131 au!
2132 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2133 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2134 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2135 au FilterReadPost *.out '[,']s/x/X/g
2136 augroup END
2137
2138 e! test.out " Edit the output file
2139 1,$!cat
2140 call assert_equal([
2141 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2142 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2143 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2144 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2145 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2146 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2147 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2148 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2149 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2150 \ ], getline(1, 9))
2151 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 \ ], readfile('test.out'))
2162
2163 augroup Test4
2164 au!
2165 augroup END
2166 set shelltemp&vim
2167
2168 " Test for the FileReadPre and FileReadPost autocmds.
2169 augroup Test5
2170 au!
2171 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2172 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2173 au FileReadPost *.gz '[,']s/l/L/
2174 augroup END
2175
2176 new
2177 0r Xtestfile.gz " Read compressed file
2178 call assert_equal([
2179 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2180 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2181 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2182 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2183 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2184 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2185 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2186 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2187 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2188 \ ], getline(1, 9))
2189 call assert_equal([
2190 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2191 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2192 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2193 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2194 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2195 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2196 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2197 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2198 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2199 \ ], readfile('Xtestfile.gz'))
2200
2201 augroup Test5
2202 au!
2203 augroup END
2204
2205 au! FileChangedShell
2206 call delete('Xtestfile.gz')
2207 call delete('Xtest.c')
2208 call delete('test.out')
2209endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002210
2211func Test_throw_in_BufWritePre()
2212 new
2213 call setline(1, ['one', 'two', 'three'])
2214 call assert_false(filereadable('Xthefile'))
2215 augroup throwing
2216 au BufWritePre X* throw 'do not write'
2217 augroup END
2218 try
2219 w Xthefile
2220 catch
2221 let caught = 1
2222 endtry
2223 call assert_equal(1, caught)
2224 call assert_false(filereadable('Xthefile'))
2225
2226 bwipe!
2227 au! throwing
2228endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002229
2230func Test_autocmd_SafeState()
2231 CheckRunVimInTerminal
2232
2233 let lines =<< trim END
2234 let g:safe = 0
2235 let g:again = ''
2236 au SafeState * let g:safe += 1
2237 au SafeStateAgain * let g:again ..= 'x'
2238 func CallTimer()
2239 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2240 endfunc
2241 END
2242 call writefile(lines, 'XSafeState')
2243 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2244
Bram Moolenaar513537b2019-09-22 23:03:58 +02002245 " Sometimes we loop to handle an K_IGNORE
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002246 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar513537b2019-09-22 23:03:58 +02002247 call WaitForAssert({-> assert_match('^[12] ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002248
2249 call term_sendkeys(buf, ":echo g:again\<CR>")
2250 call WaitForAssert({-> assert_match('^xxxx', term_getline(buf, 6))}, 1000)
2251
2252 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar513537b2019-09-22 23:03:58 +02002253 call term_wait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002254 call term_sendkeys(buf, ":echo g:again\<CR>")
2255 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2256
2257 call StopVimInTerminal(buf)
2258 call delete('XSafeState')
2259endfunc