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