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