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