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 |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 6 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 7 | func s:cleanup_buffers() abort |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 8 | for bnr in range(1, bufnr('$')) |
| 9 | if bufloaded(bnr) && bufnr('%') != bnr |
| 10 | execute 'bd! ' . bnr |
| 11 | endif |
| 12 | endfor |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 13 | endfunc |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 14 | |
Bram Moolenaar | 1473551 | 2016-03-26 21:00:08 +0100 | [diff] [blame] | 15 | func Test_vim_did_enter() |
| 16 | call assert_false(v:vim_did_enter) |
| 17 | |
| 18 | " This script will never reach the main loop, can't check if v:vim_did_enter |
| 19 | " becomes one. |
| 20 | endfunc |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 21 | |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 22 | " Test for the CursorHold autocmd |
| 23 | func Test_CursorHold_autocmd() |
| 24 | CheckRunVimInTerminal |
| 25 | call writefile(['one', 'two', 'three'], 'Xfile') |
| 26 | let before =<< trim END |
| 27 | set updatetime=10 |
| 28 | au CursorHold * call writefile([line('.')], 'Xoutput', 'a') |
| 29 | END |
| 30 | call writefile(before, 'Xinit') |
| 31 | let buf = RunVimInTerminal('-S Xinit Xfile', {}) |
Bram Moolenaar | 17f6754 | 2020-08-20 18:29:13 +0200 | [diff] [blame] | 32 | call term_sendkeys(buf, "G") |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 33 | call term_wait(buf, 50) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 34 | call term_sendkeys(buf, "gg") |
| 35 | call term_wait(buf) |
Bram Moolenaar | 17f6754 | 2020-08-20 18:29:13 +0200 | [diff] [blame] | 36 | call WaitForAssert({-> assert_equal(['1'], readfile('Xoutput')[-1:-1])}) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 37 | call term_sendkeys(buf, "j") |
| 38 | call term_wait(buf) |
Bram Moolenaar | 17f6754 | 2020-08-20 18:29:13 +0200 | [diff] [blame] | 39 | call WaitForAssert({-> assert_equal(['1', '2'], readfile('Xoutput')[-2:-1])}) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 40 | call term_sendkeys(buf, "j") |
| 41 | call term_wait(buf) |
Bram Moolenaar | 17f6754 | 2020-08-20 18:29:13 +0200 | [diff] [blame] | 42 | call WaitForAssert({-> assert_equal(['1', '2', '3'], readfile('Xoutput')[-3:-1])}) |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 43 | call StopVimInTerminal(buf) |
| 44 | |
Bram Moolenaar | 7591116 | 2020-07-21 19:44:47 +0200 | [diff] [blame] | 45 | call delete('Xinit') |
| 46 | call delete('Xoutput') |
| 47 | call delete('Xfile') |
| 48 | endfunc |
| 49 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 50 | if has('timers') |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 51 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 52 | func ExitInsertMode(id) |
| 53 | call feedkeys("\<Esc>") |
| 54 | endfunc |
| 55 | |
| 56 | func Test_cursorhold_insert() |
Bram Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 57 | " Need to move the cursor. |
| 58 | call feedkeys("ggG", "xt") |
| 59 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 60 | let g:triggered = 0 |
| 61 | au CursorHoldI * let g:triggered += 1 |
| 62 | set updatetime=20 |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 63 | call timer_start(200, 'ExitInsertMode') |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 64 | call feedkeys('a', 'x!') |
| 65 | call assert_equal(1, g:triggered) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 66 | unlet g:triggered |
| 67 | au! CursorHoldI |
| 68 | set updatetime& |
| 69 | endfunc |
| 70 | |
| 71 | func Test_cursorhold_insert_with_timer_interrupt() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 72 | CheckFeature job |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 73 | " Need to move the cursor. |
| 74 | call feedkeys("ggG", "xt") |
| 75 | |
| 76 | " Confirm the timer invoked in exit_cb of the job doesn't disturb |
| 77 | " CursorHoldI event. |
| 78 | let g:triggered = 0 |
| 79 | au CursorHoldI * let g:triggered += 1 |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 80 | set updatetime=100 |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 81 | call job_start(has('win32') ? 'cmd /c echo:' : 'echo', |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 82 | \ {'exit_cb': {-> timer_start(200, 'ExitInsertMode')}}) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 83 | call feedkeys('a', 'x!') |
| 84 | call assert_equal(1, g:triggered) |
| 85 | unlet g:triggered |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 86 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 87 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 88 | endfunc |
| 89 | |
| 90 | func Test_cursorhold_insert_ctrl_x() |
| 91 | let g:triggered = 0 |
| 92 | au CursorHoldI * let g:triggered += 1 |
| 93 | set updatetime=20 |
| 94 | call timer_start(100, 'ExitInsertMode') |
| 95 | " CursorHoldI does not trigger after CTRL-X |
| 96 | call feedkeys("a\<C-X>", 'x!') |
| 97 | call assert_equal(0, g:triggered) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 98 | unlet g:triggered |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 99 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 100 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 101 | endfunc |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 102 | |
Bram Moolenaar | 5a9357d | 2021-10-03 16:22:05 +0100 | [diff] [blame] | 103 | func Test_cursorhold_insert_ctrl_g_U() |
| 104 | au CursorHoldI * : |
| 105 | set updatetime=20 |
| 106 | new |
| 107 | call timer_start(100, { -> feedkeys("\<Left>foo\<Esc>", 't') }) |
| 108 | call feedkeys("i()\<C-g>U", 'tx!') |
| 109 | sleep 200m |
| 110 | call assert_equal('(foo)', getline(1)) |
| 111 | undo |
| 112 | call assert_equal('', getline(1)) |
| 113 | |
| 114 | bwipe! |
| 115 | au! CursorHoldI |
| 116 | set updatetime& |
| 117 | endfunc |
| 118 | |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 119 | func Test_OptionSet_modeline() |
| 120 | call test_override('starting', 1) |
| 121 | au! OptionSet |
| 122 | augroup set_tabstop |
| 123 | au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")}) |
| 124 | augroup END |
| 125 | call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline') |
| 126 | set modeline |
| 127 | let v:errmsg = '' |
| 128 | call assert_fails('split XoptionsetModeline', 'E12:') |
| 129 | call assert_equal(7, &ts) |
| 130 | call assert_equal('', v:errmsg) |
| 131 | |
| 132 | augroup set_tabstop |
| 133 | au! |
| 134 | augroup END |
| 135 | bwipe! |
| 136 | set ts& |
| 137 | call delete('XoptionsetModeline') |
| 138 | call test_override('starting', 0) |
| 139 | endfunc |
| 140 | |
| 141 | endif "has('timers') |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 142 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 143 | func Test_bufunload() |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 144 | augroup test_bufunload_group |
| 145 | autocmd! |
| 146 | autocmd BufUnload * call add(s:li, "bufunload") |
| 147 | autocmd BufDelete * call add(s:li, "bufdelete") |
| 148 | autocmd BufWipeout * call add(s:li, "bufwipeout") |
| 149 | augroup END |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 150 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 151 | let s:li = [] |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 152 | new |
| 153 | setlocal bufhidden= |
| 154 | bunload |
| 155 | call assert_equal(["bufunload", "bufdelete"], s:li) |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 156 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 157 | let s:li = [] |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 158 | new |
| 159 | setlocal bufhidden=delete |
| 160 | bunload |
| 161 | call assert_equal(["bufunload", "bufdelete"], s:li) |
| 162 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 163 | let s:li = [] |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 164 | new |
| 165 | setlocal bufhidden=unload |
| 166 | bwipeout |
| 167 | call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li) |
| 168 | |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 169 | au! test_bufunload_group |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 170 | augroup! test_bufunload_group |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 171 | endfunc |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 172 | |
| 173 | " SEGV occurs in older versions. (At least 7.4.2005 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 174 | func Test_autocmd_bufunload_with_tabnext() |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 175 | tabedit |
| 176 | tabfirst |
| 177 | |
| 178 | augroup test_autocmd_bufunload_with_tabnext_group |
| 179 | autocmd! |
| 180 | autocmd BufUnload <buffer> tabnext |
| 181 | augroup END |
| 182 | |
| 183 | quit |
| 184 | call assert_equal(2, tabpagenr('$')) |
| 185 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 186 | autocmd! test_autocmd_bufunload_with_tabnext_group |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 187 | augroup! test_autocmd_bufunload_with_tabnext_group |
| 188 | tablast |
| 189 | quit |
| 190 | endfunc |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 191 | |
Bram Moolenaar | 5ed58c7 | 2021-01-28 14:24:55 +0100 | [diff] [blame] | 192 | func Test_argdelete_in_next() |
| 193 | au BufNew,BufEnter,BufLeave,BufWinEnter * argdel |
| 194 | call assert_fails('next a b', 'E1156:') |
| 195 | au! BufNew,BufEnter,BufLeave,BufWinEnter * |
| 196 | endfunc |
| 197 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 198 | func Test_autocmd_bufwinleave_with_tabfirst() |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 199 | tabedit |
| 200 | augroup sample |
| 201 | autocmd! |
| 202 | autocmd BufWinLeave <buffer> tabfirst |
| 203 | augroup END |
| 204 | call setline(1, ['a', 'b', 'c']) |
| 205 | edit! a.txt |
Bram Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 206 | tabclose |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 207 | endfunc |
| 208 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 209 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 210 | func Test_autocmd_bufunload_avoiding_SEGV_01() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 211 | split aa.txt |
| 212 | let lastbuf = bufnr('$') |
| 213 | |
| 214 | augroup test_autocmd_bufunload |
| 215 | autocmd! |
| 216 | exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!' |
| 217 | augroup END |
| 218 | |
Bram Moolenaar | 28ee892 | 2020-10-28 20:20:00 +0100 | [diff] [blame] | 219 | call assert_fails('edit bb.txt', 'E937:') |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 220 | |
| 221 | autocmd! test_autocmd_bufunload |
| 222 | augroup! test_autocmd_bufunload |
| 223 | bwipe! aa.txt |
| 224 | bwipe! bb.txt |
| 225 | endfunc |
| 226 | |
| 227 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 228 | func Test_autocmd_bufunload_avoiding_SEGV_02() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 229 | setlocal buftype=nowrite |
| 230 | let lastbuf = bufnr('$') |
| 231 | |
| 232 | augroup test_autocmd_bufunload |
| 233 | autocmd! |
| 234 | exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!' |
| 235 | augroup END |
| 236 | |
| 237 | normal! i1 |
| 238 | call assert_fails('edit a.txt', 'E517:') |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 239 | |
| 240 | autocmd! test_autocmd_bufunload |
| 241 | augroup! test_autocmd_bufunload |
| 242 | bwipe! a.txt |
| 243 | endfunc |
| 244 | |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 245 | func Test_autocmd_dummy_wipeout() |
| 246 | " prepare files |
| 247 | call writefile([''], 'Xdummywipetest1.txt') |
| 248 | call writefile([''], 'Xdummywipetest2.txt') |
| 249 | augroup test_bufunload_group |
| 250 | autocmd! |
| 251 | autocmd BufUnload * call add(s:li, "bufunload") |
| 252 | autocmd BufDelete * call add(s:li, "bufdelete") |
| 253 | autocmd BufWipeout * call add(s:li, "bufwipeout") |
| 254 | augroup END |
| 255 | |
| 256 | let s:li = [] |
| 257 | split Xdummywipetest1.txt |
| 258 | silent! vimgrep /notmatched/ Xdummywipetest* |
| 259 | call assert_equal(["bufunload", "bufwipeout"], s:li) |
| 260 | |
| 261 | bwipeout |
| 262 | call delete('Xdummywipetest1.txt') |
| 263 | call delete('Xdummywipetest2.txt') |
| 264 | au! test_bufunload_group |
| 265 | augroup! test_bufunload_group |
| 266 | endfunc |
| 267 | |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 268 | func Test_win_tab_autocmd() |
| 269 | let g:record = [] |
| 270 | |
| 271 | augroup testing |
| 272 | au WinNew * call add(g:record, 'WinNew') |
naohiro ono | 23beefe | 2021-11-13 12:38:49 +0000 | [diff] [blame] | 273 | au WinClosed * call add(g:record, 'WinClosed') |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 274 | au WinEnter * call add(g:record, 'WinEnter') |
| 275 | au WinLeave * call add(g:record, 'WinLeave') |
| 276 | au TabNew * call add(g:record, 'TabNew') |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 277 | au TabClosed * call add(g:record, 'TabClosed') |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 278 | au TabEnter * call add(g:record, 'TabEnter') |
| 279 | au TabLeave * call add(g:record, 'TabLeave') |
| 280 | augroup END |
| 281 | |
| 282 | split |
| 283 | tabnew |
| 284 | close |
| 285 | close |
| 286 | |
| 287 | call assert_equal([ |
| 288 | \ 'WinLeave', 'WinNew', 'WinEnter', |
| 289 | \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', |
naohiro ono | 23beefe | 2021-11-13 12:38:49 +0000 | [diff] [blame] | 290 | \ 'WinLeave', 'TabLeave', 'WinClosed', 'TabClosed', 'WinEnter', 'TabEnter', |
| 291 | \ 'WinLeave', 'WinClosed', 'WinEnter' |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 292 | \ ], g:record) |
| 293 | |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 294 | let g:record = [] |
| 295 | tabnew somefile |
| 296 | tabnext |
| 297 | bwipe somefile |
| 298 | |
| 299 | call assert_equal([ |
| 300 | \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', |
| 301 | \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', |
naohiro ono | 23beefe | 2021-11-13 12:38:49 +0000 | [diff] [blame] | 302 | \ 'WinClosed', 'TabClosed' |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 303 | \ ], g:record) |
| 304 | |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 305 | augroup testing |
| 306 | au! |
| 307 | augroup END |
| 308 | unlet g:record |
| 309 | endfunc |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 310 | |
naohiro ono | 23beefe | 2021-11-13 12:38:49 +0000 | [diff] [blame] | 311 | func Test_WinClosed() |
| 312 | " Test that the pattern is matched against the closed window's ID, and both |
| 313 | " <amatch> and <afile> are set to it. |
| 314 | new |
| 315 | let winid = win_getid() |
| 316 | let g:matched = v:false |
| 317 | augroup test-WinClosed |
| 318 | autocmd! |
| 319 | execute 'autocmd WinClosed' winid 'let g:matched = v:true' |
| 320 | autocmd WinClosed * let g:amatch = str2nr(expand('<amatch>')) |
| 321 | autocmd WinClosed * let g:afile = str2nr(expand('<afile>')) |
| 322 | augroup END |
| 323 | close |
| 324 | call assert_true(g:matched) |
| 325 | call assert_equal(winid, g:amatch) |
| 326 | call assert_equal(winid, g:afile) |
| 327 | |
| 328 | " Test that WinClosed is non-recursive. |
| 329 | new |
| 330 | new |
| 331 | call assert_equal(3, winnr('$')) |
| 332 | let g:triggered = 0 |
| 333 | augroup test-WinClosed |
| 334 | autocmd! |
| 335 | autocmd WinClosed * let g:triggered += 1 |
| 336 | autocmd WinClosed * 2 wincmd c |
| 337 | augroup END |
| 338 | close |
| 339 | call assert_equal(1, winnr('$')) |
| 340 | call assert_equal(1, g:triggered) |
| 341 | |
| 342 | autocmd! test-WinClosed |
| 343 | augroup! test-WinClosed |
| 344 | unlet g:matched |
| 345 | unlet g:amatch |
| 346 | unlet g:afile |
| 347 | unlet g:triggered |
| 348 | endfunc |
| 349 | |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 350 | func s:AddAnAutocmd() |
| 351 | augroup vimBarTest |
| 352 | au BufReadCmd * echo 'hello' |
| 353 | augroup END |
| 354 | call assert_equal(3, len(split(execute('au vimBarTest'), "\n"))) |
| 355 | endfunc |
| 356 | |
| 357 | func Test_early_bar() |
| 358 | " test that a bar is recognized before the {event} |
| 359 | call s:AddAnAutocmd() |
| 360 | augroup vimBarTest | au! | augroup END |
| 361 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
| 362 | |
| 363 | call s:AddAnAutocmd() |
| 364 | augroup vimBarTest| au!| augroup END |
| 365 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
| 366 | |
| 367 | " test that a bar is recognized after the {event} |
| 368 | call s:AddAnAutocmd() |
| 369 | augroup vimBarTest| au!BufReadCmd| augroup END |
| 370 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
| 371 | |
| 372 | " test that a bar is recognized after the {group} |
| 373 | call s:AddAnAutocmd() |
| 374 | au! vimBarTest|echo 'hello' |
| 375 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
| 376 | endfunc |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 377 | |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 378 | func RemoveGroup() |
| 379 | autocmd! StartOK |
| 380 | augroup! StartOK |
| 381 | endfunc |
| 382 | |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 383 | func Test_augroup_warning() |
| 384 | augroup TheWarning |
| 385 | au VimEnter * echo 'entering' |
| 386 | augroup END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 387 | call assert_match("TheWarning.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 388 | redir => res |
| 389 | augroup! TheWarning |
| 390 | redir END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 391 | call assert_match("W19:", res) |
| 392 | call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 393 | |
| 394 | " check "Another" does not take the pace of the deleted entry |
| 395 | augroup Another |
| 396 | augroup END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 397 | call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 398 | augroup! Another |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 399 | |
| 400 | " no warning for postpone aucmd delete |
| 401 | augroup StartOK |
| 402 | au VimEnter * call RemoveGroup() |
| 403 | augroup END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 404 | call assert_match("StartOK.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 405 | redir => res |
| 406 | doautocmd VimEnter |
| 407 | redir END |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 408 | call assert_notmatch("W19:", res) |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 409 | au! VimEnter |
Bram Moolenaar | ad48e6c | 2020-04-21 22:19:45 +0200 | [diff] [blame] | 410 | |
| 411 | call assert_fails('augroup!', 'E471:') |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 412 | endfunc |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 413 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 414 | func Test_BufReadCmdHelp() |
| 415 | " This used to cause access to free memory |
| 416 | au BufReadCmd * e +h |
| 417 | help |
| 418 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 419 | au! BufReadCmd |
| 420 | endfunc |
| 421 | |
| 422 | func Test_BufReadCmdHelpJump() |
| 423 | " This used to cause access to free memory |
| 424 | au BufReadCmd * e +h{ |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 425 | " } to fix highlighting |
| 426 | call assert_fails('help', 'E434:') |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 427 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 428 | au! BufReadCmd |
| 429 | endfunc |
| 430 | |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 431 | func Test_augroup_deleted() |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 432 | " This caused a crash before E936 was introduced |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 433 | augroup x |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 434 | call assert_fails('augroup! x', 'E936:') |
| 435 | au VimEnter * echo |
| 436 | augroup end |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 437 | augroup! x |
Bram Moolenaar | 5dc4e2f | 2020-11-25 14:15:12 +0100 | [diff] [blame] | 438 | call assert_match("-Deleted-.*VimEnter", execute('au VimEnter')) |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 439 | au! VimEnter |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 440 | endfunc |
| 441 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 442 | " Tests for autocommands on :close command. |
| 443 | " This used to be in test13. |
| 444 | func Test_three_windows() |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 445 | " Clean up buffers, because in some cases this function fails. |
| 446 | call s:cleanup_buffers() |
| 447 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 448 | " Write three files and open them, each in a window. |
| 449 | " Then go to next window, with autocommand that deletes the previous one. |
| 450 | " Do this twice, writing the file. |
| 451 | e! Xtestje1 |
| 452 | call setline(1, 'testje1') |
| 453 | w |
| 454 | sp Xtestje2 |
| 455 | call setline(1, 'testje2') |
| 456 | w |
| 457 | sp Xtestje3 |
| 458 | call setline(1, 'testje3') |
| 459 | w |
| 460 | wincmd w |
| 461 | au WinLeave Xtestje2 bwipe |
| 462 | wincmd w |
| 463 | call assert_equal('Xtestje1', expand('%')) |
| 464 | |
| 465 | au WinLeave Xtestje1 bwipe Xtestje3 |
| 466 | close |
| 467 | call assert_equal('Xtestje1', expand('%')) |
| 468 | |
| 469 | " Test deleting the buffer on a Unload event. If this goes wrong there |
| 470 | " will be the ATTENTION prompt. |
| 471 | e Xtestje1 |
| 472 | au! |
| 473 | au! BufUnload Xtestje1 bwipe |
| 474 | call assert_fails('e Xtestje3', 'E937:') |
| 475 | call assert_equal('Xtestje3', expand('%')) |
| 476 | |
| 477 | e Xtestje2 |
| 478 | sp Xtestje1 |
| 479 | call assert_fails('e', 'E937:') |
Bram Moolenaar | a997b45 | 2018-04-17 23:24:06 +0200 | [diff] [blame] | 480 | call assert_equal('Xtestje1', expand('%')) |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 481 | |
| 482 | " Test changing buffers in a BufWipeout autocommand. If this goes wrong |
| 483 | " there are ml_line errors and/or a Crash. |
| 484 | au! |
| 485 | only |
| 486 | e Xanother |
| 487 | e Xtestje1 |
| 488 | bwipe Xtestje2 |
| 489 | bwipe Xtestje3 |
| 490 | au BufWipeout Xtestje1 buf Xtestje1 |
| 491 | bwipe |
| 492 | call assert_equal('Xanother', expand('%')) |
| 493 | |
| 494 | only |
| 495 | help |
| 496 | wincmd w |
| 497 | 1quit |
| 498 | call assert_equal('Xanother', expand('%')) |
| 499 | |
| 500 | au! |
Bram Moolenaar | 4520d44 | 2017-03-19 16:09:46 +0100 | [diff] [blame] | 501 | enew |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 502 | call delete('Xtestje1') |
| 503 | call delete('Xtestje2') |
| 504 | call delete('Xtestje3') |
| 505 | endfunc |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 506 | |
| 507 | func Test_BufEnter() |
| 508 | au! BufEnter |
| 509 | au Bufenter * let val = val . '+' |
| 510 | let g:val = '' |
| 511 | split NewFile |
| 512 | call assert_equal('+', g:val) |
| 513 | bwipe! |
| 514 | call assert_equal('++', g:val) |
| 515 | |
| 516 | " Also get BufEnter when editing a directory |
| 517 | call mkdir('Xdir') |
| 518 | split Xdir |
| 519 | call assert_equal('+++', g:val) |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 520 | |
| 521 | " On MS-Windows we can't edit the directory, make sure we wipe the right |
| 522 | " buffer. |
| 523 | bwipe! Xdir |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 524 | |
| 525 | call delete('Xdir', 'd') |
| 526 | au! BufEnter |
| 527 | endfunc |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 528 | |
| 529 | " Closing a window might cause an endless loop |
| 530 | " E814 for older Vims |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 531 | func Test_autocmd_bufwipe_in_SessLoadPost() |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 532 | edit Xtest |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 533 | tabnew |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 534 | file Xsomething |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 535 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 536 | mksession! |
| 537 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 538 | let content =<< trim [CODE] |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 539 | call test_override('ui_delay', 10) |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 540 | set nocp noswapfile |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 541 | let v:swapchoice = "e" |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 542 | augroup test_autocmd_sessionload |
| 543 | autocmd! |
| 544 | autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!" |
| 545 | augroup END |
| 546 | |
| 547 | func WriteErrors() |
| 548 | call writefile([execute("messages")], "Xerrors") |
| 549 | endfunc |
| 550 | au VimLeave * call WriteErrors() |
| 551 | [CODE] |
| 552 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 553 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 554 | 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] | 555 | let errors = join(readfile('Xerrors')) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 556 | call assert_match('E814:', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 557 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 558 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 559 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 560 | call delete(file) |
| 561 | endfor |
| 562 | endfunc |
| 563 | |
Bram Moolenaar | 797e63b | 2021-01-15 16:22:52 +0100 | [diff] [blame] | 564 | " Using :blast and :ball for many events caused a crash, because b_nwindows was |
| 565 | " not incremented correctly. |
| 566 | func Test_autocmd_blast_badd() |
| 567 | let content =<< trim [CODE] |
| 568 | au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* blast |
| 569 | edit foo1 |
| 570 | au BufNew,BufAdd,BufWinEnter,BufEnter,BufLeave,BufWinLeave,BufUnload,VimEnter foo* ball |
| 571 | edit foo2 |
| 572 | call writefile(['OK'], 'Xerrors') |
| 573 | qall |
| 574 | [CODE] |
| 575 | |
| 576 | call writefile(content, 'XblastBall') |
| 577 | call system(GetVimCommand() .. ' --clean -S XblastBall') |
| 578 | call assert_match('OK', readfile('Xerrors')->join()) |
| 579 | |
| 580 | call delete('XblastBall') |
| 581 | call delete('Xerrors') |
| 582 | endfunc |
| 583 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 584 | " SEGV occurs in older versions. |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 585 | func Test_autocmd_bufwipe_in_SessLoadPost2() |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 586 | tabnew |
| 587 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 588 | mksession! |
| 589 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 590 | let content =<< trim [CODE] |
| 591 | set nocp noswapfile |
| 592 | function! DeleteInactiveBufs() |
| 593 | tabfirst |
| 594 | let tabblist = [] |
| 595 | for i in range(1, tabpagenr(''$'')) |
| 596 | call extend(tabblist, tabpagebuflist(i)) |
| 597 | endfor |
| 598 | for b in range(1, bufnr(''$'')) |
| 599 | if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'') |
| 600 | exec ''bwipeout '' . b |
| 601 | endif |
| 602 | endfor |
| 603 | echomsg "SessionLoadPost DONE" |
| 604 | endfunction |
| 605 | au SessionLoadPost * call DeleteInactiveBufs() |
| 606 | |
| 607 | func WriteErrors() |
| 608 | call writefile([execute("messages")], "Xerrors") |
| 609 | endfunc |
| 610 | au VimLeave * call WriteErrors() |
| 611 | [CODE] |
| 612 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 613 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 614 | 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] | 615 | let errors = join(readfile('Xerrors')) |
| 616 | " This probably only ever matches on unix. |
| 617 | call assert_notmatch('Caught deadly signal SEGV', errors) |
| 618 | call assert_match('SessionLoadPost DONE', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 619 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 620 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 621 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 622 | call delete(file) |
| 623 | endfor |
| 624 | endfunc |
Bram Moolenaar | faf29d7 | 2017-07-09 11:07:16 +0200 | [diff] [blame] | 625 | |
| 626 | func Test_empty_doau() |
| 627 | doau \| |
| 628 | endfunc |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 629 | |
| 630 | func s:AutoCommandOptionSet(match) |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 631 | 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] | 632 | let item = remove(g:options, 0) |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 633 | let expected = printf(template, item[0], item[1], item[2], item[3], item[4], item[5], item[6]) |
| 634 | 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] | 635 | let g:opt = [expected, actual] |
| 636 | "call assert_equal(expected, actual) |
| 637 | endfunc |
| 638 | |
| 639 | func Test_OptionSet() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 640 | CheckOption autochdir |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 641 | |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 642 | badd test_autocmd.vim |
| 643 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 644 | call test_override('starting', 1) |
| 645 | set nocp |
| 646 | au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>")) |
| 647 | |
| 648 | " 1: Setting number option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 649 | let g:options = [['number', 0, 0, 0, 1, 'global', 'set']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 650 | set nu |
| 651 | call assert_equal([], g:options) |
| 652 | call assert_equal(g:opt[0], g:opt[1]) |
| 653 | |
| 654 | " 2: Setting local number option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 655 | let g:options = [['number', 1, 1, '', 0, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 656 | setlocal nonu |
| 657 | call assert_equal([], g:options) |
| 658 | call assert_equal(g:opt[0], g:opt[1]) |
| 659 | |
| 660 | " 3: Setting global number option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 661 | let g:options = [['number', 1, '', 1, 0, 'global', 'setglobal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 662 | setglobal nonu |
| 663 | call assert_equal([], g:options) |
| 664 | call assert_equal(g:opt[0], g:opt[1]) |
| 665 | |
| 666 | " 4: Setting local autoindent option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 667 | let g:options = [['autoindent', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 668 | setlocal ai |
| 669 | call assert_equal([], g:options) |
| 670 | call assert_equal(g:opt[0], g:opt[1]) |
| 671 | |
| 672 | " 5: Setting global autoindent option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 673 | let g:options = [['autoindent', 0, '', 0, 1, 'global', 'setglobal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 674 | setglobal ai |
| 675 | call assert_equal([], g:options) |
| 676 | call assert_equal(g:opt[0], g:opt[1]) |
| 677 | |
| 678 | " 6: Setting global autoindent option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 679 | let g:options = [['autoindent', 1, 1, 1, 0, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 680 | set ai! |
| 681 | call assert_equal([], g:options) |
| 682 | call assert_equal(g:opt[0], g:opt[1]) |
| 683 | |
| 684 | " 6a: Setting global autoindent option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 685 | let g:options = [['autoindent', 1, 1, 0, 0, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 686 | noa setlocal ai |
| 687 | noa setglobal noai |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 688 | set ai! |
| 689 | call assert_equal([], g:options) |
| 690 | call assert_equal(g:opt[0], g:opt[1]) |
| 691 | |
| 692 | " Should not print anything, use :noa |
| 693 | " 7: don't trigger OptionSet" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 694 | let g:options = [['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 695 | noa set nonu |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 696 | call assert_equal([['invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid', 'invalid']], g:options) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 697 | call assert_equal(g:opt[0], g:opt[1]) |
| 698 | |
| 699 | " 8: Setting several global list and number option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 700 | 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] | 701 | set list nu |
| 702 | call assert_equal([], g:options) |
| 703 | call assert_equal(g:opt[0], g:opt[1]) |
| 704 | |
| 705 | " 9: don't trigger OptionSet" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 706 | 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] | 707 | noa set nolist nonu |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 708 | 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] | 709 | call assert_equal(g:opt[0], g:opt[1]) |
| 710 | |
| 711 | " 10: Setting global acd" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 712 | let g:options = [['autochdir', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 713 | setlocal acd |
| 714 | call assert_equal([], g:options) |
| 715 | call assert_equal(g:opt[0], g:opt[1]) |
| 716 | |
| 717 | " 11: Setting global autoread (also sets local value)" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 718 | let g:options = [['autoread', 0, 0, 0, 1, 'global', 'set']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 719 | set ar |
| 720 | call assert_equal([], g:options) |
| 721 | call assert_equal(g:opt[0], g:opt[1]) |
| 722 | |
| 723 | " 12: Setting local autoread" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 724 | let g:options = [['autoread', 1, 1, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 725 | setlocal ar |
| 726 | call assert_equal([], g:options) |
| 727 | call assert_equal(g:opt[0], g:opt[1]) |
| 728 | |
| 729 | " 13: Setting global autoread" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 730 | let g:options = [['autoread', 1, '', 1, 0, 'global', 'setglobal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 731 | setglobal invar |
| 732 | call assert_equal([], g:options) |
| 733 | call assert_equal(g:opt[0], g:opt[1]) |
| 734 | |
| 735 | " 14: Setting option backspace through :let" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 736 | let g:options = [['backspace', '', '', '', 'eol,indent,start', 'global', 'set']] |
| 737 | let &bs = "eol,indent,start" |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 738 | call assert_equal([], g:options) |
| 739 | call assert_equal(g:opt[0], g:opt[1]) |
| 740 | |
| 741 | " 15: Setting option backspace through setbufvar()" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 742 | let g:options = [['backup', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 743 | " try twice, first time, shouldn't trigger because option name is invalid, |
| 744 | " second time, it should trigger |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 745 | let bnum = bufnr('%') |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 746 | call assert_fails("call setbufvar(bnum, '&l:bk', 1)", 'E355:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 747 | " should trigger, use correct option name |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 748 | call setbufvar(bnum, '&backup', 1) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 749 | call assert_equal([], g:options) |
| 750 | call assert_equal(g:opt[0], g:opt[1]) |
| 751 | |
| 752 | " 16: Setting number option using setwinvar" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 753 | let g:options = [['number', 0, 0, '', 1, 'local', 'setlocal']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 754 | call setwinvar(0, '&number', 1) |
| 755 | call assert_equal([], g:options) |
| 756 | call assert_equal(g:opt[0], g:opt[1]) |
| 757 | |
| 758 | " 17: Setting key option, shouldn't trigger" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 759 | let g:options = [['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']] |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 760 | setlocal key=blah |
| 761 | setlocal key= |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 762 | call assert_equal([['key', 'invalid', 'invalid1', 'invalid2', 'invalid3', 'invalid4', 'invalid5']], g:options) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 763 | call assert_equal(g:opt[0], g:opt[1]) |
| 764 | |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 765 | |
| 766 | " 18a: Setting string global option" |
| 767 | let oldval = &backupext |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 768 | let g:options = [['backupext', oldval, oldval, oldval, 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 769 | set backupext=foo |
| 770 | call assert_equal([], g:options) |
| 771 | call assert_equal(g:opt[0], g:opt[1]) |
| 772 | |
| 773 | " 18b: Resetting string global option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 774 | let g:options = [['backupext', 'foo', 'foo', 'foo', oldval, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 775 | set backupext& |
| 776 | call assert_equal([], g:options) |
| 777 | call assert_equal(g:opt[0], g:opt[1]) |
| 778 | |
| 779 | " 18c: Setting global string global option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 780 | let g:options = [['backupext', oldval, '', oldval, 'bar', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 781 | setglobal backupext=bar |
| 782 | call assert_equal([], g:options) |
| 783 | call assert_equal(g:opt[0], g:opt[1]) |
| 784 | |
| 785 | " 18d: Setting local string global option" |
| 786 | " As this is a global option this sets the global value even though |
| 787 | " :setlocal is used! |
| 788 | noa set backupext& " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 789 | let g:options = [['backupext', oldval, oldval, '', 'baz', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 790 | setlocal backupext=baz |
| 791 | call assert_equal([], g:options) |
| 792 | call assert_equal(g:opt[0], g:opt[1]) |
| 793 | |
| 794 | " 18e: Setting again string global option" |
| 795 | noa setglobal backupext=ext_global " Reset global and local value (without triggering autocmd) |
| 796 | noa setlocal backupext=ext_local " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 797 | 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] | 798 | set backupext=fuu |
| 799 | call assert_equal([], g:options) |
| 800 | call assert_equal(g:opt[0], g:opt[1]) |
| 801 | |
| 802 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 803 | " 19a: Setting string global-local (to buffer) option" |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 804 | let oldval = &tags |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 805 | let g:options = [['tags', oldval, oldval, oldval, 'tagpath', 'global', 'set']] |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 806 | set tags=tagpath |
| 807 | call assert_equal([], g:options) |
| 808 | call assert_equal(g:opt[0], g:opt[1]) |
| 809 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 810 | " 19b: Resetting string global-local (to buffer) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 811 | let g:options = [['tags', 'tagpath', 'tagpath', 'tagpath', oldval, 'global', 'set']] |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 812 | set tags& |
| 813 | call assert_equal([], g:options) |
| 814 | call assert_equal(g:opt[0], g:opt[1]) |
| 815 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 816 | " 19c: Setting global string global-local (to buffer) option " |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 817 | let g:options = [['tags', oldval, '', oldval, 'tagpath1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 818 | setglobal tags=tagpath1 |
| 819 | call assert_equal([], g:options) |
| 820 | call assert_equal(g:opt[0], g:opt[1]) |
| 821 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 822 | " 19d: Setting local string global-local (to buffer) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 823 | let g:options = [['tags', 'tagpath1', 'tagpath1', '', 'tagpath2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 824 | setlocal tags=tagpath2 |
| 825 | call assert_equal([], g:options) |
| 826 | call assert_equal(g:opt[0], g:opt[1]) |
| 827 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 828 | " 19e: Setting again string global-local (to buffer) option" |
| 829 | " 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] | 830 | " but the old local value for all other kinds of options. |
| 831 | noa setglobal tags=tag_global " Reset global and local value (without triggering autocmd) |
| 832 | noa setlocal tags=tag_local |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 833 | 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] | 834 | set tags=tagpath |
| 835 | call assert_equal([], g:options) |
| 836 | call assert_equal(g:opt[0], g:opt[1]) |
| 837 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 838 | " 19f: Setting string global-local (to buffer) option to an empty string" |
| 839 | " 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] | 840 | " but the old local value for all other kinds of options. |
| 841 | noa set tags=tag_global " Reset global and local value (without triggering autocmd) |
| 842 | noa setlocal tags= " empty string |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 843 | let g:options = [['tags', 'tag_global', '', 'tag_global', 'tagpath', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 844 | set tags=tagpath |
| 845 | call assert_equal([], g:options) |
| 846 | call assert_equal(g:opt[0], g:opt[1]) |
| 847 | |
| 848 | |
| 849 | " 20a: Setting string local (to buffer) option" |
| 850 | let oldval = &spelllang |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 851 | let g:options = [['spelllang', oldval, oldval, oldval, 'elvish,klingon', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 852 | set spelllang=elvish,klingon |
| 853 | call assert_equal([], g:options) |
| 854 | call assert_equal(g:opt[0], g:opt[1]) |
| 855 | |
| 856 | " 20b: Resetting string local (to buffer) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 857 | 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] | 858 | set spelllang& |
| 859 | call assert_equal([], g:options) |
| 860 | call assert_equal(g:opt[0], g:opt[1]) |
| 861 | |
| 862 | " 20c: Setting global string local (to buffer) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 863 | let g:options = [['spelllang', oldval, '', oldval, 'elvish', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 864 | setglobal spelllang=elvish |
| 865 | call assert_equal([], g:options) |
| 866 | call assert_equal(g:opt[0], g:opt[1]) |
| 867 | |
| 868 | " 20d: Setting local string local (to buffer) option" |
| 869 | noa set spelllang& " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 870 | let g:options = [['spelllang', oldval, oldval, '', 'klingon', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 871 | setlocal spelllang=klingon |
| 872 | call assert_equal([], g:options) |
| 873 | call assert_equal(g:opt[0], g:opt[1]) |
| 874 | |
| 875 | " 20e: Setting again string local (to buffer) option" |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 876 | " 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] | 877 | " but the old local value for all other kinds of options. |
| 878 | noa setglobal spelllang=spellglobal " Reset global and local value (without triggering autocmd) |
| 879 | noa setlocal spelllang=spelllocal |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 880 | let g:options = [['spelllang', 'spelllocal', 'spelllocal', 'spellglobal', 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 881 | set spelllang=foo |
| 882 | call assert_equal([], g:options) |
| 883 | call assert_equal(g:opt[0], g:opt[1]) |
| 884 | |
| 885 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 886 | " 21a: Setting string global-local (to window) option" |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 887 | let oldval = &statusline |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 888 | let g:options = [['statusline', oldval, oldval, oldval, 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 889 | set statusline=foo |
| 890 | call assert_equal([], g:options) |
| 891 | call assert_equal(g:opt[0], g:opt[1]) |
| 892 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 893 | " 21b: Resetting string global-local (to window) option" |
| 894 | " 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] | 895 | " but the old local value for all other kinds of options. |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 896 | let g:options = [['statusline', 'foo', 'foo', 'foo', oldval, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 897 | set statusline& |
| 898 | call assert_equal([], g:options) |
| 899 | call assert_equal(g:opt[0], g:opt[1]) |
| 900 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 901 | " 21c: Setting global string global-local (to window) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 902 | let g:options = [['statusline', oldval, '', oldval, 'bar', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 903 | setglobal statusline=bar |
| 904 | call assert_equal([], g:options) |
| 905 | call assert_equal(g:opt[0], g:opt[1]) |
| 906 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 907 | " 21d: Setting local string global-local (to window) option" |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 908 | noa set statusline& " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 909 | let g:options = [['statusline', oldval, oldval, '', 'baz', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 910 | setlocal statusline=baz |
| 911 | call assert_equal([], g:options) |
| 912 | call assert_equal(g:opt[0], g:opt[1]) |
| 913 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 914 | " 21e: Setting again string global-local (to window) option" |
| 915 | " 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] | 916 | " but the old local value for all other kinds of options. |
| 917 | noa setglobal statusline=bar " Reset global and local value (without triggering autocmd) |
| 918 | noa setlocal statusline=baz |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 919 | let g:options = [['statusline', 'bar', 'baz', 'bar', 'foo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 920 | set statusline=foo |
| 921 | call assert_equal([], g:options) |
| 922 | call assert_equal(g:opt[0], g:opt[1]) |
| 923 | |
| 924 | |
| 925 | " 22a: Setting string local (to window) option" |
| 926 | let oldval = &foldignore |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 927 | let g:options = [['foldignore', oldval, oldval, oldval, 'fo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 928 | set foldignore=fo |
| 929 | call assert_equal([], g:options) |
| 930 | call assert_equal(g:opt[0], g:opt[1]) |
| 931 | |
| 932 | " 22b: Resetting string local (to window) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 933 | let g:options = [['foldignore', 'fo', 'fo', 'fo', oldval, 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 934 | set foldignore& |
| 935 | call assert_equal([], g:options) |
| 936 | call assert_equal(g:opt[0], g:opt[1]) |
| 937 | |
| 938 | " 22c: Setting global string local (to window) option" |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 939 | let g:options = [['foldignore', oldval, '', oldval, 'bar', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 940 | setglobal foldignore=bar |
| 941 | call assert_equal([], g:options) |
| 942 | call assert_equal(g:opt[0], g:opt[1]) |
| 943 | |
| 944 | " 22d: Setting local string local (to window) option" |
| 945 | noa set foldignore& " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 946 | let g:options = [['foldignore', oldval, oldval, '', 'baz', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 947 | setlocal foldignore=baz |
| 948 | call assert_equal([], g:options) |
| 949 | call assert_equal(g:opt[0], g:opt[1]) |
| 950 | |
| 951 | " 22e: Setting again string local (to window) option" |
| 952 | noa setglobal foldignore=glob " Reset global and local value (without triggering autocmd) |
| 953 | noa setlocal foldignore=loc |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 954 | let g:options = [['foldignore', 'loc', 'loc', 'glob', 'fo', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 955 | set foldignore=fo |
| 956 | call assert_equal([], g:options) |
| 957 | call assert_equal(g:opt[0], g:opt[1]) |
| 958 | |
| 959 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 960 | " 23a: Setting global number global option" |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 961 | noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) |
| 962 | noa setlocal cmdheight=1 " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 963 | let g:options = [['cmdheight', '1', '', '1', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 964 | setglobal cmdheight=2 |
| 965 | call assert_equal([], g:options) |
| 966 | call assert_equal(g:opt[0], g:opt[1]) |
| 967 | |
| 968 | " 23b: Setting local number global option" |
| 969 | noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) |
| 970 | noa setlocal cmdheight=1 " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 971 | let g:options = [['cmdheight', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 972 | setlocal cmdheight=2 |
| 973 | call assert_equal([], g:options) |
| 974 | call assert_equal(g:opt[0], g:opt[1]) |
| 975 | |
| 976 | " 23c: Setting again number global option" |
| 977 | noa setglobal cmdheight=8 " Reset global and local value (without triggering autocmd) |
| 978 | noa setlocal cmdheight=1 " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 979 | let g:options = [['cmdheight', '1', '1', '1', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 980 | set cmdheight=2 |
| 981 | call assert_equal([], g:options) |
| 982 | call assert_equal(g:opt[0], g:opt[1]) |
| 983 | |
| 984 | " 23d: Setting again number global option" |
| 985 | noa set cmdheight=8 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 986 | let g:options = [['cmdheight', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 987 | set cmdheight=2 |
| 988 | call assert_equal([], g:options) |
| 989 | call assert_equal(g:opt[0], g:opt[1]) |
| 990 | |
| 991 | |
| 992 | " 24a: Setting global number global-local (to buffer) option" |
| 993 | noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) |
| 994 | noa setlocal undolevels=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 995 | let g:options = [['undolevels', '8', '', '8', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 996 | setglobal undolevels=2 |
| 997 | call assert_equal([], g:options) |
| 998 | call assert_equal(g:opt[0], g:opt[1]) |
| 999 | |
| 1000 | " 24b: Setting local number global-local (to buffer) option" |
| 1001 | noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) |
| 1002 | noa setlocal undolevels=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1003 | let g:options = [['undolevels', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1004 | setlocal undolevels=2 |
| 1005 | call assert_equal([], g:options) |
| 1006 | call assert_equal(g:opt[0], g:opt[1]) |
| 1007 | |
| 1008 | " 24c: Setting again number global-local (to buffer) option" |
| 1009 | noa setglobal undolevels=8 " Reset global and local value (without triggering autocmd) |
| 1010 | noa setlocal undolevels=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1011 | let g:options = [['undolevels', '1', '1', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1012 | set undolevels=2 |
| 1013 | call assert_equal([], g:options) |
| 1014 | call assert_equal(g:opt[0], g:opt[1]) |
| 1015 | |
| 1016 | " 24d: Setting again global number global-local (to buffer) option" |
| 1017 | noa set undolevels=8 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1018 | let g:options = [['undolevels', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1019 | set undolevels=2 |
| 1020 | call assert_equal([], g:options) |
| 1021 | call assert_equal(g:opt[0], g:opt[1]) |
| 1022 | |
| 1023 | |
| 1024 | " 25a: Setting global number local (to buffer) option" |
| 1025 | noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) |
| 1026 | noa setlocal wrapmargin=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1027 | let g:options = [['wrapmargin', '8', '', '8', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1028 | setglobal wrapmargin=2 |
| 1029 | call assert_equal([], g:options) |
| 1030 | call assert_equal(g:opt[0], g:opt[1]) |
| 1031 | |
| 1032 | " 25b: Setting local number local (to buffer) option" |
| 1033 | noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) |
| 1034 | noa setlocal wrapmargin=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1035 | let g:options = [['wrapmargin', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1036 | setlocal wrapmargin=2 |
| 1037 | call assert_equal([], g:options) |
| 1038 | call assert_equal(g:opt[0], g:opt[1]) |
| 1039 | |
| 1040 | " 25c: Setting again number local (to buffer) option" |
| 1041 | noa setglobal wrapmargin=8 " Reset global and local value (without triggering autocmd) |
| 1042 | noa setlocal wrapmargin=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1043 | let g:options = [['wrapmargin', '1', '1', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1044 | set wrapmargin=2 |
| 1045 | call assert_equal([], g:options) |
| 1046 | call assert_equal(g:opt[0], g:opt[1]) |
| 1047 | |
| 1048 | " 25d: Setting again global number local (to buffer) option" |
| 1049 | noa set wrapmargin=8 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1050 | let g:options = [['wrapmargin', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1051 | set wrapmargin=2 |
| 1052 | call assert_equal([], g:options) |
| 1053 | call assert_equal(g:opt[0], g:opt[1]) |
| 1054 | |
| 1055 | |
| 1056 | " 26: Setting number global-local (to window) option. |
| 1057 | " Such option does currently not exist. |
| 1058 | |
| 1059 | |
| 1060 | " 27a: Setting global number local (to window) option" |
| 1061 | noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) |
| 1062 | noa setlocal foldcolumn=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1063 | let g:options = [['foldcolumn', '8', '', '8', '2', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1064 | setglobal foldcolumn=2 |
| 1065 | call assert_equal([], g:options) |
| 1066 | call assert_equal(g:opt[0], g:opt[1]) |
| 1067 | |
| 1068 | " 27b: Setting local number local (to window) option" |
| 1069 | noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) |
| 1070 | noa setlocal foldcolumn=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1071 | let g:options = [['foldcolumn', '1', '1', '', '2', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1072 | setlocal foldcolumn=2 |
| 1073 | call assert_equal([], g:options) |
| 1074 | call assert_equal(g:opt[0], g:opt[1]) |
| 1075 | |
| 1076 | " 27c: Setting again number local (to window) option" |
| 1077 | noa setglobal foldcolumn=8 " Reset global and local value (without triggering autocmd) |
| 1078 | noa setlocal foldcolumn=1 |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1079 | let g:options = [['foldcolumn', '1', '1', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1080 | set foldcolumn=2 |
| 1081 | call assert_equal([], g:options) |
| 1082 | call assert_equal(g:opt[0], g:opt[1]) |
| 1083 | |
zeertzjq | b811de5 | 2021-10-21 10:50:44 +0100 | [diff] [blame] | 1084 | " 27d: Setting again global number local (to window) option" |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1085 | noa set foldcolumn=8 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1086 | let g:options = [['foldcolumn', '8', '8', '8', '2', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1087 | set foldcolumn=2 |
| 1088 | call assert_equal([], g:options) |
| 1089 | call assert_equal(g:opt[0], g:opt[1]) |
| 1090 | |
| 1091 | |
| 1092 | " 28a: Setting global boolean global option" |
| 1093 | noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) |
| 1094 | noa setlocal wrapscan " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1095 | let g:options = [['wrapscan', '1', '', '1', '0', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1096 | setglobal nowrapscan |
| 1097 | call assert_equal([], g:options) |
| 1098 | call assert_equal(g:opt[0], g:opt[1]) |
| 1099 | |
| 1100 | " 28b: Setting local boolean global option" |
| 1101 | noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) |
| 1102 | noa setlocal wrapscan " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1103 | let g:options = [['wrapscan', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1104 | setlocal nowrapscan |
| 1105 | call assert_equal([], g:options) |
| 1106 | call assert_equal(g:opt[0], g:opt[1]) |
| 1107 | |
| 1108 | " 28c: Setting again boolean global option" |
| 1109 | noa setglobal nowrapscan " Reset global and local value (without triggering autocmd) |
| 1110 | noa setlocal wrapscan " Sets the global(!) value! |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1111 | let g:options = [['wrapscan', '1', '1', '1', '0', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1112 | set nowrapscan |
| 1113 | call assert_equal([], g:options) |
| 1114 | call assert_equal(g:opt[0], g:opt[1]) |
| 1115 | |
| 1116 | " 28d: Setting again global boolean global option" |
| 1117 | noa set nowrapscan " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1118 | let g:options = [['wrapscan', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1119 | set wrapscan |
| 1120 | call assert_equal([], g:options) |
| 1121 | call assert_equal(g:opt[0], g:opt[1]) |
| 1122 | |
| 1123 | |
| 1124 | " 29a: Setting global boolean global-local (to buffer) option" |
| 1125 | noa setglobal noautoread " Reset global and local value (without triggering autocmd) |
| 1126 | noa setlocal autoread |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1127 | let g:options = [['autoread', '0', '', '0', '1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1128 | setglobal autoread |
| 1129 | call assert_equal([], g:options) |
| 1130 | call assert_equal(g:opt[0], g:opt[1]) |
| 1131 | |
| 1132 | " 29b: Setting local boolean global-local (to buffer) option" |
| 1133 | noa setglobal noautoread " Reset global and local value (without triggering autocmd) |
| 1134 | noa setlocal autoread |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1135 | let g:options = [['autoread', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1136 | setlocal noautoread |
| 1137 | call assert_equal([], g:options) |
| 1138 | call assert_equal(g:opt[0], g:opt[1]) |
| 1139 | |
| 1140 | " 29c: Setting again boolean global-local (to buffer) option" |
| 1141 | noa setglobal noautoread " Reset global and local value (without triggering autocmd) |
| 1142 | noa setlocal autoread |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1143 | let g:options = [['autoread', '1', '1', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1144 | set autoread |
| 1145 | call assert_equal([], g:options) |
| 1146 | call assert_equal(g:opt[0], g:opt[1]) |
| 1147 | |
| 1148 | " 29d: Setting again global boolean global-local (to buffer) option" |
| 1149 | noa set noautoread " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1150 | let g:options = [['autoread', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1151 | set autoread |
| 1152 | call assert_equal([], g:options) |
| 1153 | call assert_equal(g:opt[0], g:opt[1]) |
| 1154 | |
| 1155 | |
| 1156 | " 30a: Setting global boolean local (to buffer) option" |
| 1157 | noa setglobal nocindent " Reset global and local value (without triggering autocmd) |
| 1158 | noa setlocal cindent |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1159 | let g:options = [['cindent', '0', '', '0', '1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1160 | setglobal cindent |
| 1161 | call assert_equal([], g:options) |
| 1162 | call assert_equal(g:opt[0], g:opt[1]) |
| 1163 | |
| 1164 | " 30b: Setting local boolean local (to buffer) option" |
| 1165 | noa setglobal nocindent " Reset global and local value (without triggering autocmd) |
| 1166 | noa setlocal cindent |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1167 | let g:options = [['cindent', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1168 | setlocal nocindent |
| 1169 | call assert_equal([], g:options) |
| 1170 | call assert_equal(g:opt[0], g:opt[1]) |
| 1171 | |
| 1172 | " 30c: Setting again boolean local (to buffer) option" |
| 1173 | noa setglobal nocindent " Reset global and local value (without triggering autocmd) |
| 1174 | noa setlocal cindent |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1175 | let g:options = [['cindent', '1', '1', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1176 | set cindent |
| 1177 | call assert_equal([], g:options) |
| 1178 | call assert_equal(g:opt[0], g:opt[1]) |
| 1179 | |
| 1180 | " 30d: Setting again global boolean local (to buffer) option" |
| 1181 | noa set nocindent " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1182 | let g:options = [['cindent', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1183 | set cindent |
| 1184 | call assert_equal([], g:options) |
| 1185 | call assert_equal(g:opt[0], g:opt[1]) |
| 1186 | |
| 1187 | |
| 1188 | " 31: Setting boolean global-local (to window) option |
| 1189 | " Currently no such option exists. |
| 1190 | |
| 1191 | |
| 1192 | " 32a: Setting global boolean local (to window) option" |
| 1193 | noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) |
| 1194 | noa setlocal cursorcolumn |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1195 | let g:options = [['cursorcolumn', '0', '', '0', '1', 'global', 'setglobal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1196 | setglobal cursorcolumn |
| 1197 | call assert_equal([], g:options) |
| 1198 | call assert_equal(g:opt[0], g:opt[1]) |
| 1199 | |
| 1200 | " 32b: Setting local boolean local (to window) option" |
| 1201 | noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) |
| 1202 | noa setlocal cursorcolumn |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1203 | let g:options = [['cursorcolumn', '1', '1', '', '0', 'local', 'setlocal']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1204 | setlocal nocursorcolumn |
| 1205 | call assert_equal([], g:options) |
| 1206 | call assert_equal(g:opt[0], g:opt[1]) |
| 1207 | |
| 1208 | " 32c: Setting again boolean local (to window) option" |
| 1209 | noa setglobal nocursorcolumn " Reset global and local value (without triggering autocmd) |
| 1210 | noa setlocal cursorcolumn |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1211 | let g:options = [['cursorcolumn', '1', '1', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1212 | set cursorcolumn |
| 1213 | call assert_equal([], g:options) |
| 1214 | call assert_equal(g:opt[0], g:opt[1]) |
| 1215 | |
| 1216 | " 32d: Setting again global boolean local (to window) option" |
| 1217 | noa set nocursorcolumn " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1218 | let g:options = [['cursorcolumn', '0', '0', '0', '1', 'global', 'set']] |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1219 | set cursorcolumn |
| 1220 | call assert_equal([], g:options) |
| 1221 | call assert_equal(g:opt[0], g:opt[1]) |
| 1222 | |
| 1223 | |
Bram Moolenaar | 1bc353b | 2019-09-01 14:45:28 +0200 | [diff] [blame] | 1224 | " 33: Test autocommands when an option value is converted internally. |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1225 | noa set backspace=1 " Reset global and local value (without triggering autocmd) |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1226 | 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] | 1227 | set backspace=2 |
| 1228 | call assert_equal([], g:options) |
| 1229 | call assert_equal(g:opt[0], g:opt[1]) |
| 1230 | |
| 1231 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1232 | " Cleanup |
| 1233 | au! OptionSet |
Bram Moolenaar | 0331faf | 2019-06-15 18:40:37 +0200 | [diff] [blame] | 1234 | " set tags& |
Bram Moolenaar | d7c9687 | 2019-06-15 17:12:48 +0200 | [diff] [blame] | 1235 | 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] | 1236 | exe printf(":set %s&vim", opt) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1237 | endfor |
| 1238 | call test_override('starting', 0) |
| 1239 | delfunc! AutoCommandOptionSet |
| 1240 | endfunc |
| 1241 | |
| 1242 | func Test_OptionSet_diffmode() |
| 1243 | call test_override('starting', 1) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 1244 | " 18: Changing an option when entering diff mode |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1245 | new |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 1246 | au OptionSet diff :let &l:cul = v:option_new |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1247 | |
| 1248 | call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) |
| 1249 | call assert_equal(0, &l:cul) |
| 1250 | diffthis |
| 1251 | call assert_equal(1, &l:cul) |
| 1252 | |
| 1253 | vnew |
| 1254 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
| 1255 | call assert_equal(0, &l:cul) |
| 1256 | diffthis |
| 1257 | call assert_equal(1, &l:cul) |
| 1258 | |
| 1259 | diffoff |
| 1260 | call assert_equal(0, &l:cul) |
| 1261 | call assert_equal(1, getwinvar(2, '&l:cul')) |
| 1262 | bw! |
| 1263 | |
| 1264 | call assert_equal(1, &l:cul) |
| 1265 | diffoff! |
| 1266 | call assert_equal(0, &l:cul) |
| 1267 | call assert_equal(0, getwinvar(1, '&l:cul')) |
| 1268 | bw! |
| 1269 | |
| 1270 | " Cleanup |
| 1271 | au! OptionSet |
| 1272 | call test_override('starting', 0) |
| 1273 | endfunc |
| 1274 | |
| 1275 | func Test_OptionSet_diffmode_close() |
| 1276 | call test_override('starting', 1) |
| 1277 | " 19: Try to close the current window when entering diff mode |
| 1278 | " should not segfault |
| 1279 | new |
| 1280 | au OptionSet diff close |
| 1281 | |
| 1282 | call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1283 | call assert_fails(':diffthis', 'E788:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1284 | call assert_equal(1, &diff) |
| 1285 | vnew |
| 1286 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1287 | call assert_fails(':diffthis', 'E788:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1288 | call assert_equal(1, &diff) |
Bram Moolenaar | a9aa86f | 2019-11-10 21:25:45 +0100 | [diff] [blame] | 1289 | set diffopt-=closeoff |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1290 | bw! |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1291 | call assert_fails(':diffoff!', 'E788:') |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 1292 | bw! |
| 1293 | |
| 1294 | " Cleanup |
| 1295 | au! OptionSet |
| 1296 | call test_override('starting', 0) |
| 1297 | "delfunc! AutoCommandOptionSet |
| 1298 | endfunc |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 1299 | |
| 1300 | " Test for Bufleave autocommand that deletes the buffer we are about to edit. |
| 1301 | func Test_BufleaveWithDelete() |
| 1302 | new | edit Xfile1 |
| 1303 | |
| 1304 | augroup test_bufleavewithdelete |
| 1305 | autocmd! |
| 1306 | autocmd BufLeave Xfile1 bwipe Xfile2 |
| 1307 | augroup END |
| 1308 | |
| 1309 | call assert_fails('edit Xfile2', 'E143:') |
| 1310 | call assert_equal('Xfile1', bufname('%')) |
| 1311 | |
| 1312 | autocmd! test_bufleavewithdelete BufLeave Xfile1 |
| 1313 | augroup! test_bufleavewithdelete |
| 1314 | |
| 1315 | new |
| 1316 | bwipe! Xfile1 |
| 1317 | endfunc |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1318 | |
| 1319 | " Test for autocommand that changes the buffer list, when doing ":ball". |
| 1320 | func Test_Acmd_BufAll() |
| 1321 | enew! |
| 1322 | %bwipe! |
| 1323 | call writefile(['Test file Xxx1'], 'Xxx1') |
| 1324 | call writefile(['Test file Xxx2'], 'Xxx2') |
| 1325 | call writefile(['Test file Xxx3'], 'Xxx3') |
| 1326 | |
| 1327 | " Add three files to the buffer list |
| 1328 | split Xxx1 |
| 1329 | close |
| 1330 | split Xxx2 |
| 1331 | close |
| 1332 | split Xxx3 |
| 1333 | close |
| 1334 | |
| 1335 | " Wipe the buffer when the buffer is opened |
| 1336 | au BufReadPost Xxx2 bwipe |
| 1337 | |
| 1338 | call append(0, 'Test file Xxx4') |
| 1339 | ball |
| 1340 | |
| 1341 | call assert_equal(2, winnr('$')) |
| 1342 | call assert_equal('Xxx1', bufname(winbufnr(winnr('$')))) |
| 1343 | wincmd t |
| 1344 | |
| 1345 | au! BufReadPost |
| 1346 | %bwipe! |
| 1347 | call delete('Xxx1') |
| 1348 | call delete('Xxx2') |
| 1349 | call delete('Xxx3') |
| 1350 | enew! | only |
| 1351 | endfunc |
| 1352 | |
| 1353 | " Test for autocommand that changes current buffer on BufEnter event. |
| 1354 | " Check if modelines are interpreted for the correct buffer. |
| 1355 | func Test_Acmd_BufEnter() |
| 1356 | %bwipe! |
| 1357 | call writefile(['start of test file Xxx1', |
| 1358 | \ "\<Tab>this is a test", |
| 1359 | \ 'end of test file Xxx1'], 'Xxx1') |
| 1360 | call writefile(['start of test file Xxx2', |
| 1361 | \ 'vim: set noai :', |
| 1362 | \ "\<Tab>this is a test", |
| 1363 | \ 'end of test file Xxx2'], 'Xxx2') |
| 1364 | |
| 1365 | au BufEnter Xxx2 brew |
| 1366 | set ai modeline modelines=3 |
| 1367 | edit Xxx1 |
| 1368 | " edit Xxx2, autocmd will do :brew |
| 1369 | edit Xxx2 |
| 1370 | exe "normal G?this is a\<CR>" |
| 1371 | " Append text with autoindent to this file |
| 1372 | normal othis should be auto-indented |
| 1373 | call assert_equal("\<Tab>this should be auto-indented", getline('.')) |
| 1374 | call assert_equal(3, line('.')) |
| 1375 | " Remove autocmd and edit Xxx2 again |
| 1376 | au! BufEnter Xxx2 |
| 1377 | buf! Xxx2 |
| 1378 | exe "normal G?this is a\<CR>" |
| 1379 | " append text without autoindent to Xxx |
| 1380 | normal othis should be in column 1 |
| 1381 | call assert_equal("this should be in column 1", getline('.')) |
| 1382 | call assert_equal(4, line('.')) |
| 1383 | |
| 1384 | %bwipe! |
| 1385 | call delete('Xxx1') |
| 1386 | call delete('Xxx2') |
| 1387 | set ai&vim modeline&vim modelines&vim |
| 1388 | endfunc |
| 1389 | |
| 1390 | " Test for issue #57 |
| 1391 | " do not move cursor on <c-o> when autoindent is set |
| 1392 | func Test_ai_CTRL_O() |
| 1393 | enew! |
| 1394 | set ai |
| 1395 | let save_fo = &fo |
| 1396 | set fo+=r |
| 1397 | exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>" |
| 1398 | exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>" |
| 1399 | call assert_equal(['# abc', 'def', 'def'], getline(2, 4)) |
| 1400 | |
| 1401 | set ai&vim |
| 1402 | let &fo = save_fo |
| 1403 | enew! |
| 1404 | endfunc |
| 1405 | |
| 1406 | " Test for autocommand that deletes the current buffer on BufLeave event. |
| 1407 | " Also test deleting the last buffer, should give a new, empty buffer. |
| 1408 | func Test_BufLeave_Wipe() |
| 1409 | %bwipe! |
| 1410 | let content = ['start of test file Xxx', |
| 1411 | \ 'this is a test', |
| 1412 | \ 'end of test file Xxx'] |
| 1413 | call writefile(content, 'Xxx1') |
| 1414 | call writefile(content, 'Xxx2') |
| 1415 | |
| 1416 | au BufLeave Xxx2 bwipe |
| 1417 | edit Xxx1 |
| 1418 | split Xxx2 |
| 1419 | " delete buffer Xxx2, we should be back to Xxx1 |
| 1420 | bwipe |
| 1421 | call assert_equal('Xxx1', bufname('%')) |
| 1422 | call assert_equal(1, winnr('$')) |
| 1423 | |
| 1424 | " Create an alternate buffer |
| 1425 | %write! test.out |
| 1426 | call assert_equal('test.out', bufname('#')) |
| 1427 | " delete alternate buffer |
| 1428 | bwipe test.out |
| 1429 | call assert_equal('Xxx1', bufname('%')) |
| 1430 | call assert_equal('', bufname('#')) |
| 1431 | |
| 1432 | au BufLeave Xxx1 bwipe |
| 1433 | " delete current buffer, get an empty one |
| 1434 | bwipe! |
| 1435 | call assert_equal(1, line('$')) |
| 1436 | call assert_equal('', bufname('%')) |
Bram Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 1437 | let g:bufinfo = getbufinfo() |
| 1438 | call assert_equal(1, len(g:bufinfo)) |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1439 | |
| 1440 | call delete('Xxx1') |
| 1441 | call delete('Xxx2') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1442 | call delete('test.out') |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1443 | %bwipe |
| 1444 | au! BufLeave |
Bram Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 1445 | |
| 1446 | " check that bufinfo doesn't contain a pointer to freed memory |
| 1447 | call test_garbagecollect_now() |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 1448 | endfunc |
Bram Moolenaar | 87ffb5c | 2017-10-19 12:37:42 +0200 | [diff] [blame] | 1449 | |
| 1450 | func Test_QuitPre() |
| 1451 | edit Xfoo |
| 1452 | let winid = win_getid(winnr()) |
| 1453 | split Xbar |
| 1454 | au! QuitPre * let g:afile = expand('<afile>') |
| 1455 | " Close the other window, <afile> should be correct. |
| 1456 | exe win_id2win(winid) . 'q' |
| 1457 | call assert_equal('Xfoo', g:afile) |
| 1458 | |
| 1459 | unlet g:afile |
| 1460 | bwipe Xfoo |
| 1461 | bwipe Xbar |
| 1462 | endfunc |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1463 | |
| 1464 | func Test_Cmdline() |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 1465 | au! CmdlineChanged : let g:text = getcmdline() |
| 1466 | let g:text = 0 |
| 1467 | call feedkeys(":echom 'hello'\<CR>", 'xt') |
| 1468 | call assert_equal("echom 'hello'", g:text) |
| 1469 | au! CmdlineChanged |
| 1470 | |
| 1471 | au! CmdlineChanged : let g:entered = expand('<afile>') |
| 1472 | let g:entered = 0 |
| 1473 | call feedkeys(":echom 'hello'\<CR>", 'xt') |
| 1474 | call assert_equal(':', g:entered) |
| 1475 | au! CmdlineChanged |
| 1476 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1477 | au! CmdlineEnter : let g:entered = expand('<afile>') |
| 1478 | au! CmdlineLeave : let g:left = expand('<afile>') |
| 1479 | let g:entered = 0 |
| 1480 | let g:left = 0 |
| 1481 | call feedkeys(":echo 'hello'\<CR>", 'xt') |
| 1482 | call assert_equal(':', g:entered) |
| 1483 | call assert_equal(':', g:left) |
| 1484 | au! CmdlineEnter |
| 1485 | au! CmdlineLeave |
| 1486 | |
Bram Moolenaar | a4baf5b | 2018-04-22 13:27:44 +0200 | [diff] [blame] | 1487 | let save_shellslash = &shellslash |
| 1488 | set noshellslash |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1489 | au! CmdlineEnter / let g:entered = expand('<afile>') |
| 1490 | au! CmdlineLeave / let g:left = expand('<afile>') |
| 1491 | let g:entered = 0 |
| 1492 | let g:left = 0 |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1493 | new |
| 1494 | call setline(1, 'hello') |
| 1495 | call feedkeys("/hello\<CR>", 'xt') |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1496 | call assert_equal('/', g:entered) |
| 1497 | call assert_equal('/', g:left) |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1498 | bwipe! |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1499 | au! CmdlineEnter |
| 1500 | au! CmdlineLeave |
Bram Moolenaar | a4baf5b | 2018-04-22 13:27:44 +0200 | [diff] [blame] | 1501 | let &shellslash = save_shellslash |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 1502 | endfunc |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1503 | |
| 1504 | " Test for BufWritePre autocommand that deletes or unloads the buffer. |
| 1505 | func Test_BufWritePre() |
| 1506 | %bwipe |
| 1507 | au BufWritePre Xxx1 bunload |
| 1508 | au BufWritePre Xxx2 bwipe |
| 1509 | |
| 1510 | call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1') |
| 1511 | call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2') |
| 1512 | |
| 1513 | edit Xtest |
| 1514 | e! Xxx2 |
| 1515 | bdel Xtest |
| 1516 | e Xxx1 |
| 1517 | " write it, will unload it and give an error msg |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1518 | call assert_fails('w', 'E203:') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1519 | call assert_equal('Xxx2', bufname('%')) |
| 1520 | edit Xtest |
| 1521 | e! Xxx2 |
| 1522 | bwipe Xtest |
| 1523 | " write it, will delete the buffer and give an error msg |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1524 | call assert_fails('w', 'E203:') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1525 | call assert_equal('Xxx1', bufname('%')) |
| 1526 | au! BufWritePre |
| 1527 | call delete('Xxx1') |
| 1528 | call delete('Xxx2') |
| 1529 | endfunc |
| 1530 | |
| 1531 | " Test for BufUnload autocommand that unloads all the other buffers |
| 1532 | func Test_bufunload_all() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 1533 | let g:test_is_flaky = 1 |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1534 | call writefile(['Test file Xxx1'], 'Xxx1')" |
| 1535 | call writefile(['Test file Xxx2'], 'Xxx2')" |
| 1536 | |
Bram Moolenaar | c79745a | 2019-05-20 22:12:34 +0200 | [diff] [blame] | 1537 | let content =<< trim [CODE] |
| 1538 | func UnloadAllBufs() |
| 1539 | let i = 1 |
| 1540 | while i <= bufnr('$') |
| 1541 | if i != bufnr('%') && bufloaded(i) |
| 1542 | exe i . 'bunload' |
| 1543 | endif |
| 1544 | let i += 1 |
| 1545 | endwhile |
| 1546 | endfunc |
| 1547 | au BufUnload * call UnloadAllBufs() |
| 1548 | au VimLeave * call writefile(['Test Finished'], 'Xout') |
| 1549 | edit Xxx1 |
| 1550 | split Xxx2 |
| 1551 | q |
| 1552 | [CODE] |
| 1553 | |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1554 | call writefile(content, 'Xtest') |
| 1555 | |
| 1556 | call delete('Xout') |
Bram Moolenaar | 93344c2 | 2019-08-14 21:12:05 +0200 | [diff] [blame] | 1557 | call system(GetVimCommandClean() .. ' -N --not-a-term -S Xtest') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 1558 | call assert_true(filereadable('Xout')) |
| 1559 | |
| 1560 | call delete('Xxx1') |
| 1561 | call delete('Xxx2') |
| 1562 | call delete('Xtest') |
| 1563 | call delete('Xout') |
| 1564 | endfunc |
| 1565 | |
| 1566 | " Some tests for buffer-local autocommands |
| 1567 | func Test_buflocal_autocmd() |
| 1568 | let g:bname = '' |
| 1569 | edit xx |
| 1570 | au BufLeave <buffer> let g:bname = expand("%") |
| 1571 | " here, autocommand for xx should trigger. |
| 1572 | " but autocommand shall not apply to buffer named <buffer>. |
| 1573 | edit somefile |
| 1574 | call assert_equal('xx', g:bname) |
| 1575 | let g:bname = '' |
| 1576 | " here, autocommand shall be auto-deleted |
| 1577 | bwipe xx |
| 1578 | " autocmd should not trigger |
| 1579 | edit xx |
| 1580 | call assert_equal('', g:bname) |
| 1581 | " autocmd should not trigger |
| 1582 | edit somefile |
| 1583 | call assert_equal('', g:bname) |
| 1584 | enew |
| 1585 | unlet g:bname |
| 1586 | endfunc |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1587 | |
| 1588 | " Test for "*Cmd" autocommands |
| 1589 | func Test_Cmd_Autocmds() |
| 1590 | call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx') |
| 1591 | |
| 1592 | enew! |
| 1593 | au BufReadCmd XtestA 0r Xxx|$del |
| 1594 | edit XtestA " will read text of Xxd instead |
| 1595 | call assert_equal('start of Xxx', getline(1)) |
| 1596 | |
| 1597 | au BufWriteCmd XtestA call append(line("$"), "write") |
| 1598 | write " will append a line to the file |
| 1599 | call assert_equal('write', getline('$')) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1600 | call assert_fails('read XtestA', 'E484:') " should not read anything |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1601 | call assert_equal('write', getline(4)) |
| 1602 | |
| 1603 | " now we have: |
| 1604 | " 1 start of Xxx |
| 1605 | " 2 abc2 |
| 1606 | " 3 end of Xxx |
| 1607 | " 4 write |
| 1608 | |
| 1609 | au FileReadCmd XtestB '[r Xxx |
| 1610 | 2r XtestB " will read Xxx below line 2 instead |
| 1611 | call assert_equal('start of Xxx', getline(3)) |
| 1612 | |
| 1613 | " now we have: |
| 1614 | " 1 start of Xxx |
| 1615 | " 2 abc2 |
| 1616 | " 3 start of Xxx |
| 1617 | " 4 abc2 |
| 1618 | " 5 end of Xxx |
| 1619 | " 6 end of Xxx |
| 1620 | " 7 write |
| 1621 | |
| 1622 | au FileWriteCmd XtestC '[,']copy $ |
| 1623 | normal 4GA1 |
| 1624 | 4,5w XtestC " will copy lines 4 and 5 to the end |
| 1625 | call assert_equal("\tabc21", getline(8)) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1626 | call assert_fails('r XtestC', 'E484:') " should not read anything |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1627 | call assert_equal("end of Xxx", getline(9)) |
| 1628 | |
| 1629 | " now we have: |
| 1630 | " 1 start of Xxx |
| 1631 | " 2 abc2 |
| 1632 | " 3 start of Xxx |
| 1633 | " 4 abc21 |
| 1634 | " 5 end of Xxx |
| 1635 | " 6 end of Xxx |
| 1636 | " 7 write |
| 1637 | " 8 abc21 |
| 1638 | " 9 end of Xxx |
| 1639 | |
| 1640 | let g:lines = [] |
| 1641 | au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']"))) |
| 1642 | w >>XtestD " will add lines to 'lines' |
| 1643 | call assert_equal(9, len(g:lines)) |
Bram Moolenaar | e2e4075 | 2020-09-04 21:18:46 +0200 | [diff] [blame] | 1644 | call assert_fails('$r XtestD', 'E484:') " should not read anything |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 1645 | call assert_equal(9, line('$')) |
| 1646 | call assert_equal('end of Xxx', getline('$')) |
| 1647 | |
| 1648 | au BufReadCmd XtestE 0r Xxx|$del |
| 1649 | sp XtestE " split window with test.out |
| 1650 | call assert_equal('end of Xxx', getline(3)) |
| 1651 | |
| 1652 | let g:lines = [] |
| 1653 | exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>" |
| 1654 | au BufWriteCmd XtestE call extend(g:lines, getline(0, '$')) |
| 1655 | wall " will write other window to 'lines' |
| 1656 | call assert_equal(4, len(g:lines), g:lines) |
| 1657 | call assert_equal('asdf', g:lines[2]) |
| 1658 | |
| 1659 | au! BufReadCmd |
| 1660 | au! BufWriteCmd |
| 1661 | au! FileReadCmd |
| 1662 | au! FileWriteCmd |
| 1663 | au! FileAppendCmd |
| 1664 | %bwipe! |
| 1665 | call delete('Xxx') |
| 1666 | enew! |
| 1667 | endfunc |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1668 | |
Bram Moolenaar | 0fff441 | 2020-03-29 16:06:29 +0200 | [diff] [blame] | 1669 | func s:ReadFile() |
| 1670 | setl noswapfile nomodified |
| 1671 | let filename = resolve(expand("<afile>:p")) |
| 1672 | execute 'read' fnameescape(filename) |
| 1673 | 1d_ |
| 1674 | exe 'file' fnameescape(filename) |
| 1675 | setl buftype=acwrite |
| 1676 | endfunc |
| 1677 | |
| 1678 | func s:WriteFile() |
| 1679 | let filename = resolve(expand("<afile>:p")) |
| 1680 | setl buftype= |
| 1681 | noautocmd execute 'write' fnameescape(filename) |
| 1682 | setl buftype=acwrite |
| 1683 | setl nomodified |
| 1684 | endfunc |
| 1685 | |
| 1686 | func Test_BufReadCmd() |
| 1687 | autocmd BufReadCmd *.test call s:ReadFile() |
| 1688 | autocmd BufWriteCmd *.test call s:WriteFile() |
| 1689 | |
| 1690 | call writefile(['one', 'two', 'three'], 'Xcmd.test') |
| 1691 | edit Xcmd.test |
| 1692 | call assert_match('Xcmd.test" line 1 of 3', execute('file')) |
| 1693 | normal! Gofour |
| 1694 | write |
| 1695 | call assert_equal(['one', 'two', 'three', 'four'], readfile('Xcmd.test')) |
| 1696 | |
| 1697 | bwipe! |
| 1698 | call delete('Xcmd.test') |
| 1699 | au! BufReadCmd |
| 1700 | au! BufWriteCmd |
| 1701 | endfunc |
| 1702 | |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1703 | func SetChangeMarks(start, end) |
Bram Moolenaar | 97c6943 | 2021-01-15 16:45:21 +0100 | [diff] [blame] | 1704 | exe a:start .. 'mark [' |
| 1705 | exe a:end .. 'mark ]' |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1706 | endfunc |
| 1707 | |
| 1708 | " Verify the effects of autocmds on '[ and '] |
| 1709 | func Test_change_mark_in_autocmds() |
| 1710 | edit! Xtest |
Bram Moolenaar | 97c6943 | 2021-01-15 16:45:21 +0100 | [diff] [blame] | 1711 | 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] | 1712 | |
| 1713 | call SetChangeMarks(2, 3) |
| 1714 | write |
| 1715 | call assert_equal([1, 4], [line("'["), line("']")]) |
| 1716 | |
| 1717 | call SetChangeMarks(2, 3) |
| 1718 | au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1719 | write |
| 1720 | au! BufWritePre |
| 1721 | |
Bram Moolenaar | 14ddd22 | 2020-08-05 12:02:40 +0200 | [diff] [blame] | 1722 | if has('unix') |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1723 | write XtestFilter |
| 1724 | write >> XtestFilter |
| 1725 | |
| 1726 | call SetChangeMarks(2, 3) |
| 1727 | " Marks are set to the entire range of the write |
| 1728 | au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1729 | " '[ is adjusted to just before the line that will receive the filtered |
| 1730 | " data |
| 1731 | au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")]) |
| 1732 | " The filtered data is read into the buffer, and the source lines are |
| 1733 | " still present, so the range is after the source lines |
| 1734 | au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")]) |
| 1735 | %!cat XtestFilter |
| 1736 | " After the filtered data is read, the original lines are deleted |
| 1737 | call assert_equal([1, 8], [line("'["), line("']")]) |
| 1738 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 1739 | undo |
| 1740 | |
| 1741 | call SetChangeMarks(1, 4) |
| 1742 | au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1743 | au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")]) |
| 1744 | au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")]) |
| 1745 | 2,3!cat XtestFilter |
| 1746 | call assert_equal([2, 9], [line("'["), line("']")]) |
| 1747 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 1748 | undo |
| 1749 | |
| 1750 | call delete('XtestFilter') |
| 1751 | endif |
| 1752 | |
| 1753 | call SetChangeMarks(1, 4) |
| 1754 | au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1755 | 2,3write Xtest2 |
| 1756 | au! FileWritePre |
| 1757 | |
| 1758 | call SetChangeMarks(2, 3) |
| 1759 | au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1760 | write >> Xtest2 |
| 1761 | au! FileAppendPre |
| 1762 | |
| 1763 | call SetChangeMarks(1, 4) |
| 1764 | au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1765 | 2,3write >> Xtest2 |
| 1766 | au! FileAppendPre |
| 1767 | |
| 1768 | call SetChangeMarks(1, 1) |
| 1769 | au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")]) |
| 1770 | au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")]) |
| 1771 | 3read Xtest2 |
| 1772 | au! FileReadPre,FileReadPost |
| 1773 | undo |
| 1774 | |
| 1775 | call SetChangeMarks(4, 4) |
| 1776 | " When the line is 0, it's adjusted to 1 |
| 1777 | au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1778 | au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")]) |
| 1779 | 0read Xtest2 |
| 1780 | au! FileReadPre,FileReadPost |
| 1781 | undo |
| 1782 | |
| 1783 | call SetChangeMarks(4, 4) |
| 1784 | " When the line is 0, it's adjusted to 1 |
| 1785 | au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1786 | au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")]) |
| 1787 | 1read Xtest2 |
| 1788 | au! FileReadPre,FileReadPost |
| 1789 | undo |
| 1790 | |
| 1791 | bwipe! |
| 1792 | call delete('Xtest') |
| 1793 | call delete('Xtest2') |
| 1794 | endfunc |
| 1795 | |
| 1796 | func Test_Filter_noshelltemp() |
Bram Moolenaar | aeb313f | 2020-11-27 19:13:28 +0100 | [diff] [blame] | 1797 | CheckExecutable cat |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1798 | |
| 1799 | enew! |
| 1800 | call setline(1, ['a', 'b', 'c', 'd']) |
| 1801 | |
| 1802 | let shelltemp = &shelltemp |
| 1803 | set shelltemp |
| 1804 | |
| 1805 | let g:filter_au = 0 |
| 1806 | au FilterWritePre * let g:filter_au += 1 |
| 1807 | au FilterReadPre * let g:filter_au += 1 |
| 1808 | au FilterReadPost * let g:filter_au += 1 |
| 1809 | %!cat |
| 1810 | call assert_equal(3, g:filter_au) |
| 1811 | |
| 1812 | if has('filterpipe') |
| 1813 | set noshelltemp |
| 1814 | |
| 1815 | let g:filter_au = 0 |
| 1816 | au FilterWritePre * let g:filter_au += 1 |
| 1817 | au FilterReadPre * let g:filter_au += 1 |
| 1818 | au FilterReadPost * let g:filter_au += 1 |
| 1819 | %!cat |
| 1820 | call assert_equal(0, g:filter_au) |
| 1821 | endif |
| 1822 | |
| 1823 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 1824 | let &shelltemp = shelltemp |
| 1825 | bwipe! |
| 1826 | endfunc |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1827 | |
| 1828 | func Test_TextYankPost() |
| 1829 | enew! |
| 1830 | call setline(1, ['foo']) |
| 1831 | |
| 1832 | let g:event = [] |
| 1833 | au TextYankPost * let g:event = copy(v:event) |
| 1834 | |
| 1835 | call assert_equal({}, v:event) |
| 1836 | call assert_fails('let v:event = {}', 'E46:') |
| 1837 | call assert_fails('let v:event.mykey = 0', 'E742:') |
| 1838 | |
| 1839 | norm "ayiw |
| 1840 | call assert_equal( |
Bram Moolenaar | 37d1673 | 2020-06-12 22:09:01 +0200 | [diff] [blame] | 1841 | \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v', 'visual': v:false}, |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1842 | \g:event) |
| 1843 | norm y_ |
| 1844 | call assert_equal( |
Bram Moolenaar | 37d1673 | 2020-06-12 22:09:01 +0200 | [diff] [blame] | 1845 | \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:false}, |
| 1846 | \g:event) |
| 1847 | norm Vy |
| 1848 | call assert_equal( |
| 1849 | \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V', 'visual': v:true}, |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1850 | \g:event) |
| 1851 | call feedkeys("\<C-V>y", 'x') |
| 1852 | call assert_equal( |
Bram Moolenaar | 37d1673 | 2020-06-12 22:09:01 +0200 | [diff] [blame] | 1853 | \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161", 'visual': v:true}, |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1854 | \g:event) |
| 1855 | norm "xciwbar |
| 1856 | call assert_equal( |
Bram Moolenaar | 37d1673 | 2020-06-12 22:09:01 +0200 | [diff] [blame] | 1857 | \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v', 'visual': v:false}, |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1858 | \g:event) |
| 1859 | norm "bdiw |
| 1860 | call assert_equal( |
Bram Moolenaar | 37d1673 | 2020-06-12 22:09:01 +0200 | [diff] [blame] | 1861 | \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v', 'visual': v:false}, |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1862 | \g:event) |
| 1863 | |
| 1864 | call assert_equal({}, v:event) |
| 1865 | |
Bram Moolenaar | fccbf06 | 2020-11-26 20:34:00 +0100 | [diff] [blame] | 1866 | if has('clipboard_working') && !has('gui_running') |
| 1867 | " Test that when the visual selection is automatically copied to clipboard |
| 1868 | " register a TextYankPost is emitted |
| 1869 | call setline(1, ['foobar']) |
| 1870 | |
| 1871 | let @* = '' |
| 1872 | set clipboard=autoselect |
| 1873 | exe "norm! ggviw\<Esc>" |
| 1874 | call assert_equal( |
| 1875 | \{'regcontents': ['foobar'], 'regname': '*', 'operator': 'y', 'regtype': 'v', 'visual': v:true}, |
| 1876 | \g:event) |
| 1877 | |
| 1878 | let @+ = '' |
| 1879 | set clipboard=autoselectplus |
| 1880 | exe "norm! ggviw\<Esc>" |
| 1881 | call assert_equal( |
| 1882 | \{'regcontents': ['foobar'], 'regname': '+', 'operator': 'y', 'regtype': 'v', 'visual': v:true}, |
| 1883 | \g:event) |
| 1884 | |
| 1885 | set clipboard&vim |
| 1886 | endif |
| 1887 | |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1888 | au! TextYankPost |
| 1889 | unlet g:event |
| 1890 | bwipe! |
| 1891 | endfunc |
Bram Moolenaar | 9bca805 | 2017-12-18 12:37:55 +0100 | [diff] [blame] | 1892 | |
Bram Moolenaar | 9a046fd | 2021-01-28 13:47:59 +0100 | [diff] [blame] | 1893 | func Test_autocommand_all_events() |
| 1894 | call assert_fails('au * * bwipe', 'E1155:') |
| 1895 | call assert_fails('au * x bwipe', 'E1155:') |
Bram Moolenaar | 4fb921e | 2017-12-18 15:33:00 +0100 | [diff] [blame] | 1896 | endfunc |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1897 | |
| 1898 | function s:Before_test_dirchanged() |
| 1899 | augroup test_dirchanged |
| 1900 | autocmd! |
| 1901 | augroup END |
| 1902 | let s:li = [] |
| 1903 | let s:dir_this = getcwd() |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 1904 | let s:dir_foo = s:dir_this . '/Xfoo' |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1905 | call mkdir(s:dir_foo) |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 1906 | let s:dir_bar = s:dir_this . '/Xbar' |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1907 | call mkdir(s:dir_bar) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1908 | endfunc |
| 1909 | |
| 1910 | function s:After_test_dirchanged() |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1911 | call chdir(s:dir_this) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1912 | call delete(s:dir_foo, 'd') |
| 1913 | call delete(s:dir_bar, 'd') |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1914 | augroup test_dirchanged |
| 1915 | autocmd! |
| 1916 | augroup END |
| 1917 | endfunc |
| 1918 | |
| 1919 | function Test_dirchanged_global() |
| 1920 | call s:Before_test_dirchanged() |
| 1921 | autocmd test_dirchanged DirChanged global call add(s:li, "cd:") |
| 1922 | autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>")) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1923 | call chdir(s:dir_foo) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1924 | call assert_equal(["cd:", s:dir_foo], s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1925 | call chdir(s:dir_foo) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1926 | call assert_equal(["cd:", s:dir_foo], s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1927 | exe 'lcd ' .. fnameescape(s:dir_bar) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1928 | call assert_equal(["cd:", s:dir_foo], s:li) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1929 | call s:After_test_dirchanged() |
| 1930 | endfunc |
| 1931 | |
| 1932 | function Test_dirchanged_local() |
| 1933 | call s:Before_test_dirchanged() |
| 1934 | autocmd test_dirchanged DirChanged window call add(s:li, "lcd:") |
| 1935 | autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>")) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1936 | call chdir(s:dir_foo) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1937 | call assert_equal([], s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1938 | exe 'lcd ' .. fnameescape(s:dir_bar) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1939 | call assert_equal(["lcd:", s:dir_bar], s:li) |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1940 | exe 'lcd ' .. fnameescape(s:dir_bar) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1941 | call assert_equal(["lcd:", s:dir_bar], s:li) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1942 | call s:After_test_dirchanged() |
| 1943 | endfunc |
| 1944 | |
| 1945 | function Test_dirchanged_auto() |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1946 | CheckOption autochdir |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1947 | call s:Before_test_dirchanged() |
| 1948 | call test_autochdir() |
| 1949 | autocmd test_dirchanged DirChanged auto call add(s:li, "auto:") |
| 1950 | autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>")) |
| 1951 | set acd |
Bram Moolenaar | 3503d7c | 2019-11-09 20:10:17 +0100 | [diff] [blame] | 1952 | cd .. |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1953 | call assert_equal([], s:li) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1954 | exe 'edit ' . s:dir_foo . '/Xfile' |
| 1955 | call assert_equal(s:dir_foo, getcwd()) |
| 1956 | call assert_equal(["auto:", s:dir_foo], s:li) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1957 | set noacd |
| 1958 | bwipe! |
| 1959 | call s:After_test_dirchanged() |
| 1960 | endfunc |
Bram Moolenaar | 5a09343 | 2018-02-10 18:15:19 +0100 | [diff] [blame] | 1961 | |
| 1962 | " Test TextChangedI and TextChangedP |
| 1963 | func Test_ChangedP() |
| 1964 | new |
| 1965 | call setline(1, ['foo', 'bar', 'foobar']) |
| 1966 | call test_override("char_avail", 1) |
| 1967 | set complete=. completeopt=menuone |
| 1968 | |
| 1969 | func! TextChangedAutocmd(char) |
| 1970 | let g:autocmd .= a:char |
| 1971 | endfunc |
| 1972 | |
Christian Brabandt | db3b446 | 2021-10-16 11:58:55 +0100 | [diff] [blame] | 1973 | " TextChanged will not be triggered, only check that it isn't. |
Bram Moolenaar | 5a09343 | 2018-02-10 18:15:19 +0100 | [diff] [blame] | 1974 | au! TextChanged <buffer> :call TextChangedAutocmd('N') |
| 1975 | au! TextChangedI <buffer> :call TextChangedAutocmd('I') |
| 1976 | au! TextChangedP <buffer> :call TextChangedAutocmd('P') |
| 1977 | |
| 1978 | call cursor(3, 1) |
| 1979 | let g:autocmd = '' |
| 1980 | call feedkeys("o\<esc>", 'tnix') |
| 1981 | call assert_equal('I', g:autocmd) |
| 1982 | |
| 1983 | let g:autocmd = '' |
| 1984 | call feedkeys("Sf", 'tnix') |
| 1985 | call assert_equal('II', g:autocmd) |
| 1986 | |
| 1987 | let g:autocmd = '' |
| 1988 | call feedkeys("Sf\<C-N>", 'tnix') |
| 1989 | call assert_equal('IIP', g:autocmd) |
| 1990 | |
| 1991 | let g:autocmd = '' |
| 1992 | call feedkeys("Sf\<C-N>\<C-N>", 'tnix') |
| 1993 | call assert_equal('IIPP', g:autocmd) |
| 1994 | |
| 1995 | let g:autocmd = '' |
| 1996 | call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix') |
| 1997 | call assert_equal('IIPPP', g:autocmd) |
| 1998 | |
| 1999 | let g:autocmd = '' |
| 2000 | call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix') |
| 2001 | call assert_equal('IIPPPP', g:autocmd) |
| 2002 | |
| 2003 | call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$')) |
| 2004 | " TODO: how should it handle completeopt=noinsert,noselect? |
| 2005 | |
| 2006 | " CleanUp |
| 2007 | call test_override("char_avail", 0) |
| 2008 | au! TextChanged |
| 2009 | au! TextChangedI |
| 2010 | au! TextChangedP |
| 2011 | delfu TextChangedAutocmd |
| 2012 | unlet! g:autocmd |
| 2013 | set complete&vim completeopt&vim |
| 2014 | |
| 2015 | bw! |
| 2016 | endfunc |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2017 | |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 2018 | let g:setline_handled = v:false |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 2019 | func SetLineOne() |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 2020 | if !g:setline_handled |
| 2021 | call setline(1, "(x)") |
| 2022 | let g:setline_handled = v:true |
| 2023 | endif |
| 2024 | endfunc |
| 2025 | |
| 2026 | func Test_TextChangedI_with_setline() |
| 2027 | new |
| 2028 | call test_override('char_avail', 1) |
| 2029 | autocmd TextChangedI <buffer> call SetLineOne() |
| 2030 | call feedkeys("i(\<CR>\<Esc>", 'tx') |
| 2031 | call assert_equal('(', getline(1)) |
| 2032 | call assert_equal('x)', getline(2)) |
| 2033 | undo |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 2034 | call assert_equal('', getline(1)) |
Bram Moolenaar | 9fa9506 | 2018-08-08 22:08:32 +0200 | [diff] [blame] | 2035 | call assert_equal('', getline(2)) |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 2036 | |
| 2037 | call test_override('starting', 0) |
| 2038 | bwipe! |
| 2039 | endfunc |
| 2040 | |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2041 | func Test_Changed_FirstTime() |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 2042 | CheckFeature terminal |
| 2043 | CheckNotGui |
Bram Moolenaar | 3cdcb09 | 2020-03-18 19:18:10 +0100 | [diff] [blame] | 2044 | " Starting a terminal to run Vim is always considered flaky. |
Bram Moolenaar | 30d53e2 | 2020-03-18 21:10:44 +0100 | [diff] [blame] | 2045 | let g:test_is_flaky = 1 |
Bram Moolenaar | 8c5a278 | 2019-08-07 23:07:07 +0200 | [diff] [blame] | 2046 | |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2047 | " Prepare file for TextChanged event. |
| 2048 | call writefile([''], 'Xchanged.txt') |
| 2049 | let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3}) |
| 2050 | call assert_equal('running', term_getstatus(buf)) |
Bram Moolenaar | 1834d37 | 2018-03-29 17:40:46 +0200 | [diff] [blame] | 2051 | " Wait for the ruler (in the status line) to be shown. |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 2052 | " In ConPTY, there is additional character which is drawn up to the width of |
| 2053 | " the screen. |
| 2054 | if has('conpty') |
| 2055 | call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))}) |
| 2056 | else |
| 2057 | call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))}) |
| 2058 | endif |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2059 | " It's only adding autocmd, so that no event occurs. |
| 2060 | call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>") |
| 2061 | call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>") |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 2062 | call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))}) |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 2063 | call assert_equal([''], readfile('Xchanged.txt')) |
| 2064 | |
| 2065 | " clean up |
| 2066 | call delete('Xchanged.txt') |
| 2067 | bwipe! |
| 2068 | endfunc |
Bram Moolenaar | 0566e89 | 2019-01-24 19:37:40 +0100 | [diff] [blame] | 2069 | |
Bram Moolenaar | eb93f3f | 2019-04-04 15:04:56 +0200 | [diff] [blame] | 2070 | func Test_autocmd_nested() |
| 2071 | let g:did_nested = 0 |
| 2072 | augroup Testing |
| 2073 | au WinNew * edit somefile |
| 2074 | au BufNew * let g:did_nested = 1 |
| 2075 | augroup END |
| 2076 | split |
| 2077 | call assert_equal(0, g:did_nested) |
| 2078 | close |
| 2079 | bwipe! somefile |
| 2080 | |
| 2081 | " old nested argument still works |
| 2082 | augroup Testing |
| 2083 | au! |
| 2084 | au WinNew * nested edit somefile |
| 2085 | au BufNew * let g:did_nested = 1 |
| 2086 | augroup END |
| 2087 | split |
| 2088 | call assert_equal(1, g:did_nested) |
| 2089 | close |
| 2090 | bwipe! somefile |
| 2091 | |
| 2092 | " New ++nested argument works |
| 2093 | augroup Testing |
| 2094 | au! |
| 2095 | au WinNew * ++nested edit somefile |
| 2096 | au BufNew * let g:did_nested = 1 |
| 2097 | augroup END |
| 2098 | split |
| 2099 | call assert_equal(1, g:did_nested) |
| 2100 | close |
| 2101 | bwipe! somefile |
| 2102 | |
| 2103 | augroup Testing |
| 2104 | au! |
| 2105 | augroup END |
| 2106 | |
| 2107 | call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:') |
| 2108 | call assert_fails('au WinNew * nested nested echo bad', 'E983:') |
| 2109 | endfunc |
| 2110 | |
| 2111 | func Test_autocmd_once() |
| 2112 | " Without ++once WinNew triggers twice |
| 2113 | let g:did_split = 0 |
| 2114 | augroup Testing |
| 2115 | au WinNew * let g:did_split += 1 |
| 2116 | augroup END |
| 2117 | split |
| 2118 | split |
| 2119 | call assert_equal(2, g:did_split) |
| 2120 | call assert_true(exists('#WinNew')) |
| 2121 | close |
| 2122 | close |
| 2123 | |
| 2124 | " With ++once WinNew triggers once |
| 2125 | let g:did_split = 0 |
| 2126 | augroup Testing |
| 2127 | au! |
| 2128 | au WinNew * ++once let g:did_split += 1 |
| 2129 | augroup END |
| 2130 | split |
| 2131 | split |
| 2132 | call assert_equal(1, g:did_split) |
| 2133 | call assert_false(exists('#WinNew')) |
| 2134 | close |
| 2135 | close |
| 2136 | |
| 2137 | call assert_fails('au WinNew * ++once ++once echo bad', 'E983:') |
| 2138 | endfunc |
| 2139 | |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2140 | func Test_autocmd_bufreadpre() |
| 2141 | new |
| 2142 | let b:bufreadpre = 1 |
Bram Moolenaar | ab505b1 | 2020-03-23 19:28:44 +0100 | [diff] [blame] | 2143 | call append(0, range(1000)) |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2144 | w! XAutocmdBufReadPre.txt |
| 2145 | autocmd BufReadPre <buffer> :let b:bufreadpre += 1 |
Bram Moolenaar | ab505b1 | 2020-03-23 19:28:44 +0100 | [diff] [blame] | 2146 | norm! 500gg |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2147 | sp |
Bram Moolenaar | ab505b1 | 2020-03-23 19:28:44 +0100 | [diff] [blame] | 2148 | norm! 1000gg |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 2149 | wincmd p |
| 2150 | let g:wsv1 = winsaveview() |
| 2151 | wincmd p |
| 2152 | let g:wsv2 = winsaveview() |
| 2153 | " triggers BufReadPre, should not move the cursor in either window |
| 2154 | " The topline may change one line in a large window. |
| 2155 | edit |
| 2156 | call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline) |
| 2157 | call assert_equal(g:wsv2.lnum, winsaveview().lnum) |
| 2158 | call assert_equal(2, b:bufreadpre) |
| 2159 | wincmd p |
| 2160 | call assert_equal(g:wsv1.topline, winsaveview().topline) |
| 2161 | call assert_equal(g:wsv1.lnum, winsaveview().lnum) |
| 2162 | call assert_equal(2, b:bufreadpre) |
| 2163 | " Now set the cursor position in an BufReadPre autocommand |
| 2164 | " (even though the position will be invalid, this should make Vim reset the |
| 2165 | " cursor position in the other window. |
| 2166 | wincmd p |
| 2167 | set cpo+=g |
| 2168 | " won't do anything, but try to set the cursor on an invalid lnum |
| 2169 | autocmd BufReadPre <buffer> :norm! 70gg |
| 2170 | " triggers BufReadPre, should not move the cursor in either window |
| 2171 | e |
| 2172 | call assert_equal(1, winsaveview().topline) |
| 2173 | call assert_equal(1, winsaveview().lnum) |
| 2174 | call assert_equal(3, b:bufreadpre) |
| 2175 | wincmd p |
| 2176 | call assert_equal(g:wsv1.topline, winsaveview().topline) |
| 2177 | call assert_equal(g:wsv1.lnum, winsaveview().lnum) |
| 2178 | call assert_equal(3, b:bufreadpre) |
| 2179 | close |
| 2180 | close |
| 2181 | call delete('XAutocmdBufReadPre.txt') |
| 2182 | set cpo-=g |
| 2183 | endfunc |
| 2184 | |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 2185 | " FileChangedShell tested in test_filechanged.vim |
Bram Moolenaar | 69ea587 | 2019-04-25 20:29:00 +0200 | [diff] [blame] | 2186 | |
| 2187 | " Tests for the following autocommands: |
| 2188 | " - FileWritePre writing a compressed file |
| 2189 | " - FileReadPost reading a compressed file |
| 2190 | " - BufNewFile reading a file template |
| 2191 | " - BufReadPre decompressing the file to be read |
| 2192 | " - FilterReadPre substituting characters in the temp file |
| 2193 | " - FilterReadPost substituting characters after filtering |
| 2194 | " - FileReadPre set options for decompression |
| 2195 | " - FileReadPost decompress the file |
| 2196 | func Test_ReadWrite_Autocmds() |
| 2197 | " 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] | 2198 | CheckUnix |
| 2199 | CheckExecutable gzip |
Bram Moolenaar | 69ea587 | 2019-04-25 20:29:00 +0200 | [diff] [blame] | 2200 | |
| 2201 | " Make $GZIP empty, "-v" would cause trouble. |
| 2202 | let $GZIP = "" |
| 2203 | |
| 2204 | " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz' |
| 2205 | " being modified outside of Vim (noticed on Solaris). |
| 2206 | au FileChangedShell * echo 'caught FileChangedShell' |
| 2207 | |
| 2208 | " Test for the FileReadPost, FileWritePre and FileWritePost autocmds |
| 2209 | augroup Test1 |
| 2210 | au! |
| 2211 | au FileWritePre *.gz '[,']!gzip |
| 2212 | au FileWritePost *.gz undo |
| 2213 | au FileReadPost *.gz '[,']!gzip -d |
| 2214 | augroup END |
| 2215 | |
| 2216 | new |
| 2217 | set bin |
| 2218 | call append(0, [ |
| 2219 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2220 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2221 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2222 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2223 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2224 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2225 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2226 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2227 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2228 | \ ]) |
| 2229 | 1,9write! Xtestfile.gz |
| 2230 | enew! | close |
| 2231 | |
| 2232 | new |
| 2233 | " Read and decompress the testfile |
| 2234 | 0read Xtestfile.gz |
| 2235 | call assert_equal([ |
| 2236 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2237 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2238 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2239 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2240 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2241 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2242 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2243 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2244 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2245 | \ ], getline(1, 9)) |
| 2246 | enew! | close |
| 2247 | |
| 2248 | augroup Test1 |
| 2249 | au! |
| 2250 | augroup END |
| 2251 | |
| 2252 | " Test for the FileAppendPre and FileAppendPost autocmds |
| 2253 | augroup Test2 |
| 2254 | au! |
| 2255 | au BufNewFile *.c read Xtest.c |
| 2256 | au FileAppendPre *.out '[,']s/new/NEW/ |
| 2257 | au FileAppendPost *.out !cat Xtest.c >> test.out |
| 2258 | augroup END |
| 2259 | |
| 2260 | call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c') |
| 2261 | new foo.c " should load Xtest.c |
| 2262 | call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4)) |
| 2263 | w! >> test.out " append it to the output file |
| 2264 | |
| 2265 | let contents = readfile('test.out') |
| 2266 | call assert_equal(' * Here is a NEW .c file', contents[2]) |
| 2267 | call assert_equal(' * Here is a new .c file', contents[5]) |
| 2268 | |
| 2269 | call delete('test.out') |
| 2270 | enew! | close |
| 2271 | augroup Test2 |
| 2272 | au! |
| 2273 | augroup END |
| 2274 | |
| 2275 | " Test for the BufReadPre and BufReadPost autocmds |
| 2276 | augroup Test3 |
| 2277 | au! |
| 2278 | " setup autocommands to decompress before reading and re-compress |
| 2279 | " afterwards |
| 2280 | au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>")) |
| 2281 | au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>")) |
| 2282 | au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r")) |
| 2283 | au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r")) |
| 2284 | augroup END |
| 2285 | |
| 2286 | e! Xtestfile.gz " Edit compressed file |
| 2287 | call assert_equal([ |
| 2288 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2289 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2290 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2291 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2292 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2293 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2294 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2295 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2296 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2297 | \ ], getline(1, 9)) |
| 2298 | |
| 2299 | w! >> test.out " Append it to the output file |
| 2300 | |
| 2301 | augroup Test3 |
| 2302 | au! |
| 2303 | augroup END |
| 2304 | |
| 2305 | " Test for the FilterReadPre and FilterReadPost autocmds. |
| 2306 | set shelltemp " need temp files here |
| 2307 | augroup Test4 |
| 2308 | au! |
| 2309 | au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t") |
| 2310 | au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>")) |
| 2311 | au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t' |
| 2312 | au FilterReadPost *.out '[,']s/x/X/g |
| 2313 | augroup END |
| 2314 | |
| 2315 | e! test.out " Edit the output file |
| 2316 | 1,$!cat |
| 2317 | call assert_equal([ |
| 2318 | \ 'linE 2 AbcdefghijklmnopqrstuvwXyz', |
| 2319 | \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 2320 | \ 'linE 4 AbcdefghijklmnopqrstuvwXyz', |
| 2321 | \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 2322 | \ 'linE 6 AbcdefghijklmnopqrstuvwXyz', |
| 2323 | \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 2324 | \ 'linE 8 AbcdefghijklmnopqrstuvwXyz', |
| 2325 | \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 2326 | \ 'linE 10 AbcdefghijklmnopqrstuvwXyz' |
| 2327 | \ ], getline(1, 9)) |
| 2328 | call assert_equal([ |
| 2329 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2330 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2331 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2332 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2333 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2334 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2335 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2336 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2337 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2338 | \ ], readfile('test.out')) |
| 2339 | |
| 2340 | augroup Test4 |
| 2341 | au! |
| 2342 | augroup END |
| 2343 | set shelltemp&vim |
| 2344 | |
| 2345 | " Test for the FileReadPre and FileReadPost autocmds. |
| 2346 | augroup Test5 |
| 2347 | au! |
| 2348 | au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>")) |
| 2349 | au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>")) |
| 2350 | au FileReadPost *.gz '[,']s/l/L/ |
| 2351 | augroup END |
| 2352 | |
| 2353 | new |
| 2354 | 0r Xtestfile.gz " Read compressed file |
| 2355 | call assert_equal([ |
| 2356 | \ 'Line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2357 | \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2358 | \ 'Line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2359 | \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2360 | \ 'Line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2361 | \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2362 | \ 'Line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2363 | \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2364 | \ 'Line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2365 | \ ], getline(1, 9)) |
| 2366 | call assert_equal([ |
| 2367 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 2368 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2369 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 2370 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2371 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 2372 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2373 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 2374 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 2375 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 2376 | \ ], readfile('Xtestfile.gz')) |
| 2377 | |
| 2378 | augroup Test5 |
| 2379 | au! |
| 2380 | augroup END |
| 2381 | |
| 2382 | au! FileChangedShell |
| 2383 | call delete('Xtestfile.gz') |
| 2384 | call delete('Xtest.c') |
| 2385 | call delete('test.out') |
| 2386 | endfunc |
Bram Moolenaar | 23b5139 | 2019-05-09 21:38:43 +0200 | [diff] [blame] | 2387 | |
| 2388 | func Test_throw_in_BufWritePre() |
| 2389 | new |
| 2390 | call setline(1, ['one', 'two', 'three']) |
| 2391 | call assert_false(filereadable('Xthefile')) |
| 2392 | augroup throwing |
| 2393 | au BufWritePre X* throw 'do not write' |
| 2394 | augroup END |
| 2395 | try |
| 2396 | w Xthefile |
| 2397 | catch |
| 2398 | let caught = 1 |
| 2399 | endtry |
| 2400 | call assert_equal(1, caught) |
| 2401 | call assert_false(filereadable('Xthefile')) |
| 2402 | |
| 2403 | bwipe! |
| 2404 | au! throwing |
| 2405 | endfunc |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2406 | |
Bram Moolenaar | 40fa12a | 2021-09-22 14:18:13 +0200 | [diff] [blame] | 2407 | func Test_autocmd_in_try_block() |
| 2408 | call mkdir('Xdir') |
| 2409 | au BufEnter * let g:fname = expand('%') |
| 2410 | try |
| 2411 | edit Xdir/ |
| 2412 | endtry |
| 2413 | call assert_match('Xdir', g:fname) |
| 2414 | |
| 2415 | unlet g:fname |
| 2416 | au! BufEnter |
| 2417 | call delete('Xdir', 'rf') |
| 2418 | endfunc |
| 2419 | |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2420 | func Test_autocmd_SafeState() |
| 2421 | CheckRunVimInTerminal |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 2422 | let g:test_is_flaky = 1 |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2423 | |
| 2424 | let lines =<< trim END |
| 2425 | let g:safe = 0 |
| 2426 | let g:again = '' |
| 2427 | au SafeState * let g:safe += 1 |
| 2428 | au SafeStateAgain * let g:again ..= 'x' |
| 2429 | func CallTimer() |
| 2430 | call timer_start(10, {id -> execute('let g:again ..= "t"')}) |
| 2431 | endfunc |
| 2432 | END |
| 2433 | call writefile(lines, 'XSafeState') |
| 2434 | let buf = RunVimInTerminal('-S XSafeState', #{rows: 6}) |
| 2435 | |
Bram Moolenaar | 8e7d622 | 2020-12-18 19:49:56 +0100 | [diff] [blame] | 2436 | " 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] | 2437 | " more often. |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2438 | call term_sendkeys(buf, ":echo g:safe\<CR>") |
Bram Moolenaar | 8fb1b47 | 2020-02-23 16:16:26 +0100 | [diff] [blame] | 2439 | call WaitForAssert({-> assert_match('^\d ', term_getline(buf, 6))}, 1000) |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2440 | |
Bram Moolenaar | 8fb1b47 | 2020-02-23 16:16:26 +0100 | [diff] [blame] | 2441 | " SafeStateAgain should be invoked at least three times |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2442 | call term_sendkeys(buf, ":echo g:again\<CR>") |
Bram Moolenaar | 8fb1b47 | 2020-02-23 16:16:26 +0100 | [diff] [blame] | 2443 | call WaitForAssert({-> assert_match('^xxx', term_getline(buf, 6))}, 1000) |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2444 | |
| 2445 | call term_sendkeys(buf, ":let g:again = ''\<CR>:call CallTimer()\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2446 | call TermWait(buf, 50) |
Bram Moolenaar | 0f6629a | 2019-09-22 23:24:13 +0200 | [diff] [blame] | 2447 | call term_sendkeys(buf, ":\<CR>") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2448 | call TermWait(buf, 50) |
Bram Moolenaar | cadbe1b | 2019-09-22 21:50:09 +0200 | [diff] [blame] | 2449 | call term_sendkeys(buf, ":echo g:again\<CR>") |
| 2450 | call WaitForAssert({-> assert_match('xtx', term_getline(buf, 6))}, 1000) |
| 2451 | |
| 2452 | call StopVimInTerminal(buf) |
| 2453 | call delete('XSafeState') |
| 2454 | endfunc |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2455 | |
| 2456 | func Test_autocmd_CmdWinEnter() |
| 2457 | CheckRunVimInTerminal |
Bram Moolenaar | 21829c5 | 2021-01-26 22:42:21 +0100 | [diff] [blame] | 2458 | CheckFeature cmdwin |
| 2459 | |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2460 | let lines =<< trim END |
Egor Zvorykin | 125ffd2 | 2021-11-17 14:01:14 +0000 | [diff] [blame] | 2461 | augroup vimHints | au! | augroup END |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2462 | let b:dummy_var = 'This is a dummy' |
| 2463 | autocmd CmdWinEnter * quit |
| 2464 | let winnr = winnr('$') |
| 2465 | END |
Bram Moolenaar | 1cfb9bb | 2020-12-22 11:40:45 +0100 | [diff] [blame] | 2466 | let filename = 'XCmdWinEnter' |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2467 | call writefile(lines, filename) |
| 2468 | let buf = RunVimInTerminal('-S '.filename, #{rows: 6}) |
| 2469 | |
| 2470 | call term_sendkeys(buf, "q:") |
Bram Moolenaar | 6a2c5a7 | 2020-04-08 21:50:25 +0200 | [diff] [blame] | 2471 | call TermWait(buf) |
Bram Moolenaar | 23324a0 | 2019-10-01 17:39:04 +0200 | [diff] [blame] | 2472 | call term_sendkeys(buf, ":echo b:dummy_var\<cr>") |
Bram Moolenaar | 353c351 | 2020-03-15 14:19:26 +0100 | [diff] [blame] | 2473 | 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] | 2474 | call term_sendkeys(buf, ":echo &buftype\<cr>") |
| 2475 | call WaitForAssert({-> assert_notmatch('^nofile', term_getline(buf, 6))}, 1000) |
| 2476 | call term_sendkeys(buf, ":echo winnr\<cr>") |
| 2477 | call WaitForAssert({-> assert_match('^1', term_getline(buf, 6))}, 1000) |
| 2478 | |
| 2479 | " clean up |
| 2480 | call StopVimInTerminal(buf) |
| 2481 | call delete(filename) |
| 2482 | endfunc |
Bram Moolenaar | ec66c41 | 2019-10-11 21:19:13 +0200 | [diff] [blame] | 2483 | |
| 2484 | func Test_autocmd_was_using_freed_memory() |
Bram Moolenaar | 5a4c308 | 2019-12-01 15:23:11 +0100 | [diff] [blame] | 2485 | CheckFeature quickfix |
| 2486 | |
Bram Moolenaar | ec66c41 | 2019-10-11 21:19:13 +0200 | [diff] [blame] | 2487 | pedit xx |
| 2488 | n x |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 2489 | augroup winenter |
| 2490 | au WinEnter * if winnr('$') > 2 | quit | endif |
| 2491 | augroup END |
Bram Moolenaar | ec66c41 | 2019-10-11 21:19:13 +0200 | [diff] [blame] | 2492 | split |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 2493 | |
| 2494 | augroup winenter |
| 2495 | au! WinEnter |
| 2496 | augroup END |
| 2497 | |
| 2498 | bwipe xx |
| 2499 | bwipe x |
| 2500 | pclose |
Bram Moolenaar | ec66c41 | 2019-10-11 21:19:13 +0200 | [diff] [blame] | 2501 | endfunc |
Bram Moolenaar | f4a1d1c | 2019-11-16 13:50:25 +0100 | [diff] [blame] | 2502 | |
| 2503 | func Test_BufWrite_lockmarks() |
Bram Moolenaar | f08b0eb | 2021-10-16 13:00:14 +0100 | [diff] [blame] | 2504 | let g:test_is_flaky = 1 |
Bram Moolenaar | f4a1d1c | 2019-11-16 13:50:25 +0100 | [diff] [blame] | 2505 | edit! Xtest |
| 2506 | call setline(1, ['a', 'b', 'c', 'd']) |
| 2507 | |
| 2508 | " :lockmarks preserves the marks |
| 2509 | call SetChangeMarks(2, 3) |
| 2510 | lockmarks write |
| 2511 | call assert_equal([2, 3], [line("'["), line("']")]) |
| 2512 | |
| 2513 | " *WritePre autocmds get the correct line range, but lockmarks preserves the |
| 2514 | " original values for the user |
| 2515 | augroup lockmarks |
| 2516 | au! |
| 2517 | au BufWritePre,FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 2518 | au FileWritePre * call assert_equal([3, 4], [line("'["), line("']")]) |
| 2519 | augroup END |
| 2520 | |
| 2521 | lockmarks write |
| 2522 | call assert_equal([2, 3], [line("'["), line("']")]) |
| 2523 | |
| 2524 | if executable('cat') |
| 2525 | lockmarks %!cat |
| 2526 | call assert_equal([2, 3], [line("'["), line("']")]) |
| 2527 | endif |
| 2528 | |
| 2529 | lockmarks 3,4write Xtest2 |
| 2530 | call assert_equal([2, 3], [line("'["), line("']")]) |
| 2531 | |
| 2532 | au! lockmarks |
| 2533 | augroup! lockmarks |
| 2534 | call delete('Xtest') |
| 2535 | call delete('Xtest2') |
| 2536 | endfunc |
Bram Moolenaar | ce6db02 | 2020-01-07 20:11:42 +0100 | [diff] [blame] | 2537 | |
| 2538 | func Test_FileType_spell() |
| 2539 | if !isdirectory('/tmp') |
| 2540 | throw "Skipped: requires /tmp directory" |
| 2541 | endif |
| 2542 | |
| 2543 | " this was crashing with an invalid free() |
| 2544 | setglobal spellfile=/tmp/en.utf-8.add |
| 2545 | augroup crash |
| 2546 | autocmd! |
| 2547 | autocmd BufNewFile,BufReadPost crashfile setf somefiletype |
| 2548 | autocmd BufNewFile,BufReadPost crashfile set ft=anotherfiletype |
| 2549 | autocmd FileType anotherfiletype setlocal spell |
| 2550 | augroup END |
| 2551 | func! NoCrash() abort |
| 2552 | edit /tmp/crashfile |
| 2553 | endfunc |
| 2554 | call NoCrash() |
| 2555 | |
| 2556 | au! crash |
| 2557 | setglobal spellfile= |
| 2558 | endfunc |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 2559 | |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 2560 | " Test closing a window or editing another buffer from a FileChangedRO handler |
| 2561 | " in a readonly buffer |
| 2562 | func Test_FileChangedRO_winclose() |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 2563 | call test_override('ui_delay', 10) |
| 2564 | |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 2565 | augroup FileChangedROTest |
| 2566 | au! |
| 2567 | autocmd FileChangedRO * quit |
| 2568 | augroup END |
| 2569 | new |
| 2570 | set readonly |
| 2571 | call assert_fails('normal i', 'E788:') |
| 2572 | close |
| 2573 | augroup! FileChangedROTest |
| 2574 | |
| 2575 | augroup FileChangedROTest |
| 2576 | au! |
| 2577 | autocmd FileChangedRO * edit Xfile |
| 2578 | augroup END |
| 2579 | new |
| 2580 | set readonly |
| 2581 | call assert_fails('normal i', 'E788:') |
| 2582 | close |
| 2583 | augroup! FileChangedROTest |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 2584 | call test_override('ALL', 0) |
Bram Moolenaar | 406cd90 | 2020-02-18 21:54:41 +0100 | [diff] [blame] | 2585 | endfunc |
| 2586 | |
Bram Moolenaar | 0c81d1b | 2020-02-22 22:45:55 +0100 | [diff] [blame] | 2587 | func LogACmd() |
| 2588 | call add(g:logged, line('$')) |
| 2589 | endfunc |
| 2590 | |
| 2591 | func Test_TermChanged() |
Bram Moolenaar | d28e0b3 | 2020-02-22 23:08:52 +0100 | [diff] [blame] | 2592 | CheckNotGui |
| 2593 | |
Bram Moolenaar | 0c81d1b | 2020-02-22 22:45:55 +0100 | [diff] [blame] | 2594 | enew! |
| 2595 | tabnew |
| 2596 | call setline(1, ['a', 'b', 'c', 'd']) |
| 2597 | $ |
| 2598 | au TermChanged * call LogACmd() |
| 2599 | let g:logged = [] |
| 2600 | let term_save = &term |
| 2601 | set term=xterm |
| 2602 | call assert_equal([1, 4], g:logged) |
| 2603 | |
| 2604 | au! TermChanged |
| 2605 | let &term = term_save |
| 2606 | bwipe! |
| 2607 | endfunc |
| 2608 | |
Bram Moolenaar | e328487 | 2020-03-19 13:55:03 +0100 | [diff] [blame] | 2609 | " Test for FileReadCmd autocmd |
| 2610 | func Test_autocmd_FileReadCmd() |
| 2611 | func ReadFileCmd() |
| 2612 | call append(line('$'), "v:cmdarg = " .. v:cmdarg) |
| 2613 | endfunc |
| 2614 | augroup FileReadCmdTest |
| 2615 | au! |
| 2616 | au FileReadCmd Xtest call ReadFileCmd() |
| 2617 | augroup END |
| 2618 | |
| 2619 | new |
| 2620 | read ++bin Xtest |
| 2621 | read ++nobin Xtest |
| 2622 | read ++edit Xtest |
| 2623 | read ++bad=keep Xtest |
| 2624 | read ++bad=drop Xtest |
| 2625 | read ++bad=- Xtest |
| 2626 | read ++ff=unix Xtest |
| 2627 | read ++ff=dos Xtest |
| 2628 | read ++ff=mac Xtest |
| 2629 | read ++enc=utf-8 Xtest |
| 2630 | |
| 2631 | call assert_equal(['', |
| 2632 | \ 'v:cmdarg = ++bin', |
| 2633 | \ 'v:cmdarg = ++nobin', |
| 2634 | \ 'v:cmdarg = ++edit', |
| 2635 | \ 'v:cmdarg = ++bad=keep', |
| 2636 | \ 'v:cmdarg = ++bad=drop', |
| 2637 | \ 'v:cmdarg = ++bad=-', |
| 2638 | \ 'v:cmdarg = ++ff=unix', |
| 2639 | \ 'v:cmdarg = ++ff=dos', |
| 2640 | \ 'v:cmdarg = ++ff=mac', |
| 2641 | \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$')) |
| 2642 | |
| 2643 | close! |
| 2644 | augroup FileReadCmdTest |
| 2645 | au! |
| 2646 | augroup END |
| 2647 | delfunc ReadFileCmd |
| 2648 | endfunc |
| 2649 | |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 2650 | " Test for passing invalid arguments to autocmd |
| 2651 | func Test_autocmd_invalid_args() |
| 2652 | " Additional character after * for event |
| 2653 | call assert_fails('autocmd *a Xfile set ff=unix', 'E215:') |
| 2654 | augroup Test |
| 2655 | augroup END |
| 2656 | " Invalid autocmd event |
| 2657 | call assert_fails('autocmd Bufabc Xfile set ft=vim', 'E216:') |
| 2658 | " Invalid autocmd event in a autocmd group |
| 2659 | call assert_fails('autocmd Test Bufabc Xfile set ft=vim', 'E216:') |
| 2660 | augroup! Test |
| 2661 | " Execute all autocmds |
| 2662 | call assert_fails('doautocmd * BufEnter', 'E217:') |
| 2663 | call assert_fails('augroup! x1a2b3', 'E367:') |
| 2664 | call assert_fails('autocmd BufNew <buffer=999> pwd', 'E680:') |
Bram Moolenaar | 531be47 | 2020-09-23 22:38:05 +0200 | [diff] [blame] | 2665 | call assert_fails('autocmd BufNew \) set ff=unix', 'E55:') |
Bram Moolenaar | ee4e0c1 | 2020-04-06 21:35:05 +0200 | [diff] [blame] | 2666 | endfunc |
| 2667 | |
| 2668 | " Test for deep nesting of autocmds |
| 2669 | func Test_autocmd_deep_nesting() |
| 2670 | autocmd BufEnter Xfile doautocmd BufEnter Xfile |
| 2671 | call assert_fails('doautocmd BufEnter Xfile', 'E218:') |
| 2672 | autocmd! BufEnter Xfile |
| 2673 | endfunc |
| 2674 | |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 2675 | " Tests for SigUSR1 autocmd event, which is only available on posix systems. |
| 2676 | func Test_autocmd_sigusr1() |
| 2677 | CheckUnix |
Bram Moolenaar | 62cd26a | 2020-10-11 20:08:44 +0200 | [diff] [blame] | 2678 | CheckExecutable /bin/kill |
Bram Moolenaar | be5ee86 | 2020-06-10 20:56:58 +0200 | [diff] [blame] | 2679 | |
| 2680 | let g:sigusr1_passed = 0 |
| 2681 | au SigUSR1 * let g:sigusr1_passed = 1 |
| 2682 | call system('/bin/kill -s usr1 ' . getpid()) |
| 2683 | call WaitForAssert({-> assert_true(g:sigusr1_passed)}) |
| 2684 | |
| 2685 | au! SigUSR1 |
| 2686 | unlet g:sigusr1_passed |
| 2687 | endfunc |
| 2688 | |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 2689 | " Test for BufReadPre autocmd deleting the file |
| 2690 | func Test_BufReadPre_delfile() |
| 2691 | augroup TestAuCmd |
| 2692 | au! |
| 2693 | autocmd BufReadPre Xfile call delete('Xfile') |
| 2694 | augroup END |
| 2695 | call writefile([], 'Xfile') |
| 2696 | call assert_fails('new Xfile', 'E200:') |
| 2697 | call assert_equal('Xfile', @%) |
| 2698 | call assert_equal(1, &readonly) |
| 2699 | call delete('Xfile') |
| 2700 | augroup TestAuCmd |
| 2701 | au! |
| 2702 | augroup END |
| 2703 | close! |
| 2704 | endfunc |
| 2705 | |
| 2706 | " Test for BufReadPre autocmd changing the current buffer |
| 2707 | func Test_BufReadPre_changebuf() |
| 2708 | augroup TestAuCmd |
| 2709 | au! |
| 2710 | autocmd BufReadPre Xfile edit Xsomeotherfile |
| 2711 | augroup END |
| 2712 | call writefile([], 'Xfile') |
| 2713 | call assert_fails('new Xfile', 'E201:') |
| 2714 | call assert_equal('Xsomeotherfile', @%) |
| 2715 | call assert_equal(1, &readonly) |
| 2716 | call delete('Xfile') |
| 2717 | augroup TestAuCmd |
| 2718 | au! |
| 2719 | augroup END |
| 2720 | close! |
| 2721 | endfunc |
| 2722 | |
| 2723 | " Test for BufWipeouti autocmd changing the current buffer when reading a file |
| 2724 | " in an empty buffer with 'f' flag in 'cpo' |
| 2725 | func Test_BufDelete_changebuf() |
| 2726 | new |
| 2727 | augroup TestAuCmd |
| 2728 | au! |
| 2729 | autocmd BufWipeout * let bufnr = bufadd('somefile') | exe "b " .. bufnr |
| 2730 | augroup END |
| 2731 | let save_cpo = &cpo |
| 2732 | set cpo+=f |
Bram Moolenaar | 9b7bf9e | 2020-07-11 22:14:59 +0200 | [diff] [blame] | 2733 | call assert_fails('r Xfile', ['E812:', 'E484:']) |
Bram Moolenaar | b340bae | 2020-06-15 19:51:56 +0200 | [diff] [blame] | 2734 | call assert_equal('somefile', @%) |
| 2735 | let &cpo = save_cpo |
| 2736 | augroup TestAuCmd |
| 2737 | au! |
| 2738 | augroup END |
| 2739 | close! |
| 2740 | endfunc |
| 2741 | |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2742 | " Test for the temporary internal window used to execute autocmds |
| 2743 | func Test_autocmd_window() |
| 2744 | %bw! |
| 2745 | edit one.txt |
| 2746 | tabnew two.txt |
Bram Moolenaar | 41cd803 | 2021-03-13 15:47:56 +0100 | [diff] [blame] | 2747 | vnew three.txt |
| 2748 | tabnew four.txt |
| 2749 | tabprevious |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2750 | let g:blist = [] |
Bram Moolenaar | 832adf9 | 2020-06-25 19:01:36 +0200 | [diff] [blame] | 2751 | augroup aucmd_win_test1 |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2752 | au! |
| 2753 | au BufEnter * call add(g:blist, [expand('<afile>'), |
| 2754 | \ win_gettype(bufwinnr(expand('<afile>')))]) |
| 2755 | augroup END |
| 2756 | |
| 2757 | doautoall BufEnter |
Bram Moolenaar | 41cd803 | 2021-03-13 15:47:56 +0100 | [diff] [blame] | 2758 | call assert_equal([ |
| 2759 | \ ['one.txt', 'autocmd'], |
| 2760 | \ ['two.txt', ''], |
| 2761 | \ ['four.txt', 'autocmd'], |
| 2762 | \ ['three.txt', ''], |
| 2763 | \ ], g:blist) |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2764 | |
Bram Moolenaar | 832adf9 | 2020-06-25 19:01:36 +0200 | [diff] [blame] | 2765 | augroup aucmd_win_test1 |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2766 | au! |
| 2767 | augroup END |
Bram Moolenaar | 832adf9 | 2020-06-25 19:01:36 +0200 | [diff] [blame] | 2768 | augroup! aucmd_win_test1 |
| 2769 | %bw! |
| 2770 | endfunc |
| 2771 | |
| 2772 | " Test for trying to close the temporary window used for executing an autocmd |
| 2773 | func Test_close_autocmd_window() |
| 2774 | %bw! |
| 2775 | edit one.txt |
| 2776 | tabnew two.txt |
| 2777 | augroup aucmd_win_test2 |
| 2778 | au! |
| 2779 | au BufEnter * if expand('<afile>') == 'one.txt' | 1close | endif |
| 2780 | augroup END |
| 2781 | |
| 2782 | call assert_fails('doautoall BufEnter', 'E813:') |
| 2783 | |
| 2784 | augroup aucmd_win_test2 |
| 2785 | au! |
| 2786 | augroup END |
| 2787 | augroup! aucmd_win_test2 |
Bram Moolenaar | cf84417 | 2020-06-26 19:44:06 +0200 | [diff] [blame] | 2788 | %bwipe! |
| 2789 | endfunc |
| 2790 | |
| 2791 | " Test for trying to close the tab that has the temporary window for exeucing |
| 2792 | " an autocmd. |
| 2793 | func Test_close_autocmd_tab() |
| 2794 | edit one.txt |
| 2795 | tabnew two.txt |
| 2796 | augroup aucmd_win_test |
| 2797 | au! |
| 2798 | au BufEnter * if expand('<afile>') == 'one.txt' | tabfirst | tabonly | endif |
| 2799 | augroup END |
| 2800 | |
| 2801 | call assert_fails('doautoall BufEnter', 'E813:') |
| 2802 | |
| 2803 | tabonly |
| 2804 | augroup aucmd_win_test |
| 2805 | au! |
| 2806 | augroup END |
| 2807 | augroup! aucmd_win_test |
| 2808 | %bwipe! |
Bram Moolenaar | 0fe937f | 2020-06-16 22:42:04 +0200 | [diff] [blame] | 2809 | endfunc |
| 2810 | |
Bram Moolenaar | 6bcb877 | 2021-02-03 21:23:29 +0100 | [diff] [blame] | 2811 | " This was using freed memory. |
| 2812 | func Test_BufNew_arglocal() |
| 2813 | arglocal |
| 2814 | au BufNew * arglocal |
| 2815 | call assert_fails('drop xx', 'E1156:') |
| 2816 | |
| 2817 | au! BufNew |
| 2818 | endfunc |
| 2819 | |
Bram Moolenaar | 8ab3757 | 2021-02-03 21:56:59 +0100 | [diff] [blame] | 2820 | func Test_autocmd_closes_window() |
| 2821 | au BufNew,BufWinLeave * e %e |
| 2822 | file yyy |
| 2823 | au BufNew,BufWinLeave * ball |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 2824 | n xxx |
Bram Moolenaar | 8ab3757 | 2021-02-03 21:56:59 +0100 | [diff] [blame] | 2825 | |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 2826 | %bwipe |
Bram Moolenaar | 8ab3757 | 2021-02-03 21:56:59 +0100 | [diff] [blame] | 2827 | au! BufNew |
| 2828 | au! BufWinLeave |
| 2829 | endfunc |
| 2830 | |
Bram Moolenaar | 92bb83e | 2021-02-03 23:04:46 +0100 | [diff] [blame] | 2831 | func Test_autocmd_quit_psearch() |
| 2832 | sn aa bb |
| 2833 | augroup aucmd_win_test |
| 2834 | au! |
| 2835 | au BufEnter,BufLeave,BufNew,WinEnter,WinLeave,WinNew * if winnr('$') > 1 | q | endif |
| 2836 | augroup END |
| 2837 | ps / |
| 2838 | |
| 2839 | augroup aucmd_win_test |
| 2840 | au! |
| 2841 | augroup END |
| 2842 | endfunc |
| 2843 | |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 2844 | " Fuzzer found some strange combination that caused a crash. |
| 2845 | func Test_autocmd_normal_mess() |
Bram Moolenaar | dd07c02 | 2021-02-07 13:32:46 +0100 | [diff] [blame] | 2846 | " For unknown reason this hangs on MS-Windows |
| 2847 | CheckNotMSWindows |
| 2848 | |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 2849 | augroup aucmd_normal_test |
| 2850 | au BufLeave,BufWinLeave,BufHidden,BufUnload,BufDelete,BufWipeout * norm 7q/qc |
| 2851 | augroup END |
Bram Moolenaar | 983d83f | 2021-02-07 12:12:43 +0100 | [diff] [blame] | 2852 | call assert_fails('o4', 'E1159') |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 2853 | silent! H |
Bram Moolenaar | 983d83f | 2021-02-07 12:12:43 +0100 | [diff] [blame] | 2854 | call assert_fails('e xx', 'E1159') |
Bram Moolenaar | aad5f9d | 2021-02-06 17:30:31 +0100 | [diff] [blame] | 2855 | normal G |
| 2856 | |
| 2857 | augroup aucmd_normal_test |
| 2858 | au! |
| 2859 | augroup END |
| 2860 | endfunc |
| 2861 | |
Bram Moolenaar | 8c6951f | 2021-02-06 18:08:45 +0100 | [diff] [blame] | 2862 | func Test_autocmd_closing_cmdwin() |
Bram Moolenaar | dd07c02 | 2021-02-07 13:32:46 +0100 | [diff] [blame] | 2863 | " For unknown reason this hangs on MS-Windows |
| 2864 | CheckNotMSWindows |
| 2865 | |
Bram Moolenaar | 8c6951f | 2021-02-06 18:08:45 +0100 | [diff] [blame] | 2866 | au BufWinLeave * nested q |
| 2867 | call assert_fails("norm 7q?\n", 'E855:') |
| 2868 | |
| 2869 | au! BufWinLeave |
| 2870 | new |
| 2871 | only |
| 2872 | endfunc |
| 2873 | |
Bram Moolenaar | 2c7080b | 2021-02-06 19:19:42 +0100 | [diff] [blame] | 2874 | func Test_autocmd_vimgrep() |
| 2875 | augroup aucmd_vimgrep |
| 2876 | au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * sb |
| 2877 | au QuickfixCmdPre,BufNew,BufDelete,BufReadCmd * q9 |
| 2878 | augroup END |
Bram Moolenaar | 983d83f | 2021-02-07 12:12:43 +0100 | [diff] [blame] | 2879 | %bwipe! |
Bram Moolenaar | dd07c02 | 2021-02-07 13:32:46 +0100 | [diff] [blame] | 2880 | call assert_fails('lv ?a? foo', 'E926:') |
Bram Moolenaar | 2c7080b | 2021-02-06 19:19:42 +0100 | [diff] [blame] | 2881 | |
| 2882 | augroup aucmd_vimgrep |
| 2883 | au! |
| 2884 | augroup END |
| 2885 | endfunc |
| 2886 | |
Bram Moolenaar | 73b8b0a | 2021-08-01 14:52:32 +0200 | [diff] [blame] | 2887 | func Test_autocmd_with_block() |
| 2888 | augroup block_testing |
| 2889 | au BufReadPost *.xml { |
| 2890 | setlocal matchpairs+=<:> |
| 2891 | /<start |
| 2892 | } |
Bram Moolenaar | 63b9173 | 2021-08-05 20:40:03 +0200 | [diff] [blame] | 2893 | au CursorHold * { |
| 2894 | autocmd BufReadPre * ++once echo 'one' | echo 'two' |
| 2895 | g:gotSafeState = 77 |
| 2896 | } |
Bram Moolenaar | 73b8b0a | 2021-08-01 14:52:32 +0200 | [diff] [blame] | 2897 | augroup END |
| 2898 | |
| 2899 | let expected = "\n--- Autocommands ---\nblock_testing BufRead\n *.xml {^@ setlocal matchpairs+=<:>^@ /<start^@ }" |
| 2900 | call assert_equal(expected, execute('au BufReadPost *.xml')) |
| 2901 | |
Bram Moolenaar | 63b9173 | 2021-08-05 20:40:03 +0200 | [diff] [blame] | 2902 | doautocmd CursorHold |
| 2903 | call assert_equal(77, g:gotSafeState) |
| 2904 | unlet g:gotSafeState |
| 2905 | |
Bram Moolenaar | 73b8b0a | 2021-08-01 14:52:32 +0200 | [diff] [blame] | 2906 | augroup block_testing |
| 2907 | au! |
| 2908 | augroup END |
| 2909 | endfunc |
| 2910 | |
Christian Brabandt | db3b446 | 2021-10-16 11:58:55 +0100 | [diff] [blame] | 2911 | " Test TextChangedI and TextChanged |
| 2912 | func Test_Changed_ChangedI() |
| 2913 | new |
| 2914 | call test_override("char_avail", 1) |
| 2915 | let [g:autocmd_i, g:autocmd_n] = ['',''] |
| 2916 | |
| 2917 | func! TextChangedAutocmdI(char) |
| 2918 | let g:autocmd_{tolower(a:char)} = a:char .. b:changedtick |
| 2919 | endfunc |
| 2920 | |
| 2921 | augroup Test_TextChanged |
| 2922 | au! |
| 2923 | au TextChanged <buffer> :call TextChangedAutocmdI('N') |
| 2924 | au TextChangedI <buffer> :call TextChangedAutocmdI('I') |
| 2925 | augroup END |
| 2926 | |
| 2927 | call feedkeys("ifoo\<esc>", 'tnix') |
| 2928 | " TODO: Test test does not seem to trigger TextChanged autocommand, this |
| 2929 | " requires running Vim in a terminal window. |
| 2930 | " call assert_equal('N3', g:autocmd_n) |
| 2931 | call assert_equal('I3', g:autocmd_i) |
| 2932 | |
| 2933 | call feedkeys("yyp", 'tnix') |
| 2934 | " TODO: Test test does not seem to trigger TextChanged autocommand. |
| 2935 | " call assert_equal('N4', g:autocmd_n) |
| 2936 | call assert_equal('I3', g:autocmd_i) |
| 2937 | |
| 2938 | " CleanUp |
| 2939 | call test_override("char_avail", 0) |
| 2940 | au! TextChanged <buffer> |
| 2941 | au! TextChangedI <buffer> |
| 2942 | augroup! Test_TextChanged |
| 2943 | delfu TextChangedAutocmdI |
| 2944 | unlet! g:autocmd_i g:autocmd_n |
| 2945 | |
| 2946 | bw! |
| 2947 | endfunc |
Bram Moolenaar | 2c7080b | 2021-02-06 19:19:42 +0100 | [diff] [blame] | 2948 | |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 2949 | " vim: shiftwidth=2 sts=2 expandtab |