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