neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

test_textformat.vim (28299B)


      1 " Tests for the various 'formatoptions' settings
      2 
      3 source check.vim
      4 
      5 func Test_text_format()
      6  enew!
      7 
      8  setl noai tw=2 fo=t
      9  call append('$', [
     10       \ '{',
     11       \ '    ',
     12       \ '',
     13       \ '}'])
     14  exe "normal /^{/+1\n0"
     15  normal gRa b
     16  let lnum = line('.')
     17  call assert_equal([
     18       \ 'a',
     19       \ 'b'], getline(lnum - 1, lnum))
     20 
     21  normal ggdG
     22  setl ai tw=2 fo=tw
     23  call append('$', [
     24       \ '{',
     25       \ 'a  b  ',
     26       \ '',
     27       \ 'a    ',
     28       \ '}'])
     29  exe "normal /^{/+1\n0"
     30  normal gqgqjjllab
     31  let lnum = line('.')
     32  call assert_equal([
     33       \ 'a  ',
     34       \ 'b  ',
     35       \ '',
     36       \ 'a  ',
     37       \ 'b'], getline(lnum - 4, lnum))
     38 
     39  normal ggdG
     40  setl tw=3 fo=t
     41  call append('$', [
     42       \ '{',
     43       \ "a \<C-A>",
     44       \ '}'])
     45  exe "normal /^{/+1\n0"
     46  exe "normal gqgqo\na \<C-V>\<C-A>"
     47  let lnum = line('.')
     48  call assert_equal([
     49       \ 'a',
     50       \ "\<C-A>",
     51       \ '',
     52       \ 'a',
     53       \ "\<C-A>"], getline(lnum - 4, lnum))
     54 
     55  normal ggdG
     56  setl tw=2 fo=tcq1 comments=:#
     57  call append('$', [
     58       \ '{',
     59       \ 'a b',
     60       \ '#a b',
     61       \ '}'])
     62  exe "normal /^{/+1\n0"
     63  exe "normal gqgqjgqgqo\na b\n#a b"
     64  let lnum = line('.')
     65  call assert_equal([
     66       \ 'a b',
     67       \ '#a b',
     68       \ '',
     69       \ 'a b',
     70       \ '#a b'], getline(lnum - 4, lnum))
     71 
     72  normal ggdG
     73  setl tw=5 fo=tcn comments=:#
     74  call append('$', [
     75       \ '{',
     76       \ '  1 a',
     77       \ '# 1 a',
     78       \ '}'])
     79  exe "normal /^{/+1\n0"
     80  exe "normal A b\<Esc>jA b"
     81  let lnum = line('.')
     82  call assert_equal([
     83       \ '  1 a',
     84       \ '    b',
     85       \ '# 1 a',
     86       \ '#   b'], getline(lnum - 3, lnum))
     87 
     88  normal ggdG
     89  setl tw=5 fo=t2a si
     90  call append('$', [
     91       \ '{',
     92       \ '',
     93       \ '  x a',
     94       \ '  b',
     95       \ ' c',
     96       \ '',
     97       \ '}'])
     98  exe "normal /^{/+3\n0"
     99  exe "normal i  \<Esc>A_"
    100  let lnum = line('.')
    101  call assert_equal([
    102       \ '',
    103       \ '  x a',
    104       \ '    b_',
    105       \ '    c',
    106       \ ''], getline(lnum - 2, lnum + 2))
    107 
    108  normal ggdG
    109  setl tw=5 fo=qn comments=:#
    110  call append('$', [
    111       \ '{',
    112       \ '# 1 a b',
    113       \ '}'])
    114  exe "normal /^{/+1\n5|"
    115  normal gwap
    116  call assert_equal(5, col('.'))
    117  let lnum = line('.')
    118  call assert_equal([
    119       \ '# 1 a',
    120       \ '#   b'], getline(lnum, lnum + 1))
    121 
    122  normal ggdG
    123  setl tw=5 fo=q2 comments=:#
    124  call append('$', [
    125       \ '{',
    126       \ '# x',
    127       \ '#   a b',
    128       \ '}'])
    129  exe "normal /^{/+1\n0"
    130  normal gwap
    131  let lnum = line('.')
    132  call assert_equal([
    133       \ '# x a',
    134       \ '#   b'], getline(lnum, lnum + 1))
    135 
    136  normal ggdG
    137  setl tw& fo=a
    138  call append('$', [
    139       \ '{',
    140       \ '   1aa',
    141       \ '   2bb',
    142       \ '}'])
    143  exe "normal /^{/+2\n0"
    144  normal I^^
    145  call assert_equal('{ 1aa ^^2bb }', getline('.'))
    146 
    147  normal ggdG
    148  setl tw=20 fo=an12wcq comments=s1:/*,mb:*,ex:*/
    149  call append('$', [
    150       \ '/* abc def ghi jkl ',
    151       \ ' *    mno pqr stu',
    152       \ ' */'])
    153  exe "normal /mno pqr/\n"
    154  normal A vwx yz
    155  let lnum = line('.')
    156  call assert_equal([
    157       \ ' *    mno pqr stu ',
    158       \ ' *    vwx yz',
    159       \ ' */'], getline(lnum - 1, lnum + 1))
    160 
    161  normal ggdG
    162  setl tw=12 fo=tqnc comments=:#
    163  call setline('.', '# 1 xxxxx')
    164  normal A foobar
    165  call 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
    171  normal ggdG
    172  setl tw=28 fo=tcq
    173  call setline('.', 'Surely you''re joking, Mr. Feynman!')
    174  normal gqq
    175  call 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
    180  normal ggdG
    181  setl fo+=p
    182  call setline('.', 'Surely you''re joking, Mr. Feynman!')
    183  normal gqq
    184  call 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
    188  normal ggdG
    189  call setline('.', 'Surely you''re joking, Mr.  Feynman!')
    190  normal gqq
    191  call assert_equal([
    192              \ 'Surely you''re joking, Mr.',
    193              \ 'Feynman!'], getline(1, 2))
    194 
    195  setl ai& tw& fo& si& comments&
    196  enew!
    197 endfunc
    198 
    199 func Test_format_c_comment()
    200  new
    201  setl ai cindent tw=40 et fo=croql
    202  let text =<< trim END
    203      var = 2345;  // asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf
    204  END
    205  call setline(1, text)
    206  normal gql
    207  let expected =<< trim END
    208      var = 2345;  // asdf asdf asdf asdf asdf
    209                   // asdf asdf asdf asdf asdf
    210  END
    211  call assert_equal(expected, getline(1, '$'))
    212 
    213  %del
    214  let text =<< trim END
    215      var = 2345;  // asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf
    216  END
    217  call setline(1, text)
    218  normal gql
    219  let expected =<< trim END
    220      var = 2345;  // asdf asdf asdf asdf asdf
    221                   // asdf asdf asdf asdf asdf
    222                   // asdf asdf
    223  END
    224  call assert_equal(expected, getline(1, '$'))
    225 
    226  %del
    227  let text =<< trim END
    228      #if 0           // This is another long end of
    229                      // line comment that
    230                      // wraps.
    231  END
    232  call setline(1, text)
    233  normal gq2j
    234  let expected =<< trim END
    235      #if 0           // This is another long
    236                      // end of line comment
    237                      // that wraps.
    238  END
    239  call assert_equal(expected, getline(1, '$'))
    240 
    241  " Using either "o" or "O" repeats a line comment occupying a whole line.
    242  %del
    243  let text =<< trim END
    244      nop;
    245      // This is a comment
    246      val = val;
    247  END
    248  call setline(1, text)
    249  normal 2Go
    250  let expected =<< trim END
    251      nop;
    252      // This is a comment
    253      //
    254      val = val;
    255  END
    256  call assert_equal(expected, getline(1, '$'))
    257  normal 2GO
    258  let expected =<< trim END
    259      nop;
    260      //
    261      // This is a comment
    262      //
    263      val = val;
    264  END
    265  call assert_equal(expected, getline(1, '$'))
    266 
    267  " Using "o" repeats a line comment after a statement, "O" does not.
    268  %del
    269  let text =<< trim END
    270      nop;
    271      val = val;      // This is a comment
    272  END
    273  call setline(1, text)
    274  normal 2Go
    275  let expected =<< trim END
    276      nop;
    277      val = val;      // This is a comment
    278                      //
    279  END
    280  call assert_equal(expected, getline(1, '$'))
    281  3delete
    282 
    283  " No comment repeated with a slash in 'formatoptions'
    284  set fo+=/
    285  normal 2Gox
    286  let expected =<< trim END
    287      nop;
    288      val = val;      // This is a comment
    289      x
    290  END
    291  call assert_equal(expected, getline(1, '$'))
    292 
    293  " Comment is formatted when it wraps
    294  normal 2GA with some more text added
    295  let expected =<< trim END
    296      nop;
    297      val = val;      // This is a comment
    298                      // with some more text
    299                      // added
    300      x
    301  END
    302  call assert_equal(expected, getline(1, '$'))
    303 
    304  set fo-=/
    305 
    306  " using 'indentexpr' instead of 'cindent' does not repeat a comment
    307  setl nocindent indentexpr=2
    308  %del
    309  let text =<< trim END
    310      nop;
    311      val = val;      // This is a comment
    312  END
    313  call setline(1, text)
    314  normal 2Gox
    315  let expected =<< trim END
    316      nop;
    317      val = val;      // This is a comment
    318        x
    319  END
    320  call assert_equal(expected, getline(1, '$'))
    321  setl cindent indentexpr=
    322  3delete
    323 
    324  normal 2GO
    325  let expected =<< trim END
    326      nop;
    327 
    328      val = val;      // This is a comment
    329  END
    330  call assert_equal(expected, getline(1, '$'))
    331 
    332  " Using "o" does not repeat a comment in a string
    333  %del
    334  let text =<< trim END
    335      nop;
    336      val = " // This is not a comment";
    337  END
    338  call setline(1, text)
    339  normal 2Gox
    340  let expected =<< trim END
    341      nop;
    342      val = " // This is not a comment";
    343      x
    344  END
    345  call assert_equal(expected, getline(1, '$'))
    346 
    347  " Using CTRL-U after "o" fixes the indent
    348  %del
    349  let text =<< trim END
    350      {
    351         val = val;      // This is a comment
    352  END
    353  call setline(1, text)
    354  exe "normal! 2Go\<C-U>x\<Esc>"
    355  let expected =<< trim END
    356      {
    357         val = val;      // This is a comment
    358         x
    359  END
    360  call assert_equal(expected, getline(1, '$'))
    361 
    362  " typing comment text auto-wraps
    363  %del
    364  call setline(1, text)
    365  exe "normal! 2GA blah more text blah.\<Esc>"
    366  let expected =<< trim END
    367      {
    368         val = val;      // This is a comment
    369                         // blah more text
    370                         // blah.
    371  END
    372  call assert_equal(expected, getline(1, '$'))
    373 
    374  bwipe!
    375 endfunc
    376 
    377 " Tests for :right, :center and :left on text with embedded TAB.
    378 func Test_format_align()
    379  enew!
    380  set tw=65
    381 
    382  " :left alignment
    383  call 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
    394  call 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, '$'))
    405  enew!
    406 
    407  " :center alignment
    408  call 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
    419  call 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, '$'))
    430  enew!
    431 
    432  " :right alignment
    433  call 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
    518  call 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, '$'))
    603  enew!
    604 
    605  " align text with 'wrapmargin'
    606  50vnew
    607  call setline(1, ['Vim'])
    608  setl textwidth=0
    609  setl wrapmargin=30
    610  right
    611  call assert_equal("\t\t Vim", getline(1))
    612  q!
    613 
    614  " align text with 'rightleft'
    615  if has('rightleft')
    616    new
    617    call setline(1, 'Vim')
    618    setlocal rightleft
    619    left 20
    620    setlocal norightleft
    621    call assert_equal("\t\t Vim", getline(1))
    622    setlocal rightleft
    623    right
    624    setlocal norightleft
    625    call assert_equal("Vim", getline(1))
    626    close!
    627  endif
    628 
    629  set tw&
    630 endfunc
    631 
    632 " Test formatting a paragraph.
    633 func Test_format_para()
    634  enew!
    635  set fo+=tcroql tw=72
    636 
    637  call 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 \ ])
    646  exe "normal /xxxxxxxx$\<CR>"
    647  normal 0gq6kk
    648  call 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 
    658  set fo& tw&
    659  enew!
    660 endfunc
    661 
    662 " Test undo after ":%s" and formatting.
    663 func Test_format_undo()
    664  enew!
    665  map gg :.,.+2s/^/x/<CR>kk:set tw=3<CR>gqq
    666 
    667  call 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
    673  exe "normal i\<C-G>u"
    674  call cursor(1,1)
    675  normal ggu
    676  call assert_equal([
    677       \ "aa aa aa aa",
    678       \ "bb bb bb bb",
    679       \ "cc cc cc cc",
    680       \ ""
    681       \ ], getline(1, '$'))
    682 
    683  unmap gg
    684  set tw&
    685  enew!
    686 endfunc
    687 
    688 func Test_format_list_auto()
    689  new
    690  call setline(1, ['1. abc', '2. def', '3.  ghi'])
    691  set fo=tan ai bs=2
    692  call feedkeys("3G0lli\<BS>\<BS>x\<Esc>", 'tx')
    693  call assert_equal('2. defx ghi', getline(2))
    694  bwipe!
    695  set fo& ai& bs&
    696 endfunc
    697 
    698 func Test_crash_github_issue_5095()
    699  CheckFeature autocmd
    700 
    701  " This used to segfault, see https://github.com/vim/vim/issues/5095
    702  augroup testing
    703    au BufNew x center
    704  augroup END
    705 
    706  next! x
    707 
    708  bw
    709  augroup testing
    710    au!
    711  augroup END
    712  augroup! testing
    713 endfunc
    714 
    715 " Test for formatting multi-byte text with 'fo=t'
    716 func Test_tw_2_fo_t()
    717  new
    718  let t =<< trim END
    719    {
    720    XYZ
    721    abc XYZ
    722    }
    723  END
    724  call setline(1, t)
    725  call cursor(2, 1)
    726 
    727  set tw=2 fo=t
    728  let t =<< trim END
    729    XYZ
    730    abc XYZ
    731  END
    732  exe "normal gqgqjgqgq"
    733  exe "normal o\n" . join(t, "\n")
    734 
    735  let expected =<< trim END
    736    {
    737    XYZ
    738    abc
    739    XYZ
    740 
    741    XYZ
    742    abc
    743    XYZ
    744    }
    745  END
    746  call assert_equal(expected, getline(1, '$'))
    747 
    748  set tw& fo&
    749  bwipe!
    750 endfunc
    751 
    752 " Test for formatting multi-byte text with 'fo=tm' and 'tw=1'
    753 func Test_tw_1_fo_tm()
    754  new
    755  let t =<< trim END
    756    {
    757     758    Xa
    759 a
    760    XY
    761    X Y
    762    }
    763  END
    764  call setline(1, t)
    765  call cursor(2, 1)
    766 
    767  set tw=1 fo=tm
    768  let t =<< trim END
    769     770    Xa
    771 a
    772    XY
    773    X Y
    774  END
    775  exe "normal gqgqjgqgqjgqgqjgqgqjgqgq"
    776  exe "normal o\n" . join(t, "\n")
    777 
    778  let expected =<< trim END
    779    {
    780     781     782    a
    783     784    a
    785     786     787     788     789 
    790     791     792    a
    793     794    a
    795     796     797     798     799    }
    800  END
    801  call assert_equal(expected, getline(1, '$'))
    802 
    803  set tw& fo&
    804  bwipe!
    805 endfunc
    806 
    807 " Test for formatting multi-byte text with 'fo=tm' and 'tw=2'
    808 func Test_tw_2_fo_tm()
    809  new
    810  let t =<< trim END
    811    {
    812     813    Xa
    814 a
    815    XY
    816    X Y
    817    aX
    818    abX
    819    abcX
    820    abX c
    821    abXY
    822    }
    823  END
    824  call setline(1, t)
    825  call cursor(2, 1)
    826 
    827  set tw=2 fo=tm
    828  let t =<< trim END
    829     830    Xa
    831 a
    832    XY
    833    X Y
    834    aX
    835    abX
    836    abcX
    837    abX c
    838    abXY
    839  END
    840  exe "normal gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgq"
    841  exe "normal o\n" . join(t, "\n")
    842 
    843  let expected =<< trim END
    844    {
    845     846     847    a
    848     849    a
    850     851     852     853     854    a
    855     856    ab
    857     858    abc
    859     860    ab
    861     862    c
    863    ab
    864     865     866 
    867     868     869    a
    870     871    a
    872     873     874     875     876    a
    877     878    ab
    879     880    abc
    881     882    ab
    883     884    c
    885    ab
    886     887     888    }
    889  END
    890  call assert_equal(expected, getline(1, '$'))
    891 
    892  set tw& fo&
    893  bwipe!
    894 endfunc
    895 
    896 " Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'autoindent'.
    897 func Test_tw_2_fo_tm_ai()
    898  new
    899  let t =<< trim END
    900    {
    901     902      Xa
    903    }
    904  END
    905  call setline(1, t)
    906  call cursor(2, 1)
    907 
    908  set ai tw=2 fo=tm
    909  let t =<< trim END
    910     911    Xa
    912  END
    913  exe "normal gqgqjgqgq"
    914  exe "normal o\n" . join(t, "\n")
    915 
    916  let expected =<< trim END
    917    {
    918     919     920      a
    921 
    922     923     924      a
    925    }
    926  END
    927  call assert_equal(expected, getline(1, '$'))
    928 
    929  set tw& fo& ai&
    930  bwipe!
    931 endfunc
    932 
    933 " Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'noai'.
    934 func Test_tw_2_fo_tm_noai()
    935  new
    936  let t =<< trim END
    937    {
    938     939      Xa
    940    }
    941  END
    942  call setline(1, t)
    943  call cursor(2, 1)
    944 
    945  set noai tw=2 fo=tm
    946  exe "normal gqgqjgqgqo\n  X\n  Xa"
    947 
    948  let expected =<< trim END
    949    {
    950     951     952    a
    953 
    954     955     956    a
    957    }
    958  END
    959  call assert_equal(expected, getline(1, '$'))
    960 
    961  set tw& fo& ai&
    962  bwipe!
    963 endfunc
    964 
    965 func Test_tw_2_fo_tm_replace()
    966  new
    967  let t =<< trim END
    968    {
    969 
    970    }
    971  END
    972  call setline(1, t)
    973  call cursor(2, 1)
    974 
    975  set tw=2 fo=tm
    976  exe "normal RXa"
    977 
    978  let expected =<< trim END
    979    {
    980     981    a
    982    }
    983  END
    984  call assert_equal(expected, getline(1, '$'))
    985 
    986  set tw& fo&
    987  bwipe!
    988 endfunc
    989 
    990 " Test for 'matchpairs' with multibyte chars
    991 func Test_mps_multibyte()
    992  new
    993  let t =<< trim END
    994    {
    995    ‘ two three ’ four
    996    }
    997  END
    998  call setline(1, t)
    999  call cursor(2, 1)
   1000 
   1001  exe "set mps+=\u2018:\u2019"
   1002  normal d%
   1003 
   1004  let expected =<< trim END
   1005    {
   1006     four
   1007    }
   1008  END
   1009  call assert_equal(expected, getline(1, '$'))
   1010 
   1011  set mps&
   1012  bwipe!
   1013 endfunc
   1014 
   1015 " Test for 'matchpairs' in latin1 encoding
   1016 func Test_mps_latin1()
   1017  new
   1018  let save_enc = &encoding
   1019  " set encoding=latin1
   1020  call setline(1, 'abc(def)ghi')
   1021  normal %
   1022  call assert_equal(8, col('.'))
   1023  normal %
   1024  call assert_equal(4, col('.'))
   1025  call cursor(1, 6)
   1026  normal [(
   1027  call assert_equal(4, col('.'))
   1028  normal %
   1029  call assert_equal(8, col('.'))
   1030  call cursor(1, 6)
   1031  normal ])
   1032  call assert_equal(8, col('.'))
   1033  normal %
   1034  call assert_equal(4, col('.'))
   1035  let &encoding = save_enc
   1036  close!
   1037 endfunc
   1038 
   1039 func Test_empty_matchpairs()
   1040  split
   1041  set matchpairs= showmatch
   1042  call assert_nobeep('call feedkeys("ax\tx\t\<Esc>", "xt")')
   1043  set matchpairs& noshowmatch
   1044  bwipe!
   1045 endfunc
   1046 
   1047 func Test_mps_error()
   1048  let encoding_save = &encoding
   1049 
   1050  " for e in ['utf-8', 'latin1']
   1051  for e in ['utf-8']
   1052    exe 'set encoding=' .. e
   1053 
   1054    call assert_fails('set mps=<:', 'E474:', e)
   1055    call assert_fails('set mps=:>', 'E474:', e)
   1056    call assert_fails('set mps=<>', 'E474:', e)
   1057    call assert_fails('set mps=<:>_', 'E474:', e)
   1058  endfor
   1059 
   1060  let &encoding = encoding_save
   1061 endfunc
   1062 
   1063 " Test for ra on multi-byte characters
   1064 func Test_ra_multibyte()
   1065  new
   1066  let t =<< trim END
   1067    ra test
   1068    abba
   1069    aab
   1070  END
   1071  call setline(1, t)
   1072  call cursor(1, 1)
   1073 
   1074  normal jVjra
   1075 
   1076  let expected =<< trim END
   1077    ra test
   1078    aaaa
   1079    aaa
   1080  END
   1081  call assert_equal(expected, getline(1, '$'))
   1082 
   1083  bwipe!
   1084 endfunc
   1085 
   1086 " Test for 'whichwrap' with multi-byte character
   1087 func Test_whichwrap_multi_byte()
   1088  new
   1089  let t =<< trim END
   1090    á
   1091    x
   1092  END
   1093  call setline(1, t)
   1094  call cursor(2, 1)
   1095 
   1096  set whichwrap+=h
   1097  normal dh
   1098  set whichwrap&
   1099 
   1100  let expected =<< trim END
   1101    áx
   1102  END
   1103  call assert_equal(expected, getline(1, '$'))
   1104 
   1105  bwipe!
   1106 endfunc
   1107 
   1108 " Test for 'a' and 'w' flags in 'formatoptions'
   1109 func Test_fo_a_w()
   1110  new
   1111  setlocal fo+=aw tw=10
   1112  call feedkeys("iabc abc a abc\<Esc>k0weade", 'xt')
   1113  call assert_equal(['abc abcde ', 'a abc'], getline(1, '$'))
   1114 
   1115  " when a line ends with space, it is not broken up.
   1116  %d
   1117  call feedkeys("ione two to    ", 'xt')
   1118  call 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
   1123  set backspace=indent,eol,start
   1124  call setline(1, ['one    ', 'two'])
   1125  exe "normal 2Gi\<BS>"
   1126  call assert_equal(['one   two'], getline(1, '$'))
   1127  set backspace&
   1128 
   1129  " Test for 'a', 'w' and '1' options.
   1130  setlocal textwidth=0
   1131  setlocal fo=1aw
   1132  %d
   1133  call setline(1, '. foo')
   1134  normal 72ig
   1135  call feedkeys('a uu uu uu', 'xt')
   1136  call assert_equal('g uu uu ', getline(1)[-8:])
   1137  call assert_equal(['uu. foo'], getline(2, '$'))
   1138 
   1139  " using backspace or "x" triggers reformat
   1140  call setline(1, ['1 2 3 4 5 ', '6 7 8 9'])
   1141  set tw=10
   1142  set fo=taw
   1143  set bs=indent,eol,start
   1144  exe "normal 1G4la\<BS>\<BS>\<Esc>"
   1145  call assert_equal(['1 2 4 5 6 ', '7 8 9'], getline(1, 2))
   1146  exe "normal f4xx"
   1147  call assert_equal(['1 2 5 6 7 ', '8 9'], getline(1, 2))
   1148 
   1149  " using "cw" leaves cursor in right spot
   1150  call setline(1, ['Now we g whether that nation, or',
   1151      \ 'any nation so conceived and,'])
   1152  set fo=tcqa tw=35
   1153  exe "normal 2G0cwx\<Esc>"
   1154  call assert_equal(['Now we g whether that nation, or x', 'nation so conceived and,'], getline(1, 2))
   1155 
   1156  set tw=0
   1157  set fo&
   1158  %bw!
   1159 endfunc
   1160 
   1161 " Test for formatting lines using gq in visual mode
   1162 func Test_visual_gq_format()
   1163  new
   1164  call setline(1, ['one two three four', 'five six', 'one two'])
   1165  setl textwidth=10
   1166  call feedkeys('ggv$jj', 'xt')
   1167  redraw!
   1168  normal gq
   1169  %d
   1170  call setline(1, ['one two three four', 'five six', 'one two'])
   1171  normal G$
   1172  call feedkeys('v0kk', 'xt')
   1173  redraw!
   1174  normal gq
   1175  setl textwidth&
   1176  close!
   1177 endfunc
   1178 
   1179 " Test for 'n' flag in 'formatoptions' to format numbered lists
   1180 func Test_fo_n()
   1181  new
   1182  setlocal autoindent
   1183  setlocal textwidth=12
   1184  setlocal fo=n
   1185  call setline(1, ['  1) one two three four', '  2) two'])
   1186  normal gggqG
   1187  call assert_equal(['  1) one two', '     three', '     four', '  2) two'],
   1188        \ getline(1, '$'))
   1189  close!
   1190 endfunc
   1191 
   1192 " Test for 'formatlistpat' option
   1193 func Test_formatlistpat()
   1194  new
   1195  setlocal autoindent
   1196  setlocal textwidth=10
   1197  setlocal fo=n
   1198  setlocal formatlistpat=^\\s*-\\s*
   1199  call setline(1, ['  - one two three', '  - two'])
   1200  normal gggqG
   1201  call assert_equal(['  - one', '    two', '    three', '  - two'],
   1202        \ getline(1, '$'))
   1203  close!
   1204 endfunc
   1205 
   1206 " Test for the 'b' and 'v' flags in 'formatoptions'
   1207 " Text should wrap only if a space character is inserted at or before
   1208 " 'textwidth'
   1209 func Test_fo_b()
   1210  new
   1211  setlocal textwidth=20
   1212 
   1213  setlocal formatoptions=t
   1214  call setline(1, 'one two three four')
   1215  call feedkeys('Amore', 'xt')
   1216  call assert_equal(['one two three', 'fourmore'], getline(1, '$'))
   1217 
   1218  setlocal formatoptions=bt
   1219  %d
   1220  call setline(1, 'one two three four')
   1221  call feedkeys('Amore five', 'xt')
   1222  call assert_equal(['one two three fourmore five'], getline(1, '$'))
   1223 
   1224  setlocal formatoptions=bt
   1225  %d
   1226  call setline(1, 'one two three four')
   1227  call feedkeys('A five', 'xt')
   1228  call assert_equal(['one two three four', 'five'], getline(1, '$'))
   1229 
   1230  setlocal formatoptions=vt
   1231  %d
   1232  call setline(1, 'one two three four')
   1233  call feedkeys('Amore five', 'xt')
   1234  call assert_equal(['one two three fourmore', 'five'], getline(1, '$'))
   1235 
   1236  close!
   1237 endfunc
   1238 
   1239 " Test for the '1' flag in 'formatoptions'. Don't wrap text after a one letter
   1240 " word.
   1241 func Test_fo_1()
   1242  new
   1243  setlocal textwidth=20
   1244 
   1245  setlocal formatoptions=t
   1246  call setline(1, 'one two three four')
   1247  call feedkeys('A a bird', 'xt')
   1248  call assert_equal(['one two three four a', 'bird'], getline(1, '$'))
   1249 
   1250  %d
   1251  setlocal formatoptions=t1
   1252  call setline(1, 'one two three four')
   1253  call feedkeys('A a bird', 'xt')
   1254  call assert_equal(['one two three four', 'a bird'], getline(1, '$'))
   1255 
   1256  close!
   1257 endfunc
   1258 
   1259 " Test for 'l' flag in 'formatoptions'. When starting insert mode, if a line
   1260 " is longer than 'textwidth', then it is not broken.
   1261 func Test_fo_l()
   1262  new
   1263  setlocal textwidth=20
   1264 
   1265  setlocal formatoptions=t
   1266  call setline(1, 'one two three four five')
   1267  call feedkeys('A six', 'xt')
   1268  call assert_equal(['one two three four', 'five six'], getline(1, '$'))
   1269 
   1270  %d
   1271  setlocal formatoptions=tl
   1272  call setline(1, 'one two three four five')
   1273  call feedkeys('A six', 'xt')
   1274  call assert_equal(['one two three four five six'], getline(1, '$'))
   1275 
   1276  close!
   1277 endfunc
   1278 
   1279 " Test for the '2' flag in 'formatoptions'
   1280 func Test_fo_2()
   1281  new
   1282  setlocal autoindent
   1283  setlocal formatoptions=t2
   1284  setlocal textwidth=30
   1285  call setline(1, ["\tfirst line of a paragraph.",
   1286        \ "second line of the same paragraph.",
   1287        \ "third line."])
   1288  normal gggqG
   1289  call assert_equal(["\tfirst line of a",
   1290        \ "paragraph.  second line of the",
   1291        \ "same paragraph.  third line."], getline(1, '$'))
   1292  close!
   1293 endfunc
   1294 
   1295 " This was leaving the cursor after the end of a line.  Complicated way to
   1296 " have the problem show up with valgrind.
   1297 func Test_correct_cursor_position()
   1298  " set encoding=iso8859
   1299  new
   1300  norm a000“0
   1301  sil! norm gggg0i0gw0gg
   1302 
   1303  bwipe!
   1304  set encoding=utf8
   1305 endfunc
   1306 
   1307 " vim: shiftwidth=2 sts=2 expandtab