Bram Moolenaar | 1473551 | 2016-03-26 21:00:08 +0100 | [diff] [blame] | 1 | " Tests for autocommands |
| 2 | |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 3 | source shared.vim |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 4 | source check.vim |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 5 | source term_util.vim |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 6 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 7 | func s:cleanup_buffers() abort |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 8 | for bnr in range(1, bufnr('$')) |
| 9 | if bufloaded(bnr) && bufnr('%') != bnr |
| 10 | execute 'bd! ' . bnr |
| 11 | endif |
| 12 | endfor |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 13 | endfunc |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 14 | |
Bram Moolenaar | 1473551 | 2016-03-26 21:00:08 +0100 | [diff] [blame] | 15 | func 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. |
| 20 | endfunc |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 21 | |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 22 | " Test for the CursorHold autocmd |
| 23 | func 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 Moolenaar | 17f6754 | 2020-08-20 18:29:13 +0200 | [diff] [blame] | 32 | call term_sendkeys(buf, "G") |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 33 | call term_wait(buf, 50) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 34 | call term_sendkeys(buf, "gg") |
| 35 | call term_wait(buf) |
Bram Moolenaar | 17f6754 | 2020-08-20 18:29:13 +0200 | [diff] [blame] | 36 | call WaitForAssert({-> assert_equal(['1'], readfile('Xoutput')[-1:-1])}) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 37 | call term_sendkeys(buf, "j") |
| 38 | call term_wait(buf) |
Bram Moolenaar | 17f6754 | 2020-08-20 18:29:13 +0200 | [diff] [blame] | 39 | call WaitForAssert({-> assert_equal(['1', '2'], readfile('Xoutput')[-2:-1])}) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 40 | call term_sendkeys(buf, "j") |
| 41 | call term_wait(buf) |
Bram Moolenaar | 17f6754 | 2020-08-20 18:29:13 +0200 | [diff] [blame] | 42 | call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('Xoutput')[-3:-1])}) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 43 | call StopVimInTerminal(buf) |
| 44 | |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 45 | call delete('Xinit') |
| 46 | call delete('Xoutput') |
| 47 | call delete('Xfile') |
| 48 | endfunc |
| 49 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 50 | if has('timers') |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 51 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 52 | func ExitInsertMode(id) |
| 53 | call feedkeys("\<Esc>") |
| 54 | endfunc |
| 55 | |
| 56 | func Test_cursorhold_insert() |
Bram Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 57 | " Need to move the cursor. |
| 58 | call feedkeys("ggG", "xt") |
| 59 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 60 | 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 Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 66 | unlet g:triggered |
| 67 | au! CursorHoldI |
| 68 | set updatetime& |
| 69 | endfunc |
| 70 | |
| 71 | func Test_cursorhold_insert_with_timer_interrupt() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 72 | CheckFeature job |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 73 | " 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 Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 80 | set updatetime=100 |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 81 | call job_start(has('win32') ? 'cmd /c echo:' : 'echo', |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 82 | \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}}) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 83 | call feedkeys('a', 'x!') |
| 84 | call assert_equal(1, g:triggered) |
| 85 | unlet g:triggered |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 86 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 87 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 88 | 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 Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 98 | unlet g:triggered |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 99 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 100 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 101 | endfunc |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 102 | |
| 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 | |
| 125 | endif "has('timers') |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 126 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 127 | func Test_bufunload() |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 128 | 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 Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 134 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 135 | let s:li = [] |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 136 | new |
| 137 | setlocal bufhidden= |
| 138 | bunload |
| 139 | call assert_equal(["bufunload", "bufdelete"], s:li) |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 140 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 141 | let s:li = [] |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 142 | new |
| 143 | setlocal bufhidden=delete |
| 144 | bunload |
| 145 | call assert_equal(["bufunload", "bufdelete"], s:li) |
| 146 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 147 | let s:li = [] |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 148 | new |
| 149 | setlocal bufhidden=unload |
| 150 | bwipeout |
| 151 | call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li) |
| 152 | |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 153 | au! test_bufunload_group |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 154 | augroup! test_bufunload_group |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 155 | endfunc |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 156 | |
| 157 | " SEGV occurs in older versions. (At least 7.4.2005 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 158 | func Test_autocmd_bufunload_with_tabnext() |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 159 | 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 Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 170 | autocmd! test_autocmd_bufunload_with_tabnext_group |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 171 | augroup! test_autocmd_bufunload_with_tabnext_group |
| 172 | tablast |
| 173 | quit |
| 174 | endfunc |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 175 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 176 | func Test_autocmd_bufwinleave_with_tabfirst() |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 177 | 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 Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 184 | tabclose |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 185 | endfunc |
| 186 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 187 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 188 | func Test_autocmd_bufunload_avoiding_SEGV_01() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 189 | 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 Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 197 | call assert_fails('edit bb.txt', 'E937:') |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 198 | |
| 199 | autocmd! test_autocmd_bufunload |
| 200 | augroup! test_autocmd_bufunload |
| 201 | bwipe! aa.txt |
| 202 | bwipe! bb.txt |
| 203 | endfunc |
| 204 | |
| 205 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 206 | func Test_autocmd_bufunload_avoiding_SEGV_02() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 207 | 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 |
| 222 | endfunc |
| 223 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 224 | func 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 |
| 245 | endfunc |
| 246 | |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 247 | func 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 Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 255 | au TabClosed * call add(g:record, 'TabClosed') |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 256 | 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 Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 268 | \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter', |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 269 | \ 'WinLeave', 'WinEnter' |
| 270 | \ ], g:record) |
| 271 | |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 272 | 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 Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 283 | augroup testing |
| 284 | au! |
| 285 | augroup END |
| 286 | unlet g:record |
| 287 | endfunc |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 288 | |
| 289 | func s:AddAnAutocmd() |
| 290 | augroup vimBarTest |
| 291 | au BufReadCmd * echo 'hello' |
| 292 | augroup END |
| 293 | call assert_equal(3, len(split(execute('au vimBarTest'), "\n"))) |
| 294 | endfunc |
| 295 | |
| 296 | func 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"))) |
| 315 | endfunc |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 316 | |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 317 | func RemoveGroup() |
| 318 | autocmd! StartOK |
| 319 | augroup! StartOK |
| 320 | endfunc |
| 321 | |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 322 | func Test_augroup_warning() |
| 323 | augroup TheWarning |
| 324 | au VimEnter * echo 'entering' |
| 325 | augroup END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 326 | call assert_match("TheWarning.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 327 | redir => res |
| 328 | augroup! TheWarning |
| 329 | redir END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 330 | call assert_match("W19:", res) |
| 331 | call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 332 | |
| 333 | " check "Another" does not take the pace of the deleted entry |
| 334 | augroup Another |
| 335 | augroup END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 336 | call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 337 | augroup! Another |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 338 | |
| 339 | " no warning for postpone aucmd delete |
| 340 | augroup StartOK |
| 341 | au VimEnter * call RemoveGroup() |
| 342 | augroup END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 343 | call assert_match("StartOK.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 344 | redir => res |
| 345 | doautocmd VimEnter |
| 346 | redir END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 347 | call assert_notmatch("W19:", res) |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 348 | au! VimEnter |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 349 | |
| 350 | call assert_fails('augroup!', 'E471:') |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 351 | endfunc |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 352 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 353 | func Test_BufReadCmdHelp() |
| 354 | " This used to cause access to free memory |
| 355 | au BufReadCmd * e +h |
| 356 | help |
| 357 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 358 | au! BufReadCmd |
| 359 | endfunc |
| 360 | |
| 361 | func Test_BufReadCmdHelpJump() |
| 362 | " This used to cause access to free memory |
| 363 | au BufReadCmd * e +h{ |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 364 | " } to fix highlighting |
| 365 | call assert_fails('help', 'E434:') |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 366 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 367 | au! BufReadCmd |
| 368 | endfunc |
| 369 | |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 370 | func Test_augroup_deleted() |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 371 | " This caused a crash before E936 was introduced |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 372 | augroup x |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 373 | call assert_fails('augroup! x', 'E936:') |
| 374 | au VimEnter * echo |
| 375 | augroup end |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 376 | augroup! x |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 377 | call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 378 | au! VimEnter |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 379 | endfunc |
| 380 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 381 | " Tests for autocommands on :close command. |
| 382 | " This used to be in test13. |
| 383 | func Test_three_windows() |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 384 | " Clean up buffers, because in some cases this function fails. |
| 385 | call s:cleanup_buffers() |
| 386 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 387 | " 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 Moolenaar | a997b45 | 2018-04-17 23:24:06 +0200 | [diff] [blame] | 419 | call assert_equal('Xtestje1', expand('%')) |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 420 | |
| 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 Moolenaar | 4520d44 | 2017-03-19 16:09:46 +0100 | [diff] [blame] | 440 | enew |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 441 | call delete('Xtestje1') |
| 442 | call delete('Xtestje2') |
| 443 | call delete('Xtestje3') |
| 444 | endfunc |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 445 | |
| 446 | func 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 Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 459 | |
| 460 | " On MS-Windows we can't edit the directory, make sure we wipe the right |
| 461 | " buffer. |
| 462 | bwipe! Xdir |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 463 | |
| 464 | call delete('Xdir', 'd') |
| 465 | au! BufEnter |
| 466 | endfunc |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 467 | |
| 468 | " Closing a window might cause an endless loop |
| 469 | " E814 for older Vims |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 470 | func Test_autocmd_bufwipe_in_SessLoadPost() |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 471 | edit Xtest |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 472 | tabnew |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 473 | file Xsomething |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 474 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 475 | mksession! |
| 476 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 477 | let content =<< trim [CODE] |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 478 | call test_override('ui_delay', 10) |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 479 | set nocp noswapfile |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 480 | let v:swapchoice = "e" |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 481 | 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 Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 492 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 493 | call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq') |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 494 | let errors = join(readfile('Xerrors')) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 495 | call assert_match('E814:', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 496 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 497 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 498 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 499 | call delete(file) |
| 500 | endfor |
| 501 | endfunc |
| 502 | |
Bram Moolenaar | 797e63b | 2021-01-15 16:22:52 +0100 | [diff] [blame] | 503 | " Using :blast and :ball for many events caused a crash, because b_nwindows was |
| 504 | " not incremented correctly. |
| 505 | func 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') |
| 521 | endfunc |
| 522 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 523 | " SEGV occurs in older versions. |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 524 | func Test_autocmd_bufwipe_in_SessLoadPost2() |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 525 | tabnew |
| 526 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 527 | mksession! |
| 528 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 529 | 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 Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 552 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 553 | call system(GetVimCommand('Xvimrc') .. ' --not-a-term --noplugins -S Session.vim -c cq') |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 554 | 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 Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 558 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 559 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 560 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 561 | call delete(file) |
| 562 | endfor |
| 563 | endfunc |
Bram Moolenaar | faf29d7 | 2017-07-09 11:07:16 +0200 | [diff] [blame] | 564 | |
| 565 | func Test_empty_doau() |
| 566 | doau \| |
| 567 | endfunc |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 568 | |
| 569 | func s:AutoCommandOptionSet(match) |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 570 | let template = "Option: <%s>, OldVal: <%s>, OldValLocal: <%s>, OldValGlobal: <%s>, NewVal: <%s>, Scope: <%s>, Command: <%s>\n" |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 571 | let item = remove(g:options, 0) |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 572 | 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 Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 574 | let g:opt = [expected, actual] |
| 575 | "call assert_equal(expected, actual) |
| 576 | endfunc |
| 577 | |
| 578 | func Test_OptionSet() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 579 | CheckOption autochdir |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 580 | |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 581 | badd test_autocmd.vim |
| 582 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 583 | call test_override('starting', 1) |
| 584 | set nocp |
| 585 | au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>")) |
| 586 | |
| 587 | " 1: Setting number option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 588 | let g:options = [['number', 0, 0, 0, 1, 'global', 'set']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 589 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 594 | let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 595 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 600 | let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 601 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 606 | let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 607 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 612 | let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 613 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 618 | let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 619 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 624 | let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 625 | noa setlocal ai |
| 626 | noa setglobal noai |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 627 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 633 | let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 634 | noa set nonu |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 635 | call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 636 | call assert_equal(g:opt[0], g:opt[1]) |
| 637 | |
| 638 | " 8: Setting several global list and number option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 639 | let g:options = [['list', 0, 0, 0, 1, 'global', 'set'], ['number', 0, 0, 0, 1, 'global', 'set']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 640 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 645 | let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 646 | noa set nolist nonu |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 647 | call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid'], ['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 648 | call assert_equal(g:opt[0], g:opt[1]) |
| 649 | |
| 650 | " 10: Setting global acd" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 651 | let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 652 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 657 | let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 658 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 663 | let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 664 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 669 | let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 670 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 675 | let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']] |
| 676 | let &bs = "eol,indent,start" |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 677 | call assert_equal([], g:options) |
| 678 | call assert_equal(g:opt[0], g:opt[1]) |
| 679 | |
| 680 | " 15: Setting option backspace through setbufvar()" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 681 | let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 682 | " try twice, first time, shouldn't trigger because option name is invalid, |
| 683 | " second time, it should trigger |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 684 | let bnum = bufnr('%') |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 685 | call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 686 | " should trigger, use correct option name |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 687 | call setbufvar(bnum, '&backup', 1) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 688 | call assert_equal([], g:options) |
| 689 | call assert_equal(g:opt[0], g:opt[1]) |
| 690 | |
| 691 | " 16: Setting number option using setwinvar" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 692 | let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 693 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 698 | let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 699 | setlocal key=blah |
| 700 | setlocal key= |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 701 | call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 702 | call assert_equal(g:opt[0], g:opt[1]) |
| 703 | |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 704 | |
| 705 | " 18a: Setting string global option" |
| 706 | let oldval = &backupext |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 707 | let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 708 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 713 | let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 714 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 719 | let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 720 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 728 | let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 729 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 736 | let g:options = [['backupext', 'ext_local', 'ext_local', 'ext_local', 'fuu', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 737 | 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 Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 743 | let oldval = &tags |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 744 | let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']] |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 745 | set tags=tagpath |
| 746 | call assert_equal([], g:options) |
| 747 | call assert_equal(g:opt[0], g:opt[1]) |
| 748 | |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 749 | " 19b: Resetting string local-global (to buffer) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 750 | let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']] |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 751 | set tags& |
| 752 | call assert_equal([], g:options) |
| 753 | call assert_equal(g:opt[0], g:opt[1]) |
| 754 | |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 755 | " 19c: Setting global string local-global (to buffer) option " |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 756 | let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 757 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 762 | let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 763 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 772 | let g:options = [['tags', 'tag_global', 'tag_local', 'tag_global', 'tagpath', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 773 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 782 | let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 783 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 790 | let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 791 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 796 | let g:options = [['spelllang', 'elvish,klingon', 'elvish,klingon', 'elvish,klingon', oldval, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 797 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 802 | let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 803 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 809 | let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 810 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 819 | let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 820 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 827 | let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 828 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 835 | let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 836 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 841 | let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 842 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 848 | let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 849 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 858 | let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 859 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 866 | let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 867 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 872 | let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 873 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 878 | let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 879 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 885 | let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 886 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 893 | let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 894 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 902 | let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 903 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 910 | let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 911 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 918 | let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 919 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 925 | let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 926 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 934 | let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 935 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 942 | let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 943 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 950 | let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 951 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 957 | let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 958 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 966 | let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 967 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 974 | let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 975 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 982 | let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 983 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 989 | let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 990 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1002 | let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1003 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1010 | let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1011 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1018 | let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1019 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1025 | let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1026 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1034 | let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1035 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1042 | let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1043 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1050 | let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1051 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1057 | let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1058 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1066 | let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1067 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1074 | let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1075 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1082 | let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1083 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1089 | let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1090 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1098 | let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1099 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1106 | let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1107 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1114 | let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1115 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1121 | let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1122 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1134 | let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1135 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1142 | let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1143 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1150 | let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1151 | 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1157 | let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1158 | set cursorcolumn |
| 1159 | call assert_equal([], g:options) |
| 1160 | call assert_equal(g:opt[0], g:opt[1]) |
| 1161 | |
| 1162 | |
Bram Moolenaar | 1bc353b | 2019-09-01 14:45:28 +0200 | [diff] [blame] | 1163 | " 33: Test autocommands when an option value is converted internally. |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1164 | noa set backspace=1 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1165 | let g:options = [['backspace', 'indent,eol', 'indent,eol', 'indent,eol', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1166 | set backspace=2 |
| 1167 | call assert_equal([], g:options) |
| 1168 | call assert_equal(g:opt[0], g:opt[1]) |
| 1169 | |
| 1170 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1171 | " Cleanup |
| 1172 | au! OptionSet |
Bram Moolenaar | 0331faf | 2019-06-15 18:40:37 +0200 | [diff] [blame] | 1173 | " set tags& |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1174 | 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 Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 1175 | exe printf(":set %s&vim", opt) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1176 | endfor |
| 1177 | call test_override('starting', 0) |
| 1178 | delfunc! AutoCommandOptionSet |
| 1179 | endfunc |
| 1180 | |
| 1181 | func Test_OptionSet_diffmode() |
| 1182 | call test_override('starting', 1) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 1183 | " 18: Changing an option when entering diff mode |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1184 | new |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1185 | au OptionSet diff :let &l:cul = v:option_new |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1186 | |
| 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) |
| 1212 | endfunc |
| 1213 | |
| 1214 | func 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 Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1222 | call assert_fails(':diffthis', 'E788:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1223 | call assert_equal(1, &diff) |
| 1224 | vnew |
| 1225 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1226 | call assert_fails(':diffthis', 'E788:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1227 | call assert_equal(1, &diff) |
Bram Moolenaar | a9aa86f | 2019-11-10 21:25:45 +0100 | [diff] [blame] | 1228 | set diffopt-=closeoff |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1229 | bw! |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1230 | call assert_fails(':diffoff!', 'E788:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1231 | bw! |
| 1232 | |
| 1233 | " Cleanup |
| 1234 | au! OptionSet |
| 1235 | call test_override('starting', 0) |
| 1236 | "delfunc! AutoCommandOptionSet |
| 1237 | endfunc |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 1238 | |
| 1239 | " Test for Bufleave autocommand that deletes the buffer we are about to edit. |
| 1240 | func 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 |
| 1256 | endfunc |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1257 | |
| 1258 | " Test for autocommand that changes the buffer list, when doing ":ball". |
| 1259 | func 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 |
| 1290 | endfunc |
| 1291 | |
| 1292 | " Test for autocommand that changes current buffer on BufEnter event. |
| 1293 | " Check if modelines are interpreted for the correct buffer. |
| 1294 | func 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 |
| 1327 | endfunc |
| 1328 | |
| 1329 | " Test for issue #57 |
| 1330 | " do not move cursor on <c-o> when autoindent is set |
| 1331 | func 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! |
| 1343 | endfunc |
| 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. |
| 1347 | func 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 Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 1376 | let g:bufinfo = getbufinfo() |
| 1377 | call assert_equal(1, len(g:bufinfo)) |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1378 | |
| 1379 | call delete('Xxx1') |
| 1380 | call delete('Xxx2') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1381 | call delete('test.out') |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1382 | %bwipe |
| 1383 | au! BufLeave |
Bram Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 1384 | |
| 1385 | " check that bufinfo doesn't contain a pointer to freed memory |
| 1386 | call test_garbagecollect_now() |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1387 | endfunc |
Bram Moolenaar | 87ffb5c | 2017-10-19 12:37:42 +0200 | [diff] [blame] | 1388 | |
| 1389 | func 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 |
| 1401 | endfunc |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1402 | |
| 1403 | func Test_Cmdline() |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 1404 | 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 Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1416 | 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 Moolenaar | a4baf5b | 2018-04-22 13:27:44 +0200 | [diff] [blame] | 1426 | let save_shellslash = &shellslash |
| 1427 | set noshellslash |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1428 | 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 Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1432 | new |
| 1433 | call setline(1, 'hello') |
| 1434 | call feedkeys("/hello\<CR>", 'xt') |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1435 | call assert_equal('/', g:entered) |
| 1436 | call assert_equal('/', g:left) |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1437 | bwipe! |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1438 | au! CmdlineEnter |
| 1439 | au! CmdlineLeave |
Bram Moolenaar | a4baf5b | 2018-04-22 13:27:44 +0200 | [diff] [blame] | 1440 | let &shellslash = save_shellslash |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1441 | endfunc |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1442 | |
| 1443 | " Test for BufWritePre autocommand that deletes or unloads the buffer. |
| 1444 | func 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 Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1457 | call assert_fails('w', 'E203:') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1458 | 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 Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1463 | call assert_fails('w', 'E203:') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1464 | call assert_equal('Xxx1', bufname('%')) |
| 1465 | au! BufWritePre |
| 1466 | call delete('Xxx1') |
| 1467 | call delete('Xxx2') |
| 1468 | endfunc |
| 1469 | |
| 1470 | " Test for BufUnload autocommand that unloads all the other buffers |
| 1471 | func Test_bufunload_all() |
| 1472 | call writefile(['Test file Xxx1'], 'Xxx1')" |
| 1473 | call writefile(['Test file Xxx2'], 'Xxx2')" |
| 1474 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 1475 | 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 Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1492 | call writefile(content, 'Xtest') |
| 1493 | |
| 1494 | call delete('Xout') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 1495 | call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1496 | call assert_true(filereadable('Xout')) |
| 1497 | |
| 1498 | call delete('Xxx1') |
| 1499 | call delete('Xxx2') |
| 1500 | call delete('Xtest') |
| 1501 | call delete('Xout') |
| 1502 | endfunc |
| 1503 | |
| 1504 | " Some tests for buffer-local autocommands |
| 1505 | func 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 |
| 1524 | endfunc |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1525 | |
| 1526 | " Test for "*Cmd" autocommands |
| 1527 | func 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 Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1538 | call assert_fails('read XtestA', 'E484:') " should not read anything |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1539 | 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 Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1564 | call assert_fails('r XtestC', 'E484:') " should not read anything |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1565 | 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 Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1582 | call assert_fails('$r XtestD', 'E484:') " should not read anything |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1583 | 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! |
| 1605 | endfunc |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1606 | |
Bram Moolenaar | 0fff441 | 2020-03-29 16:06:29 +0200 | [diff] [blame] | 1607 | func 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 |
| 1614 | endfunc |
| 1615 | |
| 1616 | func 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 |
| 1622 | endfunc |
| 1623 | |
| 1624 | func 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 |
| 1639 | endfunc |
| 1640 | |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1641 | func SetChangeMarks(start, end) |
Bram Moolenaar | 97c6943 | 2021-01-15 16:45:21 +0100 | [diff] [blame] | 1642 | exe a:start .. 'mark [' |
| 1643 | exe a:end .. 'mark ]' |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1644 | endfunc |
| 1645 | |
| 1646 | " Verify the effects of autocmds on '[ and '] |
| 1647 | func Test_change_mark_in_autocmds() |
| 1648 | edit! Xtest |
Bram Moolenaar | 97c6943 | 2021-01-15 16:45:21 +0100 | [diff] [blame] | 1649 | call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u\<Esc>", 'xtn') |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1650 | |
| 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 Moolenaar | 14ddd22 | 2020-08-05 12:02:40 +0200 | [diff] [blame] | 1660 | if has('unix') |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1661 | 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') |
| 1732 | endfunc |
| 1733 | |
| 1734 | func Test_Filter_noshelltemp() |
Bram Moolenaar | aeb313f | 2020-11-27 19:13:28 +0100 | [diff] [blame] | 1735 | CheckExecutable cat |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1736 | |
| 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! |
| 1764 | endfunc |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1765 | |
| 1766 | func 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 Moolenaar | 37d1673 | 2020-06-12 22:09:01 +0200 | [diff] [blame] | 1779 | \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false}, |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1780 | \g:event) |
| 1781 | norm y_ |
| 1782 | call assert_equal( |
Bram Moolenaar | 37d1673 | 2020-06-12 22:09:01 +0200 | [diff] [blame] | 1783 | \{'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 Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1788 | \g:event) |
| 1789 | call feedkeys("\<C-V>y", 'x') |
| 1790 | call assert_equal( |
Bram Moolenaar | 37d1673 | 2020-06-12 22:09:01 +0200 | [diff] [blame] | 1791 | \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true}, |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1792 | \g:event) |
| 1793 | norm "xciwbar |
| 1794 | call assert_equal( |
Bram Moolenaar | 37d1673 | 2020-06-12 22:09:01 +0200 | [diff] [blame] | 1795 | \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false}, |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1796 | \g:event) |
| 1797 | norm "bdiw |
| 1798 | call assert_equal( |
Bram Moolenaar | 37d1673 | 2020-06-12 22:09:01 +0200 | [diff] [blame] | 1799 | \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false}, |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1800 | \g:event) |
| 1801 | |
| 1802 | call assert_equal({}, v:event) |
| 1803 | |
Bram Moolenaar | fccbf06 | 2020-11-26 20:34:00 +0100 | [diff] [blame] | 1804 | 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 Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1826 | au! TextYankPost |
| 1827 | unlet g:event |
| 1828 | bwipe! |
| 1829 | endfunc |
Bram Moolenaar | 9bca805 | 2017-12-18 12:37:55 +0100 | [diff] [blame] | 1830 | |
| 1831 | func Test_nocatch_wipe_all_buffers() |
| 1832 | " Real nasty autocommand: wipe all buffers on any event. |
| 1833 | au * * bwipe * |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 1834 | call assert_fails('next x', ['E94:', 'E937:']) |
Bram Moolenaar | 9bca805 | 2017-12-18 12:37:55 +0100 | [diff] [blame] | 1835 | bwipe |
| 1836 | au! |
| 1837 | endfunc |
Bram Moolenaar | 4fb921e | 2017-12-18 15:33:00 +0100 | [diff] [blame] | 1838 | |
| 1839 | func Test_nocatch_wipe_dummy_buffer() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1840 | CheckFeature quickfix |
| 1841 | " Nasty autocommand: wipe buffer on any event. |
| 1842 | au * x bwipe |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1843 | call assert_fails('lv½ /x', 'E937:') |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1844 | au! |
Bram Moolenaar | 4fb921e | 2017-12-18 15:33:00 +0100 | [diff] [blame] | 1845 | endfunc |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1846 | |
| 1847 | function s:Before_test_dirchanged() |
| 1848 | augroup test_dirchanged |
| 1849 | autocmd! |
| 1850 | augroup END |
| 1851 | let s:li = [] |
| 1852 | let s:dir_this = getcwd() |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 1853 | let s:dir_foo = s:dir_this . '/Xfoo' |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1854 | call mkdir(s:dir_foo) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 1855 | let s:dir_bar = s:dir_this . '/Xbar' |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1856 | call mkdir(s:dir_bar) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1857 | endfunc |
| 1858 | |
| 1859 | function s:After_test_dirchanged() |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1860 | call chdir(s:dir_this) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1861 | call delete(s:dir_foo, 'd') |
| 1862 | call delete(s:dir_bar, 'd') |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1863 | augroup test_dirchanged |
| 1864 | autocmd! |
| 1865 | augroup END |
| 1866 | endfunc |
| 1867 | |
| 1868 | function 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 Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1872 | call chdir(s:dir_foo) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1873 | call assert_equal(["cd:", s:dir_foo], s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1874 | call chdir(s:dir_foo) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1875 | call assert_equal(["cd:", s:dir_foo], s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1876 | exe 'lcd ' .. fnameescape(s:dir_bar) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1877 | call assert_equal(["cd:", s:dir_foo], s:li) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1878 | call s:After_test_dirchanged() |
| 1879 | endfunc |
| 1880 | |
| 1881 | function 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 Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1885 | call chdir(s:dir_foo) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1886 | call assert_equal([], s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1887 | exe 'lcd ' .. fnameescape(s:dir_bar) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1888 | call assert_equal(["lcd:", s:dir_bar], s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1889 | exe 'lcd ' .. fnameescape(s:dir_bar) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1890 | call assert_equal(["lcd:", s:dir_bar], s:li) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1891 | call s:After_test_dirchanged() |
| 1892 | endfunc |
| 1893 | |
| 1894 | function Test_dirchanged_auto() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1895 | CheckOption autochdir |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1896 | 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 Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1901 | cd .. |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1902 | call assert_equal([], s:li) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1903 | 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 Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1906 | set noacd |
| 1907 | bwipe! |
| 1908 | call s:After_test_dirchanged() |
| 1909 | endfunc |
Bram Moolenaar | 5a09343 | 2018-02-10 18:15:19 +0100 | [diff] [blame] | 1910 | |
| 1911 | " Test TextChangedI and TextChangedP |
| 1912 | func 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! |
| 1964 | endfunc |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 1965 | |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 1966 | let g:setline_handled = v:false |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 1967 | func SetLineOne() |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 1968 | if !g:setline_handled |
| 1969 | call setline(1, "(x)") |
| 1970 | let g:setline_handled = v:true |
| 1971 | endif |
| 1972 | endfunc |
| 1973 | |
| 1974 | func 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 Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 1982 | call assert_equal('', getline(1)) |
Bram Moolenaar | 9fa9506 | 2018-08-08 22:08:32 +0200 | [diff] [blame] | 1983 | call assert_equal('', getline(2)) |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 1984 | |
| 1985 | call test_override('starting', 0) |
| 1986 | bwipe! |
| 1987 | endfunc |
| 1988 | |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 1989 | func Test_Changed_FirstTime() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 1990 | CheckFeature terminal |
| 1991 | CheckNotGui |
Bram Moolenaar | 3cdcb09 | 2020-03-18 19:18:10 +0100 | [diff] [blame] | 1992 | " Starting a terminal to run Vim is always considered flaky. |
Bram Moolenaar | 30d53e2 | 2020-03-18 21:10:44 +0100 | [diff] [blame] | 1993 | let g:test_is_flaky = 1 |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 1994 | |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 1995 | " 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 Moolenaar | 1834d37 | 2018-03-29 17:40:46 +0200 | [diff] [blame] | 1999 | " Wait for the ruler (in the status line) to be shown. |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 2000 | " 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 Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2007 | " 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 Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 2010 | call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))}) |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2011 | call assert_equal([''], readfile('Xchanged.txt')) |
| 2012 | |
| 2013 | " clean up |
| 2014 | call delete('Xchanged.txt') |
| 2015 | bwipe! |
| 2016 | endfunc |
Bram Moolenaar | 0566e89 | 2019-01-24 19:37:40 +0100 | [diff] [blame] | 2017 | |
Bram Moolenaar | eb93f3f | 2019-04-04 15:04:56 +0200 | [diff] [blame] | 2018 | func 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:') |
| 2057 | endfunc |
| 2058 | |
| 2059 | func 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:') |
| 2086 | endfunc |
| 2087 | |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2088 | func Test_autocmd_bufreadpre() |
| 2089 | new |
| 2090 | let b:bufreadpre = 1 |
Bram Moolenaar | ab505b1 | 2020-03-23 19:28:44 +0100 | [diff] [blame] | 2091 | call append(0, range(1000)) |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2092 | w! XAutocmdBufReadPre.txt |
| 2093 | autocmd BufReadPre <buffer> :let b:bufreadpre += 1 |
Bram Moolenaar | ab505b1 | 2020-03-23 19:28:44 +0100 | [diff] [blame] | 2094 | norm! 500gg |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2095 | sp |
Bram Moolenaar | ab505b1 | 2020-03-23 19:28:44 +0100 | [diff] [blame] | 2096 | norm! 1000gg |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2097 | 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 |
| 2131 | endfunc |
| 2132 | |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 2133 | " FileChangedShell tested in test_filechanged.vim |
Bram Moolenaar | 69ea587 | 2019-04-25 20:29:00 +0200 | [diff] [blame] | 2134 | |
| 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 |
| 2144 | func Test_ReadWrite_Autocmds() |
| 2145 | " Run this test only on Unix-like systems and if gzip is available |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 2146 | CheckUnix |
| 2147 | CheckExecutable gzip |
Bram Moolenaar | 69ea587 | 2019-04-25 20:29:00 +0200 | [diff] [blame] | 2148 | |
| 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') |
| 2334 | endfunc |
Bram Moolenaar | 23b5139 | 2019-05-09 21:38:43 +0200 | [diff] [blame] | 2335 | |
| 2336 | func 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 |
| 2353 | endfunc |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2354 | |
| 2355 | func 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 Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 2370 | " Sometimes we loop to handle a K_IGNORE, SafeState may be triggered once or |
Bram Moolenaar | 8fb1b47 | 2020-02-23 16:16:26 +0100 | [diff] [blame] | 2371 | " more often. |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2372 | call term_sendkeys(buf, ":echo g:safe\<CR>") |
Bram Moolenaar | 8fb1b47 | 2020-02-23 16:16:26 +0100 | [diff] [blame] | 2373 | call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000) |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2374 | |
Bram Moolenaar | 8fb1b47 | 2020-02-23 16:16:26 +0100 | [diff] [blame] | 2375 | " SafeStateAgain should be invoked at least three times |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2376 | call term_sendkeys(buf, ":echo g:again\<CR>") |
Bram Moolenaar | 8fb1b47 | 2020-02-23 16:16:26 +0100 | [diff] [blame] | 2377 | call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000) |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2378 | |
| 2379 | call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2380 | call TermWait(buf, 50) |
Bram Moolenaar | 0f6629a | 2019-09-22 23:24:13 +0200 | [diff] [blame] | 2381 | call term_sendkeys(buf, ":\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2382 | call TermWait(buf, 50) |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2383 | 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') |
| 2388 | endfunc |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2389 | |
| 2390 | func 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 Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 2401 | let filename = 'XCmdWinEnter' |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2402 | call writefile(lines, filename) |
| 2403 | let buf = RunVimInTerminal('-S '.filename, #{rows: 6}) |
| 2404 | |
| 2405 | call term_sendkeys(buf, "q:") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2406 | call TermWait(buf) |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2407 | call term_sendkeys(buf, ":echo b:dummy_var\<cr>") |
Bram Moolenaar | 353c351 | 2020-03-15 14:19:26 +0100 | [diff] [blame] | 2408 | call WaitForAssert({-> assert_match('^This is a dummy', term_getline(buf, 6))}, 2000) |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2409 | 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) |
| 2417 | endfunc |
Bram Moolenaar | ec66c41 | 2019-10-11 21:19:13 +0200 | [diff] [blame] | 2418 | |
| 2419 | func Test_autocmd_was_using_freed_memory() |
Bram Moolenaar | 5a4c308 | 2019-12-01 15:23:11 +0100 | [diff] [blame] | 2420 | CheckFeature quickfix |
| 2421 | |
Bram Moolenaar | ec66c41 | 2019-10-11 21:19:13 +0200 | [diff] [blame] | 2422 | pedit xx |
| 2423 | n x |
| 2424 | au WinEnter * quit |
| 2425 | split |
| 2426 | au! WinEnter |
| 2427 | endfunc |
Bram Moolenaar | f4a1d1c | 2019-11-16 13:50:25 +0100 | [diff] [blame] | 2428 | |
| 2429 | func 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') |
| 2461 | endfunc |
Bram Moolenaar | ce6db02 | 2020-01-07 20:11:42 +0100 | [diff] [blame] | 2462 | |
| 2463 | func 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= |
| 2483 | endfunc |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 2484 | |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 2485 | " Test closing a window or editing another buffer from a FileChangedRO handler |
| 2486 | " in a readonly buffer |
| 2487 | func Test_FileChangedRO_winclose() |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 2488 | call test_override('ui_delay', 10) |
| 2489 | |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 2490 | 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 Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 2509 | call test_override('ALL', 0) |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 2510 | endfunc |
| 2511 | |
Bram Moolenaar | 0c81d1b | 2020-02-22 22:45:55 +0100 | [diff] [blame] | 2512 | func LogACmd() |
| 2513 | call add(g:logged, line('$')) |
| 2514 | endfunc |
| 2515 | |
| 2516 | func Test_TermChanged() |
Bram Moolenaar | d28e0b3 | 2020-02-22 23:08:52 +0100 | [diff] [blame] | 2517 | CheckNotGui |
| 2518 | |
Bram Moolenaar | 0c81d1b | 2020-02-22 22:45:55 +0100 | [diff] [blame] | 2519 | 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! |
| 2532 | endfunc |
| 2533 | |
Bram Moolenaar | e328487 | 2020-03-19 13:55:03 +0100 | [diff] [blame] | 2534 | " Test for FileReadCmd autocmd |
| 2535 | func 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 |
| 2573 | endfunc |
| 2574 | |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 2575 | " Test for passing invalid arguments to autocmd |
| 2576 | func 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 Moolenaar | 531be47 | 2020-09-23 22:38:05 +0200 | [diff] [blame] | 2590 | call assert_fails('autocmd BufNew \) set ff=unix', 'E55:') |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 2591 | endfunc |
| 2592 | |
| 2593 | " Test for deep nesting of autocmds |
| 2594 | func Test_autocmd_deep_nesting() |
| 2595 | autocmd BufEnter Xfile doautocmd BufEnter Xfile |
| 2596 | call assert_fails('doautocmd BufEnter Xfile', 'E218:') |
| 2597 | autocmd! BufEnter Xfile |
| 2598 | endfunc |
| 2599 | |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 2600 | " Tests for SigUSR1 autocmd event, which is only available on posix systems. |
| 2601 | func Test_autocmd_sigusr1() |
| 2602 | CheckUnix |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 2603 | CheckExecutable /bin/kill |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 2604 | |
| 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 |
| 2612 | endfunc |
| 2613 | |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 2614 | " Test for BufReadPre autocmd deleting the file |
| 2615 | func 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! |
| 2629 | endfunc |
| 2630 | |
| 2631 | " Test for BufReadPre autocmd changing the current buffer |
| 2632 | func 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! |
| 2646 | endfunc |
| 2647 | |
| 2648 | " Test for BufWipeouti autocmd changing the current buffer when reading a file |
| 2649 | " in an empty buffer with 'f' flag in 'cpo' |
| 2650 | func 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 Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 2658 | call assert_fails('r Xfile', ['E812:', 'E484:']) |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 2659 | call assert_equal('somefile', @%) |
| 2660 | let &cpo = save_cpo |
| 2661 | augroup TestAuCmd |
| 2662 | au! |
| 2663 | augroup END |
| 2664 | close! |
| 2665 | endfunc |
| 2666 | |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2667 | " Test for the temporary internal window used to execute autocmds |
| 2668 | func Test_autocmd_window() |
| 2669 | %bw! |
| 2670 | edit one.txt |
| 2671 | tabnew two.txt |
| 2672 | let g:blist = [] |
Bram Moolenaar | 832adf9 | 2020-06-25 19:01:36 +0200 | [diff] [blame] | 2673 | augroup aucmd_win_test1 |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2674 | au! |
| 2675 | au BufEnter * call add(g:blist, [expand('<afile>'), |
| 2676 | \ win_gettype(bufwinnr(expand('<afile>')))]) |
| 2677 | augroup END |
| 2678 | |
| 2679 | doautoall BufEnter |
Bram Moolenaar | 40a019f | 2020-06-17 21:41:35 +0200 | [diff] [blame] | 2680 | call assert_equal([['one.txt', 'autocmd'], ['two.txt', '']], g:blist) |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2681 | |
Bram Moolenaar | 832adf9 | 2020-06-25 19:01:36 +0200 | [diff] [blame] | 2682 | augroup aucmd_win_test1 |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2683 | au! |
| 2684 | augroup END |
Bram Moolenaar | 832adf9 | 2020-06-25 19:01:36 +0200 | [diff] [blame] | 2685 | augroup! aucmd_win_test1 |
| 2686 | %bw! |
| 2687 | endfunc |
| 2688 | |
| 2689 | " Test for trying to close the temporary window used for executing an autocmd |
| 2690 | func 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 Moolenaar | cf84417 | 2020-06-26 19:44:06 +0200 | [diff] [blame] | 2705 | %bwipe! |
| 2706 | endfunc |
| 2707 | |
| 2708 | " Test for trying to close the tab that has the temporary window for exeucing |
| 2709 | " an autocmd. |
| 2710 | func 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 Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2726 | endfunc |
| 2727 | |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 2728 | " vim: shiftwidth=2 sts=2 expandtab |