blob: ad2dc2b0ebb00f8a7874dc3a98750f8d4b29f2b5 [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)
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(
1713 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v'},
1714 \g:event)
1715 norm y_
1716 call assert_equal(
1717 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V'},
1718 \g:event)
1719 call feedkeys("\<C-V>y", 'x')
1720 call assert_equal(
1721 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161"},
1722 \g:event)
1723 norm "xciwbar
1724 call assert_equal(
1725 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v'},
1726 \g:event)
1727 norm "bdiw
1728 call assert_equal(
1729 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v'},
1730 \g:event)
1731
1732 call assert_equal({}, v:event)
1733
1734 au! TextYankPost
1735 unlet g:event
1736 bwipe!
1737endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001738
1739func Test_nocatch_wipe_all_buffers()
1740 " Real nasty autocommand: wipe all buffers on any event.
1741 au * * bwipe *
Bram Moolenaara997b452018-04-17 23:24:06 +02001742 " Get E93 first?
1743 " call assert_fails('next x', 'E93:')
1744 call assert_fails('next x', 'E517:')
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001745 bwipe
1746 au!
1747endfunc
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001748
1749func Test_nocatch_wipe_dummy_buffer()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01001750 if has('quickfix')
1751 " Nasty autocommand: wipe buffer on any event.
1752 au * x bwipe
1753 call assert_fails('lv½ /x', 'E480')
1754 au!
1755 endif
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001756endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001757
1758function s:Before_test_dirchanged()
1759 augroup test_dirchanged
1760 autocmd!
1761 augroup END
1762 let s:li = []
1763 let s:dir_this = getcwd()
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001764 let s:dir_foo = s:dir_this . '/foo'
1765 call mkdir(s:dir_foo)
1766 let s:dir_bar = s:dir_this . '/bar'
1767 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001768endfunc
1769
1770function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001771 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001772 call delete(s:dir_foo, 'd')
1773 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001774 augroup test_dirchanged
1775 autocmd!
1776 augroup END
1777endfunc
1778
1779function Test_dirchanged_global()
1780 call s:Before_test_dirchanged()
1781 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1782 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001783 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001784 call assert_equal(["cd:", s:dir_foo], s:li)
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 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001788 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001789 call s:After_test_dirchanged()
1790endfunc
1791
1792function Test_dirchanged_local()
1793 call s:Before_test_dirchanged()
1794 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1795 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001796 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001797 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001798 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001799 call assert_equal(["lcd:", s:dir_bar], 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 Moolenaarb7407d32018-02-03 17:36:27 +01001802 call s:After_test_dirchanged()
1803endfunc
1804
1805function Test_dirchanged_auto()
Bram Moolenaarec48a9c2018-02-03 20:11:40 +01001806 if !exists('+autochdir')
1807 return
1808 endif
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001809 call s:Before_test_dirchanged()
1810 call test_autochdir()
1811 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1812 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1813 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001814 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001815 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001816 exe 'edit ' . s:dir_foo . '/Xfile'
1817 call assert_equal(s:dir_foo, getcwd())
1818 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001819 set noacd
1820 bwipe!
1821 call s:After_test_dirchanged()
1822endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001823
1824" Test TextChangedI and TextChangedP
1825func Test_ChangedP()
1826 new
1827 call setline(1, ['foo', 'bar', 'foobar'])
1828 call test_override("char_avail", 1)
1829 set complete=. completeopt=menuone
1830
1831 func! TextChangedAutocmd(char)
1832 let g:autocmd .= a:char
1833 endfunc
1834
1835 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1836 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1837 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1838
1839 call cursor(3, 1)
1840 let g:autocmd = ''
1841 call feedkeys("o\<esc>", 'tnix')
1842 call assert_equal('I', g:autocmd)
1843
1844 let g:autocmd = ''
1845 call feedkeys("Sf", 'tnix')
1846 call assert_equal('II', g:autocmd)
1847
1848 let g:autocmd = ''
1849 call feedkeys("Sf\<C-N>", 'tnix')
1850 call assert_equal('IIP', g:autocmd)
1851
1852 let g:autocmd = ''
1853 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1854 call assert_equal('IIPP', g:autocmd)
1855
1856 let g:autocmd = ''
1857 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1858 call assert_equal('IIPPP', g:autocmd)
1859
1860 let g:autocmd = ''
1861 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1862 call assert_equal('IIPPPP', g:autocmd)
1863
1864 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1865 " TODO: how should it handle completeopt=noinsert,noselect?
1866
1867 " CleanUp
1868 call test_override("char_avail", 0)
1869 au! TextChanged
1870 au! TextChangedI
1871 au! TextChangedP
1872 delfu TextChangedAutocmd
1873 unlet! g:autocmd
1874 set complete&vim completeopt&vim
1875
1876 bw!
1877endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001878
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001879let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001880func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001881 if !g:setline_handled
1882 call setline(1, "(x)")
1883 let g:setline_handled = v:true
1884 endif
1885endfunc
1886
1887func Test_TextChangedI_with_setline()
1888 new
1889 call test_override('char_avail', 1)
1890 autocmd TextChangedI <buffer> call SetLineOne()
1891 call feedkeys("i(\<CR>\<Esc>", 'tx')
1892 call assert_equal('(', getline(1))
1893 call assert_equal('x)', getline(2))
1894 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001895 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001896 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001897
1898 call test_override('starting', 0)
1899 bwipe!
1900endfunc
1901
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001902func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001903 CheckFeature terminal
1904 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01001905 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01001906 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001907
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001908 " Prepare file for TextChanged event.
1909 call writefile([''], 'Xchanged.txt')
1910 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1911 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001912 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001913 " In ConPTY, there is additional character which is drawn up to the width of
1914 " the screen.
1915 if has('conpty')
1916 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
1917 else
1918 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
1919 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001920 " It's only adding autocmd, so that no event occurs.
1921 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1922 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001923 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001924 call assert_equal([''], readfile('Xchanged.txt'))
1925
1926 " clean up
1927 call delete('Xchanged.txt')
1928 bwipe!
1929endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01001930
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001931func Test_autocmd_nested()
1932 let g:did_nested = 0
1933 augroup Testing
1934 au WinNew * edit somefile
1935 au BufNew * let g:did_nested = 1
1936 augroup END
1937 split
1938 call assert_equal(0, g:did_nested)
1939 close
1940 bwipe! somefile
1941
1942 " old nested argument still works
1943 augroup Testing
1944 au!
1945 au WinNew * nested edit somefile
1946 au BufNew * let g:did_nested = 1
1947 augroup END
1948 split
1949 call assert_equal(1, g:did_nested)
1950 close
1951 bwipe! somefile
1952
1953 " New ++nested argument works
1954 augroup Testing
1955 au!
1956 au WinNew * ++nested edit somefile
1957 au BufNew * let g:did_nested = 1
1958 augroup END
1959 split
1960 call assert_equal(1, g:did_nested)
1961 close
1962 bwipe! somefile
1963
1964 augroup Testing
1965 au!
1966 augroup END
1967
1968 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
1969 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
1970endfunc
1971
1972func Test_autocmd_once()
1973 " Without ++once WinNew triggers twice
1974 let g:did_split = 0
1975 augroup Testing
1976 au WinNew * let g:did_split += 1
1977 augroup END
1978 split
1979 split
1980 call assert_equal(2, g:did_split)
1981 call assert_true(exists('#WinNew'))
1982 close
1983 close
1984
1985 " With ++once WinNew triggers once
1986 let g:did_split = 0
1987 augroup Testing
1988 au!
1989 au WinNew * ++once let g:did_split += 1
1990 augroup END
1991 split
1992 split
1993 call assert_equal(1, g:did_split)
1994 call assert_false(exists('#WinNew'))
1995 close
1996 close
1997
1998 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
1999endfunc
2000
Bram Moolenaara68e5952019-04-25 22:22:01 +02002001func Test_autocmd_bufreadpre()
2002 new
2003 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002004 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002005 w! XAutocmdBufReadPre.txt
2006 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002007 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002008 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002009 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002010 wincmd p
2011 let g:wsv1 = winsaveview()
2012 wincmd p
2013 let g:wsv2 = winsaveview()
2014 " triggers BufReadPre, should not move the cursor in either window
2015 " The topline may change one line in a large window.
2016 edit
2017 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2018 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2019 call assert_equal(2, b:bufreadpre)
2020 wincmd p
2021 call assert_equal(g:wsv1.topline, winsaveview().topline)
2022 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2023 call assert_equal(2, b:bufreadpre)
2024 " Now set the cursor position in an BufReadPre autocommand
2025 " (even though the position will be invalid, this should make Vim reset the
2026 " cursor position in the other window.
2027 wincmd p
2028 set cpo+=g
2029 " won't do anything, but try to set the cursor on an invalid lnum
2030 autocmd BufReadPre <buffer> :norm! 70gg
2031 " triggers BufReadPre, should not move the cursor in either window
2032 e
2033 call assert_equal(1, winsaveview().topline)
2034 call assert_equal(1, winsaveview().lnum)
2035 call assert_equal(3, b:bufreadpre)
2036 wincmd p
2037 call assert_equal(g:wsv1.topline, winsaveview().topline)
2038 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2039 call assert_equal(3, b:bufreadpre)
2040 close
2041 close
2042 call delete('XAutocmdBufReadPre.txt')
2043 set cpo-=g
2044endfunc
2045
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002046" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002047
2048" Tests for the following autocommands:
2049" - FileWritePre writing a compressed file
2050" - FileReadPost reading a compressed file
2051" - BufNewFile reading a file template
2052" - BufReadPre decompressing the file to be read
2053" - FilterReadPre substituting characters in the temp file
2054" - FilterReadPost substituting characters after filtering
2055" - FileReadPre set options for decompression
2056" - FileReadPost decompress the file
2057func Test_ReadWrite_Autocmds()
2058 " Run this test only on Unix-like systems and if gzip is available
2059 if !has('unix') || !executable("gzip")
2060 return
2061 endif
2062
2063 " Make $GZIP empty, "-v" would cause trouble.
2064 let $GZIP = ""
2065
2066 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2067 " being modified outside of Vim (noticed on Solaris).
2068 au FileChangedShell * echo 'caught FileChangedShell'
2069
2070 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2071 augroup Test1
2072 au!
2073 au FileWritePre *.gz '[,']!gzip
2074 au FileWritePost *.gz undo
2075 au FileReadPost *.gz '[,']!gzip -d
2076 augroup END
2077
2078 new
2079 set bin
2080 call append(0, [
2081 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2082 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2083 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2084 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2085 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2086 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2087 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2088 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2089 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2090 \ ])
2091 1,9write! Xtestfile.gz
2092 enew! | close
2093
2094 new
2095 " Read and decompress the testfile
2096 0read Xtestfile.gz
2097 call assert_equal([
2098 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2099 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2100 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2101 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2102 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2103 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2104 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2105 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2106 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2107 \ ], getline(1, 9))
2108 enew! | close
2109
2110 augroup Test1
2111 au!
2112 augroup END
2113
2114 " Test for the FileAppendPre and FileAppendPost autocmds
2115 augroup Test2
2116 au!
2117 au BufNewFile *.c read Xtest.c
2118 au FileAppendPre *.out '[,']s/new/NEW/
2119 au FileAppendPost *.out !cat Xtest.c >> test.out
2120 augroup END
2121
2122 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2123 new foo.c " should load Xtest.c
2124 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2125 w! >> test.out " append it to the output file
2126
2127 let contents = readfile('test.out')
2128 call assert_equal(' * Here is a NEW .c file', contents[2])
2129 call assert_equal(' * Here is a new .c file', contents[5])
2130
2131 call delete('test.out')
2132 enew! | close
2133 augroup Test2
2134 au!
2135 augroup END
2136
2137 " Test for the BufReadPre and BufReadPost autocmds
2138 augroup Test3
2139 au!
2140 " setup autocommands to decompress before reading and re-compress
2141 " afterwards
2142 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2143 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2144 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2145 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2146 augroup END
2147
2148 e! Xtestfile.gz " Edit compressed file
2149 call assert_equal([
2150 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2151 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2152 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2153 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2154 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2155 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2156 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2157 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2158 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2159 \ ], getline(1, 9))
2160
2161 w! >> test.out " Append it to the output file
2162
2163 augroup Test3
2164 au!
2165 augroup END
2166
2167 " Test for the FilterReadPre and FilterReadPost autocmds.
2168 set shelltemp " need temp files here
2169 augroup Test4
2170 au!
2171 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2172 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2173 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2174 au FilterReadPost *.out '[,']s/x/X/g
2175 augroup END
2176
2177 e! test.out " Edit the output file
2178 1,$!cat
2179 call assert_equal([
2180 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2181 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2182 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2183 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2184 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2185 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2186 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2187 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2188 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2189 \ ], getline(1, 9))
2190 call assert_equal([
2191 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2192 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2193 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2194 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2195 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2196 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2197 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2198 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2199 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2200 \ ], readfile('test.out'))
2201
2202 augroup Test4
2203 au!
2204 augroup END
2205 set shelltemp&vim
2206
2207 " Test for the FileReadPre and FileReadPost autocmds.
2208 augroup Test5
2209 au!
2210 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2211 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2212 au FileReadPost *.gz '[,']s/l/L/
2213 augroup END
2214
2215 new
2216 0r Xtestfile.gz " Read compressed file
2217 call assert_equal([
2218 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2219 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2220 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2221 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2222 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2223 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2224 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2225 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2226 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2227 \ ], getline(1, 9))
2228 call assert_equal([
2229 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2230 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2231 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2232 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2233 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2234 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2235 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2236 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2237 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2238 \ ], readfile('Xtestfile.gz'))
2239
2240 augroup Test5
2241 au!
2242 augroup END
2243
2244 au! FileChangedShell
2245 call delete('Xtestfile.gz')
2246 call delete('Xtest.c')
2247 call delete('test.out')
2248endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002249
2250func Test_throw_in_BufWritePre()
2251 new
2252 call setline(1, ['one', 'two', 'three'])
2253 call assert_false(filereadable('Xthefile'))
2254 augroup throwing
2255 au BufWritePre X* throw 'do not write'
2256 augroup END
2257 try
2258 w Xthefile
2259 catch
2260 let caught = 1
2261 endtry
2262 call assert_equal(1, caught)
2263 call assert_false(filereadable('Xthefile'))
2264
2265 bwipe!
2266 au! throwing
2267endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002268
2269func Test_autocmd_SafeState()
2270 CheckRunVimInTerminal
2271
2272 let lines =<< trim END
2273 let g:safe = 0
2274 let g:again = ''
2275 au SafeState * let g:safe += 1
2276 au SafeStateAgain * let g:again ..= 'x'
2277 func CallTimer()
2278 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2279 endfunc
2280 END
2281 call writefile(lines, 'XSafeState')
2282 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2283
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002284 " Sometimes we loop to handle a K_IGNORE, SafeState may be trigered once or
2285 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002286 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002287 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002288
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002289 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002290 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002291 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002292
2293 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002294 call term_wait(buf, 100)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002295 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002296 call term_wait(buf, 100)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002297 call term_sendkeys(buf, ":echo g:again\<CR>")
2298 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2299
2300 call StopVimInTerminal(buf)
2301 call delete('XSafeState')
2302endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002303
2304func Test_autocmd_CmdWinEnter()
2305 CheckRunVimInTerminal
2306 " There is not cmdwin switch, so
2307 " test for cmdline_hist
2308 " (both are available with small builds)
2309 CheckFeature cmdline_hist
2310 let lines =<< trim END
2311 let b:dummy_var = 'This is a dummy'
2312 autocmd CmdWinEnter * quit
2313 let winnr = winnr('$')
2314 END
2315 let filename='XCmdWinEnter'
2316 call writefile(lines, filename)
2317 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2318
2319 call term_sendkeys(buf, "q:")
2320 call term_wait(buf)
2321 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002322 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002323 call term_sendkeys(buf, ":echo &buftype\<cr>")
2324 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2325 call term_sendkeys(buf, ":echo winnr\<cr>")
2326 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2327
2328 " clean up
2329 call StopVimInTerminal(buf)
2330 call delete(filename)
2331endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002332
2333func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002334 CheckFeature quickfix
2335
Bram Moolenaarec66c412019-10-11 21:19:13 +02002336 pedit xx
2337 n x
2338 au WinEnter * quit
2339 split
2340 au! WinEnter
2341endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002342
2343func Test_BufWrite_lockmarks()
2344 edit! Xtest
2345 call setline(1, ['a', 'b', 'c', 'd'])
2346
2347 " :lockmarks preserves the marks
2348 call SetChangeMarks(2, 3)
2349 lockmarks write
2350 call assert_equal([2, 3], [line("'["), line("']")])
2351
2352 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2353 " original values for the user
2354 augroup lockmarks
2355 au!
2356 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2357 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2358 augroup END
2359
2360 lockmarks write
2361 call assert_equal([2, 3], [line("'["), line("']")])
2362
2363 if executable('cat')
2364 lockmarks %!cat
2365 call assert_equal([2, 3], [line("'["), line("']")])
2366 endif
2367
2368 lockmarks 3,4write Xtest2
2369 call assert_equal([2, 3], [line("'["), line("']")])
2370
2371 au! lockmarks
2372 augroup! lockmarks
2373 call delete('Xtest')
2374 call delete('Xtest2')
2375endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002376
2377func Test_FileType_spell()
2378 if !isdirectory('/tmp')
2379 throw "Skipped: requires /tmp directory"
2380 endif
2381
2382 " this was crashing with an invalid free()
2383 setglobal spellfile=/tmp/en.utf-8.add
2384 augroup crash
2385 autocmd!
2386 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2387 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2388 autocmd FileType anotherfiletype setlocal spell
2389 augroup END
2390 func! NoCrash() abort
2391 edit /tmp/crashfile
2392 endfunc
2393 call NoCrash()
2394
2395 au! crash
2396 setglobal spellfile=
2397endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002398
Bram Moolenaar406cd902020-02-18 21:54:41 +01002399" Test closing a window or editing another buffer from a FileChangedRO handler
2400" in a readonly buffer
2401func Test_FileChangedRO_winclose()
2402 augroup FileChangedROTest
2403 au!
2404 autocmd FileChangedRO * quit
2405 augroup END
2406 new
2407 set readonly
2408 call assert_fails('normal i', 'E788:')
2409 close
2410 augroup! FileChangedROTest
2411
2412 augroup FileChangedROTest
2413 au!
2414 autocmd FileChangedRO * edit Xfile
2415 augroup END
2416 new
2417 set readonly
2418 call assert_fails('normal i', 'E788:')
2419 close
2420 augroup! FileChangedROTest
2421endfunc
2422
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002423func LogACmd()
2424 call add(g:logged, line('$'))
2425endfunc
2426
2427func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002428 CheckNotGui
2429
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002430 enew!
2431 tabnew
2432 call setline(1, ['a', 'b', 'c', 'd'])
2433 $
2434 au TermChanged * call LogACmd()
2435 let g:logged = []
2436 let term_save = &term
2437 set term=xterm
2438 call assert_equal([1, 4], g:logged)
2439
2440 au! TermChanged
2441 let &term = term_save
2442 bwipe!
2443endfunc
2444
Bram Moolenaare3284872020-03-19 13:55:03 +01002445" Test for FileReadCmd autocmd
2446func Test_autocmd_FileReadCmd()
2447 func ReadFileCmd()
2448 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2449 endfunc
2450 augroup FileReadCmdTest
2451 au!
2452 au FileReadCmd Xtest call ReadFileCmd()
2453 augroup END
2454
2455 new
2456 read ++bin Xtest
2457 read ++nobin Xtest
2458 read ++edit Xtest
2459 read ++bad=keep Xtest
2460 read ++bad=drop Xtest
2461 read ++bad=- Xtest
2462 read ++ff=unix Xtest
2463 read ++ff=dos Xtest
2464 read ++ff=mac Xtest
2465 read ++enc=utf-8 Xtest
2466
2467 call assert_equal(['',
2468 \ 'v:cmdarg = ++bin',
2469 \ 'v:cmdarg = ++nobin',
2470 \ 'v:cmdarg = ++edit',
2471 \ 'v:cmdarg = ++bad=keep',
2472 \ 'v:cmdarg = ++bad=drop',
2473 \ 'v:cmdarg = ++bad=-',
2474 \ 'v:cmdarg = ++ff=unix',
2475 \ 'v:cmdarg = ++ff=dos',
2476 \ 'v:cmdarg = ++ff=mac',
2477 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2478
2479 close!
2480 augroup FileReadCmdTest
2481 au!
2482 augroup END
2483 delfunc ReadFileCmd
2484endfunc
2485
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002486" vim: shiftwidth=2 sts=2 expandtab