blob: 9c996ec2f765466fe6137f7378dc9b530ac67c55 [file] [log] [blame]
Bram Moolenaar14735512016-03-26 21:00:08 +01001" Tests for autocommands
2
Bram Moolenaar8c64a362018-03-23 22:39:31 +01003source shared.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02005source term_util.vim
Bram Moolenaar8c64a362018-03-23 22:39:31 +01006
Bram Moolenaar1e115362019-01-09 23:01:02 +01007func s:cleanup_buffers() abort
Bram Moolenaarb3435b02016-09-29 20:54:59 +02008 for bnr in range(1, bufnr('$'))
9 if bufloaded(bnr) && bufnr('%') != bnr
10 execute 'bd! ' . bnr
11 endif
12 endfor
Bram Moolenaar04f62f82017-07-19 18:18:39 +020013endfunc
Bram Moolenaarb3435b02016-09-29 20:54:59 +020014
Bram Moolenaar14735512016-03-26 21:00:08 +010015func Test_vim_did_enter()
16 call assert_false(v:vim_did_enter)
17
18 " This script will never reach the main loop, can't check if v:vim_did_enter
19 " becomes one.
20endfunc
Bram Moolenaar40b1b542016-04-20 20:18:23 +020021
Bram Moolenaar75911162020-07-21 19:44:47 +020022" Test for the CursorHold autocmd
23func Test_CursorHold_autocmd()
24 CheckRunVimInTerminal
25 call writefile(['one', 'two', 'three'], 'Xfile')
26 let before =<< trim END
27 set updatetime=10
28 au CursorHold * call writefile([line('.')], 'Xoutput', 'a')
29 END
30 call writefile(before, 'Xinit')
31 let buf = RunVimInTerminal('-S Xinit Xfile', {})
Bram Moolenaar17f67542020-08-20 18:29:13 +020032 call term_sendkeys(buf, "G")
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020033 call term_wait(buf, 50)
Bram Moolenaar75911162020-07-21 19:44:47 +020034 call term_sendkeys(buf, "gg")
35 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020036 call WaitForAssert({-> assert_equal(['1'], readfile('Xoutput')[-1:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020037 call term_sendkeys(buf, "j")
38 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020039 call WaitForAssert({-> assert_equal(['1', '2'], readfile('Xoutput')[-2:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020040 call term_sendkeys(buf, "j")
41 call term_wait(buf)
Bram Moolenaar17f67542020-08-20 18:29:13 +020042 call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('Xoutput')[-3:-1])})
Bram Moolenaar75911162020-07-21 19:44:47 +020043 call StopVimInTerminal(buf)
44
Bram Moolenaar75911162020-07-21 19:44:47 +020045 call delete('Xinit')
46 call delete('Xoutput')
47 call delete('Xfile')
48endfunc
49
Bram Moolenaarc67e8922016-05-24 16:07:40 +020050if has('timers')
Bram Moolenaar97b00752019-05-12 13:07:14 +020051
Bram Moolenaarc67e8922016-05-24 16:07:40 +020052 func ExitInsertMode(id)
53 call feedkeys("\<Esc>")
54 endfunc
55
56 func Test_cursorhold_insert()
Bram Moolenaarf18c4db2016-09-08 22:10:06 +020057 " Need to move the cursor.
58 call feedkeys("ggG", "xt")
59
Bram Moolenaarc67e8922016-05-24 16:07:40 +020060 let g:triggered = 0
61 au CursorHoldI * let g:triggered += 1
62 set updatetime=20
63 call timer_start(100, 'ExitInsertMode')
64 call feedkeys('a', 'x!')
65 call assert_equal(1, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010066 unlet g:triggered
67 au! CursorHoldI
68 set updatetime&
69 endfunc
70
71 func Test_cursorhold_insert_with_timer_interrupt()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020072 CheckFeature job
Bram Moolenaar26d98212019-01-27 22:32:55 +010073 " Need to move the cursor.
74 call feedkeys("ggG", "xt")
75
76 " Confirm the timer invoked in exit_cb of the job doesn't disturb
77 " CursorHoldI event.
78 let g:triggered = 0
79 au CursorHoldI * let g:triggered += 1
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020080 set updatetime=100
Bram Moolenaar26d98212019-01-27 22:32:55 +010081 call job_start(has('win32') ? 'cmd /c echo:' : 'echo',
Bram Moolenaar62cd26a2020-10-11 20:08:44 +020082 \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}})
Bram Moolenaar26d98212019-01-27 22:32:55 +010083 call feedkeys('a', 'x!')
84 call assert_equal(1, g:triggered)
85 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020086 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +020087 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +020088 endfunc
89
90 func Test_cursorhold_insert_ctrl_x()
91 let g:triggered = 0
92 au CursorHoldI * let g:triggered += 1
93 set updatetime=20
94 call timer_start(100, 'ExitInsertMode')
95 " CursorHoldI does not trigger after CTRL-X
96 call feedkeys("a\<C-X>", 'x!')
97 call assert_equal(0, g:triggered)
Bram Moolenaar26d98212019-01-27 22:32:55 +010098 unlet g:triggered
Bram Moolenaare99e8442016-07-26 20:43:40 +020099 au! CursorHoldI
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200100 set updatetime&
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200101 endfunc
Bram Moolenaar97b00752019-05-12 13:07:14 +0200102
103 func Test_OptionSet_modeline()
104 call test_override('starting', 1)
105 au! OptionSet
106 augroup set_tabstop
107 au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")})
108 augroup END
109 call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline')
110 set modeline
111 let v:errmsg = ''
112 call assert_fails('split XoptionsetModeline', 'E12:')
113 call assert_equal(7, &ts)
114 call assert_equal('', v:errmsg)
115
116 augroup set_tabstop
117 au!
118 augroup END
119 bwipe!
120 set ts&
121 call delete('XoptionsetModeline')
122 call test_override('starting', 0)
123 endfunc
124
125endif "has('timers')
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200126
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200127func Test_bufunload()
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200128 augroup test_bufunload_group
129 autocmd!
130 autocmd BufUnload * call add(s:li, "bufunload")
131 autocmd BufDelete * call add(s:li, "bufdelete")
132 autocmd BufWipeout * call add(s:li, "bufwipeout")
133 augroup END
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200134
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100135 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200136 new
137 setlocal bufhidden=
138 bunload
139 call assert_equal(["bufunload", "bufdelete"], s:li)
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200140
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100141 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200142 new
143 setlocal bufhidden=delete
144 bunload
145 call assert_equal(["bufunload", "bufdelete"], s:li)
146
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100147 let s:li = []
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200148 new
149 setlocal bufhidden=unload
150 bwipeout
151 call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li)
152
Bram Moolenaare99e8442016-07-26 20:43:40 +0200153 au! test_bufunload_group
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200154 augroup! test_bufunload_group
Bram Moolenaar40b1b542016-04-20 20:18:23 +0200155endfunc
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200156
157" SEGV occurs in older versions. (At least 7.4.2005 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200158func Test_autocmd_bufunload_with_tabnext()
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200159 tabedit
160 tabfirst
161
162 augroup test_autocmd_bufunload_with_tabnext_group
163 autocmd!
164 autocmd BufUnload <buffer> tabnext
165 augroup END
166
167 quit
168 call assert_equal(2, tabpagenr('$'))
169
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200170 autocmd! test_autocmd_bufunload_with_tabnext_group
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200171 augroup! test_autocmd_bufunload_with_tabnext_group
172 tablast
173 quit
174endfunc
Bram Moolenaarc917da42016-07-19 22:31:36 +0200175
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200176func Test_autocmd_bufwinleave_with_tabfirst()
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200177 tabedit
178 augroup sample
179 autocmd!
180 autocmd BufWinLeave <buffer> tabfirst
181 augroup END
182 call setline(1, ['a', 'b', 'c'])
183 edit! a.txt
Bram Moolenaarf18c4db2016-09-08 22:10:06 +0200184 tabclose
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200185endfunc
186
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200187" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200188func Test_autocmd_bufunload_avoiding_SEGV_01()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200189 split aa.txt
190 let lastbuf = bufnr('$')
191
192 augroup test_autocmd_bufunload
193 autocmd!
194 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
195 augroup END
196
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100197 call assert_fails('edit bb.txt', 'E937:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200198
199 autocmd! test_autocmd_bufunload
200 augroup! test_autocmd_bufunload
201 bwipe! aa.txt
202 bwipe! bb.txt
203endfunc
204
205" SEGV occurs in older versions. (At least 7.4.2321 or older)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200206func Test_autocmd_bufunload_avoiding_SEGV_02()
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200207 setlocal buftype=nowrite
208 let lastbuf = bufnr('$')
209
210 augroup test_autocmd_bufunload
211 autocmd!
212 exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!'
213 augroup END
214
215 normal! i1
216 call assert_fails('edit a.txt', 'E517:')
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200217
218 autocmd! test_autocmd_bufunload
219 augroup! test_autocmd_bufunload
220 bwipe! a.txt
221endfunc
222
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100223func Test_autocmd_dummy_wipeout()
224 " prepare files
225 call writefile([''], 'Xdummywipetest1.txt')
226 call writefile([''], 'Xdummywipetest2.txt')
227 augroup test_bufunload_group
228 autocmd!
229 autocmd BufUnload * call add(s:li, "bufunload")
230 autocmd BufDelete * call add(s:li, "bufdelete")
231 autocmd BufWipeout * call add(s:li, "bufwipeout")
232 augroup END
233
234 let s:li = []
235 split Xdummywipetest1.txt
236 silent! vimgrep /notmatched/ Xdummywipetest*
237 call assert_equal(["bufunload", "bufwipeout"], s:li)
238
239 bwipeout
240 call delete('Xdummywipetest1.txt')
241 call delete('Xdummywipetest2.txt')
242 au! test_bufunload_group
243 augroup! test_bufunload_group
244endfunc
245
Bram Moolenaarc917da42016-07-19 22:31:36 +0200246func Test_win_tab_autocmd()
247 let g:record = []
248
249 augroup testing
250 au WinNew * call add(g:record, 'WinNew')
251 au WinEnter * call add(g:record, 'WinEnter')
252 au WinLeave * call add(g:record, 'WinLeave')
253 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200254 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200255 au TabEnter * call add(g:record, 'TabEnter')
256 au TabLeave * call add(g:record, 'TabLeave')
257 augroup END
258
259 split
260 tabnew
261 close
262 close
263
264 call assert_equal([
265 \ 'WinLeave', 'WinNew', 'WinEnter',
266 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200267 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200268 \ 'WinLeave', 'WinEnter'
269 \ ], g:record)
270
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200271 let g:record = []
272 tabnew somefile
273 tabnext
274 bwipe somefile
275
276 call assert_equal([
277 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
278 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
279 \ 'TabClosed'
280 \ ], g:record)
281
Bram Moolenaarc917da42016-07-19 22:31:36 +0200282 augroup testing
283 au!
284 augroup END
285 unlet g:record
286endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200287
288func s:AddAnAutocmd()
289 augroup vimBarTest
290 au BufReadCmd * echo 'hello'
291 augroup END
292 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
293endfunc
294
295func Test_early_bar()
296 " test that a bar is recognized before the {event}
297 call s:AddAnAutocmd()
298 augroup vimBarTest | au! | augroup END
299 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
300
301 call s:AddAnAutocmd()
302 augroup vimBarTest| au!| augroup END
303 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
304
305 " test that a bar is recognized after the {event}
306 call s:AddAnAutocmd()
307 augroup vimBarTest| au!BufReadCmd| augroup END
308 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
309
310 " test that a bar is recognized after the {group}
311 call s:AddAnAutocmd()
312 au! vimBarTest|echo 'hello'
313 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
314endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200315
Bram Moolenaar5c809082016-09-01 16:21:48 +0200316func RemoveGroup()
317 autocmd! StartOK
318 augroup! StartOK
319endfunc
320
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200321func Test_augroup_warning()
322 augroup TheWarning
323 au VimEnter * echo 'entering'
324 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100325 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200326 redir => res
327 augroup! TheWarning
328 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100329 call assert_match("W19:", res)
330 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200331
332 " check "Another" does not take the pace of the deleted entry
333 augroup Another
334 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100335 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200336 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200337
338 " no warning for postpone aucmd delete
339 augroup StartOK
340 au VimEnter * call RemoveGroup()
341 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100342 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200343 redir => res
344 doautocmd VimEnter
345 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100346 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200347 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200348
349 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200350endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200351
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200352func Test_BufReadCmdHelp()
353 " This used to cause access to free memory
354 au BufReadCmd * e +h
355 help
356
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200357 au! BufReadCmd
358endfunc
359
360func Test_BufReadCmdHelpJump()
361 " This used to cause access to free memory
362 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200363 " } to fix highlighting
364 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200365
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200366 au! BufReadCmd
367endfunc
368
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200369func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200370 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200371 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200372 call assert_fails('augroup! x', 'E936:')
373 au VimEnter * echo
374 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200375 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100376 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200377 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200378endfunc
379
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200380" Tests for autocommands on :close command.
381" This used to be in test13.
382func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200383 " Clean up buffers, because in some cases this function fails.
384 call s:cleanup_buffers()
385
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200386 " Write three files and open them, each in a window.
387 " Then go to next window, with autocommand that deletes the previous one.
388 " Do this twice, writing the file.
389 e! Xtestje1
390 call setline(1, 'testje1')
391 w
392 sp Xtestje2
393 call setline(1, 'testje2')
394 w
395 sp Xtestje3
396 call setline(1, 'testje3')
397 w
398 wincmd w
399 au WinLeave Xtestje2 bwipe
400 wincmd w
401 call assert_equal('Xtestje1', expand('%'))
402
403 au WinLeave Xtestje1 bwipe Xtestje3
404 close
405 call assert_equal('Xtestje1', expand('%'))
406
407 " Test deleting the buffer on a Unload event. If this goes wrong there
408 " will be the ATTENTION prompt.
409 e Xtestje1
410 au!
411 au! BufUnload Xtestje1 bwipe
412 call assert_fails('e Xtestje3', 'E937:')
413 call assert_equal('Xtestje3', expand('%'))
414
415 e Xtestje2
416 sp Xtestje1
417 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200418 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200419
420 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
421 " there are ml_line errors and/or a Crash.
422 au!
423 only
424 e Xanother
425 e Xtestje1
426 bwipe Xtestje2
427 bwipe Xtestje3
428 au BufWipeout Xtestje1 buf Xtestje1
429 bwipe
430 call assert_equal('Xanother', expand('%'))
431
432 only
433 help
434 wincmd w
435 1quit
436 call assert_equal('Xanother', expand('%'))
437
438 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100439 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200440 call delete('Xtestje1')
441 call delete('Xtestje2')
442 call delete('Xtestje3')
443endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100444
445func Test_BufEnter()
446 au! BufEnter
447 au Bufenter * let val = val . '+'
448 let g:val = ''
449 split NewFile
450 call assert_equal('+', g:val)
451 bwipe!
452 call assert_equal('++', g:val)
453
454 " Also get BufEnter when editing a directory
455 call mkdir('Xdir')
456 split Xdir
457 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100458
459 " On MS-Windows we can't edit the directory, make sure we wipe the right
460 " buffer.
461 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100462
463 call delete('Xdir', 'd')
464 au! BufEnter
465endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100466
467" Closing a window might cause an endless loop
468" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200469func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200470 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100471 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200472 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100473 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100474 mksession!
475
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200476 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200477 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200478 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100479 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200480 augroup test_autocmd_sessionload
481 autocmd!
482 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
483 augroup END
484
485 func WriteErrors()
486 call writefile([execute("messages")], "Xerrors")
487 endfunc
488 au VimLeave * call WriteErrors()
489 [CODE]
490
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100491 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200492 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100493 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200494 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100495
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100496 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100497 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100498 call delete(file)
499 endfor
500endfunc
501
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100502" Using :blast and :ball for many events caused a crash, because b_nwindows was
503" not incremented correctly.
504func Test_autocmd_blast_badd()
505 let content =<< trim [CODE]
506 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
507 edit foo1
508 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
509 edit foo2
510 call writefile(['OK'], 'Xerrors')
511 qall
512 [CODE]
513
514 call writefile(content, 'XblastBall')
515 call system(GetVimCommand() .. ' --clean -S XblastBall')
516 call assert_match('OK', readfile('Xerrors')->join())
517
518 call delete('XblastBall')
519 call delete('Xerrors')
520endfunc
521
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100522" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200523func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100524 tabnew
525 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100526 mksession!
527
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200528 let content =<< trim [CODE]
529 set nocp noswapfile
530 function! DeleteInactiveBufs()
531 tabfirst
532 let tabblist = []
533 for i in range(1, tabpagenr(''$''))
534 call extend(tabblist, tabpagebuflist(i))
535 endfor
536 for b in range(1, bufnr(''$''))
537 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
538 exec ''bwipeout '' . b
539 endif
540 endfor
541 echomsg "SessionLoadPost DONE"
542 endfunction
543 au SessionLoadPost * call DeleteInactiveBufs()
544
545 func WriteErrors()
546 call writefile([execute("messages")], "Xerrors")
547 endfunc
548 au VimLeave * call WriteErrors()
549 [CODE]
550
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100551 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200552 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100553 let errors = join(readfile('Xerrors'))
554 " This probably only ever matches on unix.
555 call assert_notmatch('Caught deadly signal SEGV', errors)
556 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100557
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100558 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100559 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100560 call delete(file)
561 endfor
562endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200563
564func Test_empty_doau()
565 doau \|
566endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200567
568func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200569 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200570 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200571 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
572 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 +0200573 let g:opt = [expected, actual]
574 "call assert_equal(expected, actual)
575endfunc
576
577func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200578 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200579
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200580 badd test_autocmd.vim
581
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200582 call test_override('starting', 1)
583 set nocp
584 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
585
586 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100587 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200588 set nu
589 call assert_equal([], g:options)
590 call assert_equal(g:opt[0], g:opt[1])
591
592 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100593 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200594 setlocal nonu
595 call assert_equal([], g:options)
596 call assert_equal(g:opt[0], g:opt[1])
597
598 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100599 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200600 setglobal nonu
601 call assert_equal([], g:options)
602 call assert_equal(g:opt[0], g:opt[1])
603
604 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100605 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200606 setlocal ai
607 call assert_equal([], g:options)
608 call assert_equal(g:opt[0], g:opt[1])
609
610 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100611 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200612 setglobal ai
613 call assert_equal([], g:options)
614 call assert_equal(g:opt[0], g:opt[1])
615
616 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100617 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200618 set ai!
619 call assert_equal([], g:options)
620 call assert_equal(g:opt[0], g:opt[1])
621
622 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100623 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200624 noa setlocal ai
625 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200626 set ai!
627 call assert_equal([], g:options)
628 call assert_equal(g:opt[0], g:opt[1])
629
630 " Should not print anything, use :noa
631 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100632 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200633 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200634 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200635 call assert_equal(g:opt[0], g:opt[1])
636
637 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100638 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200639 set list nu
640 call assert_equal([], g:options)
641 call assert_equal(g:opt[0], g:opt[1])
642
643 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100644 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200645 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200646 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 +0200647 call assert_equal(g:opt[0], g:opt[1])
648
649 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100650 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200651 setlocal acd
652 call assert_equal([], g:options)
653 call assert_equal(g:opt[0], g:opt[1])
654
655 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100656 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200657 set ar
658 call assert_equal([], g:options)
659 call assert_equal(g:opt[0], g:opt[1])
660
661 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100662 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200663 setlocal ar
664 call assert_equal([], g:options)
665 call assert_equal(g:opt[0], g:opt[1])
666
667 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100668 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200669 setglobal invar
670 call assert_equal([], g:options)
671 call assert_equal(g:opt[0], g:opt[1])
672
673 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100674 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
675 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200676 call assert_equal([], g:options)
677 call assert_equal(g:opt[0], g:opt[1])
678
679 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100680 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200681 " try twice, first time, shouldn't trigger because option name is invalid,
682 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200683 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200684 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200685 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200686 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200687 call assert_equal([], g:options)
688 call assert_equal(g:opt[0], g:opt[1])
689
690 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100691 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200692 call setwinvar(0, '&number', 1)
693 call assert_equal([], g:options)
694 call assert_equal(g:opt[0], g:opt[1])
695
696 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100697 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200698 setlocal key=blah
699 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200700 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200701 call assert_equal(g:opt[0], g:opt[1])
702
Bram Moolenaard7c96872019-06-15 17:12:48 +0200703
704 " 18a: Setting string global option"
705 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100706 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200707 set backupext=foo
708 call assert_equal([], g:options)
709 call assert_equal(g:opt[0], g:opt[1])
710
711 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100712 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200713 set backupext&
714 call assert_equal([], g:options)
715 call assert_equal(g:opt[0], g:opt[1])
716
717 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100718 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200719 setglobal backupext=bar
720 call assert_equal([], g:options)
721 call assert_equal(g:opt[0], g:opt[1])
722
723 " 18d: Setting local string global option"
724 " As this is a global option this sets the global value even though
725 " :setlocal is used!
726 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100727 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200728 setlocal backupext=baz
729 call assert_equal([], g:options)
730 call assert_equal(g:opt[0], g:opt[1])
731
732 " 18e: Setting again string global option"
733 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
734 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100735 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200736 set backupext=fuu
737 call assert_equal([], g:options)
738 call assert_equal(g:opt[0], g:opt[1])
739
740
741 " 19a: Setting string local-global (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200742 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100743 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200744 set tags=tagpath
745 call assert_equal([], g:options)
746 call assert_equal(g:opt[0], g:opt[1])
747
Bram Moolenaard7c96872019-06-15 17:12:48 +0200748 " 19b: Resetting string local-global (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100749 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200750 set tags&
751 call assert_equal([], g:options)
752 call assert_equal(g:opt[0], g:opt[1])
753
Bram Moolenaard7c96872019-06-15 17:12:48 +0200754 " 19c: Setting global string local-global (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100755 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200756 setglobal tags=tagpath1
757 call assert_equal([], g:options)
758 call assert_equal(g:opt[0], g:opt[1])
759
760 " 19d: Setting local string local-global (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100761 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200762 setlocal tags=tagpath2
763 call assert_equal([], g:options)
764 call assert_equal(g:opt[0], g:opt[1])
765
766 " 19e: Setting again string local-global (to buffer) option"
767 " Note: v:option_old is the old global value for local-global string options
768 " but the old local value for all other kinds of options.
769 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
770 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100771 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200772 set tags=tagpath
773 call assert_equal([], g:options)
774 call assert_equal(g:opt[0], g:opt[1])
775
776 " 19f: Setting string local-global (to buffer) option to an empty string"
777 " Note: v:option_old is the old global value for local-global string options
778 " but the old local value for all other kinds of options.
779 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
780 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100781 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200782 set tags=tagpath
783 call assert_equal([], g:options)
784 call assert_equal(g:opt[0], g:opt[1])
785
786
787 " 20a: Setting string local (to buffer) option"
788 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100789 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200790 set spelllang=elvish,klingon
791 call assert_equal([], g:options)
792 call assert_equal(g:opt[0], g:opt[1])
793
794 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100795 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200796 set spelllang&
797 call assert_equal([], g:options)
798 call assert_equal(g:opt[0], g:opt[1])
799
800 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100801 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200802 setglobal spelllang=elvish
803 call assert_equal([], g:options)
804 call assert_equal(g:opt[0], g:opt[1])
805
806 " 20d: Setting local string local (to buffer) option"
807 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100808 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200809 setlocal spelllang=klingon
810 call assert_equal([], g:options)
811 call assert_equal(g:opt[0], g:opt[1])
812
813 " 20e: Setting again string local (to buffer) option"
814 " Note: v:option_old is the old global value for local-global string options
815 " but the old local value for all other kinds of options.
816 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
817 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100818 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200819 set spelllang=foo
820 call assert_equal([], g:options)
821 call assert_equal(g:opt[0], g:opt[1])
822
823
824 " 21a: Setting string local-global (to window) option"
825 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100826 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200827 set statusline=foo
828 call assert_equal([], g:options)
829 call assert_equal(g:opt[0], g:opt[1])
830
831 " 21b: Resetting string local-global (to window) option"
832 " Note: v:option_old is the old global value for local-global string options
833 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100834 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200835 set statusline&
836 call assert_equal([], g:options)
837 call assert_equal(g:opt[0], g:opt[1])
838
839 " 21c: Setting global string local-global (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100840 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200841 setglobal statusline=bar
842 call assert_equal([], g:options)
843 call assert_equal(g:opt[0], g:opt[1])
844
845 " 21d: Setting local string local-global (to window) option"
846 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100847 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200848 setlocal statusline=baz
849 call assert_equal([], g:options)
850 call assert_equal(g:opt[0], g:opt[1])
851
852 " 21e: Setting again string local-global (to window) option"
853 " Note: v:option_old is the old global value for local-global string options
854 " but the old local value for all other kinds of options.
855 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
856 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100857 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200858 set statusline=foo
859 call assert_equal([], g:options)
860 call assert_equal(g:opt[0], g:opt[1])
861
862
863 " 22a: Setting string local (to window) option"
864 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100865 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200866 set foldignore=fo
867 call assert_equal([], g:options)
868 call assert_equal(g:opt[0], g:opt[1])
869
870 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100871 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200872 set foldignore&
873 call assert_equal([], g:options)
874 call assert_equal(g:opt[0], g:opt[1])
875
876 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100877 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200878 setglobal foldignore=bar
879 call assert_equal([], g:options)
880 call assert_equal(g:opt[0], g:opt[1])
881
882 " 22d: Setting local string local (to window) option"
883 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100884 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200885 setlocal foldignore=baz
886 call assert_equal([], g:options)
887 call assert_equal(g:opt[0], g:opt[1])
888
889 " 22e: Setting again string local (to window) option"
890 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
891 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100892 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200893 set foldignore=fo
894 call assert_equal([], g:options)
895 call assert_equal(g:opt[0], g:opt[1])
896
897
898 " 23a: Setting global number local option"
899 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
900 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100901 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200902 setglobal cmdheight=2
903 call assert_equal([], g:options)
904 call assert_equal(g:opt[0], g:opt[1])
905
906 " 23b: Setting local number global option"
907 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
908 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100909 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200910 setlocal cmdheight=2
911 call assert_equal([], g:options)
912 call assert_equal(g:opt[0], g:opt[1])
913
914 " 23c: Setting again number global option"
915 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
916 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100917 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200918 set cmdheight=2
919 call assert_equal([], g:options)
920 call assert_equal(g:opt[0], g:opt[1])
921
922 " 23d: Setting again number global option"
923 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100924 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200925 set cmdheight=2
926 call assert_equal([], g:options)
927 call assert_equal(g:opt[0], g:opt[1])
928
929
930 " 24a: Setting global number global-local (to buffer) option"
931 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
932 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100933 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200934 setglobal undolevels=2
935 call assert_equal([], g:options)
936 call assert_equal(g:opt[0], g:opt[1])
937
938 " 24b: Setting local number global-local (to buffer) option"
939 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
940 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100941 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200942 setlocal undolevels=2
943 call assert_equal([], g:options)
944 call assert_equal(g:opt[0], g:opt[1])
945
946 " 24c: Setting again number global-local (to buffer) option"
947 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
948 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100949 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200950 set undolevels=2
951 call assert_equal([], g:options)
952 call assert_equal(g:opt[0], g:opt[1])
953
954 " 24d: Setting again global number global-local (to buffer) option"
955 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100956 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200957 set undolevels=2
958 call assert_equal([], g:options)
959 call assert_equal(g:opt[0], g:opt[1])
960
961
962 " 25a: Setting global number local (to buffer) option"
963 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
964 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100965 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200966 setglobal wrapmargin=2
967 call assert_equal([], g:options)
968 call assert_equal(g:opt[0], g:opt[1])
969
970 " 25b: Setting local number local (to buffer) option"
971 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
972 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100973 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200974 setlocal wrapmargin=2
975 call assert_equal([], g:options)
976 call assert_equal(g:opt[0], g:opt[1])
977
978 " 25c: Setting again number local (to buffer) option"
979 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
980 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100981 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200982 set wrapmargin=2
983 call assert_equal([], g:options)
984 call assert_equal(g:opt[0], g:opt[1])
985
986 " 25d: Setting again global number local (to buffer) option"
987 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100988 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200989 set wrapmargin=2
990 call assert_equal([], g:options)
991 call assert_equal(g:opt[0], g:opt[1])
992
993
994 " 26: Setting number global-local (to window) option.
995 " Such option does currently not exist.
996
997
998 " 27a: Setting global number local (to window) option"
999 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1000 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001001 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001002 setglobal foldcolumn=2
1003 call assert_equal([], g:options)
1004 call assert_equal(g:opt[0], g:opt[1])
1005
1006 " 27b: Setting local number local (to window) option"
1007 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1008 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001009 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001010 setlocal foldcolumn=2
1011 call assert_equal([], g:options)
1012 call assert_equal(g:opt[0], g:opt[1])
1013
1014 " 27c: Setting again number local (to window) option"
1015 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1016 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001017 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001018 set foldcolumn=2
1019 call assert_equal([], g:options)
1020 call assert_equal(g:opt[0], g:opt[1])
1021
1022 " 27d: Ssettin again global number local (to window) option"
1023 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001024 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001025 set foldcolumn=2
1026 call assert_equal([], g:options)
1027 call assert_equal(g:opt[0], g:opt[1])
1028
1029
1030 " 28a: Setting global boolean global option"
1031 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1032 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001033 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001034 setglobal nowrapscan
1035 call assert_equal([], g:options)
1036 call assert_equal(g:opt[0], g:opt[1])
1037
1038 " 28b: Setting local boolean global option"
1039 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1040 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001041 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001042 setlocal nowrapscan
1043 call assert_equal([], g:options)
1044 call assert_equal(g:opt[0], g:opt[1])
1045
1046 " 28c: Setting again boolean global option"
1047 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1048 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001049 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001050 set nowrapscan
1051 call assert_equal([], g:options)
1052 call assert_equal(g:opt[0], g:opt[1])
1053
1054 " 28d: Setting again global boolean global option"
1055 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001056 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001057 set wrapscan
1058 call assert_equal([], g:options)
1059 call assert_equal(g:opt[0], g:opt[1])
1060
1061
1062 " 29a: Setting global boolean global-local (to buffer) option"
1063 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1064 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001065 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001066 setglobal autoread
1067 call assert_equal([], g:options)
1068 call assert_equal(g:opt[0], g:opt[1])
1069
1070 " 29b: Setting local boolean global-local (to buffer) option"
1071 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1072 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001073 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001074 setlocal noautoread
1075 call assert_equal([], g:options)
1076 call assert_equal(g:opt[0], g:opt[1])
1077
1078 " 29c: Setting again boolean global-local (to buffer) option"
1079 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1080 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001081 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001082 set autoread
1083 call assert_equal([], g:options)
1084 call assert_equal(g:opt[0], g:opt[1])
1085
1086 " 29d: Setting again global boolean global-local (to buffer) option"
1087 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001088 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001089 set autoread
1090 call assert_equal([], g:options)
1091 call assert_equal(g:opt[0], g:opt[1])
1092
1093
1094 " 30a: Setting global boolean local (to buffer) option"
1095 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1096 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001097 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001098 setglobal cindent
1099 call assert_equal([], g:options)
1100 call assert_equal(g:opt[0], g:opt[1])
1101
1102 " 30b: Setting local boolean local (to buffer) option"
1103 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1104 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001105 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001106 setlocal nocindent
1107 call assert_equal([], g:options)
1108 call assert_equal(g:opt[0], g:opt[1])
1109
1110 " 30c: Setting again boolean local (to buffer) option"
1111 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1112 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001113 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001114 set cindent
1115 call assert_equal([], g:options)
1116 call assert_equal(g:opt[0], g:opt[1])
1117
1118 " 30d: Setting again global boolean local (to buffer) option"
1119 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001120 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001121 set cindent
1122 call assert_equal([], g:options)
1123 call assert_equal(g:opt[0], g:opt[1])
1124
1125
1126 " 31: Setting boolean global-local (to window) option
1127 " Currently no such option exists.
1128
1129
1130 " 32a: Setting global boolean local (to window) option"
1131 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1132 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001133 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001134 setglobal cursorcolumn
1135 call assert_equal([], g:options)
1136 call assert_equal(g:opt[0], g:opt[1])
1137
1138 " 32b: Setting local boolean local (to window) option"
1139 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1140 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001141 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001142 setlocal nocursorcolumn
1143 call assert_equal([], g:options)
1144 call assert_equal(g:opt[0], g:opt[1])
1145
1146 " 32c: Setting again boolean local (to window) option"
1147 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1148 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001149 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001150 set cursorcolumn
1151 call assert_equal([], g:options)
1152 call assert_equal(g:opt[0], g:opt[1])
1153
1154 " 32d: Setting again global boolean local (to window) option"
1155 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001156 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001157 set cursorcolumn
1158 call assert_equal([], g:options)
1159 call assert_equal(g:opt[0], g:opt[1])
1160
1161
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001162 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001163 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001164 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001165 set backspace=2
1166 call assert_equal([], g:options)
1167 call assert_equal(g:opt[0], g:opt[1])
1168
1169
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001170 " Cleanup
1171 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001172 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001173 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 +02001174 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001175 endfor
1176 call test_override('starting', 0)
1177 delfunc! AutoCommandOptionSet
1178endfunc
1179
1180func Test_OptionSet_diffmode()
1181 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001182 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001183 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001184 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001185
1186 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1187 call assert_equal(0, &l:cul)
1188 diffthis
1189 call assert_equal(1, &l:cul)
1190
1191 vnew
1192 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1193 call assert_equal(0, &l:cul)
1194 diffthis
1195 call assert_equal(1, &l:cul)
1196
1197 diffoff
1198 call assert_equal(0, &l:cul)
1199 call assert_equal(1, getwinvar(2, '&l:cul'))
1200 bw!
1201
1202 call assert_equal(1, &l:cul)
1203 diffoff!
1204 call assert_equal(0, &l:cul)
1205 call assert_equal(0, getwinvar(1, '&l:cul'))
1206 bw!
1207
1208 " Cleanup
1209 au! OptionSet
1210 call test_override('starting', 0)
1211endfunc
1212
1213func Test_OptionSet_diffmode_close()
1214 call test_override('starting', 1)
1215 " 19: Try to close the current window when entering diff mode
1216 " should not segfault
1217 new
1218 au OptionSet diff close
1219
1220 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001221 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001222 call assert_equal(1, &diff)
1223 vnew
1224 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001225 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001226 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001227 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001228 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001229 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001230 bw!
1231
1232 " Cleanup
1233 au! OptionSet
1234 call test_override('starting', 0)
1235 "delfunc! AutoCommandOptionSet
1236endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001237
1238" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1239func Test_BufleaveWithDelete()
1240 new | edit Xfile1
1241
1242 augroup test_bufleavewithdelete
1243 autocmd!
1244 autocmd BufLeave Xfile1 bwipe Xfile2
1245 augroup END
1246
1247 call assert_fails('edit Xfile2', 'E143:')
1248 call assert_equal('Xfile1', bufname('%'))
1249
1250 autocmd! test_bufleavewithdelete BufLeave Xfile1
1251 augroup! test_bufleavewithdelete
1252
1253 new
1254 bwipe! Xfile1
1255endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001256
1257" Test for autocommand that changes the buffer list, when doing ":ball".
1258func Test_Acmd_BufAll()
1259 enew!
1260 %bwipe!
1261 call writefile(['Test file Xxx1'], 'Xxx1')
1262 call writefile(['Test file Xxx2'], 'Xxx2')
1263 call writefile(['Test file Xxx3'], 'Xxx3')
1264
1265 " Add three files to the buffer list
1266 split Xxx1
1267 close
1268 split Xxx2
1269 close
1270 split Xxx3
1271 close
1272
1273 " Wipe the buffer when the buffer is opened
1274 au BufReadPost Xxx2 bwipe
1275
1276 call append(0, 'Test file Xxx4')
1277 ball
1278
1279 call assert_equal(2, winnr('$'))
1280 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1281 wincmd t
1282
1283 au! BufReadPost
1284 %bwipe!
1285 call delete('Xxx1')
1286 call delete('Xxx2')
1287 call delete('Xxx3')
1288 enew! | only
1289endfunc
1290
1291" Test for autocommand that changes current buffer on BufEnter event.
1292" Check if modelines are interpreted for the correct buffer.
1293func Test_Acmd_BufEnter()
1294 %bwipe!
1295 call writefile(['start of test file Xxx1',
1296 \ "\<Tab>this is a test",
1297 \ 'end of test file Xxx1'], 'Xxx1')
1298 call writefile(['start of test file Xxx2',
1299 \ 'vim: set noai :',
1300 \ "\<Tab>this is a test",
1301 \ 'end of test file Xxx2'], 'Xxx2')
1302
1303 au BufEnter Xxx2 brew
1304 set ai modeline modelines=3
1305 edit Xxx1
1306 " edit Xxx2, autocmd will do :brew
1307 edit Xxx2
1308 exe "normal G?this is a\<CR>"
1309 " Append text with autoindent to this file
1310 normal othis should be auto-indented
1311 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1312 call assert_equal(3, line('.'))
1313 " Remove autocmd and edit Xxx2 again
1314 au! BufEnter Xxx2
1315 buf! Xxx2
1316 exe "normal G?this is a\<CR>"
1317 " append text without autoindent to Xxx
1318 normal othis should be in column 1
1319 call assert_equal("this should be in column 1", getline('.'))
1320 call assert_equal(4, line('.'))
1321
1322 %bwipe!
1323 call delete('Xxx1')
1324 call delete('Xxx2')
1325 set ai&vim modeline&vim modelines&vim
1326endfunc
1327
1328" Test for issue #57
1329" do not move cursor on <c-o> when autoindent is set
1330func Test_ai_CTRL_O()
1331 enew!
1332 set ai
1333 let save_fo = &fo
1334 set fo+=r
1335 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1336 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1337 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1338
1339 set ai&vim
1340 let &fo = save_fo
1341 enew!
1342endfunc
1343
1344" Test for autocommand that deletes the current buffer on BufLeave event.
1345" Also test deleting the last buffer, should give a new, empty buffer.
1346func Test_BufLeave_Wipe()
1347 %bwipe!
1348 let content = ['start of test file Xxx',
1349 \ 'this is a test',
1350 \ 'end of test file Xxx']
1351 call writefile(content, 'Xxx1')
1352 call writefile(content, 'Xxx2')
1353
1354 au BufLeave Xxx2 bwipe
1355 edit Xxx1
1356 split Xxx2
1357 " delete buffer Xxx2, we should be back to Xxx1
1358 bwipe
1359 call assert_equal('Xxx1', bufname('%'))
1360 call assert_equal(1, winnr('$'))
1361
1362 " Create an alternate buffer
1363 %write! test.out
1364 call assert_equal('test.out', bufname('#'))
1365 " delete alternate buffer
1366 bwipe test.out
1367 call assert_equal('Xxx1', bufname('%'))
1368 call assert_equal('', bufname('#'))
1369
1370 au BufLeave Xxx1 bwipe
1371 " delete current buffer, get an empty one
1372 bwipe!
1373 call assert_equal(1, line('$'))
1374 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001375 let g:bufinfo = getbufinfo()
1376 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001377
1378 call delete('Xxx1')
1379 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001380 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001381 %bwipe
1382 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001383
1384 " check that bufinfo doesn't contain a pointer to freed memory
1385 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001386endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001387
1388func Test_QuitPre()
1389 edit Xfoo
1390 let winid = win_getid(winnr())
1391 split Xbar
1392 au! QuitPre * let g:afile = expand('<afile>')
1393 " Close the other window, <afile> should be correct.
1394 exe win_id2win(winid) . 'q'
1395 call assert_equal('Xfoo', g:afile)
1396
1397 unlet g:afile
1398 bwipe Xfoo
1399 bwipe Xbar
1400endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001401
1402func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001403 au! CmdlineChanged : let g:text = getcmdline()
1404 let g:text = 0
1405 call feedkeys(":echom 'hello'\<CR>", 'xt')
1406 call assert_equal("echom 'hello'", g:text)
1407 au! CmdlineChanged
1408
1409 au! CmdlineChanged : let g:entered = expand('<afile>')
1410 let g:entered = 0
1411 call feedkeys(":echom 'hello'\<CR>", 'xt')
1412 call assert_equal(':', g:entered)
1413 au! CmdlineChanged
1414
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001415 au! CmdlineEnter : let g:entered = expand('<afile>')
1416 au! CmdlineLeave : let g:left = expand('<afile>')
1417 let g:entered = 0
1418 let g:left = 0
1419 call feedkeys(":echo 'hello'\<CR>", 'xt')
1420 call assert_equal(':', g:entered)
1421 call assert_equal(':', g:left)
1422 au! CmdlineEnter
1423 au! CmdlineLeave
1424
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001425 let save_shellslash = &shellslash
1426 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001427 au! CmdlineEnter / let g:entered = expand('<afile>')
1428 au! CmdlineLeave / let g:left = expand('<afile>')
1429 let g:entered = 0
1430 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001431 new
1432 call setline(1, 'hello')
1433 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001434 call assert_equal('/', g:entered)
1435 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001436 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001437 au! CmdlineEnter
1438 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001439 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001440endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001441
1442" Test for BufWritePre autocommand that deletes or unloads the buffer.
1443func Test_BufWritePre()
1444 %bwipe
1445 au BufWritePre Xxx1 bunload
1446 au BufWritePre Xxx2 bwipe
1447
1448 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1449 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1450
1451 edit Xtest
1452 e! Xxx2
1453 bdel Xtest
1454 e Xxx1
1455 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001456 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001457 call assert_equal('Xxx2', bufname('%'))
1458 edit Xtest
1459 e! Xxx2
1460 bwipe Xtest
1461 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001462 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001463 call assert_equal('Xxx1', bufname('%'))
1464 au! BufWritePre
1465 call delete('Xxx1')
1466 call delete('Xxx2')
1467endfunc
1468
1469" Test for BufUnload autocommand that unloads all the other buffers
1470func Test_bufunload_all()
1471 call writefile(['Test file Xxx1'], 'Xxx1')"
1472 call writefile(['Test file Xxx2'], 'Xxx2')"
1473
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001474 let content =<< trim [CODE]
1475 func UnloadAllBufs()
1476 let i = 1
1477 while i <= bufnr('$')
1478 if i != bufnr('%') && bufloaded(i)
1479 exe i . 'bunload'
1480 endif
1481 let i += 1
1482 endwhile
1483 endfunc
1484 au BufUnload * call UnloadAllBufs()
1485 au VimLeave * call writefile(['Test Finished'], 'Xout')
1486 edit Xxx1
1487 split Xxx2
1488 q
1489 [CODE]
1490
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001491 call writefile(content, 'Xtest')
1492
1493 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001494 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001495 call assert_true(filereadable('Xout'))
1496
1497 call delete('Xxx1')
1498 call delete('Xxx2')
1499 call delete('Xtest')
1500 call delete('Xout')
1501endfunc
1502
1503" Some tests for buffer-local autocommands
1504func Test_buflocal_autocmd()
1505 let g:bname = ''
1506 edit xx
1507 au BufLeave <buffer> let g:bname = expand("%")
1508 " here, autocommand for xx should trigger.
1509 " but autocommand shall not apply to buffer named <buffer>.
1510 edit somefile
1511 call assert_equal('xx', g:bname)
1512 let g:bname = ''
1513 " here, autocommand shall be auto-deleted
1514 bwipe xx
1515 " autocmd should not trigger
1516 edit xx
1517 call assert_equal('', g:bname)
1518 " autocmd should not trigger
1519 edit somefile
1520 call assert_equal('', g:bname)
1521 enew
1522 unlet g:bname
1523endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001524
1525" Test for "*Cmd" autocommands
1526func Test_Cmd_Autocmds()
1527 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1528
1529 enew!
1530 au BufReadCmd XtestA 0r Xxx|$del
1531 edit XtestA " will read text of Xxd instead
1532 call assert_equal('start of Xxx', getline(1))
1533
1534 au BufWriteCmd XtestA call append(line("$"), "write")
1535 write " will append a line to the file
1536 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001537 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001538 call assert_equal('write', getline(4))
1539
1540 " now we have:
1541 " 1 start of Xxx
1542 " 2 abc2
1543 " 3 end of Xxx
1544 " 4 write
1545
1546 au FileReadCmd XtestB '[r Xxx
1547 2r XtestB " will read Xxx below line 2 instead
1548 call assert_equal('start of Xxx', getline(3))
1549
1550 " now we have:
1551 " 1 start of Xxx
1552 " 2 abc2
1553 " 3 start of Xxx
1554 " 4 abc2
1555 " 5 end of Xxx
1556 " 6 end of Xxx
1557 " 7 write
1558
1559 au FileWriteCmd XtestC '[,']copy $
1560 normal 4GA1
1561 4,5w XtestC " will copy lines 4 and 5 to the end
1562 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001563 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001564 call assert_equal("end of Xxx", getline(9))
1565
1566 " now we have:
1567 " 1 start of Xxx
1568 " 2 abc2
1569 " 3 start of Xxx
1570 " 4 abc21
1571 " 5 end of Xxx
1572 " 6 end of Xxx
1573 " 7 write
1574 " 8 abc21
1575 " 9 end of Xxx
1576
1577 let g:lines = []
1578 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1579 w >>XtestD " will add lines to 'lines'
1580 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001581 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001582 call assert_equal(9, line('$'))
1583 call assert_equal('end of Xxx', getline('$'))
1584
1585 au BufReadCmd XtestE 0r Xxx|$del
1586 sp XtestE " split window with test.out
1587 call assert_equal('end of Xxx', getline(3))
1588
1589 let g:lines = []
1590 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1591 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1592 wall " will write other window to 'lines'
1593 call assert_equal(4, len(g:lines), g:lines)
1594 call assert_equal('asdf', g:lines[2])
1595
1596 au! BufReadCmd
1597 au! BufWriteCmd
1598 au! FileReadCmd
1599 au! FileWriteCmd
1600 au! FileAppendCmd
1601 %bwipe!
1602 call delete('Xxx')
1603 enew!
1604endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001605
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001606func s:ReadFile()
1607 setl noswapfile nomodified
1608 let filename = resolve(expand("<afile>:p"))
1609 execute 'read' fnameescape(filename)
1610 1d_
1611 exe 'file' fnameescape(filename)
1612 setl buftype=acwrite
1613endfunc
1614
1615func s:WriteFile()
1616 let filename = resolve(expand("<afile>:p"))
1617 setl buftype=
1618 noautocmd execute 'write' fnameescape(filename)
1619 setl buftype=acwrite
1620 setl nomodified
1621endfunc
1622
1623func Test_BufReadCmd()
1624 autocmd BufReadCmd *.test call s:ReadFile()
1625 autocmd BufWriteCmd *.test call s:WriteFile()
1626
1627 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1628 edit Xcmd.test
1629 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1630 normal! Gofour
1631 write
1632 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1633
1634 bwipe!
1635 call delete('Xcmd.test')
1636 au! BufReadCmd
1637 au! BufWriteCmd
1638endfunc
1639
Bram Moolenaaraace2152017-11-05 16:23:10 +01001640func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001641 exe a:start .. 'mark ['
1642 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001643endfunc
1644
1645" Verify the effects of autocmds on '[ and ']
1646func Test_change_mark_in_autocmds()
1647 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001648 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001649
1650 call SetChangeMarks(2, 3)
1651 write
1652 call assert_equal([1, 4], [line("'["), line("']")])
1653
1654 call SetChangeMarks(2, 3)
1655 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1656 write
1657 au! BufWritePre
1658
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001659 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001660 write XtestFilter
1661 write >> XtestFilter
1662
1663 call SetChangeMarks(2, 3)
1664 " Marks are set to the entire range of the write
1665 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1666 " '[ is adjusted to just before the line that will receive the filtered
1667 " data
1668 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1669 " The filtered data is read into the buffer, and the source lines are
1670 " still present, so the range is after the source lines
1671 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1672 %!cat XtestFilter
1673 " After the filtered data is read, the original lines are deleted
1674 call assert_equal([1, 8], [line("'["), line("']")])
1675 au! FilterWritePre,FilterReadPre,FilterReadPost
1676 undo
1677
1678 call SetChangeMarks(1, 4)
1679 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1680 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1681 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1682 2,3!cat XtestFilter
1683 call assert_equal([2, 9], [line("'["), line("']")])
1684 au! FilterWritePre,FilterReadPre,FilterReadPost
1685 undo
1686
1687 call delete('XtestFilter')
1688 endif
1689
1690 call SetChangeMarks(1, 4)
1691 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1692 2,3write Xtest2
1693 au! FileWritePre
1694
1695 call SetChangeMarks(2, 3)
1696 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1697 write >> Xtest2
1698 au! FileAppendPre
1699
1700 call SetChangeMarks(1, 4)
1701 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1702 2,3write >> Xtest2
1703 au! FileAppendPre
1704
1705 call SetChangeMarks(1, 1)
1706 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1707 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1708 3read Xtest2
1709 au! FileReadPre,FileReadPost
1710 undo
1711
1712 call SetChangeMarks(4, 4)
1713 " When the line is 0, it's adjusted to 1
1714 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1715 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1716 0read Xtest2
1717 au! FileReadPre,FileReadPost
1718 undo
1719
1720 call SetChangeMarks(4, 4)
1721 " When the line is 0, it's adjusted to 1
1722 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1723 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1724 1read Xtest2
1725 au! FileReadPre,FileReadPost
1726 undo
1727
1728 bwipe!
1729 call delete('Xtest')
1730 call delete('Xtest2')
1731endfunc
1732
1733func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01001734 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01001735
1736 enew!
1737 call setline(1, ['a', 'b', 'c', 'd'])
1738
1739 let shelltemp = &shelltemp
1740 set shelltemp
1741
1742 let g:filter_au = 0
1743 au FilterWritePre * let g:filter_au += 1
1744 au FilterReadPre * let g:filter_au += 1
1745 au FilterReadPost * let g:filter_au += 1
1746 %!cat
1747 call assert_equal(3, g:filter_au)
1748
1749 if has('filterpipe')
1750 set noshelltemp
1751
1752 let g:filter_au = 0
1753 au FilterWritePre * let g:filter_au += 1
1754 au FilterReadPre * let g:filter_au += 1
1755 au FilterReadPost * let g:filter_au += 1
1756 %!cat
1757 call assert_equal(0, g:filter_au)
1758 endif
1759
1760 au! FilterWritePre,FilterReadPre,FilterReadPost
1761 let &shelltemp = shelltemp
1762 bwipe!
1763endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001764
1765func Test_TextYankPost()
1766 enew!
1767 call setline(1, ['foo'])
1768
1769 let g:event = []
1770 au TextYankPost * let g:event = copy(v:event)
1771
1772 call assert_equal({}, v:event)
1773 call assert_fails('let v:event = {}', 'E46:')
1774 call assert_fails('let v:event.mykey = 0', 'E742:')
1775
1776 norm "ayiw
1777 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001778 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001779 \g:event)
1780 norm y_
1781 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001782 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false},
1783 \g:event)
1784 norm Vy
1785 call assert_equal(
1786 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001787 \g:event)
1788 call feedkeys("\<C-V>y", 'x')
1789 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001790 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001791 \g:event)
1792 norm "xciwbar
1793 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001794 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001795 \g:event)
1796 norm "bdiw
1797 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001798 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001799 \g:event)
1800
1801 call assert_equal({}, v:event)
1802
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001803 if has('clipboard_working') && !has('gui_running')
1804 " Test that when the visual selection is automatically copied to clipboard
1805 " register a TextYankPost is emitted
1806 call setline(1, ['foobar'])
1807
1808 let @* = ''
1809 set clipboard=autoselect
1810 exe "norm! ggviw\<Esc>"
1811 call assert_equal(
1812 \{'regcontents': ['foobar'], 'regname': '*', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1813 \g:event)
1814
1815 let @+ = ''
1816 set clipboard=autoselectplus
1817 exe "norm! ggviw\<Esc>"
1818 call assert_equal(
1819 \{'regcontents': ['foobar'], 'regname': '+', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1820 \g:event)
1821
1822 set clipboard&vim
1823 endif
1824
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001825 au! TextYankPost
1826 unlet g:event
1827 bwipe!
1828endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001829
Bram Moolenaar9a046fd2021-01-28 13:47:59 +01001830func Test_autocommand_all_events()
1831 call assert_fails('au * * bwipe', 'E1155:')
1832 call assert_fails('au * x bwipe', 'E1155:')
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001833endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001834
1835function s:Before_test_dirchanged()
1836 augroup test_dirchanged
1837 autocmd!
1838 augroup END
1839 let s:li = []
1840 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001841 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001842 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001843 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001844 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001845endfunc
1846
1847function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001848 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001849 call delete(s:dir_foo, 'd')
1850 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001851 augroup test_dirchanged
1852 autocmd!
1853 augroup END
1854endfunc
1855
1856function Test_dirchanged_global()
1857 call s:Before_test_dirchanged()
1858 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1859 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001860 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001861 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001862 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001863 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001864 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001865 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001866 call s:After_test_dirchanged()
1867endfunc
1868
1869function Test_dirchanged_local()
1870 call s:Before_test_dirchanged()
1871 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1872 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001873 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001874 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001875 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001876 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001877 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001878 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001879 call s:After_test_dirchanged()
1880endfunc
1881
1882function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001883 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001884 call s:Before_test_dirchanged()
1885 call test_autochdir()
1886 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1887 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1888 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001889 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001890 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001891 exe 'edit ' . s:dir_foo . '/Xfile'
1892 call assert_equal(s:dir_foo, getcwd())
1893 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001894 set noacd
1895 bwipe!
1896 call s:After_test_dirchanged()
1897endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001898
1899" Test TextChangedI and TextChangedP
1900func Test_ChangedP()
1901 new
1902 call setline(1, ['foo', 'bar', 'foobar'])
1903 call test_override("char_avail", 1)
1904 set complete=. completeopt=menuone
1905
1906 func! TextChangedAutocmd(char)
1907 let g:autocmd .= a:char
1908 endfunc
1909
1910 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1911 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1912 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1913
1914 call cursor(3, 1)
1915 let g:autocmd = ''
1916 call feedkeys("o\<esc>", 'tnix')
1917 call assert_equal('I', g:autocmd)
1918
1919 let g:autocmd = ''
1920 call feedkeys("Sf", 'tnix')
1921 call assert_equal('II', g:autocmd)
1922
1923 let g:autocmd = ''
1924 call feedkeys("Sf\<C-N>", 'tnix')
1925 call assert_equal('IIP', g:autocmd)
1926
1927 let g:autocmd = ''
1928 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1929 call assert_equal('IIPP', g:autocmd)
1930
1931 let g:autocmd = ''
1932 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1933 call assert_equal('IIPPP', g:autocmd)
1934
1935 let g:autocmd = ''
1936 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1937 call assert_equal('IIPPPP', g:autocmd)
1938
1939 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1940 " TODO: how should it handle completeopt=noinsert,noselect?
1941
1942 " CleanUp
1943 call test_override("char_avail", 0)
1944 au! TextChanged
1945 au! TextChangedI
1946 au! TextChangedP
1947 delfu TextChangedAutocmd
1948 unlet! g:autocmd
1949 set complete&vim completeopt&vim
1950
1951 bw!
1952endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001953
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001954let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001955func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001956 if !g:setline_handled
1957 call setline(1, "(x)")
1958 let g:setline_handled = v:true
1959 endif
1960endfunc
1961
1962func Test_TextChangedI_with_setline()
1963 new
1964 call test_override('char_avail', 1)
1965 autocmd TextChangedI <buffer> call SetLineOne()
1966 call feedkeys("i(\<CR>\<Esc>", 'tx')
1967 call assert_equal('(', getline(1))
1968 call assert_equal('x)', getline(2))
1969 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001970 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001971 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001972
1973 call test_override('starting', 0)
1974 bwipe!
1975endfunc
1976
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001977func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001978 CheckFeature terminal
1979 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01001980 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01001981 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001982
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001983 " Prepare file for TextChanged event.
1984 call writefile([''], 'Xchanged.txt')
1985 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1986 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001987 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01001988 " In ConPTY, there is additional character which is drawn up to the width of
1989 " the screen.
1990 if has('conpty')
1991 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
1992 else
1993 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
1994 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001995 " It's only adding autocmd, so that no event occurs.
1996 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
1997 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02001998 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001999 call assert_equal([''], readfile('Xchanged.txt'))
2000
2001 " clean up
2002 call delete('Xchanged.txt')
2003 bwipe!
2004endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002005
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002006func Test_autocmd_nested()
2007 let g:did_nested = 0
2008 augroup Testing
2009 au WinNew * edit somefile
2010 au BufNew * let g:did_nested = 1
2011 augroup END
2012 split
2013 call assert_equal(0, g:did_nested)
2014 close
2015 bwipe! somefile
2016
2017 " old nested argument still works
2018 augroup Testing
2019 au!
2020 au WinNew * nested edit somefile
2021 au BufNew * let g:did_nested = 1
2022 augroup END
2023 split
2024 call assert_equal(1, g:did_nested)
2025 close
2026 bwipe! somefile
2027
2028 " New ++nested argument works
2029 augroup Testing
2030 au!
2031 au WinNew * ++nested edit somefile
2032 au BufNew * let g:did_nested = 1
2033 augroup END
2034 split
2035 call assert_equal(1, g:did_nested)
2036 close
2037 bwipe! somefile
2038
2039 augroup Testing
2040 au!
2041 augroup END
2042
2043 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2044 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2045endfunc
2046
2047func Test_autocmd_once()
2048 " Without ++once WinNew triggers twice
2049 let g:did_split = 0
2050 augroup Testing
2051 au WinNew * let g:did_split += 1
2052 augroup END
2053 split
2054 split
2055 call assert_equal(2, g:did_split)
2056 call assert_true(exists('#WinNew'))
2057 close
2058 close
2059
2060 " With ++once WinNew triggers once
2061 let g:did_split = 0
2062 augroup Testing
2063 au!
2064 au WinNew * ++once let g:did_split += 1
2065 augroup END
2066 split
2067 split
2068 call assert_equal(1, g:did_split)
2069 call assert_false(exists('#WinNew'))
2070 close
2071 close
2072
2073 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2074endfunc
2075
Bram Moolenaara68e5952019-04-25 22:22:01 +02002076func Test_autocmd_bufreadpre()
2077 new
2078 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002079 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002080 w! XAutocmdBufReadPre.txt
2081 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002082 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002083 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002084 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002085 wincmd p
2086 let g:wsv1 = winsaveview()
2087 wincmd p
2088 let g:wsv2 = winsaveview()
2089 " triggers BufReadPre, should not move the cursor in either window
2090 " The topline may change one line in a large window.
2091 edit
2092 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2093 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2094 call assert_equal(2, b:bufreadpre)
2095 wincmd p
2096 call assert_equal(g:wsv1.topline, winsaveview().topline)
2097 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2098 call assert_equal(2, b:bufreadpre)
2099 " Now set the cursor position in an BufReadPre autocommand
2100 " (even though the position will be invalid, this should make Vim reset the
2101 " cursor position in the other window.
2102 wincmd p
2103 set cpo+=g
2104 " won't do anything, but try to set the cursor on an invalid lnum
2105 autocmd BufReadPre <buffer> :norm! 70gg
2106 " triggers BufReadPre, should not move the cursor in either window
2107 e
2108 call assert_equal(1, winsaveview().topline)
2109 call assert_equal(1, winsaveview().lnum)
2110 call assert_equal(3, b:bufreadpre)
2111 wincmd p
2112 call assert_equal(g:wsv1.topline, winsaveview().topline)
2113 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2114 call assert_equal(3, b:bufreadpre)
2115 close
2116 close
2117 call delete('XAutocmdBufReadPre.txt')
2118 set cpo-=g
2119endfunc
2120
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002121" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002122
2123" Tests for the following autocommands:
2124" - FileWritePre writing a compressed file
2125" - FileReadPost reading a compressed file
2126" - BufNewFile reading a file template
2127" - BufReadPre decompressing the file to be read
2128" - FilterReadPre substituting characters in the temp file
2129" - FilterReadPost substituting characters after filtering
2130" - FileReadPre set options for decompression
2131" - FileReadPost decompress the file
2132func Test_ReadWrite_Autocmds()
2133 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002134 CheckUnix
2135 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002136
2137 " Make $GZIP empty, "-v" would cause trouble.
2138 let $GZIP = ""
2139
2140 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2141 " being modified outside of Vim (noticed on Solaris).
2142 au FileChangedShell * echo 'caught FileChangedShell'
2143
2144 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2145 augroup Test1
2146 au!
2147 au FileWritePre *.gz '[,']!gzip
2148 au FileWritePost *.gz undo
2149 au FileReadPost *.gz '[,']!gzip -d
2150 augroup END
2151
2152 new
2153 set bin
2154 call append(0, [
2155 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2156 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2157 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2158 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2159 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2160 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2161 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2162 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2163 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2164 \ ])
2165 1,9write! Xtestfile.gz
2166 enew! | close
2167
2168 new
2169 " Read and decompress the testfile
2170 0read Xtestfile.gz
2171 call assert_equal([
2172 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2173 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2174 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2175 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2176 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2177 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2178 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2179 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2180 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2181 \ ], getline(1, 9))
2182 enew! | close
2183
2184 augroup Test1
2185 au!
2186 augroup END
2187
2188 " Test for the FileAppendPre and FileAppendPost autocmds
2189 augroup Test2
2190 au!
2191 au BufNewFile *.c read Xtest.c
2192 au FileAppendPre *.out '[,']s/new/NEW/
2193 au FileAppendPost *.out !cat Xtest.c >> test.out
2194 augroup END
2195
2196 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2197 new foo.c " should load Xtest.c
2198 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2199 w! >> test.out " append it to the output file
2200
2201 let contents = readfile('test.out')
2202 call assert_equal(' * Here is a NEW .c file', contents[2])
2203 call assert_equal(' * Here is a new .c file', contents[5])
2204
2205 call delete('test.out')
2206 enew! | close
2207 augroup Test2
2208 au!
2209 augroup END
2210
2211 " Test for the BufReadPre and BufReadPost autocmds
2212 augroup Test3
2213 au!
2214 " setup autocommands to decompress before reading and re-compress
2215 " afterwards
2216 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2217 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2218 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2219 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2220 augroup END
2221
2222 e! Xtestfile.gz " Edit compressed file
2223 call assert_equal([
2224 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2225 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2226 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2227 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2228 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2229 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2230 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2231 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2232 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2233 \ ], getline(1, 9))
2234
2235 w! >> test.out " Append it to the output file
2236
2237 augroup Test3
2238 au!
2239 augroup END
2240
2241 " Test for the FilterReadPre and FilterReadPost autocmds.
2242 set shelltemp " need temp files here
2243 augroup Test4
2244 au!
2245 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2246 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2247 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2248 au FilterReadPost *.out '[,']s/x/X/g
2249 augroup END
2250
2251 e! test.out " Edit the output file
2252 1,$!cat
2253 call assert_equal([
2254 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2255 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2256 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2257 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2258 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2259 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2260 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2261 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2262 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2263 \ ], getline(1, 9))
2264 call assert_equal([
2265 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2266 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2267 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2268 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2269 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2270 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2271 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2272 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2273 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2274 \ ], readfile('test.out'))
2275
2276 augroup Test4
2277 au!
2278 augroup END
2279 set shelltemp&vim
2280
2281 " Test for the FileReadPre and FileReadPost autocmds.
2282 augroup Test5
2283 au!
2284 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2285 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2286 au FileReadPost *.gz '[,']s/l/L/
2287 augroup END
2288
2289 new
2290 0r Xtestfile.gz " Read compressed file
2291 call assert_equal([
2292 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2293 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2294 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2295 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2296 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2297 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2298 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2299 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2300 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2301 \ ], getline(1, 9))
2302 call assert_equal([
2303 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2304 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2305 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2306 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2307 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2308 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2309 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2310 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2311 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2312 \ ], readfile('Xtestfile.gz'))
2313
2314 augroup Test5
2315 au!
2316 augroup END
2317
2318 au! FileChangedShell
2319 call delete('Xtestfile.gz')
2320 call delete('Xtest.c')
2321 call delete('test.out')
2322endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002323
2324func Test_throw_in_BufWritePre()
2325 new
2326 call setline(1, ['one', 'two', 'three'])
2327 call assert_false(filereadable('Xthefile'))
2328 augroup throwing
2329 au BufWritePre X* throw 'do not write'
2330 augroup END
2331 try
2332 w Xthefile
2333 catch
2334 let caught = 1
2335 endtry
2336 call assert_equal(1, caught)
2337 call assert_false(filereadable('Xthefile'))
2338
2339 bwipe!
2340 au! throwing
2341endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002342
2343func Test_autocmd_SafeState()
2344 CheckRunVimInTerminal
2345
2346 let lines =<< trim END
2347 let g:safe = 0
2348 let g:again = ''
2349 au SafeState * let g:safe += 1
2350 au SafeStateAgain * let g:again ..= 'x'
2351 func CallTimer()
2352 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2353 endfunc
2354 END
2355 call writefile(lines, 'XSafeState')
2356 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2357
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002358 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002359 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002360 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002361 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002362
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002363 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002364 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002365 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002366
2367 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002368 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002369 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002370 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002371 call term_sendkeys(buf, ":echo g:again\<CR>")
2372 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2373
2374 call StopVimInTerminal(buf)
2375 call delete('XSafeState')
2376endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002377
2378func Test_autocmd_CmdWinEnter()
2379 CheckRunVimInTerminal
Bram Moolenaar21829c52021-01-26 22:42:21 +01002380 CheckFeature cmdwin
2381
Bram Moolenaar23324a02019-10-01 17:39:04 +02002382 let lines =<< trim END
2383 let b:dummy_var = 'This is a dummy'
2384 autocmd CmdWinEnter * quit
2385 let winnr = winnr('$')
2386 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002387 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002388 call writefile(lines, filename)
2389 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2390
2391 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002392 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002393 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002394 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002395 call term_sendkeys(buf, ":echo &buftype\<cr>")
2396 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2397 call term_sendkeys(buf, ":echo winnr\<cr>")
2398 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2399
2400 " clean up
2401 call StopVimInTerminal(buf)
2402 call delete(filename)
2403endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002404
2405func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002406 CheckFeature quickfix
2407
Bram Moolenaarec66c412019-10-11 21:19:13 +02002408 pedit xx
2409 n x
2410 au WinEnter * quit
2411 split
2412 au! WinEnter
2413endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002414
2415func Test_BufWrite_lockmarks()
2416 edit! Xtest
2417 call setline(1, ['a', 'b', 'c', 'd'])
2418
2419 " :lockmarks preserves the marks
2420 call SetChangeMarks(2, 3)
2421 lockmarks write
2422 call assert_equal([2, 3], [line("'["), line("']")])
2423
2424 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2425 " original values for the user
2426 augroup lockmarks
2427 au!
2428 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2429 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2430 augroup END
2431
2432 lockmarks write
2433 call assert_equal([2, 3], [line("'["), line("']")])
2434
2435 if executable('cat')
2436 lockmarks %!cat
2437 call assert_equal([2, 3], [line("'["), line("']")])
2438 endif
2439
2440 lockmarks 3,4write Xtest2
2441 call assert_equal([2, 3], [line("'["), line("']")])
2442
2443 au! lockmarks
2444 augroup! lockmarks
2445 call delete('Xtest')
2446 call delete('Xtest2')
2447endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002448
2449func Test_FileType_spell()
2450 if !isdirectory('/tmp')
2451 throw "Skipped: requires /tmp directory"
2452 endif
2453
2454 " this was crashing with an invalid free()
2455 setglobal spellfile=/tmp/en.utf-8.add
2456 augroup crash
2457 autocmd!
2458 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2459 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2460 autocmd FileType anotherfiletype setlocal spell
2461 augroup END
2462 func! NoCrash() abort
2463 edit /tmp/crashfile
2464 endfunc
2465 call NoCrash()
2466
2467 au! crash
2468 setglobal spellfile=
2469endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002470
Bram Moolenaar406cd902020-02-18 21:54:41 +01002471" Test closing a window or editing another buffer from a FileChangedRO handler
2472" in a readonly buffer
2473func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002474 call test_override('ui_delay', 10)
2475
Bram Moolenaar406cd902020-02-18 21:54:41 +01002476 augroup FileChangedROTest
2477 au!
2478 autocmd FileChangedRO * quit
2479 augroup END
2480 new
2481 set readonly
2482 call assert_fails('normal i', 'E788:')
2483 close
2484 augroup! FileChangedROTest
2485
2486 augroup FileChangedROTest
2487 au!
2488 autocmd FileChangedRO * edit Xfile
2489 augroup END
2490 new
2491 set readonly
2492 call assert_fails('normal i', 'E788:')
2493 close
2494 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002495 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002496endfunc
2497
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002498func LogACmd()
2499 call add(g:logged, line('$'))
2500endfunc
2501
2502func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002503 CheckNotGui
2504
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002505 enew!
2506 tabnew
2507 call setline(1, ['a', 'b', 'c', 'd'])
2508 $
2509 au TermChanged * call LogACmd()
2510 let g:logged = []
2511 let term_save = &term
2512 set term=xterm
2513 call assert_equal([1, 4], g:logged)
2514
2515 au! TermChanged
2516 let &term = term_save
2517 bwipe!
2518endfunc
2519
Bram Moolenaare3284872020-03-19 13:55:03 +01002520" Test for FileReadCmd autocmd
2521func Test_autocmd_FileReadCmd()
2522 func ReadFileCmd()
2523 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2524 endfunc
2525 augroup FileReadCmdTest
2526 au!
2527 au FileReadCmd Xtest call ReadFileCmd()
2528 augroup END
2529
2530 new
2531 read ++bin Xtest
2532 read ++nobin Xtest
2533 read ++edit Xtest
2534 read ++bad=keep Xtest
2535 read ++bad=drop Xtest
2536 read ++bad=- Xtest
2537 read ++ff=unix Xtest
2538 read ++ff=dos Xtest
2539 read ++ff=mac Xtest
2540 read ++enc=utf-8 Xtest
2541
2542 call assert_equal(['',
2543 \ 'v:cmdarg = ++bin',
2544 \ 'v:cmdarg = ++nobin',
2545 \ 'v:cmdarg = ++edit',
2546 \ 'v:cmdarg = ++bad=keep',
2547 \ 'v:cmdarg = ++bad=drop',
2548 \ 'v:cmdarg = ++bad=-',
2549 \ 'v:cmdarg = ++ff=unix',
2550 \ 'v:cmdarg = ++ff=dos',
2551 \ 'v:cmdarg = ++ff=mac',
2552 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2553
2554 close!
2555 augroup FileReadCmdTest
2556 au!
2557 augroup END
2558 delfunc ReadFileCmd
2559endfunc
2560
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002561" Test for passing invalid arguments to autocmd
2562func Test_autocmd_invalid_args()
2563 " Additional character after * for event
2564 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2565 augroup Test
2566 augroup END
2567 " Invalid autocmd event
2568 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2569 " Invalid autocmd event in a autocmd group
2570 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2571 augroup! Test
2572 " Execute all autocmds
2573 call assert_fails('doautocmd * BufEnter', 'E217:')
2574 call assert_fails('augroup! x1a2b3', 'E367:')
2575 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002576 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002577endfunc
2578
2579" Test for deep nesting of autocmds
2580func Test_autocmd_deep_nesting()
2581 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2582 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2583 autocmd! BufEnter Xfile
2584endfunc
2585
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002586" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2587func Test_autocmd_sigusr1()
2588 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002589 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002590
2591 let g:sigusr1_passed = 0
2592 au SigUSR1 * let g:sigusr1_passed = 1
2593 call system('/bin/kill -s usr1 ' . getpid())
2594 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2595
2596 au! SigUSR1
2597 unlet g:sigusr1_passed
2598endfunc
2599
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002600" Test for BufReadPre autocmd deleting the file
2601func Test_BufReadPre_delfile()
2602 augroup TestAuCmd
2603 au!
2604 autocmd BufReadPre Xfile call delete('Xfile')
2605 augroup END
2606 call writefile([], 'Xfile')
2607 call assert_fails('new Xfile', 'E200:')
2608 call assert_equal('Xfile', @%)
2609 call assert_equal(1, &readonly)
2610 call delete('Xfile')
2611 augroup TestAuCmd
2612 au!
2613 augroup END
2614 close!
2615endfunc
2616
2617" Test for BufReadPre autocmd changing the current buffer
2618func Test_BufReadPre_changebuf()
2619 augroup TestAuCmd
2620 au!
2621 autocmd BufReadPre Xfile edit Xsomeotherfile
2622 augroup END
2623 call writefile([], 'Xfile')
2624 call assert_fails('new Xfile', 'E201:')
2625 call assert_equal('Xsomeotherfile', @%)
2626 call assert_equal(1, &readonly)
2627 call delete('Xfile')
2628 augroup TestAuCmd
2629 au!
2630 augroup END
2631 close!
2632endfunc
2633
2634" Test for BufWipeouti autocmd changing the current buffer when reading a file
2635" in an empty buffer with 'f' flag in 'cpo'
2636func Test_BufDelete_changebuf()
2637 new
2638 augroup TestAuCmd
2639 au!
2640 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2641 augroup END
2642 let save_cpo = &cpo
2643 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002644 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002645 call assert_equal('somefile', @%)
2646 let &cpo = save_cpo
2647 augroup TestAuCmd
2648 au!
2649 augroup END
2650 close!
2651endfunc
2652
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002653" Test for the temporary internal window used to execute autocmds
2654func Test_autocmd_window()
2655 %bw!
2656 edit one.txt
2657 tabnew two.txt
2658 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002659 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002660 au!
2661 au BufEnter * call add(g:blist, [expand('<afile>'),
2662 \ win_gettype(bufwinnr(expand('<afile>')))])
2663 augroup END
2664
2665 doautoall BufEnter
Bram Moolenaar40a019f2020-06-17 21:41:35 +02002666 call assert_equal([['one.txt', 'autocmd'], ['two.txt', '']], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002667
Bram Moolenaar832adf92020-06-25 19:01:36 +02002668 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002669 au!
2670 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002671 augroup! aucmd_win_test1
2672 %bw!
2673endfunc
2674
2675" Test for trying to close the temporary window used for executing an autocmd
2676func Test_close_autocmd_window()
2677 %bw!
2678 edit one.txt
2679 tabnew two.txt
2680 augroup aucmd_win_test2
2681 au!
2682 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2683 augroup END
2684
2685 call assert_fails('doautoall BufEnter', 'E813:')
2686
2687 augroup aucmd_win_test2
2688 au!
2689 augroup END
2690 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002691 %bwipe!
2692endfunc
2693
2694" Test for trying to close the tab that has the temporary window for exeucing
2695" an autocmd.
2696func Test_close_autocmd_tab()
2697 edit one.txt
2698 tabnew two.txt
2699 augroup aucmd_win_test
2700 au!
2701 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2702 augroup END
2703
2704 call assert_fails('doautoall BufEnter', 'E813:')
2705
2706 tabonly
2707 augroup aucmd_win_test
2708 au!
2709 augroup END
2710 augroup! aucmd_win_test
2711 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002712endfunc
2713
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002714" vim: shiftwidth=2 sts=2 expandtab