blob: 62cefa1a0f2891b9c6bf67e3dbc4a5b88861e69a [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
Bram Moolenaar6e371ec2021-12-12 14:16:39 +0000199func 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 bwipe!
227endfunc
228
Bram Moolenaarcada7892018-01-31 19:30:24 +0100229" Tests for :right, :center and :left on text with embedded TAB.
230func Test_format_align()
231 enew!
232 set tw=65
233
234 " :left alignment
235 call append(0, [
236 \ " test for :left",
237 \ " a a",
238 \ " fa a",
239 \ " dfa a",
240 \ " sdfa a",
241 \ " asdfa a",
242 \ " xasdfa a",
243 \ "asxxdfa a",
244 \ ])
245 %left
246 call assert_equal([
247 \ "test for :left",
248 \ "a a",
249 \ "fa a",
250 \ "dfa a",
251 \ "sdfa a",
252 \ "asdfa a",
253 \ "xasdfa a",
254 \ "asxxdfa a",
255 \ ""
256 \ ], getline(1, '$'))
257 enew!
258
259 " :center alignment
260 call append(0, [
261 \ " test for :center",
262 \ " a a",
263 \ " fa afd asdf",
264 \ " dfa a",
265 \ " sdfa afd asdf",
266 \ " asdfa a",
267 \ " xasdfa asdfasdfasdfasdfasdf",
268 \ "asxxdfa a"
269 \ ])
270 %center
271 call assert_equal([
272 \ " test for :center",
273 \ " a a",
274 \ " fa afd asdf",
275 \ " dfa a",
276 \ " sdfa afd asdf",
277 \ " asdfa a",
278 \ " xasdfa asdfasdfasdfasdfasdf",
279 \ " asxxdfa a",
280 \ ""
281 \ ], getline(1, '$'))
282 enew!
283
284 " :right alignment
285 call append(0, [
286 \ " test for :right",
287 \ " a a",
288 \ " fa a",
289 \ " dfa a",
290 \ " sdfa a",
291 \ " asdfa a",
292 \ " xasdfa a",
293 \ " asxxdfa a",
294 \ " asxa;ofa a",
295 \ " asdfaqwer a",
296 \ " a ax",
297 \ " fa ax",
298 \ " dfa ax",
299 \ " sdfa ax",
300 \ " asdfa ax",
301 \ " xasdfa ax",
302 \ " asxxdfa ax",
303 \ " asxa;ofa ax",
304 \ " asdfaqwer ax",
305 \ " a axx",
306 \ " fa axx",
307 \ " dfa axx",
308 \ " sdfa axx",
309 \ " asdfa axx",
310 \ " xasdfa axx",
311 \ " asxxdfa axx",
312 \ " asxa;ofa axx",
313 \ " asdfaqwer axx",
314 \ " a axxx",
315 \ " fa axxx",
316 \ " dfa axxx",
317 \ " sdfa axxx",
318 \ " asdfa axxx",
319 \ " xasdfa axxx",
320 \ " asxxdfa axxx",
321 \ " asxa;ofa axxx",
322 \ " asdfaqwer axxx",
323 \ " a axxxo",
324 \ " fa axxxo",
325 \ " dfa axxxo",
326 \ " sdfa axxxo",
327 \ " asdfa axxxo",
328 \ " xasdfa axxxo",
329 \ " asxxdfa axxxo",
330 \ " asxa;ofa axxxo",
331 \ " asdfaqwer axxxo",
332 \ " a axxxoi",
333 \ " fa axxxoi",
334 \ " dfa axxxoi",
335 \ " sdfa axxxoi",
336 \ " asdfa axxxoi",
337 \ " xasdfa axxxoi",
338 \ " asxxdfa axxxoi",
339 \ " asxa;ofa axxxoi",
340 \ " asdfaqwer axxxoi",
341 \ " a axxxoik",
342 \ " fa axxxoik",
343 \ " dfa axxxoik",
344 \ " sdfa axxxoik",
345 \ " asdfa axxxoik",
346 \ " xasdfa axxxoik",
347 \ " asxxdfa axxxoik",
348 \ " asxa;ofa axxxoik",
349 \ " asdfaqwer axxxoik",
350 \ " a axxxoike",
351 \ " fa axxxoike",
352 \ " dfa axxxoike",
353 \ " sdfa axxxoike",
354 \ " asdfa axxxoike",
355 \ " xasdfa axxxoike",
356 \ " asxxdfa axxxoike",
357 \ " asxa;ofa axxxoike",
358 \ " asdfaqwer axxxoike",
359 \ " a axxxoikey",
360 \ " fa axxxoikey",
361 \ " dfa axxxoikey",
362 \ " sdfa axxxoikey",
363 \ " asdfa axxxoikey",
364 \ " xasdfa axxxoikey",
365 \ " asxxdfa axxxoikey",
366 \ " asxa;ofa axxxoikey",
367 \ " asdfaqwer axxxoikey",
368 \ ])
369 %right
370 call assert_equal([
371 \ "\t\t\t\t test for :right",
372 \ "\t\t\t\t a a",
373 \ "\t\t\t\t fa a",
374 \ "\t\t\t\t dfa a",
375 \ "\t\t\t\t sdfa a",
376 \ "\t\t\t\t asdfa a",
377 \ "\t\t\t\t xasdfa a",
378 \ "\t\t\t\t asxxdfa a",
379 \ "\t\t\t\t asxa;ofa a",
380 \ "\t\t\t\t asdfaqwer a",
381 \ "\t\t\t\t a ax",
382 \ "\t\t\t\t fa ax",
383 \ "\t\t\t\t dfa ax",
384 \ "\t\t\t\t sdfa ax",
385 \ "\t\t\t\t asdfa ax",
386 \ "\t\t\t\t xasdfa ax",
387 \ "\t\t\t\t asxxdfa ax",
388 \ "\t\t\t\t asxa;ofa ax",
389 \ "\t\t\t\t asdfaqwer ax",
390 \ "\t\t\t\t a axx",
391 \ "\t\t\t\t fa axx",
392 \ "\t\t\t\t dfa axx",
393 \ "\t\t\t\t sdfa axx",
394 \ "\t\t\t\t asdfa axx",
395 \ "\t\t\t\t xasdfa axx",
396 \ "\t\t\t\t asxxdfa axx",
397 \ "\t\t\t\t asxa;ofa axx",
398 \ "\t\t\t\t asdfaqwer axx",
399 \ "\t\t\t\t a axxx",
400 \ "\t\t\t\t fa axxx",
401 \ "\t\t\t\t dfa axxx",
402 \ "\t\t\t\t sdfa axxx",
403 \ "\t\t\t\t asdfa axxx",
404 \ "\t\t\t\t xasdfa axxx",
405 \ "\t\t\t\t asxxdfa axxx",
406 \ "\t\t\t\t asxa;ofa axxx",
407 \ "\t\t\t\t asdfaqwer axxx",
408 \ "\t\t\t\t a axxxo",
409 \ "\t\t\t\t fa axxxo",
410 \ "\t\t\t\t dfa axxxo",
411 \ "\t\t\t\t sdfa axxxo",
412 \ "\t\t\t\t asdfa axxxo",
413 \ "\t\t\t\t xasdfa axxxo",
414 \ "\t\t\t\t asxxdfa axxxo",
415 \ "\t\t\t\t asxa;ofa axxxo",
416 \ "\t\t\t\t asdfaqwer axxxo",
417 \ "\t\t\t\t a axxxoi",
418 \ "\t\t\t\t fa axxxoi",
419 \ "\t\t\t\t dfa axxxoi",
420 \ "\t\t\t\t sdfa axxxoi",
421 \ "\t\t\t\t asdfa axxxoi",
422 \ "\t\t\t\t xasdfa axxxoi",
423 \ "\t\t\t\t asxxdfa axxxoi",
424 \ "\t\t\t\t asxa;ofa axxxoi",
425 \ "\t\t\t\t asdfaqwer axxxoi",
426 \ "\t\t\t\t a axxxoik",
427 \ "\t\t\t\t fa axxxoik",
428 \ "\t\t\t\t dfa axxxoik",
429 \ "\t\t\t\t sdfa axxxoik",
430 \ "\t\t\t\t asdfa axxxoik",
431 \ "\t\t\t\t xasdfa axxxoik",
432 \ "\t\t\t\t asxxdfa axxxoik",
433 \ "\t\t\t\t asxa;ofa axxxoik",
434 \ "\t\t\t\t asdfaqwer axxxoik",
435 \ "\t\t\t\t a axxxoike",
436 \ "\t\t\t\t fa axxxoike",
437 \ "\t\t\t\t dfa axxxoike",
438 \ "\t\t\t\t sdfa axxxoike",
439 \ "\t\t\t\t asdfa axxxoike",
440 \ "\t\t\t\t xasdfa axxxoike",
441 \ "\t\t\t\t asxxdfa axxxoike",
442 \ "\t\t\t\t asxa;ofa axxxoike",
443 \ "\t\t\t\t asdfaqwer axxxoike",
444 \ "\t\t\t\t a axxxoikey",
445 \ "\t\t\t\t fa axxxoikey",
446 \ "\t\t\t\t dfa axxxoikey",
447 \ "\t\t\t\t sdfa axxxoikey",
448 \ "\t\t\t\t asdfa axxxoikey",
449 \ "\t\t\t\t xasdfa axxxoikey",
450 \ "\t\t\t\t asxxdfa axxxoikey",
451 \ "\t\t\t\t asxa;ofa axxxoikey",
452 \ "\t\t\t\t asdfaqwer axxxoikey",
453 \ ""
454 \ ], getline(1, '$'))
455 enew!
456
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100457 " align text with 'wrapmargin'
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100458 50vnew
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100459 call setline(1, ['Vim'])
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100460 setl textwidth=0
461 setl wrapmargin=30
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100462 right
463 call assert_equal("\t\t Vim", getline(1))
Bram Moolenaar0c3064b2020-01-30 16:09:25 +0100464 q!
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100465
Bram Moolenaarea3db912020-02-02 15:32:13 +0100466 " align text with 'rightleft'
467 if has('rightleft')
468 new
469 call setline(1, 'Vim')
470 setlocal rightleft
471 left 20
472 setlocal norightleft
473 call assert_equal("\t\t Vim", getline(1))
474 setlocal rightleft
475 right
476 setlocal norightleft
477 call assert_equal("Vim", getline(1))
478 close!
479 endif
480
Bram Moolenaarcada7892018-01-31 19:30:24 +0100481 set tw&
482endfunc
483
484" Test formatting a paragraph.
485func Test_format_para()
486 enew!
487 set fo+=tcroql tw=72
488
489 call append(0, [
490 \ "xxxxx xx xxxxxx ",
491 \ "xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx",
492 \ "xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx",
493 \ "xx xxxxxxx. xxxx xxxx.",
494 \ "",
495 \ "> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx",
496 \ "> xxxxxx xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx"
497 \ ])
498 exe "normal /xxxxxxxx$\<CR>"
499 normal 0gq6kk
500 call assert_equal([
501 \ "xxxxx xx xxxxxx xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx",
502 \ "xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx xx xxxxxxx.",
503 \ "xxxx xxxx.",
504 \ "",
505 \ "> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx xxxxxx",
506 \ "> xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx",
507 \ ""
508 \ ], getline(1, '$'))
509
510 set fo& tw&
511 enew!
512endfunc
513
514" Test undo after ":%s" and formatting.
515func Test_format_undo()
516 enew!
517 map gg :.,.+2s/^/x/<CR>kk:set tw=3<CR>gqq
518
519 call append(0, [
520 \ "aa aa aa aa",
521 \ "bb bb bb bb",
522 \ "cc cc cc cc"
523 \ ])
524 " undo/redo here to make the next undo only work on the following changes
525 exe "normal i\<C-G>u"
526 call cursor(1,1)
527 normal ggu
528 call assert_equal([
529 \ "aa aa aa aa",
530 \ "bb bb bb bb",
531 \ "cc cc cc cc",
532 \ ""
533 \ ], getline(1, '$'))
534
535 unmap gg
Bram Moolenaare1e714e2018-12-31 23:58:24 +0100536 set tw&
Bram Moolenaarcada7892018-01-31 19:30:24 +0100537 enew!
538endfunc
Bram Moolenaare1e714e2018-12-31 23:58:24 +0100539
540func Test_format_list_auto()
541 new
542 call setline(1, ['1. abc', '2. def', '3. ghi'])
543 set fo=tan ai bs=2
544 call feedkeys("3G0lli\<BS>\<BS>x\<Esc>", 'tx')
545 call assert_equal('2. defx ghi', getline(2))
546 bwipe!
547 set fo& ai& bs&
548endfunc
Bram Moolenaar396b7c72019-10-21 23:08:59 +0200549
550func Test_crash_github_issue_5095()
551 CheckFeature autocmd
552
553 " This used to segfault, see https://github.com/vim/vim/issues/5095
554 augroup testing
555 au BufNew x center
556 augroup END
557
558 next! x
559
560 bw
561 augroup testing
562 au!
563 augroup END
564 augroup! testing
565endfunc
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100566
567" Test for formatting multi-byte text with 'fo=t'
568func Test_tw_2_fo_t()
569 new
570 let t =<< trim END
571 {
572 XYZ
573 abc XYZ
574 }
575 END
576 call setline(1, t)
577 call cursor(2, 1)
578
579 set tw=2 fo=t
580 let t =<< trim END
581 XYZ
582 abc XYZ
583 END
584 exe "normal gqgqjgqgq"
585 exe "normal o\n" . join(t, "\n")
586
587 let expected =<< trim END
588 {
589 XYZ
590 abc
591 XYZ
592
593 XYZ
594 abc
595 XYZ
596 }
597 END
598 call assert_equal(expected, getline(1, '$'))
599
600 set tw& fo&
601 bwipe!
602endfunc
603
604" Test for formatting multi-byte text with 'fo=tm' and 'tw=1'
605func Test_tw_1_fo_tm()
606 new
607 let t =<< trim END
608 {
609
610 Xa
611 X a
612 XY
613 X Y
614 }
615 END
616 call setline(1, t)
617 call cursor(2, 1)
618
619 set tw=1 fo=tm
620 let t =<< trim END
621
622 Xa
623 X a
624 XY
625 X Y
626 END
627 exe "normal gqgqjgqgqjgqgqjgqgqjgqgq"
628 exe "normal o\n" . join(t, "\n")
629
630 let expected =<< trim END
631 {
632
633
634 a
635
636 a
637
638
639
640
641
642
643
644 a
645
646 a
647
648
649
650
651 }
652 END
653 call assert_equal(expected, getline(1, '$'))
654
655 set tw& fo&
656 bwipe!
657endfunc
658
659" Test for formatting multi-byte text with 'fo=tm' and 'tw=2'
660func Test_tw_2_fo_tm()
661 new
662 let t =<< trim END
663 {
664
665 a
666 a
667 XY
668
669 a
670 ab
671 abc
672 ab c
673 abXY
674 }
675 END
676 call setline(1, t)
677 call cursor(2, 1)
678
679 set tw=2 fo=tm
680 let t =<< trim END
681
682 a
683 a
684 XY
685
686 a
687 ab
688 abc
689 ab c
690 abXY
691 END
692 exe "normal gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgq"
693 exe "normal o\n" . join(t, "\n")
694
695 let expected =<< trim END
696 {
697
698
699 a
700
701 a
702
703
704
705
706 a
707
708 ab
709
710 abc
711
712 ab
713
714 c
715 ab
716
717
718
719
720
721 a
722
723 a
724
725
726
727
728 a
729
730 ab
731
732 abc
733
734 ab
735
736 c
737 ab
738
739
740 }
741 END
742 call assert_equal(expected, getline(1, '$'))
743
744 set tw& fo&
745 bwipe!
746endfunc
747
748" Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'autoindent'.
749func Test_tw_2_fo_tm_ai()
750 new
751 let t =<< trim END
752 {
753
754 Xa
755 }
756 END
757 call setline(1, t)
758 call cursor(2, 1)
759
760 set ai tw=2 fo=tm
761 let t =<< trim END
762
763 Xa
764 END
765 exe "normal gqgqjgqgq"
766 exe "normal o\n" . join(t, "\n")
767
768 let expected =<< trim END
769 {
770
771
772 a
773
774
775
776 a
777 }
778 END
779 call assert_equal(expected, getline(1, '$'))
780
781 set tw& fo& ai&
782 bwipe!
783endfunc
784
785" Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'noai'.
786func Test_tw_2_fo_tm_noai()
787 new
788 let t =<< trim END
789 {
790
791 a
792 }
793 END
794 call setline(1, t)
795 call cursor(2, 1)
796
797 set noai tw=2 fo=tm
798 exe "normal gqgqjgqgqo\n X\n Xa"
799
800 let expected =<< trim END
801 {
802
803
804 a
805
806
807
808 a
809 }
810 END
811 call assert_equal(expected, getline(1, '$'))
812
813 set tw& fo& ai&
814 bwipe!
815endfunc
816
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100817func Test_tw_2_fo_tm_replace()
818 new
819 let t =<< trim END
820 {
821
822 }
823 END
824 call setline(1, t)
825 call cursor(2, 1)
826
827 set tw=2 fo=tm
828 exe "normal RXa"
829
830 let expected =<< trim END
831 {
832
833 a
834 }
835 END
836 call assert_equal(expected, getline(1, '$'))
837
838 set tw& fo&
839 bwipe!
840endfunc
841
842" Test for 'matchpairs' with multibyte chars
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200843func Test_mps_multibyte()
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100844 new
845 let t =<< trim END
846 {
847 ‘ two three ’ four
848 }
849 END
850 call setline(1, t)
851 call cursor(2, 1)
852
853 exe "set mps+=\u2018:\u2019"
854 normal d%
855
856 let expected =<< trim END
857 {
858 four
859 }
860 END
861 call assert_equal(expected, getline(1, '$'))
862
863 set mps&
864 bwipe!
865endfunc
866
Bram Moolenaar224a5f12020-04-28 20:29:07 +0200867" Test for 'matchpairs' in latin1 encoding
868func Test_mps_latin1()
869 new
870 let save_enc = &encoding
871 set encoding=latin1
872 call setline(1, 'abc(def)ghi')
873 normal %
874 call assert_equal(8, col('.'))
875 normal %
876 call assert_equal(4, col('.'))
877 call cursor(1, 6)
878 normal [(
879 call assert_equal(4, col('.'))
880 normal %
881 call assert_equal(8, col('.'))
882 call cursor(1, 6)
883 normal ])
884 call assert_equal(8, col('.'))
885 normal %
886 call assert_equal(4, col('.'))
887 let &encoding = save_enc
888 close!
889endfunc
890
Bram Moolenaar5b8cabf2021-04-02 18:55:57 +0200891func Test_empty_matchpairs()
892 split
893 set matchpairs= showmatch
894 call assert_nobeep('call feedkeys("ax\tx\t\<Esc>", "xt")')
895 set matchpairs& noshowmatch
896 bwipe!
897endfunc
898
Bram Moolenaar6fd367a2021-03-13 13:14:04 +0100899func Test_mps_error()
900 let encoding_save = &encoding
901
902 for e in ['utf-8', 'latin1']
903 exe 'set encoding=' .. e
904
905 call assert_fails('set mps=<:', 'E474:', e)
906 call assert_fails('set mps=:>', 'E474:', e)
907 call assert_fails('set mps=<>', 'E474:', e)
908 call assert_fails('set mps=<:>_', 'E474:', e)
909 endfor
910
911 let &encoding = encoding_save
912endfunc
913
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100914" Test for ra on multi-byte characters
915func Test_ra_multibyte()
916 new
917 let t =<< trim END
918 ra test
919 abba
920 aab
921 END
922 call setline(1, t)
923 call cursor(1, 1)
924
925 normal jVjra
926
927 let expected =<< trim END
928 ra test
929 aaaa
930 aaa
931 END
932 call assert_equal(expected, getline(1, '$'))
933
934 bwipe!
935endfunc
936
937" Test for 'whichwrap' with multi-byte character
Bram Moolenaara48e78e2019-12-17 20:29:26 +0100938func Test_whichwrap_multi_byte()
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100939 new
940 let t =<< trim END
941 á
942 x
943 END
944 call setline(1, t)
945 call cursor(2, 1)
946
947 set whichwrap+=h
948 normal dh
Bram Moolenaara48e78e2019-12-17 20:29:26 +0100949 set whichwrap&
Bram Moolenaarafc13bd2019-12-16 22:43:31 +0100950
951 let expected =<< trim END
952 áx
953 END
954 call assert_equal(expected, getline(1, '$'))
955
956 bwipe!
957endfunc
958
Bram Moolenaarca68ae12020-03-30 19:32:53 +0200959" Test for 'a' and 'w' flags in 'formatoptions'
960func Test_fo_a_w()
961 new
962 setlocal fo+=aw tw=10
963 call feedkeys("iabc abc a abc\<Esc>k0weade", 'xt')
964 call assert_equal(['abc abcde ', 'a abc'], getline(1, '$'))
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +0200965
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200966 " when a line ends with space, it is not broken up.
967 %d
968 call feedkeys("ione two to ", 'xt')
969 call assert_equal('one two to ', getline(1))
970
971 " when a line ends with spaces and backspace is used in the next line, the
972 " last space in the previous line should be removed.
973 %d
974 set backspace=indent,eol,start
975 call setline(1, ['one ', 'two'])
976 exe "normal 2Gi\<BS>"
977 call assert_equal(['one two'], getline(1, '$'))
978 set backspace&
979
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +0200980 " Test for 'a', 'w' and '1' options.
981 setlocal textwidth=0
982 setlocal fo=1aw
983 %d
984 call setline(1, '. foo')
985 normal 72ig
986 call feedkeys('a uu uu uu', 'xt')
987 call assert_equal('g uu uu ', getline(1)[-8:])
988 call assert_equal(['uu. foo'], getline(2, '$'))
989
Bram Moolenaard0a1dee2020-12-20 13:07:48 +0100990 " using backspace or "x" triggers reformat
991 call setline(1, ['1 2 3 4 5 ', '6 7 8 9'])
992 set tw=10
993 set fo=taw
994 set bs=indent,eol,start
995 exe "normal 1G4la\<BS>\<BS>\<Esc>"
996 call assert_equal(['1 2 4 5 6 ', '7 8 9'], getline(1, 2))
997 exe "normal f4xx"
998 call assert_equal(['1 2 5 6 7 ', '8 9'], getline(1, 2))
999
Bram Moolenaar6b36d2a2021-08-23 21:19:01 +02001000 " using "cw" leaves cursor in right spot
1001 call setline(1, ['Now we g whether that nation, or',
1002 \ 'any nation so conceived and,'])
1003 set fo=tcqa tw=35
1004 exe "normal 2G0cwx\<Esc>"
1005 call assert_equal(['Now we g whether that nation, or x', 'nation so conceived and,'], getline(1, 2))
1006
Bram Moolenaard0a1dee2020-12-20 13:07:48 +01001007 set tw=0
1008 set fo&
Bram Moolenaarca68ae12020-03-30 19:32:53 +02001009 %bw!
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001010endfunc
1011
Bram Moolenaar2eaeaf32020-05-03 16:04:43 +02001012" Test for formatting lines using gq in visual mode
1013func Test_visual_gq_format()
1014 new
1015 call setline(1, ['one two three four', 'five six', 'one two'])
1016 setl textwidth=10
1017 call feedkeys('ggv$jj', 'xt')
1018 redraw!
1019 normal gq
1020 %d
1021 call setline(1, ['one two three four', 'five six', 'one two'])
1022 normal G$
1023 call feedkeys('v0kk', 'xt')
1024 redraw!
1025 normal gq
1026 setl textwidth&
1027 close!
1028endfunc
1029
1030" Test for 'n' flag in 'formatoptions' to format numbered lists
1031func Test_fo_n()
1032 new
1033 setlocal autoindent
1034 setlocal textwidth=12
1035 setlocal fo=n
1036 call setline(1, [' 1) one two three four', ' 2) two'])
1037 normal gggqG
1038 call assert_equal([' 1) one two', ' three', ' four', ' 2) two'],
1039 \ getline(1, '$'))
1040 close!
1041endfunc
1042
1043" Test for 'formatlistpat' option
1044func Test_formatlistpat()
1045 new
1046 setlocal autoindent
1047 setlocal textwidth=10
1048 setlocal fo=n
1049 setlocal formatlistpat=^\\s*-\\s*
1050 call setline(1, [' - one two three', ' - two'])
1051 normal gggqG
1052 call assert_equal([' - one', ' two', ' three', ' - two'],
1053 \ getline(1, '$'))
1054 close!
1055endfunc
1056
1057" Test for the 'b' and 'v' flags in 'formatoptions'
1058" Text should wrap only if a space character is inserted at or before
1059" 'textwidth'
1060func Test_fo_b()
1061 new
1062 setlocal textwidth=20
1063
1064 setlocal formatoptions=t
1065 call setline(1, 'one two three four')
1066 call feedkeys('Amore', 'xt')
1067 call assert_equal(['one two three', 'fourmore'], getline(1, '$'))
1068
1069 setlocal formatoptions=bt
1070 %d
1071 call setline(1, 'one two three four')
1072 call feedkeys('Amore five', 'xt')
1073 call assert_equal(['one two three fourmore five'], getline(1, '$'))
1074
1075 setlocal formatoptions=bt
1076 %d
1077 call setline(1, 'one two three four')
1078 call feedkeys('A five', 'xt')
1079 call assert_equal(['one two three four', 'five'], getline(1, '$'))
1080
1081 setlocal formatoptions=vt
1082 %d
1083 call setline(1, 'one two three four')
1084 call feedkeys('Amore five', 'xt')
1085 call assert_equal(['one two three fourmore', 'five'], getline(1, '$'))
1086
1087 close!
1088endfunc
1089
1090" Test for the '1' flag in 'formatoptions'. Don't wrap text after a one letter
1091" word.
1092func Test_fo_1()
1093 new
1094 setlocal textwidth=20
1095
1096 setlocal formatoptions=t
1097 call setline(1, 'one two three four')
1098 call feedkeys('A a bird', 'xt')
1099 call assert_equal(['one two three four a', 'bird'], getline(1, '$'))
1100
1101 %d
1102 setlocal formatoptions=t1
1103 call setline(1, 'one two three four')
1104 call feedkeys('A a bird', 'xt')
1105 call assert_equal(['one two three four', 'a bird'], getline(1, '$'))
1106
1107 close!
1108endfunc
1109
1110" Test for 'l' flag in 'formatoptions'. When starting insert mode, if a line
1111" is longer than 'textwidth', then it is not broken.
1112func Test_fo_l()
1113 new
1114 setlocal textwidth=20
1115
1116 setlocal formatoptions=t
1117 call setline(1, 'one two three four five')
1118 call feedkeys('A six', 'xt')
1119 call assert_equal(['one two three four', 'five six'], getline(1, '$'))
1120
1121 %d
1122 setlocal formatoptions=tl
1123 call setline(1, 'one two three four five')
1124 call feedkeys('A six', 'xt')
1125 call assert_equal(['one two three four five six'], getline(1, '$'))
1126
1127 close!
1128endfunc
1129
1130" Test for the '2' flag in 'formatoptions'
1131func Test_fo_2()
1132 new
1133 setlocal autoindent
1134 setlocal formatoptions=t2
1135 setlocal textwidth=30
1136 call setline(1, ["\tfirst line of a paragraph.",
1137 \ "second line of the same paragraph.",
1138 \ "third line."])
1139 normal gggqG
1140 call assert_equal(["\tfirst line of a",
1141 \ "paragraph. second line of the",
1142 \ "same paragraph. third line."], getline(1, '$'))
1143 close!
1144endfunc
1145
Bram Moolenaarafc13bd2019-12-16 22:43:31 +01001146" vim: shiftwidth=2 sts=2 expandtab