blob: 3254db8cdc952ac2e96a1a504060f658337f97f2 [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 Moolenaarc79745a2019-05-20 22:12:34 +0200426 let content =<< trim [CODE]
427 set nocp noswapfile
428 let v:swapchoice="e"
429 augroup test_autocmd_sessionload
430 autocmd!
431 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
432 augroup END
433
434 func WriteErrors()
435 call writefile([execute("messages")], "Xerrors")
436 endfunc
437 au VimLeave * call WriteErrors()
438 [CODE]
439
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100440 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100441 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
442 let errors = join(readfile('Xerrors'))
443 call assert_match('E814', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100444
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100445 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100446 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100447 call delete(file)
448 endfor
449endfunc
450
451" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200452func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100453 tabnew
454 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100455 mksession!
456
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200457 let content =<< trim [CODE]
458 set nocp noswapfile
459 function! DeleteInactiveBufs()
460 tabfirst
461 let tabblist = []
462 for i in range(1, tabpagenr(''$''))
463 call extend(tabblist, tabpagebuflist(i))
464 endfor
465 for b in range(1, bufnr(''$''))
466 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
467 exec ''bwipeout '' . b
468 endif
469 endfor
470 echomsg "SessionLoadPost DONE"
471 endfunction
472 au SessionLoadPost * call DeleteInactiveBufs()
473
474 func WriteErrors()
475 call writefile([execute("messages")], "Xerrors")
476 endfunc
477 au VimLeave * call WriteErrors()
478 [CODE]
479
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100480 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100481 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
482 let errors = join(readfile('Xerrors'))
483 " This probably only ever matches on unix.
484 call assert_notmatch('Caught deadly signal SEGV', errors)
485 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100486
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100487 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100488 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100489 call delete(file)
490 endfor
491endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200492
493func Test_empty_doau()
494 doau \|
495endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200496
497func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200498 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200499 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200500 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
501 let actual = printf(template, a:match, v:option_old, v:option_oldlocal, v:option_oldglobal, v:option_new, v:option_type, v:option_command)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200502 let g:opt = [expected, actual]
503 "call assert_equal(expected, actual)
504endfunc
505
506func Test_OptionSet()
Bram Moolenaar26d98212019-01-27 22:32:55 +0100507 if !has("eval") || !exists("+autochdir")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200508 return
509 endif
510
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200511 badd test_autocmd.vim
512
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200513 call test_override('starting', 1)
514 set nocp
515 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
516
517 " 1: Setting number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200518 let g:options=[['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200519 set nu
520 call assert_equal([], g:options)
521 call assert_equal(g:opt[0], g:opt[1])
522
523 " 2: Setting local number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200524 let g:options=[['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200525 setlocal nonu
526 call assert_equal([], g:options)
527 call assert_equal(g:opt[0], g:opt[1])
528
529 " 3: Setting global number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200530 let g:options=[['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200531 setglobal nonu
532 call assert_equal([], g:options)
533 call assert_equal(g:opt[0], g:opt[1])
534
535 " 4: Setting local autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200536 let g:options=[['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200537 setlocal ai
538 call assert_equal([], g:options)
539 call assert_equal(g:opt[0], g:opt[1])
540
541 " 5: Setting global autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200542 let g:options=[['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200543 setglobal ai
544 call assert_equal([], g:options)
545 call assert_equal(g:opt[0], g:opt[1])
546
547 " 6: Setting global autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200548 let g:options=[['autoindent', 1, 1, 1, 0, 'global', 'set']]
549 set ai!
550 call assert_equal([], g:options)
551 call assert_equal(g:opt[0], g:opt[1])
552
553 " 6a: Setting global autoindent option"
554 let g:options=[['autoindent', 1, 1, 0, 0, 'global', 'set']]
555 noa setlocal ai
556 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200557 set ai!
558 call assert_equal([], g:options)
559 call assert_equal(g:opt[0], g:opt[1])
560
561 " Should not print anything, use :noa
562 " 7: don't trigger OptionSet"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200563 let g:options=[['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200564 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200565 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200566 call assert_equal(g:opt[0], g:opt[1])
567
568 " 8: Setting several global list and number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200569 let g:options=[['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200570 set list nu
571 call assert_equal([], g:options)
572 call assert_equal(g:opt[0], g:opt[1])
573
574 " 9: don't trigger OptionSet"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200575 let g:options=[['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200576 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200577 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200578 call assert_equal(g:opt[0], g:opt[1])
579
580 " 10: Setting global acd"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200581 let g:options=[['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200582 setlocal acd
583 call assert_equal([], g:options)
584 call assert_equal(g:opt[0], g:opt[1])
585
586 " 11: Setting global autoread (also sets local value)"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200587 let g:options=[['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200588 set ar
589 call assert_equal([], g:options)
590 call assert_equal(g:opt[0], g:opt[1])
591
592 " 12: Setting local autoread"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200593 let g:options=[['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200594 setlocal ar
595 call assert_equal([], g:options)
596 call assert_equal(g:opt[0], g:opt[1])
597
598 " 13: Setting global autoread"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200599 let g:options=[['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200600 setglobal invar
601 call assert_equal([], g:options)
602 call assert_equal(g:opt[0], g:opt[1])
603
604 " 14: Setting option backspace through :let"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200605 let g:options=[['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200606 let &bs="eol,indent,start"
607 call assert_equal([], g:options)
608 call assert_equal(g:opt[0], g:opt[1])
609
610 " 15: Setting option backspace through setbufvar()"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200611 let g:options=[['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200612 " try twice, first time, shouldn't trigger because option name is invalid,
613 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200614 let bnum = bufnr('%')
615 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200616 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200617 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200618 call assert_equal([], g:options)
619 call assert_equal(g:opt[0], g:opt[1])
620
621 " 16: Setting number option using setwinvar"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200622 let g:options=[['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200623 call setwinvar(0, '&number', 1)
624 call assert_equal([], g:options)
625 call assert_equal(g:opt[0], g:opt[1])
626
627 " 17: Setting key option, shouldn't trigger"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200628 let g:options=[['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200629 setlocal key=blah
630 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200631 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200632 call assert_equal(g:opt[0], g:opt[1])
633
Bram Moolenaard7c96872019-06-15 17:12:48 +0200634
635 " 18a: Setting string global option"
636 let oldval = &backupext
637 let g:options=[['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
638 set backupext=foo
639 call assert_equal([], g:options)
640 call assert_equal(g:opt[0], g:opt[1])
641
642 " 18b: Resetting string global option"
643 let g:options=[['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
644 set backupext&
645 call assert_equal([], g:options)
646 call assert_equal(g:opt[0], g:opt[1])
647
648 " 18c: Setting global string global option"
649 let g:options=[['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
650 setglobal backupext=bar
651 call assert_equal([], g:options)
652 call assert_equal(g:opt[0], g:opt[1])
653
654 " 18d: Setting local string global option"
655 " As this is a global option this sets the global value even though
656 " :setlocal is used!
657 noa set backupext& " Reset global and local value (without triggering autocmd)
658 let g:options=[['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
659 setlocal backupext=baz
660 call assert_equal([], g:options)
661 call assert_equal(g:opt[0], g:opt[1])
662
663 " 18e: Setting again string global option"
664 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
665 noa setlocal backupext=ext_local " Sets the global(!) value!
666 let g:options=[['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
667 set backupext=fuu
668 call assert_equal([], g:options)
669 call assert_equal(g:opt[0], g:opt[1])
670
671
672 " 19a: Setting string local-global (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200673 let oldval = &tags
Bram Moolenaard7c96872019-06-15 17:12:48 +0200674 let g:options=[['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200675 set tags=tagpath
676 call assert_equal([], g:options)
677 call assert_equal(g:opt[0], g:opt[1])
678
Bram Moolenaard7c96872019-06-15 17:12:48 +0200679 " 19b: Resetting string local-global (to buffer) option"
680 let g:options=[['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200681 set tags&
682 call assert_equal([], g:options)
683 call assert_equal(g:opt[0], g:opt[1])
684
Bram Moolenaard7c96872019-06-15 17:12:48 +0200685 " 19c: Setting global string local-global (to buffer) option "
686 let g:options=[['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
687 setglobal tags=tagpath1
688 call assert_equal([], g:options)
689 call assert_equal(g:opt[0], g:opt[1])
690
691 " 19d: Setting local string local-global (to buffer) option"
692 let g:options=[['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
693 setlocal tags=tagpath2
694 call assert_equal([], g:options)
695 call assert_equal(g:opt[0], g:opt[1])
696
697 " 19e: Setting again string local-global (to buffer) option"
698 " Note: v:option_old is the old global value for local-global string options
699 " but the old local value for all other kinds of options.
700 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
701 noa setlocal tags=tag_local
702 let g:options=[['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
703 set tags=tagpath
704 call assert_equal([], g:options)
705 call assert_equal(g:opt[0], g:opt[1])
706
707 " 19f: Setting string local-global (to buffer) option to an empty string"
708 " Note: v:option_old is the old global value for local-global string options
709 " but the old local value for all other kinds of options.
710 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
711 noa setlocal tags= " empty string
712 let g:options=[['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
713 set tags=tagpath
714 call assert_equal([], g:options)
715 call assert_equal(g:opt[0], g:opt[1])
716
717
718 " 20a: Setting string local (to buffer) option"
719 let oldval = &spelllang
720 let g:options=[['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
721 set spelllang=elvish,klingon
722 call assert_equal([], g:options)
723 call assert_equal(g:opt[0], g:opt[1])
724
725 " 20b: Resetting string local (to buffer) option"
726 let g:options=[['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
727 set spelllang&
728 call assert_equal([], g:options)
729 call assert_equal(g:opt[0], g:opt[1])
730
731 " 20c: Setting global string local (to buffer) option"
732 let g:options=[['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
733 setglobal spelllang=elvish
734 call assert_equal([], g:options)
735 call assert_equal(g:opt[0], g:opt[1])
736
737 " 20d: Setting local string local (to buffer) option"
738 noa set spelllang& " Reset global and local value (without triggering autocmd)
739 let g:options=[['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
740 setlocal spelllang=klingon
741 call assert_equal([], g:options)
742 call assert_equal(g:opt[0], g:opt[1])
743
744 " 20e: Setting again string local (to buffer) option"
745 " Note: v:option_old is the old global value for local-global string options
746 " but the old local value for all other kinds of options.
747 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
748 noa setlocal spelllang=spelllocal
749 let g:options=[['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
750 set spelllang=foo
751 call assert_equal([], g:options)
752 call assert_equal(g:opt[0], g:opt[1])
753
754
755 " 21a: Setting string local-global (to window) option"
756 let oldval = &statusline
757 let g:options=[['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
758 set statusline=foo
759 call assert_equal([], g:options)
760 call assert_equal(g:opt[0], g:opt[1])
761
762 " 21b: Resetting string local-global (to window) option"
763 " Note: v:option_old is the old global value for local-global string options
764 " but the old local value for all other kinds of options.
765 let g:options=[['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
766 set statusline&
767 call assert_equal([], g:options)
768 call assert_equal(g:opt[0], g:opt[1])
769
770 " 21c: Setting global string local-global (to window) option"
771 let g:options=[['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
772 setglobal statusline=bar
773 call assert_equal([], g:options)
774 call assert_equal(g:opt[0], g:opt[1])
775
776 " 21d: Setting local string local-global (to window) option"
777 noa set statusline& " Reset global and local value (without triggering autocmd)
778 let g:options=[['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
779 setlocal statusline=baz
780 call assert_equal([], g:options)
781 call assert_equal(g:opt[0], g:opt[1])
782
783 " 21e: Setting again string local-global (to window) option"
784 " Note: v:option_old is the old global value for local-global string options
785 " but the old local value for all other kinds of options.
786 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
787 noa setlocal statusline=baz
788 let g:options=[['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
789 set statusline=foo
790 call assert_equal([], g:options)
791 call assert_equal(g:opt[0], g:opt[1])
792
793
794 " 22a: Setting string local (to window) option"
795 let oldval = &foldignore
796 let g:options=[['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
797 set foldignore=fo
798 call assert_equal([], g:options)
799 call assert_equal(g:opt[0], g:opt[1])
800
801 " 22b: Resetting string local (to window) option"
802 let g:options=[['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
803 set foldignore&
804 call assert_equal([], g:options)
805 call assert_equal(g:opt[0], g:opt[1])
806
807 " 22c: Setting global string local (to window) option"
808 let g:options=[['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
809 setglobal foldignore=bar
810 call assert_equal([], g:options)
811 call assert_equal(g:opt[0], g:opt[1])
812
813 " 22d: Setting local string local (to window) option"
814 noa set foldignore& " Reset global and local value (without triggering autocmd)
815 let g:options=[['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
816 setlocal foldignore=baz
817 call assert_equal([], g:options)
818 call assert_equal(g:opt[0], g:opt[1])
819
820 " 22e: Setting again string local (to window) option"
821 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
822 noa setlocal foldignore=loc
823 let g:options=[['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
824 set foldignore=fo
825 call assert_equal([], g:options)
826 call assert_equal(g:opt[0], g:opt[1])
827
828
829 " 23a: Setting global number local option"
830 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
831 noa setlocal cmdheight=1 " Sets the global(!) value!
832 let g:options=[['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
833 setglobal cmdheight=2
834 call assert_equal([], g:options)
835 call assert_equal(g:opt[0], g:opt[1])
836
837 " 23b: Setting local number global option"
838 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
839 noa setlocal cmdheight=1 " Sets the global(!) value!
840 let g:options=[['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
841 setlocal cmdheight=2
842 call assert_equal([], g:options)
843 call assert_equal(g:opt[0], g:opt[1])
844
845 " 23c: Setting again number global option"
846 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
847 noa setlocal cmdheight=1 " Sets the global(!) value!
848 let g:options=[['cmdheight', '1', '1', '1', '2', 'global', 'set']]
849 set cmdheight=2
850 call assert_equal([], g:options)
851 call assert_equal(g:opt[0], g:opt[1])
852
853 " 23d: Setting again number global option"
854 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
855 let g:options=[['cmdheight', '8', '8', '8', '2', 'global', 'set']]
856 set cmdheight=2
857 call assert_equal([], g:options)
858 call assert_equal(g:opt[0], g:opt[1])
859
860
861 " 24a: Setting global number global-local (to buffer) option"
862 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
863 noa setlocal undolevels=1
864 let g:options=[['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
865 setglobal undolevels=2
866 call assert_equal([], g:options)
867 call assert_equal(g:opt[0], g:opt[1])
868
869 " 24b: Setting local number global-local (to buffer) option"
870 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
871 noa setlocal undolevels=1
872 let g:options=[['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
873 setlocal undolevels=2
874 call assert_equal([], g:options)
875 call assert_equal(g:opt[0], g:opt[1])
876
877 " 24c: Setting again number global-local (to buffer) option"
878 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
879 noa setlocal undolevels=1
880 let g:options=[['undolevels', '1', '1', '8', '2', 'global', 'set']]
881 set undolevels=2
882 call assert_equal([], g:options)
883 call assert_equal(g:opt[0], g:opt[1])
884
885 " 24d: Setting again global number global-local (to buffer) option"
886 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
887 let g:options=[['undolevels', '8', '8', '8', '2', 'global', 'set']]
888 set undolevels=2
889 call assert_equal([], g:options)
890 call assert_equal(g:opt[0], g:opt[1])
891
892
893 " 25a: Setting global number local (to buffer) option"
894 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
895 noa setlocal wrapmargin=1
896 let g:options=[['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
897 setglobal wrapmargin=2
898 call assert_equal([], g:options)
899 call assert_equal(g:opt[0], g:opt[1])
900
901 " 25b: Setting local number local (to buffer) option"
902 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
903 noa setlocal wrapmargin=1
904 let g:options=[['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
905 setlocal wrapmargin=2
906 call assert_equal([], g:options)
907 call assert_equal(g:opt[0], g:opt[1])
908
909 " 25c: Setting again number local (to buffer) option"
910 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
911 noa setlocal wrapmargin=1
912 let g:options=[['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
913 set wrapmargin=2
914 call assert_equal([], g:options)
915 call assert_equal(g:opt[0], g:opt[1])
916
917 " 25d: Setting again global number local (to buffer) option"
918 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
919 let g:options=[['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
920 set wrapmargin=2
921 call assert_equal([], g:options)
922 call assert_equal(g:opt[0], g:opt[1])
923
924
925 " 26: Setting number global-local (to window) option.
926 " Such option does currently not exist.
927
928
929 " 27a: Setting global number local (to window) option"
930 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
931 noa setlocal foldcolumn=1
932 let g:options=[['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
933 setglobal foldcolumn=2
934 call assert_equal([], g:options)
935 call assert_equal(g:opt[0], g:opt[1])
936
937 " 27b: Setting local number local (to window) option"
938 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
939 noa setlocal foldcolumn=1
940 let g:options=[['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
941 setlocal foldcolumn=2
942 call assert_equal([], g:options)
943 call assert_equal(g:opt[0], g:opt[1])
944
945 " 27c: Setting again number local (to window) option"
946 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
947 noa setlocal foldcolumn=1
948 let g:options=[['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
949 set foldcolumn=2
950 call assert_equal([], g:options)
951 call assert_equal(g:opt[0], g:opt[1])
952
953 " 27d: Ssettin again global number local (to window) option"
954 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
955 let g:options=[['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
956 set foldcolumn=2
957 call assert_equal([], g:options)
958 call assert_equal(g:opt[0], g:opt[1])
959
960
961 " 28a: Setting global boolean global option"
962 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
963 noa setlocal wrapscan " Sets the global(!) value!
964 let g:options=[['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
965 setglobal nowrapscan
966 call assert_equal([], g:options)
967 call assert_equal(g:opt[0], g:opt[1])
968
969 " 28b: Setting local boolean global option"
970 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
971 noa setlocal wrapscan " Sets the global(!) value!
972 let g:options=[['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
973 setlocal nowrapscan
974 call assert_equal([], g:options)
975 call assert_equal(g:opt[0], g:opt[1])
976
977 " 28c: Setting again boolean global option"
978 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
979 noa setlocal wrapscan " Sets the global(!) value!
980 let g:options=[['wrapscan', '1', '1', '1', '0', 'global', 'set']]
981 set nowrapscan
982 call assert_equal([], g:options)
983 call assert_equal(g:opt[0], g:opt[1])
984
985 " 28d: Setting again global boolean global option"
986 noa set nowrapscan " Reset global and local value (without triggering autocmd)
987 let g:options=[['wrapscan', '0', '0', '0', '1', 'global', 'set']]
988 set wrapscan
989 call assert_equal([], g:options)
990 call assert_equal(g:opt[0], g:opt[1])
991
992
993 " 29a: Setting global boolean global-local (to buffer) option"
994 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
995 noa setlocal autoread
996 let g:options=[['autoread', '0', '', '0', '1', 'global', 'setglobal']]
997 setglobal autoread
998 call assert_equal([], g:options)
999 call assert_equal(g:opt[0], g:opt[1])
1000
1001 " 29b: Setting local boolean global-local (to buffer) option"
1002 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1003 noa setlocal autoread
1004 let g:options=[['autoread', '1', '1', '', '0', 'local', 'setlocal']]
1005 setlocal noautoread
1006 call assert_equal([], g:options)
1007 call assert_equal(g:opt[0], g:opt[1])
1008
1009 " 29c: Setting again boolean global-local (to buffer) option"
1010 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1011 noa setlocal autoread
1012 let g:options=[['autoread', '1', '1', '0', '1', 'global', 'set']]
1013 set autoread
1014 call assert_equal([], g:options)
1015 call assert_equal(g:opt[0], g:opt[1])
1016
1017 " 29d: Setting again global boolean global-local (to buffer) option"
1018 noa set noautoread " Reset global and local value (without triggering autocmd)
1019 let g:options=[['autoread', '0', '0', '0', '1', 'global', 'set']]
1020 set autoread
1021 call assert_equal([], g:options)
1022 call assert_equal(g:opt[0], g:opt[1])
1023
1024
1025 " 30a: Setting global boolean local (to buffer) option"
1026 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1027 noa setlocal cindent
1028 let g:options=[['cindent', '0', '', '0', '1', 'global', 'setglobal']]
1029 setglobal cindent
1030 call assert_equal([], g:options)
1031 call assert_equal(g:opt[0], g:opt[1])
1032
1033 " 30b: Setting local boolean local (to buffer) option"
1034 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1035 noa setlocal cindent
1036 let g:options=[['cindent', '1', '1', '', '0', 'local', 'setlocal']]
1037 setlocal nocindent
1038 call assert_equal([], g:options)
1039 call assert_equal(g:opt[0], g:opt[1])
1040
1041 " 30c: Setting again boolean local (to buffer) option"
1042 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1043 noa setlocal cindent
1044 let g:options=[['cindent', '1', '1', '0', '1', 'global', 'set']]
1045 set cindent
1046 call assert_equal([], g:options)
1047 call assert_equal(g:opt[0], g:opt[1])
1048
1049 " 30d: Setting again global boolean local (to buffer) option"
1050 noa set nocindent " Reset global and local value (without triggering autocmd)
1051 let g:options=[['cindent', '0', '0', '0', '1', 'global', 'set']]
1052 set cindent
1053 call assert_equal([], g:options)
1054 call assert_equal(g:opt[0], g:opt[1])
1055
1056
1057 " 31: Setting boolean global-local (to window) option
1058 " Currently no such option exists.
1059
1060
1061 " 32a: Setting global boolean local (to window) option"
1062 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1063 noa setlocal cursorcolumn
1064 let g:options=[['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
1065 setglobal cursorcolumn
1066 call assert_equal([], g:options)
1067 call assert_equal(g:opt[0], g:opt[1])
1068
1069 " 32b: Setting local boolean local (to window) option"
1070 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1071 noa setlocal cursorcolumn
1072 let g:options=[['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
1073 setlocal nocursorcolumn
1074 call assert_equal([], g:options)
1075 call assert_equal(g:opt[0], g:opt[1])
1076
1077 " 32c: Setting again boolean local (to window) option"
1078 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1079 noa setlocal cursorcolumn
1080 let g:options=[['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
1081 set cursorcolumn
1082 call assert_equal([], g:options)
1083 call assert_equal(g:opt[0], g:opt[1])
1084
1085 " 32d: Setting again global boolean local (to window) option"
1086 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
1087 let g:options=[['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
1088 set cursorcolumn
1089 call assert_equal([], g:options)
1090 call assert_equal(g:opt[0], g:opt[1])
1091
1092
1093 " 33: Test autocomands when an option value is converted internally.
1094 noa set backspace=1 " Reset global and local value (without triggering autocmd)
1095 let g:options=[['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
1096 set backspace=2
1097 call assert_equal([], g:options)
1098 call assert_equal(g:opt[0], g:opt[1])
1099
1100
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001101 " Cleanup
1102 au! OptionSet
Bram Moolenaard7c96872019-06-15 17:12:48 +02001103 for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp', 'backupext', 'tags', 'spelllang', 'statusline', 'foldignore', 'cmdheight', 'undolevels', 'wrapmargin', 'foldcolumn', 'wrapscan', 'autoread', 'cindent', 'cursorcolumn']
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001104 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001105 endfor
1106 call test_override('starting', 0)
1107 delfunc! AutoCommandOptionSet
1108endfunc
1109
1110func Test_OptionSet_diffmode()
1111 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001112 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001113 new
1114 au OptionSet diff :let &l:cul=v:option_new
1115
1116 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1117 call assert_equal(0, &l:cul)
1118 diffthis
1119 call assert_equal(1, &l:cul)
1120
1121 vnew
1122 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1123 call assert_equal(0, &l:cul)
1124 diffthis
1125 call assert_equal(1, &l:cul)
1126
1127 diffoff
1128 call assert_equal(0, &l:cul)
1129 call assert_equal(1, getwinvar(2, '&l:cul'))
1130 bw!
1131
1132 call assert_equal(1, &l:cul)
1133 diffoff!
1134 call assert_equal(0, &l:cul)
1135 call assert_equal(0, getwinvar(1, '&l:cul'))
1136 bw!
1137
1138 " Cleanup
1139 au! OptionSet
1140 call test_override('starting', 0)
1141endfunc
1142
1143func Test_OptionSet_diffmode_close()
1144 call test_override('starting', 1)
1145 " 19: Try to close the current window when entering diff mode
1146 " should not segfault
1147 new
1148 au OptionSet diff close
1149
1150 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1151 call assert_fails(':diffthis', 'E788')
1152 call assert_equal(1, &diff)
1153 vnew
1154 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1155 call assert_fails(':diffthis', 'E788')
1156 call assert_equal(1, &diff)
1157 bw!
1158 call assert_fails(':diffoff!', 'E788')
1159 bw!
1160
1161 " Cleanup
1162 au! OptionSet
1163 call test_override('starting', 0)
1164 "delfunc! AutoCommandOptionSet
1165endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001166
1167" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1168func Test_BufleaveWithDelete()
1169 new | edit Xfile1
1170
1171 augroup test_bufleavewithdelete
1172 autocmd!
1173 autocmd BufLeave Xfile1 bwipe Xfile2
1174 augroup END
1175
1176 call assert_fails('edit Xfile2', 'E143:')
1177 call assert_equal('Xfile1', bufname('%'))
1178
1179 autocmd! test_bufleavewithdelete BufLeave Xfile1
1180 augroup! test_bufleavewithdelete
1181
1182 new
1183 bwipe! Xfile1
1184endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001185
1186" Test for autocommand that changes the buffer list, when doing ":ball".
1187func Test_Acmd_BufAll()
1188 enew!
1189 %bwipe!
1190 call writefile(['Test file Xxx1'], 'Xxx1')
1191 call writefile(['Test file Xxx2'], 'Xxx2')
1192 call writefile(['Test file Xxx3'], 'Xxx3')
1193
1194 " Add three files to the buffer list
1195 split Xxx1
1196 close
1197 split Xxx2
1198 close
1199 split Xxx3
1200 close
1201
1202 " Wipe the buffer when the buffer is opened
1203 au BufReadPost Xxx2 bwipe
1204
1205 call append(0, 'Test file Xxx4')
1206 ball
1207
1208 call assert_equal(2, winnr('$'))
1209 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1210 wincmd t
1211
1212 au! BufReadPost
1213 %bwipe!
1214 call delete('Xxx1')
1215 call delete('Xxx2')
1216 call delete('Xxx3')
1217 enew! | only
1218endfunc
1219
1220" Test for autocommand that changes current buffer on BufEnter event.
1221" Check if modelines are interpreted for the correct buffer.
1222func Test_Acmd_BufEnter()
1223 %bwipe!
1224 call writefile(['start of test file Xxx1',
1225 \ "\<Tab>this is a test",
1226 \ 'end of test file Xxx1'], 'Xxx1')
1227 call writefile(['start of test file Xxx2',
1228 \ 'vim: set noai :',
1229 \ "\<Tab>this is a test",
1230 \ 'end of test file Xxx2'], 'Xxx2')
1231
1232 au BufEnter Xxx2 brew
1233 set ai modeline modelines=3
1234 edit Xxx1
1235 " edit Xxx2, autocmd will do :brew
1236 edit Xxx2
1237 exe "normal G?this is a\<CR>"
1238 " Append text with autoindent to this file
1239 normal othis should be auto-indented
1240 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1241 call assert_equal(3, line('.'))
1242 " Remove autocmd and edit Xxx2 again
1243 au! BufEnter Xxx2
1244 buf! Xxx2
1245 exe "normal G?this is a\<CR>"
1246 " append text without autoindent to Xxx
1247 normal othis should be in column 1
1248 call assert_equal("this should be in column 1", getline('.'))
1249 call assert_equal(4, line('.'))
1250
1251 %bwipe!
1252 call delete('Xxx1')
1253 call delete('Xxx2')
1254 set ai&vim modeline&vim modelines&vim
1255endfunc
1256
1257" Test for issue #57
1258" do not move cursor on <c-o> when autoindent is set
1259func Test_ai_CTRL_O()
1260 enew!
1261 set ai
1262 let save_fo = &fo
1263 set fo+=r
1264 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1265 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1266 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1267
1268 set ai&vim
1269 let &fo = save_fo
1270 enew!
1271endfunc
1272
1273" Test for autocommand that deletes the current buffer on BufLeave event.
1274" Also test deleting the last buffer, should give a new, empty buffer.
1275func Test_BufLeave_Wipe()
1276 %bwipe!
1277 let content = ['start of test file Xxx',
1278 \ 'this is a test',
1279 \ 'end of test file Xxx']
1280 call writefile(content, 'Xxx1')
1281 call writefile(content, 'Xxx2')
1282
1283 au BufLeave Xxx2 bwipe
1284 edit Xxx1
1285 split Xxx2
1286 " delete buffer Xxx2, we should be back to Xxx1
1287 bwipe
1288 call assert_equal('Xxx1', bufname('%'))
1289 call assert_equal(1, winnr('$'))
1290
1291 " Create an alternate buffer
1292 %write! test.out
1293 call assert_equal('test.out', bufname('#'))
1294 " delete alternate buffer
1295 bwipe test.out
1296 call assert_equal('Xxx1', bufname('%'))
1297 call assert_equal('', bufname('#'))
1298
1299 au BufLeave Xxx1 bwipe
1300 " delete current buffer, get an empty one
1301 bwipe!
1302 call assert_equal(1, line('$'))
1303 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001304 let g:bufinfo = getbufinfo()
1305 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001306
1307 call delete('Xxx1')
1308 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001309 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001310 %bwipe
1311 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001312
1313 " check that bufinfo doesn't contain a pointer to freed memory
1314 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001315endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001316
1317func Test_QuitPre()
1318 edit Xfoo
1319 let winid = win_getid(winnr())
1320 split Xbar
1321 au! QuitPre * let g:afile = expand('<afile>')
1322 " Close the other window, <afile> should be correct.
1323 exe win_id2win(winid) . 'q'
1324 call assert_equal('Xfoo', g:afile)
1325
1326 unlet g:afile
1327 bwipe Xfoo
1328 bwipe Xbar
1329endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001330
1331func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001332 au! CmdlineChanged : let g:text = getcmdline()
1333 let g:text = 0
1334 call feedkeys(":echom 'hello'\<CR>", 'xt')
1335 call assert_equal("echom 'hello'", g:text)
1336 au! CmdlineChanged
1337
1338 au! CmdlineChanged : let g:entered = expand('<afile>')
1339 let g:entered = 0
1340 call feedkeys(":echom 'hello'\<CR>", 'xt')
1341 call assert_equal(':', g:entered)
1342 au! CmdlineChanged
1343
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001344 au! CmdlineEnter : let g:entered = expand('<afile>')
1345 au! CmdlineLeave : let g:left = expand('<afile>')
1346 let g:entered = 0
1347 let g:left = 0
1348 call feedkeys(":echo 'hello'\<CR>", 'xt')
1349 call assert_equal(':', g:entered)
1350 call assert_equal(':', g:left)
1351 au! CmdlineEnter
1352 au! CmdlineLeave
1353
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001354 let save_shellslash = &shellslash
1355 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001356 au! CmdlineEnter / let g:entered = expand('<afile>')
1357 au! CmdlineLeave / let g:left = expand('<afile>')
1358 let g:entered = 0
1359 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001360 new
1361 call setline(1, 'hello')
1362 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001363 call assert_equal('/', g:entered)
1364 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001365 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001366 au! CmdlineEnter
1367 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001368 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001369endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001370
1371" Test for BufWritePre autocommand that deletes or unloads the buffer.
1372func Test_BufWritePre()
1373 %bwipe
1374 au BufWritePre Xxx1 bunload
1375 au BufWritePre Xxx2 bwipe
1376
1377 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1378 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1379
1380 edit Xtest
1381 e! Xxx2
1382 bdel Xtest
1383 e Xxx1
1384 " write it, will unload it and give an error msg
1385 call assert_fails('w', 'E203')
1386 call assert_equal('Xxx2', bufname('%'))
1387 edit Xtest
1388 e! Xxx2
1389 bwipe Xtest
1390 " write it, will delete the buffer and give an error msg
1391 call assert_fails('w', 'E203')
1392 call assert_equal('Xxx1', bufname('%'))
1393 au! BufWritePre
1394 call delete('Xxx1')
1395 call delete('Xxx2')
1396endfunc
1397
1398" Test for BufUnload autocommand that unloads all the other buffers
1399func Test_bufunload_all()
1400 call writefile(['Test file Xxx1'], 'Xxx1')"
1401 call writefile(['Test file Xxx2'], 'Xxx2')"
1402
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001403 let content =<< trim [CODE]
1404 func UnloadAllBufs()
1405 let i = 1
1406 while i <= bufnr('$')
1407 if i != bufnr('%') && bufloaded(i)
1408 exe i . 'bunload'
1409 endif
1410 let i += 1
1411 endwhile
1412 endfunc
1413 au BufUnload * call UnloadAllBufs()
1414 au VimLeave * call writefile(['Test Finished'], 'Xout')
1415 edit Xxx1
1416 split Xxx2
1417 q
1418 [CODE]
1419
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001420 call writefile(content, 'Xtest')
1421
1422 call delete('Xout')
1423 call system(v:progpath. ' --clean -N --not-a-term -S Xtest')
1424 call assert_true(filereadable('Xout'))
1425
1426 call delete('Xxx1')
1427 call delete('Xxx2')
1428 call delete('Xtest')
1429 call delete('Xout')
1430endfunc
1431
1432" Some tests for buffer-local autocommands
1433func Test_buflocal_autocmd()
1434 let g:bname = ''
1435 edit xx
1436 au BufLeave <buffer> let g:bname = expand("%")
1437 " here, autocommand for xx should trigger.
1438 " but autocommand shall not apply to buffer named <buffer>.
1439 edit somefile
1440 call assert_equal('xx', g:bname)
1441 let g:bname = ''
1442 " here, autocommand shall be auto-deleted
1443 bwipe xx
1444 " autocmd should not trigger
1445 edit xx
1446 call assert_equal('', g:bname)
1447 " autocmd should not trigger
1448 edit somefile
1449 call assert_equal('', g:bname)
1450 enew
1451 unlet g:bname
1452endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001453
1454" Test for "*Cmd" autocommands
1455func Test_Cmd_Autocmds()
1456 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1457
1458 enew!
1459 au BufReadCmd XtestA 0r Xxx|$del
1460 edit XtestA " will read text of Xxd instead
1461 call assert_equal('start of Xxx', getline(1))
1462
1463 au BufWriteCmd XtestA call append(line("$"), "write")
1464 write " will append a line to the file
1465 call assert_equal('write', getline('$'))
1466 call assert_fails('read XtestA', 'E484') " should not read anything
1467 call assert_equal('write', getline(4))
1468
1469 " now we have:
1470 " 1 start of Xxx
1471 " 2 abc2
1472 " 3 end of Xxx
1473 " 4 write
1474
1475 au FileReadCmd XtestB '[r Xxx
1476 2r XtestB " will read Xxx below line 2 instead
1477 call assert_equal('start of Xxx', getline(3))
1478
1479 " now we have:
1480 " 1 start of Xxx
1481 " 2 abc2
1482 " 3 start of Xxx
1483 " 4 abc2
1484 " 5 end of Xxx
1485 " 6 end of Xxx
1486 " 7 write
1487
1488 au FileWriteCmd XtestC '[,']copy $
1489 normal 4GA1
1490 4,5w XtestC " will copy lines 4 and 5 to the end
1491 call assert_equal("\tabc21", getline(8))
1492 call assert_fails('r XtestC', 'E484') " should not read anything
1493 call assert_equal("end of Xxx", getline(9))
1494
1495 " now we have:
1496 " 1 start of Xxx
1497 " 2 abc2
1498 " 3 start of Xxx
1499 " 4 abc21
1500 " 5 end of Xxx
1501 " 6 end of Xxx
1502 " 7 write
1503 " 8 abc21
1504 " 9 end of Xxx
1505
1506 let g:lines = []
1507 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1508 w >>XtestD " will add lines to 'lines'
1509 call assert_equal(9, len(g:lines))
1510 call assert_fails('$r XtestD', 'E484') " should not read anything
1511 call assert_equal(9, line('$'))
1512 call assert_equal('end of Xxx', getline('$'))
1513
1514 au BufReadCmd XtestE 0r Xxx|$del
1515 sp XtestE " split window with test.out
1516 call assert_equal('end of Xxx', getline(3))
1517
1518 let g:lines = []
1519 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1520 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1521 wall " will write other window to 'lines'
1522 call assert_equal(4, len(g:lines), g:lines)
1523 call assert_equal('asdf', g:lines[2])
1524
1525 au! BufReadCmd
1526 au! BufWriteCmd
1527 au! FileReadCmd
1528 au! FileWriteCmd
1529 au! FileAppendCmd
1530 %bwipe!
1531 call delete('Xxx')
1532 enew!
1533endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001534
1535func SetChangeMarks(start, end)
1536 exe a:start. 'mark ['
1537 exe a:end. 'mark ]'
1538endfunc
1539
1540" Verify the effects of autocmds on '[ and ']
1541func Test_change_mark_in_autocmds()
1542 edit! Xtest
1543 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u", 'xtn')
1544
1545 call SetChangeMarks(2, 3)
1546 write
1547 call assert_equal([1, 4], [line("'["), line("']")])
1548
1549 call SetChangeMarks(2, 3)
1550 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1551 write
1552 au! BufWritePre
1553
1554 if executable('cat')
1555 write XtestFilter
1556 write >> XtestFilter
1557
1558 call SetChangeMarks(2, 3)
1559 " Marks are set to the entire range of the write
1560 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1561 " '[ is adjusted to just before the line that will receive the filtered
1562 " data
1563 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1564 " The filtered data is read into the buffer, and the source lines are
1565 " still present, so the range is after the source lines
1566 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1567 %!cat XtestFilter
1568 " After the filtered data is read, the original lines are deleted
1569 call assert_equal([1, 8], [line("'["), line("']")])
1570 au! FilterWritePre,FilterReadPre,FilterReadPost
1571 undo
1572
1573 call SetChangeMarks(1, 4)
1574 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1575 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1576 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1577 2,3!cat XtestFilter
1578 call assert_equal([2, 9], [line("'["), line("']")])
1579 au! FilterWritePre,FilterReadPre,FilterReadPost
1580 undo
1581
1582 call delete('XtestFilter')
1583 endif
1584
1585 call SetChangeMarks(1, 4)
1586 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1587 2,3write Xtest2
1588 au! FileWritePre
1589
1590 call SetChangeMarks(2, 3)
1591 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1592 write >> Xtest2
1593 au! FileAppendPre
1594
1595 call SetChangeMarks(1, 4)
1596 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1597 2,3write >> Xtest2
1598 au! FileAppendPre
1599
1600 call SetChangeMarks(1, 1)
1601 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1602 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1603 3read Xtest2
1604 au! FileReadPre,FileReadPost
1605 undo
1606
1607 call SetChangeMarks(4, 4)
1608 " When the line is 0, it's adjusted to 1
1609 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1610 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1611 0read Xtest2
1612 au! FileReadPre,FileReadPost
1613 undo
1614
1615 call SetChangeMarks(4, 4)
1616 " When the line is 0, it's adjusted to 1
1617 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1618 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1619 1read Xtest2
1620 au! FileReadPre,FileReadPost
1621 undo
1622
1623 bwipe!
1624 call delete('Xtest')
1625 call delete('Xtest2')
1626endfunc
1627
1628func Test_Filter_noshelltemp()
1629 if !executable('cat')
1630 return
1631 endif
1632
1633 enew!
1634 call setline(1, ['a', 'b', 'c', 'd'])
1635
1636 let shelltemp = &shelltemp
1637 set shelltemp
1638
1639 let g:filter_au = 0
1640 au FilterWritePre * let g:filter_au += 1
1641 au FilterReadPre * let g:filter_au += 1
1642 au FilterReadPost * let g:filter_au += 1
1643 %!cat
1644 call assert_equal(3, g:filter_au)
1645
1646 if has('filterpipe')
1647 set noshelltemp
1648
1649 let g:filter_au = 0
1650 au FilterWritePre * let g:filter_au += 1
1651 au FilterReadPre * let g:filter_au += 1
1652 au FilterReadPost * let g:filter_au += 1
1653 %!cat
1654 call assert_equal(0, g:filter_au)
1655 endif
1656
1657 au! FilterWritePre,FilterReadPre,FilterReadPost
1658 let &shelltemp = shelltemp
1659 bwipe!
1660endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001661
1662func Test_TextYankPost()
1663 enew!
1664 call setline(1, ['foo'])
1665
1666 let g:event = []
1667 au TextYankPost * let g:event = copy(v:event)
1668
1669 call assert_equal({}, v:event)
1670 call assert_fails('let v:event = {}', 'E46:')
1671 call assert_fails('let v:event.mykey = 0', 'E742:')
1672
1673 norm "ayiw
1674 call assert_equal(
1675 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v'},
1676 \g:event)
1677 norm y_
1678 call assert_equal(
1679 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V'},
1680 \g:event)
1681 call feedkeys("\<C-V>y", 'x')
1682 call assert_equal(
1683 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161"},
1684 \g:event)
1685 norm "xciwbar
1686 call assert_equal(
1687 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v'},
1688 \g:event)
1689 norm "bdiw
1690 call assert_equal(
1691 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v'},
1692 \g:event)
1693
1694 call assert_equal({}, v:event)
1695
1696 au! TextYankPost
1697 unlet g:event
1698 bwipe!
1699endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001700
1701func Test_nocatch_wipe_all_buffers()
1702 " Real nasty autocommand: wipe all buffers on any event.
1703 au * * bwipe *
Bram Moolenaara997b452018-04-17 23:24:06 +02001704 " Get E93 first?
1705 " call assert_fails('next x', 'E93:')
1706 call assert_fails('next x', 'E517:')
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001707 bwipe
1708 au!
1709endfunc
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001710
1711func Test_nocatch_wipe_dummy_buffer()
1712 " Nasty autocommand: wipe buffer on any event.
1713 au * x bwipe
1714 call assert_fails('lv½ /x', 'E480')
1715 au!
1716endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001717
1718function s:Before_test_dirchanged()
1719 augroup test_dirchanged
1720 autocmd!
1721 augroup END
1722 let s:li = []
1723 let s:dir_this = getcwd()
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001724 let s:dir_foo = s:dir_this . '/foo'
1725 call mkdir(s:dir_foo)
1726 let s:dir_bar = s:dir_this . '/bar'
1727 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001728endfunc
1729
1730function s:After_test_dirchanged()
1731 exe 'cd' s:dir_this
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001732 call delete(s:dir_foo, 'd')
1733 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001734 augroup test_dirchanged
1735 autocmd!
1736 augroup END
1737endfunc
1738
1739function Test_dirchanged_global()
1740 call s:Before_test_dirchanged()
1741 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1742 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001743 exe 'cd' s:dir_foo
1744 call assert_equal(["cd:", s:dir_foo], s:li)
1745 exe 'cd' s:dir_foo
1746 call assert_equal(["cd:", s:dir_foo], s:li)
1747 exe 'lcd' s:dir_bar
1748 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001749 call s:After_test_dirchanged()
1750endfunc
1751
1752function Test_dirchanged_local()
1753 call s:Before_test_dirchanged()
1754 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1755 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001756 exe 'cd' s:dir_foo
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001757 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001758 exe 'lcd' s:dir_bar
1759 call assert_equal(["lcd:", s:dir_bar], s:li)
1760 exe 'lcd' s:dir_bar
1761 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001762 call s:After_test_dirchanged()
1763endfunc
1764
1765function Test_dirchanged_auto()
Bram Moolenaarec48a9c2018-02-03 20:11:40 +01001766 if !exists('+autochdir')
1767 return
1768 endif
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001769 call s:Before_test_dirchanged()
1770 call test_autochdir()
1771 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1772 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1773 set acd
1774 exe 'cd ..'
1775 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001776 exe 'edit ' . s:dir_foo . '/Xfile'
1777 call assert_equal(s:dir_foo, getcwd())
1778 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001779 set noacd
1780 bwipe!
1781 call s:After_test_dirchanged()
1782endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001783
1784" Test TextChangedI and TextChangedP
1785func Test_ChangedP()
1786 new
1787 call setline(1, ['foo', 'bar', 'foobar'])
1788 call test_override("char_avail", 1)
1789 set complete=. completeopt=menuone
1790
1791 func! TextChangedAutocmd(char)
1792 let g:autocmd .= a:char
1793 endfunc
1794
1795 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1796 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1797 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1798
1799 call cursor(3, 1)
1800 let g:autocmd = ''
1801 call feedkeys("o\<esc>", 'tnix')
1802 call assert_equal('I', g:autocmd)
1803
1804 let g:autocmd = ''
1805 call feedkeys("Sf", 'tnix')
1806 call assert_equal('II', g:autocmd)
1807
1808 let g:autocmd = ''
1809 call feedkeys("Sf\<C-N>", 'tnix')
1810 call assert_equal('IIP', g:autocmd)
1811
1812 let g:autocmd = ''
1813 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1814 call assert_equal('IIPP', g:autocmd)
1815
1816 let g:autocmd = ''
1817 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1818 call assert_equal('IIPPP', g:autocmd)
1819
1820 let g:autocmd = ''
1821 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1822 call assert_equal('IIPPPP', g:autocmd)
1823
1824 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1825 " TODO: how should it handle completeopt=noinsert,noselect?
1826
1827 " CleanUp
1828 call test_override("char_avail", 0)
1829 au! TextChanged
1830 au! TextChangedI
1831 au! TextChangedP
1832 delfu TextChangedAutocmd
1833 unlet! g:autocmd
1834 set complete&vim completeopt&vim
1835
1836 bw!
1837endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001838
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001839let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001840func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001841 if !g:setline_handled
1842 call setline(1, "(x)")
1843 let g:setline_handled = v:true
1844 endif
1845endfunc
1846
1847func Test_TextChangedI_with_setline()
1848 new
1849 call test_override('char_avail', 1)
1850 autocmd TextChangedI <buffer> call SetLineOne()
1851 call feedkeys("i(\<CR>\<Esc>", 'tx')
1852 call assert_equal('(', getline(1))
1853 call assert_equal('x)', getline(2))
1854 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001855 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001856 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001857
1858 call test_override('starting', 0)
1859 bwipe!
1860endfunc
1861
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001862func Test_Changed_FirstTime()
1863 if !has('terminal') || has('gui_running')
1864 return
1865 endif
1866 " Prepare file for TextChanged event.
1867 call writefile([''], 'Xchanged.txt')
1868 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1869 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001870 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001871 " In ConPTY, there is additional character which is drawn up to the width of
1872 " the screen.
1873 if has('conpty')
1874 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
1875 else
1876 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
1877 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001878 " It's only adding autocmd, so that no event occurs.
1879 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1880 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001881 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001882 call assert_equal([''], readfile('Xchanged.txt'))
1883
1884 " clean up
1885 call delete('Xchanged.txt')
1886 bwipe!
1887endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01001888
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001889func Test_autocmd_nested()
1890 let g:did_nested = 0
1891 augroup Testing
1892 au WinNew * edit somefile
1893 au BufNew * let g:did_nested = 1
1894 augroup END
1895 split
1896 call assert_equal(0, g:did_nested)
1897 close
1898 bwipe! somefile
1899
1900 " old nested argument still works
1901 augroup Testing
1902 au!
1903 au WinNew * nested edit somefile
1904 au BufNew * let g:did_nested = 1
1905 augroup END
1906 split
1907 call assert_equal(1, g:did_nested)
1908 close
1909 bwipe! somefile
1910
1911 " New ++nested argument works
1912 augroup Testing
1913 au!
1914 au WinNew * ++nested edit somefile
1915 au BufNew * let g:did_nested = 1
1916 augroup END
1917 split
1918 call assert_equal(1, g:did_nested)
1919 close
1920 bwipe! somefile
1921
1922 augroup Testing
1923 au!
1924 augroup END
1925
1926 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
1927 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
1928endfunc
1929
1930func Test_autocmd_once()
1931 " Without ++once WinNew triggers twice
1932 let g:did_split = 0
1933 augroup Testing
1934 au WinNew * let g:did_split += 1
1935 augroup END
1936 split
1937 split
1938 call assert_equal(2, g:did_split)
1939 call assert_true(exists('#WinNew'))
1940 close
1941 close
1942
1943 " With ++once WinNew triggers once
1944 let g:did_split = 0
1945 augroup Testing
1946 au!
1947 au WinNew * ++once let g:did_split += 1
1948 augroup END
1949 split
1950 split
1951 call assert_equal(1, g:did_split)
1952 call assert_false(exists('#WinNew'))
1953 close
1954 close
1955
1956 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
1957endfunc
1958
Bram Moolenaara68e5952019-04-25 22:22:01 +02001959func Test_autocmd_bufreadpre()
1960 new
1961 let b:bufreadpre = 1
1962 call append(0, range(100))
1963 w! XAutocmdBufReadPre.txt
1964 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
1965 norm! 50gg
1966 sp
1967 norm! 100gg
1968 wincmd p
1969 let g:wsv1 = winsaveview()
1970 wincmd p
1971 let g:wsv2 = winsaveview()
1972 " triggers BufReadPre, should not move the cursor in either window
1973 " The topline may change one line in a large window.
1974 edit
1975 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
1976 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
1977 call assert_equal(2, b:bufreadpre)
1978 wincmd p
1979 call assert_equal(g:wsv1.topline, winsaveview().topline)
1980 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
1981 call assert_equal(2, b:bufreadpre)
1982 " Now set the cursor position in an BufReadPre autocommand
1983 " (even though the position will be invalid, this should make Vim reset the
1984 " cursor position in the other window.
1985 wincmd p
1986 set cpo+=g
1987 " won't do anything, but try to set the cursor on an invalid lnum
1988 autocmd BufReadPre <buffer> :norm! 70gg
1989 " triggers BufReadPre, should not move the cursor in either window
1990 e
1991 call assert_equal(1, winsaveview().topline)
1992 call assert_equal(1, winsaveview().lnum)
1993 call assert_equal(3, b:bufreadpre)
1994 wincmd p
1995 call assert_equal(g:wsv1.topline, winsaveview().topline)
1996 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
1997 call assert_equal(3, b:bufreadpre)
1998 close
1999 close
2000 call delete('XAutocmdBufReadPre.txt')
2001 set cpo-=g
2002endfunc
2003
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002004" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002005
2006" Tests for the following autocommands:
2007" - FileWritePre writing a compressed file
2008" - FileReadPost reading a compressed file
2009" - BufNewFile reading a file template
2010" - BufReadPre decompressing the file to be read
2011" - FilterReadPre substituting characters in the temp file
2012" - FilterReadPost substituting characters after filtering
2013" - FileReadPre set options for decompression
2014" - FileReadPost decompress the file
2015func Test_ReadWrite_Autocmds()
2016 " Run this test only on Unix-like systems and if gzip is available
2017 if !has('unix') || !executable("gzip")
2018 return
2019 endif
2020
2021 " Make $GZIP empty, "-v" would cause trouble.
2022 let $GZIP = ""
2023
2024 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2025 " being modified outside of Vim (noticed on Solaris).
2026 au FileChangedShell * echo 'caught FileChangedShell'
2027
2028 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2029 augroup Test1
2030 au!
2031 au FileWritePre *.gz '[,']!gzip
2032 au FileWritePost *.gz undo
2033 au FileReadPost *.gz '[,']!gzip -d
2034 augroup END
2035
2036 new
2037 set bin
2038 call append(0, [
2039 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2040 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2041 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2042 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2043 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2044 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2045 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2046 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2047 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2048 \ ])
2049 1,9write! Xtestfile.gz
2050 enew! | close
2051
2052 new
2053 " Read and decompress the testfile
2054 0read Xtestfile.gz
2055 call assert_equal([
2056 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2057 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2058 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2059 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2060 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2061 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2062 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2063 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2064 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2065 \ ], getline(1, 9))
2066 enew! | close
2067
2068 augroup Test1
2069 au!
2070 augroup END
2071
2072 " Test for the FileAppendPre and FileAppendPost autocmds
2073 augroup Test2
2074 au!
2075 au BufNewFile *.c read Xtest.c
2076 au FileAppendPre *.out '[,']s/new/NEW/
2077 au FileAppendPost *.out !cat Xtest.c >> test.out
2078 augroup END
2079
2080 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2081 new foo.c " should load Xtest.c
2082 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2083 w! >> test.out " append it to the output file
2084
2085 let contents = readfile('test.out')
2086 call assert_equal(' * Here is a NEW .c file', contents[2])
2087 call assert_equal(' * Here is a new .c file', contents[5])
2088
2089 call delete('test.out')
2090 enew! | close
2091 augroup Test2
2092 au!
2093 augroup END
2094
2095 " Test for the BufReadPre and BufReadPost autocmds
2096 augroup Test3
2097 au!
2098 " setup autocommands to decompress before reading and re-compress
2099 " afterwards
2100 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2101 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2102 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2103 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2104 augroup END
2105
2106 e! Xtestfile.gz " Edit compressed file
2107 call assert_equal([
2108 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2109 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2110 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2111 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2112 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2113 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2114 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2115 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2116 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2117 \ ], getline(1, 9))
2118
2119 w! >> test.out " Append it to the output file
2120
2121 augroup Test3
2122 au!
2123 augroup END
2124
2125 " Test for the FilterReadPre and FilterReadPost autocmds.
2126 set shelltemp " need temp files here
2127 augroup Test4
2128 au!
2129 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2130 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2131 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2132 au FilterReadPost *.out '[,']s/x/X/g
2133 augroup END
2134
2135 e! test.out " Edit the output file
2136 1,$!cat
2137 call assert_equal([
2138 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2139 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2140 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2141 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2142 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2143 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2144 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2145 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2146 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2147 \ ], getline(1, 9))
2148 call assert_equal([
2149 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2150 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2151 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2152 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2153 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2154 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2155 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2156 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2157 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2158 \ ], readfile('test.out'))
2159
2160 augroup Test4
2161 au!
2162 augroup END
2163 set shelltemp&vim
2164
2165 " Test for the FileReadPre and FileReadPost autocmds.
2166 augroup Test5
2167 au!
2168 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2169 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2170 au FileReadPost *.gz '[,']s/l/L/
2171 augroup END
2172
2173 new
2174 0r Xtestfile.gz " Read compressed file
2175 call assert_equal([
2176 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2177 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2178 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2179 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2180 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2181 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2182 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2183 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2184 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2185 \ ], getline(1, 9))
2186 call assert_equal([
2187 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2188 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2189 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2190 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2191 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2192 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2193 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2194 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2195 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2196 \ ], readfile('Xtestfile.gz'))
2197
2198 augroup Test5
2199 au!
2200 augroup END
2201
2202 au! FileChangedShell
2203 call delete('Xtestfile.gz')
2204 call delete('Xtest.c')
2205 call delete('test.out')
2206endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002207
2208func Test_throw_in_BufWritePre()
2209 new
2210 call setline(1, ['one', 'two', 'three'])
2211 call assert_false(filereadable('Xthefile'))
2212 augroup throwing
2213 au BufWritePre X* throw 'do not write'
2214 augroup END
2215 try
2216 w Xthefile
2217 catch
2218 let caught = 1
2219 endtry
2220 call assert_equal(1, caught)
2221 call assert_false(filereadable('Xthefile'))
2222
2223 bwipe!
2224 au! throwing
2225endfunc