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