robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local clear =n . clear 7local command =n . command 8local eq =t . eq 9local eval =n . eval 10local expect =n . expect 11local feed =n . feed 12local insert =n . insert 13local fn =n . fn 14local api =n . api 15local matches =t . matches 16local ok =t . ok 17local retry =t . retry 18local source =n . source 19local poke_eventloop =n . poke_eventloop 20local sleep =vim . uv . sleep 21local testprg =n . testprg 22local assert_alive =n . assert_alive 23 24local default_text =[[ 25Inc substitution on 26two lines 27]] 28 29local multiline_text =[[ 301 2 3 31A B C 324 5 6 33X Y Z 347 8 9 35]] 36 37local multimatch_text =[[ 38a bdc eae a fgl lzia r 39x 40]] 41 42local multibyte_text =[[ 43£ ¥ ѫѫ PEPPERS 44£ ¥ ѫfѫ 45a£ ѫ¥KOL 46£ ¥ libm 47£ ¥ 48]] 49 50local long_multiline_text =[[ 511 2 3 52A B C 534 5 6 54X Y Z 557 8 9 56K L M 57a b c 58d e f 59q r s 60x y z 61£ m n 62t œ ¥ 63]] 64 65local function common_setup ( screen , inccommand , text ) 66if screen then 67command ( 'syntax on' ) 68command ( 'set nohlsearch' ) 69command ( 'hi Substitute guifg=red guibg=yellow' ) 70 71screen : add_extra_attr_ids { 72[ 100 ] ={ underline =true } , 73[ 101 ] ={ underline =true , foreground =Screen . colors . SlateBlue , bold =true } , 74vis ={ background =Screen . colors . LightGrey } , 75} 76end 77 78command ( 'set inccommand=' .. ( inccommand or '' )) 79 80if text then 81insert ( text ) 82end 83end 84 85describe ( ':substitute, inccommand=split interactivity' , function () 86before_each ( function () 87clear () 88common_setup ( nil , 'split' , default_text ) 89end ) 90 91-- Test the tests: verify that the `1==bufnr('$')` assertion 92-- in the "no preview" tests (below) actually means something. 93it ( 'previews interactive cmdline' , function () 94feed ( ':%s/tw/MO/g' ) 95retry ( nil , 1000 , function () 96eq ( 2 , eval ( "bufnr('$')" )) 97end ) 98end ) 99 100it ( 'no preview if invoked by a script' , function () 101source ( '%s/tw/MO/g' ) 102poke_eventloop () 103eq ( 1 , eval ( "bufnr('$')" )) 104-- sanity check: assert the buffer state 105expect ( default_text : gsub ( 'tw' , 'MO' )) 106end ) 107 108it ( 'no preview if invoked by feedkeys()' , function () 109-- in a script... 110source ( [[:call feedkeys(":%s/tw/MO/g\<CR>")]] ) 111-- or interactively... 112feed ( [[:call feedkeys(":%s/bs/BUU/g\<lt>CR>")<CR>]] ) 113eq ( 1 , eval ( "bufnr('$')" )) 114-- sanity check: assert the buffer state 115expect ( default_text : gsub ( 'tw' , 'MO' ): gsub ( 'bs' , 'BUU' )) 116end ) 117end ) 118 119describe ( ":substitute, 'inccommand' preserves" , function () 120before_each ( clear ) 121 122it ( 'listed buffers (:ls)' , function () 123local screen =Screen . new ( 30 , 10 ) 124common_setup ( screen , 'split' , 'ABC' ) 125 126feed ( ':%s/AB/BA/' ) 127poke_eventloop () 128feed ( '<CR>' ) 129feed ( ':ls<CR>' ) 130 131screen : expect ( [[ 132BAC | 133{1:~ }|*3 134{3: }| 135:ls | 1361 %a + "[No Name]" | 137line 1 | 138{6:Press ENTER or type command to}| 139{6: continue}^ | 140]] ) 141end ) 142 143it ( "'[ and '] marks #26439" , function () 144local screen =Screen . new ( 30 , 10 ) 145common_setup ( screen , 'nosplit' , ( 'abc\ndef\n' ): rep ( 50 )) 146 147feed ( 'ggyG' ) 148local X =api . nvim_get_vvar ( 'maxcol' ) 149eq ( { 0 , 1 , 1 , 0 } , fn . getpos ( "'[" )) 150eq ( { 0 , 101 , X , 0 } , fn . getpos ( "']" )) 151 152feed ( ":'[,']s/def/" ) 153poke_eventloop () 154eq ( { 0 , 1 , 1 , 0 } , fn . getpos ( "'[" )) 155eq ( { 0 , 101 , X , 0 } , fn . getpos ( "']" )) 156 157feed ( 'DEF/g' ) 158poke_eventloop () 159eq ( { 0 , 1 , 1 , 0 } , fn . getpos ( "'[" )) 160eq ( { 0 , 101 , X , 0 } , fn . getpos ( "']" )) 161 162feed ( '<CR>' ) 163expect (( 'abc\nDEF\n' ): rep ( 50 )) 164end ) 165 166for _ , case in pairs { '' , 'split' , 'nosplit' } do 167it ( 'various delimiters (inccommand=' .. case .. ')' , function () 168insert ( default_text ) 169command ( 'set inccommand=' .. case ) 170 171local delims ={ '/' , '#' , ';' , '%' , ',' , '@' , '!' } 172for _ , delim in pairs ( delims ) do 173feed ( ':%s' .. delim .. 'lines' .. delim .. 'LINES' .. delim .. 'g' ) 174poke_eventloop () 175feed ( '<CR>' ) 176expect ( [[ 177Inc substitution on 178two LINES 179]] ) 180command ( 'undo' ) 181end 182end ) 183end 184 185for _ , case in pairs { '' , 'split' , 'nosplit' } do 186it ( "'undolevels' (inccommand=" .. case .. ')' , function () 187command ( 'set undolevels=139' ) 188command ( 'setlocal undolevels=34' ) 189command ( 'split' ) -- Show the buffer in multiple windows 190command ( 'set inccommand=' .. case ) 191insert ( 'as' ) 192feed ( ':%s/as/glork/' ) 193poke_eventloop () 194feed ( '<enter>' ) 195eq ( 139 , api . nvim_get_option_value ( 'undolevels' , { scope ='global' } )) 196eq ( 34 , api . nvim_get_option_value ( 'undolevels' , { buf =0 } )) 197end ) 198end 199 200for _ , case in ipairs ( { '' , 'split' , 'nosplit' } ) do 201it ( 'empty undotree() (inccommand=' .. case .. ')' , function () 202command ( 'set undolevels=1000' ) 203command ( 'set inccommand=' .. case ) 204local expected_undotree =eval ( 'undotree()' ) 205 206-- Start typing an incomplete :substitute command. 207feed ( [[:%s/e/YYYY/g]] ) 208poke_eventloop () 209-- Cancel the :substitute. 210feed ( [[<C-\><C-N>]] ) 211 212-- The undo tree should be unchanged. 213eq ( expected_undotree , eval ( 'undotree()' )) 214eq ( {} , eval ( 'undotree()' )[ 'entries' ]) 215end ) 216end 217 218for _ , case in ipairs ( { '' , 'split' , 'nosplit' } ) do 219it ( 'undotree() with branches (inccommand=' .. case .. ')' , function () 220command ( 'set undolevels=1000' ) 221command ( 'set inccommand=' .. case ) 222-- Make some changes. 223feed ( [[isome text 1<C-\><C-N>]] ) 224feed ( [[osome text 2<C-\><C-N>]] ) 225-- Add an undo branch. 226feed ( [[u]] ) 227-- More changes, more undo branches. 228feed ( [[osome text 3<C-\><C-N>]] ) 229feed ( [[AX<C-\><C-N>]] ) 230feed ( [[...]] ) 231feed ( [[uu]] ) 232feed ( [[osome text 4<C-\><C-N>]] ) 233feed ( [[u<C-R>u]] ) 234feed ( [[osome text 5<C-\><C-N>]] ) 235expect ( [[ 236some text 1 237some text 3XX 238some text 5]] ) 239local expected_undotree =eval ( 'undotree()' ) 240eq ( 5 , # expected_undotree [ 'entries' ]) -- sanity 241 242-- Start typing an incomplete :substitute command. 243feed ( [[:%s/e/YYYY/g]] ) 244poke_eventloop () 245-- Cancel the :substitute. 246feed ( [[<C-\><C-N>]] ) 247 248-- The undo tree should be unchanged. 249eq ( expected_undotree , eval ( 'undotree()' )) 250end ) 251end 252 253for _ , case in pairs { '' , 'split' , 'nosplit' } do 254it ( 'b:changedtick (inccommand=' .. case .. ')' , function () 255command ( 'set inccommand=' .. case ) 256feed ( [[isome text 1<C-\><C-N>]] ) 257feed ( [[osome text 2<C-\><C-N>]] ) 258local expected_tick =eval ( 'b:changedtick' ) 259ok ( expected_tick > 0 ) 260 261expect ( [[ 262some text 1 263some text 2]] ) 264feed ( ':%s/e/XXX/' ) 265poke_eventloop () 266 267eq ( expected_tick , eval ( 'b:changedtick' )) 268end ) 269end 270 271for _ , case in ipairs ( { '' , 'split' , 'nosplit' } ) do 272it ( 'previous substitute string ~ (inccommand=' .. case .. ') #12109' , function () 273local screen =Screen . new ( 30 , 10 ) 274common_setup ( screen , case , default_text ) 275 276feed ( ':%s/Inc/SUB<CR>' ) 277expect ( [[ 278SUB substitution on 279two lines 280]] ) 281 282feed ( ':%s/line/' ) 283poke_eventloop () 284feed ( '~' ) 285poke_eventloop () 286feed ( '<CR>' ) 287expect ( [[ 288SUB substitution on 289two SUBs 290]] ) 291 292feed ( ':%s/sti/' ) 293poke_eventloop () 294feed ( '~' ) 295poke_eventloop () 296feed ( 'B' ) 297poke_eventloop () 298feed ( '<CR>' ) 299expect ( [[ 300SUB subSUBBtution on 301two SUBs 302]] ) 303 304feed ( ':%s/ion/NEW<CR>' ) 305expect ( [[ 306SUB subSUBBtutNEW on 307two SUBs 308]] ) 309 310feed ( ':%s/two/' ) 311poke_eventloop () 312feed ( 'N' ) 313poke_eventloop () 314feed ( '~' ) 315poke_eventloop () 316feed ( '<CR>' ) 317expect ( [[ 318SUB subSUBBtutNEW on 319NNEW SUBs 320]] ) 321 322feed ( ':%s/bS/' ) 323poke_eventloop () 324feed ( '~' ) 325poke_eventloop () 326feed ( 'W' ) 327poke_eventloop () 328feed ( '<CR>' ) 329expect ( [[ 330SUB suNNEWWUBBtutNEW on 331NNEW SUBs 332]] ) 333end ) 334end 335end ) 336 337describe ( ":substitute, 'inccommand' preserves undo" , function () 338local cases ={ '' , 'split' , 'nosplit' } 339 340local substrings ={ 341{ ':%s/' , '1' } , 342{ ':%s/' , '1' , '/' } , 343{ ':%s/' , '1' , '/' , '<bs>' } , 344{ ':%s/' , '1' , '/' , 'a' } , 345{ ':%s/' , '1' , '/' , 'a' , '<bs>' } , 346{ ':%s/' , '1' , '/' , 'a' , 'x' } , 347{ ':%s/' , '1' , '/' , 'a' , 'x' , '<bs>' } , 348{ ':%s/' , '1' , '/' , 'a' , 'x' , '<bs>' , '<bs>' } , 349{ ':%s/' , '1' , '/' , 'a' , 'x' , '<bs>' , '<bs>' , '<bs>' } , 350{ ':%s/' , '1' , '/' , 'a' , 'x' , '/' } , 351{ ':%s/' , '1' , '/' , 'a' , 'x' , '/' , '<bs>' } , 352{ ':%s/' , '1' , '/' , 'a' , 'x' , '/' , '<bs>' , '/' } , 353{ ':%s/' , '1' , '/' , 'a' , 'x' , '/' , 'g' } , 354{ ':%s/' , '1' , '/' , 'a' , 'x' , '/' , 'g' , '<bs>' } , 355{ ':%s/' , '1' , '/' , 'a' , 'x' , '/' , 'g' , '<bs>' , '<bs>' } , 356} 357 358local function test_sub ( substring , split , redoable ) 359command ( 'bwipe!' ) 360command ( 'set inccommand=' .. split ) 361 362insert ( '1' ) 363feed ( 'o2<esc>' ) 364command ( 'undo' ) 365feed ( 'o3<esc>' ) 366if redoable then 367feed ( 'o4<esc>' ) 368command ( 'undo' ) 369end 370for _ , s in pairs ( substring ) do 371feed ( s ) 372end 373poke_eventloop () 374feed ( '<enter>' ) 375command ( 'undo' ) 376 377feed ( 'g-' ) 378expect ( [[ 3791 3802]] ) 381 382feed ( 'g+' ) 383expect ( [[ 3841 3853]] ) 386end 387 388local function test_notsub ( substring , split , redoable ) 389command ( 'bwipe!' ) 390command ( 'set inccommand=' .. split ) 391 392insert ( '1' ) 393feed ( 'o2<esc>' ) 394command ( 'undo' ) 395feed ( 'o3<esc>' ) 396if redoable then 397feed ( 'o4<esc>' ) 398command ( 'undo' ) 399end 400for _ , s in pairs ( substring ) do 401feed ( s ) 402end 403poke_eventloop () 404feed ( '<esc>' ) 405 406feed ( 'g-' ) 407expect ( [[ 4081 4092]] ) 410 411feed ( 'g+' ) 412expect ( [[ 4131 4143]] ) 415 416if redoable then 417feed ( '<c-r>' ) 418expect ( [[ 4191 4203 4214]] ) 422end 423end 424 425local function test_threetree ( substring , split ) 426command ( 'bwipe!' ) 427command ( 'set inccommand=' .. split ) 428 429insert ( '1' ) 430feed ( 'o2<esc>' ) 431feed ( 'o3<esc>' ) 432feed ( 'uu' ) 433feed ( 'oa<esc>' ) 434feed ( 'ob<esc>' ) 435feed ( 'uu' ) 436feed ( 'oA<esc>' ) 437feed ( 'oB<esc>' ) 438 439-- This is the undo tree (x-Axis is timeline), we're at B now 440-- ----------------A - B 441-- / 442-- | --------a - b 443-- |/ 444-- 1 - 2 - 3 445 446feed ( '2u' ) 447for _ , s in pairs ( substring ) do 448feed ( s ) 449poke_eventloop () 450end 451feed ( '<esc>' ) 452expect ( [[ 4531]] ) 454feed ( 'g-' ) 455expect ( [[ 456]] ) 457feed ( 'g+' ) 458expect ( [[ 4591]] ) 460feed ( '<c-r>' ) 461expect ( [[ 4621 463A]] ) 464 465feed ( 'g-' ) -- go to b 466feed ( '2u' ) 467for _ , s in pairs ( substring ) do 468feed ( s ) 469poke_eventloop () 470end 471feed ( '<esc>' ) 472feed ( '<c-r>' ) 473expect ( [[ 4741 475a]] ) 476 477feed ( 'g-' ) -- go to 3 478feed ( '2u' ) 479for _ , s in pairs ( substring ) do 480feed ( s ) 481poke_eventloop () 482end 483feed ( '<esc>' ) 484feed ( '<c-r>' ) 485expect ( [[ 4861 4872]] ) 488end 489 490before_each ( clear ) 491 492it ( 'at a non-leaf of the undo tree' , function () 493for _ , case in pairs ( cases ) do 494for _ , str in pairs ( substrings ) do 495for _ , redoable in pairs ( { true } ) do 496test_sub ( str , case , redoable ) 497end 498end 499end 500end ) 501 502it ( 'at a leaf of the undo tree' , function () 503for _ , case in pairs ( cases ) do 504for _ , str in pairs ( substrings ) do 505for _ , redoable in pairs ( { false } ) do 506test_sub ( str , case , redoable ) 507end 508end 509end 510end ) 511 512it ( 'when interrupting substitution' , function () 513for _ , case in pairs ( cases ) do 514for _ , str in pairs ( substrings ) do 515for _ , redoable in pairs ( { true , false } ) do 516test_notsub ( str , case , redoable ) 517end 518end 519end 520end ) 521 522it ( 'in a complex undo scenario' , function () 523for _ , case in pairs ( cases ) do 524for _ , str in pairs ( substrings ) do 525test_threetree ( str , case ) 526end 527end 528end ) 529 530it ( 'with undolevels=0' , function () 531for _ , case in pairs ( cases ) do 532clear () 533common_setup ( nil , case , default_text ) 534command ( 'set undolevels=0' ) 535 536feed ( '1G0' ) 537insert ( 'X' ) 538feed ( ':%s/tw/MO/' ) 539poke_eventloop () 540feed ( '<esc>' ) 541command ( 'undo' ) 542expect ( default_text ) 543command ( 'undo' ) 544expect ( default_text : gsub ( 'Inc' , 'XInc' )) 545command ( 'undo' ) 546 547feed ( ':%s/tw/MO/g' ) 548poke_eventloop () 549feed ( '<CR>' ) 550expect ( default_text : gsub ( 'tw' , 'MO' )) 551command ( 'undo' ) 552expect ( default_text ) 553command ( 'undo' ) 554expect ( default_text : gsub ( 'tw' , 'MO' )) 555end 556end ) 557 558it ( 'with undolevels=1' , function () 559for _ , case in pairs ( cases ) do 560clear () 561local screen =Screen . new ( 20 , 10 ) 562common_setup ( screen , case , default_text ) 563screen : expect ( [[ 564Inc substitution on | 565two lines | 566^ | 567{1:~ }|*6 568| 569]] ) 570command ( 'set undolevels=1' ) 571 572feed ( '1G0' ) 573insert ( 'X' ) 574feed ( 'IY<esc>' ) 575feed ( ':%s/tw/MO/' ) 576poke_eventloop () 577feed ( '<esc>' ) 578feed ( 'u' ) 579expect ( default_text : gsub ( 'Inc' , 'XInc' )) 580feed ( 'u' ) 581expect ( default_text ) 582 583feed ( ':%s/tw/MO/g' ) 584poke_eventloop () 585feed ( '<enter>' ) 586feed ( ':%s/MO/GO/g' ) 587poke_eventloop () 588feed ( '<enter>' ) 589feed ( ':%s/GO/NO/g' ) 590poke_eventloop () 591feed ( '<enter>' ) 592feed ( 'u' ) 593expect ( default_text : gsub ( 'tw' , 'GO' )) 594feed ( 'u' ) 595expect ( default_text : gsub ( 'tw' , 'MO' )) 596feed ( 'u' ) 597 598if case == 'split' then 599screen : expect ( [[ 600Inc substitution on | 601^MOo lines | 602| 603{1:~ }|*6 604Already ...t change | 605]] ) 606else 607screen : expect ( [[ 608Inc substitution on | 609^MOo lines | 610| 611{1:~ }|*6 612Already ...t change | 613]] ) 614end 615end 616end ) 617 618it ( 'with undolevels=2' , function () 619for _ , case in pairs ( cases ) do 620clear () 621local screen =Screen . new ( 20 , 10 ) 622common_setup ( screen , case , default_text ) 623command ( 'set undolevels=2' ) 624 625feed ( '2GAx<esc>' ) 626feed ( 'Ay<esc>' ) 627feed ( 'Az<esc>' ) 628feed ( ':%s/tw/AR' ) 629poke_eventloop () 630feed ( '<esc>' ) 631feed ( 'u' ) 632expect ( default_text : gsub ( 'lines' , 'linesxy' )) 633feed ( 'u' ) 634expect ( default_text : gsub ( 'lines' , 'linesx' )) 635feed ( 'u' ) 636expect ( default_text ) 637feed ( 'u' ) 638 639if case == 'split' then 640screen : expect ( [[ 641Inc substitution on | 642^two lines | 643| 644{1:~ }|*6 645Already ...t change | 646]] ) 647else 648screen : expect ( [[ 649Inc substitution on | 650^two lines | 651| 652{1:~ }|*6 653Already ...t change | 654]] ) 655end 656 657feed ( ':%s/tw/MO/g' ) 658poke_eventloop () 659feed ( '<enter>' ) 660feed ( ':%s/MO/GO/g' ) 661poke_eventloop () 662feed ( '<enter>' ) 663feed ( ':%s/GO/NO/g' ) 664poke_eventloop () 665feed ( '<enter>' ) 666feed ( ':%s/NO/LO/g' ) 667poke_eventloop () 668feed ( '<enter>' ) 669feed ( 'u' ) 670expect ( default_text : gsub ( 'tw' , 'NO' )) 671feed ( 'u' ) 672expect ( default_text : gsub ( 'tw' , 'GO' )) 673feed ( 'u' ) 674expect ( default_text : gsub ( 'tw' , 'MO' )) 675feed ( 'u' ) 676 677if case == 'split' then 678screen : expect ( [[ 679Inc substitution on | 680^MOo lines | 681| 682{1:~ }|*6 683Already ...t change | 684]] ) 685else 686screen : expect ( [[ 687Inc substitution on | 688^MOo lines | 689| 690{1:~ }|*6 691Already ...t change | 692]] ) 693end 694end 695end ) 696 697it ( 'with undolevels=-1' , function () 698for _ , case in pairs ( cases ) do 699clear () 700local screen =Screen . new ( 20 , 10 ) 701common_setup ( screen , case , default_text ) 702 703command ( 'set undolevels=-1' ) 704feed ( ':%s/tw/MO/g' ) 705poke_eventloop () 706feed ( '<enter>' ) 707feed ( 'u' ) 708if case == 'split' then 709screen : expect ( [[ 710Inc substitution on | 711^MOo lines | 712| 713{1:~ }|*6 714Already ...t change | 715]] ) 716else 717screen : expect ( [[ 718Inc substitution on | 719^MOo lines | 720| 721{1:~ }|*6 722Already ...t change | 723]] ) 724end 725 726-- repeat with an interrupted substitution 727clear () 728screen =Screen . new ( 20 , 10 ) 729common_setup ( screen , case , default_text ) 730 731command ( 'set undolevels=-1' ) 732feed ( '1G' ) 733feed ( 'IL<esc>' ) 734feed ( ':%s/tw/MO/g' ) 735poke_eventloop () 736feed ( '<esc>' ) 737feed ( 'u' ) 738 739screen : expect ( [[ 740^LInc substitution on| 741two lines | 742| 743{1:~ }|*6 744Already ...t change | 745]] ) 746end 747end ) 748end ) 749 750describe ( ':substitute, inccommand=split' , function () 751local screen 752 753before_each ( function () 754clear () 755screen =Screen . new ( 30 , 15 ) 756common_setup ( screen , 'split' , default_text .. default_text ) 757end ) 758 759it ( "preserves 'modified' buffer flag" , function () 760command ( 'set nomodified' ) 761feed ( ':%s/tw' ) 762screen : expect ( [[ 763Inc substitution on | 764{20:tw}o lines | 765Inc substitution on | 766{20:tw}o lines | 767| 768{3:[No Name] }| 769|2| {20:tw}o lines | 770|4| {20:tw}o lines | 771{1:~ }|*5 772{2:[Preview] }| 773:%s/tw^ | 774]] ) 775feed ( [[<C-\><C-N>]] ) -- Cancel the :substitute command. 776eq ( 0 , eval ( '&modified' )) 777end ) 778 779it ( 'shows preview when cmd modifiers are present' , function () 780-- one modifier 781feed ( ':keeppatterns %s/tw/to' ) 782screen : expect { any =[[{20:to}o lines]] } 783feed ( '<Esc>' ) 784screen : expect { any =[[two lines]] } 785 786-- multiple modifiers 787feed ( ':keeppatterns silent %s/tw/to' ) 788screen : expect { any =[[{20:to}o lines]] } 789feed ( '<Esc>' ) 790screen : expect { any =[[two lines]] } 791 792-- non-modifier prefix 793feed ( ':silent tabedit %s/tw/to' ) 794screen : expect ( [[ 795Inc substitution on | 796two lines | 797Inc substitution on | 798two lines | 799| 800{1:~ }|*9 801:silent tabedit %s/tw/to^ | 802]] ) 803feed ( '<Esc>' ) 804 805-- leading colons 806feed ( ':::%s/tw/to' ) 807screen : expect { any =[[{20:to}o lines]] } 808feed ( '<Esc>' ) 809screen : expect { any =[[two lines]] } 810end ) 811 812it ( 'ignores new-window modifiers when splitting the preview window' , function () 813-- one modifier 814feed ( ':topleft %s/tw/to' ) 815screen : expect ( [[ 816Inc substitution on | 817{20:to}o lines | 818Inc substitution on | 819{20:to}o lines | 820| 821{3:[No Name] [+] }| 822|2| {20:to}o lines | 823|4| {20:to}o lines | 824{1:~ }|*5 825{2:[Preview] }| 826:topleft %s/tw/to^ | 827]] ) 828feed ( '<Esc>' ) 829screen : expect { any =[[two lines]] } 830 831-- multiple modifiers 832feed ( ':topleft vert %s/tw/to' ) 833screen : expect ( [[ 834Inc substitution on | 835{20:to}o lines | 836Inc substitution on | 837{20:to}o lines | 838| 839{3:[No Name] [+] }| 840|2| {20:to}o lines | 841|4| {20:to}o lines | 842{1:~ }|*5 843{2:[Preview] }| 844:topleft vert %s/tw/to^ | 845]] ) 846feed ( '<Esc>' ) 847screen : expect { any =[[two lines]] } 848end ) 849 850it ( 'shows split window when typing the pattern' , function () 851feed ( ':%s/tw' ) 852screen : expect ( [[ 853Inc substitution on | 854{20:tw}o lines | 855Inc substitution on | 856{20:tw}o lines | 857| 858{3:[No Name] [+] }| 859|2| {20:tw}o lines | 860|4| {20:tw}o lines | 861{1:~ }|*5 862{2:[Preview] }| 863:%s/tw^ | 864]] ) 865end ) 866 867it ( 'shows preview with empty replacement' , function () 868feed ( ':%s/tw/' ) 869screen : expect ( [[ 870Inc substitution on | 871o lines | 872Inc substitution on | 873o lines | 874| 875{3:[No Name] [+] }| 876|2| o lines | 877|4| o lines | 878{1:~ }|*5 879{2:[Preview] }| 880:%s/tw/^ | 881]] ) 882 883feed ( 'x' ) 884screen : expect ( [[ 885Inc substitution on | 886{20:x}o lines | 887Inc substitution on | 888{20:x}o lines | 889| 890{3:[No Name] [+] }| 891|2| {20:x}o lines | 892|4| {20:x}o lines | 893{1:~ }|*5 894{2:[Preview] }| 895:%s/tw/x^ | 896]] ) 897 898feed ( '<bs>' ) 899screen : expect ( [[ 900Inc substitution on | 901o lines | 902Inc substitution on | 903o lines | 904| 905{3:[No Name] [+] }| 906|2| o lines | 907|4| o lines | 908{1:~ }|*5 909{2:[Preview] }| 910:%s/tw/^ | 911]] ) 912end ) 913 914it ( 'shows split window when typing replacement' , function () 915feed ( ':%s/tw/XX' ) 916screen : expect ( [[ 917Inc substitution on | 918{20:XX}o lines | 919Inc substitution on | 920{20:XX}o lines | 921| 922{3:[No Name] [+] }| 923|2| {20:XX}o lines | 924|4| {20:XX}o lines | 925{1:~ }|*5 926{2:[Preview] }| 927:%s/tw/XX^ | 928]] ) 929end ) 930 931it ( 'does not show split window for :s/' , function () 932feed ( '2gg' ) 933feed ( ':s/tw' ) 934screen : expect ( [[ 935Inc substitution on | 936{20:tw}o lines | 937Inc substitution on | 938two lines | 939| 940{1:~ }|*9 941:s/tw^ | 942]] ) 943end ) 944 945it ( "'hlsearch' is active, 'cursorline' is not" , function () 946command ( 'set hlsearch cursorline' ) 947feed ( 'gg' ) 948 949-- Assert that 'cursorline' is active. 950screen : expect ( [[ 951{21:^Inc substitution on }| 952two lines | 953Inc substitution on | 954two lines | 955| 956{1:~ }|*9 957| 958]] ) 959 960feed ( ':%s/tw' ) 961-- 'cursorline' is NOT active during preview. 962screen : expect ( [[ 963Inc substitution on | 964{20:tw}o lines | 965Inc substitution on | 966{20:tw}o lines | 967| 968{3:[No Name] [+] }| 969|2| {20:tw}o lines | 970|4| {20:tw}o lines | 971{1:~ }|*5 972{2:[Preview] }| 973:%s/tw^ | 974]] ) 975end ) 976 977it ( 'highlights the replacement text' , function () 978feed ( 'ggO' ) 979feed ( 'M M M<esc>' ) 980feed ( ':%s/M/123/g' ) 981screen : expect ( [[ 982{20:123} {20:123} {20:123} | 983Inc substitution on | 984two lines | 985Inc substitution on | 986two lines | 987{3:[No Name] [+] }| 988|1| {20:123} {20:123} {20:123} | 989{1:~ }|*6 990{2:[Preview] }| 991:%s/M/123/g^ | 992]] ) 993end ) 994 995it ( "highlights nothing when there's no match" , function () 996feed ( 'gg' ) 997feed ( ':%s/Inx' ) 998screen : expect ( [[ 999Inc substitution on | 1000two lines | 1001Inc substitution on | 1002two lines | 1003| 1004{3:[No Name] [+] }| 1005| 1006{1:~ }|*6 1007{2:[Preview] }| 1008:%s/Inx^ | 1009]] ) 1010end ) 1011 1012it ( 'previews correctly when previewhight is small' , function () 1013command ( 'set cwh=3' ) 1014command ( 'set hls' ) 1015feed ( 'ggdG' ) 1016insert ( string . rep ( 'abc abc abc\n' , 20 )) 1017feed ( ':%s/abc/MMM/g' ) 1018screen : expect ( [[ 1019{20:MMM} {20:MMM} {20:MMM} |*9 1020{3:[No Name] [+] }| 1021| 1| {20:MMM} {20:MMM} {20:MMM} | 1022| 2| {20:MMM} {20:MMM} {20:MMM} | 1023| 3| {20:MMM} {20:MMM} {20:MMM} | 1024{2:[Preview] }| 1025:%s/abc/MMM/g^ | 1026]] ) 1027end ) 1028 1029it ( 'actually replaces text' , function () 1030feed ( ':%s/tw/XX/g' ) 1031poke_eventloop () 1032feed ( '<Enter>' ) 1033 1034screen : expect ( [[ 1035Inc substitution on | 1036XXo lines | 1037Inc substitution on | 1038^XXo lines | 1039| 1040{1:~ }|*9 1041:%s/tw/XX/g | 1042]] ) 1043end ) 1044 1045it ( 'shows correct line numbers with many lines' , function () 1046feed ( 'gg' ) 1047feed ( '2yy' ) 1048feed ( '2000p' ) 1049command ( '1,1000s/tw/BB/g' ) 1050 1051feed ( ':%s/tw/X' ) 1052screen : expect ( [[ 1053Inc substitution on | 1054BBo lines | 1055Inc substitution on | 1056{20:X}o lines | 1057Inc substitution on | 1058{3:[No Name] [+] }| 1059|1001| {20:X}o lines | 1060|1003| {20:X}o lines | 1061|1005| {20:X}o lines | 1062|1007| {20:X}o lines | 1063|1009| {20:X}o lines | 1064|1011| {20:X}o lines | 1065|1013| {20:X}o lines | 1066{2:[Preview] }| 1067:%s/tw/X^ | 1068]] ) 1069end ) 1070 1071it ( 'does not spam the buffer numbers' , function () 1072-- The preview buffer is re-used (unless user deleted it), so buffer numbers 1073-- will not increase on each keystroke. 1074feed ( ':%s/tw/Xo/g' ) 1075-- Delete and re-type the g a few times. 1076feed ( '<BS>' ) 1077poke_eventloop () 1078feed ( 'g' ) 1079poke_eventloop () 1080feed ( '<BS>' ) 1081poke_eventloop () 1082feed ( 'g' ) 1083poke_eventloop () 1084feed ( '<CR>' ) 1085poke_eventloop () 1086feed ( ':vs tmp<enter>' ) 1087eq ( 3 , fn . bufnr ( '$' )) 1088end ) 1089 1090it ( 'works with the n flag' , function () 1091feed ( ':%s/tw/Mix/n' ) 1092poke_eventloop () 1093feed ( '<Enter>' ) 1094screen : expect ( [[ 1095Inc substitution on | 1096two lines | 1097Inc substitution on | 1098two lines | 1099^ | 1100{1:~ }|*9 11012 matches on 2 lines | 1102]] ) 1103end ) 1104 1105it ( "deactivates if 'redrawtime' is exceeded #5602" , function () 1106-- prevent redraws from 'incsearch' 1107api . nvim_set_option_value ( 'incsearch' , false , {} ) 1108-- Assert that 'inccommand' is ENABLED initially. 1109eq ( 'split' , eval ( '&inccommand' )) 1110-- Set 'redrawtime' to minimal value, to ensure timeout is triggered. 1111command ( 'set redrawtime=1 nowrap' ) 1112-- Load a big file. 1113command ( 'silent edit! test/functional/fixtures/bigfile_oneline.txt' ) 1114-- Start :substitute with a slow pattern. 1115feed ( [[:%s/B.*N/x]] ) 1116poke_eventloop () 1117 1118-- Assert that 'inccommand' is DISABLED in cmdline mode. 1119eq ( '' , eval ( '&inccommand' )) 1120-- Assert that preview cleared (or never manifested). 1121screen : expect ( [[ 11220000;<control>;Cc;0;BN;;;;;N;N| 11232F923;CJK COMPATIBILITY IDEOGR| 11242F924;CJK COMPATIBILITY IDEOGR| 11252F925;CJK COMPATIBILITY IDEOGR| 11262F926;CJK COMPATIBILITY IDEOGR| 11272F927;CJK COMPATIBILITY IDEOGR| 11282F928;CJK COMPATIBILITY IDEOGR| 11292F929;CJK COMPATIBILITY IDEOGR| 11302F92A;CJK COMPATIBILITY IDEOGR| 11312F92B;CJK COMPATIBILITY IDEOGR| 11322F92C;CJK COMPATIBILITY IDEOGR| 11332F92D;CJK COMPATIBILITY IDEOGR| 11342F92E;CJK COMPATIBILITY IDEOGR| 11352F92F;CJK COMPATIBILITY IDEOGR| 1136:%s/B.*N/x^ | 1137]] ) 1138 1139-- Assert that 'inccommand' is again ENABLED after leaving cmdline mode. 1140feed ( [[<C-\><C-N>]] ) 1141eq ( 'split' , eval ( '&inccommand' )) 1142end ) 1143 1144it ( 'time limit is enforced while matching a single line #40773' , function () 1145-- prevent redraws from 'incsearch' 1146api . nvim_set_option_value ( 'incsearch' , false , {} ) 1147-- Assert that 'inccommand' is ENABLED initially. 1148eq ( 'split' , eval ( '&inccommand' )) 1149-- Set 'redrawtime' to minimal value, to ensure timeout is triggered. 1150command ( 'set redrawtime=1 nowrap' ) 1151-- Prepare the text 1152api . nvim_buf_set_lines ( 0 , 0 , - 1 , true , { ( 'aaaaaaaa/' ): rep ( 6 ) .. ( 'b' ): rep ( 200 ) } ) 1153feed ( [[:%s/.\+\/\(.\+\)\+ft\/\1]] ) 1154screen : expect ( { any =vim . pesc ( [[:%s/.\+\/\(.\+\)\+ft\/\1]] ) } ) 1155-- Assert that 'inccommand' is DISABLED in cmdline mode. 1156eq ( '' , eval ( '&inccommand' )) 1157-- Assert that 'inccommand' is again ENABLED after leaving cmdline mode. 1158feed ( [[<C-\><C-N>]] ) 1159eq ( 'split' , eval ( '&inccommand' )) 1160end ) 1161 1162it ( "deactivates if 'foldexpr' is slow #9557" , function () 1163insert ( [[ 1164a 1165a 1166a 1167a 1168a 1169a 1170a 1171a 1172]] ) 1173source ( [[ 1174function! Slowfold(lnum) 1175sleep 5m 1176return a:lnum % 3 1177endfun 1178]] ) 1179command ( 'set redrawtime=1 inccommand=split' ) 1180command ( 'set foldmethod=expr foldexpr=Slowfold(v:lnum)' ) 1181feed ( ':%s/a/bcdef' ) 1182 1183-- Assert that 'inccommand' is DISABLED in cmdline mode. 1184retry ( nil , nil , function () 1185eq ( '' , eval ( '&inccommand' )) 1186end ) 1187 1188-- Assert that 'inccommand' is again ENABLED after leaving cmdline mode. 1189feed ( [[<C-\><C-N>]] ) 1190retry ( nil , nil , function () 1191eq ( 'split' , eval ( '&inccommand' )) 1192end ) 1193end ) 1194 1195it ( 'clears preview if non-previewable command is edited #5585' , function () 1196feed ( 'gg' ) 1197-- Put a non-previewable command in history. 1198feed ( ":echo 'foo'<CR>" ) 1199-- Start an incomplete :substitute command. 1200feed ( ':1,2s/t/X' ) 1201 1202screen : expect ( [[ 1203Inc subs{20:X}itution on | 1204{20:X}wo lines | 1205Inc substitution on | 1206two lines | 1207| 1208{3:[No Name] [+] }| 1209|1| Inc subs{20:X}itution on | 1210|2| {20:X}wo lines | 1211{1:~ }|*5 1212{2:[Preview] }| 1213:1,2s/t/X^ | 1214]] ) 1215 1216-- Select the previous command. 1217feed ( '<C-P>' ) 1218-- Assert that preview was cleared. 1219screen : expect ( [[ 1220Inc substitution on | 1221two lines | 1222Inc substitution on | 1223two lines | 1224| 1225{1:~ }|*9 1226:echo 'foo'^ | 1227]] ) 1228end ) 1229 1230it ( [[preview changes correctly with c_CTRL-R_= and c_CTRL-\_e]] , function () 1231feed ( 'gg' ) 1232feed ( ':1,2s/t/X' ) 1233screen : expect ( [[ 1234Inc subs{20:X}itution on | 1235{20:X}wo lines | 1236Inc substitution on | 1237two lines | 1238| 1239{3:[No Name] [+] }| 1240|1| Inc subs{20:X}itution on | 1241|2| {20:X}wo lines | 1242{1:~ }|*5 1243{2:[Preview] }| 1244:1,2s/t/X^ | 1245]] ) 1246 1247feed ( [[<C-R>='Y']] ) 1248-- preview should be unchanged during c_CTRL-R_= editing 1249screen : expect ( [[ 1250Inc subs{20:X}itution on | 1251{20:X}wo lines | 1252Inc substitution on | 1253two lines | 1254| 1255{3:[No Name] [+] }| 1256|1| Inc subs{20:X}itution on | 1257|2| {20:X}wo lines | 1258{1:~ }|*5 1259{2:[Preview] }| 1260={26:'Y'}^ | 1261]] ) 1262 1263feed ( '<CR>' ) 1264-- preview should be changed by the result of the expression 1265screen : expect ( [[ 1266Inc subs{20:XY}itution on | 1267{20:XY}wo lines | 1268Inc substitution on | 1269two lines | 1270| 1271{3:[No Name] [+] }| 1272|1| Inc subs{20:XY}itution on | 1273|2| {20:XY}wo lines | 1274{1:~ }|*5 1275{2:[Preview] }| 1276:1,2s/t/XY^ | 1277]] ) 1278 1279feed ( [[<C-\>e'echo']] ) 1280-- preview should be unchanged during c_CTRL-\_e editing 1281screen : expect ( [[ 1282Inc subs{20:XY}itution on | 1283{20:XY}wo lines | 1284Inc substitution on | 1285two lines | 1286| 1287{3:[No Name] [+] }| 1288|1| Inc subs{20:XY}itution on | 1289|2| {20:XY}wo lines | 1290{1:~ }|*5 1291{2:[Preview] }| 1292={26:'echo'}^ | 1293]] ) 1294 1295feed ( '<CR>' ) 1296-- preview should be cleared if command is changed to a non-previewable one 1297screen : expect ( [[ 1298Inc substitution on | 1299two lines | 1300Inc substitution on | 1301two lines | 1302| 1303{1:~ }|*9 1304:echo^ | 1305]] ) 1306end ) 1307end ) 1308 1309describe ( 'inccommand=nosplit' , function () 1310local screen 1311 1312before_each ( function () 1313clear () 1314screen =Screen . new ( 20 , 10 ) 1315common_setup ( screen , 'nosplit' , default_text .. default_text ) 1316end ) 1317 1318it ( 'works with :smagic, :snomagic' , function () 1319command ( 'set hlsearch' ) 1320insert ( 'Line *.3.* here' ) 1321 1322feed ( ':%smagic/3.*/X' ) -- start :smagic command 1323screen : expect ( [[ 1324Inc substitution on | 1325two lines | 1326Inc substitution on | 1327two lines | 1328Line *.{20:X} | 1329{1:~ }|*4 1330:%smagic/3.*/X^ | 1331]] ) 1332 1333feed ( [[<C-\><C-N>]] ) -- cancel 1334feed ( ':%snomagic/3.*/X' ) -- start :snomagic command 1335screen : expect ( [[ 1336Inc substitution on | 1337two lines | 1338Inc substitution on | 1339two lines | 1340Line *.{20:X} here | 1341{1:~ }|*4 1342:%snomagic/3.*/X^ | 1343]] ) 1344end ) 1345 1346it ( 'shows preview when cmd modifiers are present' , function () 1347-- one modifier 1348feed ( ':keeppatterns %s/tw/to' ) 1349screen : expect { any =[[{20:to}o lines]] } 1350feed ( '<Esc>' ) 1351screen : expect { any =[[two lines]] } 1352 1353-- multiple modifiers 1354feed ( ':keeppatterns silent %s/tw/to' ) 1355screen : expect { any =[[{20:to}o lines]] } 1356feed ( '<Esc>' ) 1357screen : expect { any =[[two lines]] } 1358 1359-- non-modifier prefix 1360feed ( ':silent tabedit %s/tw/to' ) 1361screen : expect ( [[ 1362Inc substitution on | 1363two lines | 1364Inc substitution on | 1365two lines | 1366| 1367{1:~ }|*2 1368{3: }| 1369:silent tabedit %s/t| 1370w/to^ | 1371]] ) 1372end ) 1373 1374it ( 'does not show window after toggling :set inccommand' , function () 1375feed ( ':%s/tw/OKOK' ) 1376feed ( '<Esc>' ) 1377command ( 'set icm=split' ) 1378feed ( ':%s/tw/OKOK' ) 1379feed ( '<Esc>' ) 1380command ( 'set icm=nosplit' ) 1381feed ( ':%s/tw/OKOK' ) 1382poke_eventloop () 1383screen : expect ( [[ 1384Inc substitution on | 1385{20:OKOK}o lines | 1386Inc substitution on | 1387{20:OKOK}o lines | 1388| 1389{1:~ }|*4 1390:%s/tw/OKOK^ | 1391]] ) 1392end ) 1393 1394it ( 'never shows preview buffer' , function () 1395command ( 'set hlsearch' ) 1396 1397feed ( ':%s/tw' ) 1398screen : expect ( [[ 1399Inc substitution on | 1400{20:tw}o lines | 1401Inc substitution on | 1402{20:tw}o lines | 1403| 1404{1:~ }|*4 1405:%s/tw^ | 1406]] ) 1407 1408feed ( '/BM' ) 1409screen : expect ( [[ 1410Inc substitution on | 1411{20:BM}o lines | 1412Inc substitution on | 1413{20:BM}o lines | 1414| 1415{1:~ }|*4 1416:%s/tw/BM^ | 1417]] ) 1418 1419feed ( '/' ) 1420screen : expect ( [[ 1421Inc substitution on | 1422{20:BM}o lines | 1423Inc substitution on | 1424{20:BM}o lines | 1425| 1426{1:~ }|*4 1427:%s/tw/BM/^ | 1428]] ) 1429 1430feed ( '<enter>' ) 1431screen : expect ( [[ 1432Inc substitution on | 1433BMo lines | 1434Inc substitution on | 1435^BMo lines | 1436| 1437{1:~ }|*4 1438:%s/tw/BM/ | 1439]] ) 1440end ) 1441 1442it ( 'clears preview if non-previewable command is edited' , function () 1443-- Put a non-previewable command in history. 1444feed ( ":echo 'foo'<CR>" ) 1445-- Start an incomplete :substitute command. 1446feed ( ':1,2s/t/X' ) 1447 1448screen : expect ( [[ 1449Inc subs{20:X}itution on | 1450{20:X}wo lines | 1451Inc substitution on | 1452two lines | 1453| 1454{1:~ }|*4 1455:1,2s/t/X^ | 1456]] ) 1457 1458-- Select the previous command. 1459feed ( '<C-P>' ) 1460-- Assert that preview was cleared. 1461screen : expect ( [[ 1462Inc substitution on | 1463two lines | 1464Inc substitution on | 1465two lines | 1466| 1467{1:~ }|*4 1468:echo 'foo'^ | 1469]] ) 1470end ) 1471 1472it ( 'does not execute trailing bar-separated commands #7494' , function () 1473feed ( ':%s/two/three/g|q!' ) 1474screen : expect ( [[ 1475Inc substitution on | 1476{20:three} lines | 1477Inc substitution on | 1478{20:three} lines | 1479| 1480{1:~ }|*4 1481:%s/two/three/g|q!^ | 1482]] ) 1483eq ( eval ( 'v:null' ), eval ( 'v:exiting' )) 1484end ) 1485 1486it ( 'does not break bar-separated command #8796' , function () 1487source ( [[ 1488function! F() 1489if v:false | return | endif 1490endfun 1491]] ) 1492command ( 'call timer_start(10, {-> F()}, {"repeat":-1})' ) 1493feed ( ':%s/' ) 1494sleep ( 20 ) -- Allow some timer activity. 1495screen : expect ( [[ 1496Inc substitution on | 1497two lines | 1498Inc substitution on | 1499two lines | 1500| 1501{1:~ }|*4 1502:%s/^ | 1503]] ) 1504end ) 1505end ) 1506 1507describe ( ":substitute, 'inccommand' with a failing expression" , function () 1508local screen 1509local cases ={ '' , 'split' , 'nosplit' } 1510 1511local function refresh ( case ) 1512clear () 1513screen =Screen . new ( 20 , 10 ) 1514common_setup ( screen , case , default_text ) 1515end 1516 1517it ( 'in the pattern does nothing' , function () 1518for _ , case in pairs ( cases ) do 1519refresh ( case ) 1520command ( 'set inccommand=' .. case ) 1521feed ( ':silent! %s/tw\\(/LARD/' ) 1522poke_eventloop () 1523feed ( '<enter>' ) 1524expect ( default_text ) 1525end 1526end ) 1527 1528it ( 'in the replacement deletes the matches' , function () 1529for _ , case in pairs ( cases ) do 1530refresh ( case ) 1531local replacements ={ "\\='LARD" , '\\=xx_novar__xx' } 1532 1533for _ , repl in pairs ( replacements ) do 1534command ( 'set inccommand=' .. case ) 1535feed ( ':silent! %s/tw/' .. repl .. '/' ) 1536poke_eventloop () 1537feed ( '<enter>' ) 1538expect ( default_text : gsub ( 'tw' , '' )) 1539command ( 'undo' ) 1540end 1541end 1542end ) 1543 1544it ( 'in the range does not error #5912' , function () 1545for _ , case in pairs ( cases ) do 1546refresh ( case ) 1547feed ( ':100s/' ) 1548 1549screen : expect ( [[ 1550Inc substitution on | 1551two lines | 1552| 1553{1:~ }|*6 1554:100s/^ | 1555]] ) 1556 1557feed ( '<enter>' ) 1558screen : expect ( [[ 1559Inc substitution on | 1560two lines | 1561^ | 1562{1:~ }|*6 1563{9:E16: Invalid range} | 1564]] ) 1565end 1566end ) 1567end ) 1568 1569describe ( "'inccommand' and :cnoremap" , function () 1570local cases ={ '' , 'split' , 'nosplit' } 1571local screen 1572 1573local function refresh ( case , visual ) 1574clear () 1575screen =visual and Screen . new ( 80 , 10 ) or nil 1576common_setup ( screen , case , default_text ) 1577end 1578 1579it ( 'work with remapped characters' , function () 1580for _ , case in pairs ( cases ) do 1581refresh ( case ) 1582local cmd ='%s/lines/LINES/g' 1583 1584for i =1 , string . len ( cmd ) do 1585local c =string . sub ( cmd , i , i ) 1586command ( 'cnoremap ' .. c .. ' ' .. c ) 1587end 1588 1589feed ( ':' .. cmd ) 1590poke_eventloop () 1591feed ( '<CR>' ) 1592expect ( [[ 1593Inc substitution on 1594two LINES 1595]] ) 1596end 1597end ) 1598 1599it ( 'work when mappings move the cursor' , function () 1600for _ , case in pairs ( cases ) do 1601refresh ( case ) 1602command ( 'cnoremap ,S LINES/<left><left><left><left><left><left>' ) 1603 1604feed ( ':%s/lines/' ) 1605poke_eventloop () 1606feed ( ',S' ) 1607poke_eventloop () 1608feed ( 'or three <enter>' ) 1609poke_eventloop () 1610expect ( [[ 1611Inc substitution on 1612two or three LINES 1613]] ) 1614 1615command ( 'cnoremap ;S /X/<left><left><left>' ) 1616feed ( ':%s/' ) 1617poke_eventloop () 1618feed ( ';S' ) 1619poke_eventloop () 1620feed ( 'I<enter>' ) 1621expect ( [[ 1622Xnc substitution on 1623two or three LXNES 1624]] ) 1625 1626command ( 'cnoremap ,T //Y/<left><left><left>' ) 1627feed ( ':%s' ) 1628poke_eventloop () 1629feed ( ',T' ) 1630poke_eventloop () 1631feed ( 'X<enter>' ) 1632expect ( [[ 1633Ync substitution on 1634two or three LYNES 1635]] ) 1636 1637command ( 'cnoremap ;T s//Z/<left><left><left>' ) 1638feed ( ':%' ) 1639poke_eventloop () 1640feed ( ';T' ) 1641poke_eventloop () 1642feed ( 'Y<enter>' ) 1643expect ( [[ 1644Znc substitution on 1645two or three LZNES 1646]] ) 1647end 1648end ) 1649 1650it ( 'still works with a broken mapping' , function () 1651for _ , case in pairs ( cases ) do 1652refresh ( case , true ) 1653command ( "cnoremap <expr> x execute('bwipeout!')[-1].'x'" ) 1654 1655api . nvim_set_vvar ( 'errmsg' , '' ) 1656feed ( ':%s/tw/tox' ) 1657-- error thrown b/c of the mapping 1658matches ( '^E565:' , api . nvim_get_vvar ( 'errmsg' )) 1659 1660feed ( '<enter>' ) 1661expect ( [[ 1662Inc substitution on 1663toxo lines 1664]] ) 1665end 1666end ) 1667 1668it ( 'work when temporarily moving the cursor' , function () 1669for _ , case in pairs ( cases ) do 1670refresh ( case ) 1671command ( "cnoremap <expr> x cursor(1, 1)[-1].'x'" ) 1672 1673feed ( ':%s/tw/tox' ) 1674poke_eventloop () 1675feed ( '/g<enter>' ) 1676expect ( default_text : gsub ( 'tw' , 'tox' )) 1677end 1678end ) 1679 1680it ( "work when a mapping disables 'inccommand'" , function () 1681for _ , case in pairs ( cases ) do 1682refresh ( case ) 1683command ( "cnoremap <expr> x execute('set inccommand=')[-1]" ) 1684 1685feed ( ':%s/tw/tox' ) 1686poke_eventloop () 1687feed ( 'a/g<enter>' ) 1688expect ( default_text : gsub ( 'tw' , 'toa' )) 1689end 1690end ) 1691 1692it ( 'work with a complex mapping' , function () 1693for _ , case in pairs ( cases ) do 1694refresh ( case ) 1695source ( [[cnoremap x <C-\>eextend(g:, {'fo': getcmdline()}) 1696\.fo<CR><C-c>:new<CR>:bw!<CR>:<C-r>=remove(g:, 'fo')<CR>x]] ) 1697 1698feed ( ':%s/tw/tox' ) 1699poke_eventloop () 1700feed ( '/<enter>' ) 1701expect ( default_text : gsub ( 'tw' , 'tox' )) 1702end 1703end ) 1704end ) 1705 1706describe ( "'inccommand' autocommands" , function () 1707before_each ( clear ) 1708 1709-- keys are events to be tested 1710-- values are arrays like 1711-- { open = { 1 }, close = { 2, 3} } 1712-- which would mean that during the test below the event fires for 1713-- buffer 1 when opening the preview window, and for buffers 2 and 3 1714-- when closing the preview window 1715local eventsExpected ={ 1716BufAdd ={} , 1717BufDelete ={} , 1718BufEnter ={} , 1719BufFilePost ={} , 1720BufFilePre ={} , 1721BufHidden ={} , 1722BufLeave ={} , 1723BufNew ={} , 1724BufNewFile ={} , 1725BufRead ={} , 1726BufReadCmd ={} , 1727BufReadPre ={} , 1728BufUnload ={} , 1729BufWinEnter ={} , 1730BufWinLeave ={} , 1731BufWipeout ={} , 1732BufWrite ={} , 1733BufWriteCmd ={} , 1734BufWritePost ={} , 1735Syntax ={} , 1736FileType ={} , 1737WinEnter ={} , 1738WinLeave ={} , 1739CmdwinEnter ={} , 1740CmdwinLeave ={} , 1741} 1742 1743local function bufferlist ( q ) 1744local s ='' 1745for _ , buffer in pairs ( q ) do 1746s =s .. ', ' .. tostring ( buffer ) 1747end 1748return s 1749end 1750 1751-- fill the table with default values 1752for event , _ in pairs ( eventsExpected ) do 1753eventsExpected [ event ]. open =eventsExpected [ event ]. open or {} 1754eventsExpected [ event ]. close =eventsExpected [ event ]. close or {} 1755end 1756 1757local function register_autocmd ( event ) 1758api . nvim_set_var ( event .. '_fired' , {} ) 1759command ( 'autocmd ' .. event .. ' * call add(g:' .. event .. "_fired, expand('<abuf>'))" ) 1760end 1761 1762it ( 'are not fired when splitting' , function () 1763common_setup ( nil , 'split' , default_text ) 1764 1765local eventsObserved ={} 1766for event , _ in pairs ( eventsExpected ) do 1767eventsObserved [ event ] ={} 1768register_autocmd ( event ) 1769end 1770 1771feed ( ':%s/tw' ) 1772 1773for event , _ in pairs ( eventsExpected ) do 1774eventsObserved [ event ]. open =api . nvim_get_var ( event .. '_fired' ) 1775api . nvim_set_var ( event .. '_fired' , {} ) 1776end 1777 1778feed ( '/<enter>' ) 1779 1780for event , _ in pairs ( eventsExpected ) do 1781eventsObserved [ event ]. close =api . nvim_get_var ( event .. '_fired' ) 1782end 1783 1784for event , _ in pairs ( eventsExpected ) do 1785eq ( 1786event .. bufferlist ( eventsExpected [ event ]. open ), 1787event .. bufferlist ( eventsObserved [ event ]. open ) 1788) 1789eq ( 1790event .. bufferlist ( eventsExpected [ event ]. close ), 1791event .. bufferlist ( eventsObserved [ event ]. close ) 1792) 1793end 1794end ) 1795end ) 1796 1797describe ( "'inccommand' split windows" , function () 1798local screen 1799local function refresh () 1800clear () 1801screen =Screen . new ( 40 , 30 ) 1802common_setup ( screen , 'split' , default_text ) 1803end 1804 1805it ( 'work after more splits' , function () 1806refresh () 1807 1808feed ( 'gg' ) 1809command ( 'vsplit' ) 1810command ( 'split' ) 1811feed ( ':%s/tw' ) 1812screen : expect ( [[ 1813Inc substitution on │Inc substitution on| 1814{20:tw}o lines │{20:tw}o lines | 1815│ | 1816{1:~ }│{1:~ }|*11 1817{3:[No Name] [+] }│{1:~ }| 1818Inc substitution on │{1:~ }| 1819{20:tw}o lines │{1:~ }| 1820│{1:~ }| 1821{1:~ }│{1:~ }|*2 1822{2:[No Name] [+] [No Name] [+] }| 1823|2| {20:tw}o lines | 1824{1:~ }|*6 1825{2:[Preview] }| 1826:%s/tw^ | 1827]] ) 1828 1829feed ( '<esc>' ) 1830command ( 'only' ) 1831command ( 'split' ) 1832command ( 'vsplit' ) 1833 1834feed ( ':%s/tw' ) 1835screen : expect ( [[ 1836Inc substitution on │Inc substitution on| 1837{20:tw}o lines │{20:tw}o lines | 1838│ | 1839{1:~ }│{1:~ }|*11 1840{3:[No Name] [+] }{2:[No Name] [+] }| 1841Inc substitution on | 1842{20:tw}o lines | 1843| 1844{1:~ }|*2 1845{2:[No Name] [+] }| 1846|2| {20:tw}o lines | 1847{1:~ }|*6 1848{2:[Preview] }| 1849:%s/tw^ | 1850]] ) 1851end ) 1852 1853local settings ={ 1854'splitbelow' , 1855'splitright' , 1856'noequalalways' , 1857'equalalways eadirection=ver' , 1858'equalalways eadirection=hor' , 1859'equalalways eadirection=both' , 1860} 1861 1862it ( 'are not affected by various settings' , function () 1863for _ , setting in pairs ( settings ) do 1864refresh () 1865command ( 'set ' .. setting ) 1866 1867feed ( ':%s/tw' ) 1868 1869screen : expect ( [[ 1870Inc substitution on | 1871{20:tw}o lines | 1872| 1873{1:~ }|*17 1874{3:[No Name] [+] }| 1875|2| {20:tw}o lines | 1876{1:~ }|*6 1877{2:[Preview] }| 1878:%s/tw^ | 1879]] ) 1880end 1881end ) 1882 1883it ( "don't open if there's not enough room" , function () 1884refresh () 1885screen : try_resize ( 40 , 3 ) 1886feed ( 'gg:%s/tw' ) 1887screen : expect ( [[ 1888Inc substitution on | 1889{20:tw}o lines | 1890:%s/tw^ | 1891]] ) 1892end ) 1893end ) 1894 1895describe ( "'inccommand' with 'gdefault'" , function () 1896before_each ( function () 1897clear () 1898end ) 1899 1900it ( 'does not lock up #7244' , function () 1901common_setup ( nil , 'nosplit' , '{' ) 1902command ( 'set gdefault' ) 1903feed ( ':s/{\\n' ) 1904eq ( { mode ='c' , blocking =false } , api . nvim_get_mode ()) 1905feed ( '/A<Enter>' ) 1906expect ( 'A' ) 1907eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 1908end ) 1909 1910it ( 'with multiline text and range, does not lock up #7244' , function () 1911common_setup ( nil , 'nosplit' , '{\n\n{' ) 1912command ( 'set gdefault' ) 1913feed ( ':%s/{\\n' ) 1914eq ( { mode ='c' , blocking =false } , api . nvim_get_mode ()) 1915feed ( '/A<Enter>' ) 1916expect ( 'A\nA' ) 1917eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 1918end ) 1919 1920it ( 'does not crash on zero-width matches #7485' , function () 1921common_setup ( nil , 'split' , default_text ) 1922command ( 'set gdefault' ) 1923feed ( 'gg' ) 1924feed ( 'Vj' ) 1925feed ( ':s/\\%V' ) 1926eq ( { mode ='c' , blocking =false } , api . nvim_get_mode ()) 1927feed ( '<Esc>' ) 1928eq ( { mode ='n' , blocking =false } , api . nvim_get_mode ()) 1929end ) 1930 1931it ( 'removes highlights after abort for a zero-width match' , function () 1932local screen =Screen . new ( 30 , 5 ) 1933common_setup ( screen , 'nosplit' , default_text ) 1934command ( 'set gdefault' ) 1935 1936feed ( ':%s/\\%1c/a/' ) 1937screen : expect ( [[ 1938{20:a}Inc substitution on | 1939{20:a}two lines | 1940{20:a} | 1941{1:~ }| 1942:%s/\%1c/a/^ | 1943]] ) 1944 1945feed ( '<Esc>' ) 1946screen : expect ( [[ 1947Inc substitution on | 1948two lines | 1949^ | 1950{1:~ }| 1951| 1952]] ) 1953end ) 1954end ) 1955 1956describe ( ':substitute' , function () 1957local screen 1958before_each ( function () 1959clear () 1960screen =Screen . new ( 30 , 15 ) 1961end ) 1962 1963it ( 'inccommand=split, highlights multiline substitutions' , function () 1964common_setup ( screen , 'split' , multiline_text ) 1965feed ( 'gg' ) 1966 1967feed ( ':%s/2\\_.*X' ) 1968screen : expect ( [[ 19691 {20:2 3} | 1970{20:A B C} | 1971{20:4 5 6} | 1972{20:X} Y Z | 19737 8 9 | 1974{3:[No Name] [+] }| 1975|1| 1 {20:2 3} | 1976|2|{20: A B C} | 1977|3|{20: 4 5 6} | 1978|4|{20: X} Y Z | 1979{1:~ }|*3 1980{2:[Preview] }| 1981:%s/2\_.*X^ | 1982]] ) 1983 1984feed ( '/MMM' ) 1985screen : expect ( [[ 19861 {20:MMM} Y Z | 19877 8 9 | 1988| 1989{1:~ }|*2 1990{3:[No Name] [+] }| 1991|1| 1 {20:MMM} Y Z | 1992{1:~ }|*6 1993{2:[Preview] }| 1994:%s/2\_.*X/MMM^ | 1995]] ) 1996 1997feed ( '\\rK\\rLLL' ) 1998screen : expect ( [[ 19991 {20:MMM} | 2000{20:K} | 2001{20:LLL} Y Z | 20027 8 9 | 2003| 2004{3:[No Name] [+] }| 2005|1| 1 {20:MMM} | 2006|2|{20: K} | 2007|3|{20: LLL} Y Z | 2008{1:~ }|*4 2009{2:[Preview] }| 2010:%s/2\_.*X/MMM\rK\rLLL^ | 2011]] ) 2012end ) 2013 2014it ( 'inccommand=nosplit, highlights multiline substitutions' , function () 2015common_setup ( screen , 'nosplit' , multiline_text ) 2016feed ( 'gg' ) 2017 2018feed ( ':%s/2\\_.*X/MMM' ) 2019screen : expect ( [[ 20201 {20:MMM} Y Z | 20217 8 9 | 2022| 2023{1:~ }|*11 2024:%s/2\_.*X/MMM^ | 2025]] ) 2026 2027feed ( '\\rK\\rLLL' ) 2028screen : expect ( [[ 20291 {20:MMM} | 2030{20:K} | 2031{20:LLL} Y Z | 20327 8 9 | 2033| 2034{1:~ }|*9 2035:%s/2\_.*X/MMM\rK\rLLL^ | 2036]] ) 2037end ) 2038 2039it ( 'inccommand=split, highlights multiple matches on a line' , function () 2040common_setup ( screen , 'split' , multimatch_text ) 2041command ( 'set gdefault' ) 2042feed ( 'gg' ) 2043 2044feed ( ':%s/a/XLK' ) 2045screen : expect ( [[ 2046{20:XLK} bdc e{20:XLK}e {20:XLK} fgl lzi{20:XLK} r| 2047x | 2048| 2049{1:~ }|*2 2050{3:[No Name] [+] }| 2051|1| {20:XLK} bdc e{20:XLK}e {20:XLK} fgl lzi{20:X}| 2052{20:LK} r | 2053{1:~ }|*5 2054{2:[Preview] }| 2055:%s/a/XLK^ | 2056]] ) 2057end ) 2058 2059it ( 'inccommand=nosplit, highlights multiple matches on a line' , function () 2060common_setup ( screen , 'nosplit' , multimatch_text ) 2061command ( 'set gdefault' ) 2062feed ( 'gg' ) 2063 2064feed ( ':%s/a/XLK' ) 2065screen : expect ( [[ 2066{20:XLK} bdc e{20:XLK}e {20:XLK} fgl lzi{20:XLK} r| 2067x | 2068| 2069{1:~ }|*11 2070:%s/a/XLK^ | 2071]] ) 2072end ) 2073 2074it ( 'inccommand=split, with \\zs' , function () 2075common_setup ( screen , 'split' , multiline_text ) 2076feed ( 'gg' ) 2077 2078feed ( ':%s/[0-9]\\n\\zs[A-Z]/OKO' ) 2079screen : expect ( [[ 2080{20:OKO} B C | 20814 5 6 | 2082{20:OKO} Y Z | 20837 8 9 | 2084| 2085{3:[No Name] [+] }| 2086|1| 1 2 3 | 2087|2| {20:OKO} B C | 2088|3| 4 5 6 | 2089|4| {20:OKO} Y Z | 2090{1:~ }|*3 2091{2:[Preview] }| 2092:%s/[0-9]\n\zs[A-Z]/OKO^ | 2093]] ) 2094end ) 2095 2096it ( 'inccommand=nosplit, with \\zs' , function () 2097common_setup ( screen , 'nosplit' , multiline_text ) 2098feed ( 'gg' ) 2099 2100feed ( ':%s/[0-9]\\n\\zs[A-Z]/OKO' ) 2101screen : expect ( [[ 21021 2 3 | 2103{20:OKO} B C | 21044 5 6 | 2105{20:OKO} Y Z | 21067 8 9 | 2107| 2108{1:~ }|*8 2109:%s/[0-9]\n\zs[A-Z]/OKO^ | 2110]] ) 2111end ) 2112 2113it ( 'inccommand=split, substitutions of different length' , function () 2114common_setup ( screen , 'split' , 'T T123 T2T TTT T090804\nx' ) 2115 2116feed ( ':%s/T\\([0-9]\\+\\)/\\1\\1/g' ) 2117screen : expect ( [[ 2118T {20:123123} {20:22}T TTT {20:090804090804} | 2119x | 2120{1:~ }|*3 2121{3:[No Name] [+] }| 2122|1| T {20:123123} {20:22}T TTT {20:090804090}| 2123{20:804} | 2124{1:~ }|*5 2125{2:[Preview] }| 2126:%s/T\([0-9]\+\)/\1\1/g^ | 2127]] ) 2128end ) 2129 2130it ( 'inccommand=nosplit, substitutions of different length' , function () 2131common_setup ( screen , 'nosplit' , 'T T123 T2T TTT T090804\nx' ) 2132 2133feed ( ':%s/T\\([0-9]\\+\\)/\\1\\1/g' ) 2134screen : expect ( [[ 2135T {20:123123} {20:22}T TTT {20:090804090804} | 2136x | 2137{1:~ }|*12 2138:%s/T\([0-9]\+\)/\1\1/g^ | 2139]] ) 2140end ) 2141 2142it ( 'inccommand=split, contraction of lines' , function () 2143local text =[[ 2144T T123 T T123 T2T TT T23423424 2145x 2146afa Q 2147adf la;lkd R 2148alx 2149]] 2150 2151common_setup ( screen , 'split' , text ) 2152feed ( ':%s/[QR]\\n' ) 2153screen : expect ( [[ 2154afa {20:Q} | 2155adf la;lkd {20:R} | 2156alx | 2157| 2158{1:~ }| 2159{3:[No Name] [+] }| 2160|3| afa {20:Q} | 2161|4|{20: }adf la;lkd {20:R} | 2162|5|{20: }alx | 2163{1:~ }|*4 2164{2:[Preview] }| 2165:%s/[QR]\n^ | 2166]] ) 2167 2168feed ( '/KKK' ) 2169screen : expect ( [[ 2170T T123 T T123 T2T TT T23423424| 2171x | 2172afa {20:KKK}adf la;lkd {20:KKK}alx | 2173| 2174{1:~ }| 2175{3:[No Name] [+] }| 2176|3| afa {20:KKK}adf la;lkd {20:KKK}alx | 2177{1:~ }|*6 2178{2:[Preview] }| 2179:%s/[QR]\n/KKK^ | 2180]] ) 2181end ) 2182 2183it ( 'inccommand=nosplit, contraction of lines' , function () 2184local text =[[ 2185T T123 T T123 T2T TT T23423424 2186x 2187afa Q 2188adf la;lkd R 2189alx 2190]] 2191 2192common_setup ( screen , 'nosplit' , text ) 2193feed ( ':%s/[QR]\\n/KKK' ) 2194screen : expect ( [[ 2195T T123 T T123 T2T TT T23423424| 2196x | 2197afa {20:KKK}adf la;lkd {20:KKK}alx | 2198| 2199{1:~ }|*10 2200:%s/[QR]\n/KKK^ | 2201]] ) 2202end ) 2203 2204it ( 'inccommand=split, contraction of two subsequent NL chars' , function () 2205local text =[[ 2206AAA AA 2207 2208BBB BB 2209 2210CCC CC 2211 2212]] 2213 2214-- This used to crash, but more than 20 highlight entries are required 2215-- to reproduce it (so that the marktree has multiple nodes) 2216common_setup ( screen , 'split' , string . rep ( text , 10 )) 2217feed ( ':%s/\\n\\n/<c-v><c-m>/g' ) 2218screen : expect { 2219grid =[[ 2220CCC CC | 2221AAA AA | 2222BBB BB | 2223CCC CC | 2224| 2225{3:[No Name] [+] }| 2226| 1| AAA AA | 2227| 2|{20: }BBB BB | 2228| 3|{20: }CCC CC | 2229| 4|{20: }AAA AA | 2230| 5|{20: }BBB BB | 2231| 6|{20: }CCC CC | 2232| 7|{20: }AAA AA | 2233{2:[Preview] }| 2234:%s/\n\n/{18:^M}/g^ | 2235]] , 2236} 2237assert_alive () 2238end ) 2239 2240it ( 'inccommand=nosplit, contraction of two subsequent NL chars' , function () 2241local text =[[ 2242AAA AA 2243 2244BBB BB 2245 2246CCC CC 2247 2248]] 2249 2250common_setup ( screen , 'nosplit' , string . rep ( text , 10 )) 2251feed ( ':%s/\\n\\n/<c-v><c-m>/g' ) 2252screen : expect { 2253grid =[[ 2254CCC CC | 2255AAA AA | 2256BBB BB | 2257CCC CC | 2258AAA AA | 2259BBB BB | 2260CCC CC | 2261AAA AA | 2262BBB BB | 2263CCC CC | 2264AAA AA | 2265BBB BB | 2266CCC CC | 2267| 2268:%s/\n\n/{18:^M}/g^ | 2269]] , 2270} 2271assert_alive () 2272end ) 2273 2274it ( 'inccommand=split, multibyte text' , function () 2275common_setup ( screen , 'split' , multibyte_text ) 2276feed ( ':%s/£.*ѫ/X¥¥' ) 2277screen : expect ( [[ 2278a{20:X¥¥}¥KOL | 2279£ ¥ libm | 2280£ ¥ | 2281| 2282{1:~ }| 2283{3:[No Name] [+] }| 2284|1| {20:X¥¥} PEPPERS | 2285|2| {20:X¥¥} | 2286|3| a{20:X¥¥}¥KOL | 2287{1:~ }|*4 2288{2:[Preview] }| 2289:%s/£.*ѫ/X¥¥^ | 2290]] ) 2291 2292feed ( '\\ra££ ¥' ) 2293screen : expect ( [[ 2294a{20:X¥¥} | 2295{20:a££ ¥}¥KOL | 2296£ ¥ libm | 2297£ ¥ | 2298| 2299{3:[No Name] [+] }| 2300|1| {20:X¥¥} | 2301|2|{20: a££ ¥} PEPPERS | 2302|3| {20:X¥¥} | 2303|4|{20: a££ ¥} | 2304|5| a{20:X¥¥} | 2305|6|{20: a££ ¥}¥KOL | 2306{1:~ }| 2307{2:[Preview] }| 2308:%s/£.*ѫ/X¥¥\ra££ ¥^ | 2309]] ) 2310end ) 2311 2312it ( 'inccommand=nosplit, multibyte text' , function () 2313common_setup ( screen , 'nosplit' , multibyte_text ) 2314feed ( ':%s/£.*ѫ/X¥¥' ) 2315screen : expect ( [[ 2316{20:X¥¥} PEPPERS | 2317{20:X¥¥} | 2318a{20:X¥¥}¥KOL | 2319£ ¥ libm | 2320£ ¥ | 2321| 2322{1:~ }|*8 2323:%s/£.*ѫ/X¥¥^ | 2324]] ) 2325 2326feed ( '\\ra££ ¥' ) 2327screen : expect ( [[ 2328{20:X¥¥} | 2329{20:a££ ¥} PEPPERS | 2330{20:X¥¥} | 2331{20:a££ ¥} | 2332a{20:X¥¥} | 2333{20:a££ ¥}¥KOL | 2334£ ¥ libm | 2335£ ¥ | 2336| 2337{1:~ }|*5 2338:%s/£.*ѫ/X¥¥\ra££ ¥^ | 2339]] ) 2340end ) 2341 2342it ( 'inccommand=split, small cmdwinheight' , function () 2343common_setup ( screen , 'split' , long_multiline_text ) 2344command ( 'set cmdwinheight=2' ) 2345 2346feed ( ':%s/[a-z]' ) 2347screen : expect ( [[ 2348X Y Z | 23497 8 9 | 2350K L M | 2351{20:a} b c | 2352{20:d} e f | 2353{20:q} r s | 2354{20:x} y z | 2355£ {20:m} n | 2356{20:t} œ ¥ | 2357| 2358{3:[No Name] [+] }| 2359| 7| {20:a} b c | 2360| 8| {20:d} e f | 2361{2:[Preview] }| 2362:%s/[a-z]^ | 2363]] ) 2364 2365feed ( '/JLKR £' ) 2366screen : expect ( [[ 2367X Y Z | 23687 8 9 | 2369K L M | 2370{20:JLKR £} b c | 2371{20:JLKR £} e f | 2372{20:JLKR £} r s | 2373{20:JLKR £} y z | 2374£ {20:JLKR £} n | 2375{20:JLKR £} œ ¥ | 2376| 2377{3:[No Name] [+] }| 2378| 7| {20:JLKR £} b c | 2379| 8| {20:JLKR £} e f | 2380{2:[Preview] }| 2381:%s/[a-z]/JLKR £^ | 2382]] ) 2383 2384feed ( '\\rѫ ab \\rXXXX' ) 2385screen : expect ( [[ 23867 8 9 | 2387K L M | 2388{20:JLKR £} | 2389{20:ѫ ab } | 2390{20:XXXX} b c | 2391{20:JLKR £} | 2392{20:ѫ ab } | 2393{20:XXXX} e f | 2394{20:JLKR £} | 2395{20:ѫ ab } | 2396{3:[No Name] [+] }| 2397| 7| {20:JLKR £} | 2398{3: }| 2399:%s/[a-z]/JLKR £\rѫ ab \rXXX| 2400X^ | 2401]] ) 2402end ) 2403 2404it ( 'inccommand=split, large cmdwinheight' , function () 2405common_setup ( screen , 'split' , long_multiline_text ) 2406command ( 'set cmdwinheight=11' ) 2407 2408feed ( ':%s/. .$' ) 2409screen : expect ( [[ 2410t {20:œ ¥} | 2411{3:[No Name] [+] }| 2412| 1| 1 {20:2 3} | 2413| 2| A {20:B C} | 2414| 3| 4 {20:5 6} | 2415| 4| X {20:Y Z} | 2416| 5| 7 {20:8 9} | 2417| 6| K {20:L M} | 2418| 7| a {20:b c} | 2419| 8| d {20:e f} | 2420| 9| q {20:r s} | 2421|10| x {20:y z} | 2422|11| £ {20:m n} | 2423{2:[Preview] }| 2424:%s/. .$^ | 2425]] ) 2426 2427feed ( '/ YYY' ) 2428screen : expect ( [[ 2429t {20: YYY} | 2430{3:[No Name] [+] }| 2431| 1| 1 {20: YYY} | 2432| 2| A {20: YYY} | 2433| 3| 4 {20: YYY} | 2434| 4| X {20: YYY} | 2435| 5| 7 {20: YYY} | 2436| 6| K {20: YYY} | 2437| 7| a {20: YYY} | 2438| 8| d {20: YYY} | 2439| 9| q {20: YYY} | 2440|10| x {20: YYY} | 2441|11| £ {20: YYY} | 2442{2:[Preview] }| 2443:%s/. .$/ YYY^ | 2444]] ) 2445 2446feed ( '\\r KKK' ) 2447screen : expect ( [[ 2448a {20: YYY} | 2449{3:[No Name] [+] }| 2450| 1| 1 {20: YYY} | 2451| 2|{20: KKK} | 2452| 3| A {20: YYY} | 2453| 4|{20: KKK} | 2454| 5| 4 {20: YYY} | 2455| 6|{20: KKK} | 2456| 7| X {20: YYY} | 2457| 8|{20: KKK} | 2458| 9| 7 {20: YYY} | 2459|10|{20: KKK} | 2460|11| K {20: YYY} | 2461{2:[Preview] }| 2462:%s/. .$/ YYY\r KKK^ | 2463]] ) 2464end ) 2465 2466it ( 'inccommand=split, lookaround' , function () 2467common_setup ( screen , 'split' , 'something\neverything\nsomeone' ) 2468feed ( [[:%s/\(some\)\@<lt>=thing/one/]] ) 2469screen : expect ( [[ 2470some{20:one} | 2471everything | 2472someone | 2473{1:~ }|*2 2474{3:[No Name] [+] }| 2475|1| some{20:one} | 2476{1:~ }|*6 2477{2:[Preview] }| 2478:%s/\(some\)\@<=thing/one/^ | 2479]] ) 2480 2481feed ( '<C-c>' ) 2482feed ( 'gg' ) 2483poke_eventloop () 2484feed ( [[:%s/\(some\)\@<lt>!thing/one/]] ) 2485screen : expect ( [[ 2486something | 2487every{20:one} | 2488someone | 2489{1:~ }|*2 2490{3:[No Name] [+] }| 2491|2| every{20:one} | 2492{1:~ }|*6 2493{2:[Preview] }| 2494:%s/\(some\)\@<!thing/one/^ | 2495]] ) 2496 2497feed ( [[<C-c>]] ) 2498poke_eventloop () 2499feed ( [[:%s/some\(thing\)\@=/every/]] ) 2500screen : expect ( [[ 2501{20:every}thing | 2502everything | 2503someone | 2504{1:~ }|*2 2505{3:[No Name] [+] }| 2506|1| {20:every}thing | 2507{1:~ }|*6 2508{2:[Preview] }| 2509:%s/some\(thing\)\@=/every/^ | 2510]] ) 2511 2512feed ( [[<C-c>]] ) 2513poke_eventloop () 2514feed ( [[:%s/some\(thing\)\@!/every/]] ) 2515screen : expect ( [[ 2516something | 2517everything | 2518{20:every}one | 2519{1:~ }|*2 2520{3:[No Name] [+] }| 2521|3| {20:every}one | 2522{1:~ }|*6 2523{2:[Preview] }| 2524:%s/some\(thing\)\@!/every/^ | 2525]] ) 2526end ) 2527 2528it ( "doesn't prompt to swap cmd range" , function () 2529screen : try_resize ( 50 , 8 ) -- wide to avoid hit-enter prompt 2530common_setup ( screen , 'split' , default_text ) 2531feed ( ':2,1s/tw/MO/g' ) 2532 2533-- substitution preview should have been made, without prompting 2534screen : expect ( [[ 2535{20:MO}o lines | 2536{3:[No Name] [+] }| 2537|2| {20:MO}o lines | 2538{1:~ }|*3 2539{2:[Preview] }| 2540:2,1s/tw/MO/g^ | 2541]] ) 2542 2543-- but should be prompted on hitting enter 2544feed ( '<CR>' ) 2545screen : expect ( [[ 2546{20:MO}o lines | 2547{3:[No Name] [+] }| 2548|2| {20:MO}o lines | 2549{1:~ }|*3 2550{2:[Preview] }| 2551{6:Backwards range given, OK to swap (y/n)?}^ | 2552]] ) 2553 2554feed ( 'y' ) 2555screen : expect ( [[ 2556Inc substitution on | 2557^MOo lines | 2558| 2559{1:~ }|*4 2560{6:Backwards range given, OK to swap (y/n)?}y | 2561]] ) 2562end ) 2563end ) 2564 2565it ( ':substitute with inccommand during :terminal activity' , function () 2566if t . skip_fragile ( t . pending ) then 2567return 2568end 2569retry ( 2 , 40000 , function () 2570clear () 2571local screen =Screen . new ( 30 , 15 ) 2572 2573command ( 'set cmdwinheight=3' ) 2574feed (( [[:terminal "%s" REP 5000 xxx<cr>]] ): format ( testprg ( 'shell-test' ))) 2575command ( 'file term' ) 2576feed ( 'G' ) -- Follow :terminal output. 2577command ( 'new' ) 2578common_setup ( screen , 'split' , 'foo bar baz\nbar baz fox\nbar foo baz' ) 2579command ( 'wincmd =' ) 2580 2581feed ( 'gg' ) 2582feed ( ':%s/foo/ZZZ' ) 2583sleep ( 20 ) -- Allow some terminal activity. 2584poke_eventloop () 2585screen : sleep ( 0 ) 2586screen : expect_unchanged () 2587end ) 2588end ) 2589 2590it ( ':substitute with inccommand, timer-induced :redraw #9777' , function () 2591clear () 2592local screen =Screen . new ( 30 , 12 ) 2593command ( 'set cmdwinheight=3' ) 2594command ( 'call timer_start(10, {-> execute("redraw")}, {"repeat":-1})' ) 2595command ( 'call timer_start(10, {-> execute("redrawstatus")}, {"repeat":-1})' ) 2596common_setup ( screen , 'split' , 'foo bar baz\nbar baz fox\nbar foo baz' ) 2597 2598feed ( 'gg' ) 2599feed ( ':%s/foo/ZZZ' ) 2600sleep ( 20 ) -- Allow some timer activity. 2601screen : expect ( [[ 2602{20:ZZZ} bar baz | 2603bar baz fox | 2604bar {20:ZZZ} baz | 2605{1:~ }|*3 2606{3:[No Name] [+] }| 2607|1| {20:ZZZ} bar baz | 2608|3| bar {20:ZZZ} baz | 2609{1:~ }| 2610{2:[Preview] }| 2611:%s/foo/ZZZ^ | 2612]] ) 2613 2614-- Also with nvim__redraw() 2615command ( 'call timer_start(10, {-> nvim__redraw(#{flush:1})}, {"repeat":-1})' ) 2616command ( 'call timer_start(10, {-> nvim__redraw(#{statusline:1})}, {"repeat":-1})' ) 2617sleep ( 20 ) -- Allow some timer activity. 2618screen : expect_unchanged () 2619end ) 2620 2621it ( ':substitute with inccommand, allows :redraw before first separator is typed #18857' , function () 2622clear () 2623local screen =Screen . new ( 30 , 6 ) 2624common_setup ( screen , 'split' , 'foo bar baz\nbar baz fox\nbar foo baz' ) 2625command ( 'hi! link NormalFloat CursorLine' ) 2626local float_buf =api . nvim_create_buf ( false , true ) 2627api . nvim_open_win ( float_buf , false , { 2628relative ='editor' , 2629height =1 , 2630width =5 , 2631row =3 , 2632col =0 , 2633focusable =false , 2634} ) 2635feed ( ':' ) 2636screen : expect ( [[ 2637foo bar baz | 2638bar baz fox | 2639bar foo baz | 2640{21: }{1: }| 2641{1:~ }| 2642:^ | 2643]] ) 2644feed ( '%s' ) 2645screen : expect ( [[ 2646foo bar baz | 2647bar baz fox | 2648bar foo baz | 2649{21: }{1: }| 2650{1:~ }| 2651:%s^ | 2652]] ) 2653api . nvim_buf_set_lines ( float_buf , 0 , - 1 , true , { 'foo' } ) 2654command ( 'redraw' ) 2655screen : expect ( [[ 2656foo bar baz | 2657bar baz fox | 2658bar foo baz | 2659{21:foo }{1: }| 2660{1:~ }| 2661:%s^ | 2662]] ) 2663end ) 2664 2665it ( ':substitute with inccommand, does not crash if range contains invalid marks' , function () 2666clear () 2667local screen =Screen . new ( 30 , 6 ) 2668common_setup ( screen , 'split' , 'test' ) 2669feed ( [[:'a,'bs]] ) 2670screen : expect ( [[ 2671test | 2672{1:~ }|*4 2673:'a,'bs^ | 2674]] ) 2675-- v:errmsg shouldn't be set either before the first separator is typed 2676eq ( '' , eval ( 'v:errmsg' )) 2677feed ( '/' ) 2678screen : expect ( [[ 2679test | 2680{1:~ }|*4 2681:'a,'bs/^ | 2682]] ) 2683end ) 2684 2685it ( ':substitute with inccommand, no unnecessary redraw if preview is not shown' , function () 2686clear () 2687local screen =Screen . new ( 60 , 6 ) 2688common_setup ( screen , 'split' , 'test' ) 2689feed ( ':ls<CR>' ) 2690screen : expect ( [[ 2691test | 2692{1:~ }| 2693{3: }| 2694:ls | 26951 %a + "[No Name]" line 1 | 2696{6:Press ENTER or type command to continue}^ | 2697]] ) 2698feed ( ':s' ) 2699-- no unnecessary redraw, so messages are still shown 2700screen : expect ( [[ 2701test | 2702{1:~ }| 2703{3: }| 2704:ls | 27051 %a + "[No Name]" line 1 | 2706:s^ | 2707]] ) 2708feed ( 'o' ) 2709screen : expect ( [[ 2710test | 2711{1:~ }| 2712{3: }| 2713:ls | 27141 %a + "[No Name]" line 1 | 2715:so^ | 2716]] ) 2717feed ( '<BS>' ) 2718screen : expect ( [[ 2719test | 2720{1:~ }| 2721{3: }| 2722:ls | 27231 %a + "[No Name]" line 1 | 2724:s^ | 2725]] ) 2726feed ( '/test' ) 2727-- now inccommand is shown, so screen is redrawn 2728screen : expect ( [[ 2729{20:test} | 2730{1:~ }|*4 2731:s/test^ | 2732]] ) 2733end ) 2734 2735it ( ":substitute doesn't crash with inccommand, if undo is empty #12932" , function () 2736clear () 2737local screen =Screen . new ( 10 , 5 ) 2738command ( 'set undolevels=-1' ) 2739common_setup ( screen , 'split' , 'test' ) 2740feed ( ':%s/test' ) 2741sleep ( 100 ) 2742feed ( '/' ) 2743sleep ( 100 ) 2744feed ( 'f' ) 2745screen : expect ( [[ 2746{20:f} | 2747{1:~ }|*3 2748:%s/test/f^ | 2749]] ) 2750assert_alive () 2751end ) 2752 2753it ( ':substitute with inccommand works properly if undo is not synced #20029' , function () 2754clear () 2755local screen =Screen . new ( 30 , 6 ) 2756common_setup ( screen , 'nosplit' , 'foo\nbar\nbaz' ) 2757api . nvim_set_keymap ( 'x' , '<F2>' , '<Esc>`<Oaaaaa asdf<Esc>`>obbbbb asdf<Esc>V`<k:s/asdf/' , {} ) 2758feed ( 'gg0<C-V>lljj<F2>' ) 2759screen : expect ( [[ 2760aaaaa | 2761foo | 2762bar | 2763baz | 2764bbbbb | 2765:'<,'>s/asdf/^ | 2766]] ) 2767feed ( 'hjkl' ) 2768screen : expect ( [[ 2769aaaaa {20:hjkl} | 2770foo | 2771bar | 2772baz | 2773bbbbb {20:hjkl} | 2774:'<,'>s/asdf/hjkl^ | 2775]] ) 2776feed ( '<CR>' ) 2777expect ( [[ 2778aaaaa hjkl 2779foo 2780bar 2781baz 2782bbbbb hjkl]] ) 2783feed ( 'u' ) 2784expect ( [[ 2785foo 2786bar 2787baz]] ) 2788end ) 2789 2790it ( ':substitute with inccommand does not unexpectedly change viewport #25697' , function () 2791clear () 2792local screen =Screen . new ( 45 , 5 ) 2793common_setup ( screen , 'nosplit' , long_multiline_text ) 2794command ( 'vnew | tabnew | tabclose' ) 2795screen : expect ( [[ 2796^ │£ m n | 2797{1:~ }│t œ ¥ | 2798{1:~ }│ | 2799{3:[No Name] }{2:[No Name] [+] }| 2800| 2801]] ) 2802feed ( ':s/' ) 2803screen : expect ( [[ 2804│£ m n | 2805{1:~ }│t œ ¥ | 2806{1:~ }│ | 2807{3:[No Name] }{2:[No Name] [+] }| 2808:s/^ | 2809]] ) 2810feed ( '<Esc>' ) 2811screen : expect ( [[ 2812^ │£ m n | 2813{1:~ }│t œ ¥ | 2814{1:~ }│ | 2815{3:[No Name] }{2:[No Name] [+] }| 2816| 2817]] ) 2818end ) 2819 2820it ( 'long :%s/ with inccommand does not collapse cmdline' , function () 2821clear () 2822local screen =Screen . new ( 10 , 5 ) 2823common_setup ( screen , 'nosplit' ) 2824feed ( 2825':%s/AAAAAAA' , 2826'A' , 2827'A' , 2828'A' , 2829'A' , 2830'A' , 2831'A' , 2832'A' , 2833'A' , 2834'A' , 2835'A' , 2836'A' , 2837'A' , 2838'A' , 2839'A' , 2840'A' , 2841'A' , 2842'A' , 2843'A' , 2844'A' , 2845'A' 2846) 2847screen : expect ( [[ 2848| 2849{3: }| 2850:%s/AAAAAAAA| 2851AAAAAAAAAAAA| 2852AAAAAAA^ | 2853]] ) 2854end ) 2855 2856it ( "with 'inccommand' typing invalid `={expr}` does not show error" , function () 2857clear () 2858local screen =Screen . new ( 30 , 6 ) 2859common_setup ( screen , 'nosplit' ) 2860feed ( ':edit `=`' ) 2861screen : expect ( [[ 2862| 2863{1:~ }|*4 2864:edit `=`^ | 2865]] ) 2866end ) 2867 2868it ( "with 'inccommand' typing :filter doesn't segfault or leak memory #19057" , function () 2869clear () 2870common_setup ( nil , 'nosplit' ) 2871feed ( ':filter s' ) 2872assert_alive () 2873feed ( ' ' ) 2874assert_alive () 2875feed ( 'h' ) 2876assert_alive () 2877feed ( 'i' ) 2878assert_alive () 2879end ) 2880 2881it ( "'inccommand' cannot be changed during preview #23136" , function () 2882clear () 2883local screen =Screen . new ( 30 , 6 ) 2884common_setup ( screen , 'nosplit' , 'foo\nbar\nbaz' ) 2885source ( [[ 2886function! IncCommandToggle() 2887let prev = &inccommand 2888 2889if &inccommand ==# 'split' 2890set inccommand=nosplit 2891elseif &inccommand ==# 'nosplit' 2892set inccommand=split 2893elseif &inccommand ==# '' 2894set inccommand=nosplit 2895else 2896throw 'unknown inccommand' 2897endif 2898 2899return " \<BS>" 2900endfun 2901 2902cnoremap <expr> <C-E> IncCommandToggle() 2903]] ) 2904 2905feed ( ':%s/foo/bar<C-E><C-E><C-E>' ) 2906assert_alive () 2907end ) 2908 2909it ( "'inccommand' value can be changed multiple times #27086" , function () 2910clear () 2911local screen =Screen . new ( 30 , 20 ) 2912common_setup ( screen , 'split' , 'foo1\nfoo2\nfoo3' ) 2913for _ =1 , 3 do 2914feed ( ':%s/foo/bar' ) 2915screen : expect ( [[ 2916{20:bar}1 | 2917{20:bar}2 | 2918{20:bar}3 | 2919{1:~ }|*7 2920{3:[No Name] [+] }| 2921|1| {20:bar}1 | 2922|2| {20:bar}2 | 2923|3| {20:bar}3 | 2924{1:~ }|*4 2925{2:[Preview] }| 2926:%s/foo/bar^ | 2927]] ) 2928feed ( '<Esc>' ) 2929command ( 'set inccommand=nosplit' ) 2930feed ( ':%s/foo/bar' ) 2931screen : expect ( [[ 2932{20:bar}1 | 2933{20:bar}2 | 2934{20:bar}3 | 2935{1:~ }|*16 2936:%s/foo/bar^ | 2937]] ) 2938feed ( '<Esc>' ) 2939command ( 'set inccommand=split' ) 2940end 2941end ) 2942 2943it ( "'inccommand' disables preview if preview buffer can't be created #27086" , function () 2944clear () 2945api . nvim_buf_set_name ( 0 , '[Preview]' ) 2946local screen =Screen . new ( 30 , 20 ) 2947common_setup ( screen , 'split' , 'foo1\nfoo2\nfoo3' ) 2948eq ( 'split' , api . nvim_get_option_value ( 'inccommand' , {} )) 2949feed ( ':%s/foo/bar' ) 2950screen : expect ( [[ 2951{20:bar}1 | 2952{20:bar}2 | 2953{20:bar}3 | 2954{1:~ }|*16 2955:%s/foo/bar^ | 2956]] ) 2957eq ( 'nosplit' , api . nvim_get_option_value ( 'inccommand' , {} )) 2958end ) 2959 2960it ( "'inccommand' :substitute preview skips input() prompt #11940" , function () 2961clear () 2962local screen =Screen . new ( 30 , 3 ) 2963common_setup ( screen , 'split' , 'foo' ) 2964feed ( [[:s/f/\=input("sub: ")]] ) 2965screen : expect ( [[ 2966oo | 2967{1:~ }| 2968:s/f/\=input("sub: ")^ | 2969]] ) 2970end )