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 |
LemonBoy | 0937182 | 2022-04-08 15:18:45 +0100 | [diff] [blame] | 6 | source screendump.vim |
Bram Moolenaar | 6f2465d | 2022-03-22 18:13:01 +0000 | [diff] [blame] | 7 | import './vim9.vim' as v9 |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 8 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 9 | func s:cleanup_buffers() abort |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 10 | for bnr in range(1, bufnr('$')) |
| 11 | if bufloaded(bnr) && bufnr('%') != bnr |
| 12 | execute 'bd! ' . bnr |
| 13 | endif |
| 14 | endfor |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 15 | endfunc |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 16 | |
Bram Moolenaar | 1473551 | 2016-03-26 21:00:08 +0100 | [diff] [blame] | 17 | func Test_vim_did_enter() |
| 18 | call assert_false(v:vim_did_enter) |
| 19 | |
| 20 | " This script will never reach the main loop, can't check if v:vim_did_enter |
| 21 | " becomes one. |
| 22 | endfunc |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 23 | |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 24 | " Test for the CursorHold autocmd |
| 25 | func Test_CursorHold_autocmd() |
| 26 | CheckRunVimInTerminal |
| 27 | call writefile(['one', 'two', 'three'], 'Xfile') |
| 28 | let before =<< trim END |
| 29 | set updatetime=10 |
| 30 | au CursorHold * call writefile([line('.')], 'Xoutput', 'a') |
| 31 | END |
| 32 | call writefile(before, 'Xinit') |
| 33 | let buf = RunVimInTerminal('-S Xinit Xfile', {}) |
Bram Moolenaar | 17f6754 | 2020-08-20 18:29:13 +0200 | [diff] [blame] | 34 | call term_sendkeys(buf, "G") |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 35 | call term_wait(buf, 50) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 36 | call term_sendkeys(buf, "gg") |
| 37 | call term_wait(buf) |
Bram Moolenaar | 17f6754 | 2020-08-20 18:29:13 +0200 | [diff] [blame] | 38 | call WaitForAssert({-> assert_equal(['1'], readfile('Xoutput')[-1:-1])}) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 39 | call term_sendkeys(buf, "j") |
| 40 | call term_wait(buf) |
Bram Moolenaar | 17f6754 | 2020-08-20 18:29:13 +0200 | [diff] [blame] | 41 | call WaitForAssert({-> assert_equal(['1', '2'], readfile('Xoutput')[-2:-1])}) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 42 | call term_sendkeys(buf, "j") |
| 43 | call term_wait(buf) |
Bram Moolenaar | 17f6754 | 2020-08-20 18:29:13 +0200 | [diff] [blame] | 44 | call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('Xoutput')[-3:-1])}) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 45 | call StopVimInTerminal(buf) |
| 46 | |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 47 | call delete('Xinit') |
| 48 | call delete('Xoutput') |
| 49 | call delete('Xfile') |
| 50 | endfunc |
| 51 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 52 | if has('timers') |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 53 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 54 | func ExitInsertMode(id) |
| 55 | call feedkeys("\<Esc>") |
| 56 | endfunc |
| 57 | |
| 58 | func Test_cursorhold_insert() |
Bram Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 59 | " Need to move the cursor. |
| 60 | call feedkeys("ggG", "xt") |
| 61 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 62 | let g:triggered = 0 |
| 63 | au CursorHoldI * let g:triggered += 1 |
| 64 | set updatetime=20 |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 65 | call timer_start(200, 'ExitInsertMode') |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 66 | call feedkeys('a', 'x!') |
| 67 | call assert_equal(1, g:triggered) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 68 | unlet g:triggered |
| 69 | au! CursorHoldI |
| 70 | set updatetime& |
| 71 | endfunc |
| 72 | |
| 73 | func Test_cursorhold_insert_with_timer_interrupt() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 74 | CheckFeature job |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 75 | " Need to move the cursor. |
| 76 | call feedkeys("ggG", "xt") |
| 77 | |
| 78 | " Confirm the timer invoked in exit_cb of the job doesn't disturb |
| 79 | " CursorHoldI event. |
| 80 | let g:triggered = 0 |
| 81 | au CursorHoldI * let g:triggered += 1 |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 82 | set updatetime=100 |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 83 | call job_start(has('win32') ? 'cmd /c echo:' : 'echo', |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 84 | \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}}) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 85 | call feedkeys('a', 'x!') |
| 86 | call assert_equal(1, g:triggered) |
| 87 | unlet g:triggered |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 88 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 89 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 90 | endfunc |
| 91 | |
| 92 | func Test_cursorhold_insert_ctrl_x() |
| 93 | let g:triggered = 0 |
| 94 | au CursorHoldI * let g:triggered += 1 |
| 95 | set updatetime=20 |
| 96 | call timer_start(100, 'ExitInsertMode') |
| 97 | " CursorHoldI does not trigger after CTRL-X |
| 98 | call feedkeys("a\<C-X>", 'x!') |
| 99 | call assert_equal(0, g:triggered) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 100 | unlet g:triggered |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 101 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 102 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 103 | endfunc |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 104 | |
Bram Moolenaar | 5a9357d | 2021-10-03 16:22:05 +0100 | [diff] [blame] | 105 | func Test_cursorhold_insert_ctrl_g_U() |
| 106 | au CursorHoldI * : |
| 107 | set updatetime=20 |
| 108 | new |
| 109 | call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') }) |
| 110 | call feedkeys("i()\<C-g>U", 'tx!') |
| 111 | sleep 200m |
| 112 | call assert_equal('(foo)', getline(1)) |
| 113 | undo |
| 114 | call assert_equal('', getline(1)) |
| 115 | |
| 116 | bwipe! |
| 117 | au! CursorHoldI |
| 118 | set updatetime& |
| 119 | endfunc |
| 120 | |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 121 | func Test_OptionSet_modeline() |
| 122 | call test_override('starting', 1) |
| 123 | au! OptionSet |
| 124 | augroup set_tabstop |
| 125 | au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")}) |
| 126 | augroup END |
| 127 | call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline') |
| 128 | set modeline |
| 129 | let v:errmsg = '' |
| 130 | call assert_fails('split XoptionsetModeline', 'E12:') |
| 131 | call assert_equal(7, &ts) |
| 132 | call assert_equal('', v:errmsg) |
| 133 | |
| 134 | augroup set_tabstop |
| 135 | au! |
| 136 | augroup END |
| 137 | bwipe! |
| 138 | set ts& |
| 139 | call delete('XoptionsetModeline') |
| 140 | call test_override('starting', 0) |
| 141 | endfunc |
| 142 | |
| 143 | endif "has('timers') |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 144 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 145 | func Test_bufunload() |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 146 | augroup test_bufunload_group |
| 147 | autocmd! |
| 148 | autocmd BufUnload * call add(s:li, "bufunload") |
| 149 | autocmd BufDelete * call add(s:li, "bufdelete") |
| 150 | autocmd BufWipeout * call add(s:li, "bufwipeout") |
| 151 | augroup END |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 152 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 153 | let s:li = [] |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 154 | new |
| 155 | setlocal bufhidden= |
| 156 | bunload |
| 157 | call assert_equal(["bufunload", "bufdelete"], s:li) |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 158 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 159 | let s:li = [] |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 160 | new |
| 161 | setlocal bufhidden=delete |
| 162 | bunload |
| 163 | call assert_equal(["bufunload", "bufdelete"], s:li) |
| 164 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 165 | let s:li = [] |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 166 | new |
| 167 | setlocal bufhidden=unload |
| 168 | bwipeout |
| 169 | call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li) |
| 170 | |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 171 | au! test_bufunload_group |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 172 | augroup! test_bufunload_group |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 173 | endfunc |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 174 | |
| 175 | " SEGV occurs in older versions. (At least 7.4.2005 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 176 | func Test_autocmd_bufunload_with_tabnext() |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 177 | tabedit |
| 178 | tabfirst |
| 179 | |
| 180 | augroup test_autocmd_bufunload_with_tabnext_group |
| 181 | autocmd! |
| 182 | autocmd BufUnload <buffer> tabnext |
| 183 | augroup END |
| 184 | |
| 185 | quit |
| 186 | call assert_equal(2, tabpagenr('$')) |
| 187 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 188 | autocmd! test_autocmd_bufunload_with_tabnext_group |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 189 | augroup! test_autocmd_bufunload_with_tabnext_group |
| 190 | tablast |
| 191 | quit |
| 192 | endfunc |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 193 | |
Bram Moolenaar | 5ed58c7 | 2021-01-28 14:24:55 +0100 | [diff] [blame] | 194 | func Test_argdelete_in_next() |
| 195 | au BufNew,BufEnter,BufLeave,BufWinEnter * argdel |
| 196 | call assert_fails('next a b', 'E1156:') |
| 197 | au! BufNew,BufEnter,BufLeave,BufWinEnter * |
| 198 | endfunc |
| 199 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 200 | func Test_autocmd_bufwinleave_with_tabfirst() |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 201 | tabedit |
| 202 | augroup sample |
| 203 | autocmd! |
| 204 | autocmd BufWinLeave <buffer> tabfirst |
| 205 | augroup END |
| 206 | call setline(1, ['a', 'b', 'c']) |
| 207 | edit! a.txt |
Bram Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 208 | tabclose |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 209 | endfunc |
| 210 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 211 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 212 | func Test_autocmd_bufunload_avoiding_SEGV_01() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 213 | split aa.txt |
| 214 | let lastbuf = bufnr('$') |
| 215 | |
| 216 | augroup test_autocmd_bufunload |
| 217 | autocmd! |
| 218 | exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!' |
| 219 | augroup END |
| 220 | |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 221 | call assert_fails('edit bb.txt', 'E937:') |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 222 | |
| 223 | autocmd! test_autocmd_bufunload |
| 224 | augroup! test_autocmd_bufunload |
| 225 | bwipe! aa.txt |
| 226 | bwipe! bb.txt |
| 227 | endfunc |
| 228 | |
| 229 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 230 | func Test_autocmd_bufunload_avoiding_SEGV_02() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 231 | setlocal buftype=nowrite |
| 232 | let lastbuf = bufnr('$') |
| 233 | |
| 234 | augroup test_autocmd_bufunload |
| 235 | autocmd! |
| 236 | exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!' |
| 237 | augroup END |
| 238 | |
| 239 | normal! i1 |
| 240 | call assert_fails('edit a.txt', 'E517:') |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 241 | |
| 242 | autocmd! test_autocmd_bufunload |
| 243 | augroup! test_autocmd_bufunload |
| 244 | bwipe! a.txt |
| 245 | endfunc |
| 246 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 247 | func Test_autocmd_dummy_wipeout() |
| 248 | " prepare files |
| 249 | call writefile([''], 'Xdummywipetest1.txt') |
| 250 | call writefile([''], 'Xdummywipetest2.txt') |
| 251 | augroup test_bufunload_group |
| 252 | autocmd! |
| 253 | autocmd BufUnload * call add(s:li, "bufunload") |
| 254 | autocmd BufDelete * call add(s:li, "bufdelete") |
| 255 | autocmd BufWipeout * call add(s:li, "bufwipeout") |
| 256 | augroup END |
| 257 | |
| 258 | let s:li = [] |
| 259 | split Xdummywipetest1.txt |
| 260 | silent! vimgrep /notmatched/ Xdummywipetest* |
| 261 | call assert_equal(["bufunload", "bufwipeout"], s:li) |
| 262 | |
| 263 | bwipeout |
| 264 | call delete('Xdummywipetest1.txt') |
| 265 | call delete('Xdummywipetest2.txt') |
| 266 | au! test_bufunload_group |
| 267 | augroup! test_bufunload_group |
| 268 | endfunc |
| 269 | |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 270 | func Test_win_tab_autocmd() |
| 271 | let g:record = [] |
| 272 | |
| 273 | augroup testing |
| 274 | au WinNew * call add(g:record, 'WinNew') |
naohiro ono | 23beefe | 2021-11-13 12:38:49 +0000 | [diff] [blame] | 275 | au WinClosed * call add(g:record, 'WinClosed') |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 276 | au WinEnter * call add(g:record, 'WinEnter') |
| 277 | au WinLeave * call add(g:record, 'WinLeave') |
| 278 | au TabNew * call add(g:record, 'TabNew') |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 279 | au TabClosed * call add(g:record, 'TabClosed') |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 280 | au TabEnter * call add(g:record, 'TabEnter') |
| 281 | au TabLeave * call add(g:record, 'TabLeave') |
| 282 | augroup END |
| 283 | |
| 284 | split |
| 285 | tabnew |
| 286 | close |
| 287 | close |
| 288 | |
| 289 | call assert_equal([ |
| 290 | \ 'WinLeave', 'WinNew', 'WinEnter', |
| 291 | \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', |
naohiro ono | 23beefe | 2021-11-13 12:38:49 +0000 | [diff] [blame] | 292 | \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter', |
| 293 | \ 'WinLeave', 'WinClosed', 'WinEnter' |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 294 | \ ], g:record) |
| 295 | |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 296 | let g:record = [] |
| 297 | tabnew somefile |
| 298 | tabnext |
| 299 | bwipe somefile |
| 300 | |
| 301 | call assert_equal([ |
| 302 | \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', |
| 303 | \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', |
naohiro ono | 23beefe | 2021-11-13 12:38:49 +0000 | [diff] [blame] | 304 | \ 'WinClosed', 'TabClosed' |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 305 | \ ], g:record) |
| 306 | |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 307 | augroup testing |
| 308 | au! |
| 309 | augroup END |
| 310 | unlet g:record |
| 311 | endfunc |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 312 | |
LemonBoy | 0937182 | 2022-04-08 15:18:45 +0100 | [diff] [blame] | 313 | func Test_WinScrolled() |
| 314 | CheckRunVimInTerminal |
| 315 | |
| 316 | let lines =<< trim END |
zeertzjq | d58862d | 2022-04-12 11:32:48 +0100 | [diff] [blame] | 317 | set nowrap scrolloff=0 |
| 318 | for ii in range(1, 18) |
| 319 | call setline(ii, repeat(nr2char(96 + ii), ii * 2)) |
| 320 | endfor |
| 321 | let win_id = win_getid() |
| 322 | let g:matched = v:false |
| 323 | execute 'au WinScrolled' win_id 'let g:matched = v:true' |
| 324 | let g:scrolled = 0 |
| 325 | au WinScrolled * let g:scrolled += 1 |
| 326 | au WinScrolled * let g:amatch = str2nr(expand('<amatch>')) |
| 327 | au WinScrolled * let g:afile = str2nr(expand('<afile>')) |
LemonBoy | 0937182 | 2022-04-08 15:18:45 +0100 | [diff] [blame] | 328 | END |
| 329 | call writefile(lines, 'Xtest_winscrolled') |
| 330 | let buf = RunVimInTerminal('-S Xtest_winscrolled', {'rows': 6}) |
| 331 | |
| 332 | call term_sendkeys(buf, ":echo g:scrolled\<CR>") |
| 333 | call WaitForAssert({-> assert_match('^0 ', term_getline(buf, 6))}, 1000) |
| 334 | |
| 335 | " Scroll left/right in Normal mode. |
| 336 | call term_sendkeys(buf, "zlzh:echo g:scrolled\<CR>") |
| 337 | call WaitForAssert({-> assert_match('^2 ', term_getline(buf, 6))}, 1000) |
| 338 | |
| 339 | " Scroll up/down in Normal mode. |
| 340 | call term_sendkeys(buf, "\<c-e>\<c-y>:echo g:scrolled\<CR>") |
| 341 | call WaitForAssert({-> assert_match('^4 ', term_getline(buf, 6))}, 1000) |
| 342 | |
| 343 | " Scroll up/down in Insert mode. |
| 344 | call term_sendkeys(buf, "Mi\<c-x>\<c-e>\<Esc>i\<c-x>\<c-y>\<Esc>") |
| 345 | call term_sendkeys(buf, ":echo g:scrolled\<CR>") |
| 346 | call WaitForAssert({-> assert_match('^6 ', term_getline(buf, 6))}, 1000) |
| 347 | |
| 348 | " Scroll the window horizontally to focus the last letter of the third line |
| 349 | " containing only six characters. Moving to the previous and shorter lines |
| 350 | " should trigger another autocommand as Vim has to make them visible. |
| 351 | call term_sendkeys(buf, "5zl2k") |
| 352 | call term_sendkeys(buf, ":echo g:scrolled\<CR>") |
| 353 | call WaitForAssert({-> assert_match('^8 ', term_getline(buf, 6))}, 1000) |
| 354 | |
| 355 | " Ensure the command was triggered for the specified window ID. |
| 356 | call term_sendkeys(buf, ":echo g:matched\<CR>") |
| 357 | call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000) |
| 358 | |
| 359 | " Ensure the expansion of <amatch> and <afile> matches the window ID. |
| 360 | call term_sendkeys(buf, ":echo g:amatch == win_id && g:afile == win_id\<CR>") |
| 361 | call WaitForAssert({-> assert_match('^v:true ', term_getline(buf, 6))}, 1000) |
| 362 | |
| 363 | call StopVimInTerminal(buf) |
| 364 | call delete('Xtest_winscrolled') |
| 365 | endfunc |
| 366 | |
LemonBoy | 66e13ae | 2022-04-21 22:52:11 +0100 | [diff] [blame] | 367 | func Test_WinScrolled_mouse() |
| 368 | CheckRunVimInTerminal |
| 369 | |
| 370 | let lines =<< trim END |
| 371 | set nowrap scrolloff=0 |
| 372 | set mouse=a term=xterm ttymouse=sgr mousetime=200 clipboard= |
| 373 | call setline(1, ['foo']->repeat(32)) |
| 374 | split |
| 375 | let g:scrolled = 0 |
| 376 | au WinScrolled * let g:scrolled += 1 |
| 377 | END |
| 378 | call writefile(lines, 'Xtest_winscrolled_mouse') |
| 379 | let buf = RunVimInTerminal('-S Xtest_winscrolled_mouse', {'rows': 10}) |
| 380 | |
| 381 | " With the upper split focused, send a scroll-down event to the unfocused one. |
| 382 | call test_setmouse(7, 1) |
| 383 | call term_sendkeys(buf, "\<ScrollWheelDown>") |
| 384 | call TermWait(buf) |
| 385 | call term_sendkeys(buf, ":echo g:scrolled\<CR>") |
| 386 | call WaitForAssert({-> assert_match('^1', term_getline(buf, 10))}, 1000) |
| 387 | |
| 388 | " Again, but this time while we're in insert mode. |
| 389 | call term_sendkeys(buf, "i\<ScrollWheelDown>\<Esc>") |
| 390 | call TermWait(buf) |
| 391 | call term_sendkeys(buf, ":echo g:scrolled\<CR>") |
| 392 | call WaitForAssert({-> assert_match('^2', term_getline(buf, 10))}, 1000) |
| 393 | |
| 394 | call StopVimInTerminal(buf) |
| 395 | call delete('Xtest_winscrolled_mouse') |
| 396 | endfunc |
| 397 | |
zeertzjq | d58862d | 2022-04-12 11:32:48 +0100 | [diff] [blame] | 398 | func Test_WinScrolled_close_curwin() |
| 399 | CheckRunVimInTerminal |
| 400 | |
| 401 | let lines =<< trim END |
| 402 | set nowrap scrolloff=0 |
| 403 | call setline(1, ['aaa', 'bbb']) |
| 404 | vsplit |
| 405 | au WinScrolled * close |
| 406 | au VimLeave * call writefile(['123456'], 'Xtestout') |
| 407 | END |
| 408 | call writefile(lines, 'Xtest_winscrolled_close_curwin') |
| 409 | let buf = RunVimInTerminal('-S Xtest_winscrolled_close_curwin', {'rows': 6}) |
| 410 | |
| 411 | " This was using freed memory |
| 412 | call term_sendkeys(buf, "\<C-E>") |
| 413 | call TermWait(buf) |
| 414 | call StopVimInTerminal(buf) |
| 415 | |
| 416 | call assert_equal(['123456'], readfile('Xtestout')) |
| 417 | |
| 418 | call delete('Xtest_winscrolled_close_curwin') |
| 419 | call delete('Xtestout') |
| 420 | endfunc |
| 421 | |
naohiro ono | 23beefe | 2021-11-13 12:38:49 +0000 | [diff] [blame] | 422 | func Test_WinClosed() |
| 423 | " Test that the pattern is matched against the closed window's ID, and both |
| 424 | " <amatch> and <afile> are set to it. |
| 425 | new |
| 426 | let winid = win_getid() |
| 427 | let g:matched = v:false |
| 428 | augroup test-WinClosed |
| 429 | autocmd! |
| 430 | execute 'autocmd WinClosed' winid 'let g:matched = v:true' |
| 431 | autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>')) |
| 432 | autocmd WinClosed * let g:afile = str2nr(expand('<afile>')) |
| 433 | augroup END |
| 434 | close |
| 435 | call assert_true(g:matched) |
| 436 | call assert_equal(winid, g:amatch) |
| 437 | call assert_equal(winid, g:afile) |
| 438 | |
| 439 | " Test that WinClosed is non-recursive. |
| 440 | new |
| 441 | new |
| 442 | call assert_equal(3, winnr('$')) |
| 443 | let g:triggered = 0 |
| 444 | augroup test-WinClosed |
| 445 | autocmd! |
| 446 | autocmd WinClosed * let g:triggered += 1 |
| 447 | autocmd WinClosed * 2 wincmd c |
| 448 | augroup END |
| 449 | close |
| 450 | call assert_equal(1, winnr('$')) |
| 451 | call assert_equal(1, g:triggered) |
| 452 | |
| 453 | autocmd! test-WinClosed |
| 454 | augroup! test-WinClosed |
| 455 | unlet g:matched |
| 456 | unlet g:amatch |
| 457 | unlet g:afile |
| 458 | unlet g:triggered |
| 459 | endfunc |
| 460 | |
Bram Moolenaar | c947b9a | 2022-04-06 17:59:21 +0100 | [diff] [blame] | 461 | func Test_WinClosed_throws() |
| 462 | vnew |
| 463 | let bnr = bufnr() |
| 464 | call assert_equal(1, bufloaded(bnr)) |
| 465 | augroup test-WinClosed |
| 466 | autocmd WinClosed * throw 'foo' |
| 467 | augroup END |
| 468 | try |
| 469 | close |
| 470 | catch /.*/ |
| 471 | endtry |
| 472 | call assert_equal(0, bufloaded(bnr)) |
| 473 | |
| 474 | autocmd! test-WinClosed |
| 475 | augroup! test-WinClosed |
| 476 | endfunc |
| 477 | |
zeertzjq | 6a06940 | 2022-04-07 14:08:29 +0100 | [diff] [blame] | 478 | func Test_WinClosed_throws_with_tabs() |
| 479 | tabnew |
| 480 | let bnr = bufnr() |
| 481 | call assert_equal(1, bufloaded(bnr)) |
| 482 | augroup test-WinClosed |
| 483 | autocmd WinClosed * throw 'foo' |
| 484 | augroup END |
| 485 | try |
| 486 | close |
| 487 | catch /.*/ |
| 488 | endtry |
| 489 | call assert_equal(0, bufloaded(bnr)) |
| 490 | |
| 491 | autocmd! test-WinClosed |
| 492 | augroup! test-WinClosed |
| 493 | endfunc |
| 494 | |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 495 | func s:AddAnAutocmd() |
| 496 | augroup vimBarTest |
| 497 | au BufReadCmd * echo 'hello' |
| 498 | augroup END |
| 499 | call assert_equal(3, len(split(execute('au vimBarTest'), "\n"))) |
| 500 | endfunc |
| 501 | |
| 502 | func Test_early_bar() |
| 503 | " test that a bar is recognized before the {event} |
| 504 | call s:AddAnAutocmd() |
Bram Moolenaar | b8e642f | 2021-11-20 10:38:25 +0000 | [diff] [blame] | 505 | augroup vimBarTest | au! | let done = 77 | augroup END |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 506 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
Bram Moolenaar | b8e642f | 2021-11-20 10:38:25 +0000 | [diff] [blame] | 507 | call assert_equal(77, done) |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 508 | |
| 509 | call s:AddAnAutocmd() |
Bram Moolenaar | b8e642f | 2021-11-20 10:38:25 +0000 | [diff] [blame] | 510 | augroup vimBarTest| au!| let done = 88 | augroup END |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 511 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
Bram Moolenaar | b8e642f | 2021-11-20 10:38:25 +0000 | [diff] [blame] | 512 | call assert_equal(88, done) |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 513 | |
| 514 | " test that a bar is recognized after the {event} |
| 515 | call s:AddAnAutocmd() |
Bram Moolenaar | b8e642f | 2021-11-20 10:38:25 +0000 | [diff] [blame] | 516 | augroup vimBarTest| au!BufReadCmd| let done = 99 | augroup END |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 517 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
Bram Moolenaar | b8e642f | 2021-11-20 10:38:25 +0000 | [diff] [blame] | 518 | call assert_equal(99, done) |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 519 | |
| 520 | " test that a bar is recognized after the {group} |
| 521 | call s:AddAnAutocmd() |
| 522 | au! vimBarTest|echo 'hello' |
| 523 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
| 524 | endfunc |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 525 | |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 526 | func RemoveGroup() |
| 527 | autocmd! StartOK |
| 528 | augroup! StartOK |
| 529 | endfunc |
| 530 | |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 531 | func Test_augroup_warning() |
| 532 | augroup TheWarning |
| 533 | au VimEnter * echo 'entering' |
| 534 | augroup END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 535 | call assert_match("TheWarning.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 536 | redir => res |
| 537 | augroup! TheWarning |
| 538 | redir END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 539 | call assert_match("W19:", res) |
| 540 | call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 541 | |
| 542 | " check "Another" does not take the pace of the deleted entry |
| 543 | augroup Another |
| 544 | augroup END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 545 | call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 546 | augroup! Another |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 547 | |
| 548 | " no warning for postpone aucmd delete |
| 549 | augroup StartOK |
| 550 | au VimEnter * call RemoveGroup() |
| 551 | augroup END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 552 | call assert_match("StartOK.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 553 | redir => res |
| 554 | doautocmd VimEnter |
| 555 | redir END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 556 | call assert_notmatch("W19:", res) |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 557 | au! VimEnter |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 558 | |
| 559 | call assert_fails('augroup!', 'E471:') |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 560 | endfunc |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 561 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 562 | func Test_BufReadCmdHelp() |
| 563 | " This used to cause access to free memory |
| 564 | au BufReadCmd * e +h |
| 565 | help |
| 566 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 567 | au! BufReadCmd |
| 568 | endfunc |
| 569 | |
| 570 | func Test_BufReadCmdHelpJump() |
| 571 | " This used to cause access to free memory |
| 572 | au BufReadCmd * e +h{ |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 573 | " } to fix highlighting |
| 574 | call assert_fails('help', 'E434:') |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 575 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 576 | au! BufReadCmd |
| 577 | endfunc |
| 578 | |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 579 | func Test_augroup_deleted() |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 580 | " This caused a crash before E936 was introduced |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 581 | augroup x |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 582 | call assert_fails('augroup! x', 'E936:') |
| 583 | au VimEnter * echo |
| 584 | augroup end |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 585 | augroup! x |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 586 | call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 587 | au! VimEnter |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 588 | endfunc |
| 589 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 590 | " Tests for autocommands on :close command. |
| 591 | " This used to be in test13. |
| 592 | func Test_three_windows() |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 593 | " Clean up buffers, because in some cases this function fails. |
| 594 | call s:cleanup_buffers() |
| 595 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 596 | " Write three files and open them, each in a window. |
| 597 | " Then go to next window, with autocommand that deletes the previous one. |
| 598 | " Do this twice, writing the file. |
| 599 | e! Xtestje1 |
| 600 | call setline(1, 'testje1') |
| 601 | w |
| 602 | sp Xtestje2 |
| 603 | call setline(1, 'testje2') |
| 604 | w |
| 605 | sp Xtestje3 |
| 606 | call setline(1, 'testje3') |
| 607 | w |
| 608 | wincmd w |
| 609 | au WinLeave Xtestje2 bwipe |
| 610 | wincmd w |
| 611 | call assert_equal('Xtestje1', expand('%')) |
| 612 | |
| 613 | au WinLeave Xtestje1 bwipe Xtestje3 |
| 614 | close |
| 615 | call assert_equal('Xtestje1', expand('%')) |
| 616 | |
| 617 | " Test deleting the buffer on a Unload event. If this goes wrong there |
| 618 | " will be the ATTENTION prompt. |
| 619 | e Xtestje1 |
| 620 | au! |
| 621 | au! BufUnload Xtestje1 bwipe |
| 622 | call assert_fails('e Xtestje3', 'E937:') |
| 623 | call assert_equal('Xtestje3', expand('%')) |
| 624 | |
| 625 | e Xtestje2 |
| 626 | sp Xtestje1 |
| 627 | call assert_fails('e', 'E937:') |
Bram Moolenaar | a997b45 | 2018-04-17 23:24:06 +0200 | [diff] [blame] | 628 | call assert_equal('Xtestje1', expand('%')) |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 629 | |
| 630 | " Test changing buffers in a BufWipeout autocommand. If this goes wrong |
| 631 | " there are ml_line errors and/or a Crash. |
| 632 | au! |
| 633 | only |
| 634 | e Xanother |
| 635 | e Xtestje1 |
| 636 | bwipe Xtestje2 |
| 637 | bwipe Xtestje3 |
| 638 | au BufWipeout Xtestje1 buf Xtestje1 |
| 639 | bwipe |
| 640 | call assert_equal('Xanother', expand('%')) |
| 641 | |
| 642 | only |
| 643 | help |
| 644 | wincmd w |
| 645 | 1quit |
| 646 | call assert_equal('Xanother', expand('%')) |
| 647 | |
| 648 | au! |
Bram Moolenaar | 4520d44 | 2017-03-19 16:09:46 +0100 | [diff] [blame] | 649 | enew |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 650 | call delete('Xtestje1') |
| 651 | call delete('Xtestje2') |
| 652 | call delete('Xtestje3') |
| 653 | endfunc |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 654 | |
| 655 | func Test_BufEnter() |
| 656 | au! BufEnter |
| 657 | au Bufenter * let val = val . '+' |
| 658 | let g:val = '' |
| 659 | split NewFile |
| 660 | call assert_equal('+', g:val) |
| 661 | bwipe! |
| 662 | call assert_equal('++', g:val) |
| 663 | |
| 664 | " Also get BufEnter when editing a directory |
| 665 | call mkdir('Xdir') |
| 666 | split Xdir |
| 667 | call assert_equal('+++', g:val) |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 668 | |
| 669 | " On MS-Windows we can't edit the directory, make sure we wipe the right |
| 670 | " buffer. |
| 671 | bwipe! Xdir |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 672 | |
| 673 | call delete('Xdir', 'd') |
| 674 | au! BufEnter |
| 675 | endfunc |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 676 | |
| 677 | " Closing a window might cause an endless loop |
| 678 | " E814 for older Vims |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 679 | func Test_autocmd_bufwipe_in_SessLoadPost() |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 680 | edit Xtest |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 681 | tabnew |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 682 | file Xsomething |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 683 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 684 | mksession! |
| 685 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 686 | let content =<< trim [CODE] |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 687 | call test_override('ui_delay', 10) |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 688 | set nocp noswapfile |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 689 | let v:swapchoice = "e" |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 690 | augroup test_autocmd_sessionload |
| 691 | autocmd! |
| 692 | autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!" |
| 693 | augroup END |
| 694 | |
| 695 | func WriteErrors() |
| 696 | call writefile([execute("messages")], "Xerrors") |
| 697 | endfunc |
| 698 | au VimLeave * call WriteErrors() |
| 699 | [CODE] |
| 700 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 701 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 702 | 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] | 703 | let errors = join(readfile('Xerrors')) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 704 | call assert_match('E814:', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 705 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 706 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 707 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 708 | call delete(file) |
| 709 | endfor |
| 710 | endfunc |
| 711 | |
Bram Moolenaar | 797e63b | 2021-01-15 16:22:52 +0100 | [diff] [blame] | 712 | " Using :blast and :ball for many events caused a crash, because b_nwindows was |
| 713 | " not incremented correctly. |
| 714 | func Test_autocmd_blast_badd() |
| 715 | let content =<< trim [CODE] |
| 716 | au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast |
| 717 | edit foo1 |
| 718 | au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball |
| 719 | edit foo2 |
| 720 | call writefile(['OK'], 'Xerrors') |
| 721 | qall |
| 722 | [CODE] |
| 723 | |
| 724 | call writefile(content, 'XblastBall') |
| 725 | call system(GetVimCommand() .. ' --clean -S XblastBall') |
| 726 | call assert_match('OK', readfile('Xerrors')->join()) |
| 727 | |
| 728 | call delete('XblastBall') |
| 729 | call delete('Xerrors') |
| 730 | endfunc |
| 731 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 732 | " SEGV occurs in older versions. |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 733 | func Test_autocmd_bufwipe_in_SessLoadPost2() |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 734 | tabnew |
| 735 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 736 | mksession! |
| 737 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 738 | let content =<< trim [CODE] |
| 739 | set nocp noswapfile |
| 740 | function! DeleteInactiveBufs() |
| 741 | tabfirst |
| 742 | let tabblist = [] |
| 743 | for i in range(1, tabpagenr(''$'')) |
| 744 | call extend(tabblist, tabpagebuflist(i)) |
| 745 | endfor |
| 746 | for b in range(1, bufnr(''$'')) |
| 747 | if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'') |
| 748 | exec ''bwipeout '' . b |
| 749 | endif |
| 750 | endfor |
| 751 | echomsg "SessionLoadPost DONE" |
| 752 | endfunction |
| 753 | au SessionLoadPost * call DeleteInactiveBufs() |
| 754 | |
| 755 | func WriteErrors() |
| 756 | call writefile([execute("messages")], "Xerrors") |
| 757 | endfunc |
| 758 | au VimLeave * call WriteErrors() |
| 759 | [CODE] |
| 760 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 761 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 762 | 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] | 763 | let errors = join(readfile('Xerrors')) |
| 764 | " This probably only ever matches on unix. |
| 765 | call assert_notmatch('Caught deadly signal SEGV', errors) |
| 766 | call assert_match('SessionLoadPost DONE', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 767 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 768 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 769 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 770 | call delete(file) |
| 771 | endfor |
| 772 | endfunc |
Bram Moolenaar | faf29d7 | 2017-07-09 11:07:16 +0200 | [diff] [blame] | 773 | |
| 774 | func Test_empty_doau() |
| 775 | doau \| |
| 776 | endfunc |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 777 | |
| 778 | func s:AutoCommandOptionSet(match) |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 779 | 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] | 780 | let item = remove(g:options, 0) |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 781 | let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6]) |
| 782 | 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] | 783 | let g:opt = [expected, actual] |
| 784 | "call assert_equal(expected, actual) |
| 785 | endfunc |
| 786 | |
| 787 | func Test_OptionSet() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 788 | CheckOption autochdir |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 789 | |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 790 | badd test_autocmd.vim |
| 791 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 792 | call test_override('starting', 1) |
| 793 | set nocp |
| 794 | au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>")) |
| 795 | |
| 796 | " 1: Setting number option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 797 | let g:options = [['number', 0, 0, 0, 1, 'global', 'set']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 798 | set nu |
| 799 | call assert_equal([], g:options) |
| 800 | call assert_equal(g:opt[0], g:opt[1]) |
| 801 | |
| 802 | " 2: Setting local number option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 803 | let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 804 | setlocal nonu |
| 805 | call assert_equal([], g:options) |
| 806 | call assert_equal(g:opt[0], g:opt[1]) |
| 807 | |
| 808 | " 3: Setting global number option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 809 | let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 810 | setglobal nonu |
| 811 | call assert_equal([], g:options) |
| 812 | call assert_equal(g:opt[0], g:opt[1]) |
| 813 | |
| 814 | " 4: Setting local autoindent option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 815 | let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 816 | setlocal ai |
| 817 | call assert_equal([], g:options) |
| 818 | call assert_equal(g:opt[0], g:opt[1]) |
| 819 | |
| 820 | " 5: Setting global autoindent option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 821 | let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 822 | setglobal ai |
| 823 | call assert_equal([], g:options) |
| 824 | call assert_equal(g:opt[0], g:opt[1]) |
| 825 | |
| 826 | " 6: Setting global autoindent option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 827 | let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 828 | set ai! |
| 829 | call assert_equal([], g:options) |
| 830 | call assert_equal(g:opt[0], g:opt[1]) |
| 831 | |
| 832 | " 6a: Setting global autoindent option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 833 | let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 834 | noa setlocal ai |
| 835 | noa setglobal noai |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 836 | set ai! |
| 837 | call assert_equal([], g:options) |
| 838 | call assert_equal(g:opt[0], g:opt[1]) |
| 839 | |
| 840 | " Should not print anything, use :noa |
| 841 | " 7: don't trigger OptionSet" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 842 | let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 843 | noa set nonu |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 844 | call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 845 | call assert_equal(g:opt[0], g:opt[1]) |
| 846 | |
| 847 | " 8: Setting several global list and number option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 848 | 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] | 849 | set list nu |
| 850 | call assert_equal([], g:options) |
| 851 | call assert_equal(g:opt[0], g:opt[1]) |
| 852 | |
| 853 | " 9: don't trigger OptionSet" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 854 | 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] | 855 | noa set nolist nonu |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 856 | 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] | 857 | call assert_equal(g:opt[0], g:opt[1]) |
| 858 | |
| 859 | " 10: Setting global acd" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 860 | let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 861 | setlocal acd |
| 862 | call assert_equal([], g:options) |
| 863 | call assert_equal(g:opt[0], g:opt[1]) |
| 864 | |
| 865 | " 11: Setting global autoread (also sets local value)" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 866 | let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 867 | set ar |
| 868 | call assert_equal([], g:options) |
| 869 | call assert_equal(g:opt[0], g:opt[1]) |
| 870 | |
| 871 | " 12: Setting local autoread" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 872 | let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 873 | setlocal ar |
| 874 | call assert_equal([], g:options) |
| 875 | call assert_equal(g:opt[0], g:opt[1]) |
| 876 | |
| 877 | " 13: Setting global autoread" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 878 | let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 879 | setglobal invar |
| 880 | call assert_equal([], g:options) |
| 881 | call assert_equal(g:opt[0], g:opt[1]) |
| 882 | |
| 883 | " 14: Setting option backspace through :let" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 884 | let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']] |
| 885 | let &bs = "eol,indent,start" |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 886 | call assert_equal([], g:options) |
| 887 | call assert_equal(g:opt[0], g:opt[1]) |
| 888 | |
| 889 | " 15: Setting option backspace through setbufvar()" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 890 | let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 891 | " try twice, first time, shouldn't trigger because option name is invalid, |
| 892 | " second time, it should trigger |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 893 | let bnum = bufnr('%') |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 894 | call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 895 | " should trigger, use correct option name |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 896 | call setbufvar(bnum, '&backup', 1) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 897 | call assert_equal([], g:options) |
| 898 | call assert_equal(g:opt[0], g:opt[1]) |
| 899 | |
| 900 | " 16: Setting number option using setwinvar" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 901 | let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 902 | call setwinvar(0, '&number', 1) |
| 903 | call assert_equal([], g:options) |
| 904 | call assert_equal(g:opt[0], g:opt[1]) |
| 905 | |
| 906 | " 17: Setting key option, shouldn't trigger" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 907 | let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 908 | setlocal key=blah |
| 909 | setlocal key= |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 910 | call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 911 | call assert_equal(g:opt[0], g:opt[1]) |
| 912 | |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 913 | |
| 914 | " 18a: Setting string global option" |
| 915 | let oldval = &backupext |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 916 | let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 917 | set backupext=foo |
| 918 | call assert_equal([], g:options) |
| 919 | call assert_equal(g:opt[0], g:opt[1]) |
| 920 | |
| 921 | " 18b: Resetting string global option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 922 | let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 923 | set backupext& |
| 924 | call assert_equal([], g:options) |
| 925 | call assert_equal(g:opt[0], g:opt[1]) |
| 926 | |
| 927 | " 18c: Setting global string global option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 928 | let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 929 | setglobal backupext=bar |
| 930 | call assert_equal([], g:options) |
| 931 | call assert_equal(g:opt[0], g:opt[1]) |
| 932 | |
| 933 | " 18d: Setting local string global option" |
| 934 | " As this is a global option this sets the global value even though |
| 935 | " :setlocal is used! |
| 936 | noa set backupext& " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 937 | let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 938 | setlocal backupext=baz |
| 939 | call assert_equal([], g:options) |
| 940 | call assert_equal(g:opt[0], g:opt[1]) |
| 941 | |
| 942 | " 18e: Setting again string global option" |
| 943 | noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd) |
| 944 | noa setlocal backupext=ext_local " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 945 | 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] | 946 | set backupext=fuu |
| 947 | call assert_equal([], g:options) |
| 948 | call assert_equal(g:opt[0], g:opt[1]) |
| 949 | |
| 950 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 951 | " 19a: Setting string global-local (to buffer) option" |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 952 | let oldval = &tags |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 953 | let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']] |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 954 | set tags=tagpath |
| 955 | call assert_equal([], g:options) |
| 956 | call assert_equal(g:opt[0], g:opt[1]) |
| 957 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 958 | " 19b: Resetting string global-local (to buffer) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 959 | let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']] |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 960 | set tags& |
| 961 | call assert_equal([], g:options) |
| 962 | call assert_equal(g:opt[0], g:opt[1]) |
| 963 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 964 | " 19c: Setting global string global-local (to buffer) option " |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 965 | let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 966 | setglobal tags=tagpath1 |
| 967 | call assert_equal([], g:options) |
| 968 | call assert_equal(g:opt[0], g:opt[1]) |
| 969 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 970 | " 19d: Setting local string global-local (to buffer) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 971 | let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 972 | setlocal tags=tagpath2 |
| 973 | call assert_equal([], g:options) |
| 974 | call assert_equal(g:opt[0], g:opt[1]) |
| 975 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 976 | " 19e: Setting again string global-local (to buffer) option" |
| 977 | " Note: v:option_old is the old global value for global-local string options |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 978 | " but the old local value for all other kinds of options. |
| 979 | noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd) |
| 980 | noa setlocal tags=tag_local |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 981 | 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] | 982 | set tags=tagpath |
| 983 | call assert_equal([], g:options) |
| 984 | call assert_equal(g:opt[0], g:opt[1]) |
| 985 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 986 | " 19f: Setting string global-local (to buffer) option to an empty string" |
| 987 | " Note: v:option_old is the old global value for global-local string options |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 988 | " but the old local value for all other kinds of options. |
| 989 | noa set tags=tag_global " Reset global and local value (without triggering autocmd) |
| 990 | noa setlocal tags= " empty string |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 991 | let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 992 | set tags=tagpath |
| 993 | call assert_equal([], g:options) |
| 994 | call assert_equal(g:opt[0], g:opt[1]) |
| 995 | |
| 996 | |
| 997 | " 20a: Setting string local (to buffer) option" |
| 998 | let oldval = &spelllang |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 999 | let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1000 | set spelllang=elvish,klingon |
| 1001 | call assert_equal([], g:options) |
| 1002 | call assert_equal(g:opt[0], g:opt[1]) |
| 1003 | |
| 1004 | " 20b: Resetting string local (to buffer) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1005 | 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] | 1006 | set spelllang& |
| 1007 | call assert_equal([], g:options) |
| 1008 | call assert_equal(g:opt[0], g:opt[1]) |
| 1009 | |
| 1010 | " 20c: Setting global string local (to buffer) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1011 | let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1012 | setglobal spelllang=elvish |
| 1013 | call assert_equal([], g:options) |
| 1014 | call assert_equal(g:opt[0], g:opt[1]) |
| 1015 | |
| 1016 | " 20d: Setting local string local (to buffer) option" |
| 1017 | noa set spelllang& " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1018 | let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1019 | setlocal spelllang=klingon |
| 1020 | call assert_equal([], g:options) |
| 1021 | call assert_equal(g:opt[0], g:opt[1]) |
| 1022 | |
| 1023 | " 20e: Setting again string local (to buffer) option" |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1024 | " Note: v:option_old is the old global value for global-local string options |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1025 | " but the old local value for all other kinds of options. |
| 1026 | noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd) |
| 1027 | noa setlocal spelllang=spelllocal |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1028 | let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1029 | set spelllang=foo |
| 1030 | call assert_equal([], g:options) |
| 1031 | call assert_equal(g:opt[0], g:opt[1]) |
| 1032 | |
| 1033 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1034 | " 21a: Setting string global-local (to window) option" |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1035 | let oldval = &statusline |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1036 | let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1037 | set statusline=foo |
| 1038 | call assert_equal([], g:options) |
| 1039 | call assert_equal(g:opt[0], g:opt[1]) |
| 1040 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1041 | " 21b: Resetting string global-local (to window) option" |
| 1042 | " Note: v:option_old is the old global value for global-local string options |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1043 | " but the old local value for all other kinds of options. |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1044 | let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1045 | set statusline& |
| 1046 | call assert_equal([], g:options) |
| 1047 | call assert_equal(g:opt[0], g:opt[1]) |
| 1048 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1049 | " 21c: Setting global string global-local (to window) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1050 | let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1051 | setglobal statusline=bar |
| 1052 | call assert_equal([], g:options) |
| 1053 | call assert_equal(g:opt[0], g:opt[1]) |
| 1054 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1055 | " 21d: Setting local string global-local (to window) option" |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1056 | noa set statusline& " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1057 | let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1058 | setlocal statusline=baz |
| 1059 | call assert_equal([], g:options) |
| 1060 | call assert_equal(g:opt[0], g:opt[1]) |
| 1061 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1062 | " 21e: Setting again string global-local (to window) option" |
| 1063 | " Note: v:option_old is the old global value for global-local string options |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1064 | " but the old local value for all other kinds of options. |
| 1065 | noa setglobal statusline=bar " Reset global and local value (without triggering autocmd) |
| 1066 | noa setlocal statusline=baz |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1067 | let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1068 | set statusline=foo |
| 1069 | call assert_equal([], g:options) |
| 1070 | call assert_equal(g:opt[0], g:opt[1]) |
| 1071 | |
| 1072 | |
| 1073 | " 22a: Setting string local (to window) option" |
| 1074 | let oldval = &foldignore |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1075 | let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1076 | set foldignore=fo |
| 1077 | call assert_equal([], g:options) |
| 1078 | call assert_equal(g:opt[0], g:opt[1]) |
| 1079 | |
| 1080 | " 22b: Resetting string local (to window) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1081 | let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1082 | set foldignore& |
| 1083 | call assert_equal([], g:options) |
| 1084 | call assert_equal(g:opt[0], g:opt[1]) |
| 1085 | |
| 1086 | " 22c: Setting global string local (to window) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1087 | let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1088 | setglobal foldignore=bar |
| 1089 | call assert_equal([], g:options) |
| 1090 | call assert_equal(g:opt[0], g:opt[1]) |
| 1091 | |
| 1092 | " 22d: Setting local string local (to window) option" |
| 1093 | noa set foldignore& " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1094 | let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1095 | setlocal foldignore=baz |
| 1096 | call assert_equal([], g:options) |
| 1097 | call assert_equal(g:opt[0], g:opt[1]) |
| 1098 | |
| 1099 | " 22e: Setting again string local (to window) option" |
| 1100 | noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd) |
| 1101 | noa setlocal foldignore=loc |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1102 | let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1103 | set foldignore=fo |
| 1104 | call assert_equal([], g:options) |
| 1105 | call assert_equal(g:opt[0], g:opt[1]) |
| 1106 | |
| 1107 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1108 | " 23a: Setting global number global option" |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1109 | noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) |
| 1110 | noa setlocal cmdheight=1 " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1111 | let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1112 | setglobal cmdheight=2 |
| 1113 | call assert_equal([], g:options) |
| 1114 | call assert_equal(g:opt[0], g:opt[1]) |
| 1115 | |
| 1116 | " 23b: Setting local number global option" |
| 1117 | noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) |
| 1118 | noa setlocal cmdheight=1 " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1119 | let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1120 | setlocal cmdheight=2 |
| 1121 | call assert_equal([], g:options) |
| 1122 | call assert_equal(g:opt[0], g:opt[1]) |
| 1123 | |
| 1124 | " 23c: Setting again number global option" |
| 1125 | noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) |
| 1126 | noa setlocal cmdheight=1 " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1127 | let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1128 | set cmdheight=2 |
| 1129 | call assert_equal([], g:options) |
| 1130 | call assert_equal(g:opt[0], g:opt[1]) |
| 1131 | |
| 1132 | " 23d: Setting again number global option" |
| 1133 | noa set cmdheight=8 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1134 | let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1135 | set cmdheight=2 |
| 1136 | call assert_equal([], g:options) |
| 1137 | call assert_equal(g:opt[0], g:opt[1]) |
| 1138 | |
| 1139 | |
| 1140 | " 24a: Setting global number global-local (to buffer) option" |
| 1141 | noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) |
| 1142 | noa setlocal undolevels=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1143 | let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1144 | setglobal undolevels=2 |
| 1145 | call assert_equal([], g:options) |
| 1146 | call assert_equal(g:opt[0], g:opt[1]) |
| 1147 | |
| 1148 | " 24b: Setting local number global-local (to buffer) option" |
| 1149 | noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) |
| 1150 | noa setlocal undolevels=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1151 | let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1152 | setlocal undolevels=2 |
| 1153 | call assert_equal([], g:options) |
| 1154 | call assert_equal(g:opt[0], g:opt[1]) |
| 1155 | |
| 1156 | " 24c: Setting again number global-local (to buffer) option" |
| 1157 | noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) |
| 1158 | noa setlocal undolevels=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1159 | let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1160 | set undolevels=2 |
| 1161 | call assert_equal([], g:options) |
| 1162 | call assert_equal(g:opt[0], g:opt[1]) |
| 1163 | |
| 1164 | " 24d: Setting again global number global-local (to buffer) option" |
| 1165 | noa set undolevels=8 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1166 | let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1167 | set undolevels=2 |
| 1168 | call assert_equal([], g:options) |
| 1169 | call assert_equal(g:opt[0], g:opt[1]) |
| 1170 | |
| 1171 | |
| 1172 | " 25a: Setting global number local (to buffer) option" |
| 1173 | noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) |
| 1174 | noa setlocal wrapmargin=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1175 | let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1176 | setglobal wrapmargin=2 |
| 1177 | call assert_equal([], g:options) |
| 1178 | call assert_equal(g:opt[0], g:opt[1]) |
| 1179 | |
| 1180 | " 25b: Setting local number local (to buffer) option" |
| 1181 | noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) |
| 1182 | noa setlocal wrapmargin=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1183 | let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1184 | setlocal wrapmargin=2 |
| 1185 | call assert_equal([], g:options) |
| 1186 | call assert_equal(g:opt[0], g:opt[1]) |
| 1187 | |
| 1188 | " 25c: Setting again number local (to buffer) option" |
| 1189 | noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) |
| 1190 | noa setlocal wrapmargin=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1191 | let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1192 | set wrapmargin=2 |
| 1193 | call assert_equal([], g:options) |
| 1194 | call assert_equal(g:opt[0], g:opt[1]) |
| 1195 | |
| 1196 | " 25d: Setting again global number local (to buffer) option" |
| 1197 | noa set wrapmargin=8 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1198 | let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1199 | set wrapmargin=2 |
| 1200 | call assert_equal([], g:options) |
| 1201 | call assert_equal(g:opt[0], g:opt[1]) |
| 1202 | |
| 1203 | |
| 1204 | " 26: Setting number global-local (to window) option. |
| 1205 | " Such option does currently not exist. |
| 1206 | |
| 1207 | |
| 1208 | " 27a: Setting global number local (to window) option" |
| 1209 | noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) |
| 1210 | noa setlocal foldcolumn=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1211 | let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1212 | setglobal foldcolumn=2 |
| 1213 | call assert_equal([], g:options) |
| 1214 | call assert_equal(g:opt[0], g:opt[1]) |
| 1215 | |
| 1216 | " 27b: Setting local number local (to window) option" |
| 1217 | noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) |
| 1218 | noa setlocal foldcolumn=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1219 | let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1220 | setlocal foldcolumn=2 |
| 1221 | call assert_equal([], g:options) |
| 1222 | call assert_equal(g:opt[0], g:opt[1]) |
| 1223 | |
| 1224 | " 27c: Setting again number local (to window) option" |
| 1225 | noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) |
| 1226 | noa setlocal foldcolumn=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1227 | let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1228 | set foldcolumn=2 |
| 1229 | call assert_equal([], g:options) |
| 1230 | call assert_equal(g:opt[0], g:opt[1]) |
| 1231 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1232 | " 27d: Setting again global number local (to window) option" |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1233 | noa set foldcolumn=8 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1234 | let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1235 | set foldcolumn=2 |
| 1236 | call assert_equal([], g:options) |
| 1237 | call assert_equal(g:opt[0], g:opt[1]) |
| 1238 | |
| 1239 | |
| 1240 | " 28a: Setting global boolean global option" |
| 1241 | noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) |
| 1242 | noa setlocal wrapscan " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1243 | let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1244 | setglobal nowrapscan |
| 1245 | call assert_equal([], g:options) |
| 1246 | call assert_equal(g:opt[0], g:opt[1]) |
| 1247 | |
| 1248 | " 28b: Setting local boolean global option" |
| 1249 | noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) |
| 1250 | noa setlocal wrapscan " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1251 | let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1252 | setlocal nowrapscan |
| 1253 | call assert_equal([], g:options) |
| 1254 | call assert_equal(g:opt[0], g:opt[1]) |
| 1255 | |
| 1256 | " 28c: Setting again boolean global option" |
| 1257 | noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) |
| 1258 | noa setlocal wrapscan " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1259 | let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1260 | set nowrapscan |
| 1261 | call assert_equal([], g:options) |
| 1262 | call assert_equal(g:opt[0], g:opt[1]) |
| 1263 | |
| 1264 | " 28d: Setting again global boolean global option" |
| 1265 | noa set nowrapscan " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1266 | let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1267 | set wrapscan |
| 1268 | call assert_equal([], g:options) |
| 1269 | call assert_equal(g:opt[0], g:opt[1]) |
| 1270 | |
| 1271 | |
| 1272 | " 29a: Setting global boolean global-local (to buffer) option" |
| 1273 | noa setglobal noautoread " Reset global and local value (without triggering autocmd) |
| 1274 | noa setlocal autoread |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1275 | let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1276 | setglobal autoread |
| 1277 | call assert_equal([], g:options) |
| 1278 | call assert_equal(g:opt[0], g:opt[1]) |
| 1279 | |
| 1280 | " 29b: Setting local boolean global-local (to buffer) option" |
| 1281 | noa setglobal noautoread " Reset global and local value (without triggering autocmd) |
| 1282 | noa setlocal autoread |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1283 | let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1284 | setlocal noautoread |
| 1285 | call assert_equal([], g:options) |
| 1286 | call assert_equal(g:opt[0], g:opt[1]) |
| 1287 | |
| 1288 | " 29c: Setting again boolean global-local (to buffer) option" |
| 1289 | noa setglobal noautoread " Reset global and local value (without triggering autocmd) |
| 1290 | noa setlocal autoread |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1291 | let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1292 | set autoread |
| 1293 | call assert_equal([], g:options) |
| 1294 | call assert_equal(g:opt[0], g:opt[1]) |
| 1295 | |
| 1296 | " 29d: Setting again global boolean global-local (to buffer) option" |
| 1297 | noa set noautoread " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1298 | let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1299 | set autoread |
| 1300 | call assert_equal([], g:options) |
| 1301 | call assert_equal(g:opt[0], g:opt[1]) |
| 1302 | |
| 1303 | |
| 1304 | " 30a: Setting global boolean local (to buffer) option" |
| 1305 | noa setglobal nocindent " Reset global and local value (without triggering autocmd) |
| 1306 | noa setlocal cindent |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1307 | let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1308 | setglobal cindent |
| 1309 | call assert_equal([], g:options) |
| 1310 | call assert_equal(g:opt[0], g:opt[1]) |
| 1311 | |
| 1312 | " 30b: Setting local boolean local (to buffer) option" |
| 1313 | noa setglobal nocindent " Reset global and local value (without triggering autocmd) |
| 1314 | noa setlocal cindent |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1315 | let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1316 | setlocal nocindent |
| 1317 | call assert_equal([], g:options) |
| 1318 | call assert_equal(g:opt[0], g:opt[1]) |
| 1319 | |
| 1320 | " 30c: Setting again boolean local (to buffer) option" |
| 1321 | noa setglobal nocindent " Reset global and local value (without triggering autocmd) |
| 1322 | noa setlocal cindent |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1323 | let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1324 | set cindent |
| 1325 | call assert_equal([], g:options) |
| 1326 | call assert_equal(g:opt[0], g:opt[1]) |
| 1327 | |
| 1328 | " 30d: Setting again global boolean local (to buffer) option" |
| 1329 | noa set nocindent " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1330 | let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1331 | set cindent |
| 1332 | call assert_equal([], g:options) |
| 1333 | call assert_equal(g:opt[0], g:opt[1]) |
| 1334 | |
| 1335 | |
| 1336 | " 31: Setting boolean global-local (to window) option |
| 1337 | " Currently no such option exists. |
| 1338 | |
| 1339 | |
| 1340 | " 32a: Setting global boolean local (to window) option" |
| 1341 | noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) |
| 1342 | noa setlocal cursorcolumn |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1343 | let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1344 | setglobal cursorcolumn |
| 1345 | call assert_equal([], g:options) |
| 1346 | call assert_equal(g:opt[0], g:opt[1]) |
| 1347 | |
| 1348 | " 32b: Setting local boolean local (to window) option" |
| 1349 | noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) |
| 1350 | noa setlocal cursorcolumn |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1351 | let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1352 | setlocal nocursorcolumn |
| 1353 | call assert_equal([], g:options) |
| 1354 | call assert_equal(g:opt[0], g:opt[1]) |
| 1355 | |
| 1356 | " 32c: Setting again boolean local (to window) option" |
| 1357 | noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) |
| 1358 | noa setlocal cursorcolumn |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1359 | let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1360 | set cursorcolumn |
| 1361 | call assert_equal([], g:options) |
| 1362 | call assert_equal(g:opt[0], g:opt[1]) |
| 1363 | |
| 1364 | " 32d: Setting again global boolean local (to window) option" |
| 1365 | noa set nocursorcolumn " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1366 | let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1367 | set cursorcolumn |
| 1368 | call assert_equal([], g:options) |
| 1369 | call assert_equal(g:opt[0], g:opt[1]) |
| 1370 | |
| 1371 | |
Bram Moolenaar | 1bc353b | 2019-09-01 14:45:28 +0200 | [diff] [blame] | 1372 | " 33: Test autocommands when an option value is converted internally. |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1373 | noa set backspace=1 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1374 | 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] | 1375 | set backspace=2 |
| 1376 | call assert_equal([], g:options) |
| 1377 | call assert_equal(g:opt[0], g:opt[1]) |
| 1378 | |
| 1379 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1380 | " Cleanup |
| 1381 | au! OptionSet |
Bram Moolenaar | 0331faf | 2019-06-15 18:40:37 +0200 | [diff] [blame] | 1382 | " set tags& |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1383 | 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] | 1384 | exe printf(":set %s&vim", opt) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1385 | endfor |
| 1386 | call test_override('starting', 0) |
| 1387 | delfunc! AutoCommandOptionSet |
| 1388 | endfunc |
| 1389 | |
| 1390 | func Test_OptionSet_diffmode() |
| 1391 | call test_override('starting', 1) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 1392 | " 18: Changing an option when entering diff mode |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1393 | new |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1394 | au OptionSet diff :let &l:cul = v:option_new |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1395 | |
| 1396 | call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) |
| 1397 | call assert_equal(0, &l:cul) |
| 1398 | diffthis |
| 1399 | call assert_equal(1, &l:cul) |
| 1400 | |
| 1401 | vnew |
| 1402 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
| 1403 | call assert_equal(0, &l:cul) |
| 1404 | diffthis |
| 1405 | call assert_equal(1, &l:cul) |
| 1406 | |
| 1407 | diffoff |
| 1408 | call assert_equal(0, &l:cul) |
| 1409 | call assert_equal(1, getwinvar(2, '&l:cul')) |
| 1410 | bw! |
| 1411 | |
| 1412 | call assert_equal(1, &l:cul) |
| 1413 | diffoff! |
| 1414 | call assert_equal(0, &l:cul) |
| 1415 | call assert_equal(0, getwinvar(1, '&l:cul')) |
| 1416 | bw! |
| 1417 | |
| 1418 | " Cleanup |
| 1419 | au! OptionSet |
| 1420 | call test_override('starting', 0) |
| 1421 | endfunc |
| 1422 | |
| 1423 | func Test_OptionSet_diffmode_close() |
| 1424 | call test_override('starting', 1) |
| 1425 | " 19: Try to close the current window when entering diff mode |
| 1426 | " should not segfault |
| 1427 | new |
| 1428 | au OptionSet diff close |
| 1429 | |
| 1430 | call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1431 | call assert_fails(':diffthis', 'E788:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1432 | call assert_equal(1, &diff) |
| 1433 | vnew |
| 1434 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1435 | call assert_fails(':diffthis', 'E788:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1436 | call assert_equal(1, &diff) |
Bram Moolenaar | a9aa86f | 2019-11-10 21:25:45 +0100 | [diff] [blame] | 1437 | set diffopt-=closeoff |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1438 | bw! |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1439 | call assert_fails(':diffoff!', 'E788:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1440 | bw! |
| 1441 | |
| 1442 | " Cleanup |
| 1443 | au! OptionSet |
| 1444 | call test_override('starting', 0) |
| 1445 | "delfunc! AutoCommandOptionSet |
| 1446 | endfunc |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 1447 | |
| 1448 | " Test for Bufleave autocommand that deletes the buffer we are about to edit. |
| 1449 | func Test_BufleaveWithDelete() |
| 1450 | new | edit Xfile1 |
| 1451 | |
| 1452 | augroup test_bufleavewithdelete |
| 1453 | autocmd! |
| 1454 | autocmd BufLeave Xfile1 bwipe Xfile2 |
| 1455 | augroup END |
| 1456 | |
| 1457 | call assert_fails('edit Xfile2', 'E143:') |
| 1458 | call assert_equal('Xfile1', bufname('%')) |
| 1459 | |
| 1460 | autocmd! test_bufleavewithdelete BufLeave Xfile1 |
| 1461 | augroup! test_bufleavewithdelete |
| 1462 | |
| 1463 | new |
| 1464 | bwipe! Xfile1 |
| 1465 | endfunc |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1466 | |
| 1467 | " Test for autocommand that changes the buffer list, when doing ":ball". |
| 1468 | func Test_Acmd_BufAll() |
| 1469 | enew! |
| 1470 | %bwipe! |
| 1471 | call writefile(['Test file Xxx1'], 'Xxx1') |
| 1472 | call writefile(['Test file Xxx2'], 'Xxx2') |
| 1473 | call writefile(['Test file Xxx3'], 'Xxx3') |
| 1474 | |
| 1475 | " Add three files to the buffer list |
| 1476 | split Xxx1 |
| 1477 | close |
| 1478 | split Xxx2 |
| 1479 | close |
| 1480 | split Xxx3 |
| 1481 | close |
| 1482 | |
| 1483 | " Wipe the buffer when the buffer is opened |
| 1484 | au BufReadPost Xxx2 bwipe |
| 1485 | |
| 1486 | call append(0, 'Test file Xxx4') |
| 1487 | ball |
| 1488 | |
| 1489 | call assert_equal(2, winnr('$')) |
| 1490 | call assert_equal('Xxx1', bufname(winbufnr(winnr('$')))) |
| 1491 | wincmd t |
| 1492 | |
| 1493 | au! BufReadPost |
| 1494 | %bwipe! |
| 1495 | call delete('Xxx1') |
| 1496 | call delete('Xxx2') |
| 1497 | call delete('Xxx3') |
| 1498 | enew! | only |
| 1499 | endfunc |
| 1500 | |
| 1501 | " Test for autocommand that changes current buffer on BufEnter event. |
| 1502 | " Check if modelines are interpreted for the correct buffer. |
| 1503 | func Test_Acmd_BufEnter() |
| 1504 | %bwipe! |
| 1505 | call writefile(['start of test file Xxx1', |
| 1506 | \ "\<Tab>this is a test", |
| 1507 | \ 'end of test file Xxx1'], 'Xxx1') |
| 1508 | call writefile(['start of test file Xxx2', |
| 1509 | \ 'vim: set noai :', |
| 1510 | \ "\<Tab>this is a test", |
| 1511 | \ 'end of test file Xxx2'], 'Xxx2') |
| 1512 | |
| 1513 | au BufEnter Xxx2 brew |
| 1514 | set ai modeline modelines=3 |
| 1515 | edit Xxx1 |
| 1516 | " edit Xxx2, autocmd will do :brew |
| 1517 | edit Xxx2 |
| 1518 | exe "normal G?this is a\<CR>" |
| 1519 | " Append text with autoindent to this file |
| 1520 | normal othis should be auto-indented |
| 1521 | call assert_equal("\<Tab>this should be auto-indented", getline('.')) |
| 1522 | call assert_equal(3, line('.')) |
| 1523 | " Remove autocmd and edit Xxx2 again |
| 1524 | au! BufEnter Xxx2 |
| 1525 | buf! Xxx2 |
| 1526 | exe "normal G?this is a\<CR>" |
| 1527 | " append text without autoindent to Xxx |
| 1528 | normal othis should be in column 1 |
| 1529 | call assert_equal("this should be in column 1", getline('.')) |
| 1530 | call assert_equal(4, line('.')) |
| 1531 | |
| 1532 | %bwipe! |
| 1533 | call delete('Xxx1') |
| 1534 | call delete('Xxx2') |
| 1535 | set ai&vim modeline&vim modelines&vim |
| 1536 | endfunc |
| 1537 | |
| 1538 | " Test for issue #57 |
| 1539 | " do not move cursor on <c-o> when autoindent is set |
| 1540 | func Test_ai_CTRL_O() |
| 1541 | enew! |
| 1542 | set ai |
| 1543 | let save_fo = &fo |
| 1544 | set fo+=r |
| 1545 | exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>" |
| 1546 | exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>" |
| 1547 | call assert_equal(['# abc', 'def', 'def'], getline(2, 4)) |
| 1548 | |
| 1549 | set ai&vim |
| 1550 | let &fo = save_fo |
| 1551 | enew! |
| 1552 | endfunc |
| 1553 | |
| 1554 | " Test for autocommand that deletes the current buffer on BufLeave event. |
| 1555 | " Also test deleting the last buffer, should give a new, empty buffer. |
| 1556 | func Test_BufLeave_Wipe() |
| 1557 | %bwipe! |
| 1558 | let content = ['start of test file Xxx', |
| 1559 | \ 'this is a test', |
| 1560 | \ 'end of test file Xxx'] |
| 1561 | call writefile(content, 'Xxx1') |
| 1562 | call writefile(content, 'Xxx2') |
| 1563 | |
| 1564 | au BufLeave Xxx2 bwipe |
| 1565 | edit Xxx1 |
| 1566 | split Xxx2 |
| 1567 | " delete buffer Xxx2, we should be back to Xxx1 |
| 1568 | bwipe |
| 1569 | call assert_equal('Xxx1', bufname('%')) |
| 1570 | call assert_equal(1, winnr('$')) |
| 1571 | |
| 1572 | " Create an alternate buffer |
| 1573 | %write! test.out |
| 1574 | call assert_equal('test.out', bufname('#')) |
| 1575 | " delete alternate buffer |
| 1576 | bwipe test.out |
| 1577 | call assert_equal('Xxx1', bufname('%')) |
| 1578 | call assert_equal('', bufname('#')) |
| 1579 | |
| 1580 | au BufLeave Xxx1 bwipe |
| 1581 | " delete current buffer, get an empty one |
| 1582 | bwipe! |
| 1583 | call assert_equal(1, line('$')) |
| 1584 | call assert_equal('', bufname('%')) |
Bram Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 1585 | let g:bufinfo = getbufinfo() |
| 1586 | call assert_equal(1, len(g:bufinfo)) |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1587 | |
| 1588 | call delete('Xxx1') |
| 1589 | call delete('Xxx2') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1590 | call delete('test.out') |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1591 | %bwipe |
| 1592 | au! BufLeave |
Bram Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 1593 | |
| 1594 | " check that bufinfo doesn't contain a pointer to freed memory |
| 1595 | call test_garbagecollect_now() |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1596 | endfunc |
Bram Moolenaar | 87ffb5c | 2017-10-19 12:37:42 +0200 | [diff] [blame] | 1597 | |
| 1598 | func Test_QuitPre() |
| 1599 | edit Xfoo |
| 1600 | let winid = win_getid(winnr()) |
| 1601 | split Xbar |
| 1602 | au! QuitPre * let g:afile = expand('<afile>') |
| 1603 | " Close the other window, <afile> should be correct. |
| 1604 | exe win_id2win(winid) . 'q' |
| 1605 | call assert_equal('Xfoo', g:afile) |
LemonBoy | 66e13ae | 2022-04-21 22:52:11 +0100 | [diff] [blame] | 1606 | |
Bram Moolenaar | 87ffb5c | 2017-10-19 12:37:42 +0200 | [diff] [blame] | 1607 | unlet g:afile |
| 1608 | bwipe Xfoo |
| 1609 | bwipe Xbar |
| 1610 | endfunc |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1611 | |
| 1612 | func Test_Cmdline() |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 1613 | au! CmdlineChanged : let g:text = getcmdline() |
| 1614 | let g:text = 0 |
| 1615 | call feedkeys(":echom 'hello'\<CR>", 'xt') |
| 1616 | call assert_equal("echom 'hello'", g:text) |
| 1617 | au! CmdlineChanged |
| 1618 | |
| 1619 | au! CmdlineChanged : let g:entered = expand('<afile>') |
| 1620 | let g:entered = 0 |
| 1621 | call feedkeys(":echom 'hello'\<CR>", 'xt') |
| 1622 | call assert_equal(':', g:entered) |
| 1623 | au! CmdlineChanged |
| 1624 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1625 | au! CmdlineEnter : let g:entered = expand('<afile>') |
| 1626 | au! CmdlineLeave : let g:left = expand('<afile>') |
| 1627 | let g:entered = 0 |
| 1628 | let g:left = 0 |
| 1629 | call feedkeys(":echo 'hello'\<CR>", 'xt') |
| 1630 | call assert_equal(':', g:entered) |
| 1631 | call assert_equal(':', g:left) |
| 1632 | au! CmdlineEnter |
| 1633 | au! CmdlineLeave |
| 1634 | |
Bram Moolenaar | a4baf5b | 2018-04-22 13:27:44 +0200 | [diff] [blame] | 1635 | let save_shellslash = &shellslash |
| 1636 | set noshellslash |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1637 | au! CmdlineEnter / let g:entered = expand('<afile>') |
| 1638 | au! CmdlineLeave / let g:left = expand('<afile>') |
| 1639 | let g:entered = 0 |
| 1640 | let g:left = 0 |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1641 | new |
| 1642 | call setline(1, 'hello') |
| 1643 | call feedkeys("/hello\<CR>", 'xt') |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1644 | call assert_equal('/', g:entered) |
| 1645 | call assert_equal('/', g:left) |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1646 | bwipe! |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1647 | au! CmdlineEnter |
| 1648 | au! CmdlineLeave |
Bram Moolenaar | a4baf5b | 2018-04-22 13:27:44 +0200 | [diff] [blame] | 1649 | let &shellslash = save_shellslash |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1650 | endfunc |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1651 | |
| 1652 | " Test for BufWritePre autocommand that deletes or unloads the buffer. |
| 1653 | func Test_BufWritePre() |
| 1654 | %bwipe |
| 1655 | au BufWritePre Xxx1 bunload |
| 1656 | au BufWritePre Xxx2 bwipe |
| 1657 | |
| 1658 | call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1') |
| 1659 | call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2') |
| 1660 | |
| 1661 | edit Xtest |
| 1662 | e! Xxx2 |
| 1663 | bdel Xtest |
| 1664 | e Xxx1 |
| 1665 | " write it, will unload it and give an error msg |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1666 | call assert_fails('w', 'E203:') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1667 | call assert_equal('Xxx2', bufname('%')) |
| 1668 | edit Xtest |
| 1669 | e! Xxx2 |
| 1670 | bwipe Xtest |
| 1671 | " write it, will delete the buffer and give an error msg |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1672 | call assert_fails('w', 'E203:') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1673 | call assert_equal('Xxx1', bufname('%')) |
| 1674 | au! BufWritePre |
| 1675 | call delete('Xxx1') |
| 1676 | call delete('Xxx2') |
| 1677 | endfunc |
| 1678 | |
| 1679 | " Test for BufUnload autocommand that unloads all the other buffers |
| 1680 | func Test_bufunload_all() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 1681 | let g:test_is_flaky = 1 |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1682 | call writefile(['Test file Xxx1'], 'Xxx1')" |
| 1683 | call writefile(['Test file Xxx2'], 'Xxx2')" |
| 1684 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 1685 | let content =<< trim [CODE] |
| 1686 | func UnloadAllBufs() |
| 1687 | let i = 1 |
| 1688 | while i <= bufnr('$') |
| 1689 | if i != bufnr('%') && bufloaded(i) |
| 1690 | exe i . 'bunload' |
| 1691 | endif |
| 1692 | let i += 1 |
| 1693 | endwhile |
| 1694 | endfunc |
| 1695 | au BufUnload * call UnloadAllBufs() |
| 1696 | au VimLeave * call writefile(['Test Finished'], 'Xout') |
| 1697 | edit Xxx1 |
| 1698 | split Xxx2 |
| 1699 | q |
| 1700 | [CODE] |
| 1701 | |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1702 | call writefile(content, 'Xtest') |
| 1703 | |
| 1704 | call delete('Xout') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 1705 | call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1706 | call assert_true(filereadable('Xout')) |
| 1707 | |
| 1708 | call delete('Xxx1') |
| 1709 | call delete('Xxx2') |
| 1710 | call delete('Xtest') |
| 1711 | call delete('Xout') |
| 1712 | endfunc |
| 1713 | |
| 1714 | " Some tests for buffer-local autocommands |
| 1715 | func Test_buflocal_autocmd() |
| 1716 | let g:bname = '' |
| 1717 | edit xx |
| 1718 | au BufLeave <buffer> let g:bname = expand("%") |
| 1719 | " here, autocommand for xx should trigger. |
| 1720 | " but autocommand shall not apply to buffer named <buffer>. |
| 1721 | edit somefile |
| 1722 | call assert_equal('xx', g:bname) |
| 1723 | let g:bname = '' |
| 1724 | " here, autocommand shall be auto-deleted |
| 1725 | bwipe xx |
| 1726 | " autocmd should not trigger |
| 1727 | edit xx |
| 1728 | call assert_equal('', g:bname) |
| 1729 | " autocmd should not trigger |
| 1730 | edit somefile |
| 1731 | call assert_equal('', g:bname) |
| 1732 | enew |
| 1733 | unlet g:bname |
| 1734 | endfunc |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1735 | |
| 1736 | " Test for "*Cmd" autocommands |
| 1737 | func Test_Cmd_Autocmds() |
| 1738 | call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx') |
| 1739 | |
| 1740 | enew! |
| 1741 | au BufReadCmd XtestA 0r Xxx|$del |
| 1742 | edit XtestA " will read text of Xxd instead |
| 1743 | call assert_equal('start of Xxx', getline(1)) |
| 1744 | |
| 1745 | au BufWriteCmd XtestA call append(line("$"), "write") |
| 1746 | write " will append a line to the file |
| 1747 | call assert_equal('write', getline('$')) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1748 | call assert_fails('read XtestA', 'E484:') " should not read anything |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1749 | call assert_equal('write', getline(4)) |
| 1750 | |
| 1751 | " now we have: |
| 1752 | " 1 start of Xxx |
| 1753 | " 2 abc2 |
| 1754 | " 3 end of Xxx |
| 1755 | " 4 write |
| 1756 | |
| 1757 | au FileReadCmd XtestB '[r Xxx |
| 1758 | 2r XtestB " will read Xxx below line 2 instead |
| 1759 | call assert_equal('start of Xxx', getline(3)) |
| 1760 | |
| 1761 | " now we have: |
| 1762 | " 1 start of Xxx |
| 1763 | " 2 abc2 |
| 1764 | " 3 start of Xxx |
| 1765 | " 4 abc2 |
| 1766 | " 5 end of Xxx |
| 1767 | " 6 end of Xxx |
| 1768 | " 7 write |
| 1769 | |
| 1770 | au FileWriteCmd XtestC '[,']copy $ |
| 1771 | normal 4GA1 |
| 1772 | 4,5w XtestC " will copy lines 4 and 5 to the end |
| 1773 | call assert_equal("\tabc21", getline(8)) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1774 | call assert_fails('r XtestC', 'E484:') " should not read anything |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1775 | call assert_equal("end of Xxx", getline(9)) |
| 1776 | |
| 1777 | " now we have: |
| 1778 | " 1 start of Xxx |
| 1779 | " 2 abc2 |
| 1780 | " 3 start of Xxx |
| 1781 | " 4 abc21 |
| 1782 | " 5 end of Xxx |
| 1783 | " 6 end of Xxx |
| 1784 | " 7 write |
| 1785 | " 8 abc21 |
| 1786 | " 9 end of Xxx |
| 1787 | |
| 1788 | let g:lines = [] |
| 1789 | au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']"))) |
| 1790 | w >>XtestD " will add lines to 'lines' |
| 1791 | call assert_equal(9, len(g:lines)) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1792 | call assert_fails('$r XtestD', 'E484:') " should not read anything |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1793 | call assert_equal(9, line('$')) |
| 1794 | call assert_equal('end of Xxx', getline('$')) |
| 1795 | |
| 1796 | au BufReadCmd XtestE 0r Xxx|$del |
| 1797 | sp XtestE " split window with test.out |
| 1798 | call assert_equal('end of Xxx', getline(3)) |
| 1799 | |
| 1800 | let g:lines = [] |
| 1801 | exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>" |
| 1802 | au BufWriteCmd XtestE call extend(g:lines, getline(0, '$')) |
| 1803 | wall " will write other window to 'lines' |
| 1804 | call assert_equal(4, len(g:lines), g:lines) |
| 1805 | call assert_equal('asdf', g:lines[2]) |
| 1806 | |
| 1807 | au! BufReadCmd |
| 1808 | au! BufWriteCmd |
| 1809 | au! FileReadCmd |
| 1810 | au! FileWriteCmd |
| 1811 | au! FileAppendCmd |
| 1812 | %bwipe! |
| 1813 | call delete('Xxx') |
| 1814 | enew! |
| 1815 | endfunc |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1816 | |
Bram Moolenaar | 0fff441 | 2020-03-29 16:06:29 +0200 | [diff] [blame] | 1817 | func s:ReadFile() |
| 1818 | setl noswapfile nomodified |
| 1819 | let filename = resolve(expand("<afile>:p")) |
| 1820 | execute 'read' fnameescape(filename) |
| 1821 | 1d_ |
| 1822 | exe 'file' fnameescape(filename) |
| 1823 | setl buftype=acwrite |
| 1824 | endfunc |
| 1825 | |
| 1826 | func s:WriteFile() |
| 1827 | let filename = resolve(expand("<afile>:p")) |
| 1828 | setl buftype= |
| 1829 | noautocmd execute 'write' fnameescape(filename) |
| 1830 | setl buftype=acwrite |
| 1831 | setl nomodified |
| 1832 | endfunc |
| 1833 | |
| 1834 | func Test_BufReadCmd() |
| 1835 | autocmd BufReadCmd *.test call s:ReadFile() |
| 1836 | autocmd BufWriteCmd *.test call s:WriteFile() |
| 1837 | |
| 1838 | call writefile(['one', 'two', 'three'], 'Xcmd.test') |
| 1839 | edit Xcmd.test |
| 1840 | call assert_match('Xcmd.test" line 1 of 3', execute('file')) |
| 1841 | normal! Gofour |
| 1842 | write |
| 1843 | call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test')) |
| 1844 | |
| 1845 | bwipe! |
| 1846 | call delete('Xcmd.test') |
| 1847 | au! BufReadCmd |
| 1848 | au! BufWriteCmd |
| 1849 | endfunc |
| 1850 | |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1851 | func SetChangeMarks(start, end) |
Bram Moolenaar | 97c6943 | 2021-01-15 16:45:21 +0100 | [diff] [blame] | 1852 | exe a:start .. 'mark [' |
| 1853 | exe a:end .. 'mark ]' |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1854 | endfunc |
| 1855 | |
| 1856 | " Verify the effects of autocmds on '[ and '] |
| 1857 | func Test_change_mark_in_autocmds() |
| 1858 | edit! Xtest |
Bram Moolenaar | 97c6943 | 2021-01-15 16:45:21 +0100 | [diff] [blame] | 1859 | 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] | 1860 | |
| 1861 | call SetChangeMarks(2, 3) |
| 1862 | write |
| 1863 | call assert_equal([1, 4], [line("'["), line("']")]) |
| 1864 | |
| 1865 | call SetChangeMarks(2, 3) |
| 1866 | au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1867 | write |
| 1868 | au! BufWritePre |
| 1869 | |
Bram Moolenaar | 14ddd22 | 2020-08-05 12:02:40 +0200 | [diff] [blame] | 1870 | if has('unix') |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1871 | write XtestFilter |
| 1872 | write >> XtestFilter |
| 1873 | |
| 1874 | call SetChangeMarks(2, 3) |
| 1875 | " Marks are set to the entire range of the write |
| 1876 | au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1877 | " '[ is adjusted to just before the line that will receive the filtered |
| 1878 | " data |
| 1879 | au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")]) |
| 1880 | " The filtered data is read into the buffer, and the source lines are |
| 1881 | " still present, so the range is after the source lines |
| 1882 | au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")]) |
| 1883 | %!cat XtestFilter |
| 1884 | " After the filtered data is read, the original lines are deleted |
| 1885 | call assert_equal([1, 8], [line("'["), line("']")]) |
| 1886 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 1887 | undo |
| 1888 | |
| 1889 | call SetChangeMarks(1, 4) |
| 1890 | au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1891 | au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")]) |
| 1892 | au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")]) |
| 1893 | 2,3!cat XtestFilter |
| 1894 | call assert_equal([2, 9], [line("'["), line("']")]) |
| 1895 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 1896 | undo |
| 1897 | |
| 1898 | call delete('XtestFilter') |
| 1899 | endif |
| 1900 | |
| 1901 | call SetChangeMarks(1, 4) |
| 1902 | au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1903 | 2,3write Xtest2 |
| 1904 | au! FileWritePre |
| 1905 | |
| 1906 | call SetChangeMarks(2, 3) |
| 1907 | au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1908 | write >> Xtest2 |
| 1909 | au! FileAppendPre |
| 1910 | |
| 1911 | call SetChangeMarks(1, 4) |
| 1912 | au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1913 | 2,3write >> Xtest2 |
| 1914 | au! FileAppendPre |
| 1915 | |
| 1916 | call SetChangeMarks(1, 1) |
| 1917 | au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")]) |
| 1918 | au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")]) |
| 1919 | 3read Xtest2 |
| 1920 | au! FileReadPre,FileReadPost |
| 1921 | undo |
| 1922 | |
| 1923 | call SetChangeMarks(4, 4) |
| 1924 | " When the line is 0, it's adjusted to 1 |
| 1925 | au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1926 | au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")]) |
| 1927 | 0read Xtest2 |
| 1928 | au! FileReadPre,FileReadPost |
| 1929 | undo |
| 1930 | |
| 1931 | call SetChangeMarks(4, 4) |
| 1932 | " When the line is 0, it's adjusted to 1 |
| 1933 | au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1934 | au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")]) |
| 1935 | 1read Xtest2 |
| 1936 | au! FileReadPre,FileReadPost |
| 1937 | undo |
| 1938 | |
| 1939 | bwipe! |
| 1940 | call delete('Xtest') |
| 1941 | call delete('Xtest2') |
| 1942 | endfunc |
| 1943 | |
| 1944 | func Test_Filter_noshelltemp() |
Bram Moolenaar | aeb313f | 2020-11-27 19:13:28 +0100 | [diff] [blame] | 1945 | CheckExecutable cat |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1946 | |
| 1947 | enew! |
| 1948 | call setline(1, ['a', 'b', 'c', 'd']) |
| 1949 | |
| 1950 | let shelltemp = &shelltemp |
| 1951 | set shelltemp |
| 1952 | |
| 1953 | let g:filter_au = 0 |
| 1954 | au FilterWritePre * let g:filter_au += 1 |
| 1955 | au FilterReadPre * let g:filter_au += 1 |
| 1956 | au FilterReadPost * let g:filter_au += 1 |
| 1957 | %!cat |
| 1958 | call assert_equal(3, g:filter_au) |
| 1959 | |
| 1960 | if has('filterpipe') |
| 1961 | set noshelltemp |
| 1962 | |
| 1963 | let g:filter_au = 0 |
| 1964 | au FilterWritePre * let g:filter_au += 1 |
| 1965 | au FilterReadPre * let g:filter_au += 1 |
| 1966 | au FilterReadPost * let g:filter_au += 1 |
| 1967 | %!cat |
| 1968 | call assert_equal(0, g:filter_au) |
| 1969 | endif |
| 1970 | |
| 1971 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 1972 | let &shelltemp = shelltemp |
| 1973 | bwipe! |
| 1974 | endfunc |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1975 | |
| 1976 | func Test_TextYankPost() |
| 1977 | enew! |
| 1978 | call setline(1, ['foo']) |
| 1979 | |
| 1980 | let g:event = [] |
| 1981 | au TextYankPost * let g:event = copy(v:event) |
| 1982 | |
| 1983 | call assert_equal({}, v:event) |
| 1984 | call assert_fails('let v:event = {}', 'E46:') |
| 1985 | call assert_fails('let v:event.mykey = 0', 'E742:') |
| 1986 | |
| 1987 | norm "ayiw |
| 1988 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 1989 | \ #{regcontents: ['foo'], regname: 'a', operator: 'y', |
| 1990 | \ regtype: 'v', visual: v:false, inclusive: v:true}, |
| 1991 | \ g:event) |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1992 | norm y_ |
| 1993 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 1994 | \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V', |
| 1995 | \ visual: v:false, inclusive: v:false}, |
| 1996 | \ g:event) |
Bram Moolenaar | 37d1673 | 2020-06-12 22:09:01 +0200 | [diff] [blame] | 1997 | norm Vy |
| 1998 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 1999 | \ #{regcontents: ['foo'], regname: '', operator: 'y', regtype: 'V', |
| 2000 | \ visual: v:true, inclusive: v:true}, |
| 2001 | \ g:event) |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 2002 | call feedkeys("\<C-V>y", 'x') |
| 2003 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 2004 | \ #{regcontents: ['f'], regname: '', operator: 'y', regtype: "\x161", |
| 2005 | \ visual: v:true, inclusive: v:true}, |
| 2006 | \ g:event) |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 2007 | norm "xciwbar |
| 2008 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 2009 | \ #{regcontents: ['foo'], regname: 'x', operator: 'c', regtype: 'v', |
| 2010 | \ visual: v:false, inclusive: v:true}, |
| 2011 | \ g:event) |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 2012 | norm "bdiw |
| 2013 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 2014 | \ #{regcontents: ['bar'], regname: 'b', operator: 'd', regtype: 'v', |
| 2015 | \ visual: v:false, inclusive: v:true}, |
| 2016 | \ g:event) |
| 2017 | |
| 2018 | call setline(1, 'foobar') |
| 2019 | " exclusive motion |
| 2020 | norm $"ay0 |
| 2021 | call assert_equal( |
| 2022 | \ #{regcontents: ['fooba'], regname: 'a', operator: 'y', regtype: 'v', |
| 2023 | \ visual: v:false, inclusive: v:false}, |
| 2024 | \ g:event) |
| 2025 | " inclusive motion |
| 2026 | norm 0"ay$ |
| 2027 | call assert_equal( |
| 2028 | \ #{regcontents: ['foobar'], regname: 'a', operator: 'y', regtype: 'v', |
| 2029 | \ visual: v:false, inclusive: v:true}, |
| 2030 | \ g:event) |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 2031 | |
| 2032 | call assert_equal({}, v:event) |
| 2033 | |
Bram Moolenaar | fccbf06 | 2020-11-26 20:34:00 +0100 | [diff] [blame] | 2034 | if has('clipboard_working') && !has('gui_running') |
| 2035 | " Test that when the visual selection is automatically copied to clipboard |
| 2036 | " register a TextYankPost is emitted |
| 2037 | call setline(1, ['foobar']) |
| 2038 | |
| 2039 | let @* = '' |
| 2040 | set clipboard=autoselect |
| 2041 | exe "norm! ggviw\<Esc>" |
| 2042 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 2043 | \ #{regcontents: ['foobar'], regname: '*', operator: 'y', |
| 2044 | \ regtype: 'v', visual: v:true, inclusive: v:false}, |
| 2045 | \ g:event) |
Bram Moolenaar | fccbf06 | 2020-11-26 20:34:00 +0100 | [diff] [blame] | 2046 | |
| 2047 | let @+ = '' |
| 2048 | set clipboard=autoselectplus |
| 2049 | exe "norm! ggviw\<Esc>" |
| 2050 | call assert_equal( |
Bram Moolenaar | a016eeb | 2022-04-09 11:37:38 +0100 | [diff] [blame] | 2051 | \ #{regcontents: ['foobar'], regname: '+', operator: 'y', |
| 2052 | \ regtype: 'v', visual: v:true, inclusive: v:false}, |
| 2053 | \ g:event) |
Bram Moolenaar | fccbf06 | 2020-11-26 20:34:00 +0100 | [diff] [blame] | 2054 | |
| 2055 | set clipboard&vim |
| 2056 | endif |
| 2057 | |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 2058 | au! TextYankPost |
| 2059 | unlet g:event |
| 2060 | bwipe! |
| 2061 | endfunc |
Bram Moolenaar | 9bca805 | 2017-12-18 12:37:55 +0100 | [diff] [blame] | 2062 | |
Bram Moolenaar | 9a046fd | 2021-01-28 13:47:59 +0100 | [diff] [blame] | 2063 | func Test_autocommand_all_events() |
| 2064 | call assert_fails('au * * bwipe', 'E1155:') |
| 2065 | call assert_fails('au * x bwipe', 'E1155:') |
Bram Moolenaar | b6db146 | 2021-12-24 19:24:47 +0000 | [diff] [blame] | 2066 | call assert_fails('au! * x bwipe', 'E1155:') |
Bram Moolenaar | 4fb921e | 2017-12-18 15:33:00 +0100 | [diff] [blame] | 2067 | endfunc |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2068 | |
Bram Moolenaar | f6246f5 | 2022-02-11 16:30:12 +0000 | [diff] [blame] | 2069 | func Test_autocmd_user() |
| 2070 | au User MyEvent let s:res = [expand("<afile>"), expand("<amatch>")] |
| 2071 | doautocmd User MyEvent |
| 2072 | call assert_equal(['MyEvent', 'MyEvent'], s:res) |
| 2073 | au! User |
| 2074 | unlet s:res |
| 2075 | endfunc |
| 2076 | |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2077 | function s:Before_test_dirchanged() |
| 2078 | augroup test_dirchanged |
| 2079 | autocmd! |
| 2080 | augroup END |
| 2081 | let s:li = [] |
| 2082 | let s:dir_this = getcwd() |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2083 | let s:dir_foo = s:dir_this . '/Xfoo' |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 2084 | call mkdir(s:dir_foo) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2085 | let s:dir_bar = s:dir_this . '/Xbar' |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 2086 | call mkdir(s:dir_bar) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2087 | endfunc |
| 2088 | |
| 2089 | function s:After_test_dirchanged() |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2090 | call chdir(s:dir_this) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 2091 | call delete(s:dir_foo, 'd') |
| 2092 | call delete(s:dir_bar, 'd') |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2093 | augroup test_dirchanged |
| 2094 | autocmd! |
| 2095 | augroup END |
| 2096 | endfunc |
| 2097 | |
| 2098 | function Test_dirchanged_global() |
| 2099 | call s:Before_test_dirchanged() |
Bram Moolenaar | f6246f5 | 2022-02-11 16:30:12 +0000 | [diff] [blame] | 2100 | autocmd test_dirchanged DirChangedPre global call add(s:li, expand("<amatch>") .. " pre cd " .. v:event.directory) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2101 | autocmd test_dirchanged DirChanged global call add(s:li, "cd:") |
| 2102 | autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>")) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2103 | call chdir(s:dir_foo) |
Bram Moolenaar | f6246f5 | 2022-02-11 16:30:12 +0000 | [diff] [blame] | 2104 | let expected = ["global pre cd " .. s:dir_foo, "cd:", s:dir_foo] |
Bram Moolenaar | 28e8f73 | 2022-02-09 12:58:20 +0000 | [diff] [blame] | 2105 | call assert_equal(expected, s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2106 | call chdir(s:dir_foo) |
Bram Moolenaar | 28e8f73 | 2022-02-09 12:58:20 +0000 | [diff] [blame] | 2107 | call assert_equal(expected, s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2108 | exe 'lcd ' .. fnameescape(s:dir_bar) |
Bram Moolenaar | 28e8f73 | 2022-02-09 12:58:20 +0000 | [diff] [blame] | 2109 | call assert_equal(expected, s:li) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2110 | call s:After_test_dirchanged() |
| 2111 | endfunc |
| 2112 | |
| 2113 | function Test_dirchanged_local() |
| 2114 | call s:Before_test_dirchanged() |
| 2115 | autocmd test_dirchanged DirChanged window call add(s:li, "lcd:") |
| 2116 | autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>")) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2117 | call chdir(s:dir_foo) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2118 | call assert_equal([], s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2119 | exe 'lcd ' .. fnameescape(s:dir_bar) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 2120 | call assert_equal(["lcd:", s:dir_bar], s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2121 | exe 'lcd ' .. fnameescape(s:dir_bar) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 2122 | call assert_equal(["lcd:", s:dir_bar], s:li) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2123 | call s:After_test_dirchanged() |
| 2124 | endfunc |
| 2125 | |
| 2126 | function Test_dirchanged_auto() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 2127 | CheckOption autochdir |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2128 | call s:Before_test_dirchanged() |
| 2129 | call test_autochdir() |
Bram Moolenaar | 28e8f73 | 2022-02-09 12:58:20 +0000 | [diff] [blame] | 2130 | autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2131 | autocmd test_dirchanged DirChanged auto call add(s:li, "auto:") |
| 2132 | autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>")) |
| 2133 | set acd |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 2134 | cd .. |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2135 | call assert_equal([], s:li) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 2136 | exe 'edit ' . s:dir_foo . '/Xfile' |
| 2137 | call assert_equal(s:dir_foo, getcwd()) |
Bram Moolenaar | 28e8f73 | 2022-02-09 12:58:20 +0000 | [diff] [blame] | 2138 | let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo] |
| 2139 | call assert_equal(expected, s:li) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 2140 | set noacd |
| 2141 | bwipe! |
| 2142 | call s:After_test_dirchanged() |
| 2143 | endfunc |
Bram Moolenaar | 5a09343 | 2018-02-10 18:15:19 +0100 | [diff] [blame] | 2144 | |
| 2145 | " Test TextChangedI and TextChangedP |
| 2146 | func Test_ChangedP() |
| 2147 | new |
| 2148 | call setline(1, ['foo', 'bar', 'foobar']) |
| 2149 | call test_override("char_avail", 1) |
| 2150 | set complete=. completeopt=menuone |
| 2151 | |
| 2152 | func! TextChangedAutocmd(char) |
| 2153 | let g:autocmd .= a:char |
| 2154 | endfunc |
| 2155 | |
Christian Brabandt | db3b446 | 2021-10-16 11:58:55 +0100 | [diff] [blame] | 2156 | " TextChanged will not be triggered, only check that it isn't. |
Bram Moolenaar | 5a09343 | 2018-02-10 18:15:19 +0100 | [diff] [blame] | 2157 | au! TextChanged <buffer> :call TextChangedAutocmd('N') |
| 2158 | au! TextChangedI <buffer> :call TextChangedAutocmd('I') |
| 2159 | au! TextChangedP <buffer> :call TextChangedAutocmd('P') |
| 2160 | |
| 2161 | call cursor(3, 1) |
| 2162 | let g:autocmd = '' |
| 2163 | call feedkeys("o\<esc>", 'tnix') |
| 2164 | call assert_equal('I', g:autocmd) |
| 2165 | |
| 2166 | let g:autocmd = '' |
| 2167 | call feedkeys("Sf", 'tnix') |
| 2168 | call assert_equal('II', g:autocmd) |
| 2169 | |
| 2170 | let g:autocmd = '' |
| 2171 | call feedkeys("Sf\<C-N>", 'tnix') |
| 2172 | call assert_equal('IIP', g:autocmd) |
| 2173 | |
| 2174 | let g:autocmd = '' |
| 2175 | call feedkeys("Sf\<C-N>\<C-N>", 'tnix') |
| 2176 | call assert_equal('IIPP', g:autocmd) |
| 2177 | |
| 2178 | let g:autocmd = '' |
| 2179 | call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix') |
| 2180 | call assert_equal('IIPPP', g:autocmd) |
| 2181 | |
| 2182 | let g:autocmd = '' |
| 2183 | call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix') |
| 2184 | call assert_equal('IIPPPP', g:autocmd) |
| 2185 | |
| 2186 | call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$')) |
| 2187 | " TODO: how should it handle completeopt=noinsert,noselect? |
| 2188 | |
| 2189 | " CleanUp |
| 2190 | call test_override("char_avail", 0) |
| 2191 | au! TextChanged |
| 2192 | au! TextChangedI |
| 2193 | au! TextChangedP |
| 2194 | delfu TextChangedAutocmd |
| 2195 | unlet! g:autocmd |
| 2196 | set complete&vim completeopt&vim |
| 2197 | |
| 2198 | bw! |
| 2199 | endfunc |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2200 | |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 2201 | let g:setline_handled = v:false |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 2202 | func SetLineOne() |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 2203 | if !g:setline_handled |
| 2204 | call setline(1, "(x)") |
| 2205 | let g:setline_handled = v:true |
| 2206 | endif |
| 2207 | endfunc |
| 2208 | |
| 2209 | func Test_TextChangedI_with_setline() |
| 2210 | new |
| 2211 | call test_override('char_avail', 1) |
| 2212 | autocmd TextChangedI <buffer> call SetLineOne() |
| 2213 | call feedkeys("i(\<CR>\<Esc>", 'tx') |
| 2214 | call assert_equal('(', getline(1)) |
| 2215 | call assert_equal('x)', getline(2)) |
| 2216 | undo |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 2217 | call assert_equal('', getline(1)) |
Bram Moolenaar | 9fa9506 | 2018-08-08 22:08:32 +0200 | [diff] [blame] | 2218 | call assert_equal('', getline(2)) |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 2219 | |
Bram Moolenaar | ca34db3 | 2022-01-20 11:17:18 +0000 | [diff] [blame] | 2220 | call test_override('char_avail', 0) |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 2221 | bwipe! |
| 2222 | endfunc |
| 2223 | |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2224 | func Test_Changed_FirstTime() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 2225 | CheckFeature terminal |
| 2226 | CheckNotGui |
Bram Moolenaar | 3cdcb09 | 2020-03-18 19:18:10 +0100 | [diff] [blame] | 2227 | " Starting a terminal to run Vim is always considered flaky. |
Bram Moolenaar | 30d53e2 | 2020-03-18 21:10:44 +0100 | [diff] [blame] | 2228 | let g:test_is_flaky = 1 |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 2229 | |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2230 | " Prepare file for TextChanged event. |
| 2231 | call writefile([''], 'Xchanged.txt') |
| 2232 | let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3}) |
| 2233 | call assert_equal('running', term_getstatus(buf)) |
Bram Moolenaar | 1834d37 | 2018-03-29 17:40:46 +0200 | [diff] [blame] | 2234 | " Wait for the ruler (in the status line) to be shown. |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 2235 | " In ConPTY, there is additional character which is drawn up to the width of |
| 2236 | " the screen. |
| 2237 | if has('conpty') |
| 2238 | call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))}) |
| 2239 | else |
| 2240 | call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))}) |
| 2241 | endif |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2242 | " It's only adding autocmd, so that no event occurs. |
| 2243 | call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>") |
| 2244 | call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>") |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 2245 | call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))}) |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2246 | call assert_equal([''], readfile('Xchanged.txt')) |
| 2247 | |
| 2248 | " clean up |
| 2249 | call delete('Xchanged.txt') |
| 2250 | bwipe! |
| 2251 | endfunc |
Bram Moolenaar | 0566e89 | 2019-01-24 19:37:40 +0100 | [diff] [blame] | 2252 | |
Bram Moolenaar | eb93f3f | 2019-04-04 15:04:56 +0200 | [diff] [blame] | 2253 | func Test_autocmd_nested() |
| 2254 | let g:did_nested = 0 |
| 2255 | augroup Testing |
| 2256 | au WinNew * edit somefile |
| 2257 | au BufNew * let g:did_nested = 1 |
| 2258 | augroup END |
| 2259 | split |
| 2260 | call assert_equal(0, g:did_nested) |
| 2261 | close |
| 2262 | bwipe! somefile |
| 2263 | |
| 2264 | " old nested argument still works |
| 2265 | augroup Testing |
| 2266 | au! |
| 2267 | au WinNew * nested edit somefile |
| 2268 | au BufNew * let g:did_nested = 1 |
| 2269 | augroup END |
| 2270 | split |
| 2271 | call assert_equal(1, g:did_nested) |
| 2272 | close |
| 2273 | bwipe! somefile |
| 2274 | |
| 2275 | " New ++nested argument works |
| 2276 | augroup Testing |
| 2277 | au! |
| 2278 | au WinNew * ++nested edit somefile |
| 2279 | au BufNew * let g:did_nested = 1 |
| 2280 | augroup END |
| 2281 | split |
| 2282 | call assert_equal(1, g:did_nested) |
| 2283 | close |
| 2284 | bwipe! somefile |
| 2285 | |
Bram Moolenaar | f077514 | 2022-03-04 20:10:38 +0000 | [diff] [blame] | 2286 | " nested without ++ does not work in Vim9 script |
| 2287 | call assert_fails('vim9cmd au WinNew * nested echo fails', 'E1078:') |
| 2288 | |
Bram Moolenaar | eb93f3f | 2019-04-04 15:04:56 +0200 | [diff] [blame] | 2289 | augroup Testing |
| 2290 | au! |
| 2291 | augroup END |
| 2292 | |
| 2293 | call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:') |
| 2294 | call assert_fails('au WinNew * nested nested echo bad', 'E983:') |
| 2295 | endfunc |
| 2296 | |
| 2297 | func Test_autocmd_once() |
| 2298 | " Without ++once WinNew triggers twice |
| 2299 | let g:did_split = 0 |
| 2300 | augroup Testing |
| 2301 | au WinNew * let g:did_split += 1 |
| 2302 | augroup END |
| 2303 | split |
| 2304 | split |
| 2305 | call assert_equal(2, g:did_split) |
| 2306 | call assert_true(exists('#WinNew')) |
| 2307 | close |
| 2308 | close |
| 2309 | |
| 2310 | " With ++once WinNew triggers once |
| 2311 | let g:did_split = 0 |
| 2312 | augroup Testing |
| 2313 | au! |
| 2314 | au WinNew * ++once let g:did_split += 1 |
| 2315 | augroup END |
| 2316 | split |
| 2317 | split |
| 2318 | call assert_equal(1, g:did_split) |
| 2319 | call assert_false(exists('#WinNew')) |
| 2320 | close |
| 2321 | close |
| 2322 | |
| 2323 | call assert_fails('au WinNew * ++once ++once echo bad', 'E983:') |
| 2324 | endfunc |
| 2325 | |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2326 | func Test_autocmd_bufreadpre() |
| 2327 | new |
| 2328 | let b:bufreadpre = 1 |
Bram Moolenaar | ab505b1 | 2020-03-23 19:28:44 +0100 | [diff] [blame] | 2329 | call append(0, range(1000)) |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2330 | w! XAutocmdBufReadPre.txt |
| 2331 | autocmd BufReadPre <buffer> :let b:bufreadpre += 1 |
Bram Moolenaar | ab505b1 | 2020-03-23 19:28:44 +0100 | [diff] [blame] | 2332 | norm! 500gg |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2333 | sp |
Bram Moolenaar | ab505b1 | 2020-03-23 19:28:44 +0100 | [diff] [blame] | 2334 | norm! 1000gg |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2335 | wincmd p |
| 2336 | let g:wsv1 = winsaveview() |
| 2337 | wincmd p |
| 2338 | let g:wsv2 = winsaveview() |
| 2339 | " triggers BufReadPre, should not move the cursor in either window |
| 2340 | " The topline may change one line in a large window. |
| 2341 | edit |
| 2342 | call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline) |
| 2343 | call assert_equal(g:wsv2.lnum, winsaveview().lnum) |
| 2344 | call assert_equal(2, b:bufreadpre) |
| 2345 | wincmd p |
| 2346 | call assert_equal(g:wsv1.topline, winsaveview().topline) |
| 2347 | call assert_equal(g:wsv1.lnum, winsaveview().lnum) |
| 2348 | call assert_equal(2, b:bufreadpre) |
| 2349 | " Now set the cursor position in an BufReadPre autocommand |
| 2350 | " (even though the position will be invalid, this should make Vim reset the |
| 2351 | " cursor position in the other window. |
| 2352 | wincmd p |
| 2353 | set cpo+=g |
| 2354 | " won't do anything, but try to set the cursor on an invalid lnum |
| 2355 | autocmd BufReadPre <buffer> :norm! 70gg |
| 2356 | " triggers BufReadPre, should not move the cursor in either window |
| 2357 | e |
| 2358 | call assert_equal(1, winsaveview().topline) |
| 2359 | call assert_equal(1, winsaveview().lnum) |
| 2360 | call assert_equal(3, b:bufreadpre) |
| 2361 | wincmd p |
| 2362 | call assert_equal(g:wsv1.topline, winsaveview().topline) |
| 2363 | call assert_equal(g:wsv1.lnum, winsaveview().lnum) |
| 2364 | call assert_equal(3, b:bufreadpre) |
| 2365 | close |
| 2366 | close |
| 2367 | call delete('XAutocmdBufReadPre.txt') |
| 2368 | set cpo-=g |
| 2369 | endfunc |
| 2370 | |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 2371 | " FileChangedShell tested in test_filechanged.vim |
Bram Moolenaar | 69ea587 | 2019-04-25 20:29:00 +0200 | [diff] [blame] | 2372 | |
| 2373 | " Tests for the following autocommands: |
| 2374 | " - FileWritePre writing a compressed file |
| 2375 | " - FileReadPost reading a compressed file |
| 2376 | " - BufNewFile reading a file template |
| 2377 | " - BufReadPre decompressing the file to be read |
| 2378 | " - FilterReadPre substituting characters in the temp file |
| 2379 | " - FilterReadPost substituting characters after filtering |
| 2380 | " - FileReadPre set options for decompression |
| 2381 | " - FileReadPost decompress the file |
| 2382 | func Test_ReadWrite_Autocmds() |
| 2383 | " 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] | 2384 | CheckUnix |
| 2385 | CheckExecutable gzip |
Bram Moolenaar | 69ea587 | 2019-04-25 20:29:00 +0200 | [diff] [blame] | 2386 | |
| 2387 | " Make $GZIP empty, "-v" would cause trouble. |
| 2388 | let $GZIP = "" |
| 2389 | |
| 2390 | " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz' |
| 2391 | " being modified outside of Vim (noticed on Solaris). |
| 2392 | au FileChangedShell * echo 'caught FileChangedShell' |
| 2393 | |
| 2394 | " Test for the FileReadPost, FileWritePre and FileWritePost autocmds |
| 2395 | augroup Test1 |
| 2396 | au! |
| 2397 | au FileWritePre *.gz '[,']!gzip |
| 2398 | au FileWritePost *.gz undo |
| 2399 | au FileReadPost *.gz '[,']!gzip -d |
| 2400 | augroup END |
| 2401 | |
| 2402 | new |
| 2403 | set bin |
| 2404 | call append(0, [ |
| 2405 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2406 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2407 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2408 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2409 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2410 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2411 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2412 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2413 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2414 | \ ]) |
| 2415 | 1,9write! Xtestfile.gz |
| 2416 | enew! | close |
| 2417 | |
| 2418 | new |
| 2419 | " Read and decompress the testfile |
| 2420 | 0read Xtestfile.gz |
| 2421 | call assert_equal([ |
| 2422 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2423 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2424 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2425 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2426 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2427 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2428 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2429 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2430 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2431 | \ ], getline(1, 9)) |
| 2432 | enew! | close |
| 2433 | |
| 2434 | augroup Test1 |
| 2435 | au! |
| 2436 | augroup END |
| 2437 | |
| 2438 | " Test for the FileAppendPre and FileAppendPost autocmds |
| 2439 | augroup Test2 |
| 2440 | au! |
| 2441 | au BufNewFile *.c read Xtest.c |
| 2442 | au FileAppendPre *.out '[,']s/new/NEW/ |
| 2443 | au FileAppendPost *.out !cat Xtest.c >> test.out |
| 2444 | augroup END |
| 2445 | |
| 2446 | call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c') |
| 2447 | new foo.c " should load Xtest.c |
| 2448 | call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4)) |
| 2449 | w! >> test.out " append it to the output file |
| 2450 | |
| 2451 | let contents = readfile('test.out') |
| 2452 | call assert_equal(' * Here is a NEW .c file', contents[2]) |
| 2453 | call assert_equal(' * Here is a new .c file', contents[5]) |
| 2454 | |
| 2455 | call delete('test.out') |
| 2456 | enew! | close |
| 2457 | augroup Test2 |
| 2458 | au! |
| 2459 | augroup END |
| 2460 | |
| 2461 | " Test for the BufReadPre and BufReadPost autocmds |
| 2462 | augroup Test3 |
| 2463 | au! |
| 2464 | " setup autocommands to decompress before reading and re-compress |
| 2465 | " afterwards |
| 2466 | au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>")) |
| 2467 | au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>")) |
| 2468 | au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r")) |
| 2469 | au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r")) |
| 2470 | augroup END |
| 2471 | |
| 2472 | e! Xtestfile.gz " Edit compressed file |
| 2473 | call assert_equal([ |
| 2474 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2475 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2476 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2477 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2478 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2479 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2480 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2481 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2482 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2483 | \ ], getline(1, 9)) |
| 2484 | |
| 2485 | w! >> test.out " Append it to the output file |
| 2486 | |
| 2487 | augroup Test3 |
| 2488 | au! |
| 2489 | augroup END |
| 2490 | |
| 2491 | " Test for the FilterReadPre and FilterReadPost autocmds. |
| 2492 | set shelltemp " need temp files here |
| 2493 | augroup Test4 |
| 2494 | au! |
| 2495 | au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t") |
| 2496 | au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>")) |
| 2497 | au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t' |
| 2498 | au FilterReadPost *.out '[,']s/x/X/g |
| 2499 | augroup END |
| 2500 | |
| 2501 | e! test.out " Edit the output file |
| 2502 | 1,$!cat |
| 2503 | call assert_equal([ |
| 2504 | \ 'linE 2 AbcdefghijklmnopqrstuvwXyz', |
| 2505 | \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 2506 | \ 'linE 4 AbcdefghijklmnopqrstuvwXyz', |
| 2507 | \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 2508 | \ 'linE 6 AbcdefghijklmnopqrstuvwXyz', |
| 2509 | \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 2510 | \ 'linE 8 AbcdefghijklmnopqrstuvwXyz', |
| 2511 | \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 2512 | \ 'linE 10 AbcdefghijklmnopqrstuvwXyz' |
| 2513 | \ ], getline(1, 9)) |
| 2514 | call assert_equal([ |
| 2515 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2516 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2517 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2518 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2519 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2520 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2521 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2522 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2523 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2524 | \ ], readfile('test.out')) |
| 2525 | |
| 2526 | augroup Test4 |
| 2527 | au! |
| 2528 | augroup END |
| 2529 | set shelltemp&vim |
| 2530 | |
| 2531 | " Test for the FileReadPre and FileReadPost autocmds. |
| 2532 | augroup Test5 |
| 2533 | au! |
| 2534 | au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>")) |
| 2535 | au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>")) |
| 2536 | au FileReadPost *.gz '[,']s/l/L/ |
| 2537 | augroup END |
| 2538 | |
| 2539 | new |
| 2540 | 0r Xtestfile.gz " Read compressed file |
| 2541 | call assert_equal([ |
| 2542 | \ 'Line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2543 | \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2544 | \ 'Line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2545 | \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2546 | \ 'Line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2547 | \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2548 | \ 'Line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2549 | \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2550 | \ 'Line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2551 | \ ], getline(1, 9)) |
| 2552 | call assert_equal([ |
| 2553 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2554 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2555 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2556 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2557 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2558 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2559 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2560 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2561 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2562 | \ ], readfile('Xtestfile.gz')) |
| 2563 | |
| 2564 | augroup Test5 |
| 2565 | au! |
| 2566 | augroup END |
| 2567 | |
| 2568 | au! FileChangedShell |
| 2569 | call delete('Xtestfile.gz') |
| 2570 | call delete('Xtest.c') |
| 2571 | call delete('test.out') |
| 2572 | endfunc |
Bram Moolenaar | 23b5139 | 2019-05-09 21:38:43 +0200 | [diff] [blame] | 2573 | |
| 2574 | func Test_throw_in_BufWritePre() |
| 2575 | new |
| 2576 | call setline(1, ['one', 'two', 'three']) |
| 2577 | call assert_false(filereadable('Xthefile')) |
| 2578 | augroup throwing |
| 2579 | au BufWritePre X* throw 'do not write' |
| 2580 | augroup END |
| 2581 | try |
| 2582 | w Xthefile |
| 2583 | catch |
| 2584 | let caught = 1 |
| 2585 | endtry |
| 2586 | call assert_equal(1, caught) |
| 2587 | call assert_false(filereadable('Xthefile')) |
| 2588 | |
| 2589 | bwipe! |
| 2590 | au! throwing |
| 2591 | endfunc |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2592 | |
Bram Moolenaar | 40fa12a | 2021-09-22 14:18:13 +0200 | [diff] [blame] | 2593 | func Test_autocmd_in_try_block() |
| 2594 | call mkdir('Xdir') |
| 2595 | au BufEnter * let g:fname = expand('%') |
| 2596 | try |
| 2597 | edit Xdir/ |
| 2598 | endtry |
| 2599 | call assert_match('Xdir', g:fname) |
| 2600 | |
| 2601 | unlet g:fname |
| 2602 | au! BufEnter |
| 2603 | call delete('Xdir', 'rf') |
| 2604 | endfunc |
| 2605 | |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2606 | func Test_autocmd_SafeState() |
| 2607 | CheckRunVimInTerminal |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 2608 | let g:test_is_flaky = 1 |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2609 | |
| 2610 | let lines =<< trim END |
| 2611 | let g:safe = 0 |
| 2612 | let g:again = '' |
| 2613 | au SafeState * let g:safe += 1 |
| 2614 | au SafeStateAgain * let g:again ..= 'x' |
| 2615 | func CallTimer() |
| 2616 | call timer_start(10, {id -> execute('let g:again ..= "t"')}) |
| 2617 | endfunc |
| 2618 | END |
| 2619 | call writefile(lines, 'XSafeState') |
| 2620 | let buf = RunVimInTerminal('-S XSafeState', #{rows: 6}) |
| 2621 | |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 2622 | " 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] | 2623 | " more often. |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2624 | call term_sendkeys(buf, ":echo g:safe\<CR>") |
Bram Moolenaar | 8fb1b47 | 2020-02-23 16:16:26 +0100 | [diff] [blame] | 2625 | call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000) |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2626 | |
Bram Moolenaar | 8fb1b47 | 2020-02-23 16:16:26 +0100 | [diff] [blame] | 2627 | " SafeStateAgain should be invoked at least three times |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2628 | call term_sendkeys(buf, ":echo g:again\<CR>") |
Bram Moolenaar | 8fb1b47 | 2020-02-23 16:16:26 +0100 | [diff] [blame] | 2629 | call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000) |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2630 | |
| 2631 | call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2632 | call TermWait(buf, 50) |
Bram Moolenaar | 0f6629a | 2019-09-22 23:24:13 +0200 | [diff] [blame] | 2633 | call term_sendkeys(buf, ":\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2634 | call TermWait(buf, 50) |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2635 | call term_sendkeys(buf, ":echo g:again\<CR>") |
| 2636 | call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000) |
| 2637 | |
| 2638 | call StopVimInTerminal(buf) |
| 2639 | call delete('XSafeState') |
| 2640 | endfunc |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2641 | |
| 2642 | func Test_autocmd_CmdWinEnter() |
| 2643 | CheckRunVimInTerminal |
Bram Moolenaar | 21829c5 | 2021-01-26 22:42:21 +0100 | [diff] [blame] | 2644 | CheckFeature cmdwin |
| 2645 | |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2646 | let lines =<< trim END |
Egor Zvorykin | 125ffd2 | 2021-11-17 14:01:14 +0000 | [diff] [blame] | 2647 | augroup vimHints | au! | augroup END |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2648 | let b:dummy_var = 'This is a dummy' |
| 2649 | autocmd CmdWinEnter * quit |
| 2650 | let winnr = winnr('$') |
| 2651 | END |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 2652 | let filename = 'XCmdWinEnter' |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2653 | call writefile(lines, filename) |
| 2654 | let buf = RunVimInTerminal('-S '.filename, #{rows: 6}) |
| 2655 | |
| 2656 | call term_sendkeys(buf, "q:") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2657 | call TermWait(buf) |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2658 | call term_sendkeys(buf, ":echo b:dummy_var\<cr>") |
Bram Moolenaar | 353c351 | 2020-03-15 14:19:26 +0100 | [diff] [blame] | 2659 | 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] | 2660 | call term_sendkeys(buf, ":echo &buftype\<cr>") |
| 2661 | call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000) |
| 2662 | call term_sendkeys(buf, ":echo winnr\<cr>") |
| 2663 | call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000) |
| 2664 | |
| 2665 | " clean up |
| 2666 | call StopVimInTerminal(buf) |
| 2667 | call delete(filename) |
| 2668 | endfunc |
Bram Moolenaar | ec66c41 | 2019-10-11 21:19:13 +0200 | [diff] [blame] | 2669 | |
| 2670 | func Test_autocmd_was_using_freed_memory() |
Bram Moolenaar | 5a4c308 | 2019-12-01 15:23:11 +0100 | [diff] [blame] | 2671 | CheckFeature quickfix |
| 2672 | |
Bram Moolenaar | ec66c41 | 2019-10-11 21:19:13 +0200 | [diff] [blame] | 2673 | pedit xx |
| 2674 | n x |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 2675 | augroup winenter |
| 2676 | au WinEnter * if winnr('$') > 2 | quit | endif |
| 2677 | augroup END |
Bram Moolenaar | ec66c41 | 2019-10-11 21:19:13 +0200 | [diff] [blame] | 2678 | split |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 2679 | |
| 2680 | augroup winenter |
| 2681 | au! WinEnter |
| 2682 | augroup END |
| 2683 | |
| 2684 | bwipe xx |
| 2685 | bwipe x |
| 2686 | pclose |
Bram Moolenaar | ec66c41 | 2019-10-11 21:19:13 +0200 | [diff] [blame] | 2687 | endfunc |
Bram Moolenaar | f4a1d1c | 2019-11-16 13:50:25 +0100 | [diff] [blame] | 2688 | |
| 2689 | func Test_BufWrite_lockmarks() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 2690 | let g:test_is_flaky = 1 |
Bram Moolenaar | f4a1d1c | 2019-11-16 13:50:25 +0100 | [diff] [blame] | 2691 | edit! Xtest |
| 2692 | call setline(1, ['a', 'b', 'c', 'd']) |
| 2693 | |
| 2694 | " :lockmarks preserves the marks |
| 2695 | call SetChangeMarks(2, 3) |
| 2696 | lockmarks write |
| 2697 | call assert_equal([2, 3], [line("'["), line("']")]) |
| 2698 | |
| 2699 | " *WritePre autocmds get the correct line range, but lockmarks preserves the |
| 2700 | " original values for the user |
| 2701 | augroup lockmarks |
| 2702 | au! |
| 2703 | au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 2704 | au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")]) |
| 2705 | augroup END |
| 2706 | |
| 2707 | lockmarks write |
| 2708 | call assert_equal([2, 3], [line("'["), line("']")]) |
| 2709 | |
| 2710 | if executable('cat') |
| 2711 | lockmarks %!cat |
| 2712 | call assert_equal([2, 3], [line("'["), line("']")]) |
| 2713 | endif |
| 2714 | |
| 2715 | lockmarks 3,4write Xtest2 |
| 2716 | call assert_equal([2, 3], [line("'["), line("']")]) |
| 2717 | |
| 2718 | au! lockmarks |
| 2719 | augroup! lockmarks |
| 2720 | call delete('Xtest') |
| 2721 | call delete('Xtest2') |
| 2722 | endfunc |
Bram Moolenaar | ce6db02 | 2020-01-07 20:11:42 +0100 | [diff] [blame] | 2723 | |
| 2724 | func Test_FileType_spell() |
| 2725 | if !isdirectory('/tmp') |
| 2726 | throw "Skipped: requires /tmp directory" |
| 2727 | endif |
| 2728 | |
| 2729 | " this was crashing with an invalid free() |
| 2730 | setglobal spellfile=/tmp/en.utf-8.add |
| 2731 | augroup crash |
| 2732 | autocmd! |
| 2733 | autocmd BufNewFile,BufReadPost crashfile setf somefiletype |
| 2734 | autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype |
| 2735 | autocmd FileType anotherfiletype setlocal spell |
| 2736 | augroup END |
| 2737 | func! NoCrash() abort |
| 2738 | edit /tmp/crashfile |
| 2739 | endfunc |
| 2740 | call NoCrash() |
| 2741 | |
| 2742 | au! crash |
| 2743 | setglobal spellfile= |
| 2744 | endfunc |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 2745 | |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 2746 | " Test closing a window or editing another buffer from a FileChangedRO handler |
| 2747 | " in a readonly buffer |
| 2748 | func Test_FileChangedRO_winclose() |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 2749 | call test_override('ui_delay', 10) |
| 2750 | |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 2751 | augroup FileChangedROTest |
| 2752 | au! |
| 2753 | autocmd FileChangedRO * quit |
| 2754 | augroup END |
| 2755 | new |
| 2756 | set readonly |
| 2757 | call assert_fails('normal i', 'E788:') |
| 2758 | close |
| 2759 | augroup! FileChangedROTest |
| 2760 | |
| 2761 | augroup FileChangedROTest |
| 2762 | au! |
| 2763 | autocmd FileChangedRO * edit Xfile |
| 2764 | augroup END |
| 2765 | new |
| 2766 | set readonly |
| 2767 | call assert_fails('normal i', 'E788:') |
| 2768 | close |
| 2769 | augroup! FileChangedROTest |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 2770 | call test_override('ALL', 0) |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 2771 | endfunc |
| 2772 | |
Bram Moolenaar | 0c81d1b | 2020-02-22 22:45:55 +0100 | [diff] [blame] | 2773 | func LogACmd() |
| 2774 | call add(g:logged, line('$')) |
| 2775 | endfunc |
| 2776 | |
| 2777 | func Test_TermChanged() |
Bram Moolenaar | d28e0b3 | 2020-02-22 23:08:52 +0100 | [diff] [blame] | 2778 | CheckNotGui |
| 2779 | |
Bram Moolenaar | 0c81d1b | 2020-02-22 22:45:55 +0100 | [diff] [blame] | 2780 | enew! |
| 2781 | tabnew |
| 2782 | call setline(1, ['a', 'b', 'c', 'd']) |
| 2783 | $ |
| 2784 | au TermChanged * call LogACmd() |
| 2785 | let g:logged = [] |
| 2786 | let term_save = &term |
| 2787 | set term=xterm |
| 2788 | call assert_equal([1, 4], g:logged) |
| 2789 | |
| 2790 | au! TermChanged |
| 2791 | let &term = term_save |
| 2792 | bwipe! |
| 2793 | endfunc |
| 2794 | |
Bram Moolenaar | e328487 | 2020-03-19 13:55:03 +0100 | [diff] [blame] | 2795 | " Test for FileReadCmd autocmd |
| 2796 | func Test_autocmd_FileReadCmd() |
| 2797 | func ReadFileCmd() |
| 2798 | call append(line('$'), "v:cmdarg = " .. v:cmdarg) |
| 2799 | endfunc |
| 2800 | augroup FileReadCmdTest |
| 2801 | au! |
| 2802 | au FileReadCmd Xtest call ReadFileCmd() |
| 2803 | augroup END |
| 2804 | |
| 2805 | new |
| 2806 | read ++bin Xtest |
| 2807 | read ++nobin Xtest |
| 2808 | read ++edit Xtest |
| 2809 | read ++bad=keep Xtest |
| 2810 | read ++bad=drop Xtest |
| 2811 | read ++bad=- Xtest |
| 2812 | read ++ff=unix Xtest |
| 2813 | read ++ff=dos Xtest |
| 2814 | read ++ff=mac Xtest |
| 2815 | read ++enc=utf-8 Xtest |
| 2816 | |
| 2817 | call assert_equal(['', |
| 2818 | \ 'v:cmdarg = ++bin', |
| 2819 | \ 'v:cmdarg = ++nobin', |
| 2820 | \ 'v:cmdarg = ++edit', |
| 2821 | \ 'v:cmdarg = ++bad=keep', |
| 2822 | \ 'v:cmdarg = ++bad=drop', |
| 2823 | \ 'v:cmdarg = ++bad=-', |
| 2824 | \ 'v:cmdarg = ++ff=unix', |
| 2825 | \ 'v:cmdarg = ++ff=dos', |
| 2826 | \ 'v:cmdarg = ++ff=mac', |
| 2827 | \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$')) |
| 2828 | |
| 2829 | close! |
| 2830 | augroup FileReadCmdTest |
| 2831 | au! |
| 2832 | augroup END |
| 2833 | delfunc ReadFileCmd |
| 2834 | endfunc |
| 2835 | |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 2836 | " Test for passing invalid arguments to autocmd |
| 2837 | func Test_autocmd_invalid_args() |
| 2838 | " Additional character after * for event |
| 2839 | call assert_fails('autocmd *a Xfile set ff=unix', 'E215:') |
| 2840 | augroup Test |
| 2841 | augroup END |
| 2842 | " Invalid autocmd event |
| 2843 | call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:') |
| 2844 | " Invalid autocmd event in a autocmd group |
| 2845 | call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:') |
| 2846 | augroup! Test |
| 2847 | " Execute all autocmds |
| 2848 | call assert_fails('doautocmd * BufEnter', 'E217:') |
| 2849 | call assert_fails('augroup! x1a2b3', 'E367:') |
| 2850 | call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:') |
Bram Moolenaar | 531be47 | 2020-09-23 22:38:05 +0200 | [diff] [blame] | 2851 | call assert_fails('autocmd BufNew \) set ff=unix', 'E55:') |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 2852 | endfunc |
| 2853 | |
| 2854 | " Test for deep nesting of autocmds |
| 2855 | func Test_autocmd_deep_nesting() |
| 2856 | autocmd BufEnter Xfile doautocmd BufEnter Xfile |
| 2857 | call assert_fails('doautocmd BufEnter Xfile', 'E218:') |
| 2858 | autocmd! BufEnter Xfile |
| 2859 | endfunc |
| 2860 | |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 2861 | " Tests for SigUSR1 autocmd event, which is only available on posix systems. |
| 2862 | func Test_autocmd_sigusr1() |
| 2863 | CheckUnix |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 2864 | CheckExecutable /bin/kill |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 2865 | |
| 2866 | let g:sigusr1_passed = 0 |
| 2867 | au SigUSR1 * let g:sigusr1_passed = 1 |
| 2868 | call system('/bin/kill -s usr1 ' . getpid()) |
| 2869 | call WaitForAssert({-> assert_true(g:sigusr1_passed)}) |
| 2870 | |
| 2871 | au! SigUSR1 |
| 2872 | unlet g:sigusr1_passed |
| 2873 | endfunc |
| 2874 | |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 2875 | " Test for BufReadPre autocmd deleting the file |
| 2876 | func Test_BufReadPre_delfile() |
| 2877 | augroup TestAuCmd |
| 2878 | au! |
| 2879 | autocmd BufReadPre Xfile call delete('Xfile') |
| 2880 | augroup END |
| 2881 | call writefile([], 'Xfile') |
| 2882 | call assert_fails('new Xfile', 'E200:') |
| 2883 | call assert_equal('Xfile', @%) |
| 2884 | call assert_equal(1, &readonly) |
| 2885 | call delete('Xfile') |
| 2886 | augroup TestAuCmd |
| 2887 | au! |
| 2888 | augroup END |
| 2889 | close! |
| 2890 | endfunc |
| 2891 | |
| 2892 | " Test for BufReadPre autocmd changing the current buffer |
| 2893 | func Test_BufReadPre_changebuf() |
| 2894 | augroup TestAuCmd |
| 2895 | au! |
| 2896 | autocmd BufReadPre Xfile edit Xsomeotherfile |
| 2897 | augroup END |
| 2898 | call writefile([], 'Xfile') |
| 2899 | call assert_fails('new Xfile', 'E201:') |
| 2900 | call assert_equal('Xsomeotherfile', @%) |
| 2901 | call assert_equal(1, &readonly) |
| 2902 | call delete('Xfile') |
| 2903 | augroup TestAuCmd |
| 2904 | au! |
| 2905 | augroup END |
| 2906 | close! |
| 2907 | endfunc |
| 2908 | |
| 2909 | " Test for BufWipeouti autocmd changing the current buffer when reading a file |
| 2910 | " in an empty buffer with 'f' flag in 'cpo' |
| 2911 | func Test_BufDelete_changebuf() |
| 2912 | new |
| 2913 | augroup TestAuCmd |
| 2914 | au! |
| 2915 | autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr |
| 2916 | augroup END |
| 2917 | let save_cpo = &cpo |
| 2918 | set cpo+=f |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 2919 | call assert_fails('r Xfile', ['E812:', 'E484:']) |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 2920 | call assert_equal('somefile', @%) |
| 2921 | let &cpo = save_cpo |
| 2922 | augroup TestAuCmd |
| 2923 | au! |
| 2924 | augroup END |
| 2925 | close! |
| 2926 | endfunc |
| 2927 | |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2928 | " Test for the temporary internal window used to execute autocmds |
| 2929 | func Test_autocmd_window() |
| 2930 | %bw! |
| 2931 | edit one.txt |
| 2932 | tabnew two.txt |
Bram Moolenaar | 41cd803 | 2021-03-13 15:47:56 +0100 | [diff] [blame] | 2933 | vnew three.txt |
| 2934 | tabnew four.txt |
| 2935 | tabprevious |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2936 | let g:blist = [] |
Bram Moolenaar | 832adf9 | 2020-06-25 19:01:36 +0200 | [diff] [blame] | 2937 | augroup aucmd_win_test1 |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2938 | au! |
| 2939 | au BufEnter * call add(g:blist, [expand('<afile>'), |
| 2940 | \ win_gettype(bufwinnr(expand('<afile>')))]) |
| 2941 | augroup END |
| 2942 | |
| 2943 | doautoall BufEnter |
Bram Moolenaar | 41cd803 | 2021-03-13 15:47:56 +0100 | [diff] [blame] | 2944 | call assert_equal([ |
| 2945 | \ ['one.txt', 'autocmd'], |
| 2946 | \ ['two.txt', ''], |
| 2947 | \ ['four.txt', 'autocmd'], |
| 2948 | \ ['three.txt', ''], |
| 2949 | \ ], g:blist) |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2950 | |
Bram Moolenaar | 832adf9 | 2020-06-25 19:01:36 +0200 | [diff] [blame] | 2951 | augroup aucmd_win_test1 |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2952 | au! |
| 2953 | augroup END |
Bram Moolenaar | 832adf9 | 2020-06-25 19:01:36 +0200 | [diff] [blame] | 2954 | augroup! aucmd_win_test1 |
| 2955 | %bw! |
| 2956 | endfunc |
| 2957 | |
| 2958 | " Test for trying to close the temporary window used for executing an autocmd |
| 2959 | func Test_close_autocmd_window() |
| 2960 | %bw! |
| 2961 | edit one.txt |
| 2962 | tabnew two.txt |
| 2963 | augroup aucmd_win_test2 |
| 2964 | au! |
| 2965 | au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif |
| 2966 | augroup END |
| 2967 | |
| 2968 | call assert_fails('doautoall BufEnter', 'E813:') |
| 2969 | |
| 2970 | augroup aucmd_win_test2 |
| 2971 | au! |
| 2972 | augroup END |
| 2973 | augroup! aucmd_win_test2 |
Bram Moolenaar | cf84417 | 2020-06-26 19:44:06 +0200 | [diff] [blame] | 2974 | %bwipe! |
| 2975 | endfunc |
| 2976 | |
| 2977 | " Test for trying to close the tab that has the temporary window for exeucing |
| 2978 | " an autocmd. |
| 2979 | func Test_close_autocmd_tab() |
| 2980 | edit one.txt |
| 2981 | tabnew two.txt |
| 2982 | augroup aucmd_win_test |
| 2983 | au! |
| 2984 | au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif |
| 2985 | augroup END |
| 2986 | |
| 2987 | call assert_fails('doautoall BufEnter', 'E813:') |
| 2988 | |
| 2989 | tabonly |
| 2990 | augroup aucmd_win_test |
| 2991 | au! |
| 2992 | augroup END |
| 2993 | augroup! aucmd_win_test |
| 2994 | %bwipe! |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2995 | endfunc |
| 2996 | |
Bram Moolenaar | cb1956d | 2022-01-07 15:45:18 +0000 | [diff] [blame] | 2997 | func Test_Visual_doautoall_redraw() |
| 2998 | call setline(1, ['a', 'b']) |
| 2999 | new |
| 3000 | wincmd p |
| 3001 | call feedkeys("G\<C-V>", 'txn') |
| 3002 | autocmd User Explode ++once redraw |
| 3003 | doautoall User Explode |
| 3004 | %bwipe! |
| 3005 | endfunc |
| 3006 | |
Bram Moolenaar | 6bcb877 | 2021-02-03 21:23:29 +0100 | [diff] [blame] | 3007 | " This was using freed memory. |
| 3008 | func Test_BufNew_arglocal() |
| 3009 | arglocal |
| 3010 | au BufNew * arglocal |
| 3011 | call assert_fails('drop xx', 'E1156:') |
| 3012 | |
| 3013 | au! BufNew |
| 3014 | endfunc |
| 3015 | |
Bram Moolenaar | 8ab3757 | 2021-02-03 21:56:59 +0100 | [diff] [blame] | 3016 | func Test_autocmd_closes_window() |
| 3017 | au BufNew,BufWinLeave * e %e |
| 3018 | file yyy |
| 3019 | au BufNew,BufWinLeave * ball |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 3020 | n xxx |
Bram Moolenaar | 8ab3757 | 2021-02-03 21:56:59 +0100 | [diff] [blame] | 3021 | |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 3022 | %bwipe |
Bram Moolenaar | 8ab3757 | 2021-02-03 21:56:59 +0100 | [diff] [blame] | 3023 | au! BufNew |
| 3024 | au! BufWinLeave |
| 3025 | endfunc |
| 3026 | |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 3027 | func Test_autocmd_quit_psearch() |
| 3028 | sn aa bb |
| 3029 | augroup aucmd_win_test |
| 3030 | au! |
| 3031 | au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif |
| 3032 | augroup END |
| 3033 | ps / |
| 3034 | |
| 3035 | augroup aucmd_win_test |
| 3036 | au! |
| 3037 | augroup END |
zeertzjq | 7851c69 | 2022-04-21 11:14:01 +0100 | [diff] [blame] | 3038 | new |
| 3039 | pclose |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 3040 | endfunc |
| 3041 | |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 3042 | " Fuzzer found some strange combination that caused a crash. |
| 3043 | func Test_autocmd_normal_mess() |
Bram Moolenaar | dd07c02 | 2021-02-07 13:32:46 +0100 | [diff] [blame] | 3044 | " For unknown reason this hangs on MS-Windows |
| 3045 | CheckNotMSWindows |
| 3046 | |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 3047 | augroup aucmd_normal_test |
| 3048 | au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc |
| 3049 | augroup END |
Bram Moolenaar | 983d83f | 2021-02-07 12:12:43 +0100 | [diff] [blame] | 3050 | call assert_fails('o4', 'E1159') |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 3051 | silent! H |
Bram Moolenaar | 983d83f | 2021-02-07 12:12:43 +0100 | [diff] [blame] | 3052 | call assert_fails('e xx', 'E1159') |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 3053 | normal G |
| 3054 | |
| 3055 | augroup aucmd_normal_test |
| 3056 | au! |
| 3057 | augroup END |
| 3058 | endfunc |
| 3059 | |
Bram Moolenaar | 8c6951f | 2021-02-06 18:08:45 +0100 | [diff] [blame] | 3060 | func Test_autocmd_closing_cmdwin() |
Bram Moolenaar | dd07c02 | 2021-02-07 13:32:46 +0100 | [diff] [blame] | 3061 | " For unknown reason this hangs on MS-Windows |
| 3062 | CheckNotMSWindows |
| 3063 | |
Bram Moolenaar | 8c6951f | 2021-02-06 18:08:45 +0100 | [diff] [blame] | 3064 | au BufWinLeave * nested q |
| 3065 | call assert_fails("norm 7q?\n", 'E855:') |
| 3066 | |
| 3067 | au! BufWinLeave |
| 3068 | new |
| 3069 | only |
| 3070 | endfunc |
| 3071 | |
Bram Moolenaar | 2c7080b | 2021-02-06 19:19:42 +0100 | [diff] [blame] | 3072 | func Test_autocmd_vimgrep() |
| 3073 | augroup aucmd_vimgrep |
Charlie Groves | fef4485 | 2022-04-19 16:24:12 +0100 | [diff] [blame] | 3074 | au QuickfixCmdPre,BufNew,BufReadCmd * sb |
zeertzjq | 7851c69 | 2022-04-21 11:14:01 +0100 | [diff] [blame] | 3075 | au QuickfixCmdPre,BufNew,BufReadCmd * q9 |
Bram Moolenaar | 2c7080b | 2021-02-06 19:19:42 +0100 | [diff] [blame] | 3076 | augroup END |
Bram Moolenaar | dd07c02 | 2021-02-07 13:32:46 +0100 | [diff] [blame] | 3077 | call assert_fails('lv ?a? foo', 'E926:') |
Bram Moolenaar | 2c7080b | 2021-02-06 19:19:42 +0100 | [diff] [blame] | 3078 | |
| 3079 | augroup aucmd_vimgrep |
| 3080 | au! |
| 3081 | augroup END |
| 3082 | endfunc |
| 3083 | |
Bram Moolenaar | 73b8b0a | 2021-08-01 14:52:32 +0200 | [diff] [blame] | 3084 | func Test_autocmd_with_block() |
| 3085 | augroup block_testing |
| 3086 | au BufReadPost *.xml { |
| 3087 | setlocal matchpairs+=<:> |
| 3088 | /<start |
| 3089 | } |
Bram Moolenaar | 63b9173 | 2021-08-05 20:40:03 +0200 | [diff] [blame] | 3090 | au CursorHold * { |
| 3091 | autocmd BufReadPre * ++once echo 'one' | echo 'two' |
| 3092 | g:gotSafeState = 77 |
| 3093 | } |
Bram Moolenaar | 73b8b0a | 2021-08-01 14:52:32 +0200 | [diff] [blame] | 3094 | augroup END |
| 3095 | |
| 3096 | let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }" |
| 3097 | call assert_equal(expected, execute('au BufReadPost *.xml')) |
| 3098 | |
Bram Moolenaar | 63b9173 | 2021-08-05 20:40:03 +0200 | [diff] [blame] | 3099 | doautocmd CursorHold |
| 3100 | call assert_equal(77, g:gotSafeState) |
| 3101 | unlet g:gotSafeState |
| 3102 | |
Bram Moolenaar | 73b8b0a | 2021-08-01 14:52:32 +0200 | [diff] [blame] | 3103 | augroup block_testing |
| 3104 | au! |
| 3105 | augroup END |
| 3106 | endfunc |
| 3107 | |
Christian Brabandt | db3b446 | 2021-10-16 11:58:55 +0100 | [diff] [blame] | 3108 | " Test TextChangedI and TextChanged |
| 3109 | func Test_Changed_ChangedI() |
| 3110 | new |
| 3111 | call test_override("char_avail", 1) |
| 3112 | let [g:autocmd_i, g:autocmd_n] = ['',''] |
| 3113 | |
| 3114 | func! TextChangedAutocmdI(char) |
| 3115 | let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick |
| 3116 | endfunc |
| 3117 | |
| 3118 | augroup Test_TextChanged |
| 3119 | au! |
| 3120 | au TextChanged <buffer> :call TextChangedAutocmdI('N') |
| 3121 | au TextChangedI <buffer> :call TextChangedAutocmdI('I') |
| 3122 | augroup END |
| 3123 | |
| 3124 | call feedkeys("ifoo\<esc>", 'tnix') |
| 3125 | " TODO: Test test does not seem to trigger TextChanged autocommand, this |
| 3126 | " requires running Vim in a terminal window. |
| 3127 | " call assert_equal('N3', g:autocmd_n) |
| 3128 | call assert_equal('I3', g:autocmd_i) |
| 3129 | |
| 3130 | call feedkeys("yyp", 'tnix') |
| 3131 | " TODO: Test test does not seem to trigger TextChanged autocommand. |
| 3132 | " call assert_equal('N4', g:autocmd_n) |
| 3133 | call assert_equal('I3', g:autocmd_i) |
| 3134 | |
| 3135 | " CleanUp |
| 3136 | call test_override("char_avail", 0) |
| 3137 | au! TextChanged <buffer> |
| 3138 | au! TextChangedI <buffer> |
| 3139 | augroup! Test_TextChanged |
| 3140 | delfu TextChangedAutocmdI |
| 3141 | unlet! g:autocmd_i g:autocmd_n |
| 3142 | |
| 3143 | bw! |
| 3144 | endfunc |
Bram Moolenaar | 2c7080b | 2021-02-06 19:19:42 +0100 | [diff] [blame] | 3145 | |
Bram Moolenaar | 6f2465d | 2022-03-22 18:13:01 +0000 | [diff] [blame] | 3146 | func Test_closing_autocmd_window() |
| 3147 | let lines =<< trim END |
| 3148 | edit Xa.txt |
| 3149 | tabnew Xb.txt |
| 3150 | autocmd BufEnter Xa.txt unhide 1 |
| 3151 | doautoall BufEnter |
| 3152 | END |
| 3153 | call v9.CheckScriptFailure(lines, 'E814:') |
| 3154 | au! BufEnter |
| 3155 | only! |
| 3156 | bwipe Xa.txt |
| 3157 | bwipe Xb.txt |
| 3158 | endfunc |
| 3159 | |
Bram Moolenaar | 347538f | 2022-03-26 16:28:06 +0000 | [diff] [blame] | 3160 | func Test_bufwipeout_changes_window() |
| 3161 | " This should not crash, but we don't have any expectations about what |
| 3162 | " happens, changing window in BufWipeout has unpredictable results. |
| 3163 | tabedit |
| 3164 | let g:window_id = win_getid() |
| 3165 | topleft new |
| 3166 | setlocal bufhidden=wipe |
| 3167 | autocmd BufWipeout <buffer> call win_gotoid(g:window_id) |
| 3168 | tabprevious |
| 3169 | +tabclose |
| 3170 | |
| 3171 | unlet g:window_id |
| 3172 | au! BufWipeout |
| 3173 | %bwipe! |
| 3174 | endfunc |
| 3175 | |
zeertzjq | 021996f | 2022-04-10 11:44:04 +0100 | [diff] [blame] | 3176 | func Test_v_event_readonly() |
| 3177 | autocmd CompleteChanged * let v:event.width = 0 |
| 3178 | call assert_fails("normal! i\<C-X>\<C-V>", 'E46:') |
| 3179 | au! CompleteChanged |
| 3180 | |
| 3181 | autocmd DirChangedPre * let v:event.directory = '' |
| 3182 | call assert_fails('cd .', 'E46:') |
| 3183 | au! DirChangedPre |
| 3184 | |
| 3185 | autocmd ModeChanged * let v:event.new_mode = '' |
| 3186 | call assert_fails('normal! cc', 'E46:') |
| 3187 | au! ModeChanged |
| 3188 | |
| 3189 | autocmd TextYankPost * let v:event.operator = '' |
| 3190 | call assert_fails('normal! yy', 'E46:') |
| 3191 | au! TextYankPost |
| 3192 | endfunc |
| 3193 | |
Bram Moolenaar | 347538f | 2022-03-26 16:28:06 +0000 | [diff] [blame] | 3194 | |
Charlie Groves | fef4485 | 2022-04-19 16:24:12 +0100 | [diff] [blame] | 3195 | func Test_noname_autocmd() |
| 3196 | augroup test_noname_autocmd_group |
| 3197 | autocmd! |
| 3198 | autocmd BufEnter * call add(s:li, ["BufEnter", expand("<afile>")]) |
| 3199 | autocmd BufDelete * call add(s:li, ["BufDelete", expand("<afile>")]) |
| 3200 | autocmd BufLeave * call add(s:li, ["BufLeave", expand("<afile>")]) |
| 3201 | autocmd BufUnload * call add(s:li, ["BufUnload", expand("<afile>")]) |
| 3202 | autocmd BufWipeout * call add(s:li, ["BufWipeout", expand("<afile>")]) |
| 3203 | augroup END |
| 3204 | |
| 3205 | let s:li = [] |
| 3206 | edit foo |
| 3207 | call assert_equal([['BufUnload', ''], ['BufDelete', ''], ['BufWipeout', ''], ['BufEnter', 'foo']], s:li) |
| 3208 | |
| 3209 | au! test_noname_autocmd_group |
| 3210 | augroup! test_noname_autocmd_group |
| 3211 | endfunc |
| 3212 | |
Yegappan Lakshmanan | 1755a91 | 2022-05-19 10:31:47 +0100 | [diff] [blame] | 3213 | " Test for the autocmd_get() function |
| 3214 | func Test_autocmd_get() |
| 3215 | augroup TestAutoCmdFns |
| 3216 | au! |
| 3217 | autocmd BufAdd *.vim echo "bufadd-vim" |
| 3218 | autocmd BufAdd *.py echo "bufadd-py" |
| 3219 | autocmd BufHidden *.vim echo "bufhidden" |
| 3220 | augroup END |
| 3221 | augroup TestAutoCmdFns2 |
| 3222 | autocmd BufAdd *.vim echo "bufadd-vim-2" |
| 3223 | autocmd BufRead *.a1b2c3 echo "bufadd-vim-2" |
| 3224 | augroup END |
| 3225 | |
| 3226 | let l = autocmd_get() |
| 3227 | call assert_true(l->len() > 0) |
| 3228 | |
| 3229 | " Test for getting all the autocmds in a group |
| 3230 | let expected = [ |
| 3231 | \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns', |
| 3232 | \ pattern: '*.vim', nested: v:false, once: v:false, |
| 3233 | \ event: 'BufAdd'}, |
| 3234 | \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns', |
| 3235 | \ pattern: '*.py', nested: v:false, once: v:false, |
| 3236 | \ event: 'BufAdd'}, |
| 3237 | \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns', |
| 3238 | \ pattern: '*.vim', nested: v:false, |
| 3239 | \ once: v:false, event: 'BufHidden'}] |
| 3240 | call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'})) |
| 3241 | |
| 3242 | " Test for getting autocmds for all the patterns in a group |
| 3243 | call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns', |
| 3244 | \ event: '*'})) |
| 3245 | |
| 3246 | " Test for getting autocmds for an event in a group |
| 3247 | let expected = [ |
| 3248 | \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns', |
| 3249 | \ pattern: '*.vim', nested: v:false, once: v:false, |
| 3250 | \ event: 'BufAdd'}, |
| 3251 | \ #{cmd: 'echo "bufadd-py"', group: 'TestAutoCmdFns', |
| 3252 | \ pattern: '*.py', nested: v:false, once: v:false, |
| 3253 | \ event: 'BufAdd'}] |
| 3254 | call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns', |
| 3255 | \ event: 'BufAdd'})) |
| 3256 | |
| 3257 | " Test for getting the autocmds for all the events in a group for particular |
| 3258 | " pattern |
| 3259 | call assert_equal([{'cmd': 'echo "bufadd-py"', 'group': 'TestAutoCmdFns', |
| 3260 | \ 'pattern': '*.py', 'nested': v:false, 'once': v:false, |
| 3261 | \ 'event': 'BufAdd'}], |
| 3262 | \ autocmd_get(#{group: 'TestAutoCmdFns', event: '*', pattern: '*.py'})) |
| 3263 | |
| 3264 | " Test for getting the autocmds for an events in a group for particular |
| 3265 | " pattern |
| 3266 | let l = autocmd_get(#{group: 'TestAutoCmdFns', event: 'BufAdd', |
| 3267 | \ pattern: '*.vim'}) |
| 3268 | call assert_equal([ |
| 3269 | \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns', |
| 3270 | \ pattern: '*.vim', nested: v:false, once: v:false, |
| 3271 | \ event: 'BufAdd'}], l) |
| 3272 | |
| 3273 | " Test for getting the autocmds for a pattern in a group |
| 3274 | let l = autocmd_get(#{group: 'TestAutoCmdFns', pattern: '*.vim'}) |
| 3275 | call assert_equal([ |
| 3276 | \ #{cmd: 'echo "bufadd-vim"', group: 'TestAutoCmdFns', |
| 3277 | \ pattern: '*.vim', nested: v:false, once: v:false, |
| 3278 | \ event: 'BufAdd'}, |
| 3279 | \ #{cmd: 'echo "bufhidden"', group: 'TestAutoCmdFns', |
| 3280 | \ pattern: '*.vim', nested: v:false, |
| 3281 | \ once: v:false, event: 'BufHidden'}], l) |
| 3282 | |
| 3283 | " Test for getting the autocmds for a pattern in all the groups |
| 3284 | let l = autocmd_get(#{pattern: '*.a1b2c3'}) |
| 3285 | call assert_equal([{'cmd': 'echo "bufadd-vim-2"', 'group': 'TestAutoCmdFns2', |
| 3286 | \ 'pattern': '*.a1b2c3', 'nested': v:false, 'once': v:false, |
| 3287 | \ 'event': 'BufRead'}], l) |
| 3288 | |
| 3289 | " Test for getting autocmds for a pattern without any autocmds |
| 3290 | call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns', |
| 3291 | \ pattern: '*.abc'})) |
| 3292 | call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns', |
| 3293 | \ event: 'BufAdd', pattern: '*.abc'})) |
| 3294 | call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns', |
| 3295 | \ event: 'BufWipeout'})) |
| 3296 | call assert_fails("call autocmd_get(#{group: 'abc', event: 'BufAdd'})", |
| 3297 | \ 'E367:') |
| 3298 | let cmd = "echo autocmd_get(#{group: 'TestAutoCmdFns', event: 'abc'})" |
| 3299 | call assert_fails(cmd, 'E216:') |
| 3300 | call assert_fails("call autocmd_get(#{group: 'abc'})", 'E367:') |
| 3301 | call assert_fails("echo autocmd_get(#{event: 'abc'})", 'E216:') |
| 3302 | |
| 3303 | augroup TestAutoCmdFns |
| 3304 | au! |
| 3305 | augroup END |
| 3306 | call assert_equal([], autocmd_get(#{group: 'TestAutoCmdFns'})) |
| 3307 | |
| 3308 | " Test for nested and once autocmds |
| 3309 | augroup TestAutoCmdFns |
| 3310 | au! |
| 3311 | autocmd VimSuspend * ++nested echo "suspend" |
| 3312 | autocmd VimResume * ++once echo "resume" |
| 3313 | augroup END |
| 3314 | |
| 3315 | let expected = [ |
| 3316 | \ {'cmd': 'echo "suspend"', 'group': 'TestAutoCmdFns', 'pattern': '*', |
| 3317 | \ 'nested': v:true, 'once': v:false, 'event': 'VimSuspend'}, |
| 3318 | \ {'cmd': 'echo "resume"', 'group': 'TestAutoCmdFns', 'pattern': '*', |
| 3319 | \ 'nested': v:false, 'once': v:true, 'event': 'VimResume'}] |
| 3320 | call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'})) |
| 3321 | |
| 3322 | " Test for buffer-local autocmd |
| 3323 | augroup TestAutoCmdFns |
| 3324 | au! |
| 3325 | autocmd TextYankPost <buffer> echo "textyankpost" |
| 3326 | augroup END |
| 3327 | |
| 3328 | let expected = [ |
| 3329 | \ {'cmd': 'echo "textyankpost"', 'group': 'TestAutoCmdFns', |
| 3330 | \ 'pattern': '<buffer=' .. bufnr() .. '>', 'nested': v:false, |
| 3331 | \ 'once': v:false, 'bufnr': bufnr(), 'event': 'TextYankPost'}] |
| 3332 | call assert_equal(expected, autocmd_get(#{group: 'TestAutoCmdFns'})) |
| 3333 | |
| 3334 | augroup TestAutoCmdFns |
| 3335 | au! |
| 3336 | augroup END |
| 3337 | augroup! TestAutoCmdFns |
| 3338 | augroup TestAutoCmdFns2 |
| 3339 | au! |
| 3340 | augroup END |
| 3341 | augroup! TestAutoCmdFns2 |
| 3342 | |
| 3343 | call assert_fails("echo autocmd_get(#{group: []})", 'E730:') |
| 3344 | call assert_fails("echo autocmd_get(#{event: {}})", 'E731:') |
| 3345 | call assert_fails("echo autocmd_get([])", 'E1206:') |
| 3346 | endfunc |
| 3347 | |
| 3348 | " Test for the autocmd_add() function |
| 3349 | func Test_autocmd_add() |
| 3350 | " Define a single autocmd in a group |
| 3351 | call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh', |
| 3352 | \ cmd: 'echo "bufadd"', once: v:true, nested: v:true}]) |
| 3353 | call assert_equal([#{cmd: 'echo "bufadd"', group: 'TestAcSet', |
| 3354 | \ pattern: '*.sh', nested: v:true, once: v:true, |
| 3355 | \ event: 'BufAdd'}], autocmd_get(#{group: 'TestAcSet'})) |
| 3356 | |
| 3357 | " Define two autocmds in the same group |
| 3358 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3359 | call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pattern: '*.sh', |
| 3360 | \ cmd: 'echo "bufadd"'}, |
| 3361 | \ #{group: 'TestAcSet', event: 'BufEnter', pattern: '*.sh', |
| 3362 | \ cmd: 'echo "bufenter"'}]) |
| 3363 | call assert_equal([ |
| 3364 | \ #{cmd: 'echo "bufadd"', group: 'TestAcSet', pattern: '*.sh', |
| 3365 | \ nested: v:false, once: v:false, event: 'BufAdd'}, |
| 3366 | \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.sh', |
| 3367 | \ nested: v:false, once: v:false, event: 'BufEnter'}], |
| 3368 | \ autocmd_get(#{group: 'TestAcSet'})) |
| 3369 | |
| 3370 | " Define a buffer-local autocmd |
| 3371 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3372 | call autocmd_add([#{group: 'TestAcSet', event: 'CursorHold', |
| 3373 | \ bufnr: bufnr(), cmd: 'echo "cursorhold"'}]) |
| 3374 | call assert_equal([ |
| 3375 | \ #{cmd: 'echo "cursorhold"', group: 'TestAcSet', |
| 3376 | \ pattern: '<buffer=' .. bufnr() .. '>', nested: v:false, |
| 3377 | \ once: v:false, bufnr: bufnr(), event: 'CursorHold'}], |
| 3378 | \ autocmd_get(#{group: 'TestAcSet'})) |
| 3379 | |
| 3380 | " Use an invalid buffer number |
| 3381 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3382 | call autocmd_add([#{group: 'TestAcSet', event: 'BufEnter', |
| 3383 | \ bufnr: -1, cmd: 'echo "bufenter"'}]) |
| 3384 | let l = [#{group: 'TestAcSet', event: 'BufAdd', bufnr: 9999, |
| 3385 | \ cmd: 'echo "bufadd"'}] |
| 3386 | call assert_fails("echo autocmd_add(l)", 'E680:') |
| 3387 | let l = [#{group: 'TestAcSet', event: 'BufRead', bufnr: [], |
| 3388 | \ cmd: 'echo "bufread"'}] |
| 3389 | call assert_fails("echo autocmd_add(l)", 'E745:') |
| 3390 | call assert_equal([], autocmd_get(#{group: 'TestAcSet'})) |
| 3391 | |
| 3392 | " Add two commands to the same group, event and pattern |
| 3393 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3394 | call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload', |
| 3395 | \ pattern: 'abc', cmd: 'echo "cmd1"'}]) |
| 3396 | call autocmd_add([#{group: 'TestAcSet', event: 'BufUnload', |
| 3397 | \ pattern: 'abc', cmd: 'echo "cmd2"'}]) |
| 3398 | call assert_equal([ |
| 3399 | \ #{cmd: 'echo "cmd1"', group: 'TestAcSet', pattern: 'abc', |
| 3400 | \ nested: v:false, once: v:false, event: 'BufUnload'}, |
| 3401 | \ #{cmd: 'echo "cmd2"', group: 'TestAcSet', pattern: 'abc', |
| 3402 | \ nested: v:false, once: v:false, event: 'BufUnload'}], |
| 3403 | \ autocmd_get(#{group: 'TestAcSet'})) |
| 3404 | |
| 3405 | " When adding a new autocmd, if the autocmd 'group' is not specified, then |
| 3406 | " the current autocmd group should be used. |
| 3407 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3408 | augroup TestAcSet |
| 3409 | call autocmd_add([#{event: 'BufHidden', pattern: 'abc', cmd: 'echo "abc"'}]) |
| 3410 | augroup END |
| 3411 | call assert_equal([ |
| 3412 | \ #{cmd: 'echo "abc"', group: 'TestAcSet', pattern: 'abc', |
| 3413 | \ nested: v:false, once: v:false, event: 'BufHidden'}], |
| 3414 | \ autocmd_get(#{group: 'TestAcSet'})) |
| 3415 | |
Yegappan Lakshmanan | 971f682 | 2022-05-24 11:40:11 +0100 | [diff] [blame] | 3416 | " Test for replacing a cmd for an event in a group |
| 3417 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3418 | call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter', |
| 3419 | \ pattern: '*.py', cmd: 'echo "bufenter"'}]) |
| 3420 | call autocmd_add([#{replace: v:true, group: 'TestAcSet', event: 'BufEnter', |
| 3421 | \ pattern: '*.py', cmd: 'echo "bufenter"'}]) |
| 3422 | call assert_equal([ |
| 3423 | \ #{cmd: 'echo "bufenter"', group: 'TestAcSet', pattern: '*.py', |
| 3424 | \ nested: v:false, once: v:false, event: 'BufEnter'}], |
| 3425 | \ autocmd_get(#{group: 'TestAcSet'})) |
| 3426 | |
| 3427 | " Test for adding a command for an unsupported autocmd event |
Yegappan Lakshmanan | 1755a91 | 2022-05-19 10:31:47 +0100 | [diff] [blame] | 3428 | let l = [#{group: 'TestAcSet', event: 'abc', pattern: '*.sh', |
| 3429 | \ cmd: 'echo "bufadd"'}] |
| 3430 | call assert_fails('call autocmd_add(l)', 'E216:') |
| 3431 | |
Yegappan Lakshmanan | e0ff3a7 | 2022-05-27 18:05:33 +0100 | [diff] [blame] | 3432 | " Test for using a list of events and patterns |
| 3433 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3434 | let l = [#{group: 'TestAcSet', event: ['BufEnter', 'BufLeave'], |
| 3435 | \ pattern: ['*.py', '*.sh'], cmd: 'echo "bufcmds"'}] |
| 3436 | call autocmd_add(l) |
| 3437 | call assert_equal([ |
| 3438 | \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py', |
| 3439 | \ nested: v:false, once: v:false, event: 'BufEnter'}, |
| 3440 | \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh', |
| 3441 | \ nested: v:false, once: v:false, event: 'BufEnter'}, |
| 3442 | \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.py', |
| 3443 | \ nested: v:false, once: v:false, event: 'BufLeave'}, |
| 3444 | \ #{cmd: 'echo "bufcmds"', group: 'TestAcSet', pattern: '*.sh', |
| 3445 | \ nested: v:false, once: v:false, event: 'BufLeave'}], |
| 3446 | \ autocmd_get(#{group: 'TestAcSet'})) |
| 3447 | |
| 3448 | " Test for invalid values for 'event' item |
| 3449 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3450 | let l = [#{group: 'TestAcSet', event: test_null_string(), |
| 3451 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3452 | call assert_fails('call autocmd_add(l)', 'E928:') |
| 3453 | let l = [#{group: 'TestAcSet', event: test_null_list(), |
| 3454 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3455 | call assert_fails('call autocmd_add(l)', 'E714:') |
| 3456 | let l = [#{group: 'TestAcSet', event: {}, |
| 3457 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3458 | call assert_fails('call autocmd_add(l)', 'E777:') |
| 3459 | let l = [#{group: 'TestAcSet', event: [{}], |
| 3460 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3461 | call assert_fails('call autocmd_add(l)', 'E928:') |
| 3462 | let l = [#{group: 'TestAcSet', event: [test_null_string()], |
| 3463 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3464 | call assert_fails('call autocmd_add(l)', 'E928:') |
| 3465 | let l = [#{group: 'TestAcSet', event: 'BufEnter,BufLeave', |
| 3466 | \ pattern: '*.py', cmd: 'echo "bufcmds"'}] |
| 3467 | call assert_fails('call autocmd_add(l)', 'E216:') |
| 3468 | let l = [#{group: 'TestAcSet', event: [], |
| 3469 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3470 | call autocmd_add(l) |
| 3471 | let l = [#{group: 'TestAcSet', event: [""], |
| 3472 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3473 | call assert_fails('call autocmd_add(l)', 'E216:') |
| 3474 | let l = [#{group: 'TestAcSet', event: "", |
| 3475 | \ pattern: "*.py", cmd: 'echo "bufcmds"'}] |
| 3476 | call autocmd_add(l) |
| 3477 | call assert_equal([], autocmd_get(#{group: 'TestAcSet'})) |
| 3478 | |
| 3479 | " Test for invalid values for 'pattern' item |
| 3480 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3481 | \ pattern: test_null_string(), cmd: 'echo "bufcmds"'}] |
| 3482 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3483 | \ pattern: test_null_list(), cmd: 'echo "bufcmds"'}] |
| 3484 | call assert_fails('call autocmd_add(l)', 'E714:') |
| 3485 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3486 | \ pattern: {}, cmd: 'echo "bufcmds"'}] |
| 3487 | call assert_fails('call autocmd_add(l)', 'E777:') |
| 3488 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3489 | \ pattern: [{}], cmd: 'echo "bufcmds"'}] |
| 3490 | call assert_fails('call autocmd_add(l)', 'E928:') |
| 3491 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3492 | \ pattern: [test_null_string()], cmd: 'echo "bufcmds"'}] |
| 3493 | call assert_fails('call autocmd_add(l)', 'E928:') |
| 3494 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3495 | \ pattern: [], cmd: 'echo "bufcmds"'}] |
| 3496 | call autocmd_add(l) |
| 3497 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3498 | \ pattern: [""], cmd: 'echo "bufcmds"'}] |
| 3499 | call autocmd_add(l) |
| 3500 | let l = [#{group: 'TestAcSet', event: "BufEnter", |
| 3501 | \ pattern: "", cmd: 'echo "bufcmds"'}] |
| 3502 | call autocmd_add(l) |
| 3503 | call assert_equal([], autocmd_get(#{group: 'TestAcSet'})) |
| 3504 | |
| 3505 | let l = [#{group: 'TestAcSet', event: 'BufEnter,abc,BufLeave', |
| 3506 | \ pattern: '*.py', cmd: 'echo "bufcmds"'}] |
| 3507 | call assert_fails('call autocmd_add(l)', 'E216:') |
| 3508 | |
Yegappan Lakshmanan | 1755a91 | 2022-05-19 10:31:47 +0100 | [diff] [blame] | 3509 | call assert_fails("call autocmd_add({})", 'E1211:') |
| 3510 | call assert_equal(v:false, autocmd_add(test_null_list())) |
| 3511 | call assert_true(autocmd_add([[]])) |
| 3512 | call assert_true(autocmd_add([test_null_dict()])) |
| 3513 | |
| 3514 | augroup TestAcSet |
| 3515 | au! |
| 3516 | augroup END |
| 3517 | |
| 3518 | call autocmd_add([#{group: 'TestAcSet'}]) |
| 3519 | call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd'}]) |
| 3520 | call autocmd_add([#{group: 'TestAcSet', pat: '*.sh'}]) |
| 3521 | call autocmd_add([#{group: 'TestAcSet', cmd: 'echo "a"'}]) |
| 3522 | call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', pat: '*.sh'}]) |
| 3523 | call autocmd_add([#{group: 'TestAcSet', event: 'BufAdd', cmd: 'echo "a"'}]) |
| 3524 | call autocmd_add([#{group: 'TestAcSet', pat: '*.sh', cmd: 'echo "a"'}]) |
| 3525 | call assert_equal([], autocmd_get(#{group: 'TestAcSet'})) |
| 3526 | |
| 3527 | augroup! TestAcSet |
| 3528 | endfunc |
| 3529 | |
| 3530 | " Test for deleting autocmd events and groups |
| 3531 | func Test_autocmd_delete() |
| 3532 | " Delete an event in an autocmd group |
| 3533 | augroup TestAcSet |
| 3534 | au! |
| 3535 | au BufAdd *.sh echo "bufadd" |
| 3536 | au BufEnter *.sh echo "bufenter" |
| 3537 | augroup END |
| 3538 | call autocmd_delete([#{group: 'TestAcSet', event: 'BufAdd'}]) |
| 3539 | call assert_equal([#{cmd: 'echo "bufenter"', group: 'TestAcSet', |
| 3540 | \ pattern: '*.sh', nested: v:false, once: v:false, |
| 3541 | \ event: 'BufEnter'}], autocmd_get(#{group: 'TestAcSet'})) |
| 3542 | |
| 3543 | " Delete all the events in an autocmd group |
| 3544 | augroup TestAcSet |
| 3545 | au BufAdd *.sh echo "bufadd" |
| 3546 | augroup END |
| 3547 | call autocmd_delete([#{group: 'TestAcSet', event: '*'}]) |
| 3548 | call assert_equal([], autocmd_get(#{group: 'TestAcSet'})) |
| 3549 | |
| 3550 | " Delete a non-existing autocmd group |
| 3551 | call assert_fails("call autocmd_delete([#{group: 'abc'}])", 'E367:') |
| 3552 | " Delete a non-existing autocmd event |
| 3553 | let l = [#{group: 'TestAcSet', event: 'abc'}] |
| 3554 | call assert_fails("call autocmd_delete(l)", 'E216:') |
| 3555 | " Delete a non-existing autocmd pattern |
| 3556 | let l = [#{group: 'TestAcSet', event: 'BufAdd', pat: 'abc'}] |
| 3557 | call assert_true(autocmd_delete(l)) |
| 3558 | |
| 3559 | " Delete an autocmd group |
| 3560 | augroup TestAcSet |
| 3561 | au! |
| 3562 | au BufAdd *.sh echo "bufadd" |
| 3563 | au BufEnter *.sh echo "bufenter" |
| 3564 | augroup END |
| 3565 | call autocmd_delete([#{group: 'TestAcSet'}]) |
| 3566 | call assert_fails("call autocmd_get(#{group: 'TestAcSet'})", 'E367:') |
| 3567 | |
| 3568 | call assert_true(autocmd_delete([[]])) |
| 3569 | call assert_true(autocmd_delete([test_null_dict()])) |
| 3570 | endfunc |
| 3571 | |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 3572 | " vim: shiftwidth=2 sts=2 expandtab |