blob: 8db06d08319cb3620f6249bed6395dfdbb4d70c3 [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')
21 func ExitInsertMode(id)
22 call feedkeys("\<Esc>")
23 endfunc
24
25 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020026 " Need to move the cursor.
27 call feedkeys("ggG", "xt")
28
Bram Moolenaarc67e8922016-05-24 16:07:40 +020029 let g:triggered = 0
30 au CursorHoldI * let g:triggered += 1
31 set updatetime=20
32 call timer_start(100, 'ExitInsertMode')
33 call feedkeys('a', 'x!')
34 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010035 unlet g:triggered
36 au! CursorHoldI
37 set updatetime&
38 endfunc
39
40 func Test_cursorhold_insert_with_timer_interrupt()
41 if !has('job')
42 return
43 endif
44 " Need to move the cursor.
45 call feedkeys("ggG", "xt")
46
47 " Confirm the timer invoked in exit_cb of the job doesn't disturb
48 " CursorHoldI event.
49 let g:triggered = 0
50 au CursorHoldI * let g:triggered += 1
51 set updatetime=500
52 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar8d4ce562019-01-30 22:01:40 +010053 \ {'exit_cb': {-> timer_start(1000, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010054 call feedkeys('a', 'x!')
55 call assert_equal(1, g:triggered)
56 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020057 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020058 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020059 endfunc
60
61 func Test_cursorhold_insert_ctrl_x()
62 let g:triggered = 0
63 au CursorHoldI * let g:triggered += 1
64 set updatetime=20
65 call timer_start(100, 'ExitInsertMode')
66 " CursorHoldI does not trigger after CTRL-X
67 call feedkeys("a\<C-X>", 'x!')
68 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010069 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020070 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020071 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020072 endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020073endif
74
Bram Moolenaar04f62f82017-07-19 18:18:39 +020075func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +020076 augroup test_bufunload_group
77 autocmd!
78 autocmd BufUnload * call add(s:li, "bufunload")
79 autocmd BufDelete * call add(s:li, "bufdelete")
80 autocmd BufWipeout * call add(s:li, "bufwipeout")
81 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +020082
Bram Moolenaarc67e8922016-05-24 16:07:40 +020083 let s:li=[]
84 new
85 setlocal bufhidden=
86 bunload
87 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +020088
Bram Moolenaarc67e8922016-05-24 16:07:40 +020089 let s:li=[]
90 new
91 setlocal bufhidden=delete
92 bunload
93 call assert_equal(["bufunload", "bufdelete"], s:li)
94
95 let s:li=[]
96 new
97 setlocal bufhidden=unload
98 bwipeout
99 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
100
Bram Moolenaare99e8442016-07-26 20:43:40 +0200101 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200102 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200103endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200104
105" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200106func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200107 tabedit
108 tabfirst
109
110 augroup test_autocmd_bufunload_with_tabnext_group
111 autocmd!
112 autocmd BufUnload <buffer> tabnext
113 augroup END
114
115 quit
116 call assert_equal(2, tabpagenr('$'))
117
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200118 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200119 augroup! test_autocmd_bufunload_with_tabnext_group
120 tablast
121 quit
122endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200123
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200124func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200125 tabedit
126 augroup sample
127 autocmd!
128 autocmd BufWinLeave <buffer> tabfirst
129 augroup END
130 call setline(1, ['a', 'b', 'c'])
131 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200132 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200133endfunc
134
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200135" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200136func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200137 split aa.txt
138 let lastbuf = bufnr('$')
139
140 augroup test_autocmd_bufunload
141 autocmd!
142 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
143 augroup END
144
Bram Moolenaara997b452018-04-17 23:24:06 +0200145 " Todo: check for E937 generated first
146 " call assert_fails('edit bb.txt', 'E937:')
147 call assert_fails('edit bb.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200148
149 autocmd! test_autocmd_bufunload
150 augroup! test_autocmd_bufunload
151 bwipe! aa.txt
152 bwipe! bb.txt
153endfunc
154
155" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200156func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200157 setlocal buftype=nowrite
158 let lastbuf = bufnr('$')
159
160 augroup test_autocmd_bufunload
161 autocmd!
162 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
163 augroup END
164
165 normal! i1
166 call assert_fails('edit a.txt', 'E517:')
167 call feedkeys("\<CR>")
168
169 autocmd! test_autocmd_bufunload
170 augroup! test_autocmd_bufunload
171 bwipe! a.txt
172endfunc
173
Bram Moolenaarc917da42016-07-19 22:31:36 +0200174func Test_win_tab_autocmd()
175 let g:record = []
176
177 augroup testing
178 au WinNew * call add(g:record, 'WinNew')
179 au WinEnter * call add(g:record, 'WinEnter')
180 au WinLeave * call add(g:record, 'WinLeave')
181 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200182 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200183 au TabEnter * call add(g:record, 'TabEnter')
184 au TabLeave * call add(g:record, 'TabLeave')
185 augroup END
186
187 split
188 tabnew
189 close
190 close
191
192 call assert_equal([
193 \ 'WinLeave', 'WinNew', 'WinEnter',
194 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200195 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200196 \ 'WinLeave', 'WinEnter'
197 \ ], g:record)
198
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200199 let g:record = []
200 tabnew somefile
201 tabnext
202 bwipe somefile
203
204 call assert_equal([
205 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
206 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
207 \ 'TabClosed'
208 \ ], g:record)
209
Bram Moolenaarc917da42016-07-19 22:31:36 +0200210 augroup testing
211 au!
212 augroup END
213 unlet g:record
214endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200215
216func s:AddAnAutocmd()
217 augroup vimBarTest
218 au BufReadCmd * echo 'hello'
219 augroup END
220 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
221endfunc
222
223func Test_early_bar()
224 " test that a bar is recognized before the {event}
225 call s:AddAnAutocmd()
226 augroup vimBarTest | au! | augroup END
227 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
228
229 call s:AddAnAutocmd()
230 augroup vimBarTest| au!| augroup END
231 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
232
233 " test that a bar is recognized after the {event}
234 call s:AddAnAutocmd()
235 augroup vimBarTest| au!BufReadCmd| augroup END
236 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
237
238 " test that a bar is recognized after the {group}
239 call s:AddAnAutocmd()
240 au! vimBarTest|echo 'hello'
241 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
242endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200243
Bram Moolenaar5c809082016-09-01 16:21:48 +0200244func RemoveGroup()
245 autocmd! StartOK
246 augroup! StartOK
247endfunc
248
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200249func Test_augroup_warning()
250 augroup TheWarning
251 au VimEnter * echo 'entering'
252 augroup END
253 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
254 redir => res
255 augroup! TheWarning
256 redir END
257 call assert_true(match(res, "W19:") >= 0)
258 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
259
260 " check "Another" does not take the pace of the deleted entry
261 augroup Another
262 augroup END
263 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200264 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200265
266 " no warning for postpone aucmd delete
267 augroup StartOK
268 au VimEnter * call RemoveGroup()
269 augroup END
270 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
271 redir => res
272 doautocmd VimEnter
273 redir END
274 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200275 au! VimEnter
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200276endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200277
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200278func Test_BufReadCmdHelp()
279 " This used to cause access to free memory
280 au BufReadCmd * e +h
281 help
282
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200283 au! BufReadCmd
284endfunc
285
286func Test_BufReadCmdHelpJump()
287 " This used to cause access to free memory
288 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200289 " } to fix highlighting
290 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200291
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200292 au! BufReadCmd
293endfunc
294
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200295func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200296 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200297 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200298 call assert_fails('augroup! x', 'E936:')
299 au VimEnter * echo
300 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200301 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200302 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
303 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200304endfunc
305
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200306" Tests for autocommands on :close command.
307" This used to be in test13.
308func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200309 " Clean up buffers, because in some cases this function fails.
310 call s:cleanup_buffers()
311
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200312 " Write three files and open them, each in a window.
313 " Then go to next window, with autocommand that deletes the previous one.
314 " Do this twice, writing the file.
315 e! Xtestje1
316 call setline(1, 'testje1')
317 w
318 sp Xtestje2
319 call setline(1, 'testje2')
320 w
321 sp Xtestje3
322 call setline(1, 'testje3')
323 w
324 wincmd w
325 au WinLeave Xtestje2 bwipe
326 wincmd w
327 call assert_equal('Xtestje1', expand('%'))
328
329 au WinLeave Xtestje1 bwipe Xtestje3
330 close
331 call assert_equal('Xtestje1', expand('%'))
332
333 " Test deleting the buffer on a Unload event. If this goes wrong there
334 " will be the ATTENTION prompt.
335 e Xtestje1
336 au!
337 au! BufUnload Xtestje1 bwipe
338 call assert_fails('e Xtestje3', 'E937:')
339 call assert_equal('Xtestje3', expand('%'))
340
341 e Xtestje2
342 sp Xtestje1
343 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200344 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200345
346 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
347 " there are ml_line errors and/or a Crash.
348 au!
349 only
350 e Xanother
351 e Xtestje1
352 bwipe Xtestje2
353 bwipe Xtestje3
354 au BufWipeout Xtestje1 buf Xtestje1
355 bwipe
356 call assert_equal('Xanother', expand('%'))
357
358 only
359 help
360 wincmd w
361 1quit
362 call assert_equal('Xanother', expand('%'))
363
364 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100365 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200366 call delete('Xtestje1')
367 call delete('Xtestje2')
368 call delete('Xtestje3')
369endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100370
371func Test_BufEnter()
372 au! BufEnter
373 au Bufenter * let val = val . '+'
374 let g:val = ''
375 split NewFile
376 call assert_equal('+', g:val)
377 bwipe!
378 call assert_equal('++', g:val)
379
380 " Also get BufEnter when editing a directory
381 call mkdir('Xdir')
382 split Xdir
383 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100384
385 " On MS-Windows we can't edit the directory, make sure we wipe the right
386 " buffer.
387 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100388
389 call delete('Xdir', 'd')
390 au! BufEnter
391endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100392
393" Closing a window might cause an endless loop
394" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200395func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200396 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100397 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200398 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100399 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100400 mksession!
401
Bram Moolenaare94260f2017-03-21 15:50:12 +0100402 let content = ['set nocp noswapfile',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100403 \ 'let v:swapchoice="e"',
404 \ 'augroup test_autocmd_sessionload',
405 \ 'autocmd!',
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200406 \ 'autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100407 \ 'augroup END',
408 \ '',
409 \ 'func WriteErrors()',
410 \ ' call writefile([execute("messages")], "Xerrors")',
411 \ 'endfunc',
412 \ 'au VimLeave * call WriteErrors()',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100413 \ ]
414 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100415 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
416 let errors = join(readfile('Xerrors'))
417 call assert_match('E814', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100418
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100419 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100420 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100421 call delete(file)
422 endfor
423endfunc
424
425" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200426func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100427 tabnew
428 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100429 mksession!
430
431 let content = ['set nocp noswapfile',
432 \ 'function! DeleteInactiveBufs()',
433 \ ' tabfirst',
434 \ ' let tabblist = []',
435 \ ' for i in range(1, tabpagenr(''$''))',
436 \ ' call extend(tabblist, tabpagebuflist(i))',
437 \ ' endfor',
438 \ ' for b in range(1, bufnr(''$''))',
439 \ ' if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')',
440 \ ' exec ''bwipeout '' . b',
441 \ ' endif',
442 \ ' endfor',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100443 \ ' echomsg "SessionLoadPost DONE"',
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100444 \ 'endfunction',
Bram Moolenaare94260f2017-03-21 15:50:12 +0100445 \ 'au SessionLoadPost * call DeleteInactiveBufs()',
446 \ '',
447 \ 'func WriteErrors()',
448 \ ' call writefile([execute("messages")], "Xerrors")',
449 \ 'endfunc',
450 \ 'au VimLeave * call WriteErrors()',
451 \ ]
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100452 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100453 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
454 let errors = join(readfile('Xerrors'))
455 " This probably only ever matches on unix.
456 call assert_notmatch('Caught deadly signal SEGV', errors)
457 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100458
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100459 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100460 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100461 call delete(file)
462 endfor
463endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200464
465func Test_empty_doau()
466 doau \|
467endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200468
469func s:AutoCommandOptionSet(match)
470 let item = remove(g:options, 0)
471 let expected = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3])
472 let actual = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", a:match, v:option_old, v:option_new, v:option_type)
473 let g:opt = [expected, actual]
474 "call assert_equal(expected, actual)
475endfunc
476
477func Test_OptionSet()
Bram Moolenaar26d98212019-01-27 22:32:55 +0100478 if !has("eval") || !exists("+autochdir")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200479 return
480 endif
481
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200482 badd test_autocmd.vim
483
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200484 call test_override('starting', 1)
485 set nocp
486 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
487
488 " 1: Setting number option"
489 let g:options=[['number', 0, 1, 'global']]
490 set nu
491 call assert_equal([], g:options)
492 call assert_equal(g:opt[0], g:opt[1])
493
494 " 2: Setting local number option"
495 let g:options=[['number', 1, 0, 'local']]
496 setlocal nonu
497 call assert_equal([], g:options)
498 call assert_equal(g:opt[0], g:opt[1])
499
500 " 3: Setting global number option"
501 let g:options=[['number', 1, 0, 'global']]
502 setglobal nonu
503 call assert_equal([], g:options)
504 call assert_equal(g:opt[0], g:opt[1])
505
506 " 4: Setting local autoindent option"
507 let g:options=[['autoindent', 0, 1, 'local']]
508 setlocal ai
509 call assert_equal([], g:options)
510 call assert_equal(g:opt[0], g:opt[1])
511
512 " 5: Setting global autoindent option"
513 let g:options=[['autoindent', 0, 1, 'global']]
514 setglobal ai
515 call assert_equal([], g:options)
516 call assert_equal(g:opt[0], g:opt[1])
517
518 " 6: Setting global autoindent option"
519 let g:options=[['autoindent', 1, 0, 'global']]
520 set ai!
521 call assert_equal([], g:options)
522 call assert_equal(g:opt[0], g:opt[1])
523
524 " Should not print anything, use :noa
525 " 7: don't trigger OptionSet"
526 let g:options=[['invalid', 1, 1, 'invalid']]
527 noa set nonu
528 call assert_equal([['invalid', 1, 1, 'invalid']], g:options)
529 call assert_equal(g:opt[0], g:opt[1])
530
531 " 8: Setting several global list and number option"
532 let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']]
533 set list nu
534 call assert_equal([], g:options)
535 call assert_equal(g:opt[0], g:opt[1])
536
537 " 9: don't trigger OptionSet"
538 let g:options=[['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']]
539 noa set nolist nonu
540 call assert_equal([['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']], g:options)
541 call assert_equal(g:opt[0], g:opt[1])
542
543 " 10: Setting global acd"
544 let g:options=[['autochdir', 0, 1, 'local']]
545 setlocal acd
546 call assert_equal([], g:options)
547 call assert_equal(g:opt[0], g:opt[1])
548
549 " 11: Setting global autoread (also sets local value)"
550 let g:options=[['autoread', 0, 1, 'global']]
551 set ar
552 call assert_equal([], g:options)
553 call assert_equal(g:opt[0], g:opt[1])
554
555 " 12: Setting local autoread"
556 let g:options=[['autoread', 1, 1, 'local']]
557 setlocal ar
558 call assert_equal([], g:options)
559 call assert_equal(g:opt[0], g:opt[1])
560
561 " 13: Setting global autoread"
562 let g:options=[['autoread', 1, 0, 'global']]
563 setglobal invar
564 call assert_equal([], g:options)
565 call assert_equal(g:opt[0], g:opt[1])
566
567 " 14: Setting option backspace through :let"
568 let g:options=[['backspace', '', 'eol,indent,start', 'global']]
569 let &bs="eol,indent,start"
570 call assert_equal([], g:options)
571 call assert_equal(g:opt[0], g:opt[1])
572
573 " 15: Setting option backspace through setbufvar()"
574 let g:options=[['backup', 0, 1, 'local']]
575 " try twice, first time, shouldn't trigger because option name is invalid,
576 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200577 let bnum = bufnr('%')
578 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200579 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200580 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200581 call assert_equal([], g:options)
582 call assert_equal(g:opt[0], g:opt[1])
583
584 " 16: Setting number option using setwinvar"
585 let g:options=[['number', 0, 1, 'local']]
586 call setwinvar(0, '&number', 1)
587 call assert_equal([], g:options)
588 call assert_equal(g:opt[0], g:opt[1])
589
590 " 17: Setting key option, shouldn't trigger"
591 let g:options=[['key', 'invalid', 'invalid1', 'invalid']]
592 setlocal key=blah
593 setlocal key=
594 call assert_equal([['key', 'invalid', 'invalid1', 'invalid']], g:options)
595 call assert_equal(g:opt[0], g:opt[1])
596
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200597 " 18: Setting string option"
598 let oldval = &tags
599 let g:options=[['tags', oldval, 'tagpath', 'global']]
600 set tags=tagpath
601 call assert_equal([], g:options)
602 call assert_equal(g:opt[0], g:opt[1])
603
604 " 1l: Resetting string option"
605 let g:options=[['tags', 'tagpath', oldval, 'global']]
606 set tags&
607 call assert_equal([], g:options)
608 call assert_equal(g:opt[0], g:opt[1])
609
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200610 " Cleanup
611 au! OptionSet
612 for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp']
Bram Moolenaar91d2e782018-08-07 19:05:01 +0200613 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200614 endfor
615 call test_override('starting', 0)
616 delfunc! AutoCommandOptionSet
617endfunc
618
619func Test_OptionSet_diffmode()
620 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +0100621 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200622 new
623 au OptionSet diff :let &l:cul=v:option_new
624
625 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
626 call assert_equal(0, &l:cul)
627 diffthis
628 call assert_equal(1, &l:cul)
629
630 vnew
631 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
632 call assert_equal(0, &l:cul)
633 diffthis
634 call assert_equal(1, &l:cul)
635
636 diffoff
637 call assert_equal(0, &l:cul)
638 call assert_equal(1, getwinvar(2, '&l:cul'))
639 bw!
640
641 call assert_equal(1, &l:cul)
642 diffoff!
643 call assert_equal(0, &l:cul)
644 call assert_equal(0, getwinvar(1, '&l:cul'))
645 bw!
646
647 " Cleanup
648 au! OptionSet
649 call test_override('starting', 0)
650endfunc
651
652func Test_OptionSet_diffmode_close()
653 call test_override('starting', 1)
654 " 19: Try to close the current window when entering diff mode
655 " should not segfault
656 new
657 au OptionSet diff close
658
659 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
660 call assert_fails(':diffthis', 'E788')
661 call assert_equal(1, &diff)
662 vnew
663 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
664 call assert_fails(':diffthis', 'E788')
665 call assert_equal(1, &diff)
666 bw!
667 call assert_fails(':diffoff!', 'E788')
668 bw!
669
670 " Cleanup
671 au! OptionSet
672 call test_override('starting', 0)
673 "delfunc! AutoCommandOptionSet
674endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +0200675
Bram Moolenaar48f377a2018-12-21 13:03:28 +0100676func Test_OptionSet_modeline()
677 call test_override('starting', 1)
678 au! OptionSet
679 augroup set_tabstop
680 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
681 augroup END
682 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline')
683 set modeline
684 let v:errmsg = ''
685 call assert_fails('split XoptionsetModeline', 'E12:')
686 call assert_equal(7, &ts)
687 call assert_equal('', v:errmsg)
688
689 augroup set_tabstop
690 au!
691 augroup END
692 bwipe!
693 set ts&
694 call delete('XoptionsetModeline')
695 call test_override('starting', 0)
696endfunc
697
Bram Moolenaar4a137b42017-08-04 22:37:11 +0200698" Test for Bufleave autocommand that deletes the buffer we are about to edit.
699func Test_BufleaveWithDelete()
700 new | edit Xfile1
701
702 augroup test_bufleavewithdelete
703 autocmd!
704 autocmd BufLeave Xfile1 bwipe Xfile2
705 augroup END
706
707 call assert_fails('edit Xfile2', 'E143:')
708 call assert_equal('Xfile1', bufname('%'))
709
710 autocmd! test_bufleavewithdelete BufLeave Xfile1
711 augroup! test_bufleavewithdelete
712
713 new
714 bwipe! Xfile1
715endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200716
717" Test for autocommand that changes the buffer list, when doing ":ball".
718func Test_Acmd_BufAll()
719 enew!
720 %bwipe!
721 call writefile(['Test file Xxx1'], 'Xxx1')
722 call writefile(['Test file Xxx2'], 'Xxx2')
723 call writefile(['Test file Xxx3'], 'Xxx3')
724
725 " Add three files to the buffer list
726 split Xxx1
727 close
728 split Xxx2
729 close
730 split Xxx3
731 close
732
733 " Wipe the buffer when the buffer is opened
734 au BufReadPost Xxx2 bwipe
735
736 call append(0, 'Test file Xxx4')
737 ball
738
739 call assert_equal(2, winnr('$'))
740 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
741 wincmd t
742
743 au! BufReadPost
744 %bwipe!
745 call delete('Xxx1')
746 call delete('Xxx2')
747 call delete('Xxx3')
748 enew! | only
749endfunc
750
751" Test for autocommand that changes current buffer on BufEnter event.
752" Check if modelines are interpreted for the correct buffer.
753func Test_Acmd_BufEnter()
754 %bwipe!
755 call writefile(['start of test file Xxx1',
756 \ "\<Tab>this is a test",
757 \ 'end of test file Xxx1'], 'Xxx1')
758 call writefile(['start of test file Xxx2',
759 \ 'vim: set noai :',
760 \ "\<Tab>this is a test",
761 \ 'end of test file Xxx2'], 'Xxx2')
762
763 au BufEnter Xxx2 brew
764 set ai modeline modelines=3
765 edit Xxx1
766 " edit Xxx2, autocmd will do :brew
767 edit Xxx2
768 exe "normal G?this is a\<CR>"
769 " Append text with autoindent to this file
770 normal othis should be auto-indented
771 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
772 call assert_equal(3, line('.'))
773 " Remove autocmd and edit Xxx2 again
774 au! BufEnter Xxx2
775 buf! Xxx2
776 exe "normal G?this is a\<CR>"
777 " append text without autoindent to Xxx
778 normal othis should be in column 1
779 call assert_equal("this should be in column 1", getline('.'))
780 call assert_equal(4, line('.'))
781
782 %bwipe!
783 call delete('Xxx1')
784 call delete('Xxx2')
785 set ai&vim modeline&vim modelines&vim
786endfunc
787
788" Test for issue #57
789" do not move cursor on <c-o> when autoindent is set
790func Test_ai_CTRL_O()
791 enew!
792 set ai
793 let save_fo = &fo
794 set fo+=r
795 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
796 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
797 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
798
799 set ai&vim
800 let &fo = save_fo
801 enew!
802endfunc
803
804" Test for autocommand that deletes the current buffer on BufLeave event.
805" Also test deleting the last buffer, should give a new, empty buffer.
806func Test_BufLeave_Wipe()
807 %bwipe!
808 let content = ['start of test file Xxx',
809 \ 'this is a test',
810 \ 'end of test file Xxx']
811 call writefile(content, 'Xxx1')
812 call writefile(content, 'Xxx2')
813
814 au BufLeave Xxx2 bwipe
815 edit Xxx1
816 split Xxx2
817 " delete buffer Xxx2, we should be back to Xxx1
818 bwipe
819 call assert_equal('Xxx1', bufname('%'))
820 call assert_equal(1, winnr('$'))
821
822 " Create an alternate buffer
823 %write! test.out
824 call assert_equal('test.out', bufname('#'))
825 " delete alternate buffer
826 bwipe test.out
827 call assert_equal('Xxx1', bufname('%'))
828 call assert_equal('', bufname('#'))
829
830 au BufLeave Xxx1 bwipe
831 " delete current buffer, get an empty one
832 bwipe!
833 call assert_equal(1, line('$'))
834 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200835 let g:bufinfo = getbufinfo()
836 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200837
838 call delete('Xxx1')
839 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200840 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200841 %bwipe
842 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200843
844 " check that bufinfo doesn't contain a pointer to freed memory
845 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200846endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +0200847
848func Test_QuitPre()
849 edit Xfoo
850 let winid = win_getid(winnr())
851 split Xbar
852 au! QuitPre * let g:afile = expand('<afile>')
853 " Close the other window, <afile> should be correct.
854 exe win_id2win(winid) . 'q'
855 call assert_equal('Xfoo', g:afile)
856
857 unlet g:afile
858 bwipe Xfoo
859 bwipe Xbar
860endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200861
862func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +0100863 au! CmdlineChanged : let g:text = getcmdline()
864 let g:text = 0
865 call feedkeys(":echom 'hello'\<CR>", 'xt')
866 call assert_equal("echom 'hello'", g:text)
867 au! CmdlineChanged
868
869 au! CmdlineChanged : let g:entered = expand('<afile>')
870 let g:entered = 0
871 call feedkeys(":echom 'hello'\<CR>", 'xt')
872 call assert_equal(':', g:entered)
873 au! CmdlineChanged
874
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200875 au! CmdlineEnter : let g:entered = expand('<afile>')
876 au! CmdlineLeave : let g:left = expand('<afile>')
877 let g:entered = 0
878 let g:left = 0
879 call feedkeys(":echo 'hello'\<CR>", 'xt')
880 call assert_equal(':', g:entered)
881 call assert_equal(':', g:left)
882 au! CmdlineEnter
883 au! CmdlineLeave
884
Bram Moolenaara4baf5b2018-04-22 13:27:44 +0200885 let save_shellslash = &shellslash
886 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200887 au! CmdlineEnter / let g:entered = expand('<afile>')
888 au! CmdlineLeave / let g:left = expand('<afile>')
889 let g:entered = 0
890 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200891 new
892 call setline(1, 'hello')
893 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200894 call assert_equal('/', g:entered)
895 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200896 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200897 au! CmdlineEnter
898 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +0200899 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +0200900endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +0200901
902" Test for BufWritePre autocommand that deletes or unloads the buffer.
903func Test_BufWritePre()
904 %bwipe
905 au BufWritePre Xxx1 bunload
906 au BufWritePre Xxx2 bwipe
907
908 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
909 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
910
911 edit Xtest
912 e! Xxx2
913 bdel Xtest
914 e Xxx1
915 " write it, will unload it and give an error msg
916 call assert_fails('w', 'E203')
917 call assert_equal('Xxx2', bufname('%'))
918 edit Xtest
919 e! Xxx2
920 bwipe Xtest
921 " write it, will delete the buffer and give an error msg
922 call assert_fails('w', 'E203')
923 call assert_equal('Xxx1', bufname('%'))
924 au! BufWritePre
925 call delete('Xxx1')
926 call delete('Xxx2')
927endfunc
928
929" Test for BufUnload autocommand that unloads all the other buffers
930func Test_bufunload_all()
931 call writefile(['Test file Xxx1'], 'Xxx1')"
932 call writefile(['Test file Xxx2'], 'Xxx2')"
933
934 let content = [
935 \ "func UnloadAllBufs()",
936 \ " let i = 1",
937 \ " while i <= bufnr('$')",
938 \ " if i != bufnr('%') && bufloaded(i)",
939 \ " exe i . 'bunload'",
940 \ " endif",
941 \ " let i += 1",
942 \ " endwhile",
943 \ "endfunc",
944 \ "au BufUnload * call UnloadAllBufs()",
945 \ "au VimLeave * call writefile(['Test Finished'], 'Xout')",
946 \ "edit Xxx1",
947 \ "split Xxx2",
948 \ "q"]
949 call writefile(content, 'Xtest')
950
951 call delete('Xout')
952 call system(v:progpath. ' --clean -N --not-a-term -S Xtest')
953 call assert_true(filereadable('Xout'))
954
955 call delete('Xxx1')
956 call delete('Xxx2')
957 call delete('Xtest')
958 call delete('Xout')
959endfunc
960
961" Some tests for buffer-local autocommands
962func Test_buflocal_autocmd()
963 let g:bname = ''
964 edit xx
965 au BufLeave <buffer> let g:bname = expand("%")
966 " here, autocommand for xx should trigger.
967 " but autocommand shall not apply to buffer named <buffer>.
968 edit somefile
969 call assert_equal('xx', g:bname)
970 let g:bname = ''
971 " here, autocommand shall be auto-deleted
972 bwipe xx
973 " autocmd should not trigger
974 edit xx
975 call assert_equal('', g:bname)
976 " autocmd should not trigger
977 edit somefile
978 call assert_equal('', g:bname)
979 enew
980 unlet g:bname
981endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +0100982
983" Test for "*Cmd" autocommands
984func Test_Cmd_Autocmds()
985 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
986
987 enew!
988 au BufReadCmd XtestA 0r Xxx|$del
989 edit XtestA " will read text of Xxd instead
990 call assert_equal('start of Xxx', getline(1))
991
992 au BufWriteCmd XtestA call append(line("$"), "write")
993 write " will append a line to the file
994 call assert_equal('write', getline('$'))
995 call assert_fails('read XtestA', 'E484') " should not read anything
996 call assert_equal('write', getline(4))
997
998 " now we have:
999 " 1 start of Xxx
1000 " 2 abc2
1001 " 3 end of Xxx
1002 " 4 write
1003
1004 au FileReadCmd XtestB '[r Xxx
1005 2r XtestB " will read Xxx below line 2 instead
1006 call assert_equal('start of Xxx', getline(3))
1007
1008 " now we have:
1009 " 1 start of Xxx
1010 " 2 abc2
1011 " 3 start of Xxx
1012 " 4 abc2
1013 " 5 end of Xxx
1014 " 6 end of Xxx
1015 " 7 write
1016
1017 au FileWriteCmd XtestC '[,']copy $
1018 normal 4GA1
1019 4,5w XtestC " will copy lines 4 and 5 to the end
1020 call assert_equal("\tabc21", getline(8))
1021 call assert_fails('r XtestC', 'E484') " should not read anything
1022 call assert_equal("end of Xxx", getline(9))
1023
1024 " now we have:
1025 " 1 start of Xxx
1026 " 2 abc2
1027 " 3 start of Xxx
1028 " 4 abc21
1029 " 5 end of Xxx
1030 " 6 end of Xxx
1031 " 7 write
1032 " 8 abc21
1033 " 9 end of Xxx
1034
1035 let g:lines = []
1036 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1037 w >>XtestD " will add lines to 'lines'
1038 call assert_equal(9, len(g:lines))
1039 call assert_fails('$r XtestD', 'E484') " should not read anything
1040 call assert_equal(9, line('$'))
1041 call assert_equal('end of Xxx', getline('$'))
1042
1043 au BufReadCmd XtestE 0r Xxx|$del
1044 sp XtestE " split window with test.out
1045 call assert_equal('end of Xxx', getline(3))
1046
1047 let g:lines = []
1048 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1049 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1050 wall " will write other window to 'lines'
1051 call assert_equal(4, len(g:lines), g:lines)
1052 call assert_equal('asdf', g:lines[2])
1053
1054 au! BufReadCmd
1055 au! BufWriteCmd
1056 au! FileReadCmd
1057 au! FileWriteCmd
1058 au! FileAppendCmd
1059 %bwipe!
1060 call delete('Xxx')
1061 enew!
1062endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001063
1064func SetChangeMarks(start, end)
1065 exe a:start. 'mark ['
1066 exe a:end. 'mark ]'
1067endfunc
1068
1069" Verify the effects of autocmds on '[ and ']
1070func Test_change_mark_in_autocmds()
1071 edit! Xtest
1072 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u", 'xtn')
1073
1074 call SetChangeMarks(2, 3)
1075 write
1076 call assert_equal([1, 4], [line("'["), line("']")])
1077
1078 call SetChangeMarks(2, 3)
1079 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1080 write
1081 au! BufWritePre
1082
1083 if executable('cat')
1084 write XtestFilter
1085 write >> XtestFilter
1086
1087 call SetChangeMarks(2, 3)
1088 " Marks are set to the entire range of the write
1089 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1090 " '[ is adjusted to just before the line that will receive the filtered
1091 " data
1092 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1093 " The filtered data is read into the buffer, and the source lines are
1094 " still present, so the range is after the source lines
1095 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1096 %!cat XtestFilter
1097 " After the filtered data is read, the original lines are deleted
1098 call assert_equal([1, 8], [line("'["), line("']")])
1099 au! FilterWritePre,FilterReadPre,FilterReadPost
1100 undo
1101
1102 call SetChangeMarks(1, 4)
1103 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1104 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1105 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1106 2,3!cat XtestFilter
1107 call assert_equal([2, 9], [line("'["), line("']")])
1108 au! FilterWritePre,FilterReadPre,FilterReadPost
1109 undo
1110
1111 call delete('XtestFilter')
1112 endif
1113
1114 call SetChangeMarks(1, 4)
1115 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1116 2,3write Xtest2
1117 au! FileWritePre
1118
1119 call SetChangeMarks(2, 3)
1120 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1121 write >> Xtest2
1122 au! FileAppendPre
1123
1124 call SetChangeMarks(1, 4)
1125 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1126 2,3write >> Xtest2
1127 au! FileAppendPre
1128
1129 call SetChangeMarks(1, 1)
1130 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1131 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1132 3read Xtest2
1133 au! FileReadPre,FileReadPost
1134 undo
1135
1136 call SetChangeMarks(4, 4)
1137 " When the line is 0, it's adjusted to 1
1138 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1139 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1140 0read Xtest2
1141 au! FileReadPre,FileReadPost
1142 undo
1143
1144 call SetChangeMarks(4, 4)
1145 " When the line is 0, it's adjusted to 1
1146 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1147 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1148 1read Xtest2
1149 au! FileReadPre,FileReadPost
1150 undo
1151
1152 bwipe!
1153 call delete('Xtest')
1154 call delete('Xtest2')
1155endfunc
1156
1157func Test_Filter_noshelltemp()
1158 if !executable('cat')
1159 return
1160 endif
1161
1162 enew!
1163 call setline(1, ['a', 'b', 'c', 'd'])
1164
1165 let shelltemp = &shelltemp
1166 set shelltemp
1167
1168 let g:filter_au = 0
1169 au FilterWritePre * let g:filter_au += 1
1170 au FilterReadPre * let g:filter_au += 1
1171 au FilterReadPost * let g:filter_au += 1
1172 %!cat
1173 call assert_equal(3, g:filter_au)
1174
1175 if has('filterpipe')
1176 set noshelltemp
1177
1178 let g:filter_au = 0
1179 au FilterWritePre * let g:filter_au += 1
1180 au FilterReadPre * let g:filter_au += 1
1181 au FilterReadPost * let g:filter_au += 1
1182 %!cat
1183 call assert_equal(0, g:filter_au)
1184 endif
1185
1186 au! FilterWritePre,FilterReadPre,FilterReadPost
1187 let &shelltemp = shelltemp
1188 bwipe!
1189endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001190
1191func Test_TextYankPost()
1192 enew!
1193 call setline(1, ['foo'])
1194
1195 let g:event = []
1196 au TextYankPost * let g:event = copy(v:event)
1197
1198 call assert_equal({}, v:event)
1199 call assert_fails('let v:event = {}', 'E46:')
1200 call assert_fails('let v:event.mykey = 0', 'E742:')
1201
1202 norm "ayiw
1203 call assert_equal(
1204 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v'},
1205 \g:event)
1206 norm y_
1207 call assert_equal(
1208 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V'},
1209 \g:event)
1210 call feedkeys("\<C-V>y", 'x')
1211 call assert_equal(
1212 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161"},
1213 \g:event)
1214 norm "xciwbar
1215 call assert_equal(
1216 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v'},
1217 \g:event)
1218 norm "bdiw
1219 call assert_equal(
1220 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v'},
1221 \g:event)
1222
1223 call assert_equal({}, v:event)
1224
1225 au! TextYankPost
1226 unlet g:event
1227 bwipe!
1228endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001229
1230func Test_nocatch_wipe_all_buffers()
1231 " Real nasty autocommand: wipe all buffers on any event.
1232 au * * bwipe *
Bram Moolenaara997b452018-04-17 23:24:06 +02001233 " Get E93 first?
1234 " call assert_fails('next x', 'E93:')
1235 call assert_fails('next x', 'E517:')
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001236 bwipe
1237 au!
1238endfunc
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001239
1240func Test_nocatch_wipe_dummy_buffer()
1241 " Nasty autocommand: wipe buffer on any event.
1242 au * x bwipe
1243 call assert_fails('lv½ /x', 'E480')
1244 au!
1245endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001246
1247function s:Before_test_dirchanged()
1248 augroup test_dirchanged
1249 autocmd!
1250 augroup END
1251 let s:li = []
1252 let s:dir_this = getcwd()
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001253 let s:dir_foo = s:dir_this . '/foo'
1254 call mkdir(s:dir_foo)
1255 let s:dir_bar = s:dir_this . '/bar'
1256 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001257endfunc
1258
1259function s:After_test_dirchanged()
1260 exe 'cd' s:dir_this
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001261 call delete(s:dir_foo, 'd')
1262 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001263 augroup test_dirchanged
1264 autocmd!
1265 augroup END
1266endfunc
1267
1268function Test_dirchanged_global()
1269 call s:Before_test_dirchanged()
1270 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1271 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001272 exe 'cd' s:dir_foo
1273 call assert_equal(["cd:", s:dir_foo], s:li)
1274 exe 'cd' s:dir_foo
1275 call assert_equal(["cd:", s:dir_foo], s:li)
1276 exe 'lcd' s:dir_bar
1277 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001278 call s:After_test_dirchanged()
1279endfunc
1280
1281function Test_dirchanged_local()
1282 call s:Before_test_dirchanged()
1283 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1284 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001285 exe 'cd' s:dir_foo
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001286 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001287 exe 'lcd' s:dir_bar
1288 call assert_equal(["lcd:", s:dir_bar], s:li)
1289 exe 'lcd' s:dir_bar
1290 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001291 call s:After_test_dirchanged()
1292endfunc
1293
1294function Test_dirchanged_auto()
Bram Moolenaarec48a9c2018-02-03 20:11:40 +01001295 if !exists('+autochdir')
1296 return
1297 endif
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001298 call s:Before_test_dirchanged()
1299 call test_autochdir()
1300 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1301 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1302 set acd
1303 exe 'cd ..'
1304 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001305 exe 'edit ' . s:dir_foo . '/Xfile'
1306 call assert_equal(s:dir_foo, getcwd())
1307 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001308 set noacd
1309 bwipe!
1310 call s:After_test_dirchanged()
1311endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001312
1313" Test TextChangedI and TextChangedP
1314func Test_ChangedP()
1315 new
1316 call setline(1, ['foo', 'bar', 'foobar'])
1317 call test_override("char_avail", 1)
1318 set complete=. completeopt=menuone
1319
1320 func! TextChangedAutocmd(char)
1321 let g:autocmd .= a:char
1322 endfunc
1323
1324 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1325 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1326 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1327
1328 call cursor(3, 1)
1329 let g:autocmd = ''
1330 call feedkeys("o\<esc>", 'tnix')
1331 call assert_equal('I', g:autocmd)
1332
1333 let g:autocmd = ''
1334 call feedkeys("Sf", 'tnix')
1335 call assert_equal('II', g:autocmd)
1336
1337 let g:autocmd = ''
1338 call feedkeys("Sf\<C-N>", 'tnix')
1339 call assert_equal('IIP', g:autocmd)
1340
1341 let g:autocmd = ''
1342 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1343 call assert_equal('IIPP', g:autocmd)
1344
1345 let g:autocmd = ''
1346 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1347 call assert_equal('IIPPP', g:autocmd)
1348
1349 let g:autocmd = ''
1350 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1351 call assert_equal('IIPPPP', g:autocmd)
1352
1353 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1354 " TODO: how should it handle completeopt=noinsert,noselect?
1355
1356 " CleanUp
1357 call test_override("char_avail", 0)
1358 au! TextChanged
1359 au! TextChangedI
1360 au! TextChangedP
1361 delfu TextChangedAutocmd
1362 unlet! g:autocmd
1363 set complete&vim completeopt&vim
1364
1365 bw!
1366endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001367
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001368let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001369func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001370 if !g:setline_handled
1371 call setline(1, "(x)")
1372 let g:setline_handled = v:true
1373 endif
1374endfunc
1375
1376func Test_TextChangedI_with_setline()
1377 new
1378 call test_override('char_avail', 1)
1379 autocmd TextChangedI <buffer> call SetLineOne()
1380 call feedkeys("i(\<CR>\<Esc>", 'tx')
1381 call assert_equal('(', getline(1))
1382 call assert_equal('x)', getline(2))
1383 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001384 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001385 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001386
1387 call test_override('starting', 0)
1388 bwipe!
1389endfunc
1390
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001391func Test_Changed_FirstTime()
1392 if !has('terminal') || has('gui_running')
1393 return
1394 endif
1395 " Prepare file for TextChanged event.
1396 call writefile([''], 'Xchanged.txt')
1397 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1398 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001399 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001400 " In ConPTY, there is additional character which is drawn up to the width of
1401 " the screen.
1402 if has('conpty')
1403 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
1404 else
1405 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
1406 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001407 " It's only adding autocmd, so that no event occurs.
1408 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1409 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001410 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001411 call assert_equal([''], readfile('Xchanged.txt'))
1412
1413 " clean up
1414 call delete('Xchanged.txt')
1415 bwipe!
1416endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01001417
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001418func Test_autocmd_nested()
1419 let g:did_nested = 0
1420 augroup Testing
1421 au WinNew * edit somefile
1422 au BufNew * let g:did_nested = 1
1423 augroup END
1424 split
1425 call assert_equal(0, g:did_nested)
1426 close
1427 bwipe! somefile
1428
1429 " old nested argument still works
1430 augroup Testing
1431 au!
1432 au WinNew * nested edit somefile
1433 au BufNew * let g:did_nested = 1
1434 augroup END
1435 split
1436 call assert_equal(1, g:did_nested)
1437 close
1438 bwipe! somefile
1439
1440 " New ++nested argument works
1441 augroup Testing
1442 au!
1443 au WinNew * ++nested edit somefile
1444 au BufNew * let g:did_nested = 1
1445 augroup END
1446 split
1447 call assert_equal(1, g:did_nested)
1448 close
1449 bwipe! somefile
1450
1451 augroup Testing
1452 au!
1453 augroup END
1454
1455 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
1456 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
1457endfunc
1458
1459func Test_autocmd_once()
1460 " Without ++once WinNew triggers twice
1461 let g:did_split = 0
1462 augroup Testing
1463 au WinNew * let g:did_split += 1
1464 augroup END
1465 split
1466 split
1467 call assert_equal(2, g:did_split)
1468 call assert_true(exists('#WinNew'))
1469 close
1470 close
1471
1472 " With ++once WinNew triggers once
1473 let g:did_split = 0
1474 augroup Testing
1475 au!
1476 au WinNew * ++once let g:did_split += 1
1477 augroup END
1478 split
1479 split
1480 call assert_equal(1, g:did_split)
1481 call assert_false(exists('#WinNew'))
1482 close
1483 close
1484
1485 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
1486endfunc
1487
Bram Moolenaar5e66b422019-01-24 21:58:10 +01001488" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02001489
1490" Tests for the following autocommands:
1491" - FileWritePre writing a compressed file
1492" - FileReadPost reading a compressed file
1493" - BufNewFile reading a file template
1494" - BufReadPre decompressing the file to be read
1495" - FilterReadPre substituting characters in the temp file
1496" - FilterReadPost substituting characters after filtering
1497" - FileReadPre set options for decompression
1498" - FileReadPost decompress the file
1499func Test_ReadWrite_Autocmds()
1500 " Run this test only on Unix-like systems and if gzip is available
1501 if !has('unix') || !executable("gzip")
1502 return
1503 endif
1504
1505 " Make $GZIP empty, "-v" would cause trouble.
1506 let $GZIP = ""
1507
1508 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
1509 " being modified outside of Vim (noticed on Solaris).
1510 au FileChangedShell * echo 'caught FileChangedShell'
1511
1512 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
1513 augroup Test1
1514 au!
1515 au FileWritePre *.gz '[,']!gzip
1516 au FileWritePost *.gz undo
1517 au FileReadPost *.gz '[,']!gzip -d
1518 augroup END
1519
1520 new
1521 set bin
1522 call append(0, [
1523 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
1524 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1525 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
1526 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1527 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
1528 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1529 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
1530 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1531 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
1532 \ ])
1533 1,9write! Xtestfile.gz
1534 enew! | close
1535
1536 new
1537 " Read and decompress the testfile
1538 0read Xtestfile.gz
1539 call assert_equal([
1540 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
1541 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1542 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
1543 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1544 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
1545 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1546 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
1547 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1548 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
1549 \ ], getline(1, 9))
1550 enew! | close
1551
1552 augroup Test1
1553 au!
1554 augroup END
1555
1556 " Test for the FileAppendPre and FileAppendPost autocmds
1557 augroup Test2
1558 au!
1559 au BufNewFile *.c read Xtest.c
1560 au FileAppendPre *.out '[,']s/new/NEW/
1561 au FileAppendPost *.out !cat Xtest.c >> test.out
1562 augroup END
1563
1564 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
1565 new foo.c " should load Xtest.c
1566 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
1567 w! >> test.out " append it to the output file
1568
1569 let contents = readfile('test.out')
1570 call assert_equal(' * Here is a NEW .c file', contents[2])
1571 call assert_equal(' * Here is a new .c file', contents[5])
1572
1573 call delete('test.out')
1574 enew! | close
1575 augroup Test2
1576 au!
1577 augroup END
1578
1579 " Test for the BufReadPre and BufReadPost autocmds
1580 augroup Test3
1581 au!
1582 " setup autocommands to decompress before reading and re-compress
1583 " afterwards
1584 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
1585 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
1586 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
1587 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
1588 augroup END
1589
1590 e! Xtestfile.gz " Edit compressed file
1591 call assert_equal([
1592 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
1593 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1594 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
1595 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1596 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
1597 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1598 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
1599 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1600 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
1601 \ ], getline(1, 9))
1602
1603 w! >> test.out " Append it to the output file
1604
1605 augroup Test3
1606 au!
1607 augroup END
1608
1609 " Test for the FilterReadPre and FilterReadPost autocmds.
1610 set shelltemp " need temp files here
1611 augroup Test4
1612 au!
1613 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
1614 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
1615 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
1616 au FilterReadPost *.out '[,']s/x/X/g
1617 augroup END
1618
1619 e! test.out " Edit the output file
1620 1,$!cat
1621 call assert_equal([
1622 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
1623 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
1624 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
1625 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
1626 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
1627 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
1628 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
1629 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
1630 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
1631 \ ], getline(1, 9))
1632 call assert_equal([
1633 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
1634 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1635 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
1636 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1637 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
1638 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1639 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
1640 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1641 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
1642 \ ], readfile('test.out'))
1643
1644 augroup Test4
1645 au!
1646 augroup END
1647 set shelltemp&vim
1648
1649 " Test for the FileReadPre and FileReadPost autocmds.
1650 augroup Test5
1651 au!
1652 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
1653 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
1654 au FileReadPost *.gz '[,']s/l/L/
1655 augroup END
1656
1657 new
1658 0r Xtestfile.gz " Read compressed file
1659 call assert_equal([
1660 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
1661 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1662 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
1663 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1664 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
1665 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1666 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
1667 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1668 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
1669 \ ], getline(1, 9))
1670 call assert_equal([
1671 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
1672 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1673 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
1674 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1675 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
1676 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1677 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
1678 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
1679 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
1680 \ ], readfile('Xtestfile.gz'))
1681
1682 augroup Test5
1683 au!
1684 augroup END
1685
1686 au! FileChangedShell
1687 call delete('Xtestfile.gz')
1688 call delete('Xtest.c')
1689 call delete('test.out')
1690endfunc