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