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