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