robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

Daniel Müllerfix(textformat): don't swallow "space" with auto-formatting enabled (#38181)61f166ec40

master
28.3 KiB1333 linesraw
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
199func 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!
375endfunc
376
377" Tests for :right, :center and :left on text with embedded TAB.
378func 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    bw!
627  endif
628
629  set tw&
630endfunc
631
632" Test formatting a paragraph.
633func 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!
660endfunc
661
662" Test undo after ":%s" and formatting.
663func 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!
686endfunc
687
688func 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&
696endfunc
697
698func 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
713endfunc
714
715" Test for formatting multi-byte text with 'fo=t'
716func 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!
750endfunc
751
752" Test for formatting multi-byte text with 'fo=tm' and 'tw=1'
753func Test_tw_1_fo_tm()
754  new
755  let t =<< trim END
756    {
757    X
758    Xa
759    X 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    X
770    Xa
771    X 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    X
781    X
782    a
783    X
784    a
785    X
786    Y
787    X
788    Y
789
790    X
791    X
792    a
793    X
794    a
795    X
796    Y
797    X
798    Y
799    }
800  END
801  call assert_equal(expected, getline(1, '$'))
802
803  set tw& fo&
804  bwipe!
805endfunc
806
807" Test for formatting multi-byte text with 'fo=tm' and 'tw=2'
808func Test_tw_2_fo_tm()
809  new
810  let t =<< trim END
811    {
812    X
813    Xa
814    X 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    X
830    Xa
831    X 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    X
846    X
847    a
848    X
849    a
850    X
851    Y
852    X
853    Y
854    a
855    X
856    ab
857    X
858    abc
859    X
860    ab
861    X
862    c
863    ab
864    X
865    Y
866
867    X
868    X
869    a
870    X
871    a
872    X
873    Y
874    X
875    Y
876    a
877    X
878    ab
879    X
880    abc
881    X
882    ab
883    X
884    c
885    ab
886    X
887    Y
888    }
889  END
890  call assert_equal(expected, getline(1, '$'))
891
892  set tw& fo&
893  bwipe!
894endfunc
895
896" Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'autoindent'.
897func Test_tw_2_fo_tm_ai()
898  new
899  let t =<< trim END
900    {
901      X
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    X
911    Xa
912  END
913  exe "normal gqgqjgqgq"
914  exe "normal o\n" . join(t, "\n")
915
916  let expected =<< trim END
917    {
918      X
919      X
920      a
921
922      X
923      X
924      a
925    }
926  END
927  call assert_equal(expected, getline(1, '$'))
928
929  set tw& fo& ai&
930  bwipe!
931endfunc
932
933" Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'noai'.
934func Test_tw_2_fo_tm_noai()
935  new
936  let t =<< trim END
937    {
938      X
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      X
951      X
952    a
953
954      X
955      X
956    a
957    }
958  END
959  call assert_equal(expected, getline(1, '$'))
960
961  set tw& fo& ai&
962  bwipe!
963endfunc
964
965func 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    X
981    a
982    }
983  END
984  call assert_equal(expected, getline(1, '$'))
985
986  set tw& fo&
987  bwipe!
988endfunc
989
990" Test for 'matchpairs' with multibyte chars
991func 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!
1013endfunc
1014
1015" Test for 'matchpairs' in latin1 encoding
1016func 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  bw!
1037endfunc
1038
1039func 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!
1045endfunc
1046
1047func 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
1061endfunc
1062
1063" Test for ra on multi-byte characters
1064func 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!
1084endfunc
1085
1086" Test for 'whichwrap' with multi-byte character
1087func 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!
1106endfunc
1107
1108" Test for 'a' and 'w' flags in 'formatoptions'
1109func 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!
1159endfunc
1160
1161" Test that auto-format ('a' flag) preserves spaces typed in the middle of a line
1162func Test_fo_a_midline_space()
1163  new
1164  let lines = [
1165    \ 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa it but',
1166    \ 'Lorem Ipsum is simply dummy text of the printing and typesetting dasddd',
1167    \ 'industry.',
1168    \ ]
1169  call setline(1, lines)
1170  set fo=ta tw=70
1171
1172  " Prevent INPUT_BUFLEN batching so auto_format runs between keystrokes
1173  autocmd InsertCharPre * " nothing
1174
1175  " Position at 't' of 'it' (col 68) and type space then Z
1176  call cursor(1, 68)
1177  call feedkeys("a Z\<Esc>", 'xt')
1178
1179  " The space between 'it' and 'Z' must be preserved
1180  call assert_match('it Z', getline(1))
1181
1182  autocmd! InsertCharPre
1183  set fo& tw&
1184  bw!
1185endfunc
1186
1187" Test for formatting lines using gq in visual mode
1188func Test_visual_gq_format()
1189  new
1190  call setline(1, ['one two three four', 'five six', 'one two'])
1191  setl textwidth=10
1192  call feedkeys('ggv$jj', 'xt')
1193  redraw!
1194  normal gq
1195  %d
1196  call setline(1, ['one two three four', 'five six', 'one two'])
1197  normal G$
1198  call feedkeys('v0kk', 'xt')
1199  redraw!
1200  normal gq
1201  setl textwidth&
1202  bw!
1203endfunc
1204
1205" Test for 'n' flag in 'formatoptions' to format numbered lists
1206func Test_fo_n()
1207  new
1208  setlocal autoindent
1209  setlocal textwidth=12
1210  setlocal fo=n
1211  call setline(1, ['  1) one two three four', '  2) two'])
1212  normal gggqG
1213  call assert_equal(['  1) one two', '     three', '     four', '  2) two'],
1214        \ getline(1, '$'))
1215  bw!
1216endfunc
1217
1218" Test for 'formatlistpat' option
1219func Test_formatlistpat()
1220  new
1221  setlocal autoindent
1222  setlocal textwidth=10
1223  setlocal fo=n
1224  setlocal formatlistpat=^\\s*-\\s*
1225  call setline(1, ['  - one two three', '  - two'])
1226  normal gggqG
1227  call assert_equal(['  - one', '    two', '    three', '  - two'],
1228        \ getline(1, '$'))
1229  bw!
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()
1236  new
1237  setlocal textwidth=20
1238
1239  setlocal formatoptions=t
1240  call setline(1, 'one two three four')
1241  call feedkeys('Amore', 'xt')
1242  call assert_equal(['one two three', 'fourmore'], getline(1, '$'))
1243
1244  setlocal formatoptions=bt
1245  %d
1246  call setline(1, 'one two three four')
1247  call feedkeys('Amore five', 'xt')
1248  call assert_equal(['one two three fourmore five'], getline(1, '$'))
1249
1250  setlocal formatoptions=bt
1251  %d
1252  call setline(1, 'one two three four')
1253  call feedkeys('A five', 'xt')
1254  call assert_equal(['one two three four', 'five'], getline(1, '$'))
1255
1256  setlocal formatoptions=vt
1257  %d
1258  call setline(1, 'one two three four')
1259  call feedkeys('Amore five', 'xt')
1260  call assert_equal(['one two three fourmore', 'five'], getline(1, '$'))
1261
1262  bw!
1263endfunc
1264
1265" Test for the '1' flag in 'formatoptions'. Don't wrap text after a one letter
1266" word.
1267func Test_fo_1()
1268  new
1269  setlocal textwidth=20
1270
1271  setlocal formatoptions=t
1272  call setline(1, 'one two three four')
1273  call feedkeys('A a bird', 'xt')
1274  call assert_equal(['one two three four a', 'bird'], getline(1, '$'))
1275
1276  %d
1277  setlocal formatoptions=t1
1278  call setline(1, 'one two three four')
1279  call feedkeys('A a bird', 'xt')
1280  call assert_equal(['one two three four', 'a bird'], getline(1, '$'))
1281
1282  bw!
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()
1288  new
1289  setlocal textwidth=20
1290
1291  setlocal formatoptions=t
1292  call setline(1, 'one two three four five')
1293  call feedkeys('A six', 'xt')
1294  call assert_equal(['one two three four', 'five six'], getline(1, '$'))
1295
1296  %d
1297  setlocal formatoptions=tl
1298  call setline(1, 'one two three four five')
1299  call feedkeys('A six', 'xt')
1300  call assert_equal(['one two three four five six'], getline(1, '$'))
1301
1302  bw!
1303endfunc
1304
1305" Test for the '2' flag in 'formatoptions'
1306func Test_fo_2()
1307  new
1308  setlocal autoindent
1309  setlocal formatoptions=t2
1310  setlocal textwidth=30
1311  call setline(1, ["\tfirst line of a paragraph.",
1312        \ "second line of the same paragraph.",
1313        \ "third line."])
1314  normal gggqG
1315  call assert_equal(["\tfirst line of a",
1316        \ "paragraph.  second line of the",
1317        \ "same paragraph.  third line."], getline(1, '$'))
1318  bw!
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
1325  new
1326  norm a000“0
1327  sil! norm gggg0i0gw0gg
1328
1329  bwipe!
1330  set encoding=utf8
1331endfunc
1332
1333" vim: shiftwidth=2 sts=2 expandtab