| Bram Moolenaar | 71136db | 2019-11-30 19:48:46 +0100 | [diff] [blame] | 1 | " Tests for register operations |
| Bram Moolenaar | 71136db | 2019-11-30 19:48:46 +0100 | [diff] [blame] | 2 | |
| Bram Moolenaar | 54c8d22 | 2019-12-02 20:41:39 +0100 | [diff] [blame] | 3 | source check.vim |
| Bram Moolenaar | 11a5b19 | 2020-07-10 21:17:51 +0200 | [diff] [blame] | 4 | source view_util.vim |
| Bram Moolenaar | 54c8d22 | 2019-12-02 20:41:39 +0100 | [diff] [blame] | 5 | |
| Bram Moolenaar | 71136db | 2019-11-30 19:48:46 +0100 | [diff] [blame] | 6 | " This test must be executed first to check for empty and unset registers. |
| 7 | func Test_aaa_empty_reg_test() |
| 8 | call assert_fails('normal @@', 'E748:') |
| 9 | call assert_fails('normal @%', 'E354:') |
| 10 | call assert_fails('normal @#', 'E354:') |
| 11 | call assert_fails('normal @!', 'E354:') |
| 12 | call assert_fails('normal @:', 'E30:') |
| 13 | call assert_fails('normal @.', 'E29:') |
| Bram Moolenaar | 6f1f0ca | 2019-12-01 18:16:18 +0100 | [diff] [blame] | 14 | call assert_fails('put /', 'E35:') |
| 15 | call assert_fails('put .', 'E29:') |
| Bram Moolenaar | 71136db | 2019-11-30 19:48:46 +0100 | [diff] [blame] | 16 | endfunc |
| Bram Moolenaar | e45deb7 | 2017-07-16 17:56:16 +0200 | [diff] [blame] | 17 | |
| 18 | func Test_yank_shows_register() |
| 19 | enew |
| 20 | set report=0 |
| 21 | call setline(1, ['foo', 'bar']) |
| 22 | " Line-wise |
| 23 | exe 'norm! yy' |
| 24 | call assert_equal('1 line yanked', v:statusmsg) |
| 25 | exe 'norm! "zyy' |
| 26 | call assert_equal('1 line yanked into "z', v:statusmsg) |
| 27 | exe 'norm! yj' |
| 28 | call assert_equal('2 lines yanked', v:statusmsg) |
| 29 | exe 'norm! "zyj' |
| 30 | call assert_equal('2 lines yanked into "z', v:statusmsg) |
| 31 | |
| 32 | " Block-wise |
| 33 | exe "norm! \<C-V>y" |
| 34 | call assert_equal('block of 1 line yanked', v:statusmsg) |
| 35 | exe "norm! \<C-V>\"zy" |
| 36 | call assert_equal('block of 1 line yanked into "z', v:statusmsg) |
| 37 | exe "norm! \<C-V>jy" |
| 38 | call assert_equal('block of 2 lines yanked', v:statusmsg) |
| 39 | exe "norm! \<C-V>j\"zy" |
| 40 | call assert_equal('block of 2 lines yanked into "z', v:statusmsg) |
| 41 | |
| 42 | bwipe! |
| 43 | endfunc |
| Bram Moolenaar | 7ce551f | 2018-05-06 17:32:19 +0200 | [diff] [blame] | 44 | |
| 45 | func Test_display_registers() |
| 46 | e file1 |
| 47 | e file2 |
| 48 | call setline(1, ['foo', 'bar']) |
| 49 | /bar |
| 50 | exe 'norm! y2l"axx' |
| 51 | call feedkeys("i\<C-R>=2*4\n\<esc>") |
| 52 | call feedkeys(":ls\n", 'xt') |
| 53 | |
| 54 | let a = execute('display') |
| 55 | let b = execute('registers') |
| 56 | |
| 57 | call assert_equal(a, b) |
| Bram Moolenaar | 3691f1e | 2019-10-24 20:17:00 +0200 | [diff] [blame] | 58 | call assert_match('^\nType Name Content\n' |
| 59 | \ . ' c "" a\n' |
| 60 | \ . ' c "0 ba\n' |
| 61 | \ . ' c "a b\n' |
| Bram Moolenaar | 7ce551f | 2018-05-06 17:32:19 +0200 | [diff] [blame] | 62 | \ . '.*' |
| Bram Moolenaar | 3691f1e | 2019-10-24 20:17:00 +0200 | [diff] [blame] | 63 | \ . ' c "- a\n' |
| Bram Moolenaar | 7ce551f | 2018-05-06 17:32:19 +0200 | [diff] [blame] | 64 | \ . '.*' |
| Bram Moolenaar | 3691f1e | 2019-10-24 20:17:00 +0200 | [diff] [blame] | 65 | \ . ' c ": ls\n' |
| 66 | \ . ' c "% file2\n' |
| 67 | \ . ' c "# file1\n' |
| 68 | \ . ' c "/ bar\n' |
| 69 | \ . ' c "= 2\*4', a) |
| Bram Moolenaar | 7ce551f | 2018-05-06 17:32:19 +0200 | [diff] [blame] | 70 | |
| 71 | let a = execute('registers a') |
| Bram Moolenaar | 3691f1e | 2019-10-24 20:17:00 +0200 | [diff] [blame] | 72 | call assert_match('^\nType Name Content\n' |
| 73 | \ . ' c "a b', a) |
| Bram Moolenaar | 7ce551f | 2018-05-06 17:32:19 +0200 | [diff] [blame] | 74 | |
| 75 | let a = execute('registers :') |
| Bram Moolenaar | 3691f1e | 2019-10-24 20:17:00 +0200 | [diff] [blame] | 76 | call assert_match('^\nType Name Content\n' |
| 77 | \ . ' c ": ls', a) |
| Bram Moolenaar | 7ce551f | 2018-05-06 17:32:19 +0200 | [diff] [blame] | 78 | |
| 79 | bwipe! |
| 80 | endfunc |
| Bram Moolenaar | 9d7fdd4 | 2019-03-08 09:50:52 +0100 | [diff] [blame] | 81 | |
| 82 | func Test_register_one() |
| 83 | " delete a line goes into register one |
| 84 | new |
| 85 | call setline(1, "one") |
| 86 | normal dd |
| 87 | call assert_equal("one\n", @1) |
| 88 | |
| 89 | " delete a word does not change register one, does change "- |
| 90 | call setline(1, "two") |
| 91 | normal de |
| 92 | call assert_equal("one\n", @1) |
| 93 | call assert_equal("two", @-) |
| 94 | |
| 95 | " delete a word with a register does not change register one |
| 96 | call setline(1, "three") |
| 97 | normal "ade |
| 98 | call assert_equal("three", @a) |
| 99 | call assert_equal("one\n", @1) |
| 100 | |
| 101 | " delete a word with register DOES change register one with one of a list of |
| 102 | " operators |
| 103 | " % |
| 104 | call setline(1, ["(12)3"]) |
| 105 | normal "ad% |
| 106 | call assert_equal("(12)", @a) |
| 107 | call assert_equal("(12)", @1) |
| 108 | |
| 109 | " ( |
| 110 | call setline(1, ["first second"]) |
| 111 | normal $"ad( |
| 112 | call assert_equal("first secon", @a) |
| 113 | call assert_equal("first secon", @1) |
| 114 | |
| 115 | " ) |
| 116 | call setline(1, ["First Second."]) |
| 117 | normal gg0"ad) |
| 118 | call assert_equal("First Second.", @a) |
| 119 | call assert_equal("First Second.", @1) |
| 120 | |
| 121 | " ` |
| 122 | call setline(1, ["start here."]) |
| 123 | normal gg0fhmx0"ad`x |
| 124 | call assert_equal("start ", @a) |
| 125 | call assert_equal("start ", @1) |
| 126 | |
| 127 | " / |
| 128 | call setline(1, ["searchX"]) |
| 129 | exe "normal gg0\"ad/X\<CR>" |
| 130 | call assert_equal("search", @a) |
| 131 | call assert_equal("search", @1) |
| 132 | |
| 133 | " ? |
| 134 | call setline(1, ["Ysearch"]) |
| 135 | exe "normal gg$\"ad?Y\<CR>" |
| 136 | call assert_equal("Ysearc", @a) |
| 137 | call assert_equal("Ysearc", @1) |
| 138 | |
| 139 | " n |
| 140 | call setline(1, ["Ynext"]) |
| 141 | normal gg$"adn |
| 142 | call assert_equal("Ynex", @a) |
| 143 | call assert_equal("Ynex", @1) |
| 144 | |
| 145 | " N |
| 146 | call setline(1, ["prevY"]) |
| 147 | normal gg0"adN |
| 148 | call assert_equal("prev", @a) |
| 149 | call assert_equal("prev", @1) |
| 150 | |
| 151 | " } |
| 152 | call setline(1, ["one", ""]) |
| 153 | normal gg0"ad} |
| 154 | call assert_equal("one\n", @a) |
| 155 | call assert_equal("one\n", @1) |
| 156 | |
| 157 | " { |
| 158 | call setline(1, ["", "two"]) |
| 159 | normal 2G$"ad{ |
| 160 | call assert_equal("\ntw", @a) |
| 161 | call assert_equal("\ntw", @1) |
| 162 | |
| 163 | bwipe! |
| 164 | endfunc |
| Bram Moolenaar | 6edbbd8 | 2019-03-10 09:41:51 +0100 | [diff] [blame] | 165 | |
| Bram Moolenaar | 11a5b19 | 2020-07-10 21:17:51 +0200 | [diff] [blame] | 166 | func Test_recording_status_in_ex_line() |
| 167 | norm qx |
| 168 | redraw! |
| 169 | call assert_equal('recording @x', Screenline(&lines)) |
| 170 | set shortmess=q |
| 171 | redraw! |
| 172 | call assert_equal('recording', Screenline(&lines)) |
| 173 | set shortmess& |
| 174 | norm q |
| 175 | redraw! |
| 176 | call assert_equal('', Screenline(&lines)) |
| 177 | endfunc |
| 178 | |
| Bram Moolenaar | 6edbbd8 | 2019-03-10 09:41:51 +0100 | [diff] [blame] | 179 | " Check that replaying a typed sequence does not use an Esc and following |
| 180 | " characters as an escape sequence. |
| 181 | func Test_recording_esc_sequence() |
| 182 | new |
| Bram Moolenaar | 55d81cd | 2019-03-11 08:05:50 +0100 | [diff] [blame] | 183 | try |
| 184 | let save_F2 = &t_F2 |
| 185 | catch |
| 186 | endtry |
| Bram Moolenaar | 6edbbd8 | 2019-03-10 09:41:51 +0100 | [diff] [blame] | 187 | let t_F2 = "\<Esc>OQ" |
| 188 | call feedkeys("qqiTest\<Esc>", "xt") |
| 189 | call feedkeys("OQuirk\<Esc>q", "xt") |
| 190 | call feedkeys("Go\<Esc>@q", "xt") |
| 191 | call assert_equal(['Quirk', 'Test', 'Quirk', 'Test'], getline(1, 4)) |
| 192 | bwipe! |
| Bram Moolenaar | 55d81cd | 2019-03-11 08:05:50 +0100 | [diff] [blame] | 193 | if exists('save_F2') |
| Bram Moolenaar | a5e4460 | 2019-05-25 21:52:30 +0200 | [diff] [blame] | 194 | let &t_F2 = save_F2 |
| Bram Moolenaar | 037c54f | 2019-04-20 23:47:46 +0200 | [diff] [blame] | 195 | else |
| 196 | set t_F2= |
| Bram Moolenaar | 55d81cd | 2019-03-11 08:05:50 +0100 | [diff] [blame] | 197 | endif |
| Bram Moolenaar | 6edbbd8 | 2019-03-10 09:41:51 +0100 | [diff] [blame] | 198 | endfunc |
| Bram Moolenaar | 71136db | 2019-11-30 19:48:46 +0100 | [diff] [blame] | 199 | |
| Bram Moolenaar | c88e977 | 2022-01-03 13:47:50 +0000 | [diff] [blame] | 200 | func Test_recording_with_select_mode() |
| 201 | new |
| 202 | call feedkeys("qacc12345\<Esc>gH98765\<Esc>q", "tx") |
| 203 | call assert_equal("98765", getline(1)) |
| 204 | call assert_equal("cc12345\<Esc>gH98765\<Esc>", @a) |
| 205 | call setline(1, 'asdf') |
| 206 | normal! @a |
| 207 | call assert_equal("98765", getline(1)) |
| 208 | bwipe! |
| 209 | endfunc |
| 210 | |
| Bram Moolenaar | 71136db | 2019-11-30 19:48:46 +0100 | [diff] [blame] | 211 | " Test for executing the last used register (@) |
| 212 | func Test_last_used_exec_reg() |
| 213 | " Test for the @: command |
| 214 | let a = '' |
| 215 | call feedkeys(":let a ..= 'Vim'\<CR>", 'xt') |
| 216 | normal @: |
| 217 | call assert_equal('VimVim', a) |
| 218 | |
| 219 | " Test for the @= command |
| 220 | let x = '' |
| 221 | let a = ":let x ..= 'Vim'\<CR>" |
| 222 | exe "normal @=a\<CR>" |
| 223 | normal @@ |
| 224 | call assert_equal('VimVim', x) |
| 225 | |
| 226 | " Test for the @. command |
| 227 | let a = '' |
| 228 | call feedkeys("i:let a ..= 'Edit'\<CR>", 'xt') |
| 229 | normal @. |
| 230 | normal @@ |
| 231 | call assert_equal('EditEdit', a) |
| 232 | |
| Bram Moolenaar | 6f1f0ca | 2019-12-01 18:16:18 +0100 | [diff] [blame] | 233 | " Test for repeating the last command-line in visual mode |
| 234 | call append(0, 'register') |
| 235 | normal gg |
| 236 | let @r = '' |
| 237 | call feedkeys("v:yank R\<CR>", 'xt') |
| 238 | call feedkeys("v@:", 'xt') |
| 239 | call assert_equal("\nregister\nregister\n", @r) |
| 240 | |
| Bram Moolenaar | 71136db | 2019-11-30 19:48:46 +0100 | [diff] [blame] | 241 | enew! |
| 242 | endfunc |
| 243 | |
| 244 | func Test_get_register() |
| 245 | enew |
| 246 | edit Xfile1 |
| 247 | edit Xfile2 |
| 248 | call assert_equal('Xfile2', getreg('%')) |
| 249 | call assert_equal('Xfile1', getreg('#')) |
| 250 | |
| 251 | call feedkeys("iTwo\<Esc>", 'xt') |
| 252 | call assert_equal('Two', getreg('.')) |
| 253 | call assert_equal('', getreg('_')) |
| 254 | call assert_beeps('normal ":yy') |
| 255 | call assert_beeps('normal "%yy') |
| 256 | call assert_beeps('normal ".yy') |
| 257 | |
| 258 | call assert_equal('', getreg("\<C-F>")) |
| 259 | call assert_equal('', getreg("\<C-W>")) |
| 260 | call assert_equal('', getreg("\<C-L>")) |
| Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 261 | " Change the last used register to '"' for the next test |
| 262 | normal! ""yy |
| 263 | let @" = 'happy' |
| 264 | call assert_equal('happy', getreg()) |
| 265 | call assert_equal('happy', getreg('')) |
| Bram Moolenaar | 71136db | 2019-11-30 19:48:46 +0100 | [diff] [blame] | 266 | |
| 267 | call assert_equal('', getregtype('!')) |
| Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 268 | call assert_fails('echo getregtype([])', 'E730:') |
| 269 | call assert_equal('v', getregtype()) |
| 270 | call assert_equal('v', getregtype('')) |
| Bram Moolenaar | 71136db | 2019-11-30 19:48:46 +0100 | [diff] [blame] | 271 | |
| Bram Moolenaar | 6f1f0ca | 2019-12-01 18:16:18 +0100 | [diff] [blame] | 272 | " Test for inserting an invalid register content |
| 273 | call assert_beeps('exe "normal i\<C-R>!"') |
| 274 | |
| 275 | " Test for inserting a register with multiple lines |
| 276 | call deletebufline('', 1, '$') |
| 277 | call setreg('r', ['a', 'b']) |
| 278 | exe "normal i\<C-R>r" |
| 279 | call assert_equal(['a', 'b', ''], getline(1, '$')) |
| 280 | |
| 281 | " Test for inserting a multi-line register in the command line |
| 282 | call feedkeys(":\<C-R>r\<Esc>", 'xt') |
| 283 | call assert_equal("a\rb\r", histget(':', -1)) |
| 284 | |
| Bram Moolenaar | 99fa721 | 2020-04-26 15:59:55 +0200 | [diff] [blame] | 285 | call assert_fails('let r = getreg("=", [])', 'E745:') |
| 286 | call assert_fails('let r = getreg("=", 1, [])', 'E745:') |
| Bram Moolenaar | 71136db | 2019-11-30 19:48:46 +0100 | [diff] [blame] | 287 | enew! |
| Bram Moolenaar | d1ad99b | 2020-10-04 16:16:54 +0200 | [diff] [blame] | 288 | |
| 289 | " Using a register in operator-pending mode should fail |
| 290 | call assert_beeps('norm! c"') |
| Bram Moolenaar | 71136db | 2019-11-30 19:48:46 +0100 | [diff] [blame] | 291 | endfunc |
| 292 | |
| 293 | func Test_set_register() |
| 294 | call assert_fails("call setreg('#', 200)", 'E86:') |
| Yegappan Lakshmanan | 34fcb69 | 2021-05-25 20:14:00 +0200 | [diff] [blame] | 295 | call assert_fails("call setreg('a', test_unknown())", 'E908:') |
| Bram Moolenaar | 71136db | 2019-11-30 19:48:46 +0100 | [diff] [blame] | 296 | |
| 297 | edit Xfile_alt_1 |
| 298 | let b1 = bufnr('') |
| 299 | edit Xfile_alt_2 |
| 300 | let b2 = bufnr('') |
| 301 | edit Xfile_alt_3 |
| 302 | let b3 = bufnr('') |
| 303 | call setreg('#', 'alt_1') |
| 304 | call assert_equal('Xfile_alt_1', getreg('#')) |
| 305 | call setreg('#', b2) |
| 306 | call assert_equal('Xfile_alt_2', getreg('#')) |
| 307 | |
| 308 | let ab = 'regwrite' |
| 309 | call setreg('=', '') |
| 310 | call setreg('=', 'a', 'a') |
| 311 | call setreg('=', 'b', 'a') |
| 312 | call assert_equal('regwrite', getreg('=')) |
| 313 | |
| Bram Moolenaar | 4b96df5 | 2020-01-26 22:00:26 +0100 | [diff] [blame] | 314 | " Test for setting a list of lines to special registers |
| Bram Moolenaar | 6f1f0ca | 2019-12-01 18:16:18 +0100 | [diff] [blame] | 315 | call setreg('/', []) |
| 316 | call assert_equal('', @/) |
| 317 | call setreg('=', []) |
| 318 | call assert_equal('', @=) |
| 319 | call assert_fails("call setreg('/', ['a', 'b'])", 'E883:') |
| 320 | call assert_fails("call setreg('=', ['a', 'b'])", 'E883:') |
| 321 | call assert_equal(0, setreg('_', ['a', 'b'])) |
| 322 | |
| 323 | " Test for recording to a invalid register |
| 324 | call assert_beeps('normal q$') |
| 325 | |
| 326 | " Appending to a register when recording |
| 327 | call append(0, "text for clipboard test") |
| 328 | normal gg |
| 329 | call feedkeys('qrllq', 'xt') |
| 330 | call feedkeys('qRhhq', 'xt') |
| 331 | call assert_equal('llhh', getreg('r')) |
| 332 | |
| Bram Moolenaar | 54c8d22 | 2019-12-02 20:41:39 +0100 | [diff] [blame] | 333 | " Appending a list of characters to a register from different lines |
| 334 | let @r = '' |
| 335 | call append(0, ['abcdef', '123456']) |
| 336 | normal gg"ry3l |
| 337 | call cursor(2, 4) |
| 338 | normal "Ry3l |
| 339 | call assert_equal('abc456', @r) |
| 340 | |
| 341 | " Test for gP with multiple lines selected using characterwise motion |
| 342 | %delete |
| 343 | call append(0, ['vim editor', 'vim editor']) |
| 344 | let @r = '' |
| 345 | exe "normal ggwy/vim /e\<CR>gP" |
| 346 | call assert_equal(['vim editor', 'vim editor', 'vim editor'], getline(1, 3)) |
| 347 | |
| 348 | " Test for gP with . register |
| 349 | %delete |
| 350 | normal iabc |
| 351 | normal ".gp |
| 352 | call assert_equal('abcabc', getline(1)) |
| 353 | normal 0".gP |
| 354 | call assert_equal('abcabcabc', getline(1)) |
| 355 | |
| Bram Moolenaar | 0e05de4 | 2020-03-25 22:23:46 +0100 | [diff] [blame] | 356 | let @"='' |
| 357 | call setreg('', '1') |
| 358 | call assert_equal('1', @") |
| 359 | call setreg('@', '2') |
| 360 | call assert_equal('2', @") |
| 361 | |
| Bram Moolenaar | 71136db | 2019-11-30 19:48:46 +0100 | [diff] [blame] | 362 | enew! |
| 363 | endfunc |
| 364 | |
| Bram Moolenaar | 54c8d22 | 2019-12-02 20:41:39 +0100 | [diff] [blame] | 365 | " Test for clipboard registers (* and +) |
| 366 | func Test_clipboard_regs() |
| 367 | CheckNotGui |
| 368 | CheckFeature clipboard_working |
| 369 | |
| 370 | new |
| 371 | call append(0, "text for clipboard test") |
| 372 | normal gg"*yiw |
| 373 | call assert_equal('text', getreg('*')) |
| 374 | normal gg2w"+yiw |
| 375 | call assert_equal('clipboard', getreg('+')) |
| 376 | |
| 377 | " Test for replacing the clipboard register contents |
| 378 | set clipboard=unnamed |
| 379 | let @* = 'food' |
| 380 | normal ggviw"*p |
| 381 | call assert_equal('text', getreg('*')) |
| 382 | call assert_equal('food for clipboard test', getline(1)) |
| 383 | normal ggviw"*p |
| 384 | call assert_equal('food', getreg('*')) |
| 385 | call assert_equal('text for clipboard test', getline(1)) |
| 386 | |
| 387 | " Test for replacing the selection register contents |
| 388 | set clipboard=unnamedplus |
| 389 | let @+ = 'food' |
| 390 | normal ggviw"+p |
| 391 | call assert_equal('text', getreg('+')) |
| 392 | call assert_equal('food for clipboard test', getline(1)) |
| 393 | normal ggviw"+p |
| 394 | call assert_equal('food', getreg('+')) |
| 395 | call assert_equal('text for clipboard test', getline(1)) |
| 396 | |
| 397 | " Test for auto copying visually selected text to clipboard register |
| 398 | call setline(1, "text for clipboard test") |
| 399 | let @* = '' |
| 400 | set clipboard=autoselect |
| 401 | normal ggwwviwy |
| 402 | call assert_equal('clipboard', @*) |
| 403 | |
| 404 | " Test for auto copying visually selected text to selection register |
| 405 | let @+ = '' |
| 406 | set clipboard=autoselectplus |
| 407 | normal ggwviwy |
| 408 | call assert_equal('for', @+) |
| 409 | |
| 410 | set clipboard&vim |
| 411 | bwipe! |
| 412 | endfunc |
| 413 | |
| Ernie Rael | 559f230 | 2022-07-26 14:44:36 +0100 | [diff] [blame] | 414 | " Test unnamed for both clipboard registers (* and +) |
| 415 | func Test_clipboard_regs_both_unnamed() |
| 416 | CheckNotGui |
| 417 | CheckFeature clipboard_working |
| 418 | CheckTwoClipboards |
| 419 | |
| 420 | let @* = 'xxx' |
| 421 | let @+ = 'xxx' |
| 422 | |
| 423 | new |
| 424 | |
| 425 | set clipboard=unnamed,unnamedplus |
| 426 | call setline(1, ['foo', 'bar']) |
| 427 | |
| 428 | " op_yank copies to both |
| 429 | :1 |
| 430 | :normal yw |
| 431 | call assert_equal('foo', getreg('*')) |
| 432 | call assert_equal('foo', getreg('+')) |
| 433 | |
| 434 | " op_delete only copies to '+' |
| 435 | :2 |
| 436 | :normal dw |
| 437 | call assert_equal('foo', getreg('*')) |
| 438 | call assert_equal('bar', getreg('+')) |
| 439 | |
| 440 | set clipboard&vim |
| 441 | bwipe! |
| 442 | endfunc |
| 443 | |
| Bram Moolenaar | 54c8d22 | 2019-12-02 20:41:39 +0100 | [diff] [blame] | 444 | " Test for restarting the current mode (insert or virtual replace) after |
| 445 | " executing the contents of a register |
| 446 | func Test_put_reg_restart_mode() |
| 447 | new |
| 448 | call append(0, 'editor') |
| 449 | normal gg |
| 450 | let @r = "ivim \<Esc>" |
| 451 | call feedkeys("i\<C-O>@r\<C-R>=mode()\<CR>", 'xt') |
| 452 | call assert_equal('vimi editor', getline(1)) |
| 453 | |
| 454 | call setline(1, 'editor') |
| 455 | normal gg |
| 456 | call feedkeys("gR\<C-O>@r\<C-R>=mode()\<CR>", 'xt') |
| 457 | call assert_equal('vimReditor', getline(1)) |
| 458 | |
| 459 | bwipe! |
| 460 | endfunc |
| 461 | |
| Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 462 | " Test for executing a register using :@ command |
| 463 | func Test_execute_register() |
| 464 | call setreg('r', []) |
| 465 | call assert_beeps('@r') |
| 466 | let i = 1 |
| 467 | let @q = 'let i+= 1' |
| 468 | @q |
| 469 | @ |
| 470 | call assert_equal(3, i) |
| Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 471 | |
| Bram Moolenaar | 8a9bc95 | 2020-10-02 18:48:07 +0200 | [diff] [blame] | 472 | " try to execute expression register and use a backspace to cancel it |
| 473 | new |
| 474 | call feedkeys("@=\<BS>ax\<CR>y", 'xt') |
| 475 | call assert_equal(['x', 'y'], getline(1, '$')) |
| 476 | close! |
| 477 | |
| Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 478 | " cannot execute a register in operator pending mode |
| 479 | call assert_beeps('normal! c@r') |
| Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 480 | endfunc |
| 481 | |
| Bram Moolenaar | bb861e2 | 2020-06-07 18:16:36 +0200 | [diff] [blame] | 482 | " Test for getting register info |
| 483 | func Test_get_reginfo() |
| 484 | enew |
| 485 | call setline(1, ['foo', 'bar']) |
| 486 | |
| 487 | exe 'norm! "zyy' |
| 488 | let info = getreginfo('"') |
| 489 | call assert_equal('z', info.points_to) |
| 490 | call setreg('y', 'baz') |
| 491 | call assert_equal('z', getreginfo('').points_to) |
| 492 | call setreg('y', { 'isunnamed': v:true }) |
| 493 | call assert_equal('y', getreginfo('"').points_to) |
| 494 | |
| 495 | exe '$put' |
| 496 | call assert_equal(getreg('y'), getline(3)) |
| 497 | call setreg('', 'qux') |
| 498 | call assert_equal('0', getreginfo('').points_to) |
| 499 | call setreg('x', 'quux') |
| 500 | call assert_equal('0', getreginfo('').points_to) |
| 501 | |
| 502 | let info = getreginfo('') |
| 503 | call assert_equal(getreg('', 1, 1), info.regcontents) |
| 504 | call assert_equal(getregtype(''), info.regtype) |
| 505 | |
| 506 | exe "norm! 0\<c-v>e" .. '"zy' |
| 507 | let info = getreginfo('z') |
| 508 | call assert_equal(getreg('z', 1, 1), info.regcontents) |
| 509 | call assert_equal(getregtype('z'), info.regtype) |
| 510 | call assert_equal(1, +info.isunnamed) |
| 511 | |
| 512 | let info = getreginfo('"') |
| 513 | call assert_equal('z', info.points_to) |
| 514 | |
| Yegappan Lakshmanan | 34fcb69 | 2021-05-25 20:14:00 +0200 | [diff] [blame] | 515 | let @a="a1b2" |
| 516 | nnoremap <F2> <Cmd>let g:RegInfo = getreginfo()<CR> |
| 517 | exe "normal \"a\<F2>" |
| 518 | call assert_equal({'regcontents': ['a1b2'], 'isunnamed': v:false, |
| 519 | \ 'regtype': 'v'}, g:RegInfo) |
| 520 | nunmap <F2> |
| 521 | unlet g:RegInfo |
| 522 | |
| Bram Moolenaar | bb861e2 | 2020-06-07 18:16:36 +0200 | [diff] [blame] | 523 | bwipe! |
| 524 | endfunc |
| 525 | |
| 526 | " Test for restoring register with dict from getreginfo |
| 527 | func Test_set_register_dict() |
| 528 | enew! |
| 529 | |
| 530 | call setreg('"', #{ regcontents: ['one', 'two'], |
| 531 | \ regtype: 'V', points_to: 'z' }) |
| 532 | call assert_equal(['one', 'two'], getreg('"', 1, 1)) |
| 533 | let info = getreginfo('"') |
| 534 | call assert_equal('z', info.points_to) |
| 535 | call assert_equal('V', info.regtype) |
| 536 | call assert_equal(1, +getreginfo('z').isunnamed) |
| 537 | |
| 538 | call setreg('x', #{ regcontents: ['three', 'four'], |
| 539 | \ regtype: 'v', isunnamed: v:true }) |
| 540 | call assert_equal(['three', 'four'], getreg('"', 1, 1)) |
| 541 | let info = getreginfo('"') |
| 542 | call assert_equal('x', info.points_to) |
| 543 | call assert_equal('v', info.regtype) |
| 544 | call assert_equal(1, +getreginfo('x').isunnamed) |
| 545 | |
| 546 | call setreg('y', #{ regcontents: 'five', |
| 547 | \ regtype: "\<c-v>", isunnamed: v:false }) |
| 548 | call assert_equal("\<c-v>4", getreginfo('y').regtype) |
| 549 | call assert_equal(0, +getreginfo('y').isunnamed) |
| 550 | call assert_equal(['three', 'four'], getreg('"', 1, 1)) |
| 551 | call assert_equal('x', getreginfo('"').points_to) |
| 552 | |
| 553 | call setreg('"', #{ regcontents: 'six' }) |
| 554 | call assert_equal('0', getreginfo('"').points_to) |
| 555 | call assert_equal(1, +getreginfo('0').isunnamed) |
| 556 | call assert_equal(['six'], getreginfo('0').regcontents) |
| 557 | call assert_equal(['six'], getreginfo('"').regcontents) |
| 558 | |
| Bram Moolenaar | 7633fe5 | 2020-06-22 19:10:56 +0200 | [diff] [blame] | 559 | let @x = 'one' |
| 560 | call setreg('x', {}) |
| 561 | call assert_equal(1, len(split(execute('reg x'), '\n'))) |
| 562 | |
| 563 | call assert_fails("call setreg('0', #{regtype: 'V'}, 'v')", 'E118:') |
| 564 | call assert_fails("call setreg('0', #{regtype: 'X'})", 'E475:') |
| 565 | call assert_fails("call setreg('0', #{regtype: 'vy'})", 'E475:') |
| 566 | |
| Bram Moolenaar | bb861e2 | 2020-06-07 18:16:36 +0200 | [diff] [blame] | 567 | bwipe! |
| 568 | endfunc |
| 569 | |
| Bram Moolenaar | cc61303 | 2020-06-07 21:31:18 +0200 | [diff] [blame] | 570 | func Test_v_register() |
| 571 | enew |
| 572 | call setline(1, 'nothing') |
| 573 | |
| 574 | func s:Put() |
| 575 | let s:register = v:register |
| 576 | exec 'normal! "' .. v:register .. 'P' |
| 577 | endfunc |
| 578 | nnoremap <buffer> <plug>(test) :<c-u>call s:Put()<cr> |
| 579 | nmap <buffer> S <plug>(test) |
| 580 | |
| 581 | let @z = "testz\n" |
| 582 | let @" = "test@\n" |
| 583 | |
| 584 | let s:register = '' |
| 585 | call feedkeys('"_ddS', 'mx') |
| 586 | call assert_equal('test@', getline('.')) " fails before 8.2.0929 |
| 587 | call assert_equal('"', s:register) " fails before 8.2.0929 |
| 588 | |
| 589 | let s:register = '' |
| 590 | call feedkeys('"zS', 'mx') |
| 591 | call assert_equal('z', s:register) |
| 592 | |
| 593 | let s:register = '' |
| 594 | call feedkeys('"zSS', 'mx') |
| 595 | call assert_equal('"', s:register) |
| 596 | |
| 597 | let s:register = '' |
| 598 | call feedkeys('"_S', 'mx') |
| 599 | call assert_equal('_', s:register) |
| 600 | |
| 601 | let s:register = '' |
| 602 | normal "_ddS |
| 603 | call assert_equal('"', s:register) " fails before 8.2.0929 |
| 604 | call assert_equal('test@', getline('.')) " fails before 8.2.0929 |
| 605 | |
| 606 | let s:register = '' |
| 607 | execute 'normal "z:call' "s:Put()\n" |
| 608 | call assert_equal('z', s:register) |
| 609 | call assert_equal('testz', getline('.')) |
| 610 | |
| 611 | " Test operator and omap |
| 612 | let @b = 'testb' |
| 613 | func s:OpFunc(...) |
| 614 | let s:register2 = v:register |
| 615 | endfunc |
| 616 | set opfunc=s:OpFunc |
| 617 | |
| 618 | normal "bg@l |
| 619 | normal S |
| 620 | call assert_equal('"', s:register) " fails before 8.2.0929 |
| 621 | call assert_equal('b', s:register2) |
| 622 | |
| 623 | func s:Motion() |
| 624 | let s:register1 = v:register |
| 625 | normal! l |
| 626 | endfunc |
| 627 | onoremap <buffer> Q :<c-u>call s:Motion()<cr> |
| 628 | |
| 629 | normal "bg@Q |
| 630 | normal S |
| 631 | call assert_equal('"', s:register) |
| 632 | call assert_equal('b', s:register1) |
| 633 | call assert_equal('"', s:register2) |
| 634 | |
| 635 | set opfunc& |
| 636 | bwipe! |
| 637 | endfunc |
| 638 | |
| Bram Moolenaar | 856c111 | 2020-06-17 21:47:23 +0200 | [diff] [blame] | 639 | " Test for executing the contents of a register as an Ex command with line |
| 640 | " continuation. |
| 641 | func Test_execute_reg_as_ex_cmd() |
| 642 | " Line continuation with just two lines |
| 643 | let code =<< trim END |
| 644 | let l = [ |
| 645 | \ 1] |
| 646 | END |
| 647 | let @r = code->join("\n") |
| 648 | let l = [] |
| 649 | @r |
| 650 | call assert_equal([1], l) |
| 651 | |
| 652 | " Line continuation with more than two lines |
| 653 | let code =<< trim END |
| 654 | let l = [ |
| 655 | \ 1, |
| 656 | \ 2, |
| 657 | \ 3] |
| 658 | END |
| 659 | let @r = code->join("\n") |
| 660 | let l = [] |
| 661 | @r |
| 662 | call assert_equal([1, 2, 3], l) |
| 663 | |
| 664 | " use comments interspersed with code |
| 665 | let code =<< trim END |
| 666 | let l = [ |
| 667 | "\ one |
| 668 | \ 1, |
| 669 | "\ two |
| 670 | \ 2, |
| 671 | "\ three |
| 672 | \ 3] |
| 673 | END |
| 674 | let @r = code->join("\n") |
| 675 | let l = [] |
| 676 | @r |
| 677 | call assert_equal([1, 2, 3], l) |
| 678 | |
| 679 | " use line continuation in the middle |
| 680 | let code =<< trim END |
| 681 | let a = "one" |
| 682 | let l = [ |
| 683 | \ 1, |
| 684 | \ 2] |
| 685 | let b = "two" |
| 686 | END |
| 687 | let @r = code->join("\n") |
| 688 | let l = [] |
| 689 | @r |
| 690 | call assert_equal([1, 2], l) |
| 691 | call assert_equal("one", a) |
| 692 | call assert_equal("two", b) |
| 693 | |
| 694 | " only one line with a \ |
| 695 | let @r = "\\let l = 1" |
| 696 | call assert_fails('@r', 'E10:') |
| 697 | |
| 698 | " only one line with a "\ |
| 699 | let @r = ' "\ let i = 1' |
| 700 | @r |
| 701 | call assert_false(exists('i')) |
| 702 | |
| 703 | " first line also begins with a \ |
| 704 | let @r = "\\let l = [\n\\ 1]" |
| 705 | call assert_fails('@r', 'E10:') |
| 706 | |
| 707 | " Test with a large number of lines |
| 708 | let @r = "let str = \n" |
| 709 | let @r ..= repeat(" \\ 'abcdefghijklmnopqrstuvwxyz' ..\n", 312) |
| 710 | let @r ..= ' \ ""' |
| 711 | @r |
| 712 | call assert_equal(repeat('abcdefghijklmnopqrstuvwxyz', 312), str) |
| 713 | endfunc |
| 714 | |
| Bram Moolenaar | 25fd267 | 2020-06-22 20:30:27 +0200 | [diff] [blame] | 715 | " Test for clipboard registers with ASCII NUL |
| 716 | func Test_clipboard_nul() |
| 717 | CheckFeature clipboard_working |
| 718 | new |
| 719 | |
| 720 | " Test for putting ASCII NUL into the clipboard |
| 721 | set clipboard=unnamed |
| 722 | call append(0, "\ntest") |
| 723 | normal ggyyp |
| 724 | call assert_equal("^@test^@", strtrans(getreg('*'))) |
| 725 | call assert_equal(getline(1), getline(2)) |
| 726 | let b = split(execute(":reg *"), "\n") |
| 727 | call assert_match('"\*\s*\^@test\^J',b[1]) |
| 728 | |
| 729 | set clipboard&vim |
| 730 | bwipe! |
| 731 | endfunc |
| 732 | |
| Bram Moolenaar | ef85a9b | 2020-07-10 20:24:07 +0200 | [diff] [blame] | 733 | func Test_ve_blockpaste() |
| 734 | new |
| 735 | set ve=all |
| 736 | 0put =['QWERTZ','ASDFGH'] |
| 737 | call cursor(1,1) |
| 738 | exe ":norm! \<C-V>3ljdP" |
| 739 | call assert_equal(1, col('.')) |
| 740 | call assert_equal(getline(1, 2), ['QWERTZ', 'ASDFGH']) |
| 741 | call cursor(1,1) |
| 742 | exe ":norm! \<C-V>3ljd" |
| 743 | call cursor(1,1) |
| 744 | norm! $3lP |
| 745 | call assert_equal(5, col('.')) |
| 746 | call assert_equal(getline(1, 2), ['TZ QWER', 'GH ASDF']) |
| 747 | set ve&vim |
| 748 | bwipe! |
| 749 | endfunc |
| 750 | |
| Bram Moolenaar | 032a2d0 | 2020-12-22 17:59:35 +0100 | [diff] [blame] | 751 | func Test_insert_small_delete() |
| 752 | new |
| 753 | call setline(1, ['foo foobar bar']) |
| 754 | call cursor(1,1) |
| 755 | exe ":norm! ciw'\<C-R>-'" |
| Bram Moolenaar | 9f63a65 | 2020-12-23 12:50:20 +0100 | [diff] [blame] | 756 | call assert_equal("'foo' foobar bar", getline(1)) |
| Bram Moolenaar | 032a2d0 | 2020-12-22 17:59:35 +0100 | [diff] [blame] | 757 | exe ":norm! w.w." |
| Bram Moolenaar | 9f63a65 | 2020-12-23 12:50:20 +0100 | [diff] [blame] | 758 | call assert_equal("'foo' 'foobar' 'bar'", getline(1)) |
| Bram Moolenaar | 032a2d0 | 2020-12-22 17:59:35 +0100 | [diff] [blame] | 759 | bwipe! |
| 760 | endfunc |
| 761 | |
| Bram Moolenaar | f4fcedc | 2021-03-15 18:36:20 +0100 | [diff] [blame] | 762 | " Record in insert mode using CTRL-O |
| 763 | func Test_record_in_insert_mode() |
| 764 | new |
| 765 | let @r = '' |
| 766 | call setline(1, ['foo']) |
| 767 | call feedkeys("i\<C-O>qrbaz\<C-O>q", 'xt') |
| 768 | call assert_equal('baz', @r) |
| 769 | bwipe! |
| 770 | endfunc |
| 771 | |
| Bram Moolenaar | a4bc2dd | 2022-01-27 19:27:16 +0000 | [diff] [blame] | 772 | func Test_record_in_select_mode() |
| 773 | new |
| 774 | call setline(1, 'text') |
| 775 | sil norm q00 |
| 776 | sil norm q |
| 777 | call assert_equal('0ext', getline(1)) |
| zeertzjq | fbf4f1c | 2022-01-28 12:50:43 +0000 | [diff] [blame] | 778 | |
| 779 | %delete |
| 780 | let @r = '' |
| 781 | call setline(1, ['abc', 'abc', 'abc']) |
| 782 | smap <F2> <Right><Right>, |
| 783 | call feedkeys("qrgh\<F2>Dk\<Esc>q", 'xt') |
| 784 | call assert_equal("gh\<F2>Dk\<Esc>", @r) |
| 785 | norm j0@rj0@@ |
| 786 | call assert_equal([',Dk', ',Dk', ',Dk'], getline(1, 3)) |
| 787 | sunmap <F2> |
| 788 | |
| Bram Moolenaar | a4bc2dd | 2022-01-27 19:27:16 +0000 | [diff] [blame] | 789 | bwipe! |
| 790 | endfunc |
| 791 | |
| zeertzjq | 81b46a6 | 2022-04-09 17:58:49 +0100 | [diff] [blame] | 792 | " mapping that ends macro recording should be removed from recorded macro |
| 793 | func Test_end_record_using_mapping() |
| 794 | call setline(1, 'aaa') |
| 795 | nnoremap s q |
| 796 | call feedkeys('safas', 'tx') |
| 797 | call assert_equal('fa', @a) |
| 798 | nunmap s |
| 799 | |
| 800 | nnoremap xx q |
| 801 | call feedkeys('0xxafaxx', 'tx') |
| 802 | call assert_equal('fa', @a) |
| 803 | nunmap xx |
| 804 | |
| 805 | nnoremap xsx q |
| 806 | call feedkeys('0qafaxsx', 'tx') |
| 807 | call assert_equal('fa', @a) |
| 808 | nunmap xsx |
| 809 | |
| 810 | bwipe! |
| 811 | endfunc |
| 812 | |
| zeertzjq | 6d4e725 | 2022-04-07 13:58:04 +0100 | [diff] [blame] | 813 | func Test_end_reg_executing() |
| 814 | nnoremap s <Nop> |
| 815 | let @a = 's' |
| 816 | call feedkeys("@aqaq\<Esc>", 'tx') |
| 817 | call assert_equal('', @a) |
| 818 | call assert_equal('', getline(1)) |
| 819 | |
| 820 | call setline(1, 'aaa') |
| 821 | nnoremap s qa |
| 822 | let @a = 'fa' |
| 823 | call feedkeys("@asq\<Esc>", 'tx') |
| 824 | call assert_equal('', @a) |
| 825 | call assert_equal('aaa', getline(1)) |
| 826 | |
| 827 | nunmap s |
| 828 | bwipe! |
| 829 | endfunc |
| 830 | |
| Christian Brabandt | 78eb9cc | 2021-09-14 18:55:51 +0200 | [diff] [blame] | 831 | " Make sure that y_append is correctly reset |
| 832 | " and the previous register is working as expected |
| 833 | func Test_register_y_append_reset() |
| 834 | new |
| 835 | call setline(1, ['1', |
| 836 | \ '2 ----------------------------------------------------', |
| 837 | \ '3', |
| 838 | \ '4', |
| 839 | \ '5 ----------------------------------------------------', |
| 840 | \ '6', |
| 841 | \ '7', |
| 842 | \ '8 ----------------------------------------------------', |
| 843 | \ '9', |
| 844 | \ '10 aaaaaaa 4.', |
| 845 | \ '11 Game Dbl-Figures Leaders:', |
| 846 | \ '12 Player Pts FG% 3P% FT% RB AS BL ST TO PF EFF', |
| 847 | \ '13 bbbbbbbbb 12 (50 /0 /67 )/ 7/ 3/ 0/ 2/ 3/ 4/+15', |
| 848 | \ '14 cccccc 12 (57 /67 /100)/ 2/ 1/ 1/ 0/ 1/ 3/+12', |
| 849 | \ '15 ddddddd 10 (63 /0 /0 )/ 1/ 3/ 0/ 3/ 5/ 3/ +9', |
| 850 | \ '16 4 5-15 0-3 2-2 5-12 1-1 3-4 33.3 0.0 100 41.7 100 75 12 14', |
| 851 | \ '17 F 23-55 2-10 9-11 23-52 3-13 26-29 41.8 20 81.8 44.2 23.1 89.7 57 75', |
| 852 | \ '18 4 3 6 3 2 3 3 4 3 3 7 3 1 4 6 -1 -1 +2 -1 -2', |
| 853 | \ '19 F 13 19 5 10 4 17 22 9 14 32 13 4 20 17 -1 -13 -4 -3 -3 +5']) |
| 854 | 11 |
| 855 | exe "norm! \"a5dd" |
| 856 | norm! j |
| 857 | exe "norm! \"bY" |
| 858 | norm! 2j |
| 859 | exe "norm! \"BY" |
| 860 | norm! 4k |
| 861 | norm! 5dd |
| 862 | norm! 3k |
| 863 | " The next put should put the content of the unnamed register, not of |
| 864 | " register b! |
| 865 | norm! p |
| 866 | call assert_equal(['1', |
| 867 | \ '2 ----------------------------------------------------', |
| 868 | \ '3', |
| 869 | \ '4', |
| 870 | \ '5 ----------------------------------------------------', |
| 871 | \ '6', |
| 872 | \ '10 aaaaaaa 4.', |
| 873 | \ '16 4 5-15 0-3 2-2 5-12 1-1 3-4 33.3 0.0 100 41.7 100 75 12 14', |
| 874 | \ '17 F 23-55 2-10 9-11 23-52 3-13 26-29 41.8 20 81.8 44.2 23.1 89.7 57 75', |
| 875 | \ '18 4 3 6 3 2 3 3 4 3 3 7 3 1 4 6 -1 -1 +2 -1 -2', |
| 876 | \ '19 F 13 19 5 10 4 17 22 9 14 32 13 4 20 17 -1 -13 -4 -3 -3 +5', |
| 877 | \ '7', |
| 878 | \ '8 ----------------------------------------------------', |
| 879 | \ '9'], getline(1,'$')) |
| 880 | bwipe! |
| 881 | endfunc |
| 882 | |
| Bram Moolenaar | 71136db | 2019-11-30 19:48:46 +0100 | [diff] [blame] | 883 | " vim: shiftwidth=2 sts=2 expandtab |