robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
61f166ec40
master
1" Tests for the various 'formatoptions' settings 2 3source check.vim 4 5func Test_text_format () 6enew ! 7 8setl noai tw = 2 fo = t 9call append ( '$' , [ 10 \'{' , 11 \' ' , 12 \'' , 13 \'}' ]) 14exe "normal /^{/+1\n0" 15normal gRa b 16let lnum = line ( '.' ) 17call assert_equal ([ 18 \'a' , 19 \'b' ], getline ( lnum - 1 , lnum )) 20 21normal ggdG 22setl ai tw = 2 fo = tw 23call append ( '$' , [ 24 \'{' , 25 \'a b ' , 26 \'' , 27 \'a ' , 28 \'}' ]) 29exe "normal /^{/+1\n0" 30normal gqgqjjllab 31let lnum = line ( '.' ) 32call assert_equal ([ 33 \'a ' , 34 \'b ' , 35 \'' , 36 \'a ' , 37 \'b' ], getline ( lnum - 4 , lnum )) 38 39normal ggdG 40setl tw = 3 fo = t 41call append ( '$' , [ 42 \'{' , 43 \"a \<C-A>" , 44 \'}' ]) 45exe "normal /^{/+1\n0" 46exe "normal gqgqo\na \<C-V>\<C-A>" 47let lnum = line ( '.' ) 48call assert_equal ([ 49 \'a' , 50 \"\<C-A>" , 51 \'' , 52 \'a' , 53 \"\<C-A>" ], getline ( lnum - 4 , lnum )) 54 55normal ggdG 56setl tw = 2 fo = tcq1comments = :# 57call append ( '$' , [ 58 \'{' , 59 \'a b' , 60 \'#a b' , 61 \'}' ]) 62exe "normal /^{/+1\n0" 63exe "normal gqgqjgqgqo\na b\n#a b" 64let lnum = line ( '.' ) 65call assert_equal ([ 66 \'a b' , 67 \'#a b' , 68 \'' , 69 \'a b' , 70 \'#a b' ], getline ( lnum - 4 , lnum )) 71 72normal ggdG 73setl tw = 5 fo = tcncomments = :# 74call append ( '$' , [ 75 \'{' , 76 \' 1 a' , 77 \'# 1 a' , 78 \'}' ]) 79exe "normal /^{/+1\n0" 80exe "normal A b\<Esc>jA b" 81let lnum = line ( '.' ) 82call assert_equal ([ 83 \' 1 a' , 84 \' b' , 85 \'# 1 a' , 86 \'# b' ], getline ( lnum - 3 , lnum )) 87 88normal ggdG 89setl tw = 5 fo = t2asi 90call append ( '$' , [ 91 \'{' , 92 \'' , 93 \' x a' , 94 \' b' , 95 \' c' , 96 \'' , 97 \'}' ]) 98exe "normal /^{/+3\n0" 99exe "normal i \<Esc>A_" 100let lnum = line ( '.' ) 101call assert_equal ([ 102 \'' , 103 \' x a' , 104 \' b_' , 105 \' c' , 106 \'' ], getline ( lnum - 2 , lnum + 2 )) 107 108normal ggdG 109setl tw = 5 fo = qncomments = :# 110call append ( '$' , [ 111 \'{' , 112 \'# 1 a b' , 113 \'}' ]) 114exe "normal /^{/+1\n5|" 115normal gwap 116call assert_equal ( 5 , col ( '.' )) 117let lnum = line ( '.' ) 118call assert_equal ([ 119 \'# 1 a' , 120 \'# b' ], getline ( lnum , lnum + 1 )) 121 122normal ggdG 123setl tw = 5 fo = q2comments = :# 124call append ( '$' , [ 125 \'{' , 126 \'# x' , 127 \'# a b' , 128 \'}' ]) 129exe "normal /^{/+1\n0" 130normal gwap 131let lnum = line ( '.' ) 132call assert_equal ([ 133 \'# x a' , 134 \'# b' ], getline ( lnum , lnum + 1 )) 135 136normal ggdG 137setl tw & fo = a 138call append ( '$' , [ 139 \'{' , 140 \' 1aa' , 141 \' 2bb' , 142 \'}' ]) 143exe "normal /^{/+2\n0" 144normal I^^ 145call assert_equal ( '{ 1aa ^^2bb }' , getline ( '.' )) 146 147normal ggdG 148setl tw = 20 fo = an12wcqcomments = s1:/*,mb:*,ex:*/ 149call append ( '$' , [ 150 \'/* abc def ghi jkl ' , 151 \' * mno pqr stu' , 152 \' */' ]) 153exe "normal /mno pqr/\n" 154normal A vwx yz 155let lnum = line ( '.' ) 156call assert_equal ([ 157 \' * mno pqr stu ' , 158 \' * vwx yz' , 159 \' */' ], getline ( lnum - 1 , lnum + 1 )) 160 161normal ggdG 162setl tw = 12 fo = tqnccomments = :# 163call setline ( '.' , '# 1 xxxxx' ) 164normal A foobar 165call assert_equal ([ 166 \'# 1 xxxxx' , 167 \'# foobar' ], getline ( 1 , 2 )) 168 169" Test the 'p' flag for 'formatoptions' 170" First test without the flag: that it will break "Mr. Feynman" at the space 171normal ggdG 172setl tw = 28 fo = tcq 173call setline ( '.' , 'Surely you''re joking, Mr. Feynman!' ) 174normal gqq 175call assert_equal ([ 176 \'Surely you''re joking, Mr.' , 177 \'Feynman!' ], getline ( 1 , 2 )) 178" Now test with the flag: that it will push the name with the title onto the 179" next line 180normal ggdG 181setl fo += p 182call setline ( '.' , 'Surely you''re joking, Mr. Feynman!' ) 183normal gqq 184call assert_equal ([ 185 \'Surely you''re joking,' , 186 \'Mr. Feynman!' ], getline ( 1 , 2 )) 187" Ensure that it will still break if two spaces are entered 188normal ggdG 189call setline ( '.' , 'Surely you''re joking, Mr. Feynman!' ) 190normal gqq 191call assert_equal ([ 192 \'Surely you''re joking, Mr.' , 193 \'Feynman!' ], getline ( 1 , 2 )) 194 195setl ai & tw & fo & si & comments & 196enew ! 197endfunc 198 199func Test_format_c_comment () 200new 201setl ai cindent tw = 40 et fo = croql 202let text =<< trim END 203var = 2345; // asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf 204END 205call setline ( 1 , text ) 206normal gql 207let expected =<< trim END 208var = 2345; // asdf asdf asdf asdf asdf 209// asdf asdf asdf asdf asdf 210END 211call assert_equal ( expected , getline ( 1 , '$' )) 212 213 %del 214let text =<< trim END 215var = 2345; // asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf 216END 217call setline ( 1 , text ) 218normal gql 219let expected =<< trim END 220var = 2345; // asdf asdf asdf asdf asdf 221// asdf asdf asdf asdf asdf 222// asdf asdf 223END 224call assert_equal ( expected , getline ( 1 , '$' )) 225 226 %del 227let text =<< trim END 228#if 0 // This is another long end of 229// line comment that 230// wraps. 231END 232call setline ( 1 , text ) 233normal gq2j 234let expected =<< trim END 235#if 0 // This is another long 236// end of line comment 237// that wraps. 238END 239call assert_equal ( expected , getline ( 1 , '$' )) 240 241" Using either "o" or "O" repeats a line comment occupying a whole line. 242 %del 243let text =<< trim END 244nop; 245// This is a comment 246val = val; 247END 248call setline ( 1 , text ) 249normal 2Go 250let expected =<< trim END 251nop; 252// This is a comment 253// 254val = val; 255END 256call assert_equal ( expected , getline ( 1 , '$' )) 257normal 2GO 258let expected =<< trim END 259nop; 260// 261// This is a comment 262// 263val = val; 264END 265call assert_equal ( expected , getline ( 1 , '$' )) 266 267" Using "o" repeats a line comment after a statement, "O" does not. 268 %del 269let text =<< trim END 270nop; 271val = val; // This is a comment 272END 273call setline ( 1 , text ) 274normal 2Go 275let expected =<< trim END 276nop; 277val = val; // This is a comment 278// 279END 280call assert_equal ( expected , getline ( 1 , '$' )) 2813 delete 282 283" No comment repeated with a slash in 'formatoptions' 284set fo += / 285normal 2Gox 286let expected =<< trim END 287nop; 288val = val; // This is a comment 289x 290END 291call assert_equal ( expected , getline ( 1 , '$' )) 292 293" Comment is formatted when it wraps 294normal 2GA with some more text added 295let expected =<< trim END 296nop; 297val = val; // This is a comment 298// with some more text 299// added 300x 301END 302call assert_equal ( expected , getline ( 1 , '$' )) 303 304set fo -= / 305 306" using 'indentexpr' instead of 'cindent' does not repeat a comment 307setl nocindent indentexpr = 2 308 %del 309let text =<< trim END 310nop; 311val = val; // This is a comment 312END 313call setline ( 1 , text ) 314normal 2Gox 315let expected =<< trim END 316nop; 317val = val; // This is a comment 318x 319END 320call assert_equal ( expected , getline ( 1 , '$' )) 321setl cindent indentexpr = 3223 delete 323 324normal 2GO 325let expected =<< trim END 326nop; 327 328val = val; // This is a comment 329END 330call assert_equal ( expected , getline ( 1 , '$' )) 331 332" Using "o" does not repeat a comment in a string 333 %del 334let text =<< trim END 335nop; 336val = " // This is not a comment"; 337END 338call setline ( 1 , text ) 339normal 2Gox 340let expected =<< trim END 341nop; 342val = " // This is not a comment"; 343x 344END 345call assert_equal ( expected , getline ( 1 , '$' )) 346 347" Using CTRL-U after "o" fixes the indent 348 %del 349let text =<< trim END 350{ 351val = val; // This is a comment 352END 353call setline ( 1 , text ) 354exe "normal! 2Go\<C-U>x\<Esc>" 355let expected =<< trim END 356{ 357val = val; // This is a comment 358x 359END 360call assert_equal ( expected , getline ( 1 , '$' )) 361 362" typing comment text auto-wraps 363 %del 364call setline ( 1 , text ) 365exe "normal! 2GA blah more text blah.\<Esc>" 366let expected =<< trim END 367{ 368val = val; // This is a comment 369// blah more text 370// blah. 371END 372call assert_equal ( expected , getline ( 1 , '$' )) 373 374bwipe ! 375endfunc 376 377" Tests for :right, :center and :left on text with embedded TAB. 378func Test_format_align () 379enew ! 380set tw = 65 381 382" :left alignment 383call append ( 0 , [ 384 \" test for :left" , 385 \" a a" , 386 \" fa a" , 387 \" dfa a" , 388 \" sdfa a" , 389 \" asdfa a" , 390 \" xasdfa a" , 391 \"asxxdfa a" , 392 \]) 393 %left 394call assert_equal ([ 395 \"test for :left" , 396 \"a a" , 397 \"fa a" , 398 \"dfa a" , 399 \"sdfa a" , 400 \"asdfa a" , 401 \"xasdfa a" , 402 \"asxxdfa a" , 403 \"" 404 \], getline ( 1 , '$' )) 405enew ! 406 407" :center alignment 408call append ( 0 , [ 409 \" test for :center" , 410 \" a a" , 411 \" fa afd asdf" , 412 \" dfa a" , 413 \" sdfa afd asdf" , 414 \" asdfa a" , 415 \" xasdfa asdfasdfasdfasdfasdf" , 416 \"asxxdfa a" 417 \]) 418 %center 419call assert_equal ([ 420 \" test for :center" , 421 \" a a" , 422 \" fa afd asdf" , 423 \" dfa a" , 424 \" sdfa afd asdf" , 425 \" asdfa a" , 426 \" xasdfa asdfasdfasdfasdfasdf" , 427 \" asxxdfa a" , 428 \"" 429 \], getline ( 1 , '$' )) 430enew ! 431 432" :right alignment 433call append ( 0 , [ 434 \" test for :right" , 435 \" a a" , 436 \" fa a" , 437 \" dfa a" , 438 \" sdfa a" , 439 \" asdfa a" , 440 \" xasdfa a" , 441 \" asxxdfa a" , 442 \" asxa;ofa a" , 443 \" asdfaqwer a" , 444 \" a ax" , 445 \" fa ax" , 446 \" dfa ax" , 447 \" sdfa ax" , 448 \" asdfa ax" , 449 \" xasdfa ax" , 450 \" asxxdfa ax" , 451 \" asxa;ofa ax" , 452 \" asdfaqwer ax" , 453 \" a axx" , 454 \" fa axx" , 455 \" dfa axx" , 456 \" sdfa axx" , 457 \" asdfa axx" , 458 \" xasdfa axx" , 459 \" asxxdfa axx" , 460 \" asxa;ofa axx" , 461 \" asdfaqwer axx" , 462 \" a axxx" , 463 \" fa axxx" , 464 \" dfa axxx" , 465 \" sdfa axxx" , 466 \" asdfa axxx" , 467 \" xasdfa axxx" , 468 \" asxxdfa axxx" , 469 \" asxa;ofa axxx" , 470 \" asdfaqwer axxx" , 471 \" a axxxo" , 472 \" fa axxxo" , 473 \" dfa axxxo" , 474 \" sdfa axxxo" , 475 \" asdfa axxxo" , 476 \" xasdfa axxxo" , 477 \" asxxdfa axxxo" , 478 \" asxa;ofa axxxo" , 479 \" asdfaqwer axxxo" , 480 \" a axxxoi" , 481 \" fa axxxoi" , 482 \" dfa axxxoi" , 483 \" sdfa axxxoi" , 484 \" asdfa axxxoi" , 485 \" xasdfa axxxoi" , 486 \" asxxdfa axxxoi" , 487 \" asxa;ofa axxxoi" , 488 \" asdfaqwer axxxoi" , 489 \" a axxxoik" , 490 \" fa axxxoik" , 491 \" dfa axxxoik" , 492 \" sdfa axxxoik" , 493 \" asdfa axxxoik" , 494 \" xasdfa axxxoik" , 495 \" asxxdfa axxxoik" , 496 \" asxa;ofa axxxoik" , 497 \" asdfaqwer axxxoik" , 498 \" a axxxoike" , 499 \" fa axxxoike" , 500 \" dfa axxxoike" , 501 \" sdfa axxxoike" , 502 \" asdfa axxxoike" , 503 \" xasdfa axxxoike" , 504 \" asxxdfa axxxoike" , 505 \" asxa;ofa axxxoike" , 506 \" asdfaqwer axxxoike" , 507 \" a axxxoikey" , 508 \" fa axxxoikey" , 509 \" dfa axxxoikey" , 510 \" sdfa axxxoikey" , 511 \" asdfa axxxoikey" , 512 \" xasdfa axxxoikey" , 513 \" asxxdfa axxxoikey" , 514 \" asxa;ofa axxxoikey" , 515 \" asdfaqwer axxxoikey" , 516 \]) 517 %right 518call assert_equal ([ 519 \"\t\t\t\t test for :right" , 520 \"\t\t\t\t a a" , 521 \"\t\t\t\t fa a" , 522 \"\t\t\t\t dfa a" , 523 \"\t\t\t\t sdfa a" , 524 \"\t\t\t\t asdfa a" , 525 \"\t\t\t\t xasdfa a" , 526 \"\t\t\t\t asxxdfa a" , 527 \"\t\t\t\t asxa;ofa a" , 528 \"\t\t\t\t asdfaqwer a" , 529 \"\t\t\t\t a ax" , 530 \"\t\t\t\t fa ax" , 531 \"\t\t\t\t dfa ax" , 532 \"\t\t\t\t sdfa ax" , 533 \"\t\t\t\t asdfa ax" , 534 \"\t\t\t\t xasdfa ax" , 535 \"\t\t\t\t asxxdfa ax" , 536 \"\t\t\t\t asxa;ofa ax" , 537 \"\t\t\t\t asdfaqwer ax" , 538 \"\t\t\t\t a axx" , 539 \"\t\t\t\t fa axx" , 540 \"\t\t\t\t dfa axx" , 541 \"\t\t\t\t sdfa axx" , 542 \"\t\t\t\t asdfa axx" , 543 \"\t\t\t\t xasdfa axx" , 544 \"\t\t\t\t asxxdfa axx" , 545 \"\t\t\t\t asxa;ofa axx" , 546 \"\t\t\t\t asdfaqwer axx" , 547 \"\t\t\t\t a axxx" , 548 \"\t\t\t\t fa axxx" , 549 \"\t\t\t\t dfa axxx" , 550 \"\t\t\t\t sdfa axxx" , 551 \"\t\t\t\t asdfa axxx" , 552 \"\t\t\t\t xasdfa axxx" , 553 \"\t\t\t\t asxxdfa axxx" , 554 \"\t\t\t\t asxa;ofa axxx" , 555 \"\t\t\t\t asdfaqwer axxx" , 556 \"\t\t\t\t a axxxo" , 557 \"\t\t\t\t fa axxxo" , 558 \"\t\t\t\t dfa axxxo" , 559 \"\t\t\t\t sdfa axxxo" , 560 \"\t\t\t\t asdfa axxxo" , 561 \"\t\t\t\t xasdfa axxxo" , 562 \"\t\t\t\t asxxdfa axxxo" , 563 \"\t\t\t\t asxa;ofa axxxo" , 564 \"\t\t\t\t asdfaqwer axxxo" , 565 \"\t\t\t\t a axxxoi" , 566 \"\t\t\t\t fa axxxoi" , 567 \"\t\t\t\t dfa axxxoi" , 568 \"\t\t\t\t sdfa axxxoi" , 569 \"\t\t\t\t asdfa axxxoi" , 570 \"\t\t\t\t xasdfa axxxoi" , 571 \"\t\t\t\t asxxdfa axxxoi" , 572 \"\t\t\t\t asxa;ofa axxxoi" , 573 \"\t\t\t\t asdfaqwer axxxoi" , 574 \"\t\t\t\t a axxxoik" , 575 \"\t\t\t\t fa axxxoik" , 576 \"\t\t\t\t dfa axxxoik" , 577 \"\t\t\t\t sdfa axxxoik" , 578 \"\t\t\t\t asdfa axxxoik" , 579 \"\t\t\t\t xasdfa axxxoik" , 580 \"\t\t\t\t asxxdfa axxxoik" , 581 \"\t\t\t\t asxa;ofa axxxoik" , 582 \"\t\t\t\t asdfaqwer axxxoik" , 583 \"\t\t\t\t a axxxoike" , 584 \"\t\t\t\t fa axxxoike" , 585 \"\t\t\t\t dfa axxxoike" , 586 \"\t\t\t\t sdfa axxxoike" , 587 \"\t\t\t\t asdfa axxxoike" , 588 \"\t\t\t\t xasdfa axxxoike" , 589 \"\t\t\t\t asxxdfa axxxoike" , 590 \"\t\t\t\t asxa;ofa axxxoike" , 591 \"\t\t\t\t asdfaqwer axxxoike" , 592 \"\t\t\t\t a axxxoikey" , 593 \"\t\t\t\t fa axxxoikey" , 594 \"\t\t\t\t dfa axxxoikey" , 595 \"\t\t\t\t sdfa axxxoikey" , 596 \"\t\t\t\t asdfa axxxoikey" , 597 \"\t\t\t\t xasdfa axxxoikey" , 598 \"\t\t\t\t asxxdfa axxxoikey" , 599 \"\t\t\t\t asxa;ofa axxxoikey" , 600 \"\t\t\t\t asdfaqwer axxxoikey" , 601 \"" 602 \], getline ( 1 , '$' )) 603enew ! 604 605" align text with 'wrapmargin' 60650 vnew 607call setline ( 1 , [ 'Vim' ]) 608setl textwidth = 0 609setl wrapmargin = 30 610right 611call assert_equal ( "\t\t Vim" , getline ( 1 )) 612q ! 613 614" align text with 'rightleft' 615if has ( 'rightleft' ) 616new 617call setline ( 1 , 'Vim' ) 618setlocal rightleft 619left 20 620setlocal norightleft 621call assert_equal ( "\t\t Vim" , getline ( 1 )) 622setlocal rightleft 623right 624setlocal norightleft 625call assert_equal ( "Vim" , getline ( 1 )) 626bw ! 627endif 628 629set tw & 630endfunc 631 632" Test formatting a paragraph. 633func Test_format_para () 634enew ! 635set fo += tcroqltw = 72 636 637call append ( 0 , [ 638 \"xxxxx xx xxxxxx " , 639 \"xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx" , 640 \"xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx" , 641 \"xx xxxxxxx. xxxx xxxx." , 642 \"" , 643 \"> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx" , 644 \"> xxxxxx xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx" 645 \]) 646exe "normal /xxxxxxxx$\<CR>" 647normal 0gq6kk 648call assert_equal ([ 649 \"xxxxx xx xxxxxx xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx" , 650 \"xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx xx xxxxxxx." , 651 \"xxxx xxxx." , 652 \"" , 653 \"> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx xxxxxx" , 654 \"> xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx" , 655 \"" 656 \], getline ( 1 , '$' )) 657 658set fo & tw & 659enew ! 660endfunc 661 662" Test undo after ":%s" and formatting. 663func Test_format_undo () 664enew ! 665map gg :.,.+2s/^/x/< CR > kk:set tw=3< CR > gqq 666 667call append ( 0 , [ 668 \"aa aa aa aa" , 669 \"bb bb bb bb" , 670 \"cc cc cc cc" 671 \]) 672" undo/redo here to make the next undo only work on the following changes 673exe "normal i\<C-G>u" 674call cursor ( 1 , 1 ) 675normal ggu 676call assert_equal ([ 677 \"aa aa aa aa" , 678 \"bb bb bb bb" , 679 \"cc cc cc cc" , 680 \"" 681 \], getline ( 1 , '$' )) 682 683unmap gg 684set tw & 685enew ! 686endfunc 687 688func Test_format_list_auto () 689new 690call setline ( 1 , [ '1. abc' , '2. def' , '3. ghi' ]) 691set fo = tanai bs = 2 692call feedkeys ( "3G0lli\<BS>\<BS>x\<Esc>" , 'tx' ) 693call assert_equal ( '2. defx ghi' , getline ( 2 )) 694bwipe ! 695set fo & ai & bs & 696endfunc 697 698func Test_crash_github_issue_5095 () 699CheckFeature autocmd 700 701" This used to segfault, see https://github.com/vim/vim/issues/5095 702augroup testing 703au BufNew x center 704augroup END 705 706next ! x 707 708bw 709augroup testing 710au ! 711augroup END 712augroup ! testing 713endfunc 714 715" Test for formatting multi-byte text with 'fo=t' 716func Test_tw_2_fo_t () 717new 718let t =<< trim END 719{ 720XYZ 721abc XYZ 722} 723END 724call setline ( 1 , t ) 725call cursor ( 2 , 1 ) 726 727set tw = 2 fo = t 728let t =<< trim END 729XYZ 730abc XYZ 731END 732exe "normal gqgqjgqgq" 733exe "normal o\n" . join ( t , "\n" ) 734 735let expected =<< trim END 736{ 737XYZ 738abc 739XYZ 740 741XYZ 742abc 743XYZ 744} 745END 746call assert_equal ( expected , getline ( 1 , '$' )) 747 748set tw & fo & 749bwipe ! 750endfunc 751 752" Test for formatting multi-byte text with 'fo=tm' and 'tw=1' 753func Test_tw_1_fo_tm () 754new 755let t =<< trim END 756{ 757X 758Xa 759X a 760XY 761X Y 762} 763END 764call setline ( 1 , t ) 765call cursor ( 2 , 1 ) 766 767set tw = 1 fo = tm 768let t =<< trim END 769X 770Xa 771X a 772XY 773X Y 774END 775exe "normal gqgqjgqgqjgqgqjgqgqjgqgq" 776exe "normal o\n" . join ( t , "\n" ) 777 778let expected =<< trim END 779{ 780X 781X 782a 783X 784a 785X 786Y 787X 788Y 789 790X 791X 792a 793X 794a 795X 796Y 797X 798Y 799} 800END 801call assert_equal ( expected , getline ( 1 , '$' )) 802 803set tw & fo & 804bwipe ! 805endfunc 806 807" Test for formatting multi-byte text with 'fo=tm' and 'tw=2' 808func Test_tw_2_fo_tm () 809new 810let t =<< trim END 811{ 812X 813Xa 814X a 815XY 816X Y 817aX 818abX 819abcX 820abX c 821abXY 822} 823END 824call setline ( 1 , t ) 825call cursor ( 2 , 1 ) 826 827set tw = 2 fo = tm 828let t =<< trim END 829X 830Xa 831X a 832XY 833X Y 834aX 835abX 836abcX 837abX c 838abXY 839END 840exe "normal gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgq" 841exe "normal o\n" . join ( t , "\n" ) 842 843let expected =<< trim END 844{ 845X 846X 847a 848X 849a 850X 851Y 852X 853Y 854a 855X 856ab 857X 858abc 859X 860ab 861X 862c 863ab 864X 865Y 866 867X 868X 869a 870X 871a 872X 873Y 874X 875Y 876a 877X 878ab 879X 880abc 881X 882ab 883X 884c 885ab 886X 887Y 888} 889END 890call assert_equal ( expected , getline ( 1 , '$' )) 891 892set tw & fo & 893bwipe ! 894endfunc 895 896" Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'autoindent'. 897func Test_tw_2_fo_tm_ai () 898new 899let t =<< trim END 900{ 901X 902Xa 903} 904END 905call setline ( 1 , t ) 906call cursor ( 2 , 1 ) 907 908set ai tw = 2 fo = tm 909let t =<< trim END 910X 911Xa 912END 913exe "normal gqgqjgqgq" 914exe "normal o\n" . join ( t , "\n" ) 915 916let expected =<< trim END 917{ 918X 919X 920a 921 922X 923X 924a 925} 926END 927call assert_equal ( expected , getline ( 1 , '$' )) 928 929set tw & fo & ai & 930bwipe ! 931endfunc 932 933" Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'noai'. 934func Test_tw_2_fo_tm_noai () 935new 936let t =<< trim END 937{ 938X 939Xa 940} 941END 942call setline ( 1 , t ) 943call cursor ( 2 , 1 ) 944 945set noai tw = 2 fo = tm 946exe "normal gqgqjgqgqo\n X\n Xa" 947 948let expected =<< trim END 949{ 950X 951X 952a 953 954X 955X 956a 957} 958END 959call assert_equal ( expected , getline ( 1 , '$' )) 960 961set tw & fo & ai & 962bwipe ! 963endfunc 964 965func Test_tw_2_fo_tm_replace () 966new 967let t =<< trim END 968{ 969 970} 971END 972call setline ( 1 , t ) 973call cursor ( 2 , 1 ) 974 975set tw = 2 fo = tm 976exe "normal RXa" 977 978let expected =<< trim END 979{ 980X 981a 982} 983END 984call assert_equal ( expected , getline ( 1 , '$' )) 985 986set tw & fo & 987bwipe ! 988endfunc 989 990" Test for 'matchpairs' with multibyte chars 991func Test_mps_multibyte () 992new 993let t =<< trim END 994{ 995‘ two three ’ four 996} 997END 998call setline ( 1 , t ) 999call cursor ( 2 , 1 ) 1000 1001exe "set mps+=\u2018:\u2019" 1002normal d% 1003 1004let expected =<< trim END 1005{ 1006four 1007} 1008END 1009call assert_equal ( expected , getline ( 1 , '$' )) 1010 1011set mps & 1012bwipe ! 1013endfunc 1014 1015" Test for 'matchpairs' in latin1 encoding 1016func Test_mps_latin1 () 1017new 1018let save_enc = & encoding 1019" set encoding=latin1 1020call setline ( 1 , 'abc(def)ghi' ) 1021normal % 1022call assert_equal ( 8 , col ( '.' )) 1023normal % 1024call assert_equal ( 4 , col ( '.' )) 1025call cursor ( 1 , 6 ) 1026normal [( 1027call assert_equal ( 4 , col ( '.' )) 1028normal % 1029call assert_equal ( 8 , col ( '.' )) 1030call cursor ( 1 , 6 ) 1031normal ]) 1032call assert_equal ( 8 , col ( '.' )) 1033normal % 1034call assert_equal ( 4 , col ( '.' )) 1035let & encoding = save_enc 1036bw ! 1037endfunc 1038 1039func Test_empty_matchpairs () 1040split 1041set matchpairs = showmatch 1042call assert_nobeep ( 'call feedkeys("ax\tx\t\<Esc>", "xt")' ) 1043set matchpairs & noshowmatch 1044bwipe ! 1045endfunc 1046 1047func Test_mps_error () 1048let encoding_save = & encoding 1049 1050" for e in ['utf-8', 'latin1'] 1051for e in [ 'utf-8' ] 1052exe 'set encoding=' .. e 1053 1054call assert_fails ( 'set mps=<:' , 'E474:' , e ) 1055call assert_fails ( 'set mps=:>' , 'E474:' , e ) 1056call assert_fails ( 'set mps=<>' , 'E474:' , e ) 1057call assert_fails ( 'set mps=<:>_' , 'E474:' , e ) 1058endfor 1059 1060let & encoding = encoding_save 1061endfunc 1062 1063" Test for ra on multi-byte characters 1064func Test_ra_multibyte () 1065new 1066let t =<< trim END 1067ra test 1068abba 1069aab 1070END 1071call setline ( 1 , t ) 1072call cursor ( 1 , 1 ) 1073 1074normal jVjra 1075 1076let expected =<< trim END 1077ra test 1078aaaa 1079aaa 1080END 1081call assert_equal ( expected , getline ( 1 , '$' )) 1082 1083bwipe ! 1084endfunc 1085 1086" Test for 'whichwrap' with multi-byte character 1087func Test_whichwrap_multi_byte () 1088new 1089let t =<< trim END 1090á 1091x 1092END 1093call setline ( 1 , t ) 1094call cursor ( 2 , 1 ) 1095 1096set whichwrap += h 1097normal dh 1098set whichwrap & 1099 1100let expected =<< trim END 1101áx 1102END 1103call assert_equal ( expected , getline ( 1 , '$' )) 1104 1105bwipe ! 1106endfunc 1107 1108" Test for 'a' and 'w' flags in 'formatoptions' 1109func Test_fo_a_w () 1110new 1111setlocal fo += awtw = 10 1112call feedkeys ( "iabc abc a abc\<Esc>k0weade" , 'xt' ) 1113call assert_equal ([ 'abc abcde ' , 'a abc' ], getline ( 1 , '$' )) 1114 1115" when a line ends with space, it is not broken up. 1116 %d 1117call feedkeys ( "ione two to " , 'xt' ) 1118call assert_equal ( 'one two to ' , getline ( 1 )) 1119 1120" when a line ends with spaces and backspace is used in the next line, the 1121" last space in the previous line should be removed. 1122 %d 1123set backspace = indent,eol,start 1124call setline ( 1 , [ 'one ' , 'two' ]) 1125exe "normal 2Gi\<BS>" 1126call assert_equal ([ 'one two' ], getline ( 1 , '$' )) 1127set backspace & 1128 1129" Test for 'a', 'w' and '1' options. 1130setlocal textwidth = 0 1131setlocal fo = 1aw 1132 %d 1133call setline ( 1 , '. foo' ) 1134normal 72ig 1135call feedkeys ( 'a uu uu uu' , 'xt' ) 1136call assert_equal ( 'g uu uu ' , getline ( 1 )[ -8 :]) 1137call assert_equal ([ 'uu. foo' ], getline ( 2 , '$' )) 1138 1139" using backspace or "x" triggers reformat 1140call setline ( 1 , [ '1 2 3 4 5 ' , '6 7 8 9' ]) 1141set tw = 10 1142set fo = taw 1143set bs = indent,eol,start 1144exe "normal 1G4la\<BS>\<BS>\<Esc>" 1145call assert_equal ([ '1 2 4 5 6 ' , '7 8 9' ], getline ( 1 , 2 )) 1146exe "normal f4xx" 1147call assert_equal ([ '1 2 5 6 7 ' , '8 9' ], getline ( 1 , 2 )) 1148 1149" using "cw" leaves cursor in right spot 1150call setline ( 1 , [ 'Now we g whether that nation, or' , 1151 \'any nation so conceived and,' ]) 1152set fo = tcqatw = 35 1153exe "normal 2G0cwx\<Esc>" 1154call assert_equal ([ 'Now we g whether that nation, or x' , 'nation so conceived and,' ], getline ( 1 , 2 )) 1155 1156set tw = 0 1157set fo & 1158 %bw ! 1159endfunc 1160 1161" Test that auto-format ('a' flag) preserves spaces typed in the middle of a line 1162func Test_fo_a_midline_space () 1163new 1164let lines = [ 1165 \'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa it but' , 1166 \'Lorem Ipsum is simply dummy text of the printing and typesetting dasddd' , 1167 \'industry.' , 1168 \] 1169call setline ( 1 , lines ) 1170set fo = tatw = 70 1171 1172" Prevent INPUT_BUFLEN batching so auto_format runs between keystrokes 1173autocmd InsertCharPre * " nothing 1174 1175" Position at 't' of 'it' (col 68) and type space then Z 1176call cursor ( 1 , 68 ) 1177call feedkeys ( "a Z\<Esc>" , 'xt' ) 1178 1179" The space between 'it' and 'Z' must be preserved 1180call assert_match ( 'it Z' , getline ( 1 )) 1181 1182autocmd ! InsertCharPre 1183set fo & tw & 1184bw ! 1185endfunc 1186 1187" Test for formatting lines using gq in visual mode 1188func Test_visual_gq_format () 1189new 1190call setline ( 1 , [ 'one two three four' , 'five six' , 'one two' ]) 1191setl textwidth = 10 1192call feedkeys ( 'ggv$jj' , 'xt' ) 1193redraw ! 1194normal gq 1195 %d 1196call setline ( 1 , [ 'one two three four' , 'five six' , 'one two' ]) 1197normal G$ 1198call feedkeys ( 'v0kk' , 'xt' ) 1199redraw ! 1200normal gq 1201setl textwidth & 1202bw ! 1203endfunc 1204 1205" Test for 'n' flag in 'formatoptions' to format numbered lists 1206func Test_fo_n () 1207new 1208setlocal autoindent 1209setlocal textwidth = 12 1210setlocal fo = n 1211call setline ( 1 , [ ' 1) one two three four' , ' 2) two' ]) 1212normal gggqG 1213call assert_equal ([ ' 1) one two' , ' three' , ' four' , ' 2) two' ], 1214 \getline ( 1 , '$' )) 1215bw ! 1216endfunc 1217 1218" Test for 'formatlistpat' option 1219func Test_formatlistpat () 1220new 1221setlocal autoindent 1222setlocal textwidth = 10 1223setlocal fo = n 1224setlocal formatlistpat = ^\\s*-\\s* 1225call setline ( 1 , [ ' - one two three' , ' - two' ]) 1226normal gggqG 1227call assert_equal ([ ' - one' , ' two' , ' three' , ' - two' ], 1228 \getline ( 1 , '$' )) 1229bw ! 1230endfunc 1231 1232" Test for the 'b' and 'v' flags in 'formatoptions' 1233" Text should wrap only if a space character is inserted at or before 1234" 'textwidth' 1235func Test_fo_b () 1236new 1237setlocal textwidth = 20 1238 1239setlocal formatoptions = t 1240call setline ( 1 , 'one two three four' ) 1241call feedkeys ( 'Amore' , 'xt' ) 1242call assert_equal ([ 'one two three' , 'fourmore' ], getline ( 1 , '$' )) 1243 1244setlocal formatoptions = bt 1245 %d 1246call setline ( 1 , 'one two three four' ) 1247call feedkeys ( 'Amore five' , 'xt' ) 1248call assert_equal ([ 'one two three fourmore five' ], getline ( 1 , '$' )) 1249 1250setlocal formatoptions = bt 1251 %d 1252call setline ( 1 , 'one two three four' ) 1253call feedkeys ( 'A five' , 'xt' ) 1254call assert_equal ([ 'one two three four' , 'five' ], getline ( 1 , '$' )) 1255 1256setlocal formatoptions = vt 1257 %d 1258call setline ( 1 , 'one two three four' ) 1259call feedkeys ( 'Amore five' , 'xt' ) 1260call assert_equal ([ 'one two three fourmore' , 'five' ], getline ( 1 , '$' )) 1261 1262bw ! 1263endfunc 1264 1265" Test for the '1' flag in 'formatoptions'. Don't wrap text after a one letter 1266" word. 1267func Test_fo_1 () 1268new 1269setlocal textwidth = 20 1270 1271setlocal formatoptions = t 1272call setline ( 1 , 'one two three four' ) 1273call feedkeys ( 'A a bird' , 'xt' ) 1274call assert_equal ([ 'one two three four a' , 'bird' ], getline ( 1 , '$' )) 1275 1276 %d 1277setlocal formatoptions = t1 1278call setline ( 1 , 'one two three four' ) 1279call feedkeys ( 'A a bird' , 'xt' ) 1280call assert_equal ([ 'one two three four' , 'a bird' ], getline ( 1 , '$' )) 1281 1282bw ! 1283endfunc 1284 1285" Test for 'l' flag in 'formatoptions'. When starting insert mode, if a line 1286" is longer than 'textwidth', then it is not broken. 1287func Test_fo_l () 1288new 1289setlocal textwidth = 20 1290 1291setlocal formatoptions = t 1292call setline ( 1 , 'one two three four five' ) 1293call feedkeys ( 'A six' , 'xt' ) 1294call assert_equal ([ 'one two three four' , 'five six' ], getline ( 1 , '$' )) 1295 1296 %d 1297setlocal formatoptions = tl 1298call setline ( 1 , 'one two three four five' ) 1299call feedkeys ( 'A six' , 'xt' ) 1300call assert_equal ([ 'one two three four five six' ], getline ( 1 , '$' )) 1301 1302bw ! 1303endfunc 1304 1305" Test for the '2' flag in 'formatoptions' 1306func Test_fo_2 () 1307new 1308setlocal autoindent 1309setlocal formatoptions = t2 1310setlocal textwidth = 30 1311call setline ( 1 , [ "\tfirst line of a paragraph." , 1312 \"second line of the same paragraph." , 1313 \"third line." ]) 1314normal gggqG 1315call assert_equal ([ "\tfirst line of a" , 1316 \"paragraph. second line of the" , 1317 \"same paragraph. third line." ], getline ( 1 , '$' )) 1318bw ! 1319endfunc 1320 1321" This was leaving the cursor after the end of a line. Complicated way to 1322" have the problem show up with valgrind. 1323func Test_correct_cursor_position () 1324" set encoding=iso8859 1325new 1326norm a0000 1327sil ! norm gggg0i0gw0gg 1328 1329bwipe ! 1330set encoding = utf8 1331endfunc 1332 1333" vim: shiftwidth=2 sts=2 expandtab