1" Tests for the various 'formatoptions' settings
2
3source check.vim
4
5func 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!
197endfunc
198
199" Tests for :right, :center and :left on text with embedded TAB.
200func Test_format_align()
201  enew!
202  set tw=65
203
204  " :left alignment
205  call append(0, [
206	      \ "	test for :left",
207	      \ "	  a		a",
208	      \ "	    fa		a",
209	      \ "	  dfa		a",
210	      \ "	        sdfa		a",
211	      \ "	  asdfa		a",
212	      \ "	        xasdfa		a",
213	      \ "asxxdfa		a",
214	      \ ])
215  %left
216  call assert_equal([
217	      \ "test for :left",
218	      \ "a		a",
219	      \ "fa		a",
220	      \ "dfa		a",
221	      \ "sdfa		a",
222	      \ "asdfa		a",
223	      \ "xasdfa		a",
224	      \ "asxxdfa		a",
225	      \ ""
226	      \ ], getline(1, '$'))
227  enew!
228
229  " :center alignment
230  call append(0, [
231	      \ "	test for :center",
232	      \ "	  a		a",
233	      \ "	    fa		afd asdf",
234	      \ "	  dfa		a",
235	      \ "	        sdfa		afd asdf",
236	      \ "	  asdfa		a",
237	      \ "	        xasdfa		asdfasdfasdfasdfasdf",
238	      \ "asxxdfa		a"
239	      \ ])
240  %center
241  call assert_equal([
242	      \ "			test for :center",
243	      \ "			 a		a",
244	      \ "		      fa		afd asdf",
245	      \ "			 dfa		a",
246	      \ "		    sdfa		afd asdf",
247	      \ "			 asdfa		a",
248	      \ "	      xasdfa		asdfasdfasdfasdfasdf",
249	      \ "			asxxdfa		a",
250	      \ ""
251	      \ ], getline(1, '$'))
252  enew!
253
254  " :right alignment
255  call append(0, [
256	      \ "	test for :right",
257	      \ "	a		a",
258	      \ "	fa		a",
259	      \ "	dfa		a",
260	      \ "	sdfa		a",
261	      \ "	asdfa		a",
262	      \ "	xasdfa		a",
263	      \ "	asxxdfa		a",
264	      \ "	asxa;ofa		a",
265	      \ "	asdfaqwer		a",
266	      \ "	a		ax",
267	      \ "	fa		ax",
268	      \ "	dfa		ax",
269	      \ "	sdfa		ax",
270	      \ "	asdfa		ax",
271	      \ "	xasdfa		ax",
272	      \ "	asxxdfa		ax",
273	      \ "	asxa;ofa		ax",
274	      \ "	asdfaqwer		ax",
275	      \ "	a		axx",
276	      \ "	fa		axx",
277	      \ "	dfa		axx",
278	      \ "	sdfa		axx",
279	      \ "	asdfa		axx",
280	      \ "	xasdfa		axx",
281	      \ "	asxxdfa		axx",
282	      \ "	asxa;ofa		axx",
283	      \ "	asdfaqwer		axx",
284	      \ "	a		axxx",
285	      \ "	fa		axxx",
286	      \ "	dfa		axxx",
287	      \ "	sdfa		axxx",
288	      \ "	asdfa		axxx",
289	      \ "	xasdfa		axxx",
290	      \ "	asxxdfa		axxx",
291	      \ "	asxa;ofa		axxx",
292	      \ "	asdfaqwer		axxx",
293	      \ "	a		axxxo",
294	      \ "	fa		axxxo",
295	      \ "	dfa		axxxo",
296	      \ "	sdfa		axxxo",
297	      \ "	asdfa		axxxo",
298	      \ "	xasdfa		axxxo",
299	      \ "	asxxdfa		axxxo",
300	      \ "	asxa;ofa		axxxo",
301	      \ "	asdfaqwer		axxxo",
302	      \ "	a		axxxoi",
303	      \ "	fa		axxxoi",
304	      \ "	dfa		axxxoi",
305	      \ "	sdfa		axxxoi",
306	      \ "	asdfa		axxxoi",
307	      \ "	xasdfa		axxxoi",
308	      \ "	asxxdfa		axxxoi",
309	      \ "	asxa;ofa		axxxoi",
310	      \ "	asdfaqwer		axxxoi",
311	      \ "	a		axxxoik",
312	      \ "	fa		axxxoik",
313	      \ "	dfa		axxxoik",
314	      \ "	sdfa		axxxoik",
315	      \ "	asdfa		axxxoik",
316	      \ "	xasdfa		axxxoik",
317	      \ "	asxxdfa		axxxoik",
318	      \ "	asxa;ofa		axxxoik",
319	      \ "	asdfaqwer		axxxoik",
320	      \ "	a		axxxoike",
321	      \ "	fa		axxxoike",
322	      \ "	dfa		axxxoike",
323	      \ "	sdfa		axxxoike",
324	      \ "	asdfa		axxxoike",
325	      \ "	xasdfa		axxxoike",
326	      \ "	asxxdfa		axxxoike",
327	      \ "	asxa;ofa		axxxoike",
328	      \ "	asdfaqwer		axxxoike",
329	      \ "	a		axxxoikey",
330	      \ "	fa		axxxoikey",
331	      \ "	dfa		axxxoikey",
332	      \ "	sdfa		axxxoikey",
333	      \ "	asdfa		axxxoikey",
334	      \ "	xasdfa		axxxoikey",
335	      \ "	asxxdfa		axxxoikey",
336	      \ "	asxa;ofa		axxxoikey",
337	      \ "	asdfaqwer		axxxoikey",
338	      \ ])
339  %right
340  call assert_equal([
341	      \ "\t\t\t\t		  test for :right",
342	      \ "\t\t\t\t		      a		a",
343	      \ "\t\t\t\t		     fa		a",
344	      \ "\t\t\t\t		    dfa		a",
345	      \ "\t\t\t\t		   sdfa		a",
346	      \ "\t\t\t\t		  asdfa		a",
347	      \ "\t\t\t\t		 xasdfa		a",
348	      \ "\t\t\t\t		asxxdfa		a",
349	      \ "\t\t\t\t	       asxa;ofa		a",
350	      \ "\t\t\t\t	      asdfaqwer		a",
351	      \ "\t\t\t\t	      a		ax",
352	      \ "\t\t\t\t	     fa		ax",
353	      \ "\t\t\t\t	    dfa		ax",
354	      \ "\t\t\t\t	   sdfa		ax",
355	      \ "\t\t\t\t	  asdfa		ax",
356	      \ "\t\t\t\t	 xasdfa		ax",
357	      \ "\t\t\t\t	asxxdfa		ax",
358	      \ "\t\t\t\t       asxa;ofa		ax",
359	      \ "\t\t\t\t      asdfaqwer		ax",
360	      \ "\t\t\t\t	      a		axx",
361	      \ "\t\t\t\t	     fa		axx",
362	      \ "\t\t\t\t	    dfa		axx",
363	      \ "\t\t\t\t	   sdfa		axx",
364	      \ "\t\t\t\t	  asdfa		axx",
365	      \ "\t\t\t\t	 xasdfa		axx",
366	      \ "\t\t\t\t	asxxdfa		axx",
367	      \ "\t\t\t\t       asxa;ofa		axx",
368	      \ "\t\t\t\t      asdfaqwer		axx",
369	      \ "\t\t\t\t	      a		axxx",
370	      \ "\t\t\t\t	     fa		axxx",
371	      \ "\t\t\t\t	    dfa		axxx",
372	      \ "\t\t\t\t	   sdfa		axxx",
373	      \ "\t\t\t\t	  asdfa		axxx",
374	      \ "\t\t\t\t	 xasdfa		axxx",
375	      \ "\t\t\t\t	asxxdfa		axxx",
376	      \ "\t\t\t\t       asxa;ofa		axxx",
377	      \ "\t\t\t\t      asdfaqwer		axxx",
378	      \ "\t\t\t\t	      a		axxxo",
379	      \ "\t\t\t\t	     fa		axxxo",
380	      \ "\t\t\t\t	    dfa		axxxo",
381	      \ "\t\t\t\t	   sdfa		axxxo",
382	      \ "\t\t\t\t	  asdfa		axxxo",
383	      \ "\t\t\t\t	 xasdfa		axxxo",
384	      \ "\t\t\t\t	asxxdfa		axxxo",
385	      \ "\t\t\t\t       asxa;ofa		axxxo",
386	      \ "\t\t\t\t      asdfaqwer		axxxo",
387	      \ "\t\t\t\t	      a		axxxoi",
388	      \ "\t\t\t\t	     fa		axxxoi",
389	      \ "\t\t\t\t	    dfa		axxxoi",
390	      \ "\t\t\t\t	   sdfa		axxxoi",
391	      \ "\t\t\t\t	  asdfa		axxxoi",
392	      \ "\t\t\t\t	 xasdfa		axxxoi",
393	      \ "\t\t\t\t	asxxdfa		axxxoi",
394	      \ "\t\t\t\t       asxa;ofa		axxxoi",
395	      \ "\t\t\t\t      asdfaqwer		axxxoi",
396	      \ "\t\t\t\t	      a		axxxoik",
397	      \ "\t\t\t\t	     fa		axxxoik",
398	      \ "\t\t\t\t	    dfa		axxxoik",
399	      \ "\t\t\t\t	   sdfa		axxxoik",
400	      \ "\t\t\t\t	  asdfa		axxxoik",
401	      \ "\t\t\t\t	 xasdfa		axxxoik",
402	      \ "\t\t\t\t	asxxdfa		axxxoik",
403	      \ "\t\t\t\t       asxa;ofa		axxxoik",
404	      \ "\t\t\t\t      asdfaqwer		axxxoik",
405	      \ "\t\t\t\t	      a		axxxoike",
406	      \ "\t\t\t\t	     fa		axxxoike",
407	      \ "\t\t\t\t	    dfa		axxxoike",
408	      \ "\t\t\t\t	   sdfa		axxxoike",
409	      \ "\t\t\t\t	  asdfa		axxxoike",
410	      \ "\t\t\t\t	 xasdfa		axxxoike",
411	      \ "\t\t\t\t	asxxdfa		axxxoike",
412	      \ "\t\t\t\t       asxa;ofa		axxxoike",
413	      \ "\t\t\t\t      asdfaqwer		axxxoike",
414	      \ "\t\t\t\t	      a		axxxoikey",
415	      \ "\t\t\t\t	     fa		axxxoikey",
416	      \ "\t\t\t\t	    dfa		axxxoikey",
417	      \ "\t\t\t\t	   sdfa		axxxoikey",
418	      \ "\t\t\t\t	  asdfa		axxxoikey",
419	      \ "\t\t\t\t	 xasdfa		axxxoikey",
420	      \ "\t\t\t\t	asxxdfa		axxxoikey",
421	      \ "\t\t\t\t       asxa;ofa		axxxoikey",
422	      \ "\t\t\t\t      asdfaqwer		axxxoikey",
423	      \ ""
424	      \ ], getline(1, '$'))
425  enew!
426
427  " align text with 'wrapmargin'
428  50vnew
429  call setline(1, ['Vim'])
430  setl textwidth=0
431  setl wrapmargin=30
432  right
433  call assert_equal("\t\t Vim", getline(1))
434  q!
435
436  " align text with 'rightleft'
437  if has('rightleft')
438    new
439    call setline(1, 'Vim')
440    setlocal rightleft
441    left 20
442    setlocal norightleft
443    call assert_equal("\t\t Vim", getline(1))
444    setlocal rightleft
445    right
446    setlocal norightleft
447    call assert_equal("Vim", getline(1))
448    close!
449  endif
450
451  set tw&
452endfunc
453
454" Test formatting a paragraph.
455func Test_format_para()
456  enew!
457  set fo+=tcroql tw=72
458
459  call append(0, [
460	\ "xxxxx xx xxxxxx ",
461	\ "xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx",
462	\ "xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx",
463	\ "xx xxxxxxx. xxxx xxxx.",
464	\ "",
465	\ "> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx",
466	\ "> xxxxxx xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx"
467	\ ])
468  exe "normal /xxxxxxxx$\<CR>"
469  normal 0gq6kk
470  call assert_equal([
471	\ "xxxxx xx xxxxxx xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx",
472	\ "xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx xx xxxxxxx.",
473	\ "xxxx xxxx.",
474	\ "",
475	\ "> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx xxxxxx",
476	\ "> xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx",
477	\ ""
478	\ ], getline(1, '$'))
479
480  set fo& tw&
481  enew!
482endfunc
483
484" Test undo after ":%s" and formatting.
485func Test_format_undo()
486  enew!
487  map gg :.,.+2s/^/x/<CR>kk:set tw=3<CR>gqq
488
489  call append(0, [
490	      \ "aa aa aa aa",
491	      \ "bb bb bb bb",
492	      \ "cc cc cc cc"
493	      \ ])
494  " undo/redo here to make the next undo only work on the following changes
495  exe "normal i\<C-G>u"
496  call cursor(1,1)
497  normal ggu
498  call assert_equal([
499	      \ "aa aa aa aa",
500	      \ "bb bb bb bb",
501	      \ "cc cc cc cc",
502	      \ ""
503	      \ ], getline(1, '$'))
504
505  unmap gg
506  set tw&
507  enew!
508endfunc
509
510func Test_format_list_auto()
511  new
512  call setline(1, ['1. abc', '2. def', '3.  ghi'])
513  set fo=tan ai bs=2
514  call feedkeys("3G0lli\<BS>\<BS>x\<Esc>", 'tx')
515  call assert_equal('2. defx ghi', getline(2))
516  bwipe!
517  set fo& ai& bs&
518endfunc
519
520func Test_crash_github_issue_5095()
521  CheckFeature autocmd
522
523  " This used to segfault, see https://github.com/vim/vim/issues/5095
524  augroup testing
525    au BufNew x center
526  augroup END
527
528  next! x
529
530  bw
531  augroup testing
532    au!
533  augroup END
534  augroup! testing
535endfunc
536
537" Test for formatting multi-byte text with 'fo=t'
538func Test_tw_2_fo_t()
539  new
540  let t =<< trim END
541    {
542    XYZ
543    abc XYZ
544    }
545  END
546  call setline(1, t)
547  call cursor(2, 1)
548
549  set tw=2 fo=t
550  let t =<< trim END
551    XYZ
552    abc XYZ
553  END
554  exe "normal gqgqjgqgq"
555  exe "normal o\n" . join(t, "\n")
556
557  let expected =<< trim END
558    {
559    XYZ
560    abc
561    XYZ
562
563    XYZ
564    abc
565    XYZ
566    }
567  END
568  call assert_equal(expected, getline(1, '$'))
569
570  set tw& fo&
571  bwipe!
572endfunc
573
574" Test for formatting multi-byte text with 'fo=tm' and 'tw=1'
575func Test_tw_1_fo_tm()
576  new
577  let t =<< trim END
578    {
579580    Xa
581    X a
582    XY
583    X Y
584    }
585  END
586  call setline(1, t)
587  call cursor(2, 1)
588
589  set tw=1 fo=tm
590  let t =<< trim END
591592    Xa
593    X a
594    XY
595    X Y
596  END
597  exe "normal gqgqjgqgqjgqgqjgqgqjgqgq"
598  exe "normal o\n" . join(t, "\n")
599
600  let expected =<< trim END
601    {
602603604    a
605606    a
607608609610611
612613614    a
615616    a
617618619620621    }
622  END
623  call assert_equal(expected, getline(1, '$'))
624
625  set tw& fo&
626  bwipe!
627endfunc
628
629" Test for formatting multi-byte text with 'fo=tm' and 'tw=2'
630func Test_tw_2_fo_tm()
631  new
632  let t =<< trim END
633    {
634635    Xa
636    X a
637    XY
638    X Y
639    aX
640    abX
641    abcX
642    abX c
643    abXY
644    }
645  END
646  call setline(1, t)
647  call cursor(2, 1)
648
649  set tw=2 fo=tm
650  let t =<< trim END
651652    Xa
653    X a
654    XY
655    X Y
656    aX
657    abX
658    abcX
659    abX c
660    abXY
661  END
662  exe "normal gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgq"
663  exe "normal o\n" . join(t, "\n")
664
665  let expected =<< trim END
666    {
667668669    a
670671    a
672673674675676    a
677678    ab
679680    abc
681682    ab
683684    c
685    ab
686687688
689690691    a
692693    a
694695696697698    a
699700    ab
701702    abc
703704    ab
705706    c
707    ab
708709710    }
711  END
712  call assert_equal(expected, getline(1, '$'))
713
714  set tw& fo&
715  bwipe!
716endfunc
717
718" Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'autoindent'.
719func Test_tw_2_fo_tm_ai()
720  new
721  let t =<< trim END
722    {
723724      Xa
725    }
726  END
727  call setline(1, t)
728  call cursor(2, 1)
729
730  set ai tw=2 fo=tm
731  let t =<< trim END
732733    Xa
734  END
735  exe "normal gqgqjgqgq"
736  exe "normal o\n" . join(t, "\n")
737
738  let expected =<< trim END
739    {
740741742      a
743
744745746      a
747    }
748  END
749  call assert_equal(expected, getline(1, '$'))
750
751  set tw& fo& ai&
752  bwipe!
753endfunc
754
755" Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'noai'.
756func Test_tw_2_fo_tm_noai()
757  new
758  let t =<< trim END
759    {
760761      Xa
762    }
763  END
764  call setline(1, t)
765  call cursor(2, 1)
766
767  set noai tw=2 fo=tm
768  exe "normal gqgqjgqgqo\n  X\n  Xa"
769
770  let expected =<< trim END
771    {
772773774    a
775
776777778    a
779    }
780  END
781  call assert_equal(expected, getline(1, '$'))
782
783  set tw& fo& ai&
784  bwipe!
785endfunc
786
787func Test_tw_2_fo_tm_replace()
788  new
789  let t =<< trim END
790    {
791
792    }
793  END
794  call setline(1, t)
795  call cursor(2, 1)
796
797  set tw=2 fo=tm
798  exe "normal RXa"
799
800  let expected =<< trim END
801    {
802803    a
804    }
805  END
806  call assert_equal(expected, getline(1, '$'))
807
808  set tw& fo&
809  bwipe!
810endfunc
811
812" Test for 'matchpairs' with multibyte chars
813func Test_mps_multibyte()
814  new
815  let t =<< trim END
816    {
817    ‘ two three ’ four
818    }
819  END
820  call setline(1, t)
821  call cursor(2, 1)
822
823  exe "set mps+=\u2018:\u2019"
824  normal d%
825
826  let expected =<< trim END
827    {
828     four
829    }
830  END
831  call assert_equal(expected, getline(1, '$'))
832
833  set mps&
834  bwipe!
835endfunc
836
837" Test for 'matchpairs' in latin1 encoding
838func Test_mps_latin1()
839  new
840  let save_enc = &encoding
841  set encoding=latin1
842  call setline(1, 'abc(def)ghi')
843  normal %
844  call assert_equal(8, col('.'))
845  normal %
846  call assert_equal(4, col('.'))
847  call cursor(1, 6)
848  normal [(
849  call assert_equal(4, col('.'))
850  normal %
851  call assert_equal(8, col('.'))
852  call cursor(1, 6)
853  normal ])
854  call assert_equal(8, col('.'))
855  normal %
856  call assert_equal(4, col('.'))
857  let &encoding = save_enc
858  close!
859endfunc
860
861" Test for ra on multi-byte characters
862func Test_ra_multibyte()
863  new
864  let t =<< trim END
865    ra test
866    abba
867    aab
868  END
869  call setline(1, t)
870  call cursor(1, 1)
871
872  normal jVjra
873
874  let expected =<< trim END
875    ra test
876    aaaa
877    aaa
878  END
879  call assert_equal(expected, getline(1, '$'))
880
881  bwipe!
882endfunc
883
884" Test for 'whichwrap' with multi-byte character
885func Test_whichwrap_multi_byte()
886  new
887  let t =<< trim END
888    á
889    x
890  END
891  call setline(1, t)
892  call cursor(2, 1)
893
894  set whichwrap+=h
895  normal dh
896  set whichwrap&
897
898  let expected =<< trim END
899    áx
900  END
901  call assert_equal(expected, getline(1, '$'))
902
903  bwipe!
904endfunc
905
906" Test for 'a' and 'w' flags in 'formatoptions'
907func Test_fo_a_w()
908  new
909  setlocal fo+=aw tw=10
910  call feedkeys("iabc abc a abc\<Esc>k0weade", 'xt')
911  call assert_equal(['abc abcde ', 'a abc'], getline(1, '$'))
912
913  " when a line ends with space, it is not broken up.
914  %d
915  call feedkeys("ione two to    ", 'xt')
916  call assert_equal('one two to    ', getline(1))
917
918  " when a line ends with spaces and backspace is used in the next line, the
919  " last space in the previous line should be removed.
920  %d
921  set backspace=indent,eol,start
922  call setline(1, ['one    ', 'two'])
923  exe "normal 2Gi\<BS>"
924  call assert_equal(['one   two'], getline(1, '$'))
925  set backspace&
926
927  " Test for 'a', 'w' and '1' options.
928  setlocal textwidth=0
929  setlocal fo=1aw
930  %d
931  call setline(1, '. foo')
932  normal 72ig
933  call feedkeys('a uu uu uu', 'xt')
934  call assert_equal('g uu uu ', getline(1)[-8:])
935  call assert_equal(['uu. foo'], getline(2, '$'))
936
937  %bw!
938endfunc
939
940" Test for formatting lines using gq in visual mode
941func Test_visual_gq_format()
942  new
943  call setline(1, ['one two three four', 'five six', 'one two'])
944  setl textwidth=10
945  call feedkeys('ggv$jj', 'xt')
946  redraw!
947  normal gq
948  %d
949  call setline(1, ['one two three four', 'five six', 'one two'])
950  normal G$
951  call feedkeys('v0kk', 'xt')
952  redraw!
953  normal gq
954  setl textwidth&
955  close!
956endfunc
957
958" Test for 'n' flag in 'formatoptions' to format numbered lists
959func Test_fo_n()
960  new
961  setlocal autoindent
962  setlocal textwidth=12
963  setlocal fo=n
964  call setline(1, ['  1) one two three four', '  2) two'])
965  normal gggqG
966  call assert_equal(['  1) one two', '     three', '     four', '  2) two'],
967        \ getline(1, '$'))
968  close!
969endfunc
970
971" Test for 'formatlistpat' option
972func Test_formatlistpat()
973  new
974  setlocal autoindent
975  setlocal textwidth=10
976  setlocal fo=n
977  setlocal formatlistpat=^\\s*-\\s*
978  call setline(1, ['  - one two three', '  - two'])
979  normal gggqG
980  call assert_equal(['  - one', '    two', '    three', '  - two'],
981        \ getline(1, '$'))
982  close!
983endfunc
984
985" Test for the 'b' and 'v' flags in 'formatoptions'
986" Text should wrap only if a space character is inserted at or before
987" 'textwidth'
988func Test_fo_b()
989  new
990  setlocal textwidth=20
991
992  setlocal formatoptions=t
993  call setline(1, 'one two three four')
994  call feedkeys('Amore', 'xt')
995  call assert_equal(['one two three', 'fourmore'], getline(1, '$'))
996
997  setlocal formatoptions=bt
998  %d
999  call setline(1, 'one two three four')
1000  call feedkeys('Amore five', 'xt')
1001  call assert_equal(['one two three fourmore five'], getline(1, '$'))
1002
1003  setlocal formatoptions=bt
1004  %d
1005  call setline(1, 'one two three four')
1006  call feedkeys('A five', 'xt')
1007  call assert_equal(['one two three four', 'five'], getline(1, '$'))
1008
1009  setlocal formatoptions=vt
1010  %d
1011  call setline(1, 'one two three four')
1012  call feedkeys('Amore five', 'xt')
1013  call assert_equal(['one two three fourmore', 'five'], getline(1, '$'))
1014
1015  close!
1016endfunc
1017
1018" Test for the '1' flag in 'formatoptions'. Don't wrap text after a one letter
1019" word.
1020func Test_fo_1()
1021  new
1022  setlocal textwidth=20
1023
1024  setlocal formatoptions=t
1025  call setline(1, 'one two three four')
1026  call feedkeys('A a bird', 'xt')
1027  call assert_equal(['one two three four a', 'bird'], getline(1, '$'))
1028
1029  %d
1030  setlocal formatoptions=t1
1031  call setline(1, 'one two three four')
1032  call feedkeys('A a bird', 'xt')
1033  call assert_equal(['one two three four', 'a bird'], getline(1, '$'))
1034
1035  close!
1036endfunc
1037
1038" Test for 'l' flag in 'formatoptions'. When starting insert mode, if a line
1039" is longer than 'textwidth', then it is not broken.
1040func Test_fo_l()
1041  new
1042  setlocal textwidth=20
1043
1044  setlocal formatoptions=t
1045  call setline(1, 'one two three four five')
1046  call feedkeys('A six', 'xt')
1047  call assert_equal(['one two three four', 'five six'], getline(1, '$'))
1048
1049  %d
1050  setlocal formatoptions=tl
1051  call setline(1, 'one two three four five')
1052  call feedkeys('A six', 'xt')
1053  call assert_equal(['one two three four five six'], getline(1, '$'))
1054
1055  close!
1056endfunc
1057
1058" Test for the '2' flag in 'formatoptions'
1059func Test_fo_2()
1060  new
1061  setlocal autoindent
1062  setlocal formatoptions=t2
1063  setlocal textwidth=30
1064  call setline(1, ["\tfirst line of a paragraph.",
1065        \ "second line of the same paragraph.",
1066        \ "third line."])
1067  normal gggqG
1068  call assert_equal(["\tfirst line of a",
1069        \ "paragraph.  second line of the",
1070        \ "same paragraph.  third line."], getline(1, '$'))
1071  close!
1072endfunc
1073
1074" vim: shiftwidth=2 sts=2 expandtab
1075