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