blob: b22718aee668ee5956fe3a7800b8438e1b5d492c [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
4
Bram Moolenaar1e115362019-01-09 23:01:02 +01005func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +02006 for bnr in range(1, bufnr('$'))
7 if bufloaded(bnr) && bufnr('%') != bnr
8 execute 'bd! ' . bnr
9 endif
10 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020011endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020012
Bram Moolenaar14735512016-03-26 21:00:08 +010013func Test_vim_did_enter()
14 call assert_false(v:vim_did_enter)
15
16 " This script will never reach the main loop, can't check if v:vim_did_enter
17 " becomes one.
18endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020019
Bram Moolenaarc67e8922016-05-24 16:07:40 +020020if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020021
Bram Moolenaarc67e8922016-05-24 16:07:40 +020022 func ExitInsertMode(id)
23 call feedkeys("\<Esc>")
24 endfunc
25
26 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020027 " Need to move the cursor.
28 call feedkeys("ggG", "xt")
29
Bram Moolenaarc67e8922016-05-24 16:07:40 +020030 let g:triggered = 0
31 au CursorHoldI * let g:triggered += 1
32 set updatetime=20
33 call timer_start(100, 'ExitInsertMode')
34 call feedkeys('a', 'x!')
35 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010036 unlet g:triggered
37 au! CursorHoldI
38 set updatetime&
39 endfunc
40
41 func Test_cursorhold_insert_with_timer_interrupt()
42 if !has('job')
43 return
44 endif
45 " Need to move the cursor.
46 call feedkeys("ggG", "xt")
47
48 " Confirm the timer invoked in exit_cb of the job doesn't disturb
49 " CursorHoldI event.
50 let g:triggered = 0
51 au CursorHoldI * let g:triggered += 1
52 set updatetime=500
53 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar8d4ce562019-01-30 22:01:40 +010054 \ {'exit_cb': {-> timer_start(1000, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010055 call feedkeys('a', 'x!')
56 call assert_equal(1, g:triggered)
57 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020058 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020059 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020060 endfunc
61
62 func Test_cursorhold_insert_ctrl_x()
63 let g:triggered = 0
64 au CursorHoldI * let g:triggered += 1
65 set updatetime=20
66 call timer_start(100, 'ExitInsertMode')
67 " CursorHoldI does not trigger after CTRL-X
68 call feedkeys("a\<C-X>", 'x!')
69 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010070 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020071 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020072 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020073 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +020074
75 func Test_OptionSet_modeline()
76 call test_override('starting', 1)
77 au! OptionSet
78 augroup set_tabstop
79 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
80 augroup END
81 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline')
82 set modeline
83 let v:errmsg = ''
84 call assert_fails('split XoptionsetModeline', 'E12:')
85 call assert_equal(7, &ts)
86 call assert_equal('', v:errmsg)
87
88 augroup set_tabstop
89 au!
90 augroup END
91 bwipe!
92 set ts&
93 call delete('XoptionsetModeline')
94 call test_override('starting', 0)
95 endfunc
96
97endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +020098
Bram Moolenaar04f62f82017-07-19 18:18:39 +020099func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200100 augroup test_bufunload_group
101 autocmd!
102 autocmd BufUnload * call add(s:li, "bufunload")
103 autocmd BufDelete * call add(s:li, "bufdelete")
104 autocmd BufWipeout * call add(s:li, "bufwipeout")
105 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200106
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200107 let s:li=[]
108 new
109 setlocal bufhidden=
110 bunload
111 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200112
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200113 let s:li=[]
114 new
115 setlocal bufhidden=delete
116 bunload
117 call assert_equal(["bufunload", "bufdelete"], s:li)
118
119 let s:li=[]
120 new
121 setlocal bufhidden=unload
122 bwipeout
123 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
124
Bram Moolenaare99e8442016-07-26 20:43:40 +0200125 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200126 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200127endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200128
129" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200130func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200131 tabedit
132 tabfirst
133
134 augroup test_autocmd_bufunload_with_tabnext_group
135 autocmd!
136 autocmd BufUnload <buffer> tabnext
137 augroup END
138
139 quit
140 call assert_equal(2, tabpagenr('$'))
141
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200142 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200143 augroup! test_autocmd_bufunload_with_tabnext_group
144 tablast
145 quit
146endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200147
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200148func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200149 tabedit
150 augroup sample
151 autocmd!
152 autocmd BufWinLeave <buffer> tabfirst
153 augroup END
154 call setline(1, ['a', 'b', 'c'])
155 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200156 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200157endfunc
158
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200159" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200160func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200161 split aa.txt
162 let lastbuf = bufnr('$')
163
164 augroup test_autocmd_bufunload
165 autocmd!
166 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
167 augroup END
168
Bram Moolenaara997b452018-04-17 23:24:06 +0200169 " Todo: check for E937 generated first
170 " call assert_fails('edit bb.txt', 'E937:')
171 call assert_fails('edit bb.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200172
173 autocmd! test_autocmd_bufunload
174 augroup! test_autocmd_bufunload
175 bwipe! aa.txt
176 bwipe! bb.txt
177endfunc
178
179" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200180func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200181 setlocal buftype=nowrite
182 let lastbuf = bufnr('$')
183
184 augroup test_autocmd_bufunload
185 autocmd!
186 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
187 augroup END
188
189 normal! i1
190 call assert_fails('edit a.txt', 'E517:')
191 call feedkeys("\<CR>")
192
193 autocmd! test_autocmd_bufunload
194 augroup! test_autocmd_bufunload
195 bwipe! a.txt
196endfunc
197
Bram Moolenaarc917da42016-07-19 22:31:36 +0200198func Test_win_tab_autocmd()
199 let g:record = []
200
201 augroup testing
202 au WinNew * call add(g:record, 'WinNew')
203 au WinEnter * call add(g:record, 'WinEnter')
204 au WinLeave * call add(g:record, 'WinLeave')
205 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200206 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200207 au TabEnter * call add(g:record, 'TabEnter')
208 au TabLeave * call add(g:record, 'TabLeave')
209 augroup END
210
211 split
212 tabnew
213 close
214 close
215
216 call assert_equal([
217 \ 'WinLeave', 'WinNew', 'WinEnter',
218 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200219 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200220 \ 'WinLeave', 'WinEnter'
221 \ ], g:record)
222
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200223 let g:record = []
224 tabnew somefile
225 tabnext
226 bwipe somefile
227
228 call assert_equal([
229 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
230 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
231 \ 'TabClosed'
232 \ ], g:record)
233
Bram Moolenaarc917da42016-07-19 22:31:36 +0200234 augroup testing
235 au!
236 augroup END
237 unlet g:record
238endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200239
240func s:AddAnAutocmd()
241 augroup vimBarTest
242 au BufReadCmd * echo 'hello'
243 augroup END
244 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
245endfunc
246
247func Test_early_bar()
248 " test that a bar is recognized before the {event}
249 call s:AddAnAutocmd()
250 augroup vimBarTest | au! | augroup END
251 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
252
253 call s:AddAnAutocmd()
254 augroup vimBarTest| au!| augroup END
255 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
256
257 " test that a bar is recognized after the {event}
258 call s:AddAnAutocmd()
259 augroup vimBarTest| au!BufReadCmd| augroup END
260 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
261
262 " test that a bar is recognized after the {group}
263 call s:AddAnAutocmd()
264 au! vimBarTest|echo 'hello'
265 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
266endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200267
Bram Moolenaar5c809082016-09-01 16:21:48 +0200268func RemoveGroup()
269 autocmd! StartOK
270 augroup! StartOK
271endfunc
272
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200273func Test_augroup_warning()
274 augroup TheWarning
275 au VimEnter * echo 'entering'
276 augroup END
277 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
278 redir => res
279 augroup! TheWarning
280 redir END
281 call assert_true(match(res, "W19:") >= 0)
282 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
283
284 " check "Another" does not take the pace of the deleted entry
285 augroup Another
286 augroup END
287 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200288 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200289
290 " no warning for postpone aucmd delete
291 augroup StartOK
292 au VimEnter * call RemoveGroup()
293 augroup END
294 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
295 redir => res
296 doautocmd VimEnter
297 redir END
298 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200299 au! VimEnter
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200300endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200301
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200302func Test_BufReadCmdHelp()
303 " This used to cause access to free memory
304 au BufReadCmd * e +h
305 help
306
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200307 au! BufReadCmd
308endfunc
309
310func Test_BufReadCmdHelpJump()
311 " This used to cause access to free memory
312 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200313 " } to fix highlighting
314 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200315
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200316 au! BufReadCmd
317endfunc
318
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200319func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200320 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200321 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200322 call assert_fails('augroup! x', 'E936:')
323 au VimEnter * echo
324 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200325 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200326 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
327 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200328endfunc
329
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200330" Tests for autocommands on :close command.
331" This used to be in test13.
332func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200333 " Clean up buffers, because in some cases this function fails.
334 call s:cleanup_buffers()
335
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200336 " Write three files and open them, each in a window.
337 " Then go to next window, with autocommand that deletes the previous one.
338 " Do this twice, writing the file.
339 e! Xtestje1
340 call setline(1, 'testje1')
341 w
342 sp Xtestje2
343 call setline(1, 'testje2')
344 w
345 sp Xtestje3
346 call setline(1, 'testje3')
347 w
348 wincmd w
349 au WinLeave Xtestje2 bwipe
350 wincmd w
351 call assert_equal('Xtestje1', expand('%'))
352
353 au WinLeave Xtestje1 bwipe Xtestje3
354 close
355 call assert_equal('Xtestje1', expand('%'))
356
357 " Test deleting the buffer on a Unload event. If this goes wrong there
358 " will be the ATTENTION prompt.
359 e Xtestje1
360 au!
361 au! BufUnload Xtestje1 bwipe
362 call assert_fails('e Xtestje3', 'E937:')
363 call assert_equal('Xtestje3', expand('%'))
364
365 e Xtestje2
366 sp Xtestje1
367 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200368 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200369
370 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
371 " there are ml_line errors and/or a Crash.
372 au!
373 only
374 e Xanother
375 e Xtestje1
376 bwipe Xtestje2
377 bwipe Xtestje3
378 au BufWipeout Xtestje1 buf Xtestje1
379 bwipe
380 call assert_equal('Xanother', expand('%'))
381
382 only
383 help
384 wincmd w
385 1quit
386 call assert_equal('Xanother', expand('%'))
387
388 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100389 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200390 call delete('Xtestje1')
391 call delete('Xtestje2')
392 call delete('Xtestje3')
393endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100394
395func Test_BufEnter()
396 au! BufEnter
397 au Bufenter * let val = val . '+'
398 let g:val = ''
399 split NewFile
400 call assert_equal('+', g:val)
401 bwipe!
402 call assert_equal('++', g:val)
403
404 " Also get BufEnter when editing a directory
405 call mkdir('Xdir')
406 split Xdir
407 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100408
409 " On MS-Windows we can't edit the directory, make sure we wipe the right
410 " buffer.
411 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100412
413 call delete('Xdir', 'd')
414 au! BufEnter
415endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100416
417" Closing a window might cause an endless loop
418" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200419func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200420 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100421 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200422 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100423 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100424 mksession!
425
Bram Moolenaare94260f2017-03-21 15:50:12 +0100426 let content = ['set nocp noswapfile',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100427 \ 'let v:swapchoice="e"',
428 \ 'augroup test_autocmd_sessionload',
429 \ 'autocmd!',
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200430 \ 'autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100431 \ 'augroup END',
432 \ '',
433 \ 'func WriteErrors()',
434 \ ' call writefile([execute("messages")], "Xerrors")',
435 \ 'endfunc',
436 \ 'au VimLeave * call WriteErrors()',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100437 \ ]
438 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100439 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
440 let errors = join(readfile('Xerrors'))
441 call assert_match('E814', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100442
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100443 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100444 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100445 call delete(file)
446 endfor
447endfunc
448
449" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200450func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100451 tabnew
452 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100453 mksession!
454
455 let content = ['set nocp noswapfile',
456 \ 'function! DeleteInactiveBufs()',
457 \ ' tabfirst',
458 \ ' let tabblist = []',
459 \ ' for i in range(1, tabpagenr(''$''))',
460 \ ' call extend(tabblist, tabpagebuflist(i))',
461 \ ' endfor',
462 \ ' for b in range(1, bufnr(''$''))',
463 \ ' if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')',
464 \ ' exec ''bwipeout '' . b',
465 \ ' endif',
466 \ ' endfor',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100467 \ ' echomsg "SessionLoadPost DONE"',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100468 \ 'endfunction',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100469 \ 'au SessionLoadPost * call DeleteInactiveBufs()',
470 \ '',
471 \ 'func WriteErrors()',
472 \ ' call writefile([execute("messages")], "Xerrors")',
473 \ 'endfunc',
474 \ 'au VimLeave * call WriteErrors()',
475 \ ]
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100476 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100477 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
478 let errors = join(readfile('Xerrors'))
479 " This probably only ever matches on unix.
480 call assert_notmatch('Caught deadly signal SEGV', errors)
481 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100482
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100483 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100484 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100485 call delete(file)
486 endfor
487endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200488
489func Test_empty_doau()
490 doau \|
491endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200492
493func s:AutoCommandOptionSet(match)
494 let item = remove(g:options, 0)
495 let expected = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3])
496 let actual = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", a:match, v:option_old, v:option_new, v:option_type)
497 let g:opt = [expected, actual]
498 "call assert_equal(expected, actual)
499endfunc
500
501func Test_OptionSet()
Bram Moolenaar26d98212019-01-27 22:32:55 +0100502 if !has("eval") || !exists("+autochdir")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200503 return
504 endif
505
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200506 badd test_autocmd.vim
507
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200508 call test_override('starting', 1)
509 set nocp
510 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
511
512 " 1: Setting number option"
513 let g:options=[['number', 0, 1, 'global']]
514 set nu
515 call assert_equal([], g:options)
516 call assert_equal(g:opt[0], g:opt[1])
517
518 " 2: Setting local number option"
519 let g:options=[['number', 1, 0, 'local']]
520 setlocal nonu
521 call assert_equal([], g:options)
522 call assert_equal(g:opt[0], g:opt[1])
523
524 " 3: Setting global number option"
525 let g:options=[['number', 1, 0, 'global']]
526 setglobal nonu
527 call assert_equal([], g:options)
528 call assert_equal(g:opt[0], g:opt[1])
529
530 " 4: Setting local autoindent option"
531 let g:options=[['autoindent', 0, 1, 'local']]
532 setlocal ai
533 call assert_equal([], g:options)
534 call assert_equal(g:opt[0], g:opt[1])
535
536 " 5: Setting global autoindent option"
537 let g:options=[['autoindent', 0, 1, 'global']]
538 setglobal ai
539 call assert_equal([], g:options)
540 call assert_equal(g:opt[0], g:opt[1])
541
542 " 6: Setting global autoindent option"
543 let g:options=[['autoindent', 1, 0, 'global']]
544 set ai!
545 call assert_equal([], g:options)
546 call assert_equal(g:opt[0], g:opt[1])
547
548 " Should not print anything, use :noa
549 " 7: don't trigger OptionSet"
550 let g:options=[['invalid', 1, 1, 'invalid']]
551 noa set nonu
552 call assert_equal([['invalid', 1, 1, 'invalid']], g:options)
553 call assert_equal(g:opt[0], g:opt[1])
554
555 " 8: Setting several global list and number option"
556 let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']]
557 set list nu
558 call assert_equal([], g:options)
559 call assert_equal(g:opt[0], g:opt[1])
560
561 " 9: don't trigger OptionSet"
562 let g:options=[['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']]
563 noa set nolist nonu
564 call assert_equal([['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']], g:options)
565 call assert_equal(g:opt[0], g:opt[1])
566
567 " 10: Setting global acd"
568 let g:options=[['autochdir', 0, 1, 'local']]
569 setlocal acd
570 call assert_equal([], g:options)
571 call assert_equal(g:opt[0], g:opt[1])
572
573 " 11: Setting global autoread (also sets local value)"
574 let g:options=[['autoread', 0, 1, 'global']]
575 set ar
576 call assert_equal([], g:options)
577 call assert_equal(g:opt[0], g:opt[1])
578
579 " 12: Setting local autoread"
580 let g:options=[['autoread', 1, 1, 'local']]
581 setlocal ar
582 call assert_equal([], g:options)
583 call assert_equal(g:opt[0], g:opt[1])
584
585 " 13: Setting global autoread"
586 let g:options=[['autoread', 1, 0, 'global']]
587 setglobal invar
588 call assert_equal([], g:options)
589 call assert_equal(g:opt[0], g:opt[1])
590
591 " 14: Setting option backspace through :let"
592 let g:options=[['backspace', '', 'eol,indent,start', 'global']]
593 let &bs="eol,indent,start"
594 call assert_equal([], g:options)
595 call assert_equal(g:opt[0], g:opt[1])
596
597 " 15: Setting option backspace through setbufvar()"
598 let g:options=[['backup', 0, 1, 'local']]
599 " try twice, first time, shouldn't trigger because option name is invalid,
600 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200601 let bnum = bufnr('%')
602 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200603 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200604 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200605 call assert_equal([], g:options)
606 call assert_equal(g:opt[0], g:opt[1])
607
608 " 16: Setting number option using setwinvar"
609 let g:options=[['number', 0, 1, 'local']]
610 call setwinvar(0, '&number', 1)
611 call assert_equal([], g:options)
612 call assert_equal(g:opt[0], g:opt[1])
613
614 " 17: Setting key option, shouldn't trigger"
615 let g:options=[['key', 'invalid', 'invalid1', 'invalid']]
616 setlocal key=blah
617 setlocal key=
618 call assert_equal([['key', 'invalid', 'invalid1', 'invalid']], g:options)
619 call assert_equal(g:opt[0], g:opt[1])
620
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200621 " 18: Setting string option"
622 let oldval = &tags
623 let g:options=[['tags', oldval, 'tagpath', 'global']]
624 set tags=tagpath
625 call assert_equal([], g:options)
626 call assert_equal(g:opt[0], g:opt[1])
627
628 " 1l: Resetting string option"
629 let g:options=[['tags', 'tagpath', oldval, 'global']]
630 set tags&
631 call assert_equal([], g:options)
632 call assert_equal(g:opt[0], g:opt[1])
633
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200634 " Cleanup
635 au! OptionSet
636 for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp']
Bram Moolenaar91d2e782018-08-07 19:05:01 +0200637 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200638 endfor
639 call test_override('starting', 0)
640 delfunc! AutoCommandOptionSet
641endfunc
642
643func Test_OptionSet_diffmode()
644 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +0100645 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200646 new
647 au OptionSet diff :let &l:cul=v:option_new
648
649 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
650 call assert_equal(0, &l:cul)
651 diffthis
652 call assert_equal(1, &l:cul)
653
654 vnew
655 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
656 call assert_equal(0, &l:cul)
657 diffthis
658 call assert_equal(1, &l:cul)
659
660 diffoff
661 call assert_equal(0, &l:cul)
662 call assert_equal(1, getwinvar(2, '&l:cul'))
663 bw!
664
665 call assert_equal(1, &l:cul)
666 diffoff!
667 call assert_equal(0, &l:cul)
668 call assert_equal(0, getwinvar(1, '&l:cul'))
669 bw!
670
671 " Cleanup
672 au! OptionSet
673 call test_override('starting', 0)
674endfunc
675
676func Test_OptionSet_diffmode_close()
677 call test_override('starting', 1)
678 " 19: Try to close the current window when entering diff mode
679 " should not segfault
680 new
681 au OptionSet diff close
682
683 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
684 call assert_fails(':diffthis', 'E788')
685 call assert_equal(1, &diff)
686 vnew
687 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
688 call assert_fails(':diffthis', 'E788')
689 call assert_equal(1, &diff)
690 bw!
691 call assert_fails(':diffoff!', 'E788')
692 bw!
693
694 " Cleanup
695 au! OptionSet
696 call test_override('starting', 0)
697 "delfunc! AutoCommandOptionSet
698endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +0200699
700" Test for Bufleave autocommand that deletes the buffer we are about to edit.
701func Test_BufleaveWithDelete()
702 new | edit Xfile1
703
704 augroup test_bufleavewithdelete
705 autocmd!
706 autocmd BufLeave Xfile1 bwipe Xfile2
707 augroup END
708
709 call assert_fails('edit Xfile2', 'E143:')
710 call assert_equal('Xfile1', bufname('%'))
711
712 autocmd! test_bufleavewithdelete BufLeave Xfile1
713 augroup! test_bufleavewithdelete
714
715 new
716 bwipe! Xfile1
717endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200718
719" Test for autocommand that changes the buffer list, when doing ":ball".
720func Test_Acmd_BufAll()
721 enew!
722 %bwipe!
723 call writefile(['Test file Xxx1'], 'Xxx1')
724 call writefile(['Test file Xxx2'], 'Xxx2')
725 call writefile(['Test file Xxx3'], 'Xxx3')
726
727 " Add three files to the buffer list
728 split Xxx1
729 close
730 split Xxx2
731 close
732 split Xxx3
733 close
734
735 " Wipe the buffer when the buffer is opened
736 au BufReadPost Xxx2 bwipe
737
738 call append(0, 'Test file Xxx4')
739 ball
740
741 call assert_equal(2, winnr('$'))
742 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
743 wincmd t
744
745 au! BufReadPost
746 %bwipe!
747 call delete('Xxx1')
748 call delete('Xxx2')
749 call delete('Xxx3')
750 enew! | only
751endfunc
752
753" Test for autocommand that changes current buffer on BufEnter event.
754" Check if modelines are interpreted for the correct buffer.
755func Test_Acmd_BufEnter()
756 %bwipe!
757 call writefile(['start of test file Xxx1',
758 \ "\<Tab>this is a test",
759 \ 'end of test file Xxx1'], 'Xxx1')
760 call writefile(['start of test file Xxx2',
761 \ 'vim: set noai :',
762 \ "\<Tab>this is a test",
763 \ 'end of test file Xxx2'], 'Xxx2')
764
765 au BufEnter Xxx2 brew
766 set ai modeline modelines=3
767 edit Xxx1
768 " edit Xxx2, autocmd will do :brew
769 edit Xxx2
770 exe "normal G?this is a\<CR>"
771 " Append text with autoindent to this file
772 normal othis should be auto-indented
773 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
774 call assert_equal(3, line('.'))
775 " Remove autocmd and edit Xxx2 again
776 au! BufEnter Xxx2
777 buf! Xxx2
778 exe "normal G?this is a\<CR>"
779 " append text without autoindent to Xxx
780 normal othis should be in column 1
781 call assert_equal("this should be in column 1", getline('.'))
782 call assert_equal(4, line('.'))
783
784 %bwipe!
785 call delete('Xxx1')
786 call delete('Xxx2')
787 set ai&vim modeline&vim modelines&vim
788endfunc
789
790" Test for issue #57
791" do not move cursor on <c-o> when autoindent is set
792func Test_ai_CTRL_O()
793 enew!
794 set ai
795 let save_fo = &fo
796 set fo+=r
797 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
798 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
799 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
800
801 set ai&vim
802 let &fo = save_fo
803 enew!
804endfunc
805
806" Test for autocommand that deletes the current buffer on BufLeave event.
807" Also test deleting the last buffer, should give a new, empty buffer.
808func Test_BufLeave_Wipe()
809 %bwipe!
810 let content = ['start of test file Xxx',
811 \ 'this is a test',
812 \ 'end of test file Xxx']
813 call writefile(content, 'Xxx1')
814 call writefile(content, 'Xxx2')
815
816 au BufLeave Xxx2 bwipe
817 edit Xxx1
818 split Xxx2
819 " delete buffer Xxx2, we should be back to Xxx1
820 bwipe
821 call assert_equal('Xxx1', bufname('%'))
822 call assert_equal(1, winnr('$'))
823
824 " Create an alternate buffer
825 %write! test.out
826 call assert_equal('test.out', bufname('#'))
827 " delete alternate buffer
828 bwipe test.out
829 call assert_equal('Xxx1', bufname('%'))
830 call assert_equal('', bufname('#'))
831
832 au BufLeave Xxx1 bwipe
833 " delete current buffer, get an empty one
834 bwipe!
835 call assert_equal(1, line('$'))
836 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200837 let g:bufinfo = getbufinfo()
838 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200839
840 call delete('Xxx1')
841 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200842 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200843 %bwipe
844 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200845
846 " check that bufinfo doesn't contain a pointer to freed memory
847 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200848endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +0200849
850func Test_QuitPre()
851 edit Xfoo
852 let winid = win_getid(winnr())
853 split Xbar
854 au! QuitPre * let g:afile = expand('<afile>')
855 " Close the other window, <afile> should be correct.
856 exe win_id2win(winid) . 'q'
857 call assert_equal('Xfoo', g:afile)
858
859 unlet g:afile
860 bwipe Xfoo
861 bwipe Xbar
862endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200863
864func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +0100865 au! CmdlineChanged : let g:text = getcmdline()
866 let g:text = 0
867 call feedkeys(":echom 'hello'\<CR>", 'xt')
868 call assert_equal("echom 'hello'", g:text)
869 au! CmdlineChanged
870
871 au! CmdlineChanged : let g:entered = expand('<afile>')
872 let g:entered = 0
873 call feedkeys(":echom 'hello'\<CR>", 'xt')
874 call assert_equal(':', g:entered)
875 au! CmdlineChanged
876
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200877 au! CmdlineEnter : let g:entered = expand('<afile>')
878 au! CmdlineLeave : let g:left = expand('<afile>')
879 let g:entered = 0
880 let g:left = 0
881 call feedkeys(":echo 'hello'\<CR>", 'xt')
882 call assert_equal(':', g:entered)
883 call assert_equal(':', g:left)
884 au! CmdlineEnter
885 au! CmdlineLeave
886
Bram Moolenaara4baf5b2018-04-22 13:27:44 +0200887 let save_shellslash = &shellslash
888 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200889 au! CmdlineEnter / let g:entered = expand('<afile>')
890 au! CmdlineLeave / let g:left = expand('<afile>')
891 let g:entered = 0
892 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200893 new
894 call setline(1, 'hello')
895 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200896 call assert_equal('/', g:entered)
897 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200898 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200899 au! CmdlineEnter
900 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +0200901 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200902endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200903
904" Test for BufWritePre autocommand that deletes or unloads the buffer.
905func Test_BufWritePre()
906 %bwipe
907 au BufWritePre Xxx1 bunload
908 au BufWritePre Xxx2 bwipe
909
910 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
911 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
912
913 edit Xtest
914 e! Xxx2
915 bdel Xtest
916 e Xxx1
917 " write it, will unload it and give an error msg
918 call assert_fails('w', 'E203')
919 call assert_equal('Xxx2', bufname('%'))
920 edit Xtest
921 e! Xxx2
922 bwipe Xtest
923 " write it, will delete the buffer and give an error msg
924 call assert_fails('w', 'E203')
925 call assert_equal('Xxx1', bufname('%'))
926 au! BufWritePre
927 call delete('Xxx1')
928 call delete('Xxx2')
929endfunc
930
931" Test for BufUnload autocommand that unloads all the other buffers
932func Test_bufunload_all()
933 call writefile(['Test file Xxx1'], 'Xxx1')"
934 call writefile(['Test file Xxx2'], 'Xxx2')"
935
936 let content = [
937 \ "func UnloadAllBufs()",
938 \ " let i = 1",
939 \ " while i <= bufnr('$')",
940 \ " if i != bufnr('%') && bufloaded(i)",
941 \ " exe i . 'bunload'",
942 \ " endif",
943 \ " let i += 1",
944 \ " endwhile",
945 \ "endfunc",
946 \ "au BufUnload * call UnloadAllBufs()",
947 \ "au VimLeave * call writefile(['Test Finished'], 'Xout')",
948 \ "edit Xxx1",
949 \ "split Xxx2",
950 \ "q"]
951 call writefile(content, 'Xtest')
952
953 call delete('Xout')
954 call system(v:progpath. ' --clean -N --not-a-term -S Xtest')
955 call assert_true(filereadable('Xout'))
956
957 call delete('Xxx1')
958 call delete('Xxx2')
959 call delete('Xtest')
960 call delete('Xout')
961endfunc
962
963" Some tests for buffer-local autocommands
964func Test_buflocal_autocmd()
965 let g:bname = ''
966 edit xx
967 au BufLeave <buffer> let g:bname = expand("%")
968 " here, autocommand for xx should trigger.
969 " but autocommand shall not apply to buffer named <buffer>.
970 edit somefile
971 call assert_equal('xx', g:bname)
972 let g:bname = ''
973 " here, autocommand shall be auto-deleted
974 bwipe xx
975 " autocmd should not trigger
976 edit xx
977 call assert_equal('', g:bname)
978 " autocmd should not trigger
979 edit somefile
980 call assert_equal('', g:bname)
981 enew
982 unlet g:bname
983endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +0100984
985" Test for "*Cmd" autocommands
986func Test_Cmd_Autocmds()
987 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
988
989 enew!
990 au BufReadCmd XtestA 0r Xxx|$del
991 edit XtestA " will read text of Xxd instead
992 call assert_equal('start of Xxx', getline(1))
993
994 au BufWriteCmd XtestA call append(line("$"), "write")
995 write " will append a line to the file
996 call assert_equal('write', getline('$'))
997 call assert_fails('read XtestA', 'E484') " should not read anything
998 call assert_equal('write', getline(4))
999
1000 " now we have:
1001 " 1 start of Xxx
1002 " 2 abc2
1003 " 3 end of Xxx
1004 " 4 write
1005
1006 au FileReadCmd XtestB '[r Xxx
1007 2r XtestB " will read Xxx below line 2 instead
1008 call assert_equal('start of Xxx', getline(3))
1009
1010 " now we have:
1011 " 1 start of Xxx
1012 " 2 abc2
1013 " 3 start of Xxx
1014 " 4 abc2
1015 " 5 end of Xxx
1016 " 6 end of Xxx
1017 " 7 write
1018
1019 au FileWriteCmd XtestC '[,']copy $
1020 normal 4GA1
1021 4,5w XtestC " will copy lines 4 and 5 to the end
1022 call assert_equal("\tabc21", getline(8))
1023 call assert_fails('r XtestC', 'E484') " should not read anything
1024 call assert_equal("end of Xxx", getline(9))
1025
1026 " now we have:
1027 " 1 start of Xxx
1028 " 2 abc2
1029 " 3 start of Xxx
1030 " 4 abc21
1031 " 5 end of Xxx
1032 " 6 end of Xxx
1033 " 7 write
1034 " 8 abc21
1035 " 9 end of Xxx
1036
1037 let g:lines = []
1038 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1039 w >>XtestD " will add lines to 'lines'
1040 call assert_equal(9, len(g:lines))
1041 call assert_fails('$r XtestD', 'E484') " should not read anything
1042 call assert_equal(9, line('$'))
1043 call assert_equal('end of Xxx', getline('$'))
1044
1045 au BufReadCmd XtestE 0r Xxx|$del
1046 sp XtestE " split window with test.out
1047 call assert_equal('end of Xxx', getline(3))
1048
1049 let g:lines = []
1050 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1051 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1052 wall " will write other window to 'lines'
1053 call assert_equal(4, len(g:lines), g:lines)
1054 call assert_equal('asdf', g:lines[2])
1055
1056 au! BufReadCmd
1057 au! BufWriteCmd
1058 au! FileReadCmd
1059 au! FileWriteCmd
1060 au! FileAppendCmd
1061 %bwipe!
1062 call delete('Xxx')
1063 enew!
1064endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001065
1066func SetChangeMarks(start, end)
1067 exe a:start. 'mark ['
1068 exe a:end. 'mark ]'
1069endfunc
1070
1071" Verify the effects of autocmds on '[ and ']
1072func Test_change_mark_in_autocmds()
1073 edit! Xtest
1074 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u", 'xtn')
1075
1076 call SetChangeMarks(2, 3)
1077 write
1078 call assert_equal([1, 4], [line("'["), line("']")])
1079
1080 call SetChangeMarks(2, 3)
1081 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1082 write
1083 au! BufWritePre
1084
1085 if executable('cat')
1086 write XtestFilter
1087 write >> XtestFilter
1088
1089 call SetChangeMarks(2, 3)
1090 " Marks are set to the entire range of the write
1091 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1092 " '[ is adjusted to just before the line that will receive the filtered
1093 " data
1094 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1095 " The filtered data is read into the buffer, and the source lines are
1096 " still present, so the range is after the source lines
1097 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1098 %!cat XtestFilter
1099 " After the filtered data is read, the original lines are deleted
1100 call assert_equal([1, 8], [line("'["), line("']")])
1101 au! FilterWritePre,FilterReadPre,FilterReadPost
1102 undo
1103
1104 call SetChangeMarks(1, 4)
1105 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1106 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1107 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1108 2,3!cat XtestFilter
1109 call assert_equal([2, 9], [line("'["), line("']")])
1110 au! FilterWritePre,FilterReadPre,FilterReadPost
1111 undo
1112
1113 call delete('XtestFilter')
1114 endif
1115
1116 call SetChangeMarks(1, 4)
1117 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1118 2,3write Xtest2
1119 au! FileWritePre
1120
1121 call SetChangeMarks(2, 3)
1122 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1123 write >> Xtest2
1124 au! FileAppendPre
1125
1126 call SetChangeMarks(1, 4)
1127 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1128 2,3write >> Xtest2
1129 au! FileAppendPre
1130
1131 call SetChangeMarks(1, 1)
1132 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1133 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1134 3read Xtest2
1135 au! FileReadPre,FileReadPost
1136 undo
1137
1138 call SetChangeMarks(4, 4)
1139 " When the line is 0, it's adjusted to 1
1140 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1141 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1142 0read Xtest2
1143 au! FileReadPre,FileReadPost
1144 undo
1145
1146 call SetChangeMarks(4, 4)
1147 " When the line is 0, it's adjusted to 1
1148 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1149 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1150 1read Xtest2
1151 au! FileReadPre,FileReadPost
1152 undo
1153
1154 bwipe!
1155 call delete('Xtest')
1156 call delete('Xtest2')
1157endfunc
1158
1159func Test_Filter_noshelltemp()
1160 if !executable('cat')
1161 return
1162 endif
1163
1164 enew!
1165 call setline(1, ['a', 'b', 'c', 'd'])
1166
1167 let shelltemp = &shelltemp
1168 set shelltemp
1169
1170 let g:filter_au = 0
1171 au FilterWritePre * let g:filter_au += 1
1172 au FilterReadPre * let g:filter_au += 1
1173 au FilterReadPost * let g:filter_au += 1
1174 %!cat
1175 call assert_equal(3, g:filter_au)
1176
1177 if has('filterpipe')
1178 set noshelltemp
1179
1180 let g:filter_au = 0
1181 au FilterWritePre * let g:filter_au += 1
1182 au FilterReadPre * let g:filter_au += 1
1183 au FilterReadPost * let g:filter_au += 1
1184 %!cat
1185 call assert_equal(0, g:filter_au)
1186 endif
1187
1188 au! FilterWritePre,FilterReadPre,FilterReadPost
1189 let &shelltemp = shelltemp
1190 bwipe!
1191endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001192
1193func Test_TextYankPost()
1194 enew!
1195 call setline(1, ['foo'])
1196
1197 let g:event = []
1198 au TextYankPost * let g:event = copy(v:event)
1199
1200 call assert_equal({}, v:event)
1201 call assert_fails('let v:event = {}', 'E46:')
1202 call assert_fails('let v:event.mykey = 0', 'E742:')
1203
1204 norm "ayiw
1205 call assert_equal(
1206 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v'},
1207 \g:event)
1208 norm y_
1209 call assert_equal(
1210 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V'},
1211 \g:event)
1212 call feedkeys("\<C-V>y", 'x')
1213 call assert_equal(
1214 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161"},
1215 \g:event)
1216 norm "xciwbar
1217 call assert_equal(
1218 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v'},
1219 \g:event)
1220 norm "bdiw
1221 call assert_equal(
1222 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v'},
1223 \g:event)
1224
1225 call assert_equal({}, v:event)
1226
1227 au! TextYankPost
1228 unlet g:event
1229 bwipe!
1230endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001231
1232func Test_nocatch_wipe_all_buffers()
1233 " Real nasty autocommand: wipe all buffers on any event.
1234 au * * bwipe *
Bram Moolenaara997b452018-04-17 23:24:06 +02001235 " Get E93 first?
1236 " call assert_fails('next x', 'E93:')
1237 call assert_fails('next x', 'E517:')
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001238 bwipe
1239 au!
1240endfunc
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001241
1242func Test_nocatch_wipe_dummy_buffer()
1243 " Nasty autocommand: wipe buffer on any event.
1244 au * x bwipe
1245 call assert_fails('lv½ /x', 'E480')
1246 au!
1247endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001248
1249function s:Before_test_dirchanged()
1250 augroup test_dirchanged
1251 autocmd!
1252 augroup END
1253 let s:li = []
1254 let s:dir_this = getcwd()
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001255 let s:dir_foo = s:dir_this . '/foo'
1256 call mkdir(s:dir_foo)
1257 let s:dir_bar = s:dir_this . '/bar'
1258 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001259endfunc
1260
1261function s:After_test_dirchanged()
1262 exe 'cd' s:dir_this
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001263 call delete(s:dir_foo, 'd')
1264 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001265 augroup test_dirchanged
1266 autocmd!
1267 augroup END
1268endfunc
1269
1270function Test_dirchanged_global()
1271 call s:Before_test_dirchanged()
1272 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1273 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001274 exe 'cd' s:dir_foo
1275 call assert_equal(["cd:", s:dir_foo], s:li)
1276 exe 'cd' s:dir_foo
1277 call assert_equal(["cd:", s:dir_foo], s:li)
1278 exe 'lcd' s:dir_bar
1279 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001280 call s:After_test_dirchanged()
1281endfunc
1282
1283function Test_dirchanged_local()
1284 call s:Before_test_dirchanged()
1285 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1286 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001287 exe 'cd' s:dir_foo
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001288 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001289 exe 'lcd' s:dir_bar
1290 call assert_equal(["lcd:", s:dir_bar], s:li)
1291 exe 'lcd' s:dir_bar
1292 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001293 call s:After_test_dirchanged()
1294endfunc
1295
1296function Test_dirchanged_auto()
Bram Moolenaarec48a9c2018-02-03 20:11:40 +01001297 if !exists('+autochdir')
1298 return
1299 endif
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001300 call s:Before_test_dirchanged()
1301 call test_autochdir()
1302 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1303 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1304 set acd
1305 exe 'cd ..'
1306 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001307 exe 'edit ' . s:dir_foo . '/Xfile'
1308 call assert_equal(s:dir_foo, getcwd())
1309 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001310 set noacd
1311 bwipe!
1312 call s:After_test_dirchanged()
1313endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001314
1315" Test TextChangedI and TextChangedP
1316func Test_ChangedP()
1317 new
1318 call setline(1, ['foo', 'bar', 'foobar'])
1319 call test_override("char_avail", 1)
1320 set complete=. completeopt=menuone
1321
1322 func! TextChangedAutocmd(char)
1323 let g:autocmd .= a:char
1324 endfunc
1325
1326 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1327 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1328 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1329
1330 call cursor(3, 1)
1331 let g:autocmd = ''
1332 call feedkeys("o\<esc>", 'tnix')
1333 call assert_equal('I', g:autocmd)
1334
1335 let g:autocmd = ''
1336 call feedkeys("Sf", 'tnix')
1337 call assert_equal('II', g:autocmd)
1338
1339 let g:autocmd = ''
1340 call feedkeys("Sf\<C-N>", 'tnix')
1341 call assert_equal('IIP', g:autocmd)
1342
1343 let g:autocmd = ''
1344 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1345 call assert_equal('IIPP', g:autocmd)
1346
1347 let g:autocmd = ''
1348 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1349 call assert_equal('IIPPP', g:autocmd)
1350
1351 let g:autocmd = ''
1352 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1353 call assert_equal('IIPPPP', g:autocmd)
1354
1355 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1356 " TODO: how should it handle completeopt=noinsert,noselect?
1357
1358 " CleanUp
1359 call test_override("char_avail", 0)
1360 au! TextChanged
1361 au! TextChangedI
1362 au! TextChangedP
1363 delfu TextChangedAutocmd
1364 unlet! g:autocmd
1365 set complete&vim completeopt&vim
1366
1367 bw!
1368endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001369
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001370let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001371func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001372 if !g:setline_handled
1373 call setline(1, "(x)")
1374 let g:setline_handled = v:true
1375 endif
1376endfunc
1377
1378func Test_TextChangedI_with_setline()
1379 new
1380 call test_override('char_avail', 1)
1381 autocmd TextChangedI <buffer> call SetLineOne()
1382 call feedkeys("i(\<CR>\<Esc>", 'tx')
1383 call assert_equal('(', getline(1))
1384 call assert_equal('x)', getline(2))
1385 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001386 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001387 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001388
1389 call test_override('starting', 0)
1390 bwipe!
1391endfunc
1392
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001393func Test_Changed_FirstTime()
1394 if !has('terminal') || has('gui_running')
1395 return
1396 endif
1397 " Prepare file for TextChanged event.
1398 call writefile([''], 'Xchanged.txt')
1399 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1400 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001401 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001402 " In ConPTY, there is additional character which is drawn up to the width of
1403 " the screen.
1404 if has('conpty')
1405 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
1406 else
1407 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
1408 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001409 " It's only adding autocmd, so that no event occurs.
1410 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1411 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001412 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001413 call assert_equal([''], readfile('Xchanged.txt'))
1414
1415 " clean up
1416 call delete('Xchanged.txt')
1417 bwipe!
1418endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01001419
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001420func Test_autocmd_nested()
1421 let g:did_nested = 0
1422 augroup Testing
1423 au WinNew * edit somefile
1424 au BufNew * let g:did_nested = 1
1425 augroup END
1426 split
1427 call assert_equal(0, g:did_nested)
1428 close
1429 bwipe! somefile
1430
1431 " old nested argument still works
1432 augroup Testing
1433 au!
1434 au WinNew * nested edit somefile
1435 au BufNew * let g:did_nested = 1
1436 augroup END
1437 split
1438 call assert_equal(1, g:did_nested)
1439 close
1440 bwipe! somefile
1441
1442 " New ++nested argument works
1443 augroup Testing
1444 au!
1445 au WinNew * ++nested edit somefile
1446 au BufNew * let g:did_nested = 1
1447 augroup END
1448 split
1449 call assert_equal(1, g:did_nested)
1450 close
1451 bwipe! somefile
1452
1453 augroup Testing
1454 au!
1455 augroup END
1456
1457 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
1458 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
1459endfunc
1460
1461func Test_autocmd_once()
1462 " Without ++once WinNew triggers twice
1463 let g:did_split = 0
1464 augroup Testing
1465 au WinNew * let g:did_split += 1
1466 augroup END
1467 split
1468 split
1469 call assert_equal(2, g:did_split)
1470 call assert_true(exists('#WinNew'))
1471 close
1472 close
1473
1474 " With ++once WinNew triggers once
1475 let g:did_split = 0
1476 augroup Testing
1477 au!
1478 au WinNew * ++once let g:did_split += 1
1479 augroup END
1480 split
1481 split
1482 call assert_equal(1, g:did_split)
1483 call assert_false(exists('#WinNew'))
1484 close
1485 close
1486
1487 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
1488endfunc
1489
Bram Moolenaara68e5952019-04-25 22:22:01 +02001490func Test_autocmd_bufreadpre()
1491 new
1492 let b:bufreadpre = 1
1493 call append(0, range(100))
1494 w! XAutocmdBufReadPre.txt
1495 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
1496 norm! 50gg
1497 sp
1498 norm! 100gg
1499 wincmd p
1500 let g:wsv1 = winsaveview()
1501 wincmd p
1502 let g:wsv2 = winsaveview()
1503 " triggers BufReadPre, should not move the cursor in either window
1504 " The topline may change one line in a large window.
1505 edit
1506 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
1507 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
1508 call assert_equal(2, b:bufreadpre)
1509 wincmd p
1510 call assert_equal(g:wsv1.topline, winsaveview().topline)
1511 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
1512 call assert_equal(2, b:bufreadpre)
1513 " Now set the cursor position in an BufReadPre autocommand
1514 " (even though the position will be invalid, this should make Vim reset the
1515 " cursor position in the other window.
1516 wincmd p
1517 set cpo+=g
1518 " won't do anything, but try to set the cursor on an invalid lnum
1519 autocmd BufReadPre <buffer> :norm! 70gg
1520 " triggers BufReadPre, should not move the cursor in either window
1521 e
1522 call assert_equal(1, winsaveview().topline)
1523 call assert_equal(1, winsaveview().lnum)
1524 call assert_equal(3, b:bufreadpre)
1525 wincmd p
1526 call assert_equal(g:wsv1.topline, winsaveview().topline)
1527 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
1528 call assert_equal(3, b:bufreadpre)
1529 close
1530 close
1531 call delete('XAutocmdBufReadPre.txt')
1532 set cpo-=g
1533endfunc
1534
Bram Moolenaar5e66b422019-01-24 21:58:10 +01001535" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02001536
1537" Tests for the following autocommands:
1538" - FileWritePre writing a compressed file
1539" - FileReadPost reading a compressed file
1540" - BufNewFile reading a file template
1541" - BufReadPre decompressing the file to be read
1542" - FilterReadPre substituting characters in the temp file
1543" - FilterReadPost substituting characters after filtering
1544" - FileReadPre set options for decompression
1545" - FileReadPost decompress the file
1546func Test_ReadWrite_Autocmds()
1547 " Run this test only on Unix-like systems and if gzip is available
1548 if !has('unix') || !executable("gzip")
1549 return
1550 endif
1551
1552 " Make $GZIP empty, "-v" would cause trouble.
1553 let $GZIP = ""
1554
1555 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
1556 " being modified outside of Vim (noticed on Solaris).
1557 au FileChangedShell * echo 'caught FileChangedShell'
1558
1559 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
1560 augroup Test1
1561 au!
1562 au FileWritePre *.gz '[,']!gzip
1563 au FileWritePost *.gz undo
1564 au FileReadPost *.gz '[,']!gzip -d
1565 augroup END
1566
1567 new
1568 set bin
1569 call append(0, [
1570 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
1571 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1572 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
1573 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1574 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
1575 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1576 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
1577 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1578 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
1579 \ ])
1580 1,9write! Xtestfile.gz
1581 enew! | close
1582
1583 new
1584 " Read and decompress the testfile
1585 0read Xtestfile.gz
1586 call assert_equal([
1587 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
1588 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1589 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
1590 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1591 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
1592 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1593 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
1594 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1595 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
1596 \ ], getline(1, 9))
1597 enew! | close
1598
1599 augroup Test1
1600 au!
1601 augroup END
1602
1603 " Test for the FileAppendPre and FileAppendPost autocmds
1604 augroup Test2
1605 au!
1606 au BufNewFile *.c read Xtest.c
1607 au FileAppendPre *.out '[,']s/new/NEW/
1608 au FileAppendPost *.out !cat Xtest.c >> test.out
1609 augroup END
1610
1611 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
1612 new foo.c " should load Xtest.c
1613 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
1614 w! >> test.out " append it to the output file
1615
1616 let contents = readfile('test.out')
1617 call assert_equal(' * Here is a NEW .c file', contents[2])
1618 call assert_equal(' * Here is a new .c file', contents[5])
1619
1620 call delete('test.out')
1621 enew! | close
1622 augroup Test2
1623 au!
1624 augroup END
1625
1626 " Test for the BufReadPre and BufReadPost autocmds
1627 augroup Test3
1628 au!
1629 " setup autocommands to decompress before reading and re-compress
1630 " afterwards
1631 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
1632 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
1633 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
1634 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
1635 augroup END
1636
1637 e! Xtestfile.gz " Edit compressed file
1638 call assert_equal([
1639 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
1640 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1641 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
1642 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1643 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
1644 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1645 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
1646 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1647 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
1648 \ ], getline(1, 9))
1649
1650 w! >> test.out " Append it to the output file
1651
1652 augroup Test3
1653 au!
1654 augroup END
1655
1656 " Test for the FilterReadPre and FilterReadPost autocmds.
1657 set shelltemp " need temp files here
1658 augroup Test4
1659 au!
1660 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
1661 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
1662 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
1663 au FilterReadPost *.out '[,']s/x/X/g
1664 augroup END
1665
1666 e! test.out " Edit the output file
1667 1,$!cat
1668 call assert_equal([
1669 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
1670 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
1671 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
1672 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
1673 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
1674 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
1675 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
1676 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
1677 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
1678 \ ], getline(1, 9))
1679 call assert_equal([
1680 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
1681 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1682 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
1683 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1684 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
1685 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1686 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
1687 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1688 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
1689 \ ], readfile('test.out'))
1690
1691 augroup Test4
1692 au!
1693 augroup END
1694 set shelltemp&vim
1695
1696 " Test for the FileReadPre and FileReadPost autocmds.
1697 augroup Test5
1698 au!
1699 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
1700 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
1701 au FileReadPost *.gz '[,']s/l/L/
1702 augroup END
1703
1704 new
1705 0r Xtestfile.gz " Read compressed file
1706 call assert_equal([
1707 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
1708 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1709 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
1710 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1711 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
1712 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1713 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
1714 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1715 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
1716 \ ], getline(1, 9))
1717 call assert_equal([
1718 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
1719 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1720 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
1721 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1722 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
1723 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1724 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
1725 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1726 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
1727 \ ], readfile('Xtestfile.gz'))
1728
1729 augroup Test5
1730 au!
1731 augroup END
1732
1733 au! FileChangedShell
1734 call delete('Xtestfile.gz')
1735 call delete('Xtest.c')
1736 call delete('test.out')
1737endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02001738
1739func Test_throw_in_BufWritePre()
1740 new
1741 call setline(1, ['one', 'two', 'three'])
1742 call assert_false(filereadable('Xthefile'))
1743 augroup throwing
1744 au BufWritePre X* throw 'do not write'
1745 augroup END
1746 try
1747 w Xthefile
1748 catch
1749 let caught = 1
1750 endtry
1751 call assert_equal(1, caught)
1752 call assert_false(filereadable('Xthefile'))
1753
1754 bwipe!
1755 au! throwing
1756endfunc