blob: 5ec854e67640a87c603936c25d67c883a3d3cfc7 [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:')
217 call feedkeys("\<CR>")
218
219 autocmd! test_autocmd_bufunload
220 augroup! test_autocmd_bufunload
221 bwipe! a.txt
222endfunc
223
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100224func Test_autocmd_dummy_wipeout()
225 " prepare files
226 call writefile([''], 'Xdummywipetest1.txt')
227 call writefile([''], 'Xdummywipetest2.txt')
228 augroup test_bufunload_group
229 autocmd!
230 autocmd BufUnload * call add(s:li, "bufunload")
231 autocmd BufDelete * call add(s:li, "bufdelete")
232 autocmd BufWipeout * call add(s:li, "bufwipeout")
233 augroup END
234
235 let s:li = []
236 split Xdummywipetest1.txt
237 silent! vimgrep /notmatched/ Xdummywipetest*
238 call assert_equal(["bufunload", "bufwipeout"], s:li)
239
240 bwipeout
241 call delete('Xdummywipetest1.txt')
242 call delete('Xdummywipetest2.txt')
243 au! test_bufunload_group
244 augroup! test_bufunload_group
245endfunc
246
Bram Moolenaarc917da42016-07-19 22:31:36 +0200247func Test_win_tab_autocmd()
248 let g:record = []
249
250 augroup testing
251 au WinNew * call add(g:record, 'WinNew')
252 au WinEnter * call add(g:record, 'WinEnter')
253 au WinLeave * call add(g:record, 'WinLeave')
254 au TabNew * call add(g:record, 'TabNew')
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200255 au TabClosed * call add(g:record, 'TabClosed')
Bram Moolenaarc917da42016-07-19 22:31:36 +0200256 au TabEnter * call add(g:record, 'TabEnter')
257 au TabLeave * call add(g:record, 'TabLeave')
258 augroup END
259
260 split
261 tabnew
262 close
263 close
264
265 call assert_equal([
266 \ 'WinLeave', 'WinNew', 'WinEnter',
267 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200268 \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter',
Bram Moolenaarc917da42016-07-19 22:31:36 +0200269 \ 'WinLeave', 'WinEnter'
270 \ ], g:record)
271
Bram Moolenaar12c11d52016-07-19 23:13:03 +0200272 let g:record = []
273 tabnew somefile
274 tabnext
275 bwipe somefile
276
277 call assert_equal([
278 \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter',
279 \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter',
280 \ 'TabClosed'
281 \ ], g:record)
282
Bram Moolenaarc917da42016-07-19 22:31:36 +0200283 augroup testing
284 au!
285 augroup END
286 unlet g:record
287endfunc
Bram Moolenaare99e8442016-07-26 20:43:40 +0200288
289func s:AddAnAutocmd()
290 augroup vimBarTest
291 au BufReadCmd * echo 'hello'
292 augroup END
293 call assert_equal(3, len(split(execute('au vimBarTest'), "\n")))
294endfunc
295
296func Test_early_bar()
297 " test that a bar is recognized before the {event}
298 call s:AddAnAutocmd()
299 augroup vimBarTest | au! | augroup END
300 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
301
302 call s:AddAnAutocmd()
303 augroup vimBarTest| au!| augroup END
304 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
305
306 " test that a bar is recognized after the {event}
307 call s:AddAnAutocmd()
308 augroup vimBarTest| au!BufReadCmd| augroup END
309 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
310
311 " test that a bar is recognized after the {group}
312 call s:AddAnAutocmd()
313 au! vimBarTest|echo 'hello'
314 call assert_equal(1, len(split(execute('au vimBarTest'), "\n")))
315endfunc
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200316
Bram Moolenaar5c809082016-09-01 16:21:48 +0200317func RemoveGroup()
318 autocmd! StartOK
319 augroup! StartOK
320endfunc
321
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200322func Test_augroup_warning()
323 augroup TheWarning
324 au VimEnter * echo 'entering'
325 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100326 call assert_match("TheWarning.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200327 redir => res
328 augroup! TheWarning
329 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100330 call assert_match("W19:", res)
331 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200332
333 " check "Another" does not take the pace of the deleted entry
334 augroup Another
335 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100336 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaaraeac9002016-09-06 22:15:08 +0200337 augroup! Another
Bram Moolenaar5c809082016-09-01 16:21:48 +0200338
339 " no warning for postpone aucmd delete
340 augroup StartOK
341 au VimEnter * call RemoveGroup()
342 augroup END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100343 call assert_match("StartOK.*VimEnter", execute('au VimEnter'))
Bram Moolenaar5c809082016-09-01 16:21:48 +0200344 redir => res
345 doautocmd VimEnter
346 redir END
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100347 call assert_notmatch("W19:", res)
Bram Moolenaarde653f02016-09-03 16:59:06 +0200348 au! VimEnter
Bram Moolenaarad48e6c2020-04-21 22:19:45 +0200349
350 call assert_fails('augroup!', 'E471:')
Bram Moolenaarf2c4c392016-07-29 20:50:24 +0200351endfunc
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200352
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200353func Test_BufReadCmdHelp()
354 " This used to cause access to free memory
355 au BufReadCmd * e +h
356 help
357
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200358 au! BufReadCmd
359endfunc
360
361func Test_BufReadCmdHelpJump()
362 " This used to cause access to free memory
363 au BufReadCmd * e +h{
Bram Moolenaarcf1ba352017-10-27 00:55:04 +0200364 " } to fix highlighting
365 call assert_fails('help', 'E434:')
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200366
Bram Moolenaar8d84ff12017-10-26 16:42:16 +0200367 au! BufReadCmd
368endfunc
369
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200370func Test_augroup_deleted()
Bram Moolenaarde653f02016-09-03 16:59:06 +0200371 " This caused a crash before E936 was introduced
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200372 augroup x
Bram Moolenaarde653f02016-09-03 16:59:06 +0200373 call assert_fails('augroup! x', 'E936:')
374 au VimEnter * echo
375 augroup end
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200376 augroup! x
Bram Moolenaar5dc4e2f2020-11-25 14:15:12 +0100377 call assert_match("-Deleted-.*VimEnter", execute('au VimEnter'))
Bram Moolenaarde653f02016-09-03 16:59:06 +0200378 au! VimEnter
Bram Moolenaarb62cc362016-09-03 16:43:53 +0200379endfunc
380
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200381" Tests for autocommands on :close command.
382" This used to be in test13.
383func Test_three_windows()
Bram Moolenaarb3435b02016-09-29 20:54:59 +0200384 " Clean up buffers, because in some cases this function fails.
385 call s:cleanup_buffers()
386
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200387 " Write three files and open them, each in a window.
388 " Then go to next window, with autocommand that deletes the previous one.
389 " Do this twice, writing the file.
390 e! Xtestje1
391 call setline(1, 'testje1')
392 w
393 sp Xtestje2
394 call setline(1, 'testje2')
395 w
396 sp Xtestje3
397 call setline(1, 'testje3')
398 w
399 wincmd w
400 au WinLeave Xtestje2 bwipe
401 wincmd w
402 call assert_equal('Xtestje1', expand('%'))
403
404 au WinLeave Xtestje1 bwipe Xtestje3
405 close
406 call assert_equal('Xtestje1', expand('%'))
407
408 " Test deleting the buffer on a Unload event. If this goes wrong there
409 " will be the ATTENTION prompt.
410 e Xtestje1
411 au!
412 au! BufUnload Xtestje1 bwipe
413 call assert_fails('e Xtestje3', 'E937:')
414 call assert_equal('Xtestje3', expand('%'))
415
416 e Xtestje2
417 sp Xtestje1
418 call assert_fails('e', 'E937:')
Bram Moolenaara997b452018-04-17 23:24:06 +0200419 call assert_equal('Xtestje1', expand('%'))
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200420
421 " Test changing buffers in a BufWipeout autocommand. If this goes wrong
422 " there are ml_line errors and/or a Crash.
423 au!
424 only
425 e Xanother
426 e Xtestje1
427 bwipe Xtestje2
428 bwipe Xtestje3
429 au BufWipeout Xtestje1 buf Xtestje1
430 bwipe
431 call assert_equal('Xanother', expand('%'))
432
433 only
434 help
435 wincmd w
436 1quit
437 call assert_equal('Xanother', expand('%'))
438
439 au!
Bram Moolenaar4520d442017-03-19 16:09:46 +0100440 enew
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200441 call delete('Xtestje1')
442 call delete('Xtestje2')
443 call delete('Xtestje3')
444endfunc
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100445
446func Test_BufEnter()
447 au! BufEnter
448 au Bufenter * let val = val . '+'
449 let g:val = ''
450 split NewFile
451 call assert_equal('+', g:val)
452 bwipe!
453 call assert_equal('++', g:val)
454
455 " Also get BufEnter when editing a directory
456 call mkdir('Xdir')
457 split Xdir
458 call assert_equal('+++', g:val)
Bram Moolenaare94260f2017-03-21 15:50:12 +0100459
460 " On MS-Windows we can't edit the directory, make sure we wipe the right
461 " buffer.
462 bwipe! Xdir
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100463
464 call delete('Xdir', 'd')
465 au! BufEnter
466endfunc
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100467
468" Closing a window might cause an endless loop
469" E814 for older Vims
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200470func Test_autocmd_bufwipe_in_SessLoadPost()
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200471 edit Xtest
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100472 tabnew
Bram Moolenaar1d68d9b2017-10-13 22:33:32 +0200473 file Xsomething
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100474 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100475 mksession!
476
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200477 let content =<< trim [CODE]
Bram Moolenaar62cd26a2020-10-11 20:08:44 +0200478 call test_override('ui_delay', 10)
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200479 set nocp noswapfile
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100480 let v:swapchoice = "e"
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200481 augroup test_autocmd_sessionload
482 autocmd!
483 autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"
484 augroup END
485
486 func WriteErrors()
487 call writefile([execute("messages")], "Xerrors")
488 endfunc
489 au VimLeave * call WriteErrors()
490 [CODE]
491
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100492 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200493 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100494 let errors = join(readfile('Xerrors'))
Bram Moolenaare2e40752020-09-04 21:18:46 +0200495 call assert_match('E814:', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100496
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100497 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100498 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100499 call delete(file)
500 endfor
501endfunc
502
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100503" Using :blast and :ball for many events caused a crash, because b_nwindows was
504" not incremented correctly.
505func Test_autocmd_blast_badd()
Bram Moolenaare2924322021-01-16 13:11:42 +0100506 " The system() here causes SetChangeMarks() to fail, when run in the GUI
507 " under Windows. No idea why. Happens with any external command, not
508 " related to the actual test.
509 " TODO: find the cause
510 if has('win32')
511 throw 'Skipped: calling system() causes problems'
512 endif
513
Bram Moolenaar797e63b2021-01-15 16:22:52 +0100514 let content =<< trim [CODE]
515 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
516 edit foo1
517 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
518 edit foo2
519 call writefile(['OK'], 'Xerrors')
520 qall
521 [CODE]
522
523 call writefile(content, 'XblastBall')
524 call system(GetVimCommand() .. ' --clean -S XblastBall')
525 call assert_match('OK', readfile('Xerrors')->join())
526
527 call delete('XblastBall')
528 call delete('Xerrors')
529endfunc
530
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100531" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200532func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100533 tabnew
534 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100535 mksession!
536
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200537 let content =<< trim [CODE]
538 set nocp noswapfile
539 function! DeleteInactiveBufs()
540 tabfirst
541 let tabblist = []
542 for i in range(1, tabpagenr(''$''))
543 call extend(tabblist, tabpagebuflist(i))
544 endfor
545 for b in range(1, bufnr(''$''))
546 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
547 exec ''bwipeout '' . b
548 endif
549 endfor
550 echomsg "SessionLoadPost DONE"
551 endfunction
552 au SessionLoadPost * call DeleteInactiveBufs()
553
554 func WriteErrors()
555 call writefile([execute("messages")], "Xerrors")
556 endfunc
557 au VimLeave * call WriteErrors()
558 [CODE]
559
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100560 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200561 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100562 let errors = join(readfile('Xerrors'))
563 " This probably only ever matches on unix.
564 call assert_notmatch('Caught deadly signal SEGV', errors)
565 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100566
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100567 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100568 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100569 call delete(file)
570 endfor
571endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200572
573func Test_empty_doau()
574 doau \|
575endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200576
577func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200578 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200579 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200580 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
581 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 +0200582 let g:opt = [expected, actual]
583 "call assert_equal(expected, actual)
584endfunc
585
586func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200587 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200588
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200589 badd test_autocmd.vim
590
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200591 call test_override('starting', 1)
592 set nocp
593 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
594
595 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100596 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200597 set nu
598 call assert_equal([], g:options)
599 call assert_equal(g:opt[0], g:opt[1])
600
601 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100602 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200603 setlocal nonu
604 call assert_equal([], g:options)
605 call assert_equal(g:opt[0], g:opt[1])
606
607 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100608 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200609 setglobal nonu
610 call assert_equal([], g:options)
611 call assert_equal(g:opt[0], g:opt[1])
612
613 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100614 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200615 setlocal ai
616 call assert_equal([], g:options)
617 call assert_equal(g:opt[0], g:opt[1])
618
619 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100620 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200621 setglobal ai
622 call assert_equal([], g:options)
623 call assert_equal(g:opt[0], g:opt[1])
624
625 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100626 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200627 set ai!
628 call assert_equal([], g:options)
629 call assert_equal(g:opt[0], g:opt[1])
630
631 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100632 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200633 noa setlocal ai
634 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200635 set ai!
636 call assert_equal([], g:options)
637 call assert_equal(g:opt[0], g:opt[1])
638
639 " Should not print anything, use :noa
640 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100641 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200642 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200643 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200644 call assert_equal(g:opt[0], g:opt[1])
645
646 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100647 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200648 set list nu
649 call assert_equal([], g:options)
650 call assert_equal(g:opt[0], g:opt[1])
651
652 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100653 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200654 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200655 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 +0200656 call assert_equal(g:opt[0], g:opt[1])
657
658 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100659 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200660 setlocal acd
661 call assert_equal([], g:options)
662 call assert_equal(g:opt[0], g:opt[1])
663
664 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100665 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200666 set ar
667 call assert_equal([], g:options)
668 call assert_equal(g:opt[0], g:opt[1])
669
670 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100671 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200672 setlocal ar
673 call assert_equal([], g:options)
674 call assert_equal(g:opt[0], g:opt[1])
675
676 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100677 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200678 setglobal invar
679 call assert_equal([], g:options)
680 call assert_equal(g:opt[0], g:opt[1])
681
682 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100683 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
684 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200685 call assert_equal([], g:options)
686 call assert_equal(g:opt[0], g:opt[1])
687
688 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100689 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200690 " try twice, first time, shouldn't trigger because option name is invalid,
691 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200692 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200693 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200694 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200695 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200696 call assert_equal([], g:options)
697 call assert_equal(g:opt[0], g:opt[1])
698
699 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100700 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200701 call setwinvar(0, '&number', 1)
702 call assert_equal([], g:options)
703 call assert_equal(g:opt[0], g:opt[1])
704
705 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100706 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200707 setlocal key=blah
708 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200709 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200710 call assert_equal(g:opt[0], g:opt[1])
711
Bram Moolenaard7c96872019-06-15 17:12:48 +0200712
713 " 18a: Setting string global option"
714 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100715 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200716 set backupext=foo
717 call assert_equal([], g:options)
718 call assert_equal(g:opt[0], g:opt[1])
719
720 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100721 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200722 set backupext&
723 call assert_equal([], g:options)
724 call assert_equal(g:opt[0], g:opt[1])
725
726 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100727 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200728 setglobal backupext=bar
729 call assert_equal([], g:options)
730 call assert_equal(g:opt[0], g:opt[1])
731
732 " 18d: Setting local string global option"
733 " As this is a global option this sets the global value even though
734 " :setlocal is used!
735 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100736 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200737 setlocal backupext=baz
738 call assert_equal([], g:options)
739 call assert_equal(g:opt[0], g:opt[1])
740
741 " 18e: Setting again string global option"
742 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
743 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100744 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200745 set backupext=fuu
746 call assert_equal([], g:options)
747 call assert_equal(g:opt[0], g:opt[1])
748
749
750 " 19a: Setting string local-global (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200751 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100752 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200753 set tags=tagpath
754 call assert_equal([], g:options)
755 call assert_equal(g:opt[0], g:opt[1])
756
Bram Moolenaard7c96872019-06-15 17:12:48 +0200757 " 19b: Resetting string local-global (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100758 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200759 set tags&
760 call assert_equal([], g:options)
761 call assert_equal(g:opt[0], g:opt[1])
762
Bram Moolenaard7c96872019-06-15 17:12:48 +0200763 " 19c: Setting global string local-global (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100764 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200765 setglobal tags=tagpath1
766 call assert_equal([], g:options)
767 call assert_equal(g:opt[0], g:opt[1])
768
769 " 19d: Setting local string local-global (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100770 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200771 setlocal tags=tagpath2
772 call assert_equal([], g:options)
773 call assert_equal(g:opt[0], g:opt[1])
774
775 " 19e: Setting again string local-global (to buffer) option"
776 " Note: v:option_old is the old global value for local-global string options
777 " but the old local value for all other kinds of options.
778 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
779 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100780 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200781 set tags=tagpath
782 call assert_equal([], g:options)
783 call assert_equal(g:opt[0], g:opt[1])
784
785 " 19f: Setting string local-global (to buffer) option to an empty string"
786 " Note: v:option_old is the old global value for local-global string options
787 " but the old local value for all other kinds of options.
788 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
789 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100790 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200791 set tags=tagpath
792 call assert_equal([], g:options)
793 call assert_equal(g:opt[0], g:opt[1])
794
795
796 " 20a: Setting string local (to buffer) option"
797 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100798 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200799 set spelllang=elvish,klingon
800 call assert_equal([], g:options)
801 call assert_equal(g:opt[0], g:opt[1])
802
803 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100804 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200805 set spelllang&
806 call assert_equal([], g:options)
807 call assert_equal(g:opt[0], g:opt[1])
808
809 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100810 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200811 setglobal spelllang=elvish
812 call assert_equal([], g:options)
813 call assert_equal(g:opt[0], g:opt[1])
814
815 " 20d: Setting local string local (to buffer) option"
816 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100817 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200818 setlocal spelllang=klingon
819 call assert_equal([], g:options)
820 call assert_equal(g:opt[0], g:opt[1])
821
822 " 20e: Setting again string local (to buffer) option"
823 " Note: v:option_old is the old global value for local-global string options
824 " but the old local value for all other kinds of options.
825 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
826 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100827 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200828 set spelllang=foo
829 call assert_equal([], g:options)
830 call assert_equal(g:opt[0], g:opt[1])
831
832
833 " 21a: Setting string local-global (to window) option"
834 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100835 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200836 set statusline=foo
837 call assert_equal([], g:options)
838 call assert_equal(g:opt[0], g:opt[1])
839
840 " 21b: Resetting string local-global (to window) option"
841 " Note: v:option_old is the old global value for local-global string options
842 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100843 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200844 set statusline&
845 call assert_equal([], g:options)
846 call assert_equal(g:opt[0], g:opt[1])
847
848 " 21c: Setting global string local-global (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100849 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200850 setglobal statusline=bar
851 call assert_equal([], g:options)
852 call assert_equal(g:opt[0], g:opt[1])
853
854 " 21d: Setting local string local-global (to window) option"
855 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100856 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200857 setlocal statusline=baz
858 call assert_equal([], g:options)
859 call assert_equal(g:opt[0], g:opt[1])
860
861 " 21e: Setting again string local-global (to window) option"
862 " Note: v:option_old is the old global value for local-global string options
863 " but the old local value for all other kinds of options.
864 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
865 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100866 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200867 set statusline=foo
868 call assert_equal([], g:options)
869 call assert_equal(g:opt[0], g:opt[1])
870
871
872 " 22a: Setting string local (to window) option"
873 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100874 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200875 set foldignore=fo
876 call assert_equal([], g:options)
877 call assert_equal(g:opt[0], g:opt[1])
878
879 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100880 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200881 set foldignore&
882 call assert_equal([], g:options)
883 call assert_equal(g:opt[0], g:opt[1])
884
885 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100886 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200887 setglobal foldignore=bar
888 call assert_equal([], g:options)
889 call assert_equal(g:opt[0], g:opt[1])
890
891 " 22d: Setting local string local (to window) option"
892 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100893 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200894 setlocal foldignore=baz
895 call assert_equal([], g:options)
896 call assert_equal(g:opt[0], g:opt[1])
897
898 " 22e: Setting again string local (to window) option"
899 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
900 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100901 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200902 set foldignore=fo
903 call assert_equal([], g:options)
904 call assert_equal(g:opt[0], g:opt[1])
905
906
907 " 23a: Setting global number local option"
908 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
909 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100910 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200911 setglobal cmdheight=2
912 call assert_equal([], g:options)
913 call assert_equal(g:opt[0], g:opt[1])
914
915 " 23b: Setting local number global option"
916 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
917 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100918 let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200919 setlocal cmdheight=2
920 call assert_equal([], g:options)
921 call assert_equal(g:opt[0], g:opt[1])
922
923 " 23c: Setting again number global option"
924 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
925 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100926 let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200927 set cmdheight=2
928 call assert_equal([], g:options)
929 call assert_equal(g:opt[0], g:opt[1])
930
931 " 23d: Setting again number global option"
932 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100933 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200934 set cmdheight=2
935 call assert_equal([], g:options)
936 call assert_equal(g:opt[0], g:opt[1])
937
938
939 " 24a: Setting global number global-local (to buffer) option"
940 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
941 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100942 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200943 setglobal undolevels=2
944 call assert_equal([], g:options)
945 call assert_equal(g:opt[0], g:opt[1])
946
947 " 24b: Setting local number global-local (to buffer) option"
948 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
949 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100950 let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200951 setlocal undolevels=2
952 call assert_equal([], g:options)
953 call assert_equal(g:opt[0], g:opt[1])
954
955 " 24c: Setting again number global-local (to buffer) option"
956 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
957 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100958 let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200959 set undolevels=2
960 call assert_equal([], g:options)
961 call assert_equal(g:opt[0], g:opt[1])
962
963 " 24d: Setting again global number global-local (to buffer) option"
964 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100965 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200966 set undolevels=2
967 call assert_equal([], g:options)
968 call assert_equal(g:opt[0], g:opt[1])
969
970
971 " 25a: Setting global number local (to buffer) option"
972 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
973 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100974 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200975 setglobal wrapmargin=2
976 call assert_equal([], g:options)
977 call assert_equal(g:opt[0], g:opt[1])
978
979 " 25b: Setting local number local (to buffer) option"
980 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
981 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100982 let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200983 setlocal wrapmargin=2
984 call assert_equal([], g:options)
985 call assert_equal(g:opt[0], g:opt[1])
986
987 " 25c: Setting again number local (to buffer) option"
988 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
989 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100990 let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200991 set wrapmargin=2
992 call assert_equal([], g:options)
993 call assert_equal(g:opt[0], g:opt[1])
994
995 " 25d: Setting again global number local (to buffer) option"
996 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100997 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200998 set wrapmargin=2
999 call assert_equal([], g:options)
1000 call assert_equal(g:opt[0], g:opt[1])
1001
1002
1003 " 26: Setting number global-local (to window) option.
1004 " Such option does currently not exist.
1005
1006
1007 " 27a: Setting global number local (to window) option"
1008 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1009 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001010 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001011 setglobal foldcolumn=2
1012 call assert_equal([], g:options)
1013 call assert_equal(g:opt[0], g:opt[1])
1014
1015 " 27b: Setting local number local (to window) option"
1016 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1017 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001018 let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001019 setlocal foldcolumn=2
1020 call assert_equal([], g:options)
1021 call assert_equal(g:opt[0], g:opt[1])
1022
1023 " 27c: Setting again number local (to window) option"
1024 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1025 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001026 let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001027 set foldcolumn=2
1028 call assert_equal([], g:options)
1029 call assert_equal(g:opt[0], g:opt[1])
1030
1031 " 27d: Ssettin again global number local (to window) option"
1032 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001033 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001034 set foldcolumn=2
1035 call assert_equal([], g:options)
1036 call assert_equal(g:opt[0], g:opt[1])
1037
1038
1039 " 28a: Setting global boolean global option"
1040 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1041 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001042 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001043 setglobal nowrapscan
1044 call assert_equal([], g:options)
1045 call assert_equal(g:opt[0], g:opt[1])
1046
1047 " 28b: Setting local boolean global option"
1048 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1049 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001050 let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001051 setlocal nowrapscan
1052 call assert_equal([], g:options)
1053 call assert_equal(g:opt[0], g:opt[1])
1054
1055 " 28c: Setting again boolean global option"
1056 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1057 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001058 let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001059 set nowrapscan
1060 call assert_equal([], g:options)
1061 call assert_equal(g:opt[0], g:opt[1])
1062
1063 " 28d: Setting again global boolean global option"
1064 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001065 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001066 set wrapscan
1067 call assert_equal([], g:options)
1068 call assert_equal(g:opt[0], g:opt[1])
1069
1070
1071 " 29a: Setting global boolean global-local (to buffer) option"
1072 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1073 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001074 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001075 setglobal autoread
1076 call assert_equal([], g:options)
1077 call assert_equal(g:opt[0], g:opt[1])
1078
1079 " 29b: Setting local boolean global-local (to buffer) option"
1080 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1081 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001082 let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001083 setlocal noautoread
1084 call assert_equal([], g:options)
1085 call assert_equal(g:opt[0], g:opt[1])
1086
1087 " 29c: Setting again boolean global-local (to buffer) option"
1088 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1089 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001090 let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001091 set autoread
1092 call assert_equal([], g:options)
1093 call assert_equal(g:opt[0], g:opt[1])
1094
1095 " 29d: Setting again global boolean global-local (to buffer) option"
1096 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001097 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001098 set autoread
1099 call assert_equal([], g:options)
1100 call assert_equal(g:opt[0], g:opt[1])
1101
1102
1103 " 30a: Setting global boolean local (to buffer) option"
1104 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1105 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001106 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001107 setglobal cindent
1108 call assert_equal([], g:options)
1109 call assert_equal(g:opt[0], g:opt[1])
1110
1111 " 30b: Setting local boolean local (to buffer) option"
1112 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1113 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001114 let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001115 setlocal nocindent
1116 call assert_equal([], g:options)
1117 call assert_equal(g:opt[0], g:opt[1])
1118
1119 " 30c: Setting again boolean local (to buffer) option"
1120 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1121 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001122 let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001123 set cindent
1124 call assert_equal([], g:options)
1125 call assert_equal(g:opt[0], g:opt[1])
1126
1127 " 30d: Setting again global boolean local (to buffer) option"
1128 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001129 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001130 set cindent
1131 call assert_equal([], g:options)
1132 call assert_equal(g:opt[0], g:opt[1])
1133
1134
1135 " 31: Setting boolean global-local (to window) option
1136 " Currently no such option exists.
1137
1138
1139 " 32a: Setting global boolean local (to window) option"
1140 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1141 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001142 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001143 setglobal cursorcolumn
1144 call assert_equal([], g:options)
1145 call assert_equal(g:opt[0], g:opt[1])
1146
1147 " 32b: Setting local boolean local (to window) option"
1148 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1149 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001150 let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001151 setlocal nocursorcolumn
1152 call assert_equal([], g:options)
1153 call assert_equal(g:opt[0], g:opt[1])
1154
1155 " 32c: Setting again boolean local (to window) option"
1156 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1157 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001158 let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001159 set cursorcolumn
1160 call assert_equal([], g:options)
1161 call assert_equal(g:opt[0], g:opt[1])
1162
1163 " 32d: Setting again global boolean local (to window) option"
1164 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001165 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001166 set cursorcolumn
1167 call assert_equal([], g:options)
1168 call assert_equal(g:opt[0], g:opt[1])
1169
1170
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001171 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001172 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001173 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001174 set backspace=2
1175 call assert_equal([], g:options)
1176 call assert_equal(g:opt[0], g:opt[1])
1177
1178
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001179 " Cleanup
1180 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001181 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001182 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 +02001183 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001184 endfor
1185 call test_override('starting', 0)
1186 delfunc! AutoCommandOptionSet
1187endfunc
1188
1189func Test_OptionSet_diffmode()
1190 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001191 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001192 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001193 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001194
1195 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1196 call assert_equal(0, &l:cul)
1197 diffthis
1198 call assert_equal(1, &l:cul)
1199
1200 vnew
1201 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1202 call assert_equal(0, &l:cul)
1203 diffthis
1204 call assert_equal(1, &l:cul)
1205
1206 diffoff
1207 call assert_equal(0, &l:cul)
1208 call assert_equal(1, getwinvar(2, '&l:cul'))
1209 bw!
1210
1211 call assert_equal(1, &l:cul)
1212 diffoff!
1213 call assert_equal(0, &l:cul)
1214 call assert_equal(0, getwinvar(1, '&l:cul'))
1215 bw!
1216
1217 " Cleanup
1218 au! OptionSet
1219 call test_override('starting', 0)
1220endfunc
1221
1222func Test_OptionSet_diffmode_close()
1223 call test_override('starting', 1)
1224 " 19: Try to close the current window when entering diff mode
1225 " should not segfault
1226 new
1227 au OptionSet diff close
1228
1229 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001230 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001231 call assert_equal(1, &diff)
1232 vnew
1233 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001234 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001235 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001236 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001237 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001238 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001239 bw!
1240
1241 " Cleanup
1242 au! OptionSet
1243 call test_override('starting', 0)
1244 "delfunc! AutoCommandOptionSet
1245endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001246
1247" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1248func Test_BufleaveWithDelete()
1249 new | edit Xfile1
1250
1251 augroup test_bufleavewithdelete
1252 autocmd!
1253 autocmd BufLeave Xfile1 bwipe Xfile2
1254 augroup END
1255
1256 call assert_fails('edit Xfile2', 'E143:')
1257 call assert_equal('Xfile1', bufname('%'))
1258
1259 autocmd! test_bufleavewithdelete BufLeave Xfile1
1260 augroup! test_bufleavewithdelete
1261
1262 new
1263 bwipe! Xfile1
1264endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001265
1266" Test for autocommand that changes the buffer list, when doing ":ball".
1267func Test_Acmd_BufAll()
1268 enew!
1269 %bwipe!
1270 call writefile(['Test file Xxx1'], 'Xxx1')
1271 call writefile(['Test file Xxx2'], 'Xxx2')
1272 call writefile(['Test file Xxx3'], 'Xxx3')
1273
1274 " Add three files to the buffer list
1275 split Xxx1
1276 close
1277 split Xxx2
1278 close
1279 split Xxx3
1280 close
1281
1282 " Wipe the buffer when the buffer is opened
1283 au BufReadPost Xxx2 bwipe
1284
1285 call append(0, 'Test file Xxx4')
1286 ball
1287
1288 call assert_equal(2, winnr('$'))
1289 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1290 wincmd t
1291
1292 au! BufReadPost
1293 %bwipe!
1294 call delete('Xxx1')
1295 call delete('Xxx2')
1296 call delete('Xxx3')
1297 enew! | only
1298endfunc
1299
1300" Test for autocommand that changes current buffer on BufEnter event.
1301" Check if modelines are interpreted for the correct buffer.
1302func Test_Acmd_BufEnter()
1303 %bwipe!
1304 call writefile(['start of test file Xxx1',
1305 \ "\<Tab>this is a test",
1306 \ 'end of test file Xxx1'], 'Xxx1')
1307 call writefile(['start of test file Xxx2',
1308 \ 'vim: set noai :',
1309 \ "\<Tab>this is a test",
1310 \ 'end of test file Xxx2'], 'Xxx2')
1311
1312 au BufEnter Xxx2 brew
1313 set ai modeline modelines=3
1314 edit Xxx1
1315 " edit Xxx2, autocmd will do :brew
1316 edit Xxx2
1317 exe "normal G?this is a\<CR>"
1318 " Append text with autoindent to this file
1319 normal othis should be auto-indented
1320 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1321 call assert_equal(3, line('.'))
1322 " Remove autocmd and edit Xxx2 again
1323 au! BufEnter Xxx2
1324 buf! Xxx2
1325 exe "normal G?this is a\<CR>"
1326 " append text without autoindent to Xxx
1327 normal othis should be in column 1
1328 call assert_equal("this should be in column 1", getline('.'))
1329 call assert_equal(4, line('.'))
1330
1331 %bwipe!
1332 call delete('Xxx1')
1333 call delete('Xxx2')
1334 set ai&vim modeline&vim modelines&vim
1335endfunc
1336
1337" Test for issue #57
1338" do not move cursor on <c-o> when autoindent is set
1339func Test_ai_CTRL_O()
1340 enew!
1341 set ai
1342 let save_fo = &fo
1343 set fo+=r
1344 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1345 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1346 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1347
1348 set ai&vim
1349 let &fo = save_fo
1350 enew!
1351endfunc
1352
1353" Test for autocommand that deletes the current buffer on BufLeave event.
1354" Also test deleting the last buffer, should give a new, empty buffer.
1355func Test_BufLeave_Wipe()
1356 %bwipe!
1357 let content = ['start of test file Xxx',
1358 \ 'this is a test',
1359 \ 'end of test file Xxx']
1360 call writefile(content, 'Xxx1')
1361 call writefile(content, 'Xxx2')
1362
1363 au BufLeave Xxx2 bwipe
1364 edit Xxx1
1365 split Xxx2
1366 " delete buffer Xxx2, we should be back to Xxx1
1367 bwipe
1368 call assert_equal('Xxx1', bufname('%'))
1369 call assert_equal(1, winnr('$'))
1370
1371 " Create an alternate buffer
1372 %write! test.out
1373 call assert_equal('test.out', bufname('#'))
1374 " delete alternate buffer
1375 bwipe test.out
1376 call assert_equal('Xxx1', bufname('%'))
1377 call assert_equal('', bufname('#'))
1378
1379 au BufLeave Xxx1 bwipe
1380 " delete current buffer, get an empty one
1381 bwipe!
1382 call assert_equal(1, line('$'))
1383 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001384 let g:bufinfo = getbufinfo()
1385 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001386
1387 call delete('Xxx1')
1388 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001389 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001390 %bwipe
1391 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001392
1393 " check that bufinfo doesn't contain a pointer to freed memory
1394 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001395endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001396
1397func Test_QuitPre()
1398 edit Xfoo
1399 let winid = win_getid(winnr())
1400 split Xbar
1401 au! QuitPre * let g:afile = expand('<afile>')
1402 " Close the other window, <afile> should be correct.
1403 exe win_id2win(winid) . 'q'
1404 call assert_equal('Xfoo', g:afile)
1405
1406 unlet g:afile
1407 bwipe Xfoo
1408 bwipe Xbar
1409endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001410
1411func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001412 au! CmdlineChanged : let g:text = getcmdline()
1413 let g:text = 0
1414 call feedkeys(":echom 'hello'\<CR>", 'xt')
1415 call assert_equal("echom 'hello'", g:text)
1416 au! CmdlineChanged
1417
1418 au! CmdlineChanged : let g:entered = expand('<afile>')
1419 let g:entered = 0
1420 call feedkeys(":echom 'hello'\<CR>", 'xt')
1421 call assert_equal(':', g:entered)
1422 au! CmdlineChanged
1423
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001424 au! CmdlineEnter : let g:entered = expand('<afile>')
1425 au! CmdlineLeave : let g:left = expand('<afile>')
1426 let g:entered = 0
1427 let g:left = 0
1428 call feedkeys(":echo 'hello'\<CR>", 'xt')
1429 call assert_equal(':', g:entered)
1430 call assert_equal(':', g:left)
1431 au! CmdlineEnter
1432 au! CmdlineLeave
1433
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001434 let save_shellslash = &shellslash
1435 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001436 au! CmdlineEnter / let g:entered = expand('<afile>')
1437 au! CmdlineLeave / let g:left = expand('<afile>')
1438 let g:entered = 0
1439 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001440 new
1441 call setline(1, 'hello')
1442 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001443 call assert_equal('/', g:entered)
1444 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001445 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001446 au! CmdlineEnter
1447 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001448 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001449endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001450
1451" Test for BufWritePre autocommand that deletes or unloads the buffer.
1452func Test_BufWritePre()
1453 %bwipe
1454 au BufWritePre Xxx1 bunload
1455 au BufWritePre Xxx2 bwipe
1456
1457 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1458 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1459
1460 edit Xtest
1461 e! Xxx2
1462 bdel Xtest
1463 e Xxx1
1464 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001465 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001466 call assert_equal('Xxx2', bufname('%'))
1467 edit Xtest
1468 e! Xxx2
1469 bwipe Xtest
1470 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001471 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001472 call assert_equal('Xxx1', bufname('%'))
1473 au! BufWritePre
1474 call delete('Xxx1')
1475 call delete('Xxx2')
1476endfunc
1477
1478" Test for BufUnload autocommand that unloads all the other buffers
1479func Test_bufunload_all()
1480 call writefile(['Test file Xxx1'], 'Xxx1')"
1481 call writefile(['Test file Xxx2'], 'Xxx2')"
1482
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001483 let content =<< trim [CODE]
1484 func UnloadAllBufs()
1485 let i = 1
1486 while i <= bufnr('$')
1487 if i != bufnr('%') && bufloaded(i)
1488 exe i . 'bunload'
1489 endif
1490 let i += 1
1491 endwhile
1492 endfunc
1493 au BufUnload * call UnloadAllBufs()
1494 au VimLeave * call writefile(['Test Finished'], 'Xout')
1495 edit Xxx1
1496 split Xxx2
1497 q
1498 [CODE]
1499
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001500 call writefile(content, 'Xtest')
1501
1502 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001503 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001504 call assert_true(filereadable('Xout'))
1505
1506 call delete('Xxx1')
1507 call delete('Xxx2')
1508 call delete('Xtest')
1509 call delete('Xout')
1510endfunc
1511
1512" Some tests for buffer-local autocommands
1513func Test_buflocal_autocmd()
1514 let g:bname = ''
1515 edit xx
1516 au BufLeave <buffer> let g:bname = expand("%")
1517 " here, autocommand for xx should trigger.
1518 " but autocommand shall not apply to buffer named <buffer>.
1519 edit somefile
1520 call assert_equal('xx', g:bname)
1521 let g:bname = ''
1522 " here, autocommand shall be auto-deleted
1523 bwipe xx
1524 " autocmd should not trigger
1525 edit xx
1526 call assert_equal('', g:bname)
1527 " autocmd should not trigger
1528 edit somefile
1529 call assert_equal('', g:bname)
1530 enew
1531 unlet g:bname
1532endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001533
1534" Test for "*Cmd" autocommands
1535func Test_Cmd_Autocmds()
1536 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1537
1538 enew!
1539 au BufReadCmd XtestA 0r Xxx|$del
1540 edit XtestA " will read text of Xxd instead
1541 call assert_equal('start of Xxx', getline(1))
1542
1543 au BufWriteCmd XtestA call append(line("$"), "write")
1544 write " will append a line to the file
1545 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001546 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001547 call assert_equal('write', getline(4))
1548
1549 " now we have:
1550 " 1 start of Xxx
1551 " 2 abc2
1552 " 3 end of Xxx
1553 " 4 write
1554
1555 au FileReadCmd XtestB '[r Xxx
1556 2r XtestB " will read Xxx below line 2 instead
1557 call assert_equal('start of Xxx', getline(3))
1558
1559 " now we have:
1560 " 1 start of Xxx
1561 " 2 abc2
1562 " 3 start of Xxx
1563 " 4 abc2
1564 " 5 end of Xxx
1565 " 6 end of Xxx
1566 " 7 write
1567
1568 au FileWriteCmd XtestC '[,']copy $
1569 normal 4GA1
1570 4,5w XtestC " will copy lines 4 and 5 to the end
1571 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001572 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001573 call assert_equal("end of Xxx", getline(9))
1574
1575 " now we have:
1576 " 1 start of Xxx
1577 " 2 abc2
1578 " 3 start of Xxx
1579 " 4 abc21
1580 " 5 end of Xxx
1581 " 6 end of Xxx
1582 " 7 write
1583 " 8 abc21
1584 " 9 end of Xxx
1585
1586 let g:lines = []
1587 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1588 w >>XtestD " will add lines to 'lines'
1589 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001590 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001591 call assert_equal(9, line('$'))
1592 call assert_equal('end of Xxx', getline('$'))
1593
1594 au BufReadCmd XtestE 0r Xxx|$del
1595 sp XtestE " split window with test.out
1596 call assert_equal('end of Xxx', getline(3))
1597
1598 let g:lines = []
1599 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1600 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1601 wall " will write other window to 'lines'
1602 call assert_equal(4, len(g:lines), g:lines)
1603 call assert_equal('asdf', g:lines[2])
1604
1605 au! BufReadCmd
1606 au! BufWriteCmd
1607 au! FileReadCmd
1608 au! FileWriteCmd
1609 au! FileAppendCmd
1610 %bwipe!
1611 call delete('Xxx')
1612 enew!
1613endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001614
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001615func s:ReadFile()
1616 setl noswapfile nomodified
1617 let filename = resolve(expand("<afile>:p"))
1618 execute 'read' fnameescape(filename)
1619 1d_
1620 exe 'file' fnameescape(filename)
1621 setl buftype=acwrite
1622endfunc
1623
1624func s:WriteFile()
1625 let filename = resolve(expand("<afile>:p"))
1626 setl buftype=
1627 noautocmd execute 'write' fnameescape(filename)
1628 setl buftype=acwrite
1629 setl nomodified
1630endfunc
1631
1632func Test_BufReadCmd()
1633 autocmd BufReadCmd *.test call s:ReadFile()
1634 autocmd BufWriteCmd *.test call s:WriteFile()
1635
1636 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1637 edit Xcmd.test
1638 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1639 normal! Gofour
1640 write
1641 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1642
1643 bwipe!
1644 call delete('Xcmd.test')
1645 au! BufReadCmd
1646 au! BufWriteCmd
1647endfunc
1648
Bram Moolenaaraace2152017-11-05 16:23:10 +01001649func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001650 exe a:start .. 'mark ['
1651 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001652endfunc
1653
1654" Verify the effects of autocmds on '[ and ']
1655func Test_change_mark_in_autocmds()
1656 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001657 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001658
1659 call SetChangeMarks(2, 3)
1660 write
1661 call assert_equal([1, 4], [line("'["), line("']")])
1662
1663 call SetChangeMarks(2, 3)
1664 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1665 write
1666 au! BufWritePre
1667
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001668 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001669 write XtestFilter
1670 write >> XtestFilter
1671
1672 call SetChangeMarks(2, 3)
1673 " Marks are set to the entire range of the write
1674 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1675 " '[ is adjusted to just before the line that will receive the filtered
1676 " data
1677 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1678 " The filtered data is read into the buffer, and the source lines are
1679 " still present, so the range is after the source lines
1680 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1681 %!cat XtestFilter
1682 " After the filtered data is read, the original lines are deleted
1683 call assert_equal([1, 8], [line("'["), line("']")])
1684 au! FilterWritePre,FilterReadPre,FilterReadPost
1685 undo
1686
1687 call SetChangeMarks(1, 4)
1688 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1689 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1690 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1691 2,3!cat XtestFilter
1692 call assert_equal([2, 9], [line("'["), line("']")])
1693 au! FilterWritePre,FilterReadPre,FilterReadPost
1694 undo
1695
1696 call delete('XtestFilter')
1697 endif
1698
1699 call SetChangeMarks(1, 4)
1700 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1701 2,3write Xtest2
1702 au! FileWritePre
1703
1704 call SetChangeMarks(2, 3)
1705 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1706 write >> Xtest2
1707 au! FileAppendPre
1708
1709 call SetChangeMarks(1, 4)
1710 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1711 2,3write >> Xtest2
1712 au! FileAppendPre
1713
1714 call SetChangeMarks(1, 1)
1715 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1716 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1717 3read Xtest2
1718 au! FileReadPre,FileReadPost
1719 undo
1720
1721 call SetChangeMarks(4, 4)
1722 " When the line is 0, it's adjusted to 1
1723 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1724 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1725 0read Xtest2
1726 au! FileReadPre,FileReadPost
1727 undo
1728
1729 call SetChangeMarks(4, 4)
1730 " When the line is 0, it's adjusted to 1
1731 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1732 au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")])
1733 1read Xtest2
1734 au! FileReadPre,FileReadPost
1735 undo
1736
1737 bwipe!
1738 call delete('Xtest')
1739 call delete('Xtest2')
1740endfunc
1741
1742func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01001743 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01001744
1745 enew!
1746 call setline(1, ['a', 'b', 'c', 'd'])
1747
1748 let shelltemp = &shelltemp
1749 set shelltemp
1750
1751 let g:filter_au = 0
1752 au FilterWritePre * let g:filter_au += 1
1753 au FilterReadPre * let g:filter_au += 1
1754 au FilterReadPost * let g:filter_au += 1
1755 %!cat
1756 call assert_equal(3, g:filter_au)
1757
1758 if has('filterpipe')
1759 set noshelltemp
1760
1761 let g:filter_au = 0
1762 au FilterWritePre * let g:filter_au += 1
1763 au FilterReadPre * let g:filter_au += 1
1764 au FilterReadPost * let g:filter_au += 1
1765 %!cat
1766 call assert_equal(0, g:filter_au)
1767 endif
1768
1769 au! FilterWritePre,FilterReadPre,FilterReadPost
1770 let &shelltemp = shelltemp
1771 bwipe!
1772endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001773
1774func Test_TextYankPost()
1775 enew!
1776 call setline(1, ['foo'])
1777
1778 let g:event = []
1779 au TextYankPost * let g:event = copy(v:event)
1780
1781 call assert_equal({}, v:event)
1782 call assert_fails('let v:event = {}', 'E46:')
1783 call assert_fails('let v:event.mykey = 0', 'E742:')
1784
1785 norm "ayiw
1786 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001787 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001788 \g:event)
1789 norm y_
1790 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001791 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false},
1792 \g:event)
1793 norm Vy
1794 call assert_equal(
1795 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001796 \g:event)
1797 call feedkeys("\<C-V>y", 'x')
1798 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001799 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001800 \g:event)
1801 norm "xciwbar
1802 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001803 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001804 \g:event)
1805 norm "bdiw
1806 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001807 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001808 \g:event)
1809
1810 call assert_equal({}, v:event)
1811
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001812 if has('clipboard_working') && !has('gui_running')
1813 " Test that when the visual selection is automatically copied to clipboard
1814 " register a TextYankPost is emitted
1815 call setline(1, ['foobar'])
1816
1817 let @* = ''
1818 set clipboard=autoselect
1819 exe "norm! ggviw\<Esc>"
1820 call assert_equal(
1821 \{'regcontents': ['foobar'], 'regname': '*', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1822 \g:event)
1823
1824 let @+ = ''
1825 set clipboard=autoselectplus
1826 exe "norm! ggviw\<Esc>"
1827 call assert_equal(
1828 \{'regcontents': ['foobar'], 'regname': '+', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1829 \g:event)
1830
1831 set clipboard&vim
1832 endif
1833
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001834 au! TextYankPost
1835 unlet g:event
1836 bwipe!
1837endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001838
1839func Test_nocatch_wipe_all_buffers()
1840 " Real nasty autocommand: wipe all buffers on any event.
1841 au * * bwipe *
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001842 call assert_fails('next x', ['E94:', 'E937:'])
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001843 bwipe
1844 au!
1845endfunc
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001846
1847func Test_nocatch_wipe_dummy_buffer()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001848 CheckFeature quickfix
1849 " Nasty autocommand: wipe buffer on any event.
1850 au * x bwipe
Bram Moolenaare2e40752020-09-04 21:18:46 +02001851 call assert_fails('lv½ /x', 'E937:')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001852 au!
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001853endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001854
1855function s:Before_test_dirchanged()
1856 augroup test_dirchanged
1857 autocmd!
1858 augroup END
1859 let s:li = []
1860 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001861 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001862 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001863 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001864 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001865endfunc
1866
1867function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001868 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001869 call delete(s:dir_foo, 'd')
1870 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001871 augroup test_dirchanged
1872 autocmd!
1873 augroup END
1874endfunc
1875
1876function Test_dirchanged_global()
1877 call s:Before_test_dirchanged()
1878 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1879 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001880 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001881 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001882 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001883 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001884 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001885 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001886 call s:After_test_dirchanged()
1887endfunc
1888
1889function Test_dirchanged_local()
1890 call s:Before_test_dirchanged()
1891 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1892 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001893 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001894 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001895 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001896 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001897 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001898 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001899 call s:After_test_dirchanged()
1900endfunc
1901
1902function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001903 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001904 call s:Before_test_dirchanged()
1905 call test_autochdir()
1906 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1907 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1908 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001909 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001910 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001911 exe 'edit ' . s:dir_foo . '/Xfile'
1912 call assert_equal(s:dir_foo, getcwd())
1913 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001914 set noacd
1915 bwipe!
1916 call s:After_test_dirchanged()
1917endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001918
1919" Test TextChangedI and TextChangedP
1920func Test_ChangedP()
1921 new
1922 call setline(1, ['foo', 'bar', 'foobar'])
1923 call test_override("char_avail", 1)
1924 set complete=. completeopt=menuone
1925
1926 func! TextChangedAutocmd(char)
1927 let g:autocmd .= a:char
1928 endfunc
1929
1930 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1931 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1932 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1933
1934 call cursor(3, 1)
1935 let g:autocmd = ''
1936 call feedkeys("o\<esc>", 'tnix')
1937 call assert_equal('I', g:autocmd)
1938
1939 let g:autocmd = ''
1940 call feedkeys("Sf", 'tnix')
1941 call assert_equal('II', g:autocmd)
1942
1943 let g:autocmd = ''
1944 call feedkeys("Sf\<C-N>", 'tnix')
1945 call assert_equal('IIP', g:autocmd)
1946
1947 let g:autocmd = ''
1948 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1949 call assert_equal('IIPP', g:autocmd)
1950
1951 let g:autocmd = ''
1952 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1953 call assert_equal('IIPPP', g:autocmd)
1954
1955 let g:autocmd = ''
1956 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1957 call assert_equal('IIPPPP', g:autocmd)
1958
1959 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1960 " TODO: how should it handle completeopt=noinsert,noselect?
1961
1962 " CleanUp
1963 call test_override("char_avail", 0)
1964 au! TextChanged
1965 au! TextChangedI
1966 au! TextChangedP
1967 delfu TextChangedAutocmd
1968 unlet! g:autocmd
1969 set complete&vim completeopt&vim
1970
1971 bw!
1972endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001973
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001974let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001975func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001976 if !g:setline_handled
1977 call setline(1, "(x)")
1978 let g:setline_handled = v:true
1979 endif
1980endfunc
1981
1982func Test_TextChangedI_with_setline()
1983 new
1984 call test_override('char_avail', 1)
1985 autocmd TextChangedI <buffer> call SetLineOne()
1986 call feedkeys("i(\<CR>\<Esc>", 'tx')
1987 call assert_equal('(', getline(1))
1988 call assert_equal('x)', getline(2))
1989 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001990 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001991 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001992
1993 call test_override('starting', 0)
1994 bwipe!
1995endfunc
1996
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001997func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001998 CheckFeature terminal
1999 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01002000 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01002001 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02002002
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002003 " Prepare file for TextChanged event.
2004 call writefile([''], 'Xchanged.txt')
2005 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
2006 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02002007 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002008 " In ConPTY, there is additional character which is drawn up to the width of
2009 " the screen.
2010 if has('conpty')
2011 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2012 else
2013 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2014 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002015 " It's only adding autocmd, so that no event occurs.
2016 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2017 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002018 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002019 call assert_equal([''], readfile('Xchanged.txt'))
2020
2021 " clean up
2022 call delete('Xchanged.txt')
2023 bwipe!
2024endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002025
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002026func Test_autocmd_nested()
2027 let g:did_nested = 0
2028 augroup Testing
2029 au WinNew * edit somefile
2030 au BufNew * let g:did_nested = 1
2031 augroup END
2032 split
2033 call assert_equal(0, g:did_nested)
2034 close
2035 bwipe! somefile
2036
2037 " old nested argument still works
2038 augroup Testing
2039 au!
2040 au WinNew * nested edit somefile
2041 au BufNew * let g:did_nested = 1
2042 augroup END
2043 split
2044 call assert_equal(1, g:did_nested)
2045 close
2046 bwipe! somefile
2047
2048 " New ++nested argument works
2049 augroup Testing
2050 au!
2051 au WinNew * ++nested edit somefile
2052 au BufNew * let g:did_nested = 1
2053 augroup END
2054 split
2055 call assert_equal(1, g:did_nested)
2056 close
2057 bwipe! somefile
2058
2059 augroup Testing
2060 au!
2061 augroup END
2062
2063 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2064 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2065endfunc
2066
2067func Test_autocmd_once()
2068 " Without ++once WinNew triggers twice
2069 let g:did_split = 0
2070 augroup Testing
2071 au WinNew * let g:did_split += 1
2072 augroup END
2073 split
2074 split
2075 call assert_equal(2, g:did_split)
2076 call assert_true(exists('#WinNew'))
2077 close
2078 close
2079
2080 " With ++once WinNew triggers once
2081 let g:did_split = 0
2082 augroup Testing
2083 au!
2084 au WinNew * ++once let g:did_split += 1
2085 augroup END
2086 split
2087 split
2088 call assert_equal(1, g:did_split)
2089 call assert_false(exists('#WinNew'))
2090 close
2091 close
2092
2093 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2094endfunc
2095
Bram Moolenaara68e5952019-04-25 22:22:01 +02002096func Test_autocmd_bufreadpre()
2097 new
2098 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002099 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002100 w! XAutocmdBufReadPre.txt
2101 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002102 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002103 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002104 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002105 wincmd p
2106 let g:wsv1 = winsaveview()
2107 wincmd p
2108 let g:wsv2 = winsaveview()
2109 " triggers BufReadPre, should not move the cursor in either window
2110 " The topline may change one line in a large window.
2111 edit
2112 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2113 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2114 call assert_equal(2, b:bufreadpre)
2115 wincmd p
2116 call assert_equal(g:wsv1.topline, winsaveview().topline)
2117 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2118 call assert_equal(2, b:bufreadpre)
2119 " Now set the cursor position in an BufReadPre autocommand
2120 " (even though the position will be invalid, this should make Vim reset the
2121 " cursor position in the other window.
2122 wincmd p
2123 set cpo+=g
2124 " won't do anything, but try to set the cursor on an invalid lnum
2125 autocmd BufReadPre <buffer> :norm! 70gg
2126 " triggers BufReadPre, should not move the cursor in either window
2127 e
2128 call assert_equal(1, winsaveview().topline)
2129 call assert_equal(1, winsaveview().lnum)
2130 call assert_equal(3, b:bufreadpre)
2131 wincmd p
2132 call assert_equal(g:wsv1.topline, winsaveview().topline)
2133 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2134 call assert_equal(3, b:bufreadpre)
2135 close
2136 close
2137 call delete('XAutocmdBufReadPre.txt')
2138 set cpo-=g
2139endfunc
2140
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002141" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002142
2143" Tests for the following autocommands:
2144" - FileWritePre writing a compressed file
2145" - FileReadPost reading a compressed file
2146" - BufNewFile reading a file template
2147" - BufReadPre decompressing the file to be read
2148" - FilterReadPre substituting characters in the temp file
2149" - FilterReadPost substituting characters after filtering
2150" - FileReadPre set options for decompression
2151" - FileReadPost decompress the file
2152func Test_ReadWrite_Autocmds()
2153 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002154 CheckUnix
2155 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002156
2157 " Make $GZIP empty, "-v" would cause trouble.
2158 let $GZIP = ""
2159
2160 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2161 " being modified outside of Vim (noticed on Solaris).
2162 au FileChangedShell * echo 'caught FileChangedShell'
2163
2164 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2165 augroup Test1
2166 au!
2167 au FileWritePre *.gz '[,']!gzip
2168 au FileWritePost *.gz undo
2169 au FileReadPost *.gz '[,']!gzip -d
2170 augroup END
2171
2172 new
2173 set bin
2174 call append(0, [
2175 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2176 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2177 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2178 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2179 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2180 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2181 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2182 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2183 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2184 \ ])
2185 1,9write! Xtestfile.gz
2186 enew! | close
2187
2188 new
2189 " Read and decompress the testfile
2190 0read Xtestfile.gz
2191 call assert_equal([
2192 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2193 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2194 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2195 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2196 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2197 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2198 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2199 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2200 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2201 \ ], getline(1, 9))
2202 enew! | close
2203
2204 augroup Test1
2205 au!
2206 augroup END
2207
2208 " Test for the FileAppendPre and FileAppendPost autocmds
2209 augroup Test2
2210 au!
2211 au BufNewFile *.c read Xtest.c
2212 au FileAppendPre *.out '[,']s/new/NEW/
2213 au FileAppendPost *.out !cat Xtest.c >> test.out
2214 augroup END
2215
2216 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2217 new foo.c " should load Xtest.c
2218 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2219 w! >> test.out " append it to the output file
2220
2221 let contents = readfile('test.out')
2222 call assert_equal(' * Here is a NEW .c file', contents[2])
2223 call assert_equal(' * Here is a new .c file', contents[5])
2224
2225 call delete('test.out')
2226 enew! | close
2227 augroup Test2
2228 au!
2229 augroup END
2230
2231 " Test for the BufReadPre and BufReadPost autocmds
2232 augroup Test3
2233 au!
2234 " setup autocommands to decompress before reading and re-compress
2235 " afterwards
2236 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2237 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2238 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2239 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2240 augroup END
2241
2242 e! Xtestfile.gz " Edit compressed file
2243 call assert_equal([
2244 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2245 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2246 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2247 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2248 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2249 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2250 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2251 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2252 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2253 \ ], getline(1, 9))
2254
2255 w! >> test.out " Append it to the output file
2256
2257 augroup Test3
2258 au!
2259 augroup END
2260
2261 " Test for the FilterReadPre and FilterReadPost autocmds.
2262 set shelltemp " need temp files here
2263 augroup Test4
2264 au!
2265 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2266 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2267 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2268 au FilterReadPost *.out '[,']s/x/X/g
2269 augroup END
2270
2271 e! test.out " Edit the output file
2272 1,$!cat
2273 call assert_equal([
2274 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2275 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2276 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2277 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2278 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2279 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2280 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2281 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2282 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2283 \ ], getline(1, 9))
2284 call assert_equal([
2285 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2286 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2287 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2288 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2289 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2290 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2291 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2292 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2293 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2294 \ ], readfile('test.out'))
2295
2296 augroup Test4
2297 au!
2298 augroup END
2299 set shelltemp&vim
2300
2301 " Test for the FileReadPre and FileReadPost autocmds.
2302 augroup Test5
2303 au!
2304 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2305 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2306 au FileReadPost *.gz '[,']s/l/L/
2307 augroup END
2308
2309 new
2310 0r Xtestfile.gz " Read compressed file
2311 call assert_equal([
2312 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2313 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2314 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2315 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2316 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2317 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2318 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2319 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2320 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2321 \ ], getline(1, 9))
2322 call assert_equal([
2323 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2324 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2325 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2326 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2327 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2328 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2329 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2330 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2331 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2332 \ ], readfile('Xtestfile.gz'))
2333
2334 augroup Test5
2335 au!
2336 augroup END
2337
2338 au! FileChangedShell
2339 call delete('Xtestfile.gz')
2340 call delete('Xtest.c')
2341 call delete('test.out')
2342endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002343
2344func Test_throw_in_BufWritePre()
2345 new
2346 call setline(1, ['one', 'two', 'three'])
2347 call assert_false(filereadable('Xthefile'))
2348 augroup throwing
2349 au BufWritePre X* throw 'do not write'
2350 augroup END
2351 try
2352 w Xthefile
2353 catch
2354 let caught = 1
2355 endtry
2356 call assert_equal(1, caught)
2357 call assert_false(filereadable('Xthefile'))
2358
2359 bwipe!
2360 au! throwing
2361endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002362
2363func Test_autocmd_SafeState()
2364 CheckRunVimInTerminal
2365
2366 let lines =<< trim END
2367 let g:safe = 0
2368 let g:again = ''
2369 au SafeState * let g:safe += 1
2370 au SafeStateAgain * let g:again ..= 'x'
2371 func CallTimer()
2372 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2373 endfunc
2374 END
2375 call writefile(lines, 'XSafeState')
2376 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2377
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002378 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002379 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002380 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002381 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002382
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002383 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002384 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002385 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002386
2387 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002388 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002389 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002390 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002391 call term_sendkeys(buf, ":echo g:again\<CR>")
2392 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2393
2394 call StopVimInTerminal(buf)
2395 call delete('XSafeState')
2396endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002397
2398func Test_autocmd_CmdWinEnter()
2399 CheckRunVimInTerminal
2400 " There is not cmdwin switch, so
2401 " test for cmdline_hist
2402 " (both are available with small builds)
2403 CheckFeature cmdline_hist
2404 let lines =<< trim END
2405 let b:dummy_var = 'This is a dummy'
2406 autocmd CmdWinEnter * quit
2407 let winnr = winnr('$')
2408 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002409 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002410 call writefile(lines, filename)
2411 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2412
2413 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002414 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002415 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002416 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002417 call term_sendkeys(buf, ":echo &buftype\<cr>")
2418 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2419 call term_sendkeys(buf, ":echo winnr\<cr>")
2420 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2421
2422 " clean up
2423 call StopVimInTerminal(buf)
2424 call delete(filename)
2425endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002426
2427func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002428 CheckFeature quickfix
2429
Bram Moolenaarec66c412019-10-11 21:19:13 +02002430 pedit xx
2431 n x
2432 au WinEnter * quit
2433 split
2434 au! WinEnter
2435endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002436
2437func Test_BufWrite_lockmarks()
2438 edit! Xtest
2439 call setline(1, ['a', 'b', 'c', 'd'])
2440
2441 " :lockmarks preserves the marks
2442 call SetChangeMarks(2, 3)
2443 lockmarks write
2444 call assert_equal([2, 3], [line("'["), line("']")])
2445
2446 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2447 " original values for the user
2448 augroup lockmarks
2449 au!
2450 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2451 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2452 augroup END
2453
2454 lockmarks write
2455 call assert_equal([2, 3], [line("'["), line("']")])
2456
2457 if executable('cat')
2458 lockmarks %!cat
2459 call assert_equal([2, 3], [line("'["), line("']")])
2460 endif
2461
2462 lockmarks 3,4write Xtest2
2463 call assert_equal([2, 3], [line("'["), line("']")])
2464
2465 au! lockmarks
2466 augroup! lockmarks
2467 call delete('Xtest')
2468 call delete('Xtest2')
2469endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002470
2471func Test_FileType_spell()
2472 if !isdirectory('/tmp')
2473 throw "Skipped: requires /tmp directory"
2474 endif
2475
2476 " this was crashing with an invalid free()
2477 setglobal spellfile=/tmp/en.utf-8.add
2478 augroup crash
2479 autocmd!
2480 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2481 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2482 autocmd FileType anotherfiletype setlocal spell
2483 augroup END
2484 func! NoCrash() abort
2485 edit /tmp/crashfile
2486 endfunc
2487 call NoCrash()
2488
2489 au! crash
2490 setglobal spellfile=
2491endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002492
Bram Moolenaar406cd902020-02-18 21:54:41 +01002493" Test closing a window or editing another buffer from a FileChangedRO handler
2494" in a readonly buffer
2495func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002496 call test_override('ui_delay', 10)
2497
Bram Moolenaar406cd902020-02-18 21:54:41 +01002498 augroup FileChangedROTest
2499 au!
2500 autocmd FileChangedRO * quit
2501 augroup END
2502 new
2503 set readonly
2504 call assert_fails('normal i', 'E788:')
2505 close
2506 augroup! FileChangedROTest
2507
2508 augroup FileChangedROTest
2509 au!
2510 autocmd FileChangedRO * edit Xfile
2511 augroup END
2512 new
2513 set readonly
2514 call assert_fails('normal i', 'E788:')
2515 close
2516 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002517 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002518endfunc
2519
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002520func LogACmd()
2521 call add(g:logged, line('$'))
2522endfunc
2523
2524func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002525 CheckNotGui
2526
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002527 enew!
2528 tabnew
2529 call setline(1, ['a', 'b', 'c', 'd'])
2530 $
2531 au TermChanged * call LogACmd()
2532 let g:logged = []
2533 let term_save = &term
2534 set term=xterm
2535 call assert_equal([1, 4], g:logged)
2536
2537 au! TermChanged
2538 let &term = term_save
2539 bwipe!
2540endfunc
2541
Bram Moolenaare3284872020-03-19 13:55:03 +01002542" Test for FileReadCmd autocmd
2543func Test_autocmd_FileReadCmd()
2544 func ReadFileCmd()
2545 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2546 endfunc
2547 augroup FileReadCmdTest
2548 au!
2549 au FileReadCmd Xtest call ReadFileCmd()
2550 augroup END
2551
2552 new
2553 read ++bin Xtest
2554 read ++nobin Xtest
2555 read ++edit Xtest
2556 read ++bad=keep Xtest
2557 read ++bad=drop Xtest
2558 read ++bad=- Xtest
2559 read ++ff=unix Xtest
2560 read ++ff=dos Xtest
2561 read ++ff=mac Xtest
2562 read ++enc=utf-8 Xtest
2563
2564 call assert_equal(['',
2565 \ 'v:cmdarg = ++bin',
2566 \ 'v:cmdarg = ++nobin',
2567 \ 'v:cmdarg = ++edit',
2568 \ 'v:cmdarg = ++bad=keep',
2569 \ 'v:cmdarg = ++bad=drop',
2570 \ 'v:cmdarg = ++bad=-',
2571 \ 'v:cmdarg = ++ff=unix',
2572 \ 'v:cmdarg = ++ff=dos',
2573 \ 'v:cmdarg = ++ff=mac',
2574 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2575
2576 close!
2577 augroup FileReadCmdTest
2578 au!
2579 augroup END
2580 delfunc ReadFileCmd
2581endfunc
2582
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002583" Test for passing invalid arguments to autocmd
2584func Test_autocmd_invalid_args()
2585 " Additional character after * for event
2586 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2587 augroup Test
2588 augroup END
2589 " Invalid autocmd event
2590 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2591 " Invalid autocmd event in a autocmd group
2592 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2593 augroup! Test
2594 " Execute all autocmds
2595 call assert_fails('doautocmd * BufEnter', 'E217:')
2596 call assert_fails('augroup! x1a2b3', 'E367:')
2597 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002598 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002599endfunc
2600
2601" Test for deep nesting of autocmds
2602func Test_autocmd_deep_nesting()
2603 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2604 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2605 autocmd! BufEnter Xfile
2606endfunc
2607
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002608" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2609func Test_autocmd_sigusr1()
2610 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002611 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002612
2613 let g:sigusr1_passed = 0
2614 au SigUSR1 * let g:sigusr1_passed = 1
2615 call system('/bin/kill -s usr1 ' . getpid())
2616 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2617
2618 au! SigUSR1
2619 unlet g:sigusr1_passed
2620endfunc
2621
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002622" Test for BufReadPre autocmd deleting the file
2623func Test_BufReadPre_delfile()
2624 augroup TestAuCmd
2625 au!
2626 autocmd BufReadPre Xfile call delete('Xfile')
2627 augroup END
2628 call writefile([], 'Xfile')
2629 call assert_fails('new Xfile', 'E200:')
2630 call assert_equal('Xfile', @%)
2631 call assert_equal(1, &readonly)
2632 call delete('Xfile')
2633 augroup TestAuCmd
2634 au!
2635 augroup END
2636 close!
2637endfunc
2638
2639" Test for BufReadPre autocmd changing the current buffer
2640func Test_BufReadPre_changebuf()
2641 augroup TestAuCmd
2642 au!
2643 autocmd BufReadPre Xfile edit Xsomeotherfile
2644 augroup END
2645 call writefile([], 'Xfile')
2646 call assert_fails('new Xfile', 'E201:')
2647 call assert_equal('Xsomeotherfile', @%)
2648 call assert_equal(1, &readonly)
2649 call delete('Xfile')
2650 augroup TestAuCmd
2651 au!
2652 augroup END
2653 close!
2654endfunc
2655
2656" Test for BufWipeouti autocmd changing the current buffer when reading a file
2657" in an empty buffer with 'f' flag in 'cpo'
2658func Test_BufDelete_changebuf()
2659 new
2660 augroup TestAuCmd
2661 au!
2662 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2663 augroup END
2664 let save_cpo = &cpo
2665 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002666 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002667 call assert_equal('somefile', @%)
2668 let &cpo = save_cpo
2669 augroup TestAuCmd
2670 au!
2671 augroup END
2672 close!
2673endfunc
2674
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002675" Test for the temporary internal window used to execute autocmds
2676func Test_autocmd_window()
2677 %bw!
2678 edit one.txt
2679 tabnew two.txt
2680 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002681 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002682 au!
2683 au BufEnter * call add(g:blist, [expand('<afile>'),
2684 \ win_gettype(bufwinnr(expand('<afile>')))])
2685 augroup END
2686
2687 doautoall BufEnter
Bram Moolenaar40a019f2020-06-17 21:41:35 +02002688 call assert_equal([['one.txt', 'autocmd'], ['two.txt', '']], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002689
Bram Moolenaar832adf92020-06-25 19:01:36 +02002690 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002691 au!
2692 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002693 augroup! aucmd_win_test1
2694 %bw!
2695endfunc
2696
2697" Test for trying to close the temporary window used for executing an autocmd
2698func Test_close_autocmd_window()
2699 %bw!
2700 edit one.txt
2701 tabnew two.txt
2702 augroup aucmd_win_test2
2703 au!
2704 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2705 augroup END
2706
2707 call assert_fails('doautoall BufEnter', 'E813:')
2708
2709 augroup aucmd_win_test2
2710 au!
2711 augroup END
2712 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002713 %bwipe!
2714endfunc
2715
2716" Test for trying to close the tab that has the temporary window for exeucing
2717" an autocmd.
2718func Test_close_autocmd_tab()
2719 edit one.txt
2720 tabnew two.txt
2721 augroup aucmd_win_test
2722 au!
2723 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2724 augroup END
2725
2726 call assert_fails('doautoall BufEnter', 'E813:')
2727
2728 tabonly
2729 augroup aucmd_win_test
2730 au!
2731 augroup END
2732 augroup! aucmd_win_test
2733 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002734endfunc
2735
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002736" vim: shiftwidth=2 sts=2 expandtab