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