blob: 5bb601e2d7136fa8541e4daf41db76f4229fd460 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaar8c64a362018-03-23 22:39:31 +01005
Bram Moolenaar1e115362019-01-09 23:01:02 +01006func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +02007 for bnr in range(1, bufnr('$'))
8 if bufloaded(bnr) && bufnr('%') != bnr
9 execute 'bd! ' . bnr
10 endif
11 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020012endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020013
Bram Moolenaar14735512016-03-26 21:00:08 +010014func Test_vim_did_enter()
15 call assert_false(v:vim_did_enter)
16
17 " This script will never reach the main loop, can't check if v:vim_did_enter
18 " becomes one.
19endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020020
Bram Moolenaarc67e8922016-05-24 16:07:40 +020021if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020022
Bram Moolenaarc67e8922016-05-24 16:07:40 +020023 func ExitInsertMode(id)
24 call feedkeys("\<Esc>")
25 endfunc
26
27 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020028 " Need to move the cursor.
29 call feedkeys("ggG", "xt")
30
Bram Moolenaarc67e8922016-05-24 16:07:40 +020031 let g:triggered = 0
32 au CursorHoldI * let g:triggered += 1
33 set updatetime=20
34 call timer_start(100, 'ExitInsertMode')
35 call feedkeys('a', 'x!')
36 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010037 unlet g:triggered
38 au! CursorHoldI
39 set updatetime&
40 endfunc
41
42 func Test_cursorhold_insert_with_timer_interrupt()
43 if !has('job')
44 return
45 endif
46 " Need to move the cursor.
47 call feedkeys("ggG", "xt")
48
49 " Confirm the timer invoked in exit_cb of the job doesn't disturb
50 " CursorHoldI event.
51 let g:triggered = 0
52 au CursorHoldI * let g:triggered += 1
53 set updatetime=500
54 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar8d4ce562019-01-30 22:01:40 +010055 \ {'exit_cb': {-> timer_start(1000, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010056 call feedkeys('a', 'x!')
57 call assert_equal(1, g:triggered)
58 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020059 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020060 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020061 endfunc
62
63 func Test_cursorhold_insert_ctrl_x()
64 let g:triggered = 0
65 au CursorHoldI * let g:triggered += 1
66 set updatetime=20
67 call timer_start(100, 'ExitInsertMode')
68 " CursorHoldI does not trigger after CTRL-X
69 call feedkeys("a\<C-X>", 'x!')
70 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010071 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020072 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020073 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020074 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +020075
76 func Test_OptionSet_modeline()
77 call test_override('starting', 1)
78 au! OptionSet
79 augroup set_tabstop
80 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
81 augroup END
82 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline')
83 set modeline
84 let v:errmsg = ''
85 call assert_fails('split XoptionsetModeline', 'E12:')
86 call assert_equal(7, &ts)
87 call assert_equal('', v:errmsg)
88
89 augroup set_tabstop
90 au!
91 augroup END
92 bwipe!
93 set ts&
94 call delete('XoptionsetModeline')
95 call test_override('starting', 0)
96 endfunc
97
98endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +020099
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200100func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200101 augroup test_bufunload_group
102 autocmd!
103 autocmd BufUnload * call add(s:li, "bufunload")
104 autocmd BufDelete * call add(s:li, "bufdelete")
105 autocmd BufWipeout * call add(s:li, "bufwipeout")
106 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200107
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200108 let s:li=[]
109 new
110 setlocal bufhidden=
111 bunload
112 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200113
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200114 let s:li=[]
115 new
116 setlocal bufhidden=delete
117 bunload
118 call assert_equal(["bufunload", "bufdelete"], s:li)
119
120 let s:li=[]
121 new
122 setlocal bufhidden=unload
123 bwipeout
124 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
125
Bram Moolenaare99e8442016-07-26 20:43:40 +0200126 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200127 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200128endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200129
130" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200131func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200132 tabedit
133 tabfirst
134
135 augroup test_autocmd_bufunload_with_tabnext_group
136 autocmd!
137 autocmd BufUnload <buffer> tabnext
138 augroup END
139
140 quit
141 call assert_equal(2, tabpagenr('$'))
142
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200143 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200144 augroup! test_autocmd_bufunload_with_tabnext_group
145 tablast
146 quit
147endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200148
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200149func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200150 tabedit
151 augroup sample
152 autocmd!
153 autocmd BufWinLeave <buffer> tabfirst
154 augroup END
155 call setline(1, ['a', 'b', 'c'])
156 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200157 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200158endfunc
159
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200160" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200161func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200162 split aa.txt
163 let lastbuf = bufnr('$')
164
165 augroup test_autocmd_bufunload
166 autocmd!
167 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
168 augroup END
169
Bram Moolenaara997b452018-04-17 23:24:06 +0200170 " Todo: check for E937 generated first
171 " call assert_fails('edit bb.txt', 'E937:')
172 call assert_fails('edit bb.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200173
174 autocmd! test_autocmd_bufunload
175 augroup! test_autocmd_bufunload
176 bwipe! aa.txt
177 bwipe! bb.txt
178endfunc
179
180" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200181func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200182 setlocal buftype=nowrite
183 let lastbuf = bufnr('$')
184
185 augroup test_autocmd_bufunload
186 autocmd!
187 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
188 augroup END
189
190 normal! i1
191 call assert_fails('edit a.txt', 'E517:')
192 call feedkeys("\<CR>")
193
194 autocmd! test_autocmd_bufunload
195 augroup! test_autocmd_bufunload
196 bwipe! a.txt
197endfunc
198
Bram Moolenaarc917da42016-07-19 22:31:36 +0200199func Test_win_tab_autocmd()
200 let g:record = []
201
202 augroup testing
203 au WinNew * call add(g:record, 'WinNew')
204 au WinEnter * call add(g:record, 'WinEnter')
205 au WinLeave * call add(g:record, 'WinLeave')
206 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200207 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200208 au TabEnter * call add(g:record, 'TabEnter')
209 au TabLeave * call add(g:record, 'TabLeave')
210 augroup END
211
212 split
213 tabnew
214 close
215 close
216
217 call assert_equal([
218 \ 'WinLeave', 'WinNew', 'WinEnter',
219 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200220 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200221 \ 'WinLeave', 'WinEnter'
222 \ ], g:record)
223
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200224 let g:record = []
225 tabnew somefile
226 tabnext
227 bwipe somefile
228
229 call assert_equal([
230 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
231 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
232 \ 'TabClosed'
233 \ ], g:record)
234
Bram Moolenaarc917da42016-07-19 22:31:36 +0200235 augroup testing
236 au!
237 augroup END
238 unlet g:record
239endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200240
241func s:AddAnAutocmd()
242 augroup vimBarTest
243 au BufReadCmd * echo 'hello'
244 augroup END
245 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
246endfunc
247
248func Test_early_bar()
249 " test that a bar is recognized before the {event}
250 call s:AddAnAutocmd()
251 augroup vimBarTest | au! | augroup END
252 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
253
254 call s:AddAnAutocmd()
255 augroup vimBarTest| au!| augroup END
256 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
257
258 " test that a bar is recognized after the {event}
259 call s:AddAnAutocmd()
260 augroup vimBarTest| au!BufReadCmd| augroup END
261 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
262
263 " test that a bar is recognized after the {group}
264 call s:AddAnAutocmd()
265 au! vimBarTest|echo 'hello'
266 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
267endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200268
Bram Moolenaar5c809082016-09-01 16:21:48 +0200269func RemoveGroup()
270 autocmd! StartOK
271 augroup! StartOK
272endfunc
273
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200274func Test_augroup_warning()
275 augroup TheWarning
276 au VimEnter * echo 'entering'
277 augroup END
278 call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0)
279 redir => res
280 augroup! TheWarning
281 redir END
282 call assert_true(match(res, "W19:") >= 0)
283 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
284
285 " check "Another" does not take the pace of the deleted entry
286 augroup Another
287 augroup END
288 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200289 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200290
291 " no warning for postpone aucmd delete
292 augroup StartOK
293 au VimEnter * call RemoveGroup()
294 augroup END
295 call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0)
296 redir => res
297 doautocmd VimEnter
298 redir END
299 call assert_true(match(res, "W19:") < 0)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200300 au! VimEnter
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200301endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200302
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200303func Test_BufReadCmdHelp()
304 " This used to cause access to free memory
305 au BufReadCmd * e +h
306 help
307
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200308 au! BufReadCmd
309endfunc
310
311func Test_BufReadCmdHelpJump()
312 " This used to cause access to free memory
313 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200314 " } to fix highlighting
315 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200316
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200317 au! BufReadCmd
318endfunc
319
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200320func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200321 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200322 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200323 call assert_fails('augroup! x', 'E936:')
324 au VimEnter * echo
325 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200326 augroup! x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200327 call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0)
328 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200329endfunc
330
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200331" Tests for autocommands on :close command.
332" This used to be in test13.
333func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200334 " Clean up buffers, because in some cases this function fails.
335 call s:cleanup_buffers()
336
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200337 " Write three files and open them, each in a window.
338 " Then go to next window, with autocommand that deletes the previous one.
339 " Do this twice, writing the file.
340 e! Xtestje1
341 call setline(1, 'testje1')
342 w
343 sp Xtestje2
344 call setline(1, 'testje2')
345 w
346 sp Xtestje3
347 call setline(1, 'testje3')
348 w
349 wincmd w
350 au WinLeave Xtestje2 bwipe
351 wincmd w
352 call assert_equal('Xtestje1', expand('%'))
353
354 au WinLeave Xtestje1 bwipe Xtestje3
355 close
356 call assert_equal('Xtestje1', expand('%'))
357
358 " Test deleting the buffer on a Unload event. If this goes wrong there
359 " will be the ATTENTION prompt.
360 e Xtestje1
361 au!
362 au! BufUnload Xtestje1 bwipe
363 call assert_fails('e Xtestje3', 'E937:')
364 call assert_equal('Xtestje3', expand('%'))
365
366 e Xtestje2
367 sp Xtestje1
368 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200369 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200370
371 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
372 " there are ml_line errors and/or a Crash.
373 au!
374 only
375 e Xanother
376 e Xtestje1
377 bwipe Xtestje2
378 bwipe Xtestje3
379 au BufWipeout Xtestje1 buf Xtestje1
380 bwipe
381 call assert_equal('Xanother', expand('%'))
382
383 only
384 help
385 wincmd w
386 1quit
387 call assert_equal('Xanother', expand('%'))
388
389 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100390 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200391 call delete('Xtestje1')
392 call delete('Xtestje2')
393 call delete('Xtestje3')
394endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100395
396func Test_BufEnter()
397 au! BufEnter
398 au Bufenter * let val = val . '+'
399 let g:val = ''
400 split NewFile
401 call assert_equal('+', g:val)
402 bwipe!
403 call assert_equal('++', g:val)
404
405 " Also get BufEnter when editing a directory
406 call mkdir('Xdir')
407 split Xdir
408 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100409
410 " On MS-Windows we can't edit the directory, make sure we wipe the right
411 " buffer.
412 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100413
414 call delete('Xdir', 'd')
415 au! BufEnter
416endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100417
418" Closing a window might cause an endless loop
419" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200420func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200421 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100422 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200423 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100424 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100425 mksession!
426
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200427 let content =<< trim [CODE]
428 set nocp noswapfile
429 let v:swapchoice="e"
430 augroup test_autocmd_sessionload
431 autocmd!
432 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
433 augroup END
434
435 func WriteErrors()
436 call writefile([execute("messages")], "Xerrors")
437 endfunc
438 au VimLeave * call WriteErrors()
439 [CODE]
440
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100441 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100442 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
443 let errors = join(readfile('Xerrors'))
444 call assert_match('E814', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100445
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100446 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100447 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100448 call delete(file)
449 endfor
450endfunc
451
452" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200453func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100454 tabnew
455 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100456 mksession!
457
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200458 let content =<< trim [CODE]
459 set nocp noswapfile
460 function! DeleteInactiveBufs()
461 tabfirst
462 let tabblist = []
463 for i in range(1, tabpagenr(''$''))
464 call extend(tabblist, tabpagebuflist(i))
465 endfor
466 for b in range(1, bufnr(''$''))
467 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
468 exec ''bwipeout '' . b
469 endif
470 endfor
471 echomsg "SessionLoadPost DONE"
472 endfunction
473 au SessionLoadPost * call DeleteInactiveBufs()
474
475 func WriteErrors()
476 call writefile([execute("messages")], "Xerrors")
477 endfunc
478 au VimLeave * call WriteErrors()
479 [CODE]
480
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100481 call writefile(content, 'Xvimrc')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100482 call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq')
483 let errors = join(readfile('Xerrors'))
484 " This probably only ever matches on unix.
485 call assert_notmatch('Caught deadly signal SEGV', errors)
486 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100487
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100488 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100489 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100490 call delete(file)
491 endfor
492endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200493
494func Test_empty_doau()
495 doau \|
496endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200497
498func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200499 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200500 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200501 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
502 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 +0200503 let g:opt = [expected, actual]
504 "call assert_equal(expected, actual)
505endfunc
506
507func Test_OptionSet()
Bram Moolenaar26d98212019-01-27 22:32:55 +0100508 if !has("eval") || !exists("+autochdir")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200509 return
510 endif
511
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200512 badd test_autocmd.vim
513
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200514 call test_override('starting', 1)
515 set nocp
516 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
517
518 " 1: Setting number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200519 let g:options=[['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200520 set nu
521 call assert_equal([], g:options)
522 call assert_equal(g:opt[0], g:opt[1])
523
524 " 2: Setting local number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200525 let g:options=[['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200526 setlocal nonu
527 call assert_equal([], g:options)
528 call assert_equal(g:opt[0], g:opt[1])
529
530 " 3: Setting global number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200531 let g:options=[['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200532 setglobal nonu
533 call assert_equal([], g:options)
534 call assert_equal(g:opt[0], g:opt[1])
535
536 " 4: Setting local autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200537 let g:options=[['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200538 setlocal ai
539 call assert_equal([], g:options)
540 call assert_equal(g:opt[0], g:opt[1])
541
542 " 5: Setting global autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200543 let g:options=[['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200544 setglobal ai
545 call assert_equal([], g:options)
546 call assert_equal(g:opt[0], g:opt[1])
547
548 " 6: Setting global autoindent option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200549 let g:options=[['autoindent', 1, 1, 1, 0, 'global', 'set']]
550 set ai!
551 call assert_equal([], g:options)
552 call assert_equal(g:opt[0], g:opt[1])
553
554 " 6a: Setting global autoindent option"
555 let g:options=[['autoindent', 1, 1, 0, 0, 'global', 'set']]
556 noa setlocal ai
557 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200558 set ai!
559 call assert_equal([], g:options)
560 call assert_equal(g:opt[0], g:opt[1])
561
562 " Should not print anything, use :noa
563 " 7: don't trigger OptionSet"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200564 let g:options=[['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200565 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200566 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200567 call assert_equal(g:opt[0], g:opt[1])
568
569 " 8: Setting several global list and number option"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200570 let g:options=[['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200571 set list nu
572 call assert_equal([], g:options)
573 call assert_equal(g:opt[0], g:opt[1])
574
575 " 9: don't trigger OptionSet"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200576 let g:options=[['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200577 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200578 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 +0200579 call assert_equal(g:opt[0], g:opt[1])
580
581 " 10: Setting global acd"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200582 let g:options=[['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200583 setlocal acd
584 call assert_equal([], g:options)
585 call assert_equal(g:opt[0], g:opt[1])
586
587 " 11: Setting global autoread (also sets local value)"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200588 let g:options=[['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200589 set ar
590 call assert_equal([], g:options)
591 call assert_equal(g:opt[0], g:opt[1])
592
593 " 12: Setting local autoread"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200594 let g:options=[['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200595 setlocal ar
596 call assert_equal([], g:options)
597 call assert_equal(g:opt[0], g:opt[1])
598
599 " 13: Setting global autoread"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200600 let g:options=[['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200601 setglobal invar
602 call assert_equal([], g:options)
603 call assert_equal(g:opt[0], g:opt[1])
604
605 " 14: Setting option backspace through :let"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200606 let g:options=[['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200607 let &bs="eol,indent,start"
608 call assert_equal([], g:options)
609 call assert_equal(g:opt[0], g:opt[1])
610
611 " 15: Setting option backspace through setbufvar()"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200612 let g:options=[['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200613 " try twice, first time, shouldn't trigger because option name is invalid,
614 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200615 let bnum = bufnr('%')
616 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355")
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200617 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200618 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200619 call assert_equal([], g:options)
620 call assert_equal(g:opt[0], g:opt[1])
621
622 " 16: Setting number option using setwinvar"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200623 let g:options=[['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200624 call setwinvar(0, '&number', 1)
625 call assert_equal([], g:options)
626 call assert_equal(g:opt[0], g:opt[1])
627
628 " 17: Setting key option, shouldn't trigger"
Bram Moolenaard7c96872019-06-15 17:12:48 +0200629 let g:options=[['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200630 setlocal key=blah
631 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200632 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200633 call assert_equal(g:opt[0], g:opt[1])
634
Bram Moolenaard7c96872019-06-15 17:12:48 +0200635
636 " 18a: Setting string global option"
637 let oldval = &backupext
638 let g:options=[['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
639 set backupext=foo
640 call assert_equal([], g:options)
641 call assert_equal(g:opt[0], g:opt[1])
642
643 " 18b: Resetting string global option"
644 let g:options=[['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
645 set backupext&
646 call assert_equal([], g:options)
647 call assert_equal(g:opt[0], g:opt[1])
648
649 " 18c: Setting global string global option"
650 let g:options=[['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
651 setglobal backupext=bar
652 call assert_equal([], g:options)
653 call assert_equal(g:opt[0], g:opt[1])
654
655 " 18d: Setting local string global option"
656 " As this is a global option this sets the global value even though
657 " :setlocal is used!
658 noa set backupext& " Reset global and local value (without triggering autocmd)
659 let g:options=[['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
660 setlocal backupext=baz
661 call assert_equal([], g:options)
662 call assert_equal(g:opt[0], g:opt[1])
663
664 " 18e: Setting again string global option"
665 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
666 noa setlocal backupext=ext_local " Sets the global(!) value!
667 let g:options=[['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
668 set backupext=fuu
669 call assert_equal([], g:options)
670 call assert_equal(g:opt[0], g:opt[1])
671
672
673 " 19a: Setting string local-global (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200674 let oldval = &tags
Bram Moolenaard7c96872019-06-15 17:12:48 +0200675 let g:options=[['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200676 set tags=tagpath
677 call assert_equal([], g:options)
678 call assert_equal(g:opt[0], g:opt[1])
679
Bram Moolenaard7c96872019-06-15 17:12:48 +0200680 " 19b: Resetting string local-global (to buffer) option"
681 let g:options=[['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200682 set tags&
683 call assert_equal([], g:options)
684 call assert_equal(g:opt[0], g:opt[1])
685
Bram Moolenaard7c96872019-06-15 17:12:48 +0200686 " 19c: Setting global string local-global (to buffer) option "
687 let g:options=[['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
688 setglobal tags=tagpath1
689 call assert_equal([], g:options)
690 call assert_equal(g:opt[0], g:opt[1])
691
692 " 19d: Setting local string local-global (to buffer) option"
693 let g:options=[['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
694 setlocal tags=tagpath2
695 call assert_equal([], g:options)
696 call assert_equal(g:opt[0], g:opt[1])
697
698 " 19e: Setting again string local-global (to buffer) option"
699 " Note: v:option_old is the old global value for local-global string options
700 " but the old local value for all other kinds of options.
701 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
702 noa setlocal tags=tag_local
703 let g:options=[['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
704 set tags=tagpath
705 call assert_equal([], g:options)
706 call assert_equal(g:opt[0], g:opt[1])
707
708 " 19f: Setting string local-global (to buffer) option to an empty string"
709 " Note: v:option_old is the old global value for local-global string options
710 " but the old local value for all other kinds of options.
711 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
712 noa setlocal tags= " empty string
713 let g:options=[['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
714 set tags=tagpath
715 call assert_equal([], g:options)
716 call assert_equal(g:opt[0], g:opt[1])
717
718
719 " 20a: Setting string local (to buffer) option"
720 let oldval = &spelllang
721 let g:options=[['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
722 set spelllang=elvish,klingon
723 call assert_equal([], g:options)
724 call assert_equal(g:opt[0], g:opt[1])
725
726 " 20b: Resetting string local (to buffer) option"
727 let g:options=[['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
728 set spelllang&
729 call assert_equal([], g:options)
730 call assert_equal(g:opt[0], g:opt[1])
731
732 " 20c: Setting global string local (to buffer) option"
733 let g:options=[['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
734 setglobal spelllang=elvish
735 call assert_equal([], g:options)
736 call assert_equal(g:opt[0], g:opt[1])
737
738 " 20d: Setting local string local (to buffer) option"
739 noa set spelllang& " Reset global and local value (without triggering autocmd)
740 let g:options=[['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
741 setlocal spelllang=klingon
742 call assert_equal([], g:options)
743 call assert_equal(g:opt[0], g:opt[1])
744
745 " 20e: Setting again string local (to buffer) option"
746 " Note: v:option_old is the old global value for local-global string options
747 " but the old local value for all other kinds of options.
748 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
749 noa setlocal spelllang=spelllocal
750 let g:options=[['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
751 set spelllang=foo
752 call assert_equal([], g:options)
753 call assert_equal(g:opt[0], g:opt[1])
754
755
756 " 21a: Setting string local-global (to window) option"
757 let oldval = &statusline
758 let g:options=[['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
759 set statusline=foo
760 call assert_equal([], g:options)
761 call assert_equal(g:opt[0], g:opt[1])
762
763 " 21b: Resetting string local-global (to window) option"
764 " Note: v:option_old is the old global value for local-global string options
765 " but the old local value for all other kinds of options.
766 let g:options=[['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
767 set statusline&
768 call assert_equal([], g:options)
769 call assert_equal(g:opt[0], g:opt[1])
770
771 " 21c: Setting global string local-global (to window) option"
772 let g:options=[['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
773 setglobal statusline=bar
774 call assert_equal([], g:options)
775 call assert_equal(g:opt[0], g:opt[1])
776
777 " 21d: Setting local string local-global (to window) option"
778 noa set statusline& " Reset global and local value (without triggering autocmd)
779 let g:options=[['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
780 setlocal statusline=baz
781 call assert_equal([], g:options)
782 call assert_equal(g:opt[0], g:opt[1])
783
784 " 21e: Setting again string local-global (to window) option"
785 " Note: v:option_old is the old global value for local-global string options
786 " but the old local value for all other kinds of options.
787 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
788 noa setlocal statusline=baz
789 let g:options=[['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
790 set statusline=foo
791 call assert_equal([], g:options)
792 call assert_equal(g:opt[0], g:opt[1])
793
794
795 " 22a: Setting string local (to window) option"
796 let oldval = &foldignore
797 let g:options=[['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
798 set foldignore=fo
799 call assert_equal([], g:options)
800 call assert_equal(g:opt[0], g:opt[1])
801
802 " 22b: Resetting string local (to window) option"
803 let g:options=[['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
804 set foldignore&
805 call assert_equal([], g:options)
806 call assert_equal(g:opt[0], g:opt[1])
807
808 " 22c: Setting global string local (to window) option"
809 let g:options=[['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
810 setglobal foldignore=bar
811 call assert_equal([], g:options)
812 call assert_equal(g:opt[0], g:opt[1])
813
814 " 22d: Setting local string local (to window) option"
815 noa set foldignore& " Reset global and local value (without triggering autocmd)
816 let g:options=[['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
817 setlocal foldignore=baz
818 call assert_equal([], g:options)
819 call assert_equal(g:opt[0], g:opt[1])
820
821 " 22e: Setting again string local (to window) option"
822 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
823 noa setlocal foldignore=loc
824 let g:options=[['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
825 set foldignore=fo
826 call assert_equal([], g:options)
827 call assert_equal(g:opt[0], g:opt[1])
828
829
830 " 23a: Setting global number local option"
831 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
832 noa setlocal cmdheight=1 " Sets the global(!) value!
833 let g:options=[['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
834 setglobal cmdheight=2
835 call assert_equal([], g:options)
836 call assert_equal(g:opt[0], g:opt[1])
837
838 " 23b: Setting local number global option"
839 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
840 noa setlocal cmdheight=1 " Sets the global(!) value!
841 let g:options=[['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
842 setlocal cmdheight=2
843 call assert_equal([], g:options)
844 call assert_equal(g:opt[0], g:opt[1])
845
846 " 23c: Setting again number global option"
847 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
848 noa setlocal cmdheight=1 " Sets the global(!) value!
849 let g:options=[['cmdheight', '1', '1', '1', '2', 'global', 'set']]
850 set cmdheight=2
851 call assert_equal([], g:options)
852 call assert_equal(g:opt[0], g:opt[1])
853
854 " 23d: Setting again number global option"
855 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
856 let g:options=[['cmdheight', '8', '8', '8', '2', 'global', 'set']]
857 set cmdheight=2
858 call assert_equal([], g:options)
859 call assert_equal(g:opt[0], g:opt[1])
860
861
862 " 24a: Setting global number global-local (to buffer) option"
863 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
864 noa setlocal undolevels=1
865 let g:options=[['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
866 setglobal undolevels=2
867 call assert_equal([], g:options)
868 call assert_equal(g:opt[0], g:opt[1])
869
870 " 24b: Setting local number global-local (to buffer) option"
871 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
872 noa setlocal undolevels=1
873 let g:options=[['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
874 setlocal undolevels=2
875 call assert_equal([], g:options)
876 call assert_equal(g:opt[0], g:opt[1])
877
878 " 24c: Setting again number global-local (to buffer) option"
879 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
880 noa setlocal undolevels=1
881 let g:options=[['undolevels', '1', '1', '8', '2', 'global', 'set']]
882 set undolevels=2
883 call assert_equal([], g:options)
884 call assert_equal(g:opt[0], g:opt[1])
885
886 " 24d: Setting again global number global-local (to buffer) option"
887 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
888 let g:options=[['undolevels', '8', '8', '8', '2', 'global', 'set']]
889 set undolevels=2
890 call assert_equal([], g:options)
891 call assert_equal(g:opt[0], g:opt[1])
892
893
894 " 25a: Setting global number local (to buffer) option"
895 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
896 noa setlocal wrapmargin=1
897 let g:options=[['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
898 setglobal wrapmargin=2
899 call assert_equal([], g:options)
900 call assert_equal(g:opt[0], g:opt[1])
901
902 " 25b: Setting local number local (to buffer) option"
903 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
904 noa setlocal wrapmargin=1
905 let g:options=[['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
906 setlocal wrapmargin=2
907 call assert_equal([], g:options)
908 call assert_equal(g:opt[0], g:opt[1])
909
910 " 25c: Setting again number local (to buffer) option"
911 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
912 noa setlocal wrapmargin=1
913 let g:options=[['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
914 set wrapmargin=2
915 call assert_equal([], g:options)
916 call assert_equal(g:opt[0], g:opt[1])
917
918 " 25d: Setting again global number local (to buffer) option"
919 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
920 let g:options=[['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
921 set wrapmargin=2
922 call assert_equal([], g:options)
923 call assert_equal(g:opt[0], g:opt[1])
924
925
926 " 26: Setting number global-local (to window) option.
927 " Such option does currently not exist.
928
929
930 " 27a: Setting global number local (to window) option"
931 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
932 noa setlocal foldcolumn=1
933 let g:options=[['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
934 setglobal foldcolumn=2
935 call assert_equal([], g:options)
936 call assert_equal(g:opt[0], g:opt[1])
937
938 " 27b: Setting local number local (to window) option"
939 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
940 noa setlocal foldcolumn=1
941 let g:options=[['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
942 setlocal foldcolumn=2
943 call assert_equal([], g:options)
944 call assert_equal(g:opt[0], g:opt[1])
945
946 " 27c: Setting again number local (to window) option"
947 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
948 noa setlocal foldcolumn=1
949 let g:options=[['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
950 set foldcolumn=2
951 call assert_equal([], g:options)
952 call assert_equal(g:opt[0], g:opt[1])
953
954 " 27d: Ssettin again global number local (to window) option"
955 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
956 let g:options=[['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
957 set foldcolumn=2
958 call assert_equal([], g:options)
959 call assert_equal(g:opt[0], g:opt[1])
960
961
962 " 28a: Setting global boolean global option"
963 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
964 noa setlocal wrapscan " Sets the global(!) value!
965 let g:options=[['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
966 setglobal nowrapscan
967 call assert_equal([], g:options)
968 call assert_equal(g:opt[0], g:opt[1])
969
970 " 28b: Setting local boolean global option"
971 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
972 noa setlocal wrapscan " Sets the global(!) value!
973 let g:options=[['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
974 setlocal nowrapscan
975 call assert_equal([], g:options)
976 call assert_equal(g:opt[0], g:opt[1])
977
978 " 28c: Setting again boolean global option"
979 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
980 noa setlocal wrapscan " Sets the global(!) value!
981 let g:options=[['wrapscan', '1', '1', '1', '0', 'global', 'set']]
982 set nowrapscan
983 call assert_equal([], g:options)
984 call assert_equal(g:opt[0], g:opt[1])
985
986 " 28d: Setting again global boolean global option"
987 noa set nowrapscan " Reset global and local value (without triggering autocmd)
988 let g:options=[['wrapscan', '0', '0', '0', '1', 'global', 'set']]
989 set wrapscan
990 call assert_equal([], g:options)
991 call assert_equal(g:opt[0], g:opt[1])
992
993
994 " 29a: Setting global boolean global-local (to buffer) option"
995 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
996 noa setlocal autoread
997 let g:options=[['autoread', '0', '', '0', '1', 'global', 'setglobal']]
998 setglobal autoread
999 call assert_equal([], g:options)
1000 call assert_equal(g:opt[0], g:opt[1])
1001
1002 " 29b: Setting local boolean global-local (to buffer) option"
1003 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1004 noa setlocal autoread
1005 let g:options=[['autoread', '1', '1', '', '0', 'local', 'setlocal']]
1006 setlocal noautoread
1007 call assert_equal([], g:options)
1008 call assert_equal(g:opt[0], g:opt[1])
1009
1010 " 29c: Setting again boolean global-local (to buffer) option"
1011 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1012 noa setlocal autoread
1013 let g:options=[['autoread', '1', '1', '0', '1', 'global', 'set']]
1014 set autoread
1015 call assert_equal([], g:options)
1016 call assert_equal(g:opt[0], g:opt[1])
1017
1018 " 29d: Setting again global boolean global-local (to buffer) option"
1019 noa set noautoread " Reset global and local value (without triggering autocmd)
1020 let g:options=[['autoread', '0', '0', '0', '1', 'global', 'set']]
1021 set autoread
1022 call assert_equal([], g:options)
1023 call assert_equal(g:opt[0], g:opt[1])
1024
1025
1026 " 30a: Setting global boolean local (to buffer) option"
1027 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1028 noa setlocal cindent
1029 let g:options=[['cindent', '0', '', '0', '1', 'global', 'setglobal']]
1030 setglobal cindent
1031 call assert_equal([], g:options)
1032 call assert_equal(g:opt[0], g:opt[1])
1033
1034 " 30b: Setting local boolean local (to buffer) option"
1035 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1036 noa setlocal cindent
1037 let g:options=[['cindent', '1', '1', '', '0', 'local', 'setlocal']]
1038 setlocal nocindent
1039 call assert_equal([], g:options)
1040 call assert_equal(g:opt[0], g:opt[1])
1041
1042 " 30c: Setting again boolean local (to buffer) option"
1043 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1044 noa setlocal cindent
1045 let g:options=[['cindent', '1', '1', '0', '1', 'global', 'set']]
1046 set cindent
1047 call assert_equal([], g:options)
1048 call assert_equal(g:opt[0], g:opt[1])
1049
1050 " 30d: Setting again global boolean local (to buffer) option"
1051 noa set nocindent " Reset global and local value (without triggering autocmd)
1052 let g:options=[['cindent', '0', '0', '0', '1', 'global', 'set']]
1053 set cindent
1054 call assert_equal([], g:options)
1055 call assert_equal(g:opt[0], g:opt[1])
1056
1057
1058 " 31: Setting boolean global-local (to window) option
1059 " Currently no such option exists.
1060
1061
1062 " 32a: Setting global boolean local (to window) option"
1063 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1064 noa setlocal cursorcolumn
1065 let g:options=[['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
1066 setglobal cursorcolumn
1067 call assert_equal([], g:options)
1068 call assert_equal(g:opt[0], g:opt[1])
1069
1070 " 32b: Setting local boolean local (to window) option"
1071 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1072 noa setlocal cursorcolumn
1073 let g:options=[['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
1074 setlocal nocursorcolumn
1075 call assert_equal([], g:options)
1076 call assert_equal(g:opt[0], g:opt[1])
1077
1078 " 32c: Setting again boolean local (to window) option"
1079 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1080 noa setlocal cursorcolumn
1081 let g:options=[['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
1082 set cursorcolumn
1083 call assert_equal([], g:options)
1084 call assert_equal(g:opt[0], g:opt[1])
1085
1086 " 32d: Setting again global boolean local (to window) option"
1087 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
1088 let g:options=[['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
1089 set cursorcolumn
1090 call assert_equal([], g:options)
1091 call assert_equal(g:opt[0], g:opt[1])
1092
1093
1094 " 33: Test autocomands when an option value is converted internally.
1095 noa set backspace=1 " Reset global and local value (without triggering autocmd)
1096 let g:options=[['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
1097 set backspace=2
1098 call assert_equal([], g:options)
1099 call assert_equal(g:opt[0], g:opt[1])
1100
1101
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001102 " Cleanup
1103 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001104 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001105 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 +02001106 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001107 endfor
1108 call test_override('starting', 0)
1109 delfunc! AutoCommandOptionSet
1110endfunc
1111
1112func Test_OptionSet_diffmode()
1113 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001114 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001115 new
1116 au OptionSet diff :let &l:cul=v:option_new
1117
1118 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1119 call assert_equal(0, &l:cul)
1120 diffthis
1121 call assert_equal(1, &l:cul)
1122
1123 vnew
1124 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1125 call assert_equal(0, &l:cul)
1126 diffthis
1127 call assert_equal(1, &l:cul)
1128
1129 diffoff
1130 call assert_equal(0, &l:cul)
1131 call assert_equal(1, getwinvar(2, '&l:cul'))
1132 bw!
1133
1134 call assert_equal(1, &l:cul)
1135 diffoff!
1136 call assert_equal(0, &l:cul)
1137 call assert_equal(0, getwinvar(1, '&l:cul'))
1138 bw!
1139
1140 " Cleanup
1141 au! OptionSet
1142 call test_override('starting', 0)
1143endfunc
1144
1145func Test_OptionSet_diffmode_close()
1146 call test_override('starting', 1)
1147 " 19: Try to close the current window when entering diff mode
1148 " should not segfault
1149 new
1150 au OptionSet diff close
1151
1152 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1153 call assert_fails(':diffthis', 'E788')
1154 call assert_equal(1, &diff)
1155 vnew
1156 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1157 call assert_fails(':diffthis', 'E788')
1158 call assert_equal(1, &diff)
1159 bw!
1160 call assert_fails(':diffoff!', 'E788')
1161 bw!
1162
1163 " Cleanup
1164 au! OptionSet
1165 call test_override('starting', 0)
1166 "delfunc! AutoCommandOptionSet
1167endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001168
1169" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1170func Test_BufleaveWithDelete()
1171 new | edit Xfile1
1172
1173 augroup test_bufleavewithdelete
1174 autocmd!
1175 autocmd BufLeave Xfile1 bwipe Xfile2
1176 augroup END
1177
1178 call assert_fails('edit Xfile2', 'E143:')
1179 call assert_equal('Xfile1', bufname('%'))
1180
1181 autocmd! test_bufleavewithdelete BufLeave Xfile1
1182 augroup! test_bufleavewithdelete
1183
1184 new
1185 bwipe! Xfile1
1186endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001187
1188" Test for autocommand that changes the buffer list, when doing ":ball".
1189func Test_Acmd_BufAll()
1190 enew!
1191 %bwipe!
1192 call writefile(['Test file Xxx1'], 'Xxx1')
1193 call writefile(['Test file Xxx2'], 'Xxx2')
1194 call writefile(['Test file Xxx3'], 'Xxx3')
1195
1196 " Add three files to the buffer list
1197 split Xxx1
1198 close
1199 split Xxx2
1200 close
1201 split Xxx3
1202 close
1203
1204 " Wipe the buffer when the buffer is opened
1205 au BufReadPost Xxx2 bwipe
1206
1207 call append(0, 'Test file Xxx4')
1208 ball
1209
1210 call assert_equal(2, winnr('$'))
1211 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1212 wincmd t
1213
1214 au! BufReadPost
1215 %bwipe!
1216 call delete('Xxx1')
1217 call delete('Xxx2')
1218 call delete('Xxx3')
1219 enew! | only
1220endfunc
1221
1222" Test for autocommand that changes current buffer on BufEnter event.
1223" Check if modelines are interpreted for the correct buffer.
1224func Test_Acmd_BufEnter()
1225 %bwipe!
1226 call writefile(['start of test file Xxx1',
1227 \ "\<Tab>this is a test",
1228 \ 'end of test file Xxx1'], 'Xxx1')
1229 call writefile(['start of test file Xxx2',
1230 \ 'vim: set noai :',
1231 \ "\<Tab>this is a test",
1232 \ 'end of test file Xxx2'], 'Xxx2')
1233
1234 au BufEnter Xxx2 brew
1235 set ai modeline modelines=3
1236 edit Xxx1
1237 " edit Xxx2, autocmd will do :brew
1238 edit Xxx2
1239 exe "normal G?this is a\<CR>"
1240 " Append text with autoindent to this file
1241 normal othis should be auto-indented
1242 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1243 call assert_equal(3, line('.'))
1244 " Remove autocmd and edit Xxx2 again
1245 au! BufEnter Xxx2
1246 buf! Xxx2
1247 exe "normal G?this is a\<CR>"
1248 " append text without autoindent to Xxx
1249 normal othis should be in column 1
1250 call assert_equal("this should be in column 1", getline('.'))
1251 call assert_equal(4, line('.'))
1252
1253 %bwipe!
1254 call delete('Xxx1')
1255 call delete('Xxx2')
1256 set ai&vim modeline&vim modelines&vim
1257endfunc
1258
1259" Test for issue #57
1260" do not move cursor on <c-o> when autoindent is set
1261func Test_ai_CTRL_O()
1262 enew!
1263 set ai
1264 let save_fo = &fo
1265 set fo+=r
1266 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1267 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1268 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1269
1270 set ai&vim
1271 let &fo = save_fo
1272 enew!
1273endfunc
1274
1275" Test for autocommand that deletes the current buffer on BufLeave event.
1276" Also test deleting the last buffer, should give a new, empty buffer.
1277func Test_BufLeave_Wipe()
1278 %bwipe!
1279 let content = ['start of test file Xxx',
1280 \ 'this is a test',
1281 \ 'end of test file Xxx']
1282 call writefile(content, 'Xxx1')
1283 call writefile(content, 'Xxx2')
1284
1285 au BufLeave Xxx2 bwipe
1286 edit Xxx1
1287 split Xxx2
1288 " delete buffer Xxx2, we should be back to Xxx1
1289 bwipe
1290 call assert_equal('Xxx1', bufname('%'))
1291 call assert_equal(1, winnr('$'))
1292
1293 " Create an alternate buffer
1294 %write! test.out
1295 call assert_equal('test.out', bufname('#'))
1296 " delete alternate buffer
1297 bwipe test.out
1298 call assert_equal('Xxx1', bufname('%'))
1299 call assert_equal('', bufname('#'))
1300
1301 au BufLeave Xxx1 bwipe
1302 " delete current buffer, get an empty one
1303 bwipe!
1304 call assert_equal(1, line('$'))
1305 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001306 let g:bufinfo = getbufinfo()
1307 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001308
1309 call delete('Xxx1')
1310 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001311 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001312 %bwipe
1313 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001314
1315 " check that bufinfo doesn't contain a pointer to freed memory
1316 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001317endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001318
1319func Test_QuitPre()
1320 edit Xfoo
1321 let winid = win_getid(winnr())
1322 split Xbar
1323 au! QuitPre * let g:afile = expand('<afile>')
1324 " Close the other window, <afile> should be correct.
1325 exe win_id2win(winid) . 'q'
1326 call assert_equal('Xfoo', g:afile)
1327
1328 unlet g:afile
1329 bwipe Xfoo
1330 bwipe Xbar
1331endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001332
1333func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001334 au! CmdlineChanged : let g:text = getcmdline()
1335 let g:text = 0
1336 call feedkeys(":echom 'hello'\<CR>", 'xt')
1337 call assert_equal("echom 'hello'", g:text)
1338 au! CmdlineChanged
1339
1340 au! CmdlineChanged : let g:entered = expand('<afile>')
1341 let g:entered = 0
1342 call feedkeys(":echom 'hello'\<CR>", 'xt')
1343 call assert_equal(':', g:entered)
1344 au! CmdlineChanged
1345
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001346 au! CmdlineEnter : let g:entered = expand('<afile>')
1347 au! CmdlineLeave : let g:left = expand('<afile>')
1348 let g:entered = 0
1349 let g:left = 0
1350 call feedkeys(":echo 'hello'\<CR>", 'xt')
1351 call assert_equal(':', g:entered)
1352 call assert_equal(':', g:left)
1353 au! CmdlineEnter
1354 au! CmdlineLeave
1355
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001356 let save_shellslash = &shellslash
1357 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001358 au! CmdlineEnter / let g:entered = expand('<afile>')
1359 au! CmdlineLeave / let g:left = expand('<afile>')
1360 let g:entered = 0
1361 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001362 new
1363 call setline(1, 'hello')
1364 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001365 call assert_equal('/', g:entered)
1366 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001367 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001368 au! CmdlineEnter
1369 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001370 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001371endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001372
1373" Test for BufWritePre autocommand that deletes or unloads the buffer.
1374func Test_BufWritePre()
1375 %bwipe
1376 au BufWritePre Xxx1 bunload
1377 au BufWritePre Xxx2 bwipe
1378
1379 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1380 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1381
1382 edit Xtest
1383 e! Xxx2
1384 bdel Xtest
1385 e Xxx1
1386 " write it, will unload it and give an error msg
1387 call assert_fails('w', 'E203')
1388 call assert_equal('Xxx2', bufname('%'))
1389 edit Xtest
1390 e! Xxx2
1391 bwipe Xtest
1392 " write it, will delete the buffer and give an error msg
1393 call assert_fails('w', 'E203')
1394 call assert_equal('Xxx1', bufname('%'))
1395 au! BufWritePre
1396 call delete('Xxx1')
1397 call delete('Xxx2')
1398endfunc
1399
1400" Test for BufUnload autocommand that unloads all the other buffers
1401func Test_bufunload_all()
1402 call writefile(['Test file Xxx1'], 'Xxx1')"
1403 call writefile(['Test file Xxx2'], 'Xxx2')"
1404
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001405 let content =<< trim [CODE]
1406 func UnloadAllBufs()
1407 let i = 1
1408 while i <= bufnr('$')
1409 if i != bufnr('%') && bufloaded(i)
1410 exe i . 'bunload'
1411 endif
1412 let i += 1
1413 endwhile
1414 endfunc
1415 au BufUnload * call UnloadAllBufs()
1416 au VimLeave * call writefile(['Test Finished'], 'Xout')
1417 edit Xxx1
1418 split Xxx2
1419 q
1420 [CODE]
1421
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001422 call writefile(content, 'Xtest')
1423
1424 call delete('Xout')
1425 call system(v:progpath. ' --clean -N --not-a-term -S Xtest')
1426 call assert_true(filereadable('Xout'))
1427
1428 call delete('Xxx1')
1429 call delete('Xxx2')
1430 call delete('Xtest')
1431 call delete('Xout')
1432endfunc
1433
1434" Some tests for buffer-local autocommands
1435func Test_buflocal_autocmd()
1436 let g:bname = ''
1437 edit xx
1438 au BufLeave <buffer> let g:bname = expand("%")
1439 " here, autocommand for xx should trigger.
1440 " but autocommand shall not apply to buffer named <buffer>.
1441 edit somefile
1442 call assert_equal('xx', g:bname)
1443 let g:bname = ''
1444 " here, autocommand shall be auto-deleted
1445 bwipe xx
1446 " autocmd should not trigger
1447 edit xx
1448 call assert_equal('', g:bname)
1449 " autocmd should not trigger
1450 edit somefile
1451 call assert_equal('', g:bname)
1452 enew
1453 unlet g:bname
1454endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001455
1456" Test for "*Cmd" autocommands
1457func Test_Cmd_Autocmds()
1458 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1459
1460 enew!
1461 au BufReadCmd XtestA 0r Xxx|$del
1462 edit XtestA " will read text of Xxd instead
1463 call assert_equal('start of Xxx', getline(1))
1464
1465 au BufWriteCmd XtestA call append(line("$"), "write")
1466 write " will append a line to the file
1467 call assert_equal('write', getline('$'))
1468 call assert_fails('read XtestA', 'E484') " should not read anything
1469 call assert_equal('write', getline(4))
1470
1471 " now we have:
1472 " 1 start of Xxx
1473 " 2 abc2
1474 " 3 end of Xxx
1475 " 4 write
1476
1477 au FileReadCmd XtestB '[r Xxx
1478 2r XtestB " will read Xxx below line 2 instead
1479 call assert_equal('start of Xxx', getline(3))
1480
1481 " now we have:
1482 " 1 start of Xxx
1483 " 2 abc2
1484 " 3 start of Xxx
1485 " 4 abc2
1486 " 5 end of Xxx
1487 " 6 end of Xxx
1488 " 7 write
1489
1490 au FileWriteCmd XtestC '[,']copy $
1491 normal 4GA1
1492 4,5w XtestC " will copy lines 4 and 5 to the end
1493 call assert_equal("\tabc21", getline(8))
1494 call assert_fails('r XtestC', 'E484') " should not read anything
1495 call assert_equal("end of Xxx", getline(9))
1496
1497 " now we have:
1498 " 1 start of Xxx
1499 " 2 abc2
1500 " 3 start of Xxx
1501 " 4 abc21
1502 " 5 end of Xxx
1503 " 6 end of Xxx
1504 " 7 write
1505 " 8 abc21
1506 " 9 end of Xxx
1507
1508 let g:lines = []
1509 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1510 w >>XtestD " will add lines to 'lines'
1511 call assert_equal(9, len(g:lines))
1512 call assert_fails('$r XtestD', 'E484') " should not read anything
1513 call assert_equal(9, line('$'))
1514 call assert_equal('end of Xxx', getline('$'))
1515
1516 au BufReadCmd XtestE 0r Xxx|$del
1517 sp XtestE " split window with test.out
1518 call assert_equal('end of Xxx', getline(3))
1519
1520 let g:lines = []
1521 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1522 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1523 wall " will write other window to 'lines'
1524 call assert_equal(4, len(g:lines), g:lines)
1525 call assert_equal('asdf', g:lines[2])
1526
1527 au! BufReadCmd
1528 au! BufWriteCmd
1529 au! FileReadCmd
1530 au! FileWriteCmd
1531 au! FileAppendCmd
1532 %bwipe!
1533 call delete('Xxx')
1534 enew!
1535endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001536
1537func SetChangeMarks(start, end)
1538 exe a:start. 'mark ['
1539 exe a:end. 'mark ]'
1540endfunc
1541
1542" Verify the effects of autocmds on '[ and ']
1543func Test_change_mark_in_autocmds()
1544 edit! Xtest
1545 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u", 'xtn')
1546
1547 call SetChangeMarks(2, 3)
1548 write
1549 call assert_equal([1, 4], [line("'["), line("']")])
1550
1551 call SetChangeMarks(2, 3)
1552 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1553 write
1554 au! BufWritePre
1555
1556 if executable('cat')
1557 write XtestFilter
1558 write >> XtestFilter
1559
1560 call SetChangeMarks(2, 3)
1561 " Marks are set to the entire range of the write
1562 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1563 " '[ is adjusted to just before the line that will receive the filtered
1564 " data
1565 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1566 " The filtered data is read into the buffer, and the source lines are
1567 " still present, so the range is after the source lines
1568 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1569 %!cat XtestFilter
1570 " After the filtered data is read, the original lines are deleted
1571 call assert_equal([1, 8], [line("'["), line("']")])
1572 au! FilterWritePre,FilterReadPre,FilterReadPost
1573 undo
1574
1575 call SetChangeMarks(1, 4)
1576 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1577 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1578 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1579 2,3!cat XtestFilter
1580 call assert_equal([2, 9], [line("'["), line("']")])
1581 au! FilterWritePre,FilterReadPre,FilterReadPost
1582 undo
1583
1584 call delete('XtestFilter')
1585 endif
1586
1587 call SetChangeMarks(1, 4)
1588 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1589 2,3write Xtest2
1590 au! FileWritePre
1591
1592 call SetChangeMarks(2, 3)
1593 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1594 write >> Xtest2
1595 au! FileAppendPre
1596
1597 call SetChangeMarks(1, 4)
1598 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1599 2,3write >> Xtest2
1600 au! FileAppendPre
1601
1602 call SetChangeMarks(1, 1)
1603 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1604 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1605 3read Xtest2
1606 au! FileReadPre,FileReadPost
1607 undo
1608
1609 call SetChangeMarks(4, 4)
1610 " When the line is 0, it's adjusted to 1
1611 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1612 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1613 0read Xtest2
1614 au! FileReadPre,FileReadPost
1615 undo
1616
1617 call SetChangeMarks(4, 4)
1618 " When the line is 0, it's adjusted to 1
1619 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1620 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1621 1read Xtest2
1622 au! FileReadPre,FileReadPost
1623 undo
1624
1625 bwipe!
1626 call delete('Xtest')
1627 call delete('Xtest2')
1628endfunc
1629
1630func Test_Filter_noshelltemp()
1631 if !executable('cat')
1632 return
1633 endif
1634
1635 enew!
1636 call setline(1, ['a', 'b', 'c', 'd'])
1637
1638 let shelltemp = &shelltemp
1639 set shelltemp
1640
1641 let g:filter_au = 0
1642 au FilterWritePre * let g:filter_au += 1
1643 au FilterReadPre * let g:filter_au += 1
1644 au FilterReadPost * let g:filter_au += 1
1645 %!cat
1646 call assert_equal(3, g:filter_au)
1647
1648 if has('filterpipe')
1649 set noshelltemp
1650
1651 let g:filter_au = 0
1652 au FilterWritePre * let g:filter_au += 1
1653 au FilterReadPre * let g:filter_au += 1
1654 au FilterReadPost * let g:filter_au += 1
1655 %!cat
1656 call assert_equal(0, g:filter_au)
1657 endif
1658
1659 au! FilterWritePre,FilterReadPre,FilterReadPost
1660 let &shelltemp = shelltemp
1661 bwipe!
1662endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001663
1664func Test_TextYankPost()
1665 enew!
1666 call setline(1, ['foo'])
1667
1668 let g:event = []
1669 au TextYankPost * let g:event = copy(v:event)
1670
1671 call assert_equal({}, v:event)
1672 call assert_fails('let v:event = {}', 'E46:')
1673 call assert_fails('let v:event.mykey = 0', 'E742:')
1674
1675 norm "ayiw
1676 call assert_equal(
1677 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v'},
1678 \g:event)
1679 norm y_
1680 call assert_equal(
1681 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V'},
1682 \g:event)
1683 call feedkeys("\<C-V>y", 'x')
1684 call assert_equal(
1685 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161"},
1686 \g:event)
1687 norm "xciwbar
1688 call assert_equal(
1689 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v'},
1690 \g:event)
1691 norm "bdiw
1692 call assert_equal(
1693 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v'},
1694 \g:event)
1695
1696 call assert_equal({}, v:event)
1697
1698 au! TextYankPost
1699 unlet g:event
1700 bwipe!
1701endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001702
1703func Test_nocatch_wipe_all_buffers()
1704 " Real nasty autocommand: wipe all buffers on any event.
1705 au * * bwipe *
Bram Moolenaara997b452018-04-17 23:24:06 +02001706 " Get E93 first?
1707 " call assert_fails('next x', 'E93:')
1708 call assert_fails('next x', 'E517:')
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001709 bwipe
1710 au!
1711endfunc
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001712
1713func Test_nocatch_wipe_dummy_buffer()
1714 " Nasty autocommand: wipe buffer on any event.
1715 au * x bwipe
1716 call assert_fails('lv½ /x', 'E480')
1717 au!
1718endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001719
1720function s:Before_test_dirchanged()
1721 augroup test_dirchanged
1722 autocmd!
1723 augroup END
1724 let s:li = []
1725 let s:dir_this = getcwd()
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001726 let s:dir_foo = s:dir_this . '/foo'
1727 call mkdir(s:dir_foo)
1728 let s:dir_bar = s:dir_this . '/bar'
1729 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001730endfunc
1731
1732function s:After_test_dirchanged()
1733 exe 'cd' s:dir_this
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001734 call delete(s:dir_foo, 'd')
1735 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001736 augroup test_dirchanged
1737 autocmd!
1738 augroup END
1739endfunc
1740
1741function Test_dirchanged_global()
1742 call s:Before_test_dirchanged()
1743 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1744 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001745 exe 'cd' s:dir_foo
1746 call assert_equal(["cd:", s:dir_foo], s:li)
1747 exe 'cd' s:dir_foo
1748 call assert_equal(["cd:", s:dir_foo], s:li)
1749 exe 'lcd' s:dir_bar
1750 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001751 call s:After_test_dirchanged()
1752endfunc
1753
1754function Test_dirchanged_local()
1755 call s:Before_test_dirchanged()
1756 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1757 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001758 exe 'cd' s:dir_foo
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001759 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001760 exe 'lcd' s:dir_bar
1761 call assert_equal(["lcd:", s:dir_bar], s:li)
1762 exe 'lcd' s:dir_bar
1763 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001764 call s:After_test_dirchanged()
1765endfunc
1766
1767function Test_dirchanged_auto()
Bram Moolenaarec48a9c2018-02-03 20:11:40 +01001768 if !exists('+autochdir')
1769 return
1770 endif
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001771 call s:Before_test_dirchanged()
1772 call test_autochdir()
1773 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1774 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1775 set acd
1776 exe 'cd ..'
1777 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001778 exe 'edit ' . s:dir_foo . '/Xfile'
1779 call assert_equal(s:dir_foo, getcwd())
1780 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001781 set noacd
1782 bwipe!
1783 call s:After_test_dirchanged()
1784endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001785
1786" Test TextChangedI and TextChangedP
1787func Test_ChangedP()
1788 new
1789 call setline(1, ['foo', 'bar', 'foobar'])
1790 call test_override("char_avail", 1)
1791 set complete=. completeopt=menuone
1792
1793 func! TextChangedAutocmd(char)
1794 let g:autocmd .= a:char
1795 endfunc
1796
1797 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1798 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1799 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1800
1801 call cursor(3, 1)
1802 let g:autocmd = ''
1803 call feedkeys("o\<esc>", 'tnix')
1804 call assert_equal('I', g:autocmd)
1805
1806 let g:autocmd = ''
1807 call feedkeys("Sf", 'tnix')
1808 call assert_equal('II', g:autocmd)
1809
1810 let g:autocmd = ''
1811 call feedkeys("Sf\<C-N>", 'tnix')
1812 call assert_equal('IIP', g:autocmd)
1813
1814 let g:autocmd = ''
1815 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1816 call assert_equal('IIPP', g:autocmd)
1817
1818 let g:autocmd = ''
1819 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1820 call assert_equal('IIPPP', g:autocmd)
1821
1822 let g:autocmd = ''
1823 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1824 call assert_equal('IIPPPP', g:autocmd)
1825
1826 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1827 " TODO: how should it handle completeopt=noinsert,noselect?
1828
1829 " CleanUp
1830 call test_override("char_avail", 0)
1831 au! TextChanged
1832 au! TextChangedI
1833 au! TextChangedP
1834 delfu TextChangedAutocmd
1835 unlet! g:autocmd
1836 set complete&vim completeopt&vim
1837
1838 bw!
1839endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001840
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001841let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001842func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001843 if !g:setline_handled
1844 call setline(1, "(x)")
1845 let g:setline_handled = v:true
1846 endif
1847endfunc
1848
1849func Test_TextChangedI_with_setline()
1850 new
1851 call test_override('char_avail', 1)
1852 autocmd TextChangedI <buffer> call SetLineOne()
1853 call feedkeys("i(\<CR>\<Esc>", 'tx')
1854 call assert_equal('(', getline(1))
1855 call assert_equal('x)', getline(2))
1856 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001857 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001858 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001859
1860 call test_override('starting', 0)
1861 bwipe!
1862endfunc
1863
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001864func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001865 CheckFeature terminal
1866 CheckNotGui
1867
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001868 " Prepare file for TextChanged event.
1869 call writefile([''], 'Xchanged.txt')
1870 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1871 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001872 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001873 " In ConPTY, there is additional character which is drawn up to the width of
1874 " the screen.
1875 if has('conpty')
1876 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
1877 else
1878 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
1879 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001880 " It's only adding autocmd, so that no event occurs.
1881 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1882 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001883 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001884 call assert_equal([''], readfile('Xchanged.txt'))
1885
1886 " clean up
1887 call delete('Xchanged.txt')
1888 bwipe!
1889endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01001890
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02001891func Test_autocmd_nested()
1892 let g:did_nested = 0
1893 augroup Testing
1894 au WinNew * edit somefile
1895 au BufNew * let g:did_nested = 1
1896 augroup END
1897 split
1898 call assert_equal(0, g:did_nested)
1899 close
1900 bwipe! somefile
1901
1902 " old nested argument still works
1903 augroup Testing
1904 au!
1905 au WinNew * nested edit somefile
1906 au BufNew * let g:did_nested = 1
1907 augroup END
1908 split
1909 call assert_equal(1, g:did_nested)
1910 close
1911 bwipe! somefile
1912
1913 " New ++nested argument works
1914 augroup Testing
1915 au!
1916 au WinNew * ++nested edit somefile
1917 au BufNew * let g:did_nested = 1
1918 augroup END
1919 split
1920 call assert_equal(1, g:did_nested)
1921 close
1922 bwipe! somefile
1923
1924 augroup Testing
1925 au!
1926 augroup END
1927
1928 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
1929 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
1930endfunc
1931
1932func Test_autocmd_once()
1933 " Without ++once WinNew triggers twice
1934 let g:did_split = 0
1935 augroup Testing
1936 au WinNew * let g:did_split += 1
1937 augroup END
1938 split
1939 split
1940 call assert_equal(2, g:did_split)
1941 call assert_true(exists('#WinNew'))
1942 close
1943 close
1944
1945 " With ++once WinNew triggers once
1946 let g:did_split = 0
1947 augroup Testing
1948 au!
1949 au WinNew * ++once let g:did_split += 1
1950 augroup END
1951 split
1952 split
1953 call assert_equal(1, g:did_split)
1954 call assert_false(exists('#WinNew'))
1955 close
1956 close
1957
1958 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
1959endfunc
1960
Bram Moolenaara68e5952019-04-25 22:22:01 +02001961func Test_autocmd_bufreadpre()
1962 new
1963 let b:bufreadpre = 1
1964 call append(0, range(100))
1965 w! XAutocmdBufReadPre.txt
1966 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
1967 norm! 50gg
1968 sp
1969 norm! 100gg
1970 wincmd p
1971 let g:wsv1 = winsaveview()
1972 wincmd p
1973 let g:wsv2 = winsaveview()
1974 " triggers BufReadPre, should not move the cursor in either window
1975 " The topline may change one line in a large window.
1976 edit
1977 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
1978 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
1979 call assert_equal(2, b:bufreadpre)
1980 wincmd p
1981 call assert_equal(g:wsv1.topline, winsaveview().topline)
1982 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
1983 call assert_equal(2, b:bufreadpre)
1984 " Now set the cursor position in an BufReadPre autocommand
1985 " (even though the position will be invalid, this should make Vim reset the
1986 " cursor position in the other window.
1987 wincmd p
1988 set cpo+=g
1989 " won't do anything, but try to set the cursor on an invalid lnum
1990 autocmd BufReadPre <buffer> :norm! 70gg
1991 " triggers BufReadPre, should not move the cursor in either window
1992 e
1993 call assert_equal(1, winsaveview().topline)
1994 call assert_equal(1, winsaveview().lnum)
1995 call assert_equal(3, b:bufreadpre)
1996 wincmd p
1997 call assert_equal(g:wsv1.topline, winsaveview().topline)
1998 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
1999 call assert_equal(3, b:bufreadpre)
2000 close
2001 close
2002 call delete('XAutocmdBufReadPre.txt')
2003 set cpo-=g
2004endfunc
2005
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002006" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002007
2008" Tests for the following autocommands:
2009" - FileWritePre writing a compressed file
2010" - FileReadPost reading a compressed file
2011" - BufNewFile reading a file template
2012" - BufReadPre decompressing the file to be read
2013" - FilterReadPre substituting characters in the temp file
2014" - FilterReadPost substituting characters after filtering
2015" - FileReadPre set options for decompression
2016" - FileReadPost decompress the file
2017func Test_ReadWrite_Autocmds()
2018 " Run this test only on Unix-like systems and if gzip is available
2019 if !has('unix') || !executable("gzip")
2020 return
2021 endif
2022
2023 " Make $GZIP empty, "-v" would cause trouble.
2024 let $GZIP = ""
2025
2026 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2027 " being modified outside of Vim (noticed on Solaris).
2028 au FileChangedShell * echo 'caught FileChangedShell'
2029
2030 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2031 augroup Test1
2032 au!
2033 au FileWritePre *.gz '[,']!gzip
2034 au FileWritePost *.gz undo
2035 au FileReadPost *.gz '[,']!gzip -d
2036 augroup END
2037
2038 new
2039 set bin
2040 call append(0, [
2041 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2042 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2043 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2044 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2045 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2046 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2047 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2048 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2049 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2050 \ ])
2051 1,9write! Xtestfile.gz
2052 enew! | close
2053
2054 new
2055 " Read and decompress the testfile
2056 0read Xtestfile.gz
2057 call assert_equal([
2058 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2059 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2060 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2061 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2062 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2063 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2064 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2065 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2066 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2067 \ ], getline(1, 9))
2068 enew! | close
2069
2070 augroup Test1
2071 au!
2072 augroup END
2073
2074 " Test for the FileAppendPre and FileAppendPost autocmds
2075 augroup Test2
2076 au!
2077 au BufNewFile *.c read Xtest.c
2078 au FileAppendPre *.out '[,']s/new/NEW/
2079 au FileAppendPost *.out !cat Xtest.c >> test.out
2080 augroup END
2081
2082 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2083 new foo.c " should load Xtest.c
2084 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2085 w! >> test.out " append it to the output file
2086
2087 let contents = readfile('test.out')
2088 call assert_equal(' * Here is a NEW .c file', contents[2])
2089 call assert_equal(' * Here is a new .c file', contents[5])
2090
2091 call delete('test.out')
2092 enew! | close
2093 augroup Test2
2094 au!
2095 augroup END
2096
2097 " Test for the BufReadPre and BufReadPost autocmds
2098 augroup Test3
2099 au!
2100 " setup autocommands to decompress before reading and re-compress
2101 " afterwards
2102 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2103 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2104 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2105 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2106 augroup END
2107
2108 e! Xtestfile.gz " Edit compressed file
2109 call assert_equal([
2110 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2111 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2112 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2113 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2114 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2115 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2116 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2117 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2118 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2119 \ ], getline(1, 9))
2120
2121 w! >> test.out " Append it to the output file
2122
2123 augroup Test3
2124 au!
2125 augroup END
2126
2127 " Test for the FilterReadPre and FilterReadPost autocmds.
2128 set shelltemp " need temp files here
2129 augroup Test4
2130 au!
2131 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2132 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2133 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2134 au FilterReadPost *.out '[,']s/x/X/g
2135 augroup END
2136
2137 e! test.out " Edit the output file
2138 1,$!cat
2139 call assert_equal([
2140 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2141 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2142 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2143 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2144 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2145 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2146 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2147 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2148 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2149 \ ], getline(1, 9))
2150 call assert_equal([
2151 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2152 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2153 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2154 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2155 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2156 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2157 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2158 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2159 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2160 \ ], readfile('test.out'))
2161
2162 augroup Test4
2163 au!
2164 augroup END
2165 set shelltemp&vim
2166
2167 " Test for the FileReadPre and FileReadPost autocmds.
2168 augroup Test5
2169 au!
2170 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2171 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2172 au FileReadPost *.gz '[,']s/l/L/
2173 augroup END
2174
2175 new
2176 0r Xtestfile.gz " Read compressed file
2177 call assert_equal([
2178 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2179 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2180 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2181 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2182 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2183 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2184 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2185 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2186 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2187 \ ], getline(1, 9))
2188 call assert_equal([
2189 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2190 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2191 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2192 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2193 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2194 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2195 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2196 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2197 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2198 \ ], readfile('Xtestfile.gz'))
2199
2200 augroup Test5
2201 au!
2202 augroup END
2203
2204 au! FileChangedShell
2205 call delete('Xtestfile.gz')
2206 call delete('Xtest.c')
2207 call delete('test.out')
2208endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002209
2210func Test_throw_in_BufWritePre()
2211 new
2212 call setline(1, ['one', 'two', 'three'])
2213 call assert_false(filereadable('Xthefile'))
2214 augroup throwing
2215 au BufWritePre X* throw 'do not write'
2216 augroup END
2217 try
2218 w Xthefile
2219 catch
2220 let caught = 1
2221 endtry
2222 call assert_equal(1, caught)
2223 call assert_false(filereadable('Xthefile'))
2224
2225 bwipe!
2226 au! throwing
2227endfunc