1" Test for the search command 2 3source shared.vim 4source screendump.vim 5source check.vim 6 7func Test_search_cmdline() 8 CheckOption incsearch 9 10 " need to disable char_avail, 11 " so that expansion of commandline works 12 call test_override("char_avail", 1) 13 new 14 call setline(1, [' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar']) 15 " Test 1 16 " CTRL-N / CTRL-P skips through the previous search history 17 set noincsearch 18 :1 19 call feedkeys("/foobar\<cr>", 'tx') 20 call feedkeys("/the\<cr>", 'tx') 21 call assert_equal('the', @/) 22 call feedkeys("/thes\<C-P>\<C-P>\<cr>", 'tx') 23 call assert_equal('foobar', @/) 24 25 " Test 2 26 " Ctrl-G goes from one match to the next 27 " until the end of the buffer 28 set incsearch nowrapscan 29 :1 30 " first match 31 call feedkeys("/the\<cr>", 'tx') 32 call assert_equal(' 2 these', getline('.')) 33 :1 34 " second match 35 call feedkeys("/the\<C-G>\<cr>", 'tx') 36 call assert_equal(' 3 the', getline('.')) 37 call assert_equal([0, 0, 0, 0], getpos('"')) 38 :1 39 " third match 40 call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx') 41 call assert_equal(' 4 their', getline('.')) 42 :1 43 " fourth match 44 call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx') 45 call assert_equal(' 5 there', getline('.')) 46 :1 47 " fifth match 48 call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx') 49 call assert_equal(' 6 their', getline('.')) 50 :1 51 " sixth match 52 call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx') 53 call assert_equal(' 7 the', getline('.')) 54 :1 55 " seventh match 56 call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx') 57 call assert_equal(' 8 them', getline('.')) 58 :1 59 " eighth match 60 call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx') 61 call assert_equal(' 9 these', getline('.')) 62 :1 63 " no further match 64 call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx') 65 call assert_equal(' 9 these', getline('.')) 66 call assert_equal([0, 0, 0, 0], getpos('"')) 67 68 " Test 3 69 " Ctrl-G goes from one match to the next 70 " and continues back at the top 71 set incsearch wrapscan 72 :1 73 " first match 74 call feedkeys("/the\<cr>", 'tx') 75 call assert_equal(' 2 these', getline('.')) 76 :1 77 " second match 78 call feedkeys("/the\<C-G>\<cr>", 'tx') 79 call assert_equal(' 3 the', getline('.')) 80 :1 81 " third match 82 call feedkeys("/the".repeat("\<C-G>", 2)."\<cr>", 'tx') 83 call assert_equal(' 4 their', getline('.')) 84 :1 85 " fourth match 86 call feedkeys("/the".repeat("\<C-G>", 3)."\<cr>", 'tx') 87 call assert_equal(' 5 there', getline('.')) 88 :1 89 " fifth match 90 call feedkeys("/the".repeat("\<C-G>", 4)."\<cr>", 'tx') 91 call assert_equal(' 6 their', getline('.')) 92 :1 93 " sixth match 94 call feedkeys("/the".repeat("\<C-G>", 5)."\<cr>", 'tx') 95 call assert_equal(' 7 the', getline('.')) 96 :1 97 " seventh match 98 call feedkeys("/the".repeat("\<C-G>", 6)."\<cr>", 'tx') 99 call assert_equal(' 8 them', getline('.')) 100 :1 101 " eighth match 102 call feedkeys("/the".repeat("\<C-G>", 7)."\<cr>", 'tx') 103 call assert_equal(' 9 these', getline('.')) 104 :1 105 " back at first match 106 call feedkeys("/the".repeat("\<C-G>", 8)."\<cr>", 'tx') 107 call assert_equal(' 2 these', getline('.')) 108 109 " Test 4 110 " CTRL-T goes to the previous match 111 set incsearch nowrapscan 112 $ 113 " first match 114 call feedkeys("?the\<cr>", 'tx') 115 call assert_equal(' 9 these', getline('.')) 116 $ 117 " first match 118 call feedkeys("?the\<C-G>\<cr>", 'tx') 119 call assert_equal(' 9 these', getline('.')) 120 $ 121 " second match 122 call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx') 123 call assert_equal(' 8 them', getline('.')) 124 $ 125 " last match 126 call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx') 127 call assert_equal(' 2 these', getline('.')) 128 $ 129 " last match 130 call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx') 131 call assert_equal(' 2 these', getline('.')) 132 133 " Test 5 134 " CTRL-T goes to the previous match 135 set incsearch wrapscan 136 $ 137 " first match 138 call feedkeys("?the\<cr>", 'tx') 139 call assert_equal(' 9 these', getline('.')) 140 $ 141 " first match at the top 142 call feedkeys("?the\<C-G>\<cr>", 'tx') 143 call assert_equal(' 2 these', getline('.')) 144 $ 145 " second match 146 call feedkeys("?the".repeat("\<C-T>", 1)."\<cr>", 'tx') 147 call assert_equal(' 8 them', getline('.')) 148 $ 149 " last match 150 call feedkeys("?the".repeat("\<C-T>", 7)."\<cr>", 'tx') 151 call assert_equal(' 2 these', getline('.')) 152 $ 153 " back at the bottom of the buffer 154 call feedkeys("?the".repeat("\<C-T>", 8)."\<cr>", 'tx') 155 call assert_equal(' 9 these', getline('.')) 156 157 " Test 6 158 " CTRL-L adds to the search pattern 159 set incsearch wrapscan 160 1 161 " first match 162 call feedkeys("/the\<c-l>\<cr>", 'tx') 163 call assert_equal(' 2 these', getline('.')) 164 1 165 " go to next match of 'thes' 166 call feedkeys("/the\<c-l>\<C-G>\<cr>", 'tx') 167 call assert_equal(' 9 these', getline('.')) 168 1 169 " wrap around 170 call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx') 171 call assert_equal(' 2 these', getline('.')) 172 1 173 " wrap around 174 set nowrapscan 175 call feedkeys("/the\<c-l>\<C-G>\<C-G>\<cr>", 'tx') 176 call assert_equal(' 9 these', getline('.')) 177 178 " Test 7 179 " <bs> remove from match, but stay at current match 180 set incsearch wrapscan 181 1 182 " first match 183 call feedkeys("/thei\<cr>", 'tx') 184 call assert_equal(' 4 their', getline('.')) 185 1 186 " delete one char, add another 187 call feedkeys("/thei\<bs>s\<cr>", 'tx') 188 call assert_equal(' 2 these', getline('.')) 189 1 190 " delete one char, add another, go to previous match, add one char 191 call feedkeys("/thei\<bs>s\<bs>\<C-T>\<c-l>\<cr>", 'tx') 192 call assert_equal(' 9 these', getline('.')) 193 1 194 " delete all chars, start from the beginning again 195 call feedkeys("/them". repeat("\<bs>",4).'the\>'."\<cr>", 'tx') 196 call assert_equal(' 3 the', getline('.')) 197 198 " clean up 199 call test_override("char_avail", 0) 200 bw! 201endfunc 202 203func Test_search_cmdline2() 204 CheckOption incsearch 205 206 " need to disable char_avail, 207 " so that expansion of commandline works 208 call test_override("char_avail", 1) 209 new 210 call setline(1, [' 1', ' 2 these', ' 3 the theother']) 211 " Test 1 212 " Ctrl-T goes correctly back and forth 213 set incsearch 214 1 215 " first match 216 call feedkeys("/the\<cr>", 'tx') 217 call assert_equal(' 2 these', getline('.')) 218 1 219 " go to next match (on next line) 220 call feedkeys("/the\<C-G>\<cr>", 'tx') 221 call assert_equal(' 3 the theother', getline('.')) 222 1 223 " go to next match (still on line 3) 224 call feedkeys("/the\<C-G>\<C-G>\<cr>", 'tx') 225 call assert_equal(' 3 the theother', getline('.')) 226 1 227 " go to next match (still on line 3) 228 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<cr>", 'tx') 229 call assert_equal(' 3 the theother', getline('.')) 230 1 231 " go to previous match (on line 3) 232 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<cr>", 'tx') 233 call assert_equal(' 3 the theother', getline('.')) 234 1 235 " go to previous match (on line 3) 236 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<cr>", 'tx') 237 call assert_equal(' 3 the theother', getline('.')) 238 1 239 " go to previous match (on line 2) 240 call feedkeys("/the\<C-G>\<C-G>\<C-G>\<C-T>\<C-T>\<C-T>\<cr>", 'tx') 241 call assert_equal(' 2 these', getline('.')) 242 1 243 " go to previous match (on line 2) 244 call feedkeys("/the\<C-G>\<C-R>\<C-W>\<cr>", 'tx') 245 call assert_equal('theother', @/) 246 247 " Test 2: keep the view, 248 " after deleting a character from the search cmd 249 call setline(1, [' 1', ' 2 these', ' 3 the', ' 4 their', ' 5 there', ' 6 their', ' 7 the', ' 8 them', ' 9 these', ' 10 foobar']) 250 resize 5 251 1 252 call feedkeys("/foo\<bs>\<cr>", 'tx') 253 redraw 254 call assert_equal({'lnum': 10, 'leftcol': 0, 'col': 4, 'topfill': 0, 'topline': 6, 'coladd': 0, 'skipcol': 0, 'curswant': 4}, winsaveview()) 255 256 " remove all history entries 257 for i in range(11) 258 call histdel('/') 259 endfor 260 261 " Test 3: reset the view, 262 " after deleting all characters from the search cmd 263 norm! 1gg0 264 " unfortunately, neither "/foo\<c-w>\<cr>", nor "/foo\<bs>\<bs>\<bs>\<cr>", 265 " nor "/foo\<c-u>\<cr>" works to delete the commandline. 266 " In that case Vim should return "E35 no previous regular expression", 267 " but it looks like Vim still sees /foo and therefore the test fails. 268 " Therefore, disabling this test 269 "call assert_fails(feedkeys("/foo\<c-w>\<cr>", 'tx'), 'E35') 270 "call assert_equal({'lnum': 1, 'leftcol': 0, 'col': 0, 'topfill': 0, 'topline': 1, 'coladd': 0, 'skipcol': 0, 'curswant': 0}, winsaveview()) 271 272 " clean up 273 set noincsearch 274 call test_override("char_avail", 0) 275 bw! 276endfunc 277 278func Test_use_sub_pat() 279 split 280 let @/ = '' 281 func X() 282 s/^/a/ 283 / 284 endfunc 285 call X() 286 bwipe! 287endfunc 288 289func Test_searchpair() 290 new 291 call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]']) 292 293 4 294 call assert_equal(3, searchpair('\[', '', ']', 'bW')) 295 call assert_equal([0, 3, 2, 0], getpos('.')) 296 4 297 call assert_equal(2, searchpair('\[', '', ']', 'bWr')) 298 call assert_equal([0, 2, 6, 0], getpos('.')) 299 4 300 call assert_equal(1, searchpair('\[', '', ']', 'bWm')) 301 call assert_equal([0, 3, 2, 0], getpos('.')) 302 4|norm ^ 303 call assert_equal(5, searchpair('\[', '', ']', 'Wn')) 304 call assert_equal([0, 4, 2, 0], getpos('.')) 305 4 306 call assert_equal(2, searchpair('\[', '', ']', 'bW', 307 \ 'getline(".") =~ "^\\s*\["')) 308 call assert_equal([0, 2, 6, 0], getpos('.')) 309 set nomagic 310 4 311 call assert_equal(3, searchpair('\[', '', ']', 'bW')) 312 call assert_equal([0, 3, 2, 0], getpos('.')) 313 set magic 314 4|norm ^ 315 call assert_equal(0, searchpair('{', '', '}', 'bW')) 316 call assert_equal([0, 4, 2, 0], getpos('.')) 317 318 %d 319 call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1']) 320 321 /\<if 1 322 call assert_equal(5, searchpair('\<if\>', '\<else\>', '\<endif\>', 'W')) 323 call assert_equal([0, 5, 1, 0], getpos('.')) 324 /\<if 2 325 call assert_equal(3, searchpair('\<if\>', '\<else\>', '\<endif\>', 'W')) 326 call assert_equal([0, 3, 3, 0], getpos('.')) 327 328 q! 329endfunc 330 331func Test_searchpairpos() 332 new 333 call setline(1, ['other code', 'here [', ' [', ' " cursor here', ' ]]']) 334 335 4 336 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW')) 337 call assert_equal([0, 3, 2, 0], getpos('.')) 338 4 339 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr')) 340 call assert_equal([0, 2, 6, 0], getpos('.')) 341 4|norm ^ 342 call assert_equal([5, 2], searchpairpos('\[', '', ']', 'Wn')) 343 call assert_equal([0, 4, 2, 0], getpos('.')) 344 4 345 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bW', 346 \ 'getline(".") =~ "^\\s*\["')) 347 call assert_equal([0, 2, 6, 0], getpos('.')) 348 4 349 call assert_equal([2, 6], searchpairpos('\[', '', ']', 'bWr')) 350 call assert_equal([0, 2, 6, 0], getpos('.')) 351 set nomagic 352 4 353 call assert_equal([3, 2], searchpairpos('\[', '', ']', 'bW')) 354 call assert_equal([0, 3, 2, 0], getpos('.')) 355 set magic 356 4|norm ^ 357 call assert_equal([0, 0], searchpairpos('{', '', '}', 'bW')) 358 call assert_equal([0, 4, 2, 0], getpos('.')) 359 360 %d 361 call setline(1, ['if 1', ' if 2', ' else', ' endif 2', 'endif 1']) 362 /\<if 1 363 call assert_equal([5, 1], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W')) 364 call assert_equal([0, 5, 1, 0], getpos('.')) 365 /\<if 2 366 call assert_equal([3, 3], searchpairpos('\<if\>', '\<else\>', '\<endif\>', 'W')) 367 call assert_equal([0, 3, 3, 0], getpos('.')) 368 369 q! 370endfunc 371 372func Test_searchpair_errors() 373 call assert_fails("call searchpair([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: using List as a String') 374 call assert_fails("call searchpair('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: using Funcref as a String') 375 call assert_fails("call searchpair('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: using Dictionary as a String') 376 call assert_fails("call searchpair('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags') 377 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 0, 99, 100)", 'E475: Invalid argument: 0') 378 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99') 379 call assert_fails("call searchpair('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100') 380 call assert_fails("call searchpair('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e') 381 call assert_fails("call searchpair('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn') 382endfunc 383 384func Test_searchpairpos_errors() 385 call assert_fails("call searchpairpos([0], 'middle', 'end', 'bW', 'skip', 99, 100)", 'E730: using List as a String') 386 call assert_fails("call searchpairpos('start', {-> 0}, 'end', 'bW', 'skip', 99, 100)", 'E729: using Funcref as a String') 387 call assert_fails("call searchpairpos('start', 'middle', {'one': 1}, 'bW', 'skip', 99, 100)", 'E731: using Dictionary as a String') 388 call assert_fails("call searchpairpos('start', 'middle', 'end', 'flags', 'skip', 99, 100)", 'E475: Invalid argument: flags') 389 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 0, 99, 100)", 'E475: Invalid argument: 0') 390 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', -99, 100)", 'E475: Invalid argument: -99') 391 call assert_fails("call searchpairpos('start', 'middle', 'end', 'bW', 'func', 99, -100)", 'E475: Invalid argument: -100') 392 call assert_fails("call searchpairpos('start', 'middle', 'end', 'e')", 'E475: Invalid argument: e') 393 call assert_fails("call searchpairpos('start', 'middle', 'end', 'sn')", 'E475: Invalid argument: sn') 394endfunc 395 396func Test_searchpair_skip() 397 func Zero() 398 return 0 399 endfunc 400 func Partial(x) 401 return a:x 402 endfunc 403 new 404 call setline(1, ['{', 'foo', 'foo', 'foo', '}']) 405 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', '')) 406 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', '0')) 407 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', {-> 0})) 408 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Zero'))) 409 3 | call assert_equal(1, searchpair('{', '', '}', 'bWn', function('Partial', [0]))) 410 bw! 411endfunc 412 413func Test_searchpair_leak() 414 new 415 call setline(1, 'if one else another endif') 416 417 " The error in the skip expression caused memory to leak. 418 call assert_fails("call searchpair('\\<if\\>', '\\<else\\>', '\\<endif\\>', '', '\"foo\" 2')", 'E15:') 419 420 bwipe! 421endfunc 422 423func Test_searchc() 424 " These commands used to cause memory overflow in searchc(). 425 new 426 norm ixx 427 exe "norm 0t\u93cf" 428 bw! 429endfunc 430 431func Cmdline3_prep() 432 " need to disable char_avail, 433 " so that expansion of commandline works 434 call test_override("char_avail", 1) 435 new 436 call setline(1, [' 1', ' 2 the~e', ' 3 the theother']) 437 set incsearch 438endfunc 439 440func Incsearch_cleanup() 441 set noincsearch 442 call test_override("char_avail", 0) 443 bw! 444endfunc 445 446func Test_search_cmdline3() 447 CheckOption incsearch 448 449 call Cmdline3_prep() 450 1 451 " first match 452 call feedkeys("/the\<c-l>\<cr>", 'tx') 453 call assert_equal(' 2 the~e', getline('.')) 454 455 call Incsearch_cleanup() 456endfunc 457 458func Test_search_cmdline3s() 459 CheckOption incsearch 460 461 call Cmdline3_prep() 462 1 463 call feedkeys(":%s/the\<c-l>/xxx\<cr>", 'tx') 464 call assert_equal(' 2 xxxe', getline('.')) 465 undo 466 call feedkeys(":%subs/the\<c-l>/xxx\<cr>", 'tx') 467 call assert_equal(' 2 xxxe', getline('.')) 468 undo 469 call feedkeys(":%substitute/the\<c-l>/xxx\<cr>", 'tx') 470 call assert_equal(' 2 xxxe', getline('.')) 471 undo 472 call feedkeys(":%smagic/the.e/xxx\<cr>", 'tx') 473 call assert_equal(' 2 xxx', getline('.')) 474 undo 475 call assert_fails(":%snomagic/the.e/xxx\<cr>", 'E486') 476 " 477 call feedkeys(":%snomagic/the\\.e/xxx\<cr>", 'tx') 478 call assert_equal(' 2 xxx', getline('.')) 479 480 call Incsearch_cleanup() 481endfunc 482 483func Test_search_cmdline3g() 484 CheckOption incsearch 485 486 call Cmdline3_prep() 487 1 488 call feedkeys(":g/the\<c-l>/d\<cr>", 'tx') 489 call assert_equal(' 3 the theother', getline(2)) 490 undo 491 call feedkeys(":global/the\<c-l>/d\<cr>", 'tx') 492 call assert_equal(' 3 the theother', getline(2)) 493 undo 494 call feedkeys(":g!/the\<c-l>/d\<cr>", 'tx') 495 call assert_equal(1, line('$')) 496 call assert_equal(' 2 the~e', getline(1)) 497 undo 498 call feedkeys(":global!/the\<c-l>/d\<cr>", 'tx') 499 call assert_equal(1, line('$')) 500 call assert_equal(' 2 the~e', getline(1)) 501 502 call Incsearch_cleanup() 503endfunc 504 505func Test_search_cmdline3v() 506 CheckOption incsearch 507 508 call Cmdline3_prep() 509 1 510 call feedkeys(":v/the\<c-l>/d\<cr>", 'tx') 511 call assert_equal(1, line('$')) 512 call assert_equal(' 2 the~e', getline(1)) 513 undo 514 call feedkeys(":vglobal/the\<c-l>/d\<cr>", 'tx') 515 call assert_equal(1, line('$')) 516 call assert_equal(' 2 the~e', getline(1)) 517 518 call Incsearch_cleanup() 519endfunc 520 521func Test_search_cmdline4() 522 CheckOption incsearch 523 524 " need to disable char_avail, 525 " so that expansion of commandline works 526 call test_override("char_avail", 1) 527 new 528 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third']) 529 set incsearch 530 $ 531 call feedkeys("?the\<c-g>\<cr>", 'tx') 532 call assert_equal(' 3 the third', getline('.')) 533 $ 534 call feedkeys("?the\<c-g>\<c-g>\<cr>", 'tx') 535 call assert_equal(' 1 the first', getline('.')) 536 $ 537 call feedkeys("?the\<c-g>\<c-g>\<c-g>\<cr>", 'tx') 538 call assert_equal(' 2 the second', getline('.')) 539 $ 540 call feedkeys("?the\<c-t>\<cr>", 'tx') 541 call assert_equal(' 1 the first', getline('.')) 542 $ 543 call feedkeys("?the\<c-t>\<c-t>\<cr>", 'tx') 544 call assert_equal(' 3 the third', getline('.')) 545 $ 546 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx') 547 call assert_equal(' 2 the second', getline('.')) 548 " clean up 549 set noincsearch 550 call test_override("char_avail", 0) 551 bw! 552endfunc 553 554func Test_search_cmdline5() 555 CheckOption incsearch 556 557 " Do not call test_override("char_avail", 1) so that <C-g> and <C-t> work 558 " regardless char_avail. 559 new 560 call setline(1, [' 1 the first', ' 2 the second', ' 3 the third', '']) 561 set incsearch 562 1 563 call feedkeys("/the\<c-g>\<c-g>\<cr>", 'tx') 564 call assert_equal(' 3 the third', getline('.')) 565 $ 566 call feedkeys("?the\<c-t>\<c-t>\<c-t>\<cr>", 'tx') 567 call assert_equal(' 1 the first', getline('.')) 568 " clean up 569 set noincsearch 570 bw! 571endfunc 572 573func Test_search_cmdline6() 574 " Test that consecutive matches 575 " are caught by <c-g>/<c-t> 576 CheckOption incsearch 577 578 " need to disable char_avail, 579 " so that expansion of commandline works 580 call test_override("char_avail", 1) 581 new 582 call setline(1, [' bbvimb', '']) 583 set incsearch 584 " first match 585 norm! gg0 586 call feedkeys("/b\<cr>", 'tx') 587 call assert_equal([0,1,2,0], getpos('.')) 588 " second match 589 norm! gg0 590 call feedkeys("/b\<c-g>\<cr>", 'tx') 591 call assert_equal([0,1,3,0], getpos('.')) 592 " third match 593 norm! gg0 594 call feedkeys("/b\<c-g>\<c-g>\<cr>", 'tx') 595 call assert_equal([0,1,7,0], getpos('.')) 596 " first match again 597 norm! gg0 598 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx') 599 call assert_equal([0,1,2,0], getpos('.')) 600 set nowrapscan 601 " last match 602 norm! gg0 603 call feedkeys("/b\<c-g>\<c-g>\<c-g>\<cr>", 'tx') 604 call assert_equal([0,1,7,0], getpos('.')) 605 " clean up 606 set wrapscan&vim 607 set noincsearch 608 call test_override("char_avail", 0) 609 bw! 610endfunc 611 612func Test_search_cmdline7() 613 " Test that an pressing <c-g> in an empty command line 614 " does not move the cursor 615 CheckOption incsearch 616 617 " need to disable char_avail, 618 " so that expansion of commandline works 619 call test_override("char_avail", 1) 620 new 621 let @/ = 'b' 622 call setline(1, [' bbvimb', '']) 623 set incsearch 624 " first match 625 norm! gg0 626 " moves to next match of previous search pattern, just like /<cr> 627 call feedkeys("/\<c-g>\<cr>", 'tx') 628 call assert_equal([0,1,2,0], getpos('.')) 629 " moves to next match of previous search pattern, just like /<cr> 630 call feedkeys("/\<cr>", 'tx') 631 call assert_equal([0,1,3,0], getpos('.')) 632 " moves to next match of previous search pattern, just like /<cr> 633 call feedkeys("/\<c-t>\<cr>", 'tx') 634 call assert_equal([0,1,7,0], getpos('.')) 635 636 " using an offset uses the last search pattern 637 call cursor(1, 1) 638 call setline(1, ['1 bbvimb', ' 2 bbvimb']) 639 let @/ = 'b' 640 call feedkeys("//e\<c-g>\<cr>", 'tx') 641 call assert_equal('1 bbvimb', getline('.')) 642 call assert_equal(4, col('.')) 643 644 set noincsearch 645 call test_override("char_avail", 0) 646 bw! 647endfunc 648 649func Test_search_cmdline8() 650 " Highlighting is cleared in all windows 651 " since hls applies to all windows 652 CheckOption incsearch 653 CheckFeature terminal 654 CheckNotGui 655 if has("win32") 656 throw "Skipped: Bug with sending <ESC> to terminal window not fixed yet" 657 endif 658 659 let h = winheight(0) 660 if h < 3 661 return 662 endif 663 " Prepare buffer text 664 let lines = ['abb vim vim vi', 'vimvivim'] 665 call writefile(lines, 'Xsearch.txt') 666 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3}) 667 668 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])}) 669 670 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>") 671 call term_sendkeys(buf, ":14vsp\<cr>") 672 call term_sendkeys(buf, "/vim\<cr>") 673 call term_sendkeys(buf, "/b\<esc>") 674 call term_sendkeys(buf, "gg0") 675 call TermWait(buf, 250) 676 let screen_line = term_scrape(buf, 1) 677 let [a0,a1,a2,a3] = [screen_line[3].attr, screen_line[4].attr, 678 \ screen_line[18].attr, screen_line[19].attr] 679 call assert_notequal(a0, a1) 680 call assert_notequal(a0, a3) 681 call assert_notequal(a1, a2) 682 call assert_equal(a0, a2) 683 call assert_equal(a1, a3) 684 " clean up 685 call delete('Xsearch.txt') 686 687 bwipe! 688endfunc 689 690" Tests for regexp with various magic settings 691func Run_search_regexp_magic_opt() 692 put ='1 a aa abb abbccc' 693 exe 'normal! /a*b\{2}c\+/e' . "\<CR>" 694 call assert_equal([0, 2, 17, 0], getpos('.')) 695 696 put ='2 d dd dee deefff' 697 exe 'normal! /\Md\*e\{2}f\+/e' . "\<CR>" 698 call assert_equal([0, 3, 17, 0], getpos('.')) 699 700 set nomagic 701 put ='3 g gg ghh ghhiii' 702 exe 'normal! /g\*h\{2}i\+/e' . "\<CR>" 703 call assert_equal([0, 4, 17, 0], getpos('.')) 704 705 put ='4 j jj jkk jkklll' 706 exe 'normal! /\mj*k\{2}l\+/e' . "\<CR>" 707 call assert_equal([0, 5, 17, 0], getpos('.')) 708 709 put ='5 m mm mnn mnnooo' 710 exe 'normal! /\vm*n{2}o+/e' . "\<CR>" 711 call assert_equal([0, 6, 17, 0], getpos('.')) 712 713 put ='6 x ^aa$ x' 714 exe 'normal! /\V^aa$' . "\<CR>" 715 call assert_equal([0, 7, 5, 0], getpos('.')) 716 717 set magic 718 put ='7 (a)(b) abbaa' 719 exe 'normal! /\v(a)(b)\2\1\1/e' . "\<CR>" 720 call assert_equal([0, 8, 14, 0], getpos('.')) 721 722 put ='8 axx [ab]xx' 723 exe 'normal! /\V[ab]\(\[xy]\)\1' . "\<CR>" 724 call assert_equal([0, 9, 7, 0], getpos('.')) 725 726 %d 727endfunc 728 729func Test_search_regexp() 730 enew! 731 732 set regexpengine=1 733 call Run_search_regexp_magic_opt() 734 set regexpengine=2 735 call Run_search_regexp_magic_opt() 736 set regexpengine& 737 738 set undolevels=100 739 put ='9 foobar' 740 put ='' 741 exe "normal! a\<C-G>u\<Esc>" 742 normal G 743 exe 'normal! dv?bar?' . "\<CR>" 744 call assert_equal('9 foo', getline('.')) 745 call assert_equal([0, 2, 5, 0], getpos('.')) 746 call assert_equal(2, line('$')) 747 normal u 748 call assert_equal('9 foobar', getline('.')) 749 call assert_equal([0, 2, 6, 0], getpos('.')) 750 call assert_equal(3, line('$')) 751 752 set undolevels& 753 enew! 754endfunc 755 756func Test_search_cmdline_incsearch_highlight() 757 CheckOption incsearch 758 759 set incsearch hlsearch 760 " need to disable char_avail, 761 " so that expansion of commandline works 762 call test_override("char_avail", 1) 763 new 764 call setline(1, ['aaa 1 the first', ' 2 the second', ' 3 the third']) 765 766 1 767 call feedkeys("/second\<cr>", 'tx') 768 call assert_equal('second', @/) 769 call assert_equal(' 2 the second', getline('.')) 770 771 " Canceling search won't change @/ 772 1 773 let @/ = 'last pattern' 774 call feedkeys("/third\<C-c>", 'tx') 775 call assert_equal('last pattern', @/) 776 call feedkeys("/third\<Esc>", 'tx') 777 call assert_equal('last pattern', @/) 778 call feedkeys("/3\<bs>\<bs>", 'tx') 779 call assert_equal('last pattern', @/) 780 call feedkeys("/third\<c-g>\<c-t>\<Esc>", 'tx') 781 call assert_equal('last pattern', @/) 782 783 " clean up 784 set noincsearch nohlsearch 785 bw! 786endfunc 787 788func Test_search_cmdline_incsearch_highlight_attr() 789 CheckOption incsearch 790 CheckFeature terminal 791 CheckNotGui 792 793 let h = winheight(0) 794 if h < 3 795 return 796 endif 797 798 " Prepare buffer text 799 let lines = ['abb vim vim vi', 'vimvivim'] 800 call writefile(lines, 'Xsearch.txt') 801 let buf = term_start([GetVimProg(), '--clean', '-c', 'set noswapfile', 'Xsearch.txt'], {'term_rows': 3}) 802 803 call WaitForAssert({-> assert_equal(lines, [term_getline(buf, 1), term_getline(buf, 2)])}) 804 " wait for vim to complete initialization 805 call TermWait(buf) 806 807 " Get attr of normal(a0), incsearch(a1), hlsearch(a2) highlight 808 call term_sendkeys(buf, ":set incsearch hlsearch\<cr>") 809 call term_sendkeys(buf, '/b') 810 call TermWait(buf, 100) 811 let screen_line1 = term_scrape(buf, 1) 812 call assert_true(len(screen_line1) > 2) 813 " a0: attr_normal 814 let a0 = screen_line1[0].attr 815 " a1: attr_incsearch 816 let a1 = screen_line1[1].attr 817 " a2: attr_hlsearch 818 let a2 = screen_line1[2].attr 819 call assert_notequal(a0, a1) 820 call assert_notequal(a0, a2) 821 call assert_notequal(a1, a2) 822 call term_sendkeys(buf, "\<cr>gg0") 823 824 " Test incremental highlight search 825 call term_sendkeys(buf, "/vim") 826 call TermWait(buf, 100) 827 " Buffer: 828 " abb vim vim vi 829 " vimvivim 830 " Search: /vim 831 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a2,a2,a2,a0,a0,a0] 832 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2] 833 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr')) 834 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr')) 835 836 " Test <C-g> 837 call term_sendkeys(buf, "\<C-g>\<C-g>") 838 call TermWait(buf, 100) 839 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0] 840 let attr_line2 = [a1,a1,a1,a0,a0,a2,a2,a2] 841 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr')) 842 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr')) 843 844 " Test <C-t> 845 call term_sendkeys(buf, "\<C-t>") 846 call TermWait(buf, 100) 847 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a1,a1,a1,a0,a0,a0] 848 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2] 849 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr')) 850 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr')) 851 852 " Type Enter and a1(incsearch highlight) should become a2(hlsearch highlight) 853 call term_sendkeys(buf, "\<cr>") 854 call TermWait(buf, 100) 855 let attr_line1 = [a0,a0,a0,a0,a2,a2,a2,a0,a2,a2,a2,a0,a0,a0] 856 let attr_line2 = [a2,a2,a2,a0,a0,a2,a2,a2] 857 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr')) 858 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr')) 859 860 " Test nohlsearch. a2(hlsearch highlight) should become a0(normal highlight) 861 call term_sendkeys(buf, ":1\<cr>") 862 call term_sendkeys(buf, ":set nohlsearch\<cr>") 863 call term_sendkeys(buf, "/vim") 864 call TermWait(buf, 100) 865 let attr_line1 = [a0,a0,a0,a0,a1,a1,a1,a0,a0,a0,a0,a0,a0,a0] 866 let attr_line2 = [a0,a0,a0,a0,a0,a0,a0,a0] 867 call assert_equal(attr_line1, map(term_scrape(buf, 1)[:len(attr_line1)-1], 'v:val.attr')) 868 call assert_equal(attr_line2, map(term_scrape(buf, 2)[:len(attr_line2)-1], 'v:val.attr')) 869 call delete('Xsearch.txt') 870 871 call delete('Xsearch.txt') 872 bwipe! 873endfunc 874 875func Test_incsearch_cmdline_modifier() 876 CheckOption incsearch 877 878 call test_override("char_avail", 1) 879 new 880 call setline(1, ['foo']) 881 set incsearch 882 " Test that error E14 does not occur in parsing command modifier. 883 call feedkeys("V:tab", 'tx') 884 885 call Incsearch_cleanup() 886endfunc 887 888func Test_incsearch_scrolling() 889 if !CanRunVimInTerminal() 890 throw 'Skipped: cannot make screendumps' 891 endif 892 call assert_equal(0, &scrolloff) 893 call writefile([ 894 \ 'let dots = repeat(".", 120)', 895 \ 'set incsearch cmdheight=2 scrolloff=0', 896 \ 'call setline(1, [dots, dots, dots, "", "target", dots, dots])', 897 \ 'normal gg', 898 \ 'redraw', 899 \ ], 'Xscript') 900 let buf = RunVimInTerminal('-S Xscript', {'rows': 9, 'cols': 70}) 901 " Need to send one key at a time to force a redraw 902 call term_sendkeys(buf, '/') 903 sleep 100m 904 call term_sendkeys(buf, 't') 905 sleep 100m 906 call term_sendkeys(buf, 'a') 907 sleep 100m 908 call term_sendkeys(buf, 'r') 909 sleep 100m 910 call term_sendkeys(buf, 'g') 911 call VerifyScreenDump(buf, 'Test_incsearch_scrolling_01', {}) 912 913 call term_sendkeys(buf, "\<Esc>") 914 call StopVimInTerminal(buf) 915 call delete('Xscript') 916endfunc 917 918func Test_incsearch_search_dump() 919 CheckOption incsearch 920 CheckScreendump 921 922 call writefile([ 923 \ 'set incsearch hlsearch scrolloff=0', 924 \ 'for n in range(1, 8)', 925 \ ' call setline(n, "foo " . n)', 926 \ 'endfor', 927 \ '3', 928 \ ], 'Xis_search_script') 929 let buf = RunVimInTerminal('-S Xis_search_script', {'rows': 9, 'cols': 70}) 930 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by 931 " the 'ambiwidth' check. 932 sleep 100m 933 934 " Need to send one key at a time to force a redraw. 935 call term_sendkeys(buf, '/fo') 936 call VerifyScreenDump(buf, 'Test_incsearch_search_01', {}) 937 call term_sendkeys(buf, "\<Esc>") 938 sleep 100m 939 940 call term_sendkeys(buf, '/\v') 941 call VerifyScreenDump(buf, 'Test_incsearch_search_02', {}) 942 call term_sendkeys(buf, "\<Esc>") 943 944 call StopVimInTerminal(buf) 945 call delete('Xis_search_script') 946endfunc 947 948func Test_incsearch_substitute() 949 CheckOption incsearch 950 951 call test_override("char_avail", 1) 952 new 953 set incsearch 954 for n in range(1, 10) 955 call setline(n, 'foo ' . n) 956 endfor 957 4 958 call feedkeys(":.,.+2s/foo\<BS>o\<BS>o/xxx\<cr>", 'tx') 959 call assert_equal('foo 3', getline(3)) 960 call assert_equal('xxx 4', getline(4)) 961 call assert_equal('xxx 5', getline(5)) 962 call assert_equal('xxx 6', getline(6)) 963 call assert_equal('foo 7', getline(7)) 964 965 call Incsearch_cleanup() 966endfunc 967 968" Similar to Test_incsearch_substitute() but with a screendump halfway. 969func Test_incsearch_substitute_dump() 970 CheckOption incsearch 971 CheckScreendump 972 973 call writefile([ 974 \ 'set incsearch hlsearch scrolloff=0', 975 \ 'for n in range(1, 10)', 976 \ ' call setline(n, "foo " . n)', 977 \ 'endfor', 978 \ 'call setline(11, "bar 11")', 979 \ '3', 980 \ ], 'Xis_subst_script') 981 let buf = RunVimInTerminal('-S Xis_subst_script', {'rows': 9, 'cols': 70}) 982 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by 983 " the 'ambiwidth' check. 984 sleep 100m 985 986 " Need to send one key at a time to force a redraw. 987 " Select three lines at the cursor with typed pattern. 988 call term_sendkeys(buf, ':.,.+2s/') 989 sleep 100m 990 call term_sendkeys(buf, 'f') 991 sleep 100m 992 call term_sendkeys(buf, 'o') 993 sleep 100m 994 call term_sendkeys(buf, 'o') 995 call VerifyScreenDump(buf, 'Test_incsearch_substitute_01', {}) 996 call term_sendkeys(buf, "\<Esc>") 997 998 " Select three lines at the cursor using previous pattern. 999 call term_sendkeys(buf, "/foo\<CR>") 1000 sleep 100m 1001 call term_sendkeys(buf, ':.,.+2s//') 1002 call VerifyScreenDump(buf, 'Test_incsearch_substitute_02', {}) 1003 1004 " Deleting last slash should remove the match. 1005 call term_sendkeys(buf, "\<BS>") 1006 call VerifyScreenDump(buf, 'Test_incsearch_substitute_03', {}) 1007 call term_sendkeys(buf, "\<Esc>") 1008 1009 " Reverse range is accepted 1010 call term_sendkeys(buf, ':5,2s/foo') 1011 call VerifyScreenDump(buf, 'Test_incsearch_substitute_04', {}) 1012 call term_sendkeys(buf, "\<Esc>") 1013 1014 " White space after the command is skipped 1015 call term_sendkeys(buf, ':2,3sub /fo') 1016 call VerifyScreenDump(buf, 'Test_incsearch_substitute_05', {}) 1017 call term_sendkeys(buf, "\<Esc>") 1018 1019 " Command modifiers are skipped 1020 call term_sendkeys(buf, ':above below browse botr confirm keepmar keepalt keeppat keepjum filter xxx hide lockm leftabove noau noswap rightbel sandbox silent silent! $tab top unsil vert verbose 4,5s/fo.') 1021 call VerifyScreenDump(buf, 'Test_incsearch_substitute_06', {}) 1022 call term_sendkeys(buf, "\<Esc>") 1023 1024 " Cursorline highlighting at match 1025 call term_sendkeys(buf, ":set cursorline\<CR>") 1026 call term_sendkeys(buf, 'G9G') 1027 call term_sendkeys(buf, ':9,11s/bar') 1028 call VerifyScreenDump(buf, 'Test_incsearch_substitute_07', {}) 1029 call term_sendkeys(buf, "\<Esc>") 1030 1031 " Cursorline highlighting at cursor when no match 1032 call term_sendkeys(buf, ':9,10s/bar') 1033 call VerifyScreenDump(buf, 'Test_incsearch_substitute_08', {}) 1034 call term_sendkeys(buf, "\<Esc>") 1035 1036 " Only \v handled as empty pattern, does not move cursor 1037 call term_sendkeys(buf, '3G4G') 1038 call term_sendkeys(buf, ":nohlsearch\<CR>") 1039 call term_sendkeys(buf, ':6,7s/\v') 1040 call VerifyScreenDump(buf, 'Test_incsearch_substitute_09', {}) 1041 call term_sendkeys(buf, "\<Esc>") 1042 1043 call term_sendkeys(buf, ":set nocursorline\<CR>") 1044 1045 " All matches are highlighted for 'hlsearch' after the incsearch canceled 1046 call term_sendkeys(buf, "1G*") 1047 call term_sendkeys(buf, ":2,5s/foo") 1048 sleep 100m 1049 call term_sendkeys(buf, "\<Esc>") 1050 call VerifyScreenDump(buf, 'Test_incsearch_substitute_10', {}) 1051 1052 call term_sendkeys(buf, ":split\<CR>") 1053 call term_sendkeys(buf, ":let @/ = 'xyz'\<CR>") 1054 call term_sendkeys(buf, ":%s/.") 1055 call VerifyScreenDump(buf, 'Test_incsearch_substitute_11', {}) 1056 call term_sendkeys(buf, "\<BS>") 1057 call VerifyScreenDump(buf, 'Test_incsearch_substitute_12', {}) 1058 call term_sendkeys(buf, "\<Esc>") 1059 call VerifyScreenDump(buf, 'Test_incsearch_substitute_13', {}) 1060 call term_sendkeys(buf, ":%bwipe!\<CR>") 1061 call term_sendkeys(buf, ":only!\<CR>") 1062 1063 " get :'<,'>s command in history 1064 call term_sendkeys(buf, ":set cmdheight=2\<CR>") 1065 call term_sendkeys(buf, "aasdfasdf\<Esc>") 1066 call term_sendkeys(buf, "V:s/a/b/g\<CR>") 1067 " Using '<,'> does not give E20 1068 call term_sendkeys(buf, ":new\<CR>") 1069 call term_sendkeys(buf, "aasdfasdf\<Esc>") 1070 call term_sendkeys(buf, ":\<Up>\<Up>") 1071 call VerifyScreenDump(buf, 'Test_incsearch_substitute_14', {}) 1072 call term_sendkeys(buf, "<Esc>") 1073 1074 call StopVimInTerminal(buf) 1075 call delete('Xis_subst_script') 1076endfunc 1077 1078func Test_incsearch_highlighting() 1079 CheckOption incsearch 1080 CheckScreendump 1081 1082 call writefile([ 1083 \ 'set incsearch hlsearch', 1084 \ 'call setline(1, "hello/there")', 1085 \ ], 'Xis_subst_hl_script') 1086 let buf = RunVimInTerminal('-S Xis_subst_hl_script', {'rows': 4, 'cols': 20}) 1087 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by 1088 " the 'ambiwidth' check. 1089 sleep 300m 1090 1091 " Using a different search delimiter should still highlight matches 1092 " that contain a '/'. 1093 call term_sendkeys(buf, ":%s;ello/the") 1094 call VerifyScreenDump(buf, 'Test_incsearch_substitute_15', {}) 1095 call term_sendkeys(buf, "<Esc>") 1096 1097 call StopVimInTerminal(buf) 1098 call delete('Xis_subst_hl_script') 1099endfunc 1100 1101func Test_incsearch_with_change() 1102 CheckFeature timers 1103 CheckOption incsearch 1104 CheckScreendump 1105 1106 call writefile([ 1107 \ 'set incsearch hlsearch scrolloff=0', 1108 \ 'call setline(1, ["one", "two ------ X", "three"])', 1109 \ 'call timer_start(200, { _ -> setline(2, "x")})', 1110 \ ], 'Xis_change_script') 1111 let buf = RunVimInTerminal('-S Xis_change_script', {'rows': 9, 'cols': 70}) 1112 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by 1113 " the 'ambiwidth' check. 1114 sleep 300m 1115 1116 " Highlight X, it will be deleted by the timer callback. 1117 call term_sendkeys(buf, ':%s/X') 1118 call VerifyScreenDump(buf, 'Test_incsearch_change_01', {}) 1119 call term_sendkeys(buf, "\<Esc>") 1120 1121 call StopVimInTerminal(buf) 1122 call delete('Xis_change_script') 1123endfunc 1124 1125" Similar to Test_incsearch_substitute_dump() for :sort 1126func Test_incsearch_sort_dump() 1127 CheckOption incsearch 1128 CheckScreendump 1129 1130 call writefile([ 1131 \ 'set incsearch hlsearch scrolloff=0', 1132 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])', 1133 \ ], 'Xis_sort_script') 1134 let buf = RunVimInTerminal('-S Xis_sort_script', {'rows': 9, 'cols': 70}) 1135 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by 1136 " the 'ambiwidth' check. 1137 sleep 100m 1138 1139 call term_sendkeys(buf, ':sort ni u /on') 1140 call VerifyScreenDump(buf, 'Test_incsearch_sort_01', {}) 1141 call term_sendkeys(buf, "\<Esc>") 1142 1143 call term_sendkeys(buf, ':sort! /on') 1144 call VerifyScreenDump(buf, 'Test_incsearch_sort_02', {}) 1145 call term_sendkeys(buf, "\<Esc>") 1146 1147 call StopVimInTerminal(buf) 1148 call delete('Xis_sort_script') 1149endfunc 1150 1151" Similar to Test_incsearch_substitute_dump() for :vimgrep famiry 1152func Test_incsearch_vimgrep_dump() 1153 CheckOption incsearch 1154 CheckScreendump 1155 1156 call writefile([ 1157 \ 'set incsearch hlsearch scrolloff=0', 1158 \ 'call setline(1, ["another one 2", "that one 3", "the one 1"])', 1159 \ ], 'Xis_vimgrep_script') 1160 let buf = RunVimInTerminal('-S Xis_vimgrep_script', {'rows': 9, 'cols': 70}) 1161 " Give Vim a chance to redraw to get rid of the spaces in line 2 caused by 1162 " the 'ambiwidth' check. 1163 sleep 100m 1164 1165 " Need to send one key at a time to force a redraw. 1166 call term_sendkeys(buf, ':vimgrep on') 1167 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_01', {}) 1168 call term_sendkeys(buf, "\<Esc>") 1169 1170 call term_sendkeys(buf, ':vimg /on/ *.txt') 1171 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_02', {}) 1172 call term_sendkeys(buf, "\<Esc>") 1173 1174 call term_sendkeys(buf, ':vimgrepadd "\<on') 1175 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_03', {}) 1176 call term_sendkeys(buf, "\<Esc>") 1177 1178 call term_sendkeys(buf, ':lv "tha') 1179 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_04', {}) 1180 call term_sendkeys(buf, "\<Esc>") 1181 1182 call term_sendkeys(buf, ':lvimgrepa "the" **/*.txt') 1183 call VerifyScreenDump(buf, 'Test_incsearch_vimgrep_05', {}) 1184 call term_sendkeys(buf, "\<Esc>") 1185 1186 call StopVimInTerminal(buf) 1187 call delete('Xis_vimgrep_script') 1188endfunc 1189 1190func Test_keep_last_search_pattern() 1191 CheckOption incsearch 1192 1193 new 1194 call setline(1, ['foo', 'foo', 'foo']) 1195 set incsearch 1196 call test_override("char_avail", 1) 1197 let @/ = 'bar' 1198 call feedkeys(":/foo/s//\<Esc>", 'ntx') 1199 call assert_equal('bar', @/) 1200 1201 " no error message if pattern not found 1202 call feedkeys(":/xyz/s//\<Esc>", 'ntx') 1203 call assert_equal('bar', @/) 1204 1205 bwipe! 1206 call test_override("ALL", 0) 1207 set noincsearch 1208endfunc 1209 1210func Test_word_under_cursor_after_match() 1211 CheckOption incsearch 1212 1213 new 1214 call setline(1, 'foo bar') 1215 set incsearch 1216 call test_override("char_avail", 1) 1217 try 1218 call feedkeys("/foo\<C-R>\<C-W>\<CR>", 'ntx') 1219 catch /E486:/ 1220 endtry 1221 call assert_equal('foobar', @/) 1222 1223 bwipe! 1224 call test_override("ALL", 0) 1225 set noincsearch 1226endfunc 1227 1228func Test_subst_word_under_cursor() 1229 CheckOption incsearch 1230 1231 new 1232 call setline(1, ['int SomeLongName;', 'for (xxx = 1; xxx < len; ++xxx)']) 1233 set incsearch 1234 call test_override("char_avail", 1) 1235 call feedkeys("/LongName\<CR>", 'ntx') 1236 call feedkeys(":%s/xxx/\<C-R>\<C-W>/g\<CR>", 'ntx') 1237 call assert_equal('for (SomeLongName = 1; SomeLongName < len; ++SomeLongName)', getline(2)) 1238 1239 bwipe! 1240 call test_override("ALL", 0) 1241 set noincsearch 1242endfunc 1243 1244func Test_search_undefined_behaviour() 1245 CheckFeature terminal 1246 1247 let h = winheight(0) 1248 if h < 3 1249 return 1250 endif 1251 " did cause an undefined left shift 1252 let g:buf = term_start([GetVimProg(), '--clean', '-e', '-s', '-c', 'call search(getline("."))', 'samples/test000'], {'term_rows': 3}) 1253 call assert_equal([''], getline(1, '$')) 1254 call term_sendkeys(g:buf, ":qa!\<cr>") 1255 bwipe! 1256endfunc 1257 1258func Test_search_undefined_behaviour2() 1259 call search("\%UC0000000") 1260endfunc 1261 1262" Test for search('multi-byte char', 'bce') 1263func Test_search_multibyte() 1264 let save_enc = &encoding 1265 set encoding=utf8 1266 enew! 1267 call append('$', 'A') 1268 call cursor(2, 1) 1269 call assert_equal(2, search('A', 'bce', line('.'))) 1270 enew! 1271 let &encoding = save_enc 1272endfunc 1273 1274" This was causing E874. Also causes an invalid read? 1275func Test_look_behind() 1276 new 1277 call setline(1, '0\|\&\n\@<=') 1278 call search(getline(".")) 1279 bwipe! 1280endfunc 1281 1282func Test_search_sentence() 1283 new 1284 " this used to cause a crash 1285 call assert_fails("/\\%')", 'E486') 1286 call assert_fails("/", 'E486') 1287 /\%'( 1288 / 1289endfunc 1290 1291" Test that there is no crash when there is a last search pattern but no last 1292" substitute pattern. 1293func Test_no_last_substitute_pat() 1294 " Use viminfo to set the last search pattern to a string and make the last 1295 " substitute pattern the most recent used and make it empty (NULL). 1296 call writefile(['~MSle0/bar', '~MSle0~&'], 'Xviminfo') 1297 rviminfo! Xviminfo 1298 call assert_fails('normal n', 'E35:') 1299 1300 call delete('Xviminfo') 1301endfunc 1302 1303func Test_search_Ctrl_L_combining() 1304 " Make sure, that Ctrl-L works correctly with combining characters. 1305 " It uses an artificial example of an 'a' with 4 combining chars: 1306 " 'a' U+0061 Dec:97 LATIN SMALL LETTER A a /\%u61\Z "\u0061" 1307 " ' ̀' U+0300 Dec:768 COMBINING GRAVE ACCENT ̀ /\%u300\Z "\u0300" 1308 " ' ́' U+0301 Dec:769 COMBINING ACUTE ACCENT ́ /\%u301\Z "\u0301" 1309 " ' ̇' U+0307 Dec:775 COMBINING DOT ABOVE ̇ /\%u307\Z "\u0307" 1310 " ' ̣' U+0323 Dec:803 COMBINING DOT BELOW ̣ /\%u323 "\u0323" 1311 " Those should also appear on the commandline 1312 CheckOption incsearch 1313 1314 call Cmdline3_prep() 1315 1 1316 let bufcontent = ['', 'Miạ̀́̇m'] 1317 call append('$', bufcontent) 1318 call feedkeys("/Mi\<c-l>\<c-l>\<cr>", 'tx') 1319 call assert_equal(5, line('.')) 1320 call assert_equal(bufcontent[1], @/) 1321 call Incsearch_cleanup() 1322endfunc 1323 1324func Test_large_hex_chars1() 1325 " This used to cause a crash, the character becomes an NFA state. 1326 try 1327 /\%Ufffffc23 1328 catch 1329 call assert_match('E678:', v:exception) 1330 endtry 1331 try 1332 set re=1 1333 /\%Ufffffc23 1334 catch 1335 call assert_match('E678:', v:exception) 1336 endtry 1337 set re& 1338endfunc 1339 1340func Test_large_hex_chars2() 1341 " This used to cause a crash, the character becomes an NFA state. 1342 try 1343 /[\Ufffffc1f] 1344 catch 1345 call assert_match('E486:', v:exception) 1346 endtry 1347 try 1348 set re=1 1349 /[\Ufffffc1f] 1350 catch 1351 call assert_match('E486:', v:exception) 1352 endtry 1353 set re& 1354endfunc 1355 1356func Test_one_error_msg() 1357 " This was also giving an internal error 1358 call assert_fails('call search(" \\((\\v[[=P=]]){185}+ ")', 'E871:') 1359endfunc 1360 1361func Test_incsearch_add_char_under_cursor() 1362 CheckOption incsearch 1363 1364 set incsearch 1365 new 1366 call setline(1, ['find match', 'anything']) 1367 1 1368 call test_override('char_avail', 1) 1369 call feedkeys("fc/m\<C-L>\<C-L>\<C-L>\<C-L>\<C-L>\<CR>", 'tx') 1370 call assert_equal('match', @/) 1371 call test_override('char_avail', 0) 1372 1373 set incsearch& 1374 bwipe! 1375endfunc 1376 1377" Test for the search() function with match at the cursor position 1378func Test_search_match_at_curpos() 1379 new 1380 call append(0, ['foobar', '', 'one two', '']) 1381 1382 normal gg 1383 1384 eval 'foobar'->search('c') 1385 call assert_equal([1, 1], [line('.'), col('.')]) 1386 1387 normal j 1388 call search('^$', 'c') 1389 call assert_equal([2, 1], [line('.'), col('.')]) 1390 1391 call search('^$', 'bc') 1392 call assert_equal([2, 1], [line('.'), col('.')]) 1393 1394 exe "normal /two\<CR>" 1395 call search('.', 'c') 1396 call assert_equal([3, 5], [line('.'), col('.')]) 1397 1398 close! 1399endfunc 1400 1401" Test for error cases with the search() function 1402func Test_search_errors() 1403 call assert_fails("call search('pat', [])", 'E730:') 1404 call assert_fails("call search('pat', 'b', {})", 'E728:') 1405 call assert_fails("call search('pat', 'b', 1, [])", 'E745:') 1406 call assert_fails("call search('pat', 'ns')", 'E475:') 1407 call assert_fails("call search('pat', 'mr')", 'E475:') 1408 1409 new 1410 call setline(1, ['foo', 'bar']) 1411 call assert_fails('call feedkeys("/foo/;/bar/;\<CR>", "tx")', 'E386:') 1412 bwipe! 1413endfunc 1414 1415func Test_search_display_pattern() 1416 new 1417 call setline(1, ['foo', 'bar', 'foobar']) 1418 1419 call cursor(1, 1) 1420 let @/ = 'foo' 1421 let pat = @/->escape('()*?'. '\s\+') 1422 let g:a = execute(':unsilent :norm! n') 1423 call assert_match(pat, g:a) 1424 1425 " right-left 1426 if exists("+rightleft") 1427 set rl 1428 call cursor(1, 1) 1429 let @/ = 'foo' 1430 let pat = 'oof/\s\+' 1431 let g:a = execute(':unsilent :norm! n') 1432 call assert_match(pat, g:a) 1433 set norl 1434 endif 1435endfunc 1436 1437func Test_searchdecl() 1438 let lines =<< trim END 1439 int global; 1440 1441 func() 1442 { 1443 int global; 1444 if (cond) { 1445 int local; 1446 } 1447 int local; 1448 // comment 1449 } 1450 END 1451 new 1452 call setline(1, lines) 1453 10 1454 call assert_equal(0, searchdecl('local', 0, 0)) 1455 call assert_equal(7, getcurpos()[1]) 1456 1457 10 1458 call assert_equal(0, 'local'->searchdecl(0, 1)) 1459 call assert_equal(9, getcurpos()[1]) 1460 1461 10 1462 call assert_equal(0, searchdecl('global')) 1463 call assert_equal(5, getcurpos()[1]) 1464 1465 10 1466 call assert_equal(0, searchdecl('global', 1)) 1467 call assert_equal(1, getcurpos()[1]) 1468 1469 bwipe! 1470endfunc 1471 1472func Test_search_special() 1473 " this was causing illegal memory access and an endless loop 1474 set t_PE= 1475 exe "norm /\x80PS" 1476endfunc 1477 1478" Test for command failures when the last search pattern is not set. 1479" Need to run this in a new vim instance where last search pattern is not set. 1480func Test_search_with_no_last_pat() 1481 let lines =<< trim [SCRIPT] 1482 call assert_fails("normal i\<C-R>/\e", 'E35:') 1483 call assert_fails("exe '/'", 'E35:') 1484 call assert_fails("exe '?'", 'E35:') 1485 call assert_fails("/", 'E35:') 1486 call assert_fails("?", 'E35:') 1487 call assert_fails("normal n", 'E35:') 1488 call assert_fails("normal N", 'E35:') 1489 call assert_fails("normal gn", 'E35:') 1490 call assert_fails("normal gN", 'E35:') 1491 call assert_fails("normal cgn", 'E35:') 1492 call assert_fails("normal cgN", 'E35:') 1493 let p = [] 1494 let p = @/ 1495 call assert_equal('', p) 1496 call assert_fails("normal :\<C-R>/", 'E35:') 1497 call assert_fails("//p", 'E35:') 1498 call assert_fails(";//p", 'E35:') 1499 call assert_fails("??p", 'E35:') 1500 call assert_fails(";??p", 'E35:') 1501 call assert_fails('g//p', 'E476:') 1502 call assert_fails('v//p', 'E476:') 1503 call writefile(v:errors, 'Xresult') 1504 qall! 1505 [SCRIPT] 1506 call writefile(lines, 'Xscript') 1507 1508 if RunVim([], [], '--clean -S Xscript') 1509 call assert_equal([], readfile('Xresult')) 1510 endif 1511 call delete('Xscript') 1512 call delete('Xresult') 1513endfunc 1514 1515" Test for using tilde (~) atom in search. This should use the last used 1516" substitute pattern 1517func Test_search_tilde_pat() 1518 let lines =<< trim [SCRIPT] 1519 set regexpengine=1 1520 call assert_fails('exe "normal /~\<CR>"', 'E33:') 1521 call assert_fails('exe "normal ?~\<CR>"', 'E33:') 1522 set regexpengine=2 1523 call assert_fails('exe "normal /~\<CR>"', 'E383:') 1524 call assert_fails('exe "normal ?~\<CR>"', 'E383:') 1525 set regexpengine& 1526 call writefile(v:errors, 'Xresult') 1527 qall! 1528 [SCRIPT] 1529 call writefile(lines, 'Xscript') 1530 if RunVim([], [], '--clean -S Xscript') 1531 call assert_equal([], readfile('Xresult')) 1532 endif 1533 call delete('Xscript') 1534 call delete('Xresult') 1535endfunc 1536 1537" Test for searching a pattern that is not present with 'nowrapscan' 1538func Test_search_pat_not_found() 1539 new 1540 set nowrapscan 1541 let @/ = '1abcxyz2' 1542 call assert_fails('normal n', 'E385:') 1543 call assert_fails('normal N', 'E384:') 1544 set wrapscan& 1545 close 1546endfunc 1547 1548" Test for v:searchforward variable 1549func Test_searchforward_var() 1550 new 1551 call setline(1, ['foo', '', 'foo']) 1552 call cursor(2, 1) 1553 let @/ = 'foo' 1554 let v:searchforward = 0 1555 normal N 1556 call assert_equal(3, line('.')) 1557 call cursor(2, 1) 1558 let v:searchforward = 1 1559 normal N 1560 call assert_equal(1, line('.')) 1561 close! 1562endfunc 1563 1564" Test for invalid regular expressions 1565func Test_invalid_regexp() 1566 set regexpengine=1 1567 call assert_fails("call search(repeat('\\(.\\)', 10))", 'E51:') 1568 call assert_fails("call search('\\%(')", 'E53:') 1569 call assert_fails("call search('\\(')", 'E54:') 1570 call assert_fails("call search('\\)')", 'E55:') 1571 call assert_fails("call search('x\\@#')", 'E59:') 1572 call assert_fails('call search(''\v%(%(%(%(%(%(%(%(%(%(%(a){1}){1}){1}){1}){1}){1}){1}){1}){1}){1}){1}'')', 'E60:') 1573 call assert_fails("call search('a\\+*')", 'E61:') 1574 call assert_fails("call search('\\_m')", 'E63:') 1575 call assert_fails("call search('\\+')", 'E64:') 1576 call assert_fails("call search('\\1')", 'E65:') 1577 call assert_fails("call search('\\z\\(\\)')", 'E66:') 1578 call assert_fails("call search('\\z2')", 'E67:') 1579 call assert_fails("call search('\\zx')", 'E68:') 1580 call assert_fails("call search('\\%[ab')", 'E69:') 1581 call assert_fails("call search('\\%[]')", 'E70:') 1582 call assert_fails("call search('\\%a')", 'E71:') 1583 call assert_fails("call search('ab\\%[\\(cd\\)]')", 'E369:') 1584 call assert_fails("call search('ab\\%[\\%(cd\\)]')", 'E369:') 1585 set regexpengine=2 1586 call assert_fails("call search('\\_')", 'E865:') 1587 call assert_fails("call search('\\+')", 'E866:') 1588 call assert_fails("call search('\\zx')", 'E867:') 1589 call assert_fails("call search('\\%a')", 'E867:') 1590 call assert_fails("call search('x\\@#')", 'E869:') 1591 call assert_fails("call search(repeat('\\(.\\)', 10))", 'E872:') 1592 call assert_fails("call search('\\_m')", 'E877:') 1593 call assert_fails("call search('\\%(')", 'E53:') 1594 call assert_fails("call search('\\(')", 'E54:') 1595 call assert_fails("call search('\\)')", 'E55:') 1596 call assert_fails("call search('\\z\\(\\)')", 'E66:') 1597 call assert_fails("call search('\\%[ab')", 'E69:') 1598 call assert_fails("call search('\\%[]')", 'E70:') 1599 call assert_fails("call search('\\%9999999999999999999999999999v')", 'E951:') 1600 set regexpengine& 1601 call assert_fails("call search('\\%#=3ab')", 'E864:') 1602endfunc 1603 1604" Test for searching a very complex pattern in a string. Should switch the 1605" regexp engine from NFA to the old engine. 1606func Test_regexp_switch_engine() 1607 let l = readfile('samples/re.freeze.txt') 1608 let v = substitute(l[4], '..\@<!', '', '') 1609 call assert_equal(l[4], v) 1610endfunc 1611 1612" Test for the \%V atom to search within visually selected text 1613func Test_search_in_visual_area() 1614 new 1615 call setline(1, ['foo bar1', 'foo bar2', 'foo bar3', 'foo bar4']) 1616 exe "normal 2GVjo/\\%Vbar\<CR>\<Esc>" 1617 call assert_equal([2, 5], [line('.'), col('.')]) 1618 exe "normal 2GVj$?\\%Vbar\<CR>\<Esc>" 1619 call assert_equal([3, 5], [line('.'), col('.')]) 1620 close! 1621endfunc 1622 1623" Test for searching with 'smartcase' and 'ignorecase' 1624func Test_search_smartcase() 1625 new 1626 call setline(1, ['', 'Hello']) 1627 set noignorecase nosmartcase 1628 call assert_fails('exe "normal /\\a\\_.\\(.*\\)O\<CR>"', 'E486:') 1629 1630 set ignorecase nosmartcase 1631 exe "normal /\\a\\_.\\(.*\\)O\<CR>" 1632 call assert_equal([2, 1], [line('.'), col('.')]) 1633 1634 call cursor(1, 1) 1635 set ignorecase smartcase 1636 call assert_fails('exe "normal /\\a\\_.\\(.*\\)O\<CR>"', 'E486:') 1637 1638 exe "normal /\\a\\_.\\(.*\\)o\<CR>" 1639 call assert_equal([2, 1], [line('.'), col('.')]) 1640 1641 " Test for using special atoms with 'smartcase' 1642 call setline(1, ['', ' Hello\ ']) 1643 call cursor(1, 1) 1644 call feedkeys('/\_.\%(\uello\)\' .. "\<CR>", 'xt') 1645 call assert_equal([2, 4], [line('.'), col('.')]) 1646 1647 set ignorecase& smartcase& 1648 close! 1649endfunc 1650 1651" Test searching past the end of a file 1652func Test_search_past_eof() 1653 new 1654 call setline(1, ['Line']) 1655 exe "normal /\\n\\zs\<CR>" 1656 call assert_equal([1, 4], [line('.'), col('.')]) 1657 close! 1658endfunc 1659 1660" Test for various search offsets 1661func Test_search_offset() 1662 " With /e, for a match in the first column of a line, the cursor should be 1663 " placed at the end of the previous line. 1664 new 1665 call setline(1, ['one two', 'three four']) 1666 call search('two\_.', 'e') 1667 call assert_equal([1, 7], [line('.'), col('.')]) 1668 1669 " with cursor at the beginning of the file, use /s+1 1670 call cursor(1, 1) 1671 exe "normal /two/s+1\<CR>" 1672 call assert_equal([1, 6], [line('.'), col('.')]) 1673 1674 " with cursor at the end of the file, use /e-1 1675 call cursor(2, 10) 1676 exe "normal ?three?e-1\<CR>" 1677 call assert_equal([2, 4], [line('.'), col('.')]) 1678 1679 " line offset - after the last line 1680 call cursor(1, 1) 1681 exe "normal /three/+1\<CR>" 1682 call assert_equal([2, 1], [line('.'), col('.')]) 1683 1684 " line offset - before the first line 1685 call cursor(2, 1) 1686 exe "normal ?one?-1\<CR>" 1687 call assert_equal([1, 1], [line('.'), col('.')]) 1688 1689 " character offset - before the first character in the file 1690 call cursor(2, 1) 1691 exe "normal ?one?s-1\<CR>" 1692 call assert_equal([1, 1], [line('.'), col('.')]) 1693 call cursor(2, 1) 1694 exe "normal ?one?e-3\<CR>" 1695 call assert_equal([1, 1], [line('.'), col('.')]) 1696 1697 " character offset - after the last character in the file 1698 call cursor(1, 1) 1699 exe "normal /four/s+4\<CR>" 1700 call assert_equal([2, 10], [line('.'), col('.')]) 1701 call cursor(1, 1) 1702 exe "normal /four/e+1\<CR>" 1703 call assert_equal([2, 10], [line('.'), col('.')]) 1704 1705 close! 1706endfunc 1707 1708" Test for searching for matching parenthesis using % 1709func Test_search_match_paren() 1710 new 1711 call setline(1, "abc(def')'ghi'('jk'\\t'lm)no") 1712 " searching for a matching parenthesis should skip single quoted characters 1713 call cursor(1, 4) 1714 normal % 1715 call assert_equal([1, 25], [line('.'), col('.')]) 1716 normal % 1717 call assert_equal([1, 4], [line('.'), col('.')]) 1718 call cursor(1, 5) 1719 normal ]) 1720 call assert_equal([1, 25], [line('.'), col('.')]) 1721 call cursor(1, 24) 1722 normal [( 1723 call assert_equal([1, 4], [line('.'), col('.')]) 1724 1725 " matching parenthesis in 'virtualedit' mode with cursor after the eol 1726 call setline(1, 'abc(defgh)') 1727 set virtualedit=all 1728 normal 20|% 1729 call assert_equal(4, col('.')) 1730 set virtualedit& 1731 close! 1732endfunc 1733 1734" Test for searching a pattern and stopping before a specified line 1735func Test_search_stopline() 1736 new 1737 call setline(1, ['', '', '', 'vim']) 1738 call assert_equal(0, search('vim', 'n', 3)) 1739 call assert_equal(4, search('vim', 'n', 4)) 1740 call setline(1, ['vim', '', '', '']) 1741 call cursor(4, 1) 1742 call assert_equal(0, search('vim', 'bn', 2)) 1743 call assert_equal(1, search('vim', 'bn', 1)) 1744 close! 1745endfunc 1746 1747" vim: shiftwidth=2 sts=2 expandtab 1748