blob: 6f8c973d7fc4cc22e60e8dc049f0574e345552ea [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()
506 let content =<< trim [CODE]
507 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast
508 edit foo1
509 au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball
510 edit foo2
511 call writefile(['OK'], 'Xerrors')
512 qall
513 [CODE]
514
515 call writefile(content, 'XblastBall')
516 call system(GetVimCommand() .. ' --clean -S XblastBall')
517 call assert_match('OK', readfile('Xerrors')->join())
518
519 call delete('XblastBall')
520 call delete('Xerrors')
521endfunc
522
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100523" SEGV occurs in older versions.
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200524func Test_autocmd_bufwipe_in_SessLoadPost2()
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100525 tabnew
526 set noswapfile
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100527 mksession!
528
Bram Moolenaarc79745a2019-05-20 22:12:34 +0200529 let content =<< trim [CODE]
530 set nocp noswapfile
531 function! DeleteInactiveBufs()
532 tabfirst
533 let tabblist = []
534 for i in range(1, tabpagenr(''$''))
535 call extend(tabblist, tabpagebuflist(i))
536 endfor
537 for b in range(1, bufnr(''$''))
538 if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')
539 exec ''bwipeout '' . b
540 endif
541 endfor
542 echomsg "SessionLoadPost DONE"
543 endfunction
544 au SessionLoadPost * call DeleteInactiveBufs()
545
546 func WriteErrors()
547 call writefile([execute("messages")], "Xerrors")
548 endfunc
549 au VimLeave * call WriteErrors()
550 [CODE]
551
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100552 call writefile(content, 'Xvimrc')
Bram Moolenaar93344c22019-08-14 21:12:05 +0200553 call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq')
Bram Moolenaare94260f2017-03-21 15:50:12 +0100554 let errors = join(readfile('Xerrors'))
555 " This probably only ever matches on unix.
556 call assert_notmatch('Caught deadly signal SEGV', errors)
557 call assert_match('SessionLoadPost DONE', errors)
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100558
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100559 set swapfile
Bram Moolenaare94260f2017-03-21 15:50:12 +0100560 for file in ['Session.vim', 'Xvimrc', 'Xerrors']
Bram Moolenaar8c752bd2017-03-19 17:09:56 +0100561 call delete(file)
562 endfor
563endfunc
Bram Moolenaarfaf29d72017-07-09 11:07:16 +0200564
565func Test_empty_doau()
566 doau \|
567endfunc
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200568
569func s:AutoCommandOptionSet(match)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200570 let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200571 let item = remove(g:options, 0)
Bram Moolenaard7c96872019-06-15 17:12:48 +0200572 let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6])
573 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 +0200574 let g:opt = [expected, actual]
575 "call assert_equal(expected, actual)
576endfunc
577
578func Test_OptionSet()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200579 CheckOption autochdir
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200580
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200581 badd test_autocmd.vim
582
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200583 call test_override('starting', 1)
584 set nocp
585 au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>"))
586
587 " 1: Setting number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100588 let g:options = [['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200589 set nu
590 call assert_equal([], g:options)
591 call assert_equal(g:opt[0], g:opt[1])
592
593 " 2: Setting local number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100594 let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200595 setlocal nonu
596 call assert_equal([], g:options)
597 call assert_equal(g:opt[0], g:opt[1])
598
599 " 3: Setting global number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100600 let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200601 setglobal nonu
602 call assert_equal([], g:options)
603 call assert_equal(g:opt[0], g:opt[1])
604
605 " 4: Setting local autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100606 let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200607 setlocal ai
608 call assert_equal([], g:options)
609 call assert_equal(g:opt[0], g:opt[1])
610
611 " 5: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100612 let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200613 setglobal ai
614 call assert_equal([], g:options)
615 call assert_equal(g:opt[0], g:opt[1])
616
617 " 6: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100618 let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200619 set ai!
620 call assert_equal([], g:options)
621 call assert_equal(g:opt[0], g:opt[1])
622
623 " 6a: Setting global autoindent option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100624 let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200625 noa setlocal ai
626 noa setglobal noai
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200627 set ai!
628 call assert_equal([], g:options)
629 call assert_equal(g:opt[0], g:opt[1])
630
631 " Should not print anything, use :noa
632 " 7: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100633 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200634 noa set nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200635 call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200636 call assert_equal(g:opt[0], g:opt[1])
637
638 " 8: Setting several global list and number option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100639 let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200640 set list nu
641 call assert_equal([], g:options)
642 call assert_equal(g:opt[0], g:opt[1])
643
644 " 9: don't trigger OptionSet"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100645 let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200646 noa set nolist nonu
Bram Moolenaard7c96872019-06-15 17:12:48 +0200647 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 +0200648 call assert_equal(g:opt[0], g:opt[1])
649
650 " 10: Setting global acd"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100651 let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200652 setlocal acd
653 call assert_equal([], g:options)
654 call assert_equal(g:opt[0], g:opt[1])
655
656 " 11: Setting global autoread (also sets local value)"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100657 let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200658 set ar
659 call assert_equal([], g:options)
660 call assert_equal(g:opt[0], g:opt[1])
661
662 " 12: Setting local autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100663 let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200664 setlocal ar
665 call assert_equal([], g:options)
666 call assert_equal(g:opt[0], g:opt[1])
667
668 " 13: Setting global autoread"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100669 let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200670 setglobal invar
671 call assert_equal([], g:options)
672 call assert_equal(g:opt[0], g:opt[1])
673
674 " 14: Setting option backspace through :let"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100675 let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']]
676 let &bs = "eol,indent,start"
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200677 call assert_equal([], g:options)
678 call assert_equal(g:opt[0], g:opt[1])
679
680 " 15: Setting option backspace through setbufvar()"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100681 let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200682 " try twice, first time, shouldn't trigger because option name is invalid,
683 " second time, it should trigger
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200684 let bnum = bufnr('%')
Bram Moolenaare2e40752020-09-04 21:18:46 +0200685 call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200686 " should trigger, use correct option name
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +0200687 call setbufvar(bnum, '&backup', 1)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200688 call assert_equal([], g:options)
689 call assert_equal(g:opt[0], g:opt[1])
690
691 " 16: Setting number option using setwinvar"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100692 let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200693 call setwinvar(0, '&number', 1)
694 call assert_equal([], g:options)
695 call assert_equal(g:opt[0], g:opt[1])
696
697 " 17: Setting key option, shouldn't trigger"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100698 let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']]
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200699 setlocal key=blah
700 setlocal key=
Bram Moolenaard7c96872019-06-15 17:12:48 +0200701 call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options)
Bram Moolenaar04f62f82017-07-19 18:18:39 +0200702 call assert_equal(g:opt[0], g:opt[1])
703
Bram Moolenaard7c96872019-06-15 17:12:48 +0200704
705 " 18a: Setting string global option"
706 let oldval = &backupext
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100707 let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200708 set backupext=foo
709 call assert_equal([], g:options)
710 call assert_equal(g:opt[0], g:opt[1])
711
712 " 18b: Resetting string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100713 let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200714 set backupext&
715 call assert_equal([], g:options)
716 call assert_equal(g:opt[0], g:opt[1])
717
718 " 18c: Setting global string global option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100719 let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200720 setglobal backupext=bar
721 call assert_equal([], g:options)
722 call assert_equal(g:opt[0], g:opt[1])
723
724 " 18d: Setting local string global option"
725 " As this is a global option this sets the global value even though
726 " :setlocal is used!
727 noa set backupext& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100728 let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200729 setlocal backupext=baz
730 call assert_equal([], g:options)
731 call assert_equal(g:opt[0], g:opt[1])
732
733 " 18e: Setting again string global option"
734 noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd)
735 noa setlocal backupext=ext_local " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100736 let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200737 set backupext=fuu
738 call assert_equal([], g:options)
739 call assert_equal(g:opt[0], g:opt[1])
740
741
742 " 19a: Setting string local-global (to buffer) option"
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200743 let oldval = &tags
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100744 let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200745 set tags=tagpath
746 call assert_equal([], g:options)
747 call assert_equal(g:opt[0], g:opt[1])
748
Bram Moolenaard7c96872019-06-15 17:12:48 +0200749 " 19b: Resetting string local-global (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100750 let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']]
Bram Moolenaar8efa0262017-08-20 15:47:20 +0200751 set tags&
752 call assert_equal([], g:options)
753 call assert_equal(g:opt[0], g:opt[1])
754
Bram Moolenaard7c96872019-06-15 17:12:48 +0200755 " 19c: Setting global string local-global (to buffer) option "
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100756 let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200757 setglobal tags=tagpath1
758 call assert_equal([], g:options)
759 call assert_equal(g:opt[0], g:opt[1])
760
761 " 19d: Setting local string local-global (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100762 let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200763 setlocal tags=tagpath2
764 call assert_equal([], g:options)
765 call assert_equal(g:opt[0], g:opt[1])
766
767 " 19e: Setting again string local-global (to buffer) option"
768 " Note: v:option_old is the old global value for local-global string options
769 " but the old local value for all other kinds of options.
770 noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd)
771 noa setlocal tags=tag_local
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100772 let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200773 set tags=tagpath
774 call assert_equal([], g:options)
775 call assert_equal(g:opt[0], g:opt[1])
776
777 " 19f: Setting string local-global (to buffer) option to an empty string"
778 " Note: v:option_old is the old global value for local-global string options
779 " but the old local value for all other kinds of options.
780 noa set tags=tag_global " Reset global and local value (without triggering autocmd)
781 noa setlocal tags= " empty string
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100782 let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200783 set tags=tagpath
784 call assert_equal([], g:options)
785 call assert_equal(g:opt[0], g:opt[1])
786
787
788 " 20a: Setting string local (to buffer) option"
789 let oldval = &spelllang
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100790 let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200791 set spelllang=elvish,klingon
792 call assert_equal([], g:options)
793 call assert_equal(g:opt[0], g:opt[1])
794
795 " 20b: Resetting string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100796 let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200797 set spelllang&
798 call assert_equal([], g:options)
799 call assert_equal(g:opt[0], g:opt[1])
800
801 " 20c: Setting global string local (to buffer) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100802 let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200803 setglobal spelllang=elvish
804 call assert_equal([], g:options)
805 call assert_equal(g:opt[0], g:opt[1])
806
807 " 20d: Setting local string local (to buffer) option"
808 noa set spelllang& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100809 let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200810 setlocal spelllang=klingon
811 call assert_equal([], g:options)
812 call assert_equal(g:opt[0], g:opt[1])
813
814 " 20e: Setting again string local (to buffer) option"
815 " Note: v:option_old is the old global value for local-global string options
816 " but the old local value for all other kinds of options.
817 noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd)
818 noa setlocal spelllang=spelllocal
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100819 let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200820 set spelllang=foo
821 call assert_equal([], g:options)
822 call assert_equal(g:opt[0], g:opt[1])
823
824
825 " 21a: Setting string local-global (to window) option"
826 let oldval = &statusline
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100827 let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200828 set statusline=foo
829 call assert_equal([], g:options)
830 call assert_equal(g:opt[0], g:opt[1])
831
832 " 21b: Resetting string local-global (to window) option"
833 " Note: v:option_old is the old global value for local-global string options
834 " but the old local value for all other kinds of options.
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100835 let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200836 set statusline&
837 call assert_equal([], g:options)
838 call assert_equal(g:opt[0], g:opt[1])
839
840 " 21c: Setting global string local-global (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100841 let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200842 setglobal statusline=bar
843 call assert_equal([], g:options)
844 call assert_equal(g:opt[0], g:opt[1])
845
846 " 21d: Setting local string local-global (to window) option"
847 noa set statusline& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100848 let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200849 setlocal statusline=baz
850 call assert_equal([], g:options)
851 call assert_equal(g:opt[0], g:opt[1])
852
853 " 21e: Setting again string local-global (to window) option"
854 " Note: v:option_old is the old global value for local-global string options
855 " but the old local value for all other kinds of options.
856 noa setglobal statusline=bar " Reset global and local value (without triggering autocmd)
857 noa setlocal statusline=baz
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100858 let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200859 set statusline=foo
860 call assert_equal([], g:options)
861 call assert_equal(g:opt[0], g:opt[1])
862
863
864 " 22a: Setting string local (to window) option"
865 let oldval = &foldignore
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100866 let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200867 set foldignore=fo
868 call assert_equal([], g:options)
869 call assert_equal(g:opt[0], g:opt[1])
870
871 " 22b: Resetting string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100872 let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200873 set foldignore&
874 call assert_equal([], g:options)
875 call assert_equal(g:opt[0], g:opt[1])
876
877 " 22c: Setting global string local (to window) option"
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100878 let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200879 setglobal foldignore=bar
880 call assert_equal([], g:options)
881 call assert_equal(g:opt[0], g:opt[1])
882
883 " 22d: Setting local string local (to window) option"
884 noa set foldignore& " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100885 let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200886 setlocal foldignore=baz
887 call assert_equal([], g:options)
888 call assert_equal(g:opt[0], g:opt[1])
889
890 " 22e: Setting again string local (to window) option"
891 noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd)
892 noa setlocal foldignore=loc
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100893 let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200894 set foldignore=fo
895 call assert_equal([], g:options)
896 call assert_equal(g:opt[0], g:opt[1])
897
898
899 " 23a: Setting global number local option"
900 noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd)
901 noa setlocal cmdheight=1 " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100902 let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200903 setglobal cmdheight=2
904 call assert_equal([], g:options)
905 call assert_equal(g:opt[0], g:opt[1])
906
907 " 23b: Setting local number global 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', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200911 setlocal cmdheight=2
912 call assert_equal([], g:options)
913 call assert_equal(g:opt[0], g:opt[1])
914
915 " 23c: Setting again 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', '1', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200919 set cmdheight=2
920 call assert_equal([], g:options)
921 call assert_equal(g:opt[0], g:opt[1])
922
923 " 23d: Setting again number global option"
924 noa set cmdheight=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100925 let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200926 set cmdheight=2
927 call assert_equal([], g:options)
928 call assert_equal(g:opt[0], g:opt[1])
929
930
931 " 24a: Setting global number global-local (to buffer) option"
932 noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd)
933 noa setlocal undolevels=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100934 let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200935 setglobal undolevels=2
936 call assert_equal([], g:options)
937 call assert_equal(g:opt[0], g:opt[1])
938
939 " 24b: Setting local 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', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200943 setlocal undolevels=2
944 call assert_equal([], g:options)
945 call assert_equal(g:opt[0], g:opt[1])
946
947 " 24c: Setting again 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', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200951 set undolevels=2
952 call assert_equal([], g:options)
953 call assert_equal(g:opt[0], g:opt[1])
954
955 " 24d: Setting again global number global-local (to buffer) option"
956 noa set undolevels=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100957 let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200958 set undolevels=2
959 call assert_equal([], g:options)
960 call assert_equal(g:opt[0], g:opt[1])
961
962
963 " 25a: Setting global number local (to buffer) option"
964 noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd)
965 noa setlocal wrapmargin=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100966 let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200967 setglobal wrapmargin=2
968 call assert_equal([], g:options)
969 call assert_equal(g:opt[0], g:opt[1])
970
971 " 25b: Setting local 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', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200975 setlocal wrapmargin=2
976 call assert_equal([], g:options)
977 call assert_equal(g:opt[0], g:opt[1])
978
979 " 25c: Setting again 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', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200983 set wrapmargin=2
984 call assert_equal([], g:options)
985 call assert_equal(g:opt[0], g:opt[1])
986
987 " 25d: Setting again global number local (to buffer) option"
988 noa set wrapmargin=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +0100989 let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +0200990 set wrapmargin=2
991 call assert_equal([], g:options)
992 call assert_equal(g:opt[0], g:opt[1])
993
994
995 " 26: Setting number global-local (to window) option.
996 " Such option does currently not exist.
997
998
999 " 27a: Setting global number local (to window) option"
1000 noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd)
1001 noa setlocal foldcolumn=1
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001002 let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001003 setglobal foldcolumn=2
1004 call assert_equal([], g:options)
1005 call assert_equal(g:opt[0], g:opt[1])
1006
1007 " 27b: Setting local 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', '1', '1', '', '2', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001011 setlocal foldcolumn=2
1012 call assert_equal([], g:options)
1013 call assert_equal(g:opt[0], g:opt[1])
1014
1015 " 27c: Setting again 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', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001019 set foldcolumn=2
1020 call assert_equal([], g:options)
1021 call assert_equal(g:opt[0], g:opt[1])
1022
1023 " 27d: Ssettin again global number local (to window) option"
1024 noa set foldcolumn=8 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001025 let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001026 set foldcolumn=2
1027 call assert_equal([], g:options)
1028 call assert_equal(g:opt[0], g:opt[1])
1029
1030
1031 " 28a: Setting global boolean global option"
1032 noa setglobal nowrapscan " Reset global and local value (without triggering autocmd)
1033 noa setlocal wrapscan " Sets the global(!) value!
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001034 let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001035 setglobal nowrapscan
1036 call assert_equal([], g:options)
1037 call assert_equal(g:opt[0], g:opt[1])
1038
1039 " 28b: Setting local 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', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001043 setlocal nowrapscan
1044 call assert_equal([], g:options)
1045 call assert_equal(g:opt[0], g:opt[1])
1046
1047 " 28c: Setting again 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', '1', '0', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001051 set nowrapscan
1052 call assert_equal([], g:options)
1053 call assert_equal(g:opt[0], g:opt[1])
1054
1055 " 28d: Setting again global boolean global option"
1056 noa set nowrapscan " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001057 let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001058 set wrapscan
1059 call assert_equal([], g:options)
1060 call assert_equal(g:opt[0], g:opt[1])
1061
1062
1063 " 29a: Setting global boolean global-local (to buffer) option"
1064 noa setglobal noautoread " Reset global and local value (without triggering autocmd)
1065 noa setlocal autoread
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001066 let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001067 setglobal autoread
1068 call assert_equal([], g:options)
1069 call assert_equal(g:opt[0], g:opt[1])
1070
1071 " 29b: Setting local 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', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001075 setlocal noautoread
1076 call assert_equal([], g:options)
1077 call assert_equal(g:opt[0], g:opt[1])
1078
1079 " 29c: Setting again 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', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001083 set autoread
1084 call assert_equal([], g:options)
1085 call assert_equal(g:opt[0], g:opt[1])
1086
1087 " 29d: Setting again global boolean global-local (to buffer) option"
1088 noa set noautoread " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001089 let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001090 set autoread
1091 call assert_equal([], g:options)
1092 call assert_equal(g:opt[0], g:opt[1])
1093
1094
1095 " 30a: Setting global boolean local (to buffer) option"
1096 noa setglobal nocindent " Reset global and local value (without triggering autocmd)
1097 noa setlocal cindent
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001098 let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001099 setglobal cindent
1100 call assert_equal([], g:options)
1101 call assert_equal(g:opt[0], g:opt[1])
1102
1103 " 30b: Setting local 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', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001107 setlocal nocindent
1108 call assert_equal([], g:options)
1109 call assert_equal(g:opt[0], g:opt[1])
1110
1111 " 30c: Setting again 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', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001115 set cindent
1116 call assert_equal([], g:options)
1117 call assert_equal(g:opt[0], g:opt[1])
1118
1119 " 30d: Setting again global boolean local (to buffer) option"
1120 noa set nocindent " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001121 let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001122 set cindent
1123 call assert_equal([], g:options)
1124 call assert_equal(g:opt[0], g:opt[1])
1125
1126
1127 " 31: Setting boolean global-local (to window) option
1128 " Currently no such option exists.
1129
1130
1131 " 32a: Setting global boolean local (to window) option"
1132 noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd)
1133 noa setlocal cursorcolumn
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001134 let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001135 setglobal cursorcolumn
1136 call assert_equal([], g:options)
1137 call assert_equal(g:opt[0], g:opt[1])
1138
1139 " 32b: Setting local 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', '1', '1', '', '0', 'local', 'setlocal']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001143 setlocal nocursorcolumn
1144 call assert_equal([], g:options)
1145 call assert_equal(g:opt[0], g:opt[1])
1146
1147 " 32c: Setting again 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', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001151 set cursorcolumn
1152 call assert_equal([], g:options)
1153 call assert_equal(g:opt[0], g:opt[1])
1154
1155 " 32d: Setting again global boolean local (to window) option"
1156 noa set nocursorcolumn " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001157 let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001158 set cursorcolumn
1159 call assert_equal([], g:options)
1160 call assert_equal(g:opt[0], g:opt[1])
1161
1162
Bram Moolenaar1bc353b2019-09-01 14:45:28 +02001163 " 33: Test autocommands when an option value is converted internally.
Bram Moolenaard7c96872019-06-15 17:12:48 +02001164 noa set backspace=1 " Reset global and local value (without triggering autocmd)
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001165 let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']]
Bram Moolenaard7c96872019-06-15 17:12:48 +02001166 set backspace=2
1167 call assert_equal([], g:options)
1168 call assert_equal(g:opt[0], g:opt[1])
1169
1170
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001171 " Cleanup
1172 au! OptionSet
Bram Moolenaar0331faf2019-06-15 18:40:37 +02001173 " set tags&
Bram Moolenaard7c96872019-06-15 17:12:48 +02001174 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 +02001175 exe printf(":set %s&vim", opt)
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001176 endfor
1177 call test_override('starting', 0)
1178 delfunc! AutoCommandOptionSet
1179endfunc
1180
1181func Test_OptionSet_diffmode()
1182 call test_override('starting', 1)
Bram Moolenaar26d98212019-01-27 22:32:55 +01001183 " 18: Changing an option when entering diff mode
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001184 new
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01001185 au OptionSet diff :let &l:cul = v:option_new
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001186
1187 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
1188 call assert_equal(0, &l:cul)
1189 diffthis
1190 call assert_equal(1, &l:cul)
1191
1192 vnew
1193 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
1194 call assert_equal(0, &l:cul)
1195 diffthis
1196 call assert_equal(1, &l:cul)
1197
1198 diffoff
1199 call assert_equal(0, &l:cul)
1200 call assert_equal(1, getwinvar(2, '&l:cul'))
1201 bw!
1202
1203 call assert_equal(1, &l:cul)
1204 diffoff!
1205 call assert_equal(0, &l:cul)
1206 call assert_equal(0, getwinvar(1, '&l:cul'))
1207 bw!
1208
1209 " Cleanup
1210 au! OptionSet
1211 call test_override('starting', 0)
1212endfunc
1213
1214func Test_OptionSet_diffmode_close()
1215 call test_override('starting', 1)
1216 " 19: Try to close the current window when entering diff mode
1217 " should not segfault
1218 new
1219 au OptionSet diff close
1220
1221 call setline(1, ['buffer 1', 'line2', 'line3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001222 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001223 call assert_equal(1, &diff)
1224 vnew
1225 call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4'])
Bram Moolenaare2e40752020-09-04 21:18:46 +02001226 call assert_fails(':diffthis', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001227 call assert_equal(1, &diff)
Bram Moolenaara9aa86f2019-11-10 21:25:45 +01001228 set diffopt-=closeoff
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001229 bw!
Bram Moolenaare2e40752020-09-04 21:18:46 +02001230 call assert_fails(':diffoff!', 'E788:')
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001231 bw!
1232
1233 " Cleanup
1234 au! OptionSet
1235 call test_override('starting', 0)
1236 "delfunc! AutoCommandOptionSet
1237endfunc
Bram Moolenaar4a137b42017-08-04 22:37:11 +02001238
1239" Test for Bufleave autocommand that deletes the buffer we are about to edit.
1240func Test_BufleaveWithDelete()
1241 new | edit Xfile1
1242
1243 augroup test_bufleavewithdelete
1244 autocmd!
1245 autocmd BufLeave Xfile1 bwipe Xfile2
1246 augroup END
1247
1248 call assert_fails('edit Xfile2', 'E143:')
1249 call assert_equal('Xfile1', bufname('%'))
1250
1251 autocmd! test_bufleavewithdelete BufLeave Xfile1
1252 augroup! test_bufleavewithdelete
1253
1254 new
1255 bwipe! Xfile1
1256endfunc
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001257
1258" Test for autocommand that changes the buffer list, when doing ":ball".
1259func Test_Acmd_BufAll()
1260 enew!
1261 %bwipe!
1262 call writefile(['Test file Xxx1'], 'Xxx1')
1263 call writefile(['Test file Xxx2'], 'Xxx2')
1264 call writefile(['Test file Xxx3'], 'Xxx3')
1265
1266 " Add three files to the buffer list
1267 split Xxx1
1268 close
1269 split Xxx2
1270 close
1271 split Xxx3
1272 close
1273
1274 " Wipe the buffer when the buffer is opened
1275 au BufReadPost Xxx2 bwipe
1276
1277 call append(0, 'Test file Xxx4')
1278 ball
1279
1280 call assert_equal(2, winnr('$'))
1281 call assert_equal('Xxx1', bufname(winbufnr(winnr('$'))))
1282 wincmd t
1283
1284 au! BufReadPost
1285 %bwipe!
1286 call delete('Xxx1')
1287 call delete('Xxx2')
1288 call delete('Xxx3')
1289 enew! | only
1290endfunc
1291
1292" Test for autocommand that changes current buffer on BufEnter event.
1293" Check if modelines are interpreted for the correct buffer.
1294func Test_Acmd_BufEnter()
1295 %bwipe!
1296 call writefile(['start of test file Xxx1',
1297 \ "\<Tab>this is a test",
1298 \ 'end of test file Xxx1'], 'Xxx1')
1299 call writefile(['start of test file Xxx2',
1300 \ 'vim: set noai :',
1301 \ "\<Tab>this is a test",
1302 \ 'end of test file Xxx2'], 'Xxx2')
1303
1304 au BufEnter Xxx2 brew
1305 set ai modeline modelines=3
1306 edit Xxx1
1307 " edit Xxx2, autocmd will do :brew
1308 edit Xxx2
1309 exe "normal G?this is a\<CR>"
1310 " Append text with autoindent to this file
1311 normal othis should be auto-indented
1312 call assert_equal("\<Tab>this should be auto-indented", getline('.'))
1313 call assert_equal(3, line('.'))
1314 " Remove autocmd and edit Xxx2 again
1315 au! BufEnter Xxx2
1316 buf! Xxx2
1317 exe "normal G?this is a\<CR>"
1318 " append text without autoindent to Xxx
1319 normal othis should be in column 1
1320 call assert_equal("this should be in column 1", getline('.'))
1321 call assert_equal(4, line('.'))
1322
1323 %bwipe!
1324 call delete('Xxx1')
1325 call delete('Xxx2')
1326 set ai&vim modeline&vim modelines&vim
1327endfunc
1328
1329" Test for issue #57
1330" do not move cursor on <c-o> when autoindent is set
1331func Test_ai_CTRL_O()
1332 enew!
1333 set ai
1334 let save_fo = &fo
1335 set fo+=r
1336 exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>"
1337 exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>"
1338 call assert_equal(['# abc', 'def', 'def'], getline(2, 4))
1339
1340 set ai&vim
1341 let &fo = save_fo
1342 enew!
1343endfunc
1344
1345" Test for autocommand that deletes the current buffer on BufLeave event.
1346" Also test deleting the last buffer, should give a new, empty buffer.
1347func Test_BufLeave_Wipe()
1348 %bwipe!
1349 let content = ['start of test file Xxx',
1350 \ 'this is a test',
1351 \ 'end of test file Xxx']
1352 call writefile(content, 'Xxx1')
1353 call writefile(content, 'Xxx2')
1354
1355 au BufLeave Xxx2 bwipe
1356 edit Xxx1
1357 split Xxx2
1358 " delete buffer Xxx2, we should be back to Xxx1
1359 bwipe
1360 call assert_equal('Xxx1', bufname('%'))
1361 call assert_equal(1, winnr('$'))
1362
1363 " Create an alternate buffer
1364 %write! test.out
1365 call assert_equal('test.out', bufname('#'))
1366 " delete alternate buffer
1367 bwipe test.out
1368 call assert_equal('Xxx1', bufname('%'))
1369 call assert_equal('', bufname('#'))
1370
1371 au BufLeave Xxx1 bwipe
1372 " delete current buffer, get an empty one
1373 bwipe!
1374 call assert_equal(1, line('$'))
1375 call assert_equal('', bufname('%'))
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001376 let g:bufinfo = getbufinfo()
1377 call assert_equal(1, len(g:bufinfo))
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001378
1379 call delete('Xxx1')
1380 call delete('Xxx2')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001381 call delete('test.out')
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001382 %bwipe
1383 au! BufLeave
Bram Moolenaarb2c87502017-10-14 21:15:58 +02001384
1385 " check that bufinfo doesn't contain a pointer to freed memory
1386 call test_garbagecollect_now()
Bram Moolenaar4a6fcf82017-10-12 21:29:22 +02001387endfunc
Bram Moolenaar87ffb5c2017-10-19 12:37:42 +02001388
1389func Test_QuitPre()
1390 edit Xfoo
1391 let winid = win_getid(winnr())
1392 split Xbar
1393 au! QuitPre * let g:afile = expand('<afile>')
1394 " Close the other window, <afile> should be correct.
1395 exe win_id2win(winid) . 'q'
1396 call assert_equal('Xfoo', g:afile)
1397
1398 unlet g:afile
1399 bwipe Xfoo
1400 bwipe Xbar
1401endfunc
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001402
1403func Test_Cmdline()
Bram Moolenaar153b7042018-01-31 15:48:32 +01001404 au! CmdlineChanged : let g:text = getcmdline()
1405 let g:text = 0
1406 call feedkeys(":echom 'hello'\<CR>", 'xt')
1407 call assert_equal("echom 'hello'", g:text)
1408 au! CmdlineChanged
1409
1410 au! CmdlineChanged : let g:entered = expand('<afile>')
1411 let g:entered = 0
1412 call feedkeys(":echom 'hello'\<CR>", 'xt')
1413 call assert_equal(':', g:entered)
1414 au! CmdlineChanged
1415
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001416 au! CmdlineEnter : let g:entered = expand('<afile>')
1417 au! CmdlineLeave : let g:left = expand('<afile>')
1418 let g:entered = 0
1419 let g:left = 0
1420 call feedkeys(":echo 'hello'\<CR>", 'xt')
1421 call assert_equal(':', g:entered)
1422 call assert_equal(':', g:left)
1423 au! CmdlineEnter
1424 au! CmdlineLeave
1425
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001426 let save_shellslash = &shellslash
1427 set noshellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001428 au! CmdlineEnter / let g:entered = expand('<afile>')
1429 au! CmdlineLeave / let g:left = expand('<afile>')
1430 let g:entered = 0
1431 let g:left = 0
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001432 new
1433 call setline(1, 'hello')
1434 call feedkeys("/hello\<CR>", 'xt')
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001435 call assert_equal('/', g:entered)
1436 call assert_equal('/', g:left)
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001437 bwipe!
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001438 au! CmdlineEnter
1439 au! CmdlineLeave
Bram Moolenaara4baf5b2018-04-22 13:27:44 +02001440 let &shellslash = save_shellslash
Bram Moolenaarfafcf0d2017-10-19 18:35:51 +02001441endfunc
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001442
1443" Test for BufWritePre autocommand that deletes or unloads the buffer.
1444func Test_BufWritePre()
1445 %bwipe
1446 au BufWritePre Xxx1 bunload
1447 au BufWritePre Xxx2 bwipe
1448
1449 call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1')
1450 call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2')
1451
1452 edit Xtest
1453 e! Xxx2
1454 bdel Xtest
1455 e Xxx1
1456 " write it, will unload it and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001457 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001458 call assert_equal('Xxx2', bufname('%'))
1459 edit Xtest
1460 e! Xxx2
1461 bwipe Xtest
1462 " write it, will delete the buffer and give an error msg
Bram Moolenaare2e40752020-09-04 21:18:46 +02001463 call assert_fails('w', 'E203:')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001464 call assert_equal('Xxx1', bufname('%'))
1465 au! BufWritePre
1466 call delete('Xxx1')
1467 call delete('Xxx2')
1468endfunc
1469
1470" Test for BufUnload autocommand that unloads all the other buffers
1471func Test_bufunload_all()
1472 call writefile(['Test file Xxx1'], 'Xxx1')"
1473 call writefile(['Test file Xxx2'], 'Xxx2')"
1474
Bram Moolenaarc79745a2019-05-20 22:12:34 +02001475 let content =<< trim [CODE]
1476 func UnloadAllBufs()
1477 let i = 1
1478 while i <= bufnr('$')
1479 if i != bufnr('%') && bufloaded(i)
1480 exe i . 'bunload'
1481 endif
1482 let i += 1
1483 endwhile
1484 endfunc
1485 au BufUnload * call UnloadAllBufs()
1486 au VimLeave * call writefile(['Test Finished'], 'Xout')
1487 edit Xxx1
1488 split Xxx2
1489 q
1490 [CODE]
1491
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001492 call writefile(content, 'Xtest')
1493
1494 call delete('Xout')
Bram Moolenaar93344c22019-08-14 21:12:05 +02001495 call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest')
Bram Moolenaar53f0c962017-10-22 14:23:59 +02001496 call assert_true(filereadable('Xout'))
1497
1498 call delete('Xxx1')
1499 call delete('Xxx2')
1500 call delete('Xtest')
1501 call delete('Xout')
1502endfunc
1503
1504" Some tests for buffer-local autocommands
1505func Test_buflocal_autocmd()
1506 let g:bname = ''
1507 edit xx
1508 au BufLeave <buffer> let g:bname = expand("%")
1509 " here, autocommand for xx should trigger.
1510 " but autocommand shall not apply to buffer named <buffer>.
1511 edit somefile
1512 call assert_equal('xx', g:bname)
1513 let g:bname = ''
1514 " here, autocommand shall be auto-deleted
1515 bwipe xx
1516 " autocmd should not trigger
1517 edit xx
1518 call assert_equal('', g:bname)
1519 " autocmd should not trigger
1520 edit somefile
1521 call assert_equal('', g:bname)
1522 enew
1523 unlet g:bname
1524endfunc
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001525
1526" Test for "*Cmd" autocommands
1527func Test_Cmd_Autocmds()
1528 call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx')
1529
1530 enew!
1531 au BufReadCmd XtestA 0r Xxx|$del
1532 edit XtestA " will read text of Xxd instead
1533 call assert_equal('start of Xxx', getline(1))
1534
1535 au BufWriteCmd XtestA call append(line("$"), "write")
1536 write " will append a line to the file
1537 call assert_equal('write', getline('$'))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001538 call assert_fails('read XtestA', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001539 call assert_equal('write', getline(4))
1540
1541 " now we have:
1542 " 1 start of Xxx
1543 " 2 abc2
1544 " 3 end of Xxx
1545 " 4 write
1546
1547 au FileReadCmd XtestB '[r Xxx
1548 2r XtestB " will read Xxx below line 2 instead
1549 call assert_equal('start of Xxx', getline(3))
1550
1551 " now we have:
1552 " 1 start of Xxx
1553 " 2 abc2
1554 " 3 start of Xxx
1555 " 4 abc2
1556 " 5 end of Xxx
1557 " 6 end of Xxx
1558 " 7 write
1559
1560 au FileWriteCmd XtestC '[,']copy $
1561 normal 4GA1
1562 4,5w XtestC " will copy lines 4 and 5 to the end
1563 call assert_equal("\tabc21", getline(8))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001564 call assert_fails('r XtestC', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001565 call assert_equal("end of Xxx", getline(9))
1566
1567 " now we have:
1568 " 1 start of Xxx
1569 " 2 abc2
1570 " 3 start of Xxx
1571 " 4 abc21
1572 " 5 end of Xxx
1573 " 6 end of Xxx
1574 " 7 write
1575 " 8 abc21
1576 " 9 end of Xxx
1577
1578 let g:lines = []
1579 au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']")))
1580 w >>XtestD " will add lines to 'lines'
1581 call assert_equal(9, len(g:lines))
Bram Moolenaare2e40752020-09-04 21:18:46 +02001582 call assert_fails('$r XtestD', 'E484:') " should not read anything
Bram Moolenaar430dc5d2017-11-02 21:04:47 +01001583 call assert_equal(9, line('$'))
1584 call assert_equal('end of Xxx', getline('$'))
1585
1586 au BufReadCmd XtestE 0r Xxx|$del
1587 sp XtestE " split window with test.out
1588 call assert_equal('end of Xxx', getline(3))
1589
1590 let g:lines = []
1591 exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>"
1592 au BufWriteCmd XtestE call extend(g:lines, getline(0, '$'))
1593 wall " will write other window to 'lines'
1594 call assert_equal(4, len(g:lines), g:lines)
1595 call assert_equal('asdf', g:lines[2])
1596
1597 au! BufReadCmd
1598 au! BufWriteCmd
1599 au! FileReadCmd
1600 au! FileWriteCmd
1601 au! FileAppendCmd
1602 %bwipe!
1603 call delete('Xxx')
1604 enew!
1605endfunc
Bram Moolenaaraace2152017-11-05 16:23:10 +01001606
Bram Moolenaar0fff4412020-03-29 16:06:29 +02001607func s:ReadFile()
1608 setl noswapfile nomodified
1609 let filename = resolve(expand("<afile>:p"))
1610 execute 'read' fnameescape(filename)
1611 1d_
1612 exe 'file' fnameescape(filename)
1613 setl buftype=acwrite
1614endfunc
1615
1616func s:WriteFile()
1617 let filename = resolve(expand("<afile>:p"))
1618 setl buftype=
1619 noautocmd execute 'write' fnameescape(filename)
1620 setl buftype=acwrite
1621 setl nomodified
1622endfunc
1623
1624func Test_BufReadCmd()
1625 autocmd BufReadCmd *.test call s:ReadFile()
1626 autocmd BufWriteCmd *.test call s:WriteFile()
1627
1628 call writefile(['one', 'two', 'three'], 'Xcmd.test')
1629 edit Xcmd.test
1630 call assert_match('Xcmd.test" line 1 of 3', execute('file'))
1631 normal! Gofour
1632 write
1633 call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test'))
1634
1635 bwipe!
1636 call delete('Xcmd.test')
1637 au! BufReadCmd
1638 au! BufWriteCmd
1639endfunc
1640
Bram Moolenaaraace2152017-11-05 16:23:10 +01001641func SetChangeMarks(start, end)
Bram Moolenaar97c69432021-01-15 16:45:21 +01001642 exe a:start .. 'mark ['
1643 exe a:end .. 'mark ]'
Bram Moolenaaraace2152017-11-05 16:23:10 +01001644endfunc
1645
1646" Verify the effects of autocmds on '[ and ']
1647func Test_change_mark_in_autocmds()
1648 edit! Xtest
Bram Moolenaar97c69432021-01-15 16:45:21 +01001649 call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001650
1651 call SetChangeMarks(2, 3)
1652 write
1653 call assert_equal([1, 4], [line("'["), line("']")])
1654
1655 call SetChangeMarks(2, 3)
1656 au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1657 write
1658 au! BufWritePre
1659
Bram Moolenaar14ddd222020-08-05 12:02:40 +02001660 if has('unix')
Bram Moolenaaraace2152017-11-05 16:23:10 +01001661 write XtestFilter
1662 write >> XtestFilter
1663
1664 call SetChangeMarks(2, 3)
1665 " Marks are set to the entire range of the write
1666 au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
1667 " '[ is adjusted to just before the line that will receive the filtered
1668 " data
1669 au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")])
1670 " The filtered data is read into the buffer, and the source lines are
1671 " still present, so the range is after the source lines
1672 au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")])
1673 %!cat XtestFilter
1674 " After the filtered data is read, the original lines are deleted
1675 call assert_equal([1, 8], [line("'["), line("']")])
1676 au! FilterWritePre,FilterReadPre,FilterReadPost
1677 undo
1678
1679 call SetChangeMarks(1, 4)
1680 au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1681 au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")])
1682 au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1683 2,3!cat XtestFilter
1684 call assert_equal([2, 9], [line("'["), line("']")])
1685 au! FilterWritePre,FilterReadPre,FilterReadPost
1686 undo
1687
1688 call delete('XtestFilter')
1689 endif
1690
1691 call SetChangeMarks(1, 4)
1692 au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")])
1693 2,3write Xtest2
1694 au! FileWritePre
1695
1696 call SetChangeMarks(2, 3)
1697 au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")])
1698 write >> Xtest2
1699 au! FileAppendPre
1700
1701 call SetChangeMarks(1, 4)
1702 au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")])
1703 2,3write >> Xtest2
1704 au! FileAppendPre
1705
1706 call SetChangeMarks(1, 1)
1707 au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")])
1708 au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")])
1709 3read Xtest2
1710 au! FileReadPre,FileReadPost
1711 undo
1712
1713 call SetChangeMarks(4, 4)
1714 " When the line is 0, it's adjusted to 1
1715 au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")])
1716 au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")])
1717 0read 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([2, 9], [line("'["), line("']")])
1725 1read Xtest2
1726 au! FileReadPre,FileReadPost
1727 undo
1728
1729 bwipe!
1730 call delete('Xtest')
1731 call delete('Xtest2')
1732endfunc
1733
1734func Test_Filter_noshelltemp()
Bram Moolenaaraeb313f2020-11-27 19:13:28 +01001735 CheckExecutable cat
Bram Moolenaaraace2152017-11-05 16:23:10 +01001736
1737 enew!
1738 call setline(1, ['a', 'b', 'c', 'd'])
1739
1740 let shelltemp = &shelltemp
1741 set shelltemp
1742
1743 let g:filter_au = 0
1744 au FilterWritePre * let g:filter_au += 1
1745 au FilterReadPre * let g:filter_au += 1
1746 au FilterReadPost * let g:filter_au += 1
1747 %!cat
1748 call assert_equal(3, g:filter_au)
1749
1750 if has('filterpipe')
1751 set noshelltemp
1752
1753 let g:filter_au = 0
1754 au FilterWritePre * let g:filter_au += 1
1755 au FilterReadPre * let g:filter_au += 1
1756 au FilterReadPost * let g:filter_au += 1
1757 %!cat
1758 call assert_equal(0, g:filter_au)
1759 endif
1760
1761 au! FilterWritePre,FilterReadPre,FilterReadPost
1762 let &shelltemp = shelltemp
1763 bwipe!
1764endfunc
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001765
1766func Test_TextYankPost()
1767 enew!
1768 call setline(1, ['foo'])
1769
1770 let g:event = []
1771 au TextYankPost * let g:event = copy(v:event)
1772
1773 call assert_equal({}, v:event)
1774 call assert_fails('let v:event = {}', 'E46:')
1775 call assert_fails('let v:event.mykey = 0', 'E742:')
1776
1777 norm "ayiw
1778 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001779 \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001780 \g:event)
1781 norm y_
1782 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001783 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false},
1784 \g:event)
1785 norm Vy
1786 call assert_equal(
1787 \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001788 \g:event)
1789 call feedkeys("\<C-V>y", 'x')
1790 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001791 \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001792 \g:event)
1793 norm "xciwbar
1794 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001795 \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001796 \g:event)
1797 norm "bdiw
1798 call assert_equal(
Bram Moolenaar37d16732020-06-12 22:09:01 +02001799 \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false},
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001800 \g:event)
1801
1802 call assert_equal({}, v:event)
1803
Bram Moolenaarfccbf062020-11-26 20:34:00 +01001804 if has('clipboard_working') && !has('gui_running')
1805 " Test that when the visual selection is automatically copied to clipboard
1806 " register a TextYankPost is emitted
1807 call setline(1, ['foobar'])
1808
1809 let @* = ''
1810 set clipboard=autoselect
1811 exe "norm! ggviw\<Esc>"
1812 call assert_equal(
1813 \{'regcontents': ['foobar'], 'regname': '*', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1814 \g:event)
1815
1816 let @+ = ''
1817 set clipboard=autoselectplus
1818 exe "norm! ggviw\<Esc>"
1819 call assert_equal(
1820 \{'regcontents': ['foobar'], 'regname': '+', 'operator': 'y', 'regtype': 'v', 'visual': v:true},
1821 \g:event)
1822
1823 set clipboard&vim
1824 endif
1825
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001826 au! TextYankPost
1827 unlet g:event
1828 bwipe!
1829endfunc
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001830
1831func Test_nocatch_wipe_all_buffers()
1832 " Real nasty autocommand: wipe all buffers on any event.
1833 au * * bwipe *
Bram Moolenaar28ee8922020-10-28 20:20:00 +01001834 call assert_fails('next x', ['E94:', 'E937:'])
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001835 bwipe
1836 au!
1837endfunc
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001838
1839func Test_nocatch_wipe_dummy_buffer()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001840 CheckFeature quickfix
1841 " Nasty autocommand: wipe buffer on any event.
1842 au * x bwipe
Bram Moolenaare2e40752020-09-04 21:18:46 +02001843 call assert_fails('lv½ /x', 'E937:')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001844 au!
Bram Moolenaar4fb921e2017-12-18 15:33:00 +01001845endfunc
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001846
1847function s:Before_test_dirchanged()
1848 augroup test_dirchanged
1849 autocmd!
1850 augroup END
1851 let s:li = []
1852 let s:dir_this = getcwd()
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001853 let s:dir_foo = s:dir_this . '/Xfoo'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001854 call mkdir(s:dir_foo)
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02001855 let s:dir_bar = s:dir_this . '/Xbar'
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001856 call mkdir(s:dir_bar)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001857endfunc
1858
1859function s:After_test_dirchanged()
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001860 call chdir(s:dir_this)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001861 call delete(s:dir_foo, 'd')
1862 call delete(s:dir_bar, 'd')
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001863 augroup test_dirchanged
1864 autocmd!
1865 augroup END
1866endfunc
1867
1868function Test_dirchanged_global()
1869 call s:Before_test_dirchanged()
1870 autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1871 autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001872 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001873 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001874 call chdir(s:dir_foo)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001875 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001876 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001877 call assert_equal(["cd:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001878 call s:After_test_dirchanged()
1879endfunc
1880
1881function Test_dirchanged_local()
1882 call s:Before_test_dirchanged()
1883 autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
1884 autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001885 call chdir(s:dir_foo)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001886 call assert_equal([], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001887 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001888 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001889 exe 'lcd ' .. fnameescape(s:dir_bar)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001890 call assert_equal(["lcd:", s:dir_bar], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001891 call s:After_test_dirchanged()
1892endfunc
1893
1894function Test_dirchanged_auto()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001895 CheckOption autochdir
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001896 call s:Before_test_dirchanged()
1897 call test_autochdir()
1898 autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1899 autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1900 set acd
Bram Moolenaar3503d7c2019-11-09 20:10:17 +01001901 cd ..
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001902 call assert_equal([], s:li)
Bram Moolenaar2caad3f2018-12-16 15:38:02 +01001903 exe 'edit ' . s:dir_foo . '/Xfile'
1904 call assert_equal(s:dir_foo, getcwd())
1905 call assert_equal(["auto:", s:dir_foo], s:li)
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001906 set noacd
1907 bwipe!
1908 call s:After_test_dirchanged()
1909endfunc
Bram Moolenaar5a093432018-02-10 18:15:19 +01001910
1911" Test TextChangedI and TextChangedP
1912func Test_ChangedP()
1913 new
1914 call setline(1, ['foo', 'bar', 'foobar'])
1915 call test_override("char_avail", 1)
1916 set complete=. completeopt=menuone
1917
1918 func! TextChangedAutocmd(char)
1919 let g:autocmd .= a:char
1920 endfunc
1921
1922 au! TextChanged <buffer> :call TextChangedAutocmd('N')
1923 au! TextChangedI <buffer> :call TextChangedAutocmd('I')
1924 au! TextChangedP <buffer> :call TextChangedAutocmd('P')
1925
1926 call cursor(3, 1)
1927 let g:autocmd = ''
1928 call feedkeys("o\<esc>", 'tnix')
1929 call assert_equal('I', g:autocmd)
1930
1931 let g:autocmd = ''
1932 call feedkeys("Sf", 'tnix')
1933 call assert_equal('II', g:autocmd)
1934
1935 let g:autocmd = ''
1936 call feedkeys("Sf\<C-N>", 'tnix')
1937 call assert_equal('IIP', g:autocmd)
1938
1939 let g:autocmd = ''
1940 call feedkeys("Sf\<C-N>\<C-N>", 'tnix')
1941 call assert_equal('IIPP', g:autocmd)
1942
1943 let g:autocmd = ''
1944 call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix')
1945 call assert_equal('IIPPP', g:autocmd)
1946
1947 let g:autocmd = ''
1948 call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix')
1949 call assert_equal('IIPPPP', g:autocmd)
1950
1951 call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$'))
1952 " TODO: how should it handle completeopt=noinsert,noselect?
1953
1954 " CleanUp
1955 call test_override("char_avail", 0)
1956 au! TextChanged
1957 au! TextChangedI
1958 au! TextChangedP
1959 delfu TextChangedAutocmd
1960 unlet! g:autocmd
1961 set complete&vim completeopt&vim
1962
1963 bw!
1964endfunc
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001965
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001966let g:setline_handled = v:false
Bram Moolenaar1e115362019-01-09 23:01:02 +01001967func SetLineOne()
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001968 if !g:setline_handled
1969 call setline(1, "(x)")
1970 let g:setline_handled = v:true
1971 endif
1972endfunc
1973
1974func Test_TextChangedI_with_setline()
1975 new
1976 call test_override('char_avail', 1)
1977 autocmd TextChangedI <buffer> call SetLineOne()
1978 call feedkeys("i(\<CR>\<Esc>", 'tx')
1979 call assert_equal('(', getline(1))
1980 call assert_equal('x)', getline(2))
1981 undo
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001982 call assert_equal('', getline(1))
Bram Moolenaar9fa95062018-08-08 22:08:32 +02001983 call assert_equal('', getline(2))
Bram Moolenaar91d2e782018-08-07 19:05:01 +02001984
1985 call test_override('starting', 0)
1986 bwipe!
1987endfunc
1988
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001989func Test_Changed_FirstTime()
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001990 CheckFeature terminal
1991 CheckNotGui
Bram Moolenaar3cdcb092020-03-18 19:18:10 +01001992 " Starting a terminal to run Vim is always considered flaky.
Bram Moolenaar30d53e22020-03-18 21:10:44 +01001993 let g:test_is_flaky = 1
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02001994
Bram Moolenaar8c64a362018-03-23 22:39:31 +01001995 " Prepare file for TextChanged event.
1996 call writefile([''], 'Xchanged.txt')
1997 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3})
1998 call assert_equal('running', term_getstatus(buf))
Bram Moolenaar1834d372018-03-29 17:40:46 +02001999 " Wait for the ruler (in the status line) to be shown.
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002000 " In ConPTY, there is additional character which is drawn up to the width of
2001 " the screen.
2002 if has('conpty')
2003 call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))})
2004 else
2005 call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))})
2006 endif
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002007 " It's only adding autocmd, so that no event occurs.
2008 call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>")
2009 call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>")
Bram Moolenaar50182fa2018-04-28 21:34:40 +02002010 call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))})
Bram Moolenaar8c64a362018-03-23 22:39:31 +01002011 call assert_equal([''], readfile('Xchanged.txt'))
2012
2013 " clean up
2014 call delete('Xchanged.txt')
2015 bwipe!
2016endfunc
Bram Moolenaar0566e892019-01-24 19:37:40 +01002017
Bram Moolenaareb93f3f2019-04-04 15:04:56 +02002018func Test_autocmd_nested()
2019 let g:did_nested = 0
2020 augroup Testing
2021 au WinNew * edit somefile
2022 au BufNew * let g:did_nested = 1
2023 augroup END
2024 split
2025 call assert_equal(0, g:did_nested)
2026 close
2027 bwipe! somefile
2028
2029 " old nested argument still works
2030 augroup Testing
2031 au!
2032 au WinNew * nested edit somefile
2033 au BufNew * let g:did_nested = 1
2034 augroup END
2035 split
2036 call assert_equal(1, g:did_nested)
2037 close
2038 bwipe! somefile
2039
2040 " New ++nested argument works
2041 augroup Testing
2042 au!
2043 au WinNew * ++nested edit somefile
2044 au BufNew * let g:did_nested = 1
2045 augroup END
2046 split
2047 call assert_equal(1, g:did_nested)
2048 close
2049 bwipe! somefile
2050
2051 augroup Testing
2052 au!
2053 augroup END
2054
2055 call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:')
2056 call assert_fails('au WinNew * nested nested echo bad', 'E983:')
2057endfunc
2058
2059func Test_autocmd_once()
2060 " Without ++once WinNew triggers twice
2061 let g:did_split = 0
2062 augroup Testing
2063 au WinNew * let g:did_split += 1
2064 augroup END
2065 split
2066 split
2067 call assert_equal(2, g:did_split)
2068 call assert_true(exists('#WinNew'))
2069 close
2070 close
2071
2072 " With ++once WinNew triggers once
2073 let g:did_split = 0
2074 augroup Testing
2075 au!
2076 au WinNew * ++once let g:did_split += 1
2077 augroup END
2078 split
2079 split
2080 call assert_equal(1, g:did_split)
2081 call assert_false(exists('#WinNew'))
2082 close
2083 close
2084
2085 call assert_fails('au WinNew * ++once ++once echo bad', 'E983:')
2086endfunc
2087
Bram Moolenaara68e5952019-04-25 22:22:01 +02002088func Test_autocmd_bufreadpre()
2089 new
2090 let b:bufreadpre = 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002091 call append(0, range(1000))
Bram Moolenaara68e5952019-04-25 22:22:01 +02002092 w! XAutocmdBufReadPre.txt
2093 autocmd BufReadPre <buffer> :let b:bufreadpre += 1
Bram Moolenaarab505b12020-03-23 19:28:44 +01002094 norm! 500gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002095 sp
Bram Moolenaarab505b12020-03-23 19:28:44 +01002096 norm! 1000gg
Bram Moolenaara68e5952019-04-25 22:22:01 +02002097 wincmd p
2098 let g:wsv1 = winsaveview()
2099 wincmd p
2100 let g:wsv2 = winsaveview()
2101 " triggers BufReadPre, should not move the cursor in either window
2102 " The topline may change one line in a large window.
2103 edit
2104 call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline)
2105 call assert_equal(g:wsv2.lnum, winsaveview().lnum)
2106 call assert_equal(2, b:bufreadpre)
2107 wincmd p
2108 call assert_equal(g:wsv1.topline, winsaveview().topline)
2109 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2110 call assert_equal(2, b:bufreadpre)
2111 " Now set the cursor position in an BufReadPre autocommand
2112 " (even though the position will be invalid, this should make Vim reset the
2113 " cursor position in the other window.
2114 wincmd p
2115 set cpo+=g
2116 " won't do anything, but try to set the cursor on an invalid lnum
2117 autocmd BufReadPre <buffer> :norm! 70gg
2118 " triggers BufReadPre, should not move the cursor in either window
2119 e
2120 call assert_equal(1, winsaveview().topline)
2121 call assert_equal(1, winsaveview().lnum)
2122 call assert_equal(3, b:bufreadpre)
2123 wincmd p
2124 call assert_equal(g:wsv1.topline, winsaveview().topline)
2125 call assert_equal(g:wsv1.lnum, winsaveview().lnum)
2126 call assert_equal(3, b:bufreadpre)
2127 close
2128 close
2129 call delete('XAutocmdBufReadPre.txt')
2130 set cpo-=g
2131endfunc
2132
Bram Moolenaar5e66b422019-01-24 21:58:10 +01002133" FileChangedShell tested in test_filechanged.vim
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002134
2135" Tests for the following autocommands:
2136" - FileWritePre writing a compressed file
2137" - FileReadPost reading a compressed file
2138" - BufNewFile reading a file template
2139" - BufReadPre decompressing the file to be read
2140" - FilterReadPre substituting characters in the temp file
2141" - FilterReadPost substituting characters after filtering
2142" - FileReadPre set options for decompression
2143" - FileReadPost decompress the file
2144func Test_ReadWrite_Autocmds()
2145 " Run this test only on Unix-like systems and if gzip is available
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02002146 CheckUnix
2147 CheckExecutable gzip
Bram Moolenaar69ea5872019-04-25 20:29:00 +02002148
2149 " Make $GZIP empty, "-v" would cause trouble.
2150 let $GZIP = ""
2151
2152 " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz'
2153 " being modified outside of Vim (noticed on Solaris).
2154 au FileChangedShell * echo 'caught FileChangedShell'
2155
2156 " Test for the FileReadPost, FileWritePre and FileWritePost autocmds
2157 augroup Test1
2158 au!
2159 au FileWritePre *.gz '[,']!gzip
2160 au FileWritePost *.gz undo
2161 au FileReadPost *.gz '[,']!gzip -d
2162 augroup END
2163
2164 new
2165 set bin
2166 call append(0, [
2167 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2168 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2169 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2170 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2171 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2172 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2173 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2174 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2175 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2176 \ ])
2177 1,9write! Xtestfile.gz
2178 enew! | close
2179
2180 new
2181 " Read and decompress the testfile
2182 0read Xtestfile.gz
2183 call assert_equal([
2184 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2185 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2186 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2187 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2188 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2189 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2190 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2191 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2192 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2193 \ ], getline(1, 9))
2194 enew! | close
2195
2196 augroup Test1
2197 au!
2198 augroup END
2199
2200 " Test for the FileAppendPre and FileAppendPost autocmds
2201 augroup Test2
2202 au!
2203 au BufNewFile *.c read Xtest.c
2204 au FileAppendPre *.out '[,']s/new/NEW/
2205 au FileAppendPost *.out !cat Xtest.c >> test.out
2206 augroup END
2207
2208 call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c')
2209 new foo.c " should load Xtest.c
2210 call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4))
2211 w! >> test.out " append it to the output file
2212
2213 let contents = readfile('test.out')
2214 call assert_equal(' * Here is a NEW .c file', contents[2])
2215 call assert_equal(' * Here is a new .c file', contents[5])
2216
2217 call delete('test.out')
2218 enew! | close
2219 augroup Test2
2220 au!
2221 augroup END
2222
2223 " Test for the BufReadPre and BufReadPost autocmds
2224 augroup Test3
2225 au!
2226 " setup autocommands to decompress before reading and re-compress
2227 " afterwards
2228 au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>"))
2229 au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2230 au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r"))
2231 au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r"))
2232 augroup END
2233
2234 e! Xtestfile.gz " Edit compressed file
2235 call assert_equal([
2236 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2237 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2238 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2239 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2240 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2241 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2242 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2243 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2244 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2245 \ ], getline(1, 9))
2246
2247 w! >> test.out " Append it to the output file
2248
2249 augroup Test3
2250 au!
2251 augroup END
2252
2253 " Test for the FilterReadPre and FilterReadPost autocmds.
2254 set shelltemp " need temp files here
2255 augroup Test4
2256 au!
2257 au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t")
2258 au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>"))
2259 au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t'
2260 au FilterReadPost *.out '[,']s/x/X/g
2261 augroup END
2262
2263 e! test.out " Edit the output file
2264 1,$!cat
2265 call assert_equal([
2266 \ 'linE 2 AbcdefghijklmnopqrstuvwXyz',
2267 \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2268 \ 'linE 4 AbcdefghijklmnopqrstuvwXyz',
2269 \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2270 \ 'linE 6 AbcdefghijklmnopqrstuvwXyz',
2271 \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2272 \ 'linE 8 AbcdefghijklmnopqrstuvwXyz',
2273 \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
2274 \ 'linE 10 AbcdefghijklmnopqrstuvwXyz'
2275 \ ], getline(1, 9))
2276 call assert_equal([
2277 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2278 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2279 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2280 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2281 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2282 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2283 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2284 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2285 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2286 \ ], readfile('test.out'))
2287
2288 augroup Test4
2289 au!
2290 augroup END
2291 set shelltemp&vim
2292
2293 " Test for the FileReadPre and FileReadPost autocmds.
2294 augroup Test5
2295 au!
2296 au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>"))
2297 au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>"))
2298 au FileReadPost *.gz '[,']s/l/L/
2299 augroup END
2300
2301 new
2302 0r Xtestfile.gz " Read compressed file
2303 call assert_equal([
2304 \ 'Line 2 Abcdefghijklmnopqrstuvwxyz',
2305 \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2306 \ 'Line 4 Abcdefghijklmnopqrstuvwxyz',
2307 \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2308 \ 'Line 6 Abcdefghijklmnopqrstuvwxyz',
2309 \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2310 \ 'Line 8 Abcdefghijklmnopqrstuvwxyz',
2311 \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2312 \ 'Line 10 Abcdefghijklmnopqrstuvwxyz'
2313 \ ], getline(1, 9))
2314 call assert_equal([
2315 \ 'line 2 Abcdefghijklmnopqrstuvwxyz',
2316 \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2317 \ 'line 4 Abcdefghijklmnopqrstuvwxyz',
2318 \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2319 \ 'line 6 Abcdefghijklmnopqrstuvwxyz',
2320 \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2321 \ 'line 8 Abcdefghijklmnopqrstuvwxyz',
2322 \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
2323 \ 'line 10 Abcdefghijklmnopqrstuvwxyz'
2324 \ ], readfile('Xtestfile.gz'))
2325
2326 augroup Test5
2327 au!
2328 augroup END
2329
2330 au! FileChangedShell
2331 call delete('Xtestfile.gz')
2332 call delete('Xtest.c')
2333 call delete('test.out')
2334endfunc
Bram Moolenaar23b51392019-05-09 21:38:43 +02002335
2336func Test_throw_in_BufWritePre()
2337 new
2338 call setline(1, ['one', 'two', 'three'])
2339 call assert_false(filereadable('Xthefile'))
2340 augroup throwing
2341 au BufWritePre X* throw 'do not write'
2342 augroup END
2343 try
2344 w Xthefile
2345 catch
2346 let caught = 1
2347 endtry
2348 call assert_equal(1, caught)
2349 call assert_false(filereadable('Xthefile'))
2350
2351 bwipe!
2352 au! throwing
2353endfunc
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002354
2355func Test_autocmd_SafeState()
2356 CheckRunVimInTerminal
2357
2358 let lines =<< trim END
2359 let g:safe = 0
2360 let g:again = ''
2361 au SafeState * let g:safe += 1
2362 au SafeStateAgain * let g:again ..= 'x'
2363 func CallTimer()
2364 call timer_start(10, {id -> execute('let g:again ..= "t"')})
2365 endfunc
2366 END
2367 call writefile(lines, 'XSafeState')
2368 let buf = RunVimInTerminal('-S XSafeState', #{rows: 6})
2369
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01002370 " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002371 " more often.
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002372 call term_sendkeys(buf, ":echo g:safe\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002373 call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002374
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002375 " SafeStateAgain should be invoked at least three times
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002376 call term_sendkeys(buf, ":echo g:again\<CR>")
Bram Moolenaar8fb1b472020-02-23 16:16:26 +01002377 call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002378
2379 call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002380 call TermWait(buf, 50)
Bram Moolenaar0f6629a2019-09-22 23:24:13 +02002381 call term_sendkeys(buf, ":\<CR>")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002382 call TermWait(buf, 50)
Bram Moolenaarcadbe1b2019-09-22 21:50:09 +02002383 call term_sendkeys(buf, ":echo g:again\<CR>")
2384 call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000)
2385
2386 call StopVimInTerminal(buf)
2387 call delete('XSafeState')
2388endfunc
Bram Moolenaar23324a02019-10-01 17:39:04 +02002389
2390func Test_autocmd_CmdWinEnter()
2391 CheckRunVimInTerminal
2392 " There is not cmdwin switch, so
2393 " test for cmdline_hist
2394 " (both are available with small builds)
2395 CheckFeature cmdline_hist
2396 let lines =<< trim END
2397 let b:dummy_var = 'This is a dummy'
2398 autocmd CmdWinEnter * quit
2399 let winnr = winnr('$')
2400 END
Bram Moolenaar1cfb9bb2020-12-22 11:40:45 +01002401 let filename = 'XCmdWinEnter'
Bram Moolenaar23324a02019-10-01 17:39:04 +02002402 call writefile(lines, filename)
2403 let buf = RunVimInTerminal('-S '.filename, #{rows: 6})
2404
2405 call term_sendkeys(buf, "q:")
Bram Moolenaar6a2c5a72020-04-08 21:50:25 +02002406 call TermWait(buf)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002407 call term_sendkeys(buf, ":echo b:dummy_var\<cr>")
Bram Moolenaar353c3512020-03-15 14:19:26 +01002408 call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000)
Bram Moolenaar23324a02019-10-01 17:39:04 +02002409 call term_sendkeys(buf, ":echo &buftype\<cr>")
2410 call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000)
2411 call term_sendkeys(buf, ":echo winnr\<cr>")
2412 call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000)
2413
2414 " clean up
2415 call StopVimInTerminal(buf)
2416 call delete(filename)
2417endfunc
Bram Moolenaarec66c412019-10-11 21:19:13 +02002418
2419func Test_autocmd_was_using_freed_memory()
Bram Moolenaar5a4c3082019-12-01 15:23:11 +01002420 CheckFeature quickfix
2421
Bram Moolenaarec66c412019-10-11 21:19:13 +02002422 pedit xx
2423 n x
2424 au WinEnter * quit
2425 split
2426 au! WinEnter
2427endfunc
Bram Moolenaarf4a1d1c2019-11-16 13:50:25 +01002428
2429func Test_BufWrite_lockmarks()
2430 edit! Xtest
2431 call setline(1, ['a', 'b', 'c', 'd'])
2432
2433 " :lockmarks preserves the marks
2434 call SetChangeMarks(2, 3)
2435 lockmarks write
2436 call assert_equal([2, 3], [line("'["), line("']")])
2437
2438 " *WritePre autocmds get the correct line range, but lockmarks preserves the
2439 " original values for the user
2440 augroup lockmarks
2441 au!
2442 au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")])
2443 au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")])
2444 augroup END
2445
2446 lockmarks write
2447 call assert_equal([2, 3], [line("'["), line("']")])
2448
2449 if executable('cat')
2450 lockmarks %!cat
2451 call assert_equal([2, 3], [line("'["), line("']")])
2452 endif
2453
2454 lockmarks 3,4write Xtest2
2455 call assert_equal([2, 3], [line("'["), line("']")])
2456
2457 au! lockmarks
2458 augroup! lockmarks
2459 call delete('Xtest')
2460 call delete('Xtest2')
2461endfunc
Bram Moolenaarce6db022020-01-07 20:11:42 +01002462
2463func Test_FileType_spell()
2464 if !isdirectory('/tmp')
2465 throw "Skipped: requires /tmp directory"
2466 endif
2467
2468 " this was crashing with an invalid free()
2469 setglobal spellfile=/tmp/en.utf-8.add
2470 augroup crash
2471 autocmd!
2472 autocmd BufNewFile,BufReadPost crashfile setf somefiletype
2473 autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype
2474 autocmd FileType anotherfiletype setlocal spell
2475 augroup END
2476 func! NoCrash() abort
2477 edit /tmp/crashfile
2478 endfunc
2479 call NoCrash()
2480
2481 au! crash
2482 setglobal spellfile=
2483endfunc
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002484
Bram Moolenaar406cd902020-02-18 21:54:41 +01002485" Test closing a window or editing another buffer from a FileChangedRO handler
2486" in a readonly buffer
2487func Test_FileChangedRO_winclose()
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002488 call test_override('ui_delay', 10)
2489
Bram Moolenaar406cd902020-02-18 21:54:41 +01002490 augroup FileChangedROTest
2491 au!
2492 autocmd FileChangedRO * quit
2493 augroup END
2494 new
2495 set readonly
2496 call assert_fails('normal i', 'E788:')
2497 close
2498 augroup! FileChangedROTest
2499
2500 augroup FileChangedROTest
2501 au!
2502 autocmd FileChangedRO * edit Xfile
2503 augroup END
2504 new
2505 set readonly
2506 call assert_fails('normal i', 'E788:')
2507 close
2508 augroup! FileChangedROTest
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002509 call test_override('ALL', 0)
Bram Moolenaar406cd902020-02-18 21:54:41 +01002510endfunc
2511
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002512func LogACmd()
2513 call add(g:logged, line('$'))
2514endfunc
2515
2516func Test_TermChanged()
Bram Moolenaard28e0b32020-02-22 23:08:52 +01002517 CheckNotGui
2518
Bram Moolenaar0c81d1b2020-02-22 22:45:55 +01002519 enew!
2520 tabnew
2521 call setline(1, ['a', 'b', 'c', 'd'])
2522 $
2523 au TermChanged * call LogACmd()
2524 let g:logged = []
2525 let term_save = &term
2526 set term=xterm
2527 call assert_equal([1, 4], g:logged)
2528
2529 au! TermChanged
2530 let &term = term_save
2531 bwipe!
2532endfunc
2533
Bram Moolenaare3284872020-03-19 13:55:03 +01002534" Test for FileReadCmd autocmd
2535func Test_autocmd_FileReadCmd()
2536 func ReadFileCmd()
2537 call append(line('$'), "v:cmdarg = " .. v:cmdarg)
2538 endfunc
2539 augroup FileReadCmdTest
2540 au!
2541 au FileReadCmd Xtest call ReadFileCmd()
2542 augroup END
2543
2544 new
2545 read ++bin Xtest
2546 read ++nobin Xtest
2547 read ++edit Xtest
2548 read ++bad=keep Xtest
2549 read ++bad=drop Xtest
2550 read ++bad=- Xtest
2551 read ++ff=unix Xtest
2552 read ++ff=dos Xtest
2553 read ++ff=mac Xtest
2554 read ++enc=utf-8 Xtest
2555
2556 call assert_equal(['',
2557 \ 'v:cmdarg = ++bin',
2558 \ 'v:cmdarg = ++nobin',
2559 \ 'v:cmdarg = ++edit',
2560 \ 'v:cmdarg = ++bad=keep',
2561 \ 'v:cmdarg = ++bad=drop',
2562 \ 'v:cmdarg = ++bad=-',
2563 \ 'v:cmdarg = ++ff=unix',
2564 \ 'v:cmdarg = ++ff=dos',
2565 \ 'v:cmdarg = ++ff=mac',
2566 \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$'))
2567
2568 close!
2569 augroup FileReadCmdTest
2570 au!
2571 augroup END
2572 delfunc ReadFileCmd
2573endfunc
2574
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002575" Test for passing invalid arguments to autocmd
2576func Test_autocmd_invalid_args()
2577 " Additional character after * for event
2578 call assert_fails('autocmd *a Xfile set ff=unix', 'E215:')
2579 augroup Test
2580 augroup END
2581 " Invalid autocmd event
2582 call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:')
2583 " Invalid autocmd event in a autocmd group
2584 call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:')
2585 augroup! Test
2586 " Execute all autocmds
2587 call assert_fails('doautocmd * BufEnter', 'E217:')
2588 call assert_fails('augroup! x1a2b3', 'E367:')
2589 call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:')
Bram Moolenaar531be472020-09-23 22:38:05 +02002590 call assert_fails('autocmd BufNew \) set ff=unix', 'E55:')
Bram Moolenaaree4e0c12020-04-06 21:35:05 +02002591endfunc
2592
2593" Test for deep nesting of autocmds
2594func Test_autocmd_deep_nesting()
2595 autocmd BufEnter Xfile doautocmd BufEnter Xfile
2596 call assert_fails('doautocmd BufEnter Xfile', 'E218:')
2597 autocmd! BufEnter Xfile
2598endfunc
2599
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002600" Tests for SigUSR1 autocmd event, which is only available on posix systems.
2601func Test_autocmd_sigusr1()
2602 CheckUnix
Bram Moolenaar62cd26a2020-10-11 20:08:44 +02002603 CheckExecutable /bin/kill
Bram Moolenaarbe5ee862020-06-10 20:56:58 +02002604
2605 let g:sigusr1_passed = 0
2606 au SigUSR1 * let g:sigusr1_passed = 1
2607 call system('/bin/kill -s usr1 ' . getpid())
2608 call WaitForAssert({-> assert_true(g:sigusr1_passed)})
2609
2610 au! SigUSR1
2611 unlet g:sigusr1_passed
2612endfunc
2613
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002614" Test for BufReadPre autocmd deleting the file
2615func Test_BufReadPre_delfile()
2616 augroup TestAuCmd
2617 au!
2618 autocmd BufReadPre Xfile call delete('Xfile')
2619 augroup END
2620 call writefile([], 'Xfile')
2621 call assert_fails('new Xfile', 'E200:')
2622 call assert_equal('Xfile', @%)
2623 call assert_equal(1, &readonly)
2624 call delete('Xfile')
2625 augroup TestAuCmd
2626 au!
2627 augroup END
2628 close!
2629endfunc
2630
2631" Test for BufReadPre autocmd changing the current buffer
2632func Test_BufReadPre_changebuf()
2633 augroup TestAuCmd
2634 au!
2635 autocmd BufReadPre Xfile edit Xsomeotherfile
2636 augroup END
2637 call writefile([], 'Xfile')
2638 call assert_fails('new Xfile', 'E201:')
2639 call assert_equal('Xsomeotherfile', @%)
2640 call assert_equal(1, &readonly)
2641 call delete('Xfile')
2642 augroup TestAuCmd
2643 au!
2644 augroup END
2645 close!
2646endfunc
2647
2648" Test for BufWipeouti autocmd changing the current buffer when reading a file
2649" in an empty buffer with 'f' flag in 'cpo'
2650func Test_BufDelete_changebuf()
2651 new
2652 augroup TestAuCmd
2653 au!
2654 autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr
2655 augroup END
2656 let save_cpo = &cpo
2657 set cpo+=f
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +02002658 call assert_fails('r Xfile', ['E812:', 'E484:'])
Bram Moolenaarb340bae2020-06-15 19:51:56 +02002659 call assert_equal('somefile', @%)
2660 let &cpo = save_cpo
2661 augroup TestAuCmd
2662 au!
2663 augroup END
2664 close!
2665endfunc
2666
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002667" Test for the temporary internal window used to execute autocmds
2668func Test_autocmd_window()
2669 %bw!
2670 edit one.txt
2671 tabnew two.txt
2672 let g:blist = []
Bram Moolenaar832adf92020-06-25 19:01:36 +02002673 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002674 au!
2675 au BufEnter * call add(g:blist, [expand('<afile>'),
2676 \ win_gettype(bufwinnr(expand('<afile>')))])
2677 augroup END
2678
2679 doautoall BufEnter
Bram Moolenaar40a019f2020-06-17 21:41:35 +02002680 call assert_equal([['one.txt', 'autocmd'], ['two.txt', '']], g:blist)
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002681
Bram Moolenaar832adf92020-06-25 19:01:36 +02002682 augroup aucmd_win_test1
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002683 au!
2684 augroup END
Bram Moolenaar832adf92020-06-25 19:01:36 +02002685 augroup! aucmd_win_test1
2686 %bw!
2687endfunc
2688
2689" Test for trying to close the temporary window used for executing an autocmd
2690func Test_close_autocmd_window()
2691 %bw!
2692 edit one.txt
2693 tabnew two.txt
2694 augroup aucmd_win_test2
2695 au!
2696 au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif
2697 augroup END
2698
2699 call assert_fails('doautoall BufEnter', 'E813:')
2700
2701 augroup aucmd_win_test2
2702 au!
2703 augroup END
2704 augroup! aucmd_win_test2
Bram Moolenaarcf844172020-06-26 19:44:06 +02002705 %bwipe!
2706endfunc
2707
2708" Test for trying to close the tab that has the temporary window for exeucing
2709" an autocmd.
2710func Test_close_autocmd_tab()
2711 edit one.txt
2712 tabnew two.txt
2713 augroup aucmd_win_test
2714 au!
2715 au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif
2716 augroup END
2717
2718 call assert_fails('doautoall BufEnter', 'E813:')
2719
2720 tabonly
2721 augroup aucmd_win_test
2722 au!
2723 augroup END
2724 augroup! aucmd_win_test
2725 %bwipe!
Bram Moolenaar0fe937f2020-06-16 22:42:04 +02002726endfunc
2727
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +01002728" vim: shiftwidth=2 sts=2 expandtab