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