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