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