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