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