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