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