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