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