blob: 32ff3cc70b1ed979ae3288db58622a18a55c1383 [file] [log] [blame]
Bram Moolenaar209d3872017-11-16 21:52:51 +01001" Tests for the various 'formatoptions' settings
Bram Moolenaar396b7c72019-10-21 23:08:59 +02002
3source check.vim
4
Bram Moolenaar209d3872017-11-16 21:52:51 +01005func 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
Bram Moolenaarc3c31582019-01-11 22:15:05 +0100169 " 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
Bram Moolenaar209d3872017-11-16 21:52:51 +0100195 setl ai& tw& fo& si& comments&
196 enew!
197endfunc
Bram Moolenaarcada7892018-01-31 19:30:24 +0100198
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
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100427 " align text with 'wrapmargin'
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100428 50vnew
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100429 call setline(1, ['Vim'])
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100430 setl textwidth=0
431 setl wrapmargin=30
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100432 right
433 call assert_equal("\t\t Vim", getline(1))
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100434 q!
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100435
Bram Moolenaarea3db912020-02-02 15:32:13 +0100436 " 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
Bram Moolenaarcada7892018-01-31 19:30:24 +0100451 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
Bram Moolenaare1e714e2018-12-31 23:58:24 +0100506 set tw&
Bram Moolenaarcada7892018-01-31 19:30:24 +0100507 enew!
508endfunc
Bram Moolenaare1e714e2018-12-31 23:58:24 +0100509
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
Bram Moolenaar396b7c72019-10-21 23:08:59 +0200519
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
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100536
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 {
579
580 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
591
592 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 {
602
603
604 a
605
606 a
607
608
609
610
611
612
613
614 a
615
616 a
617
618
619
620
621 }
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 {
634
635 a
636 a
637 XY
638
639 a
640 ab
641 abc
642 ab 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
651
652 a
653 a
654 XY
655
656 a
657 ab
658 abc
659 ab c
660 abXY
661 END
662 exe "normal gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgq"
663 exe "normal o\n" . join(t, "\n")
664
665 let expected =<< trim END
666 {
667
668
669 a
670
671 a
672
673
674
675
676 a
677
678 ab
679
680 abc
681
682 ab
683
684 c
685 ab
686
687
688
689
690
691 a
692
693 a
694
695
696
697
698 a
699
700 ab
701
702 abc
703
704 ab
705
706 c
707 ab
708
709
710 }
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 {
723
724 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
732
733 Xa
734 END
735 exe "normal gqgqjgqgq"
736 exe "normal o\n" . join(t, "\n")
737
738 let expected =<< trim END
739 {
740
741
742 a
743
744
745
746 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 {
760
761 a
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 {
772
773
774 a
775
776
777
778 a
779 }
780 END
781 call assert_equal(expected, getline(1, '$'))
782
783 set tw& fo& ai&
784 bwipe!
785endfunc
786
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100787func 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 {
802
803 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
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200813func Test_mps_multibyte()
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100814 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
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200837" 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
Bram Moolenaar6fd367a2021-03-13 13:14:04 +0100861func Test_mps_error()
862 let encoding_save = &encoding
863
864 for e in ['utf-8', 'latin1']
865 exe 'set encoding=' .. e
866
867 call assert_fails('set mps=<:', 'E474:', e)
868 call assert_fails('set mps=:>', 'E474:', e)
869 call assert_fails('set mps=<>', 'E474:', e)
870 call assert_fails('set mps=<:>_', 'E474:', e)
871 endfor
872
873 let &encoding = encoding_save
874endfunc
875
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100876" Test for ra on multi-byte characters
877func Test_ra_multibyte()
878 new
879 let t =<< trim END
880 ra test
881 abba
882 aab
883 END
884 call setline(1, t)
885 call cursor(1, 1)
886
887 normal jVjra
888
889 let expected =<< trim END
890 ra test
891 aaaa
892 aaa
893 END
894 call assert_equal(expected, getline(1, '$'))
895
896 bwipe!
897endfunc
898
899" Test for 'whichwrap' with multi-byte character
Bram Moolenaara48e78e2019-12-17 20:29:26 +0100900func Test_whichwrap_multi_byte()
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100901 new
902 let t =<< trim END
903 á
904 x
905 END
906 call setline(1, t)
907 call cursor(2, 1)
908
909 set whichwrap+=h
910 normal dh
Bram Moolenaara48e78e2019-12-17 20:29:26 +0100911 set whichwrap&
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100912
913 let expected =<< trim END
914 áx
915 END
916 call assert_equal(expected, getline(1, '$'))
917
918 bwipe!
919endfunc
920
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200921" Test for 'a' and 'w' flags in 'formatoptions'
922func Test_fo_a_w()
923 new
924 setlocal fo+=aw tw=10
925 call feedkeys("iabc abc a abc\<Esc>k0weade", 'xt')
926 call assert_equal(['abc abcde ', 'a abc'], getline(1, '$'))
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +0200927
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200928 " when a line ends with space, it is not broken up.
929 %d
930 call feedkeys("ione two to ", 'xt')
931 call assert_equal('one two to ', getline(1))
932
933 " when a line ends with spaces and backspace is used in the next line, the
934 " last space in the previous line should be removed.
935 %d
936 set backspace=indent,eol,start
937 call setline(1, ['one ', 'two'])
938 exe "normal 2Gi\<BS>"
939 call assert_equal(['one two'], getline(1, '$'))
940 set backspace&
941
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +0200942 " Test for 'a', 'w' and '1' options.
943 setlocal textwidth=0
944 setlocal fo=1aw
945 %d
946 call setline(1, '. foo')
947 normal 72ig
948 call feedkeys('a uu uu uu', 'xt')
949 call assert_equal('g uu uu ', getline(1)[-8:])
950 call assert_equal(['uu. foo'], getline(2, '$'))
951
Bram Moolenaard0a1dee2020-12-20 13:07:48 +0100952 " using backspace or "x" triggers reformat
953 call setline(1, ['1 2 3 4 5 ', '6 7 8 9'])
954 set tw=10
955 set fo=taw
956 set bs=indent,eol,start
957 exe "normal 1G4la\<BS>\<BS>\<Esc>"
958 call assert_equal(['1 2 4 5 6 ', '7 8 9'], getline(1, 2))
959 exe "normal f4xx"
960 call assert_equal(['1 2 5 6 7 ', '8 9'], getline(1, 2))
961
962 set tw=0
963 set fo&
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200964 %bw!
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100965endfunc
966
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +0200967" Test for formatting lines using gq in visual mode
968func Test_visual_gq_format()
969 new
970 call setline(1, ['one two three four', 'five six', 'one two'])
971 setl textwidth=10
972 call feedkeys('ggv$jj', 'xt')
973 redraw!
974 normal gq
975 %d
976 call setline(1, ['one two three four', 'five six', 'one two'])
977 normal G$
978 call feedkeys('v0kk', 'xt')
979 redraw!
980 normal gq
981 setl textwidth&
982 close!
983endfunc
984
985" Test for 'n' flag in 'formatoptions' to format numbered lists
986func Test_fo_n()
987 new
988 setlocal autoindent
989 setlocal textwidth=12
990 setlocal fo=n
991 call setline(1, [' 1) one two three four', ' 2) two'])
992 normal gggqG
993 call assert_equal([' 1) one two', ' three', ' four', ' 2) two'],
994 \ getline(1, '$'))
995 close!
996endfunc
997
998" Test for 'formatlistpat' option
999func Test_formatlistpat()
1000 new
1001 setlocal autoindent
1002 setlocal textwidth=10
1003 setlocal fo=n
1004 setlocal formatlistpat=^\\s*-\\s*
1005 call setline(1, [' - one two three', ' - two'])
1006 normal gggqG
1007 call assert_equal([' - one', ' two', ' three', ' - two'],
1008 \ getline(1, '$'))
1009 close!
1010endfunc
1011
1012" Test for the 'b' and 'v' flags in 'formatoptions'
1013" Text should wrap only if a space character is inserted at or before
1014" 'textwidth'
1015func Test_fo_b()
1016 new
1017 setlocal textwidth=20
1018
1019 setlocal formatoptions=t
1020 call setline(1, 'one two three four')
1021 call feedkeys('Amore', 'xt')
1022 call assert_equal(['one two three', 'fourmore'], getline(1, '$'))
1023
1024 setlocal formatoptions=bt
1025 %d
1026 call setline(1, 'one two three four')
1027 call feedkeys('Amore five', 'xt')
1028 call assert_equal(['one two three fourmore five'], getline(1, '$'))
1029
1030 setlocal formatoptions=bt
1031 %d
1032 call setline(1, 'one two three four')
1033 call feedkeys('A five', 'xt')
1034 call assert_equal(['one two three four', 'five'], getline(1, '$'))
1035
1036 setlocal formatoptions=vt
1037 %d
1038 call setline(1, 'one two three four')
1039 call feedkeys('Amore five', 'xt')
1040 call assert_equal(['one two three fourmore', 'five'], getline(1, '$'))
1041
1042 close!
1043endfunc
1044
1045" Test for the '1' flag in 'formatoptions'. Don't wrap text after a one letter
1046" word.
1047func Test_fo_1()
1048 new
1049 setlocal textwidth=20
1050
1051 setlocal formatoptions=t
1052 call setline(1, 'one two three four')
1053 call feedkeys('A a bird', 'xt')
1054 call assert_equal(['one two three four a', 'bird'], getline(1, '$'))
1055
1056 %d
1057 setlocal formatoptions=t1
1058 call setline(1, 'one two three four')
1059 call feedkeys('A a bird', 'xt')
1060 call assert_equal(['one two three four', 'a bird'], getline(1, '$'))
1061
1062 close!
1063endfunc
1064
1065" Test for 'l' flag in 'formatoptions'. When starting insert mode, if a line
1066" is longer than 'textwidth', then it is not broken.
1067func Test_fo_l()
1068 new
1069 setlocal textwidth=20
1070
1071 setlocal formatoptions=t
1072 call setline(1, 'one two three four five')
1073 call feedkeys('A six', 'xt')
1074 call assert_equal(['one two three four', 'five six'], getline(1, '$'))
1075
1076 %d
1077 setlocal formatoptions=tl
1078 call setline(1, 'one two three four five')
1079 call feedkeys('A six', 'xt')
1080 call assert_equal(['one two three four five six'], getline(1, '$'))
1081
1082 close!
1083endfunc
1084
1085" Test for the '2' flag in 'formatoptions'
1086func Test_fo_2()
1087 new
1088 setlocal autoindent
1089 setlocal formatoptions=t2
1090 setlocal textwidth=30
1091 call setline(1, ["\tfirst line of a paragraph.",
1092 \ "second line of the same paragraph.",
1093 \ "third line."])
1094 normal gggqG
1095 call assert_equal(["\tfirst line of a",
1096 \ "paragraph. second line of the",
1097 \ "same paragraph. third line."], getline(1, '$'))
1098 close!
1099endfunc
1100
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001101" vim: shiftwidth=2 sts=2 expandtab