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 |
| 4 | |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 5 | func s:cleanup_buffers() abort |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 6 | for bnr in range(1, bufnr('$')) |
| 7 | if bufloaded(bnr) && bufnr('%') != bnr |
| 8 | execute 'bd! ' . bnr |
| 9 | endif |
| 10 | endfor |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 11 | endfunc |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 12 | |
Bram Moolenaar | 1473551 | 2016-03-26 21:00:08 +0100 | [diff] [blame] | 13 | func Test_vim_did_enter() |
| 14 | call assert_false(v:vim_did_enter) |
| 15 | |
| 16 | " This script will never reach the main loop, can't check if v:vim_did_enter |
| 17 | " becomes one. |
| 18 | endfunc |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 19 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 20 | if has('timers') |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 21 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 22 | func ExitInsertMode(id) |
| 23 | call feedkeys("\<Esc>") |
| 24 | endfunc |
| 25 | |
| 26 | func Test_cursorhold_insert() |
Bram Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 27 | " Need to move the cursor. |
| 28 | call feedkeys("ggG", "xt") |
| 29 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 30 | let g:triggered = 0 |
| 31 | au CursorHoldI * let g:triggered += 1 |
| 32 | set updatetime=20 |
| 33 | call timer_start(100, 'ExitInsertMode') |
| 34 | call feedkeys('a', 'x!') |
| 35 | call assert_equal(1, g:triggered) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 36 | unlet g:triggered |
| 37 | au! CursorHoldI |
| 38 | set updatetime& |
| 39 | endfunc |
| 40 | |
| 41 | func Test_cursorhold_insert_with_timer_interrupt() |
| 42 | if !has('job') |
| 43 | return |
| 44 | endif |
| 45 | " Need to move the cursor. |
| 46 | call feedkeys("ggG", "xt") |
| 47 | |
| 48 | " Confirm the timer invoked in exit_cb of the job doesn't disturb |
| 49 | " CursorHoldI event. |
| 50 | let g:triggered = 0 |
| 51 | au CursorHoldI * let g:triggered += 1 |
| 52 | set updatetime=500 |
| 53 | call job_start(has('win32') ? 'cmd /c echo:' : 'echo', |
Bram Moolenaar | 8d4ce56 | 2019-01-30 22:01:40 +0100 | [diff] [blame] | 54 | \ {'exit_cb': {-> timer_start(1000, 'ExitInsertMode')}}) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 55 | call feedkeys('a', 'x!') |
| 56 | call assert_equal(1, g:triggered) |
| 57 | unlet g:triggered |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 58 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 59 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 60 | endfunc |
| 61 | |
| 62 | func Test_cursorhold_insert_ctrl_x() |
| 63 | let g:triggered = 0 |
| 64 | au CursorHoldI * let g:triggered += 1 |
| 65 | set updatetime=20 |
| 66 | call timer_start(100, 'ExitInsertMode') |
| 67 | " CursorHoldI does not trigger after CTRL-X |
| 68 | call feedkeys("a\<C-X>", 'x!') |
| 69 | call assert_equal(0, g:triggered) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 70 | unlet g:triggered |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 71 | au! CursorHoldI |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 72 | set updatetime& |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 73 | endfunc |
Bram Moolenaar | 97b0075 | 2019-05-12 13:07:14 +0200 | [diff] [blame] | 74 | |
| 75 | func Test_OptionSet_modeline() |
| 76 | call test_override('starting', 1) |
| 77 | au! OptionSet |
| 78 | augroup set_tabstop |
| 79 | au OptionSet tabstop call timer_start(1, {-> execute("echo 'Handler called'", "")}) |
| 80 | augroup END |
| 81 | call writefile(['vim: set ts=7 sw=5 :', 'something'], 'XoptionsetModeline') |
| 82 | set modeline |
| 83 | let v:errmsg = '' |
| 84 | call assert_fails('split XoptionsetModeline', 'E12:') |
| 85 | call assert_equal(7, &ts) |
| 86 | call assert_equal('', v:errmsg) |
| 87 | |
| 88 | augroup set_tabstop |
| 89 | au! |
| 90 | augroup END |
| 91 | bwipe! |
| 92 | set ts& |
| 93 | call delete('XoptionsetModeline') |
| 94 | call test_override('starting', 0) |
| 95 | endfunc |
| 96 | |
| 97 | endif "has('timers') |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 98 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 99 | func Test_bufunload() |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 100 | augroup test_bufunload_group |
| 101 | autocmd! |
| 102 | autocmd BufUnload * call add(s:li, "bufunload") |
| 103 | autocmd BufDelete * call add(s:li, "bufdelete") |
| 104 | autocmd BufWipeout * call add(s:li, "bufwipeout") |
| 105 | augroup END |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 106 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 107 | let s:li=[] |
| 108 | new |
| 109 | setlocal bufhidden= |
| 110 | bunload |
| 111 | call assert_equal(["bufunload", "bufdelete"], s:li) |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 112 | |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 113 | let s:li=[] |
| 114 | new |
| 115 | setlocal bufhidden=delete |
| 116 | bunload |
| 117 | call assert_equal(["bufunload", "bufdelete"], s:li) |
| 118 | |
| 119 | let s:li=[] |
| 120 | new |
| 121 | setlocal bufhidden=unload |
| 122 | bwipeout |
| 123 | call assert_equal(["bufunload", "bufdelete", "bufwipeout"], s:li) |
| 124 | |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 125 | au! test_bufunload_group |
Bram Moolenaar | c67e892 | 2016-05-24 16:07:40 +0200 | [diff] [blame] | 126 | augroup! test_bufunload_group |
Bram Moolenaar | 40b1b54 | 2016-04-20 20:18:23 +0200 | [diff] [blame] | 127 | endfunc |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 128 | |
| 129 | " SEGV occurs in older versions. (At least 7.4.2005 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 130 | func Test_autocmd_bufunload_with_tabnext() |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 131 | tabedit |
| 132 | tabfirst |
| 133 | |
| 134 | augroup test_autocmd_bufunload_with_tabnext_group |
| 135 | autocmd! |
| 136 | autocmd BufUnload <buffer> tabnext |
| 137 | augroup END |
| 138 | |
| 139 | quit |
| 140 | call assert_equal(2, tabpagenr('$')) |
| 141 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 142 | autocmd! test_autocmd_bufunload_with_tabnext_group |
Bram Moolenaar | 30445cb | 2016-07-09 15:21:02 +0200 | [diff] [blame] | 143 | augroup! test_autocmd_bufunload_with_tabnext_group |
| 144 | tablast |
| 145 | quit |
| 146 | endfunc |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 147 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 148 | func Test_autocmd_bufwinleave_with_tabfirst() |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 149 | tabedit |
| 150 | augroup sample |
| 151 | autocmd! |
| 152 | autocmd BufWinLeave <buffer> tabfirst |
| 153 | augroup END |
| 154 | call setline(1, ['a', 'b', 'c']) |
| 155 | edit! a.txt |
Bram Moolenaar | f18c4db | 2016-09-08 22:10:06 +0200 | [diff] [blame] | 156 | tabclose |
Bram Moolenaar | f9e687e | 2016-09-04 21:33:09 +0200 | [diff] [blame] | 157 | endfunc |
| 158 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 159 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 160 | func Test_autocmd_bufunload_avoiding_SEGV_01() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 161 | split aa.txt |
| 162 | let lastbuf = bufnr('$') |
| 163 | |
| 164 | augroup test_autocmd_bufunload |
| 165 | autocmd! |
| 166 | exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!' |
| 167 | augroup END |
| 168 | |
Bram Moolenaar | a997b45 | 2018-04-17 23:24:06 +0200 | [diff] [blame] | 169 | " Todo: check for E937 generated first |
| 170 | " call assert_fails('edit bb.txt', 'E937:') |
| 171 | call assert_fails('edit bb.txt', 'E517:') |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 172 | |
| 173 | autocmd! test_autocmd_bufunload |
| 174 | augroup! test_autocmd_bufunload |
| 175 | bwipe! aa.txt |
| 176 | bwipe! bb.txt |
| 177 | endfunc |
| 178 | |
| 179 | " SEGV occurs in older versions. (At least 7.4.2321 or older) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 180 | func Test_autocmd_bufunload_avoiding_SEGV_02() |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 181 | setlocal buftype=nowrite |
| 182 | let lastbuf = bufnr('$') |
| 183 | |
| 184 | augroup test_autocmd_bufunload |
| 185 | autocmd! |
| 186 | exe 'autocmd BufUnload <buffer> ' . (lastbuf + 1) . 'bwipeout!' |
| 187 | augroup END |
| 188 | |
| 189 | normal! i1 |
| 190 | call assert_fails('edit a.txt', 'E517:') |
| 191 | call feedkeys("\<CR>") |
| 192 | |
| 193 | autocmd! test_autocmd_bufunload |
| 194 | augroup! test_autocmd_bufunload |
| 195 | bwipe! a.txt |
| 196 | endfunc |
| 197 | |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 198 | func Test_win_tab_autocmd() |
| 199 | let g:record = [] |
| 200 | |
| 201 | augroup testing |
| 202 | au WinNew * call add(g:record, 'WinNew') |
| 203 | au WinEnter * call add(g:record, 'WinEnter') |
| 204 | au WinLeave * call add(g:record, 'WinLeave') |
| 205 | au TabNew * call add(g:record, 'TabNew') |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 206 | au TabClosed * call add(g:record, 'TabClosed') |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 207 | au TabEnter * call add(g:record, 'TabEnter') |
| 208 | au TabLeave * call add(g:record, 'TabLeave') |
| 209 | augroup END |
| 210 | |
| 211 | split |
| 212 | tabnew |
| 213 | close |
| 214 | close |
| 215 | |
| 216 | call assert_equal([ |
| 217 | \ 'WinLeave', 'WinNew', 'WinEnter', |
| 218 | \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 219 | \ 'WinLeave', 'TabLeave', 'TabClosed', 'WinEnter', 'TabEnter', |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 220 | \ 'WinLeave', 'WinEnter' |
| 221 | \ ], g:record) |
| 222 | |
Bram Moolenaar | 12c11d5 | 2016-07-19 23:13:03 +0200 | [diff] [blame] | 223 | let g:record = [] |
| 224 | tabnew somefile |
| 225 | tabnext |
| 226 | bwipe somefile |
| 227 | |
| 228 | call assert_equal([ |
| 229 | \ 'WinLeave', 'TabLeave', 'WinNew', 'WinEnter', 'TabNew', 'TabEnter', |
| 230 | \ 'WinLeave', 'TabLeave', 'WinEnter', 'TabEnter', |
| 231 | \ 'TabClosed' |
| 232 | \ ], g:record) |
| 233 | |
Bram Moolenaar | c917da4 | 2016-07-19 22:31:36 +0200 | [diff] [blame] | 234 | augroup testing |
| 235 | au! |
| 236 | augroup END |
| 237 | unlet g:record |
| 238 | endfunc |
Bram Moolenaar | e99e844 | 2016-07-26 20:43:40 +0200 | [diff] [blame] | 239 | |
| 240 | func s:AddAnAutocmd() |
| 241 | augroup vimBarTest |
| 242 | au BufReadCmd * echo 'hello' |
| 243 | augroup END |
| 244 | call assert_equal(3, len(split(execute('au vimBarTest'), "\n"))) |
| 245 | endfunc |
| 246 | |
| 247 | func Test_early_bar() |
| 248 | " test that a bar is recognized before the {event} |
| 249 | call s:AddAnAutocmd() |
| 250 | augroup vimBarTest | au! | augroup END |
| 251 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
| 252 | |
| 253 | call s:AddAnAutocmd() |
| 254 | augroup vimBarTest| au!| augroup END |
| 255 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
| 256 | |
| 257 | " test that a bar is recognized after the {event} |
| 258 | call s:AddAnAutocmd() |
| 259 | augroup vimBarTest| au!BufReadCmd| augroup END |
| 260 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
| 261 | |
| 262 | " test that a bar is recognized after the {group} |
| 263 | call s:AddAnAutocmd() |
| 264 | au! vimBarTest|echo 'hello' |
| 265 | call assert_equal(1, len(split(execute('au vimBarTest'), "\n"))) |
| 266 | endfunc |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 267 | |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 268 | func RemoveGroup() |
| 269 | autocmd! StartOK |
| 270 | augroup! StartOK |
| 271 | endfunc |
| 272 | |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 273 | func Test_augroup_warning() |
| 274 | augroup TheWarning |
| 275 | au VimEnter * echo 'entering' |
| 276 | augroup END |
| 277 | call assert_true(match(execute('au VimEnter'), "TheWarning.*VimEnter") >= 0) |
| 278 | redir => res |
| 279 | augroup! TheWarning |
| 280 | redir END |
| 281 | call assert_true(match(res, "W19:") >= 0) |
| 282 | call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0) |
| 283 | |
| 284 | " check "Another" does not take the pace of the deleted entry |
| 285 | augroup Another |
| 286 | augroup END |
| 287 | call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0) |
Bram Moolenaar | aeac900 | 2016-09-06 22:15:08 +0200 | [diff] [blame] | 288 | augroup! Another |
Bram Moolenaar | 5c80908 | 2016-09-01 16:21:48 +0200 | [diff] [blame] | 289 | |
| 290 | " no warning for postpone aucmd delete |
| 291 | augroup StartOK |
| 292 | au VimEnter * call RemoveGroup() |
| 293 | augroup END |
| 294 | call assert_true(match(execute('au VimEnter'), "StartOK.*VimEnter") >= 0) |
| 295 | redir => res |
| 296 | doautocmd VimEnter |
| 297 | redir END |
| 298 | call assert_true(match(res, "W19:") < 0) |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 299 | au! VimEnter |
Bram Moolenaar | f2c4c39 | 2016-07-29 20:50:24 +0200 | [diff] [blame] | 300 | endfunc |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 301 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 302 | func Test_BufReadCmdHelp() |
| 303 | " This used to cause access to free memory |
| 304 | au BufReadCmd * e +h |
| 305 | help |
| 306 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 307 | au! BufReadCmd |
| 308 | endfunc |
| 309 | |
| 310 | func Test_BufReadCmdHelpJump() |
| 311 | " This used to cause access to free memory |
| 312 | au BufReadCmd * e +h{ |
Bram Moolenaar | cf1ba35 | 2017-10-27 00:55:04 +0200 | [diff] [blame] | 313 | " } to fix highlighting |
| 314 | call assert_fails('help', 'E434:') |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 315 | |
Bram Moolenaar | 8d84ff1 | 2017-10-26 16:42:16 +0200 | [diff] [blame] | 316 | au! BufReadCmd |
| 317 | endfunc |
| 318 | |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 319 | func Test_augroup_deleted() |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 320 | " This caused a crash before E936 was introduced |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 321 | augroup x |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 322 | call assert_fails('augroup! x', 'E936:') |
| 323 | au VimEnter * echo |
| 324 | augroup end |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 325 | augroup! x |
Bram Moolenaar | de653f0 | 2016-09-03 16:59:06 +0200 | [diff] [blame] | 326 | call assert_true(match(execute('au VimEnter'), "-Deleted-.*VimEnter") >= 0) |
| 327 | au! VimEnter |
Bram Moolenaar | b62cc36 | 2016-09-03 16:43:53 +0200 | [diff] [blame] | 328 | endfunc |
| 329 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 330 | " Tests for autocommands on :close command. |
| 331 | " This used to be in test13. |
| 332 | func Test_three_windows() |
Bram Moolenaar | b3435b0 | 2016-09-29 20:54:59 +0200 | [diff] [blame] | 333 | " Clean up buffers, because in some cases this function fails. |
| 334 | call s:cleanup_buffers() |
| 335 | |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 336 | " Write three files and open them, each in a window. |
| 337 | " Then go to next window, with autocommand that deletes the previous one. |
| 338 | " Do this twice, writing the file. |
| 339 | e! Xtestje1 |
| 340 | call setline(1, 'testje1') |
| 341 | w |
| 342 | sp Xtestje2 |
| 343 | call setline(1, 'testje2') |
| 344 | w |
| 345 | sp Xtestje3 |
| 346 | call setline(1, 'testje3') |
| 347 | w |
| 348 | wincmd w |
| 349 | au WinLeave Xtestje2 bwipe |
| 350 | wincmd w |
| 351 | call assert_equal('Xtestje1', expand('%')) |
| 352 | |
| 353 | au WinLeave Xtestje1 bwipe Xtestje3 |
| 354 | close |
| 355 | call assert_equal('Xtestje1', expand('%')) |
| 356 | |
| 357 | " Test deleting the buffer on a Unload event. If this goes wrong there |
| 358 | " will be the ATTENTION prompt. |
| 359 | e Xtestje1 |
| 360 | au! |
| 361 | au! BufUnload Xtestje1 bwipe |
| 362 | call assert_fails('e Xtestje3', 'E937:') |
| 363 | call assert_equal('Xtestje3', expand('%')) |
| 364 | |
| 365 | e Xtestje2 |
| 366 | sp Xtestje1 |
| 367 | call assert_fails('e', 'E937:') |
Bram Moolenaar | a997b45 | 2018-04-17 23:24:06 +0200 | [diff] [blame] | 368 | call assert_equal('Xtestje1', expand('%')) |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 369 | |
| 370 | " Test changing buffers in a BufWipeout autocommand. If this goes wrong |
| 371 | " there are ml_line errors and/or a Crash. |
| 372 | au! |
| 373 | only |
| 374 | e Xanother |
| 375 | e Xtestje1 |
| 376 | bwipe Xtestje2 |
| 377 | bwipe Xtestje3 |
| 378 | au BufWipeout Xtestje1 buf Xtestje1 |
| 379 | bwipe |
| 380 | call assert_equal('Xanother', expand('%')) |
| 381 | |
| 382 | only |
| 383 | help |
| 384 | wincmd w |
| 385 | 1quit |
| 386 | call assert_equal('Xanother', expand('%')) |
| 387 | |
| 388 | au! |
Bram Moolenaar | 4520d44 | 2017-03-19 16:09:46 +0100 | [diff] [blame] | 389 | enew |
Bram Moolenaar | e0ab94e | 2016-09-04 19:50:54 +0200 | [diff] [blame] | 390 | call delete('Xtestje1') |
| 391 | call delete('Xtestje2') |
| 392 | call delete('Xtestje3') |
| 393 | endfunc |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 394 | |
| 395 | func Test_BufEnter() |
| 396 | au! BufEnter |
| 397 | au Bufenter * let val = val . '+' |
| 398 | let g:val = '' |
| 399 | split NewFile |
| 400 | call assert_equal('+', g:val) |
| 401 | bwipe! |
| 402 | call assert_equal('++', g:val) |
| 403 | |
| 404 | " Also get BufEnter when editing a directory |
| 405 | call mkdir('Xdir') |
| 406 | split Xdir |
| 407 | call assert_equal('+++', g:val) |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 408 | |
| 409 | " On MS-Windows we can't edit the directory, make sure we wipe the right |
| 410 | " buffer. |
| 411 | bwipe! Xdir |
Bram Moolenaar | e13b9af | 2017-01-13 22:01:02 +0100 | [diff] [blame] | 412 | |
| 413 | call delete('Xdir', 'd') |
| 414 | au! BufEnter |
| 415 | endfunc |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 416 | |
| 417 | " Closing a window might cause an endless loop |
| 418 | " E814 for older Vims |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 419 | func Test_autocmd_bufwipe_in_SessLoadPost() |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 420 | edit Xtest |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 421 | tabnew |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 422 | file Xsomething |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 423 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 424 | mksession! |
| 425 | |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 426 | let content = ['set nocp noswapfile', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 427 | \ 'let v:swapchoice="e"', |
| 428 | \ 'augroup test_autocmd_sessionload', |
| 429 | \ 'autocmd!', |
Bram Moolenaar | 1d68d9b | 2017-10-13 22:33:32 +0200 | [diff] [blame] | 430 | \ 'autocmd SessionLoadPost * exe bufnr("Xsomething") . "bw!"', |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 431 | \ 'augroup END', |
| 432 | \ '', |
| 433 | \ 'func WriteErrors()', |
| 434 | \ ' call writefile([execute("messages")], "Xerrors")', |
| 435 | \ 'endfunc', |
| 436 | \ 'au VimLeave * call WriteErrors()', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 437 | \ ] |
| 438 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 439 | call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq') |
| 440 | let errors = join(readfile('Xerrors')) |
| 441 | call assert_match('E814', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 442 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 443 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 444 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 445 | call delete(file) |
| 446 | endfor |
| 447 | endfunc |
| 448 | |
| 449 | " SEGV occurs in older versions. |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 450 | func Test_autocmd_bufwipe_in_SessLoadPost2() |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 451 | tabnew |
| 452 | set noswapfile |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 453 | mksession! |
| 454 | |
| 455 | let content = ['set nocp noswapfile', |
| 456 | \ 'function! DeleteInactiveBufs()', |
| 457 | \ ' tabfirst', |
| 458 | \ ' let tabblist = []', |
| 459 | \ ' for i in range(1, tabpagenr(''$''))', |
| 460 | \ ' call extend(tabblist, tabpagebuflist(i))', |
| 461 | \ ' endfor', |
| 462 | \ ' for b in range(1, bufnr(''$''))', |
| 463 | \ ' if bufexists(b) && buflisted(b) && (index(tabblist, b) == -1 || bufname(b) =~# ''^$'')', |
| 464 | \ ' exec ''bwipeout '' . b', |
| 465 | \ ' endif', |
| 466 | \ ' endfor', |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 467 | \ ' echomsg "SessionLoadPost DONE"', |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 468 | \ 'endfunction', |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 469 | \ 'au SessionLoadPost * call DeleteInactiveBufs()', |
| 470 | \ '', |
| 471 | \ 'func WriteErrors()', |
| 472 | \ ' call writefile([execute("messages")], "Xerrors")', |
| 473 | \ 'endfunc', |
| 474 | \ 'au VimLeave * call WriteErrors()', |
| 475 | \ ] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 476 | call writefile(content, 'Xvimrc') |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 477 | call system(v:progpath. ' -u Xvimrc --not-a-term --noplugins -S Session.vim -c cq') |
| 478 | let errors = join(readfile('Xerrors')) |
| 479 | " This probably only ever matches on unix. |
| 480 | call assert_notmatch('Caught deadly signal SEGV', errors) |
| 481 | call assert_match('SessionLoadPost DONE', errors) |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 482 | |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 483 | set swapfile |
Bram Moolenaar | e94260f | 2017-03-21 15:50:12 +0100 | [diff] [blame] | 484 | for file in ['Session.vim', 'Xvimrc', 'Xerrors'] |
Bram Moolenaar | 8c752bd | 2017-03-19 17:09:56 +0100 | [diff] [blame] | 485 | call delete(file) |
| 486 | endfor |
| 487 | endfunc |
Bram Moolenaar | faf29d7 | 2017-07-09 11:07:16 +0200 | [diff] [blame] | 488 | |
| 489 | func Test_empty_doau() |
| 490 | doau \| |
| 491 | endfunc |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 492 | |
| 493 | func s:AutoCommandOptionSet(match) |
| 494 | let item = remove(g:options, 0) |
| 495 | let expected = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", item[0], item[1], item[2], item[3]) |
| 496 | let actual = printf("Option: <%s>, Oldval: <%s>, NewVal: <%s>, Scope: <%s>\n", a:match, v:option_old, v:option_new, v:option_type) |
| 497 | let g:opt = [expected, actual] |
| 498 | "call assert_equal(expected, actual) |
| 499 | endfunc |
| 500 | |
| 501 | func Test_OptionSet() |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 502 | if !has("eval") || !exists("+autochdir") |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 503 | return |
| 504 | endif |
| 505 | |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 506 | badd test_autocmd.vim |
| 507 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 508 | call test_override('starting', 1) |
| 509 | set nocp |
| 510 | au OptionSet * :call s:AutoCommandOptionSet(expand("<amatch>")) |
| 511 | |
| 512 | " 1: Setting number option" |
| 513 | let g:options=[['number', 0, 1, 'global']] |
| 514 | set nu |
| 515 | call assert_equal([], g:options) |
| 516 | call assert_equal(g:opt[0], g:opt[1]) |
| 517 | |
| 518 | " 2: Setting local number option" |
| 519 | let g:options=[['number', 1, 0, 'local']] |
| 520 | setlocal nonu |
| 521 | call assert_equal([], g:options) |
| 522 | call assert_equal(g:opt[0], g:opt[1]) |
| 523 | |
| 524 | " 3: Setting global number option" |
| 525 | let g:options=[['number', 1, 0, 'global']] |
| 526 | setglobal nonu |
| 527 | call assert_equal([], g:options) |
| 528 | call assert_equal(g:opt[0], g:opt[1]) |
| 529 | |
| 530 | " 4: Setting local autoindent option" |
| 531 | let g:options=[['autoindent', 0, 1, 'local']] |
| 532 | setlocal ai |
| 533 | call assert_equal([], g:options) |
| 534 | call assert_equal(g:opt[0], g:opt[1]) |
| 535 | |
| 536 | " 5: Setting global autoindent option" |
| 537 | let g:options=[['autoindent', 0, 1, 'global']] |
| 538 | setglobal ai |
| 539 | call assert_equal([], g:options) |
| 540 | call assert_equal(g:opt[0], g:opt[1]) |
| 541 | |
| 542 | " 6: Setting global autoindent option" |
| 543 | let g:options=[['autoindent', 1, 0, 'global']] |
| 544 | set ai! |
| 545 | call assert_equal([], g:options) |
| 546 | call assert_equal(g:opt[0], g:opt[1]) |
| 547 | |
| 548 | " Should not print anything, use :noa |
| 549 | " 7: don't trigger OptionSet" |
| 550 | let g:options=[['invalid', 1, 1, 'invalid']] |
| 551 | noa set nonu |
| 552 | call assert_equal([['invalid', 1, 1, 'invalid']], g:options) |
| 553 | call assert_equal(g:opt[0], g:opt[1]) |
| 554 | |
| 555 | " 8: Setting several global list and number option" |
| 556 | let g:options=[['list', 0, 1, 'global'], ['number', 0, 1, 'global']] |
| 557 | set list nu |
| 558 | call assert_equal([], g:options) |
| 559 | call assert_equal(g:opt[0], g:opt[1]) |
| 560 | |
| 561 | " 9: don't trigger OptionSet" |
| 562 | let g:options=[['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']] |
| 563 | noa set nolist nonu |
| 564 | call assert_equal([['invalid', 1, 1, 'invalid'], ['invalid', 1, 1, 'invalid']], g:options) |
| 565 | call assert_equal(g:opt[0], g:opt[1]) |
| 566 | |
| 567 | " 10: Setting global acd" |
| 568 | let g:options=[['autochdir', 0, 1, 'local']] |
| 569 | setlocal acd |
| 570 | call assert_equal([], g:options) |
| 571 | call assert_equal(g:opt[0], g:opt[1]) |
| 572 | |
| 573 | " 11: Setting global autoread (also sets local value)" |
| 574 | let g:options=[['autoread', 0, 1, 'global']] |
| 575 | set ar |
| 576 | call assert_equal([], g:options) |
| 577 | call assert_equal(g:opt[0], g:opt[1]) |
| 578 | |
| 579 | " 12: Setting local autoread" |
| 580 | let g:options=[['autoread', 1, 1, 'local']] |
| 581 | setlocal ar |
| 582 | call assert_equal([], g:options) |
| 583 | call assert_equal(g:opt[0], g:opt[1]) |
| 584 | |
| 585 | " 13: Setting global autoread" |
| 586 | let g:options=[['autoread', 1, 0, 'global']] |
| 587 | setglobal invar |
| 588 | call assert_equal([], g:options) |
| 589 | call assert_equal(g:opt[0], g:opt[1]) |
| 590 | |
| 591 | " 14: Setting option backspace through :let" |
| 592 | let g:options=[['backspace', '', 'eol,indent,start', 'global']] |
| 593 | let &bs="eol,indent,start" |
| 594 | call assert_equal([], g:options) |
| 595 | call assert_equal(g:opt[0], g:opt[1]) |
| 596 | |
| 597 | " 15: Setting option backspace through setbufvar()" |
| 598 | let g:options=[['backup', 0, 1, 'local']] |
| 599 | " try twice, first time, shouldn't trigger because option name is invalid, |
| 600 | " second time, it should trigger |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 601 | let bnum = bufnr('%') |
| 602 | call assert_fails("call setbufvar(bnum, '&l:bk', 1)", "E355") |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 603 | " should trigger, use correct option name |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 604 | call setbufvar(bnum, '&backup', 1) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 605 | call assert_equal([], g:options) |
| 606 | call assert_equal(g:opt[0], g:opt[1]) |
| 607 | |
| 608 | " 16: Setting number option using setwinvar" |
| 609 | let g:options=[['number', 0, 1, 'local']] |
| 610 | call setwinvar(0, '&number', 1) |
| 611 | call assert_equal([], g:options) |
| 612 | call assert_equal(g:opt[0], g:opt[1]) |
| 613 | |
| 614 | " 17: Setting key option, shouldn't trigger" |
| 615 | let g:options=[['key', 'invalid', 'invalid1', 'invalid']] |
| 616 | setlocal key=blah |
| 617 | setlocal key= |
| 618 | call assert_equal([['key', 'invalid', 'invalid1', 'invalid']], g:options) |
| 619 | call assert_equal(g:opt[0], g:opt[1]) |
| 620 | |
Bram Moolenaar | 8efa026 | 2017-08-20 15:47:20 +0200 | [diff] [blame] | 621 | " 18: Setting string option" |
| 622 | let oldval = &tags |
| 623 | let g:options=[['tags', oldval, 'tagpath', 'global']] |
| 624 | set tags=tagpath |
| 625 | call assert_equal([], g:options) |
| 626 | call assert_equal(g:opt[0], g:opt[1]) |
| 627 | |
| 628 | " 1l: Resetting string option" |
| 629 | let g:options=[['tags', 'tagpath', oldval, 'global']] |
| 630 | set tags& |
| 631 | call assert_equal([], g:options) |
| 632 | call assert_equal(g:opt[0], g:opt[1]) |
| 633 | |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 634 | " Cleanup |
| 635 | au! OptionSet |
| 636 | for opt in ['nu', 'ai', 'acd', 'ar', 'bs', 'backup', 'cul', 'cp'] |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 637 | exe printf(":set %s&vim", opt) |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 638 | endfor |
| 639 | call test_override('starting', 0) |
| 640 | delfunc! AutoCommandOptionSet |
| 641 | endfunc |
| 642 | |
| 643 | func Test_OptionSet_diffmode() |
| 644 | call test_override('starting', 1) |
Bram Moolenaar | 26d9821 | 2019-01-27 22:32:55 +0100 | [diff] [blame] | 645 | " 18: Changing an option when entering diff mode |
Bram Moolenaar | 04f62f8 | 2017-07-19 18:18:39 +0200 | [diff] [blame] | 646 | new |
| 647 | au OptionSet diff :let &l:cul=v:option_new |
| 648 | |
| 649 | call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) |
| 650 | call assert_equal(0, &l:cul) |
| 651 | diffthis |
| 652 | call assert_equal(1, &l:cul) |
| 653 | |
| 654 | vnew |
| 655 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
| 656 | call assert_equal(0, &l:cul) |
| 657 | diffthis |
| 658 | call assert_equal(1, &l:cul) |
| 659 | |
| 660 | diffoff |
| 661 | call assert_equal(0, &l:cul) |
| 662 | call assert_equal(1, getwinvar(2, '&l:cul')) |
| 663 | bw! |
| 664 | |
| 665 | call assert_equal(1, &l:cul) |
| 666 | diffoff! |
| 667 | call assert_equal(0, &l:cul) |
| 668 | call assert_equal(0, getwinvar(1, '&l:cul')) |
| 669 | bw! |
| 670 | |
| 671 | " Cleanup |
| 672 | au! OptionSet |
| 673 | call test_override('starting', 0) |
| 674 | endfunc |
| 675 | |
| 676 | func Test_OptionSet_diffmode_close() |
| 677 | call test_override('starting', 1) |
| 678 | " 19: Try to close the current window when entering diff mode |
| 679 | " should not segfault |
| 680 | new |
| 681 | au OptionSet diff close |
| 682 | |
| 683 | call setline(1, ['buffer 1', 'line2', 'line3', 'line4']) |
| 684 | call assert_fails(':diffthis', 'E788') |
| 685 | call assert_equal(1, &diff) |
| 686 | vnew |
| 687 | call setline(1, ['buffer 2', 'line 2', 'line 3', 'line4']) |
| 688 | call assert_fails(':diffthis', 'E788') |
| 689 | call assert_equal(1, &diff) |
| 690 | bw! |
| 691 | call assert_fails(':diffoff!', 'E788') |
| 692 | bw! |
| 693 | |
| 694 | " Cleanup |
| 695 | au! OptionSet |
| 696 | call test_override('starting', 0) |
| 697 | "delfunc! AutoCommandOptionSet |
| 698 | endfunc |
Bram Moolenaar | 4a137b4 | 2017-08-04 22:37:11 +0200 | [diff] [blame] | 699 | |
| 700 | " Test for Bufleave autocommand that deletes the buffer we are about to edit. |
| 701 | func Test_BufleaveWithDelete() |
| 702 | new | edit Xfile1 |
| 703 | |
| 704 | augroup test_bufleavewithdelete |
| 705 | autocmd! |
| 706 | autocmd BufLeave Xfile1 bwipe Xfile2 |
| 707 | augroup END |
| 708 | |
| 709 | call assert_fails('edit Xfile2', 'E143:') |
| 710 | call assert_equal('Xfile1', bufname('%')) |
| 711 | |
| 712 | autocmd! test_bufleavewithdelete BufLeave Xfile1 |
| 713 | augroup! test_bufleavewithdelete |
| 714 | |
| 715 | new |
| 716 | bwipe! Xfile1 |
| 717 | endfunc |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 718 | |
| 719 | " Test for autocommand that changes the buffer list, when doing ":ball". |
| 720 | func Test_Acmd_BufAll() |
| 721 | enew! |
| 722 | %bwipe! |
| 723 | call writefile(['Test file Xxx1'], 'Xxx1') |
| 724 | call writefile(['Test file Xxx2'], 'Xxx2') |
| 725 | call writefile(['Test file Xxx3'], 'Xxx3') |
| 726 | |
| 727 | " Add three files to the buffer list |
| 728 | split Xxx1 |
| 729 | close |
| 730 | split Xxx2 |
| 731 | close |
| 732 | split Xxx3 |
| 733 | close |
| 734 | |
| 735 | " Wipe the buffer when the buffer is opened |
| 736 | au BufReadPost Xxx2 bwipe |
| 737 | |
| 738 | call append(0, 'Test file Xxx4') |
| 739 | ball |
| 740 | |
| 741 | call assert_equal(2, winnr('$')) |
| 742 | call assert_equal('Xxx1', bufname(winbufnr(winnr('$')))) |
| 743 | wincmd t |
| 744 | |
| 745 | au! BufReadPost |
| 746 | %bwipe! |
| 747 | call delete('Xxx1') |
| 748 | call delete('Xxx2') |
| 749 | call delete('Xxx3') |
| 750 | enew! | only |
| 751 | endfunc |
| 752 | |
| 753 | " Test for autocommand that changes current buffer on BufEnter event. |
| 754 | " Check if modelines are interpreted for the correct buffer. |
| 755 | func Test_Acmd_BufEnter() |
| 756 | %bwipe! |
| 757 | call writefile(['start of test file Xxx1', |
| 758 | \ "\<Tab>this is a test", |
| 759 | \ 'end of test file Xxx1'], 'Xxx1') |
| 760 | call writefile(['start of test file Xxx2', |
| 761 | \ 'vim: set noai :', |
| 762 | \ "\<Tab>this is a test", |
| 763 | \ 'end of test file Xxx2'], 'Xxx2') |
| 764 | |
| 765 | au BufEnter Xxx2 brew |
| 766 | set ai modeline modelines=3 |
| 767 | edit Xxx1 |
| 768 | " edit Xxx2, autocmd will do :brew |
| 769 | edit Xxx2 |
| 770 | exe "normal G?this is a\<CR>" |
| 771 | " Append text with autoindent to this file |
| 772 | normal othis should be auto-indented |
| 773 | call assert_equal("\<Tab>this should be auto-indented", getline('.')) |
| 774 | call assert_equal(3, line('.')) |
| 775 | " Remove autocmd and edit Xxx2 again |
| 776 | au! BufEnter Xxx2 |
| 777 | buf! Xxx2 |
| 778 | exe "normal G?this is a\<CR>" |
| 779 | " append text without autoindent to Xxx |
| 780 | normal othis should be in column 1 |
| 781 | call assert_equal("this should be in column 1", getline('.')) |
| 782 | call assert_equal(4, line('.')) |
| 783 | |
| 784 | %bwipe! |
| 785 | call delete('Xxx1') |
| 786 | call delete('Xxx2') |
| 787 | set ai&vim modeline&vim modelines&vim |
| 788 | endfunc |
| 789 | |
| 790 | " Test for issue #57 |
| 791 | " do not move cursor on <c-o> when autoindent is set |
| 792 | func Test_ai_CTRL_O() |
| 793 | enew! |
| 794 | set ai |
| 795 | let save_fo = &fo |
| 796 | set fo+=r |
| 797 | exe "normal o# abcdef\<Esc>2hi\<CR>\<C-O>d0\<Esc>" |
| 798 | exe "normal o# abcdef\<Esc>2hi\<C-O>d0\<Esc>" |
| 799 | call assert_equal(['# abc', 'def', 'def'], getline(2, 4)) |
| 800 | |
| 801 | set ai&vim |
| 802 | let &fo = save_fo |
| 803 | enew! |
| 804 | endfunc |
| 805 | |
| 806 | " Test for autocommand that deletes the current buffer on BufLeave event. |
| 807 | " Also test deleting the last buffer, should give a new, empty buffer. |
| 808 | func Test_BufLeave_Wipe() |
| 809 | %bwipe! |
| 810 | let content = ['start of test file Xxx', |
| 811 | \ 'this is a test', |
| 812 | \ 'end of test file Xxx'] |
| 813 | call writefile(content, 'Xxx1') |
| 814 | call writefile(content, 'Xxx2') |
| 815 | |
| 816 | au BufLeave Xxx2 bwipe |
| 817 | edit Xxx1 |
| 818 | split Xxx2 |
| 819 | " delete buffer Xxx2, we should be back to Xxx1 |
| 820 | bwipe |
| 821 | call assert_equal('Xxx1', bufname('%')) |
| 822 | call assert_equal(1, winnr('$')) |
| 823 | |
| 824 | " Create an alternate buffer |
| 825 | %write! test.out |
| 826 | call assert_equal('test.out', bufname('#')) |
| 827 | " delete alternate buffer |
| 828 | bwipe test.out |
| 829 | call assert_equal('Xxx1', bufname('%')) |
| 830 | call assert_equal('', bufname('#')) |
| 831 | |
| 832 | au BufLeave Xxx1 bwipe |
| 833 | " delete current buffer, get an empty one |
| 834 | bwipe! |
| 835 | call assert_equal(1, line('$')) |
| 836 | call assert_equal('', bufname('%')) |
Bram Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 837 | let g:bufinfo = getbufinfo() |
| 838 | call assert_equal(1, len(g:bufinfo)) |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 839 | |
| 840 | call delete('Xxx1') |
| 841 | call delete('Xxx2') |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 842 | call delete('test.out') |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 843 | %bwipe |
| 844 | au! BufLeave |
Bram Moolenaar | b2c8750 | 2017-10-14 21:15:58 +0200 | [diff] [blame] | 845 | |
| 846 | " check that bufinfo doesn't contain a pointer to freed memory |
| 847 | call test_garbagecollect_now() |
Bram Moolenaar | 4a6fcf8 | 2017-10-12 21:29:22 +0200 | [diff] [blame] | 848 | endfunc |
Bram Moolenaar | 87ffb5c | 2017-10-19 12:37:42 +0200 | [diff] [blame] | 849 | |
| 850 | func Test_QuitPre() |
| 851 | edit Xfoo |
| 852 | let winid = win_getid(winnr()) |
| 853 | split Xbar |
| 854 | au! QuitPre * let g:afile = expand('<afile>') |
| 855 | " Close the other window, <afile> should be correct. |
| 856 | exe win_id2win(winid) . 'q' |
| 857 | call assert_equal('Xfoo', g:afile) |
| 858 | |
| 859 | unlet g:afile |
| 860 | bwipe Xfoo |
| 861 | bwipe Xbar |
| 862 | endfunc |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 863 | |
| 864 | func Test_Cmdline() |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 865 | au! CmdlineChanged : let g:text = getcmdline() |
| 866 | let g:text = 0 |
| 867 | call feedkeys(":echom 'hello'\<CR>", 'xt') |
| 868 | call assert_equal("echom 'hello'", g:text) |
| 869 | au! CmdlineChanged |
| 870 | |
| 871 | au! CmdlineChanged : let g:entered = expand('<afile>') |
| 872 | let g:entered = 0 |
| 873 | call feedkeys(":echom 'hello'\<CR>", 'xt') |
| 874 | call assert_equal(':', g:entered) |
| 875 | au! CmdlineChanged |
| 876 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 877 | au! CmdlineEnter : let g:entered = expand('<afile>') |
| 878 | au! CmdlineLeave : let g:left = expand('<afile>') |
| 879 | let g:entered = 0 |
| 880 | let g:left = 0 |
| 881 | call feedkeys(":echo 'hello'\<CR>", 'xt') |
| 882 | call assert_equal(':', g:entered) |
| 883 | call assert_equal(':', g:left) |
| 884 | au! CmdlineEnter |
| 885 | au! CmdlineLeave |
| 886 | |
Bram Moolenaar | a4baf5b | 2018-04-22 13:27:44 +0200 | [diff] [blame] | 887 | let save_shellslash = &shellslash |
| 888 | set noshellslash |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 889 | au! CmdlineEnter / let g:entered = expand('<afile>') |
| 890 | au! CmdlineLeave / let g:left = expand('<afile>') |
| 891 | let g:entered = 0 |
| 892 | let g:left = 0 |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 893 | new |
| 894 | call setline(1, 'hello') |
| 895 | call feedkeys("/hello\<CR>", 'xt') |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 896 | call assert_equal('/', g:entered) |
| 897 | call assert_equal('/', g:left) |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 898 | bwipe! |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 899 | au! CmdlineEnter |
| 900 | au! CmdlineLeave |
Bram Moolenaar | a4baf5b | 2018-04-22 13:27:44 +0200 | [diff] [blame] | 901 | let &shellslash = save_shellslash |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 902 | endfunc |
Bram Moolenaar | 53f0c96 | 2017-10-22 14:23:59 +0200 | [diff] [blame] | 903 | |
| 904 | " Test for BufWritePre autocommand that deletes or unloads the buffer. |
| 905 | func Test_BufWritePre() |
| 906 | %bwipe |
| 907 | au BufWritePre Xxx1 bunload |
| 908 | au BufWritePre Xxx2 bwipe |
| 909 | |
| 910 | call writefile(['start of Xxx1', 'test', 'end of Xxx1'], 'Xxx1') |
| 911 | call writefile(['start of Xxx2', 'test', 'end of Xxx2'], 'Xxx2') |
| 912 | |
| 913 | edit Xtest |
| 914 | e! Xxx2 |
| 915 | bdel Xtest |
| 916 | e Xxx1 |
| 917 | " write it, will unload it and give an error msg |
| 918 | call assert_fails('w', 'E203') |
| 919 | call assert_equal('Xxx2', bufname('%')) |
| 920 | edit Xtest |
| 921 | e! Xxx2 |
| 922 | bwipe Xtest |
| 923 | " write it, will delete the buffer and give an error msg |
| 924 | call assert_fails('w', 'E203') |
| 925 | call assert_equal('Xxx1', bufname('%')) |
| 926 | au! BufWritePre |
| 927 | call delete('Xxx1') |
| 928 | call delete('Xxx2') |
| 929 | endfunc |
| 930 | |
| 931 | " Test for BufUnload autocommand that unloads all the other buffers |
| 932 | func Test_bufunload_all() |
| 933 | call writefile(['Test file Xxx1'], 'Xxx1')" |
| 934 | call writefile(['Test file Xxx2'], 'Xxx2')" |
| 935 | |
| 936 | let content = [ |
| 937 | \ "func UnloadAllBufs()", |
| 938 | \ " let i = 1", |
| 939 | \ " while i <= bufnr('$')", |
| 940 | \ " if i != bufnr('%') && bufloaded(i)", |
| 941 | \ " exe i . 'bunload'", |
| 942 | \ " endif", |
| 943 | \ " let i += 1", |
| 944 | \ " endwhile", |
| 945 | \ "endfunc", |
| 946 | \ "au BufUnload * call UnloadAllBufs()", |
| 947 | \ "au VimLeave * call writefile(['Test Finished'], 'Xout')", |
| 948 | \ "edit Xxx1", |
| 949 | \ "split Xxx2", |
| 950 | \ "q"] |
| 951 | call writefile(content, 'Xtest') |
| 952 | |
| 953 | call delete('Xout') |
| 954 | call system(v:progpath. ' --clean -N --not-a-term -S Xtest') |
| 955 | call assert_true(filereadable('Xout')) |
| 956 | |
| 957 | call delete('Xxx1') |
| 958 | call delete('Xxx2') |
| 959 | call delete('Xtest') |
| 960 | call delete('Xout') |
| 961 | endfunc |
| 962 | |
| 963 | " Some tests for buffer-local autocommands |
| 964 | func Test_buflocal_autocmd() |
| 965 | let g:bname = '' |
| 966 | edit xx |
| 967 | au BufLeave <buffer> let g:bname = expand("%") |
| 968 | " here, autocommand for xx should trigger. |
| 969 | " but autocommand shall not apply to buffer named <buffer>. |
| 970 | edit somefile |
| 971 | call assert_equal('xx', g:bname) |
| 972 | let g:bname = '' |
| 973 | " here, autocommand shall be auto-deleted |
| 974 | bwipe xx |
| 975 | " autocmd should not trigger |
| 976 | edit xx |
| 977 | call assert_equal('', g:bname) |
| 978 | " autocmd should not trigger |
| 979 | edit somefile |
| 980 | call assert_equal('', g:bname) |
| 981 | enew |
| 982 | unlet g:bname |
| 983 | endfunc |
Bram Moolenaar | 430dc5d | 2017-11-02 21:04:47 +0100 | [diff] [blame] | 984 | |
| 985 | " Test for "*Cmd" autocommands |
| 986 | func Test_Cmd_Autocmds() |
| 987 | call writefile(['start of Xxx', "\tabc2", 'end of Xxx'], 'Xxx') |
| 988 | |
| 989 | enew! |
| 990 | au BufReadCmd XtestA 0r Xxx|$del |
| 991 | edit XtestA " will read text of Xxd instead |
| 992 | call assert_equal('start of Xxx', getline(1)) |
| 993 | |
| 994 | au BufWriteCmd XtestA call append(line("$"), "write") |
| 995 | write " will append a line to the file |
| 996 | call assert_equal('write', getline('$')) |
| 997 | call assert_fails('read XtestA', 'E484') " should not read anything |
| 998 | call assert_equal('write', getline(4)) |
| 999 | |
| 1000 | " now we have: |
| 1001 | " 1 start of Xxx |
| 1002 | " 2 abc2 |
| 1003 | " 3 end of Xxx |
| 1004 | " 4 write |
| 1005 | |
| 1006 | au FileReadCmd XtestB '[r Xxx |
| 1007 | 2r XtestB " will read Xxx below line 2 instead |
| 1008 | call assert_equal('start of Xxx', getline(3)) |
| 1009 | |
| 1010 | " now we have: |
| 1011 | " 1 start of Xxx |
| 1012 | " 2 abc2 |
| 1013 | " 3 start of Xxx |
| 1014 | " 4 abc2 |
| 1015 | " 5 end of Xxx |
| 1016 | " 6 end of Xxx |
| 1017 | " 7 write |
| 1018 | |
| 1019 | au FileWriteCmd XtestC '[,']copy $ |
| 1020 | normal 4GA1 |
| 1021 | 4,5w XtestC " will copy lines 4 and 5 to the end |
| 1022 | call assert_equal("\tabc21", getline(8)) |
| 1023 | call assert_fails('r XtestC', 'E484') " should not read anything |
| 1024 | call assert_equal("end of Xxx", getline(9)) |
| 1025 | |
| 1026 | " now we have: |
| 1027 | " 1 start of Xxx |
| 1028 | " 2 abc2 |
| 1029 | " 3 start of Xxx |
| 1030 | " 4 abc21 |
| 1031 | " 5 end of Xxx |
| 1032 | " 6 end of Xxx |
| 1033 | " 7 write |
| 1034 | " 8 abc21 |
| 1035 | " 9 end of Xxx |
| 1036 | |
| 1037 | let g:lines = [] |
| 1038 | au FileAppendCmd XtestD call extend(g:lines, getline(line("'["), line("']"))) |
| 1039 | w >>XtestD " will add lines to 'lines' |
| 1040 | call assert_equal(9, len(g:lines)) |
| 1041 | call assert_fails('$r XtestD', 'E484') " should not read anything |
| 1042 | call assert_equal(9, line('$')) |
| 1043 | call assert_equal('end of Xxx', getline('$')) |
| 1044 | |
| 1045 | au BufReadCmd XtestE 0r Xxx|$del |
| 1046 | sp XtestE " split window with test.out |
| 1047 | call assert_equal('end of Xxx', getline(3)) |
| 1048 | |
| 1049 | let g:lines = [] |
| 1050 | exe "normal 2Goasdf\<Esc>\<C-W>\<C-W>" |
| 1051 | au BufWriteCmd XtestE call extend(g:lines, getline(0, '$')) |
| 1052 | wall " will write other window to 'lines' |
| 1053 | call assert_equal(4, len(g:lines), g:lines) |
| 1054 | call assert_equal('asdf', g:lines[2]) |
| 1055 | |
| 1056 | au! BufReadCmd |
| 1057 | au! BufWriteCmd |
| 1058 | au! FileReadCmd |
| 1059 | au! FileWriteCmd |
| 1060 | au! FileAppendCmd |
| 1061 | %bwipe! |
| 1062 | call delete('Xxx') |
| 1063 | enew! |
| 1064 | endfunc |
Bram Moolenaar | aace215 | 2017-11-05 16:23:10 +0100 | [diff] [blame] | 1065 | |
| 1066 | func SetChangeMarks(start, end) |
| 1067 | exe a:start. 'mark [' |
| 1068 | exe a:end. 'mark ]' |
| 1069 | endfunc |
| 1070 | |
| 1071 | " Verify the effects of autocmds on '[ and '] |
| 1072 | func Test_change_mark_in_autocmds() |
| 1073 | edit! Xtest |
| 1074 | call feedkeys("ia\<CR>b\<CR>c\<CR>d\<C-g>u", 'xtn') |
| 1075 | |
| 1076 | call SetChangeMarks(2, 3) |
| 1077 | write |
| 1078 | call assert_equal([1, 4], [line("'["), line("']")]) |
| 1079 | |
| 1080 | call SetChangeMarks(2, 3) |
| 1081 | au BufWritePre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1082 | write |
| 1083 | au! BufWritePre |
| 1084 | |
| 1085 | if executable('cat') |
| 1086 | write XtestFilter |
| 1087 | write >> XtestFilter |
| 1088 | |
| 1089 | call SetChangeMarks(2, 3) |
| 1090 | " Marks are set to the entire range of the write |
| 1091 | au FilterWritePre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1092 | " '[ is adjusted to just before the line that will receive the filtered |
| 1093 | " data |
| 1094 | au FilterReadPre * call assert_equal([4, 4], [line("'["), line("']")]) |
| 1095 | " The filtered data is read into the buffer, and the source lines are |
| 1096 | " still present, so the range is after the source lines |
| 1097 | au FilterReadPost * call assert_equal([5, 12], [line("'["), line("']")]) |
| 1098 | %!cat XtestFilter |
| 1099 | " After the filtered data is read, the original lines are deleted |
| 1100 | call assert_equal([1, 8], [line("'["), line("']")]) |
| 1101 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 1102 | undo |
| 1103 | |
| 1104 | call SetChangeMarks(1, 4) |
| 1105 | au FilterWritePre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1106 | au FilterReadPre * call assert_equal([3, 3], [line("'["), line("']")]) |
| 1107 | au FilterReadPost * call assert_equal([4, 11], [line("'["), line("']")]) |
| 1108 | 2,3!cat XtestFilter |
| 1109 | call assert_equal([2, 9], [line("'["), line("']")]) |
| 1110 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 1111 | undo |
| 1112 | |
| 1113 | call delete('XtestFilter') |
| 1114 | endif |
| 1115 | |
| 1116 | call SetChangeMarks(1, 4) |
| 1117 | au FileWritePre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1118 | 2,3write Xtest2 |
| 1119 | au! FileWritePre |
| 1120 | |
| 1121 | call SetChangeMarks(2, 3) |
| 1122 | au FileAppendPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1123 | write >> Xtest2 |
| 1124 | au! FileAppendPre |
| 1125 | |
| 1126 | call SetChangeMarks(1, 4) |
| 1127 | au FileAppendPre * call assert_equal([2, 3], [line("'["), line("']")]) |
| 1128 | 2,3write >> Xtest2 |
| 1129 | au! FileAppendPre |
| 1130 | |
| 1131 | call SetChangeMarks(1, 1) |
| 1132 | au FileReadPre * call assert_equal([3, 1], [line("'["), line("']")]) |
| 1133 | au FileReadPost * call assert_equal([4, 11], [line("'["), line("']")]) |
| 1134 | 3read Xtest2 |
| 1135 | au! FileReadPre,FileReadPost |
| 1136 | undo |
| 1137 | |
| 1138 | call SetChangeMarks(4, 4) |
| 1139 | " When the line is 0, it's adjusted to 1 |
| 1140 | au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1141 | au FileReadPost * call assert_equal([1, 8], [line("'["), line("']")]) |
| 1142 | 0read Xtest2 |
| 1143 | au! FileReadPre,FileReadPost |
| 1144 | undo |
| 1145 | |
| 1146 | call SetChangeMarks(4, 4) |
| 1147 | " When the line is 0, it's adjusted to 1 |
| 1148 | au FileReadPre * call assert_equal([1, 4], [line("'["), line("']")]) |
| 1149 | au FileReadPost * call assert_equal([2, 9], [line("'["), line("']")]) |
| 1150 | 1read Xtest2 |
| 1151 | au! FileReadPre,FileReadPost |
| 1152 | undo |
| 1153 | |
| 1154 | bwipe! |
| 1155 | call delete('Xtest') |
| 1156 | call delete('Xtest2') |
| 1157 | endfunc |
| 1158 | |
| 1159 | func Test_Filter_noshelltemp() |
| 1160 | if !executable('cat') |
| 1161 | return |
| 1162 | endif |
| 1163 | |
| 1164 | enew! |
| 1165 | call setline(1, ['a', 'b', 'c', 'd']) |
| 1166 | |
| 1167 | let shelltemp = &shelltemp |
| 1168 | set shelltemp |
| 1169 | |
| 1170 | let g:filter_au = 0 |
| 1171 | au FilterWritePre * let g:filter_au += 1 |
| 1172 | au FilterReadPre * let g:filter_au += 1 |
| 1173 | au FilterReadPost * let g:filter_au += 1 |
| 1174 | %!cat |
| 1175 | call assert_equal(3, g:filter_au) |
| 1176 | |
| 1177 | if has('filterpipe') |
| 1178 | set noshelltemp |
| 1179 | |
| 1180 | let g:filter_au = 0 |
| 1181 | au FilterWritePre * let g:filter_au += 1 |
| 1182 | au FilterReadPre * let g:filter_au += 1 |
| 1183 | au FilterReadPost * let g:filter_au += 1 |
| 1184 | %!cat |
| 1185 | call assert_equal(0, g:filter_au) |
| 1186 | endif |
| 1187 | |
| 1188 | au! FilterWritePre,FilterReadPre,FilterReadPost |
| 1189 | let &shelltemp = shelltemp |
| 1190 | bwipe! |
| 1191 | endfunc |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1192 | |
| 1193 | func Test_TextYankPost() |
| 1194 | enew! |
| 1195 | call setline(1, ['foo']) |
| 1196 | |
| 1197 | let g:event = [] |
| 1198 | au TextYankPost * let g:event = copy(v:event) |
| 1199 | |
| 1200 | call assert_equal({}, v:event) |
| 1201 | call assert_fails('let v:event = {}', 'E46:') |
| 1202 | call assert_fails('let v:event.mykey = 0', 'E742:') |
| 1203 | |
| 1204 | norm "ayiw |
| 1205 | call assert_equal( |
| 1206 | \{'regcontents': ['foo'], 'regname': 'a', 'operator': 'y', 'regtype': 'v'}, |
| 1207 | \g:event) |
| 1208 | norm y_ |
| 1209 | call assert_equal( |
| 1210 | \{'regcontents': ['foo'], 'regname': '', 'operator': 'y', 'regtype': 'V'}, |
| 1211 | \g:event) |
| 1212 | call feedkeys("\<C-V>y", 'x') |
| 1213 | call assert_equal( |
| 1214 | \{'regcontents': ['f'], 'regname': '', 'operator': 'y', 'regtype': "\x161"}, |
| 1215 | \g:event) |
| 1216 | norm "xciwbar |
| 1217 | call assert_equal( |
| 1218 | \{'regcontents': ['foo'], 'regname': 'x', 'operator': 'c', 'regtype': 'v'}, |
| 1219 | \g:event) |
| 1220 | norm "bdiw |
| 1221 | call assert_equal( |
| 1222 | \{'regcontents': ['bar'], 'regname': 'b', 'operator': 'd', 'regtype': 'v'}, |
| 1223 | \g:event) |
| 1224 | |
| 1225 | call assert_equal({}, v:event) |
| 1226 | |
| 1227 | au! TextYankPost |
| 1228 | unlet g:event |
| 1229 | bwipe! |
| 1230 | endfunc |
Bram Moolenaar | 9bca805 | 2017-12-18 12:37:55 +0100 | [diff] [blame] | 1231 | |
| 1232 | func Test_nocatch_wipe_all_buffers() |
| 1233 | " Real nasty autocommand: wipe all buffers on any event. |
| 1234 | au * * bwipe * |
Bram Moolenaar | a997b45 | 2018-04-17 23:24:06 +0200 | [diff] [blame] | 1235 | " Get E93 first? |
| 1236 | " call assert_fails('next x', 'E93:') |
| 1237 | call assert_fails('next x', 'E517:') |
Bram Moolenaar | 9bca805 | 2017-12-18 12:37:55 +0100 | [diff] [blame] | 1238 | bwipe |
| 1239 | au! |
| 1240 | endfunc |
Bram Moolenaar | 4fb921e | 2017-12-18 15:33:00 +0100 | [diff] [blame] | 1241 | |
| 1242 | func Test_nocatch_wipe_dummy_buffer() |
| 1243 | " Nasty autocommand: wipe buffer on any event. |
| 1244 | au * x bwipe |
| 1245 | call assert_fails('lv½ /x', 'E480') |
| 1246 | au! |
| 1247 | endfunc |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1248 | |
| 1249 | function s:Before_test_dirchanged() |
| 1250 | augroup test_dirchanged |
| 1251 | autocmd! |
| 1252 | augroup END |
| 1253 | let s:li = [] |
| 1254 | let s:dir_this = getcwd() |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1255 | let s:dir_foo = s:dir_this . '/foo' |
| 1256 | call mkdir(s:dir_foo) |
| 1257 | let s:dir_bar = s:dir_this . '/bar' |
| 1258 | call mkdir(s:dir_bar) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1259 | endfunc |
| 1260 | |
| 1261 | function s:After_test_dirchanged() |
| 1262 | exe 'cd' s:dir_this |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1263 | call delete(s:dir_foo, 'd') |
| 1264 | call delete(s:dir_bar, 'd') |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1265 | augroup test_dirchanged |
| 1266 | autocmd! |
| 1267 | augroup END |
| 1268 | endfunc |
| 1269 | |
| 1270 | function Test_dirchanged_global() |
| 1271 | call s:Before_test_dirchanged() |
| 1272 | autocmd test_dirchanged DirChanged global call add(s:li, "cd:") |
| 1273 | autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>")) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1274 | exe 'cd' s:dir_foo |
| 1275 | call assert_equal(["cd:", s:dir_foo], s:li) |
| 1276 | exe 'cd' s:dir_foo |
| 1277 | call assert_equal(["cd:", s:dir_foo], s:li) |
| 1278 | exe 'lcd' s:dir_bar |
| 1279 | call assert_equal(["cd:", s:dir_foo], s:li) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1280 | call s:After_test_dirchanged() |
| 1281 | endfunc |
| 1282 | |
| 1283 | function Test_dirchanged_local() |
| 1284 | call s:Before_test_dirchanged() |
| 1285 | autocmd test_dirchanged DirChanged window call add(s:li, "lcd:") |
| 1286 | autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>")) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1287 | exe 'cd' s:dir_foo |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1288 | call assert_equal([], s:li) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1289 | exe 'lcd' s:dir_bar |
| 1290 | call assert_equal(["lcd:", s:dir_bar], s:li) |
| 1291 | exe 'lcd' s:dir_bar |
| 1292 | call assert_equal(["lcd:", s:dir_bar], s:li) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1293 | call s:After_test_dirchanged() |
| 1294 | endfunc |
| 1295 | |
| 1296 | function Test_dirchanged_auto() |
Bram Moolenaar | ec48a9c | 2018-02-03 20:11:40 +0100 | [diff] [blame] | 1297 | if !exists('+autochdir') |
| 1298 | return |
| 1299 | endif |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1300 | call s:Before_test_dirchanged() |
| 1301 | call test_autochdir() |
| 1302 | autocmd test_dirchanged DirChanged auto call add(s:li, "auto:") |
| 1303 | autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>")) |
| 1304 | set acd |
| 1305 | exe 'cd ..' |
| 1306 | call assert_equal([], s:li) |
Bram Moolenaar | 2caad3f | 2018-12-16 15:38:02 +0100 | [diff] [blame] | 1307 | exe 'edit ' . s:dir_foo . '/Xfile' |
| 1308 | call assert_equal(s:dir_foo, getcwd()) |
| 1309 | call assert_equal(["auto:", s:dir_foo], s:li) |
Bram Moolenaar | b7407d3 | 2018-02-03 17:36:27 +0100 | [diff] [blame] | 1310 | set noacd |
| 1311 | bwipe! |
| 1312 | call s:After_test_dirchanged() |
| 1313 | endfunc |
Bram Moolenaar | 5a09343 | 2018-02-10 18:15:19 +0100 | [diff] [blame] | 1314 | |
| 1315 | " Test TextChangedI and TextChangedP |
| 1316 | func Test_ChangedP() |
| 1317 | new |
| 1318 | call setline(1, ['foo', 'bar', 'foobar']) |
| 1319 | call test_override("char_avail", 1) |
| 1320 | set complete=. completeopt=menuone |
| 1321 | |
| 1322 | func! TextChangedAutocmd(char) |
| 1323 | let g:autocmd .= a:char |
| 1324 | endfunc |
| 1325 | |
| 1326 | au! TextChanged <buffer> :call TextChangedAutocmd('N') |
| 1327 | au! TextChangedI <buffer> :call TextChangedAutocmd('I') |
| 1328 | au! TextChangedP <buffer> :call TextChangedAutocmd('P') |
| 1329 | |
| 1330 | call cursor(3, 1) |
| 1331 | let g:autocmd = '' |
| 1332 | call feedkeys("o\<esc>", 'tnix') |
| 1333 | call assert_equal('I', g:autocmd) |
| 1334 | |
| 1335 | let g:autocmd = '' |
| 1336 | call feedkeys("Sf", 'tnix') |
| 1337 | call assert_equal('II', g:autocmd) |
| 1338 | |
| 1339 | let g:autocmd = '' |
| 1340 | call feedkeys("Sf\<C-N>", 'tnix') |
| 1341 | call assert_equal('IIP', g:autocmd) |
| 1342 | |
| 1343 | let g:autocmd = '' |
| 1344 | call feedkeys("Sf\<C-N>\<C-N>", 'tnix') |
| 1345 | call assert_equal('IIPP', g:autocmd) |
| 1346 | |
| 1347 | let g:autocmd = '' |
| 1348 | call feedkeys("Sf\<C-N>\<C-N>\<C-N>", 'tnix') |
| 1349 | call assert_equal('IIPPP', g:autocmd) |
| 1350 | |
| 1351 | let g:autocmd = '' |
| 1352 | call feedkeys("Sf\<C-N>\<C-N>\<C-N>\<C-N>", 'tnix') |
| 1353 | call assert_equal('IIPPPP', g:autocmd) |
| 1354 | |
| 1355 | call assert_equal(['foo', 'bar', 'foobar', 'foo'], getline(1, '$')) |
| 1356 | " TODO: how should it handle completeopt=noinsert,noselect? |
| 1357 | |
| 1358 | " CleanUp |
| 1359 | call test_override("char_avail", 0) |
| 1360 | au! TextChanged |
| 1361 | au! TextChangedI |
| 1362 | au! TextChangedP |
| 1363 | delfu TextChangedAutocmd |
| 1364 | unlet! g:autocmd |
| 1365 | set complete&vim completeopt&vim |
| 1366 | |
| 1367 | bw! |
| 1368 | endfunc |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 1369 | |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 1370 | let g:setline_handled = v:false |
Bram Moolenaar | 1e11536 | 2019-01-09 23:01:02 +0100 | [diff] [blame] | 1371 | func SetLineOne() |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 1372 | if !g:setline_handled |
| 1373 | call setline(1, "(x)") |
| 1374 | let g:setline_handled = v:true |
| 1375 | endif |
| 1376 | endfunc |
| 1377 | |
| 1378 | func Test_TextChangedI_with_setline() |
| 1379 | new |
| 1380 | call test_override('char_avail', 1) |
| 1381 | autocmd TextChangedI <buffer> call SetLineOne() |
| 1382 | call feedkeys("i(\<CR>\<Esc>", 'tx') |
| 1383 | call assert_equal('(', getline(1)) |
| 1384 | call assert_equal('x)', getline(2)) |
| 1385 | undo |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 1386 | call assert_equal('', getline(1)) |
Bram Moolenaar | 9fa9506 | 2018-08-08 22:08:32 +0200 | [diff] [blame] | 1387 | call assert_equal('', getline(2)) |
Bram Moolenaar | 91d2e78 | 2018-08-07 19:05:01 +0200 | [diff] [blame] | 1388 | |
| 1389 | call test_override('starting', 0) |
| 1390 | bwipe! |
| 1391 | endfunc |
| 1392 | |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 1393 | func Test_Changed_FirstTime() |
| 1394 | if !has('terminal') || has('gui_running') |
| 1395 | return |
| 1396 | endif |
| 1397 | " Prepare file for TextChanged event. |
| 1398 | call writefile([''], 'Xchanged.txt') |
| 1399 | let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile'], {'term_rows': 3}) |
| 1400 | call assert_equal('running', term_getstatus(buf)) |
Bram Moolenaar | 1834d37 | 2018-03-29 17:40:46 +0200 | [diff] [blame] | 1401 | " Wait for the ruler (in the status line) to be shown. |
Bram Moolenaar | aa5df7e | 2019-02-03 14:53:10 +0100 | [diff] [blame] | 1402 | " In ConPTY, there is additional character which is drawn up to the width of |
| 1403 | " the screen. |
| 1404 | if has('conpty') |
| 1405 | call WaitForAssert({-> assert_match('\<All.*$', term_getline(buf, 3))}) |
| 1406 | else |
| 1407 | call WaitForAssert({-> assert_match('\<All$', term_getline(buf, 3))}) |
| 1408 | endif |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 1409 | " It's only adding autocmd, so that no event occurs. |
| 1410 | call term_sendkeys(buf, ":au! TextChanged <buffer> call writefile(['No'], 'Xchanged.txt')\<cr>") |
| 1411 | call term_sendkeys(buf, "\<C-\\>\<C-N>:qa!\<cr>") |
Bram Moolenaar | 50182fa | 2018-04-28 21:34:40 +0200 | [diff] [blame] | 1412 | call WaitForAssert({-> assert_equal('finished', term_getstatus(buf))}) |
Bram Moolenaar | 8c64a36 | 2018-03-23 22:39:31 +0100 | [diff] [blame] | 1413 | call assert_equal([''], readfile('Xchanged.txt')) |
| 1414 | |
| 1415 | " clean up |
| 1416 | call delete('Xchanged.txt') |
| 1417 | bwipe! |
| 1418 | endfunc |
Bram Moolenaar | 0566e89 | 2019-01-24 19:37:40 +0100 | [diff] [blame] | 1419 | |
Bram Moolenaar | eb93f3f | 2019-04-04 15:04:56 +0200 | [diff] [blame] | 1420 | func Test_autocmd_nested() |
| 1421 | let g:did_nested = 0 |
| 1422 | augroup Testing |
| 1423 | au WinNew * edit somefile |
| 1424 | au BufNew * let g:did_nested = 1 |
| 1425 | augroup END |
| 1426 | split |
| 1427 | call assert_equal(0, g:did_nested) |
| 1428 | close |
| 1429 | bwipe! somefile |
| 1430 | |
| 1431 | " old nested argument still works |
| 1432 | augroup Testing |
| 1433 | au! |
| 1434 | au WinNew * nested edit somefile |
| 1435 | au BufNew * let g:did_nested = 1 |
| 1436 | augroup END |
| 1437 | split |
| 1438 | call assert_equal(1, g:did_nested) |
| 1439 | close |
| 1440 | bwipe! somefile |
| 1441 | |
| 1442 | " New ++nested argument works |
| 1443 | augroup Testing |
| 1444 | au! |
| 1445 | au WinNew * ++nested edit somefile |
| 1446 | au BufNew * let g:did_nested = 1 |
| 1447 | augroup END |
| 1448 | split |
| 1449 | call assert_equal(1, g:did_nested) |
| 1450 | close |
| 1451 | bwipe! somefile |
| 1452 | |
| 1453 | augroup Testing |
| 1454 | au! |
| 1455 | augroup END |
| 1456 | |
| 1457 | call assert_fails('au WinNew * ++nested ++nested echo bad', 'E983:') |
| 1458 | call assert_fails('au WinNew * nested nested echo bad', 'E983:') |
| 1459 | endfunc |
| 1460 | |
| 1461 | func Test_autocmd_once() |
| 1462 | " Without ++once WinNew triggers twice |
| 1463 | let g:did_split = 0 |
| 1464 | augroup Testing |
| 1465 | au WinNew * let g:did_split += 1 |
| 1466 | augroup END |
| 1467 | split |
| 1468 | split |
| 1469 | call assert_equal(2, g:did_split) |
| 1470 | call assert_true(exists('#WinNew')) |
| 1471 | close |
| 1472 | close |
| 1473 | |
| 1474 | " With ++once WinNew triggers once |
| 1475 | let g:did_split = 0 |
| 1476 | augroup Testing |
| 1477 | au! |
| 1478 | au WinNew * ++once let g:did_split += 1 |
| 1479 | augroup END |
| 1480 | split |
| 1481 | split |
| 1482 | call assert_equal(1, g:did_split) |
| 1483 | call assert_false(exists('#WinNew')) |
| 1484 | close |
| 1485 | close |
| 1486 | |
| 1487 | call assert_fails('au WinNew * ++once ++once echo bad', 'E983:') |
| 1488 | endfunc |
| 1489 | |
Bram Moolenaar | a68e595 | 2019-04-25 22:22:01 +0200 | [diff] [blame] | 1490 | func Test_autocmd_bufreadpre() |
| 1491 | new |
| 1492 | let b:bufreadpre = 1 |
| 1493 | call append(0, range(100)) |
| 1494 | w! XAutocmdBufReadPre.txt |
| 1495 | autocmd BufReadPre <buffer> :let b:bufreadpre += 1 |
| 1496 | norm! 50gg |
| 1497 | sp |
| 1498 | norm! 100gg |
| 1499 | wincmd p |
| 1500 | let g:wsv1 = winsaveview() |
| 1501 | wincmd p |
| 1502 | let g:wsv2 = winsaveview() |
| 1503 | " triggers BufReadPre, should not move the cursor in either window |
| 1504 | " The topline may change one line in a large window. |
| 1505 | edit |
| 1506 | call assert_inrange(g:wsv2.topline - 1, g:wsv2.topline + 1, winsaveview().topline) |
| 1507 | call assert_equal(g:wsv2.lnum, winsaveview().lnum) |
| 1508 | call assert_equal(2, b:bufreadpre) |
| 1509 | wincmd p |
| 1510 | call assert_equal(g:wsv1.topline, winsaveview().topline) |
| 1511 | call assert_equal(g:wsv1.lnum, winsaveview().lnum) |
| 1512 | call assert_equal(2, b:bufreadpre) |
| 1513 | " Now set the cursor position in an BufReadPre autocommand |
| 1514 | " (even though the position will be invalid, this should make Vim reset the |
| 1515 | " cursor position in the other window. |
| 1516 | wincmd p |
| 1517 | set cpo+=g |
| 1518 | " won't do anything, but try to set the cursor on an invalid lnum |
| 1519 | autocmd BufReadPre <buffer> :norm! 70gg |
| 1520 | " triggers BufReadPre, should not move the cursor in either window |
| 1521 | e |
| 1522 | call assert_equal(1, winsaveview().topline) |
| 1523 | call assert_equal(1, winsaveview().lnum) |
| 1524 | call assert_equal(3, b:bufreadpre) |
| 1525 | wincmd p |
| 1526 | call assert_equal(g:wsv1.topline, winsaveview().topline) |
| 1527 | call assert_equal(g:wsv1.lnum, winsaveview().lnum) |
| 1528 | call assert_equal(3, b:bufreadpre) |
| 1529 | close |
| 1530 | close |
| 1531 | call delete('XAutocmdBufReadPre.txt') |
| 1532 | set cpo-=g |
| 1533 | endfunc |
| 1534 | |
Bram Moolenaar | 5e66b42 | 2019-01-24 21:58:10 +0100 | [diff] [blame] | 1535 | " FileChangedShell tested in test_filechanged.vim |
Bram Moolenaar | 69ea587 | 2019-04-25 20:29:00 +0200 | [diff] [blame] | 1536 | |
| 1537 | " Tests for the following autocommands: |
| 1538 | " - FileWritePre writing a compressed file |
| 1539 | " - FileReadPost reading a compressed file |
| 1540 | " - BufNewFile reading a file template |
| 1541 | " - BufReadPre decompressing the file to be read |
| 1542 | " - FilterReadPre substituting characters in the temp file |
| 1543 | " - FilterReadPost substituting characters after filtering |
| 1544 | " - FileReadPre set options for decompression |
| 1545 | " - FileReadPost decompress the file |
| 1546 | func Test_ReadWrite_Autocmds() |
| 1547 | " Run this test only on Unix-like systems and if gzip is available |
| 1548 | if !has('unix') || !executable("gzip") |
| 1549 | return |
| 1550 | endif |
| 1551 | |
| 1552 | " Make $GZIP empty, "-v" would cause trouble. |
| 1553 | let $GZIP = "" |
| 1554 | |
| 1555 | " Use a FileChangedShell autocommand to avoid a prompt for 'Xtestfile.gz' |
| 1556 | " being modified outside of Vim (noticed on Solaris). |
| 1557 | au FileChangedShell * echo 'caught FileChangedShell' |
| 1558 | |
| 1559 | " Test for the FileReadPost, FileWritePre and FileWritePost autocmds |
| 1560 | augroup Test1 |
| 1561 | au! |
| 1562 | au FileWritePre *.gz '[,']!gzip |
| 1563 | au FileWritePost *.gz undo |
| 1564 | au FileReadPost *.gz '[,']!gzip -d |
| 1565 | augroup END |
| 1566 | |
| 1567 | new |
| 1568 | set bin |
| 1569 | call append(0, [ |
| 1570 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 1571 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1572 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 1573 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1574 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 1575 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1576 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 1577 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1578 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 1579 | \ ]) |
| 1580 | 1,9write! Xtestfile.gz |
| 1581 | enew! | close |
| 1582 | |
| 1583 | new |
| 1584 | " Read and decompress the testfile |
| 1585 | 0read Xtestfile.gz |
| 1586 | call assert_equal([ |
| 1587 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 1588 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1589 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 1590 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1591 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 1592 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1593 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 1594 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1595 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 1596 | \ ], getline(1, 9)) |
| 1597 | enew! | close |
| 1598 | |
| 1599 | augroup Test1 |
| 1600 | au! |
| 1601 | augroup END |
| 1602 | |
| 1603 | " Test for the FileAppendPre and FileAppendPost autocmds |
| 1604 | augroup Test2 |
| 1605 | au! |
| 1606 | au BufNewFile *.c read Xtest.c |
| 1607 | au FileAppendPre *.out '[,']s/new/NEW/ |
| 1608 | au FileAppendPost *.out !cat Xtest.c >> test.out |
| 1609 | augroup END |
| 1610 | |
| 1611 | call writefile(['/*', ' * Here is a new .c file', ' */'], 'Xtest.c') |
| 1612 | new foo.c " should load Xtest.c |
| 1613 | call assert_equal(['/*', ' * Here is a new .c file', ' */'], getline(2, 4)) |
| 1614 | w! >> test.out " append it to the output file |
| 1615 | |
| 1616 | let contents = readfile('test.out') |
| 1617 | call assert_equal(' * Here is a NEW .c file', contents[2]) |
| 1618 | call assert_equal(' * Here is a new .c file', contents[5]) |
| 1619 | |
| 1620 | call delete('test.out') |
| 1621 | enew! | close |
| 1622 | augroup Test2 |
| 1623 | au! |
| 1624 | augroup END |
| 1625 | |
| 1626 | " Test for the BufReadPre and BufReadPost autocmds |
| 1627 | augroup Test3 |
| 1628 | au! |
| 1629 | " setup autocommands to decompress before reading and re-compress |
| 1630 | " afterwards |
| 1631 | au BufReadPre *.gz exe '!gzip -d ' . shellescape(expand("<afile>")) |
| 1632 | au BufReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>")) |
| 1633 | au BufReadPost *.gz call rename(expand("<afile>"), expand("<afile>:r")) |
| 1634 | au BufReadPost *.gz exe '!gzip ' . shellescape(expand("<afile>:r")) |
| 1635 | augroup END |
| 1636 | |
| 1637 | e! Xtestfile.gz " Edit compressed file |
| 1638 | call assert_equal([ |
| 1639 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 1640 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1641 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 1642 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1643 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 1644 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1645 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 1646 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1647 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 1648 | \ ], getline(1, 9)) |
| 1649 | |
| 1650 | w! >> test.out " Append it to the output file |
| 1651 | |
| 1652 | augroup Test3 |
| 1653 | au! |
| 1654 | augroup END |
| 1655 | |
| 1656 | " Test for the FilterReadPre and FilterReadPost autocmds. |
| 1657 | set shelltemp " need temp files here |
| 1658 | augroup Test4 |
| 1659 | au! |
| 1660 | au FilterReadPre *.out call rename(expand("<afile>"), expand("<afile>") . ".t") |
| 1661 | au FilterReadPre *.out exe 'silent !sed s/e/E/ ' . shellescape(expand("<afile>")) . ".t >" . shellescape(expand("<afile>")) |
| 1662 | au FilterReadPre *.out exe 'silent !rm ' . shellescape(expand("<afile>")) . '.t' |
| 1663 | au FilterReadPost *.out '[,']s/x/X/g |
| 1664 | augroup END |
| 1665 | |
| 1666 | e! test.out " Edit the output file |
| 1667 | 1,$!cat |
| 1668 | call assert_equal([ |
| 1669 | \ 'linE 2 AbcdefghijklmnopqrstuvwXyz', |
| 1670 | \ 'linE 3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 1671 | \ 'linE 4 AbcdefghijklmnopqrstuvwXyz', |
| 1672 | \ 'linE 5 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 1673 | \ 'linE 6 AbcdefghijklmnopqrstuvwXyz', |
| 1674 | \ 'linE 7 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 1675 | \ 'linE 8 AbcdefghijklmnopqrstuvwXyz', |
| 1676 | \ 'linE 9 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX', |
| 1677 | \ 'linE 10 AbcdefghijklmnopqrstuvwXyz' |
| 1678 | \ ], getline(1, 9)) |
| 1679 | call assert_equal([ |
| 1680 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 1681 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1682 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 1683 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1684 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 1685 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1686 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 1687 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1688 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 1689 | \ ], readfile('test.out')) |
| 1690 | |
| 1691 | augroup Test4 |
| 1692 | au! |
| 1693 | augroup END |
| 1694 | set shelltemp&vim |
| 1695 | |
| 1696 | " Test for the FileReadPre and FileReadPost autocmds. |
| 1697 | augroup Test5 |
| 1698 | au! |
| 1699 | au FileReadPre *.gz exe 'silent !gzip -d ' . shellescape(expand("<afile>")) |
| 1700 | au FileReadPre *.gz call rename(expand("<afile>:r"), expand("<afile>")) |
| 1701 | au FileReadPost *.gz '[,']s/l/L/ |
| 1702 | augroup END |
| 1703 | |
| 1704 | new |
| 1705 | 0r Xtestfile.gz " Read compressed file |
| 1706 | call assert_equal([ |
| 1707 | \ 'Line 2 Abcdefghijklmnopqrstuvwxyz', |
| 1708 | \ 'Line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1709 | \ 'Line 4 Abcdefghijklmnopqrstuvwxyz', |
| 1710 | \ 'Line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1711 | \ 'Line 6 Abcdefghijklmnopqrstuvwxyz', |
| 1712 | \ 'Line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1713 | \ 'Line 8 Abcdefghijklmnopqrstuvwxyz', |
| 1714 | \ 'Line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1715 | \ 'Line 10 Abcdefghijklmnopqrstuvwxyz' |
| 1716 | \ ], getline(1, 9)) |
| 1717 | call assert_equal([ |
| 1718 | \ 'line 2 Abcdefghijklmnopqrstuvwxyz', |
| 1719 | \ 'line 3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1720 | \ 'line 4 Abcdefghijklmnopqrstuvwxyz', |
| 1721 | \ 'line 5 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1722 | \ 'line 6 Abcdefghijklmnopqrstuvwxyz', |
| 1723 | \ 'line 7 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1724 | \ 'line 8 Abcdefghijklmnopqrstuvwxyz', |
| 1725 | \ 'line 9 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', |
| 1726 | \ 'line 10 Abcdefghijklmnopqrstuvwxyz' |
| 1727 | \ ], readfile('Xtestfile.gz')) |
| 1728 | |
| 1729 | augroup Test5 |
| 1730 | au! |
| 1731 | augroup END |
| 1732 | |
| 1733 | au! FileChangedShell |
| 1734 | call delete('Xtestfile.gz') |
| 1735 | call delete('Xtest.c') |
| 1736 | call delete('test.out') |
| 1737 | endfunc |
Bram Moolenaar | 23b5139 | 2019-05-09 21:38:43 +0200 | [diff] [blame] | 1738 | |
| 1739 | func Test_throw_in_BufWritePre() |
| 1740 | new |
| 1741 | call setline(1, ['one', 'two', 'three']) |
| 1742 | call assert_false(filereadable('Xthefile')) |
| 1743 | augroup throwing |
| 1744 | au BufWritePre X* throw 'do not write' |
| 1745 | augroup END |
| 1746 | try |
| 1747 | w Xthefile |
| 1748 | catch |
| 1749 | let caught = 1 |
| 1750 | endtry |
| 1751 | call assert_equal(1, caught) |
| 1752 | call assert_false(filereadable('Xthefile')) |
| 1753 | |
| 1754 | bwipe! |
| 1755 | au! throwing |
| 1756 | endfunc |