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