Bram Moolenaar | 209d387 | 2017-11-16 21:52:51 +0100 | [diff] [blame] | 1 | " Tests for the various 'formatoptions' settings |
Bram Moolenaar | 396b7c7 | 2019-10-21 23:08:59 +0200 | [diff] [blame] | 2 | |
| 3 | source check.vim |
| 4 | |
Bram Moolenaar | 209d387 | 2017-11-16 21:52:51 +0100 | [diff] [blame] | 5 | func 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 Moolenaar | c3c3158 | 2019-01-11 22:15:05 +0100 | [diff] [blame] | 169 | " 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 Moolenaar | 209d387 | 2017-11-16 21:52:51 +0100 | [diff] [blame] | 195 | setl ai& tw& fo& si& comments& |
| 196 | enew! |
| 197 | endfunc |
Bram Moolenaar | cada789 | 2018-01-31 19:30:24 +0100 | [diff] [blame] | 198 | |
| 199 | " Tests for :right, :center and :left on text with embedded TAB. |
| 200 | func Test_format_align() |
| 201 | enew! |
| 202 | set tw=65 |
| 203 | |
| 204 | " :left alignment |
| 205 | call append(0, [ |
| 206 | \ " test for :left", |
| 207 | \ " a a", |
| 208 | \ " fa a", |
| 209 | \ " dfa a", |
| 210 | \ " sdfa a", |
| 211 | \ " asdfa a", |
| 212 | \ " xasdfa a", |
| 213 | \ "asxxdfa a", |
| 214 | \ ]) |
| 215 | %left |
| 216 | call assert_equal([ |
| 217 | \ "test for :left", |
| 218 | \ "a a", |
| 219 | \ "fa a", |
| 220 | \ "dfa a", |
| 221 | \ "sdfa a", |
| 222 | \ "asdfa a", |
| 223 | \ "xasdfa a", |
| 224 | \ "asxxdfa a", |
| 225 | \ "" |
| 226 | \ ], getline(1, '$')) |
| 227 | enew! |
| 228 | |
| 229 | " :center alignment |
| 230 | call append(0, [ |
| 231 | \ " test for :center", |
| 232 | \ " a a", |
| 233 | \ " fa afd asdf", |
| 234 | \ " dfa a", |
| 235 | \ " sdfa afd asdf", |
| 236 | \ " asdfa a", |
| 237 | \ " xasdfa asdfasdfasdfasdfasdf", |
| 238 | \ "asxxdfa a" |
| 239 | \ ]) |
| 240 | %center |
| 241 | call assert_equal([ |
| 242 | \ " test for :center", |
| 243 | \ " a a", |
| 244 | \ " fa afd asdf", |
| 245 | \ " dfa a", |
| 246 | \ " sdfa afd asdf", |
| 247 | \ " asdfa a", |
| 248 | \ " xasdfa asdfasdfasdfasdfasdf", |
| 249 | \ " asxxdfa a", |
| 250 | \ "" |
| 251 | \ ], getline(1, '$')) |
| 252 | enew! |
| 253 | |
| 254 | " :right alignment |
| 255 | call append(0, [ |
| 256 | \ " test for :right", |
| 257 | \ " a a", |
| 258 | \ " fa a", |
| 259 | \ " dfa a", |
| 260 | \ " sdfa a", |
| 261 | \ " asdfa a", |
| 262 | \ " xasdfa a", |
| 263 | \ " asxxdfa a", |
| 264 | \ " asxa;ofa a", |
| 265 | \ " asdfaqwer a", |
| 266 | \ " a ax", |
| 267 | \ " fa ax", |
| 268 | \ " dfa ax", |
| 269 | \ " sdfa ax", |
| 270 | \ " asdfa ax", |
| 271 | \ " xasdfa ax", |
| 272 | \ " asxxdfa ax", |
| 273 | \ " asxa;ofa ax", |
| 274 | \ " asdfaqwer ax", |
| 275 | \ " a axx", |
| 276 | \ " fa axx", |
| 277 | \ " dfa axx", |
| 278 | \ " sdfa axx", |
| 279 | \ " asdfa axx", |
| 280 | \ " xasdfa axx", |
| 281 | \ " asxxdfa axx", |
| 282 | \ " asxa;ofa axx", |
| 283 | \ " asdfaqwer axx", |
| 284 | \ " a axxx", |
| 285 | \ " fa axxx", |
| 286 | \ " dfa axxx", |
| 287 | \ " sdfa axxx", |
| 288 | \ " asdfa axxx", |
| 289 | \ " xasdfa axxx", |
| 290 | \ " asxxdfa axxx", |
| 291 | \ " asxa;ofa axxx", |
| 292 | \ " asdfaqwer axxx", |
| 293 | \ " a axxxo", |
| 294 | \ " fa axxxo", |
| 295 | \ " dfa axxxo", |
| 296 | \ " sdfa axxxo", |
| 297 | \ " asdfa axxxo", |
| 298 | \ " xasdfa axxxo", |
| 299 | \ " asxxdfa axxxo", |
| 300 | \ " asxa;ofa axxxo", |
| 301 | \ " asdfaqwer axxxo", |
| 302 | \ " a axxxoi", |
| 303 | \ " fa axxxoi", |
| 304 | \ " dfa axxxoi", |
| 305 | \ " sdfa axxxoi", |
| 306 | \ " asdfa axxxoi", |
| 307 | \ " xasdfa axxxoi", |
| 308 | \ " asxxdfa axxxoi", |
| 309 | \ " asxa;ofa axxxoi", |
| 310 | \ " asdfaqwer axxxoi", |
| 311 | \ " a axxxoik", |
| 312 | \ " fa axxxoik", |
| 313 | \ " dfa axxxoik", |
| 314 | \ " sdfa axxxoik", |
| 315 | \ " asdfa axxxoik", |
| 316 | \ " xasdfa axxxoik", |
| 317 | \ " asxxdfa axxxoik", |
| 318 | \ " asxa;ofa axxxoik", |
| 319 | \ " asdfaqwer axxxoik", |
| 320 | \ " a axxxoike", |
| 321 | \ " fa axxxoike", |
| 322 | \ " dfa axxxoike", |
| 323 | \ " sdfa axxxoike", |
| 324 | \ " asdfa axxxoike", |
| 325 | \ " xasdfa axxxoike", |
| 326 | \ " asxxdfa axxxoike", |
| 327 | \ " asxa;ofa axxxoike", |
| 328 | \ " asdfaqwer axxxoike", |
| 329 | \ " a axxxoikey", |
| 330 | \ " fa axxxoikey", |
| 331 | \ " dfa axxxoikey", |
| 332 | \ " sdfa axxxoikey", |
| 333 | \ " asdfa axxxoikey", |
| 334 | \ " xasdfa axxxoikey", |
| 335 | \ " asxxdfa axxxoikey", |
| 336 | \ " asxa;ofa axxxoikey", |
| 337 | \ " asdfaqwer axxxoikey", |
| 338 | \ ]) |
| 339 | %right |
| 340 | call assert_equal([ |
| 341 | \ "\t\t\t\t test for :right", |
| 342 | \ "\t\t\t\t a a", |
| 343 | \ "\t\t\t\t fa a", |
| 344 | \ "\t\t\t\t dfa a", |
| 345 | \ "\t\t\t\t sdfa a", |
| 346 | \ "\t\t\t\t asdfa a", |
| 347 | \ "\t\t\t\t xasdfa a", |
| 348 | \ "\t\t\t\t asxxdfa a", |
| 349 | \ "\t\t\t\t asxa;ofa a", |
| 350 | \ "\t\t\t\t asdfaqwer a", |
| 351 | \ "\t\t\t\t a ax", |
| 352 | \ "\t\t\t\t fa ax", |
| 353 | \ "\t\t\t\t dfa ax", |
| 354 | \ "\t\t\t\t sdfa ax", |
| 355 | \ "\t\t\t\t asdfa ax", |
| 356 | \ "\t\t\t\t xasdfa ax", |
| 357 | \ "\t\t\t\t asxxdfa ax", |
| 358 | \ "\t\t\t\t asxa;ofa ax", |
| 359 | \ "\t\t\t\t asdfaqwer ax", |
| 360 | \ "\t\t\t\t a axx", |
| 361 | \ "\t\t\t\t fa axx", |
| 362 | \ "\t\t\t\t dfa axx", |
| 363 | \ "\t\t\t\t sdfa axx", |
| 364 | \ "\t\t\t\t asdfa axx", |
| 365 | \ "\t\t\t\t xasdfa axx", |
| 366 | \ "\t\t\t\t asxxdfa axx", |
| 367 | \ "\t\t\t\t asxa;ofa axx", |
| 368 | \ "\t\t\t\t asdfaqwer axx", |
| 369 | \ "\t\t\t\t a axxx", |
| 370 | \ "\t\t\t\t fa axxx", |
| 371 | \ "\t\t\t\t dfa axxx", |
| 372 | \ "\t\t\t\t sdfa axxx", |
| 373 | \ "\t\t\t\t asdfa axxx", |
| 374 | \ "\t\t\t\t xasdfa axxx", |
| 375 | \ "\t\t\t\t asxxdfa axxx", |
| 376 | \ "\t\t\t\t asxa;ofa axxx", |
| 377 | \ "\t\t\t\t asdfaqwer axxx", |
| 378 | \ "\t\t\t\t a axxxo", |
| 379 | \ "\t\t\t\t fa axxxo", |
| 380 | \ "\t\t\t\t dfa axxxo", |
| 381 | \ "\t\t\t\t sdfa axxxo", |
| 382 | \ "\t\t\t\t asdfa axxxo", |
| 383 | \ "\t\t\t\t xasdfa axxxo", |
| 384 | \ "\t\t\t\t asxxdfa axxxo", |
| 385 | \ "\t\t\t\t asxa;ofa axxxo", |
| 386 | \ "\t\t\t\t asdfaqwer axxxo", |
| 387 | \ "\t\t\t\t a axxxoi", |
| 388 | \ "\t\t\t\t fa axxxoi", |
| 389 | \ "\t\t\t\t dfa axxxoi", |
| 390 | \ "\t\t\t\t sdfa axxxoi", |
| 391 | \ "\t\t\t\t asdfa axxxoi", |
| 392 | \ "\t\t\t\t xasdfa axxxoi", |
| 393 | \ "\t\t\t\t asxxdfa axxxoi", |
| 394 | \ "\t\t\t\t asxa;ofa axxxoi", |
| 395 | \ "\t\t\t\t asdfaqwer axxxoi", |
| 396 | \ "\t\t\t\t a axxxoik", |
| 397 | \ "\t\t\t\t fa axxxoik", |
| 398 | \ "\t\t\t\t dfa axxxoik", |
| 399 | \ "\t\t\t\t sdfa axxxoik", |
| 400 | \ "\t\t\t\t asdfa axxxoik", |
| 401 | \ "\t\t\t\t xasdfa axxxoik", |
| 402 | \ "\t\t\t\t asxxdfa axxxoik", |
| 403 | \ "\t\t\t\t asxa;ofa axxxoik", |
| 404 | \ "\t\t\t\t asdfaqwer axxxoik", |
| 405 | \ "\t\t\t\t a axxxoike", |
| 406 | \ "\t\t\t\t fa axxxoike", |
| 407 | \ "\t\t\t\t dfa axxxoike", |
| 408 | \ "\t\t\t\t sdfa axxxoike", |
| 409 | \ "\t\t\t\t asdfa axxxoike", |
| 410 | \ "\t\t\t\t xasdfa axxxoike", |
| 411 | \ "\t\t\t\t asxxdfa axxxoike", |
| 412 | \ "\t\t\t\t asxa;ofa axxxoike", |
| 413 | \ "\t\t\t\t asdfaqwer axxxoike", |
| 414 | \ "\t\t\t\t a axxxoikey", |
| 415 | \ "\t\t\t\t fa axxxoikey", |
| 416 | \ "\t\t\t\t dfa axxxoikey", |
| 417 | \ "\t\t\t\t sdfa axxxoikey", |
| 418 | \ "\t\t\t\t asdfa axxxoikey", |
| 419 | \ "\t\t\t\t xasdfa axxxoikey", |
| 420 | \ "\t\t\t\t asxxdfa axxxoikey", |
| 421 | \ "\t\t\t\t asxa;ofa axxxoikey", |
| 422 | \ "\t\t\t\t asdfaqwer axxxoikey", |
| 423 | \ "" |
| 424 | \ ], getline(1, '$')) |
| 425 | enew! |
| 426 | |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 427 | " align text with 'wrapmargin' |
Bram Moolenaar | 0c3064b | 2020-01-30 16:09:25 +0100 | [diff] [blame] | 428 | 50vnew |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 429 | call setline(1, ['Vim']) |
Bram Moolenaar | 0c3064b | 2020-01-30 16:09:25 +0100 | [diff] [blame] | 430 | setl textwidth=0 |
| 431 | setl wrapmargin=30 |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 432 | right |
| 433 | call assert_equal("\t\t Vim", getline(1)) |
Bram Moolenaar | 0c3064b | 2020-01-30 16:09:25 +0100 | [diff] [blame] | 434 | q! |
Bram Moolenaar | 5d98dc2 | 2020-01-29 21:57:34 +0100 | [diff] [blame] | 435 | |
Bram Moolenaar | ea3db91 | 2020-02-02 15:32:13 +0100 | [diff] [blame] | 436 | " align text with 'rightleft' |
| 437 | if has('rightleft') |
| 438 | new |
| 439 | call setline(1, 'Vim') |
| 440 | setlocal rightleft |
| 441 | left 20 |
| 442 | setlocal norightleft |
| 443 | call assert_equal("\t\t Vim", getline(1)) |
| 444 | setlocal rightleft |
| 445 | right |
| 446 | setlocal norightleft |
| 447 | call assert_equal("Vim", getline(1)) |
| 448 | close! |
| 449 | endif |
| 450 | |
Bram Moolenaar | cada789 | 2018-01-31 19:30:24 +0100 | [diff] [blame] | 451 | set tw& |
| 452 | endfunc |
| 453 | |
| 454 | " Test formatting a paragraph. |
| 455 | func Test_format_para() |
| 456 | enew! |
| 457 | set fo+=tcroql tw=72 |
| 458 | |
| 459 | call append(0, [ |
| 460 | \ "xxxxx xx xxxxxx ", |
| 461 | \ "xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx", |
| 462 | \ "xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx", |
| 463 | \ "xx xxxxxxx. xxxx xxxx.", |
| 464 | \ "", |
| 465 | \ "> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx", |
| 466 | \ "> xxxxxx xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx" |
| 467 | \ ]) |
| 468 | exe "normal /xxxxxxxx$\<CR>" |
| 469 | normal 0gq6kk |
| 470 | call assert_equal([ |
| 471 | \ "xxxxx xx xxxxxx xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx", |
| 472 | \ "xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx xx xxxxxxx.", |
| 473 | \ "xxxx xxxx.", |
| 474 | \ "", |
| 475 | \ "> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx xxxxxx", |
| 476 | \ "> xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx", |
| 477 | \ "" |
| 478 | \ ], getline(1, '$')) |
| 479 | |
| 480 | set fo& tw& |
| 481 | enew! |
| 482 | endfunc |
| 483 | |
| 484 | " Test undo after ":%s" and formatting. |
| 485 | func Test_format_undo() |
| 486 | enew! |
| 487 | map gg :.,.+2s/^/x/<CR>kk:set tw=3<CR>gqq |
| 488 | |
| 489 | call append(0, [ |
| 490 | \ "aa aa aa aa", |
| 491 | \ "bb bb bb bb", |
| 492 | \ "cc cc cc cc" |
| 493 | \ ]) |
| 494 | " undo/redo here to make the next undo only work on the following changes |
| 495 | exe "normal i\<C-G>u" |
| 496 | call cursor(1,1) |
| 497 | normal ggu |
| 498 | call assert_equal([ |
| 499 | \ "aa aa aa aa", |
| 500 | \ "bb bb bb bb", |
| 501 | \ "cc cc cc cc", |
| 502 | \ "" |
| 503 | \ ], getline(1, '$')) |
| 504 | |
| 505 | unmap gg |
Bram Moolenaar | e1e714e | 2018-12-31 23:58:24 +0100 | [diff] [blame] | 506 | set tw& |
Bram Moolenaar | cada789 | 2018-01-31 19:30:24 +0100 | [diff] [blame] | 507 | enew! |
| 508 | endfunc |
Bram Moolenaar | e1e714e | 2018-12-31 23:58:24 +0100 | [diff] [blame] | 509 | |
| 510 | func Test_format_list_auto() |
| 511 | new |
| 512 | call setline(1, ['1. abc', '2. def', '3. ghi']) |
| 513 | set fo=tan ai bs=2 |
| 514 | call feedkeys("3G0lli\<BS>\<BS>x\<Esc>", 'tx') |
| 515 | call assert_equal('2. defx ghi', getline(2)) |
| 516 | bwipe! |
| 517 | set fo& ai& bs& |
| 518 | endfunc |
Bram Moolenaar | 396b7c7 | 2019-10-21 23:08:59 +0200 | [diff] [blame] | 519 | |
| 520 | func Test_crash_github_issue_5095() |
| 521 | CheckFeature autocmd |
| 522 | |
| 523 | " This used to segfault, see https://github.com/vim/vim/issues/5095 |
| 524 | augroup testing |
| 525 | au BufNew x center |
| 526 | augroup END |
| 527 | |
| 528 | next! x |
| 529 | |
| 530 | bw |
| 531 | augroup testing |
| 532 | au! |
| 533 | augroup END |
| 534 | augroup! testing |
| 535 | endfunc |
Bram Moolenaar | afc13bd | 2019-12-16 22:43:31 +0100 | [diff] [blame] | 536 | |
| 537 | " Test for formatting multi-byte text with 'fo=t' |
| 538 | func Test_tw_2_fo_t() |
| 539 | new |
| 540 | let t =<< trim END |
| 541 | { |
| 542 | XYZ |
| 543 | abc XYZ |
| 544 | } |
| 545 | END |
| 546 | call setline(1, t) |
| 547 | call cursor(2, 1) |
| 548 | |
| 549 | set tw=2 fo=t |
| 550 | let t =<< trim END |
| 551 | XYZ |
| 552 | abc XYZ |
| 553 | END |
| 554 | exe "normal gqgqjgqgq" |
| 555 | exe "normal o\n" . join(t, "\n") |
| 556 | |
| 557 | let expected =<< trim END |
| 558 | { |
| 559 | XYZ |
| 560 | abc |
| 561 | XYZ |
| 562 | |
| 563 | XYZ |
| 564 | abc |
| 565 | XYZ |
| 566 | } |
| 567 | END |
| 568 | call assert_equal(expected, getline(1, '$')) |
| 569 | |
| 570 | set tw& fo& |
| 571 | bwipe! |
| 572 | endfunc |
| 573 | |
| 574 | " Test for formatting multi-byte text with 'fo=tm' and 'tw=1' |
| 575 | func Test_tw_1_fo_tm() |
| 576 | new |
| 577 | let t =<< trim END |
| 578 | { |
| 579 | X |
| 580 | Xa |
| 581 | X a |
| 582 | XY |
| 583 | X Y |
| 584 | } |
| 585 | END |
| 586 | call setline(1, t) |
| 587 | call cursor(2, 1) |
| 588 | |
| 589 | set tw=1 fo=tm |
| 590 | let t =<< trim END |
| 591 | X |
| 592 | Xa |
| 593 | X a |
| 594 | XY |
| 595 | X Y |
| 596 | END |
| 597 | exe "normal gqgqjgqgqjgqgqjgqgqjgqgq" |
| 598 | exe "normal o\n" . join(t, "\n") |
| 599 | |
| 600 | let expected =<< trim END |
| 601 | { |
| 602 | X |
| 603 | X |
| 604 | a |
| 605 | X |
| 606 | a |
| 607 | X |
| 608 | Y |
| 609 | X |
| 610 | Y |
| 611 | |
| 612 | X |
| 613 | X |
| 614 | a |
| 615 | X |
| 616 | a |
| 617 | X |
| 618 | Y |
| 619 | X |
| 620 | Y |
| 621 | } |
| 622 | END |
| 623 | call assert_equal(expected, getline(1, '$')) |
| 624 | |
| 625 | set tw& fo& |
| 626 | bwipe! |
| 627 | endfunc |
| 628 | |
| 629 | " Test for formatting multi-byte text with 'fo=tm' and 'tw=2' |
| 630 | func Test_tw_2_fo_tm() |
| 631 | new |
| 632 | let t =<< trim END |
| 633 | { |
| 634 | X |
| 635 | Xa |
| 636 | X a |
| 637 | XY |
| 638 | X Y |
| 639 | aX |
| 640 | abX |
| 641 | abcX |
| 642 | abX c |
| 643 | abXY |
| 644 | } |
| 645 | END |
| 646 | call setline(1, t) |
| 647 | call cursor(2, 1) |
| 648 | |
| 649 | set tw=2 fo=tm |
| 650 | let t =<< trim END |
| 651 | X |
| 652 | Xa |
| 653 | X a |
| 654 | XY |
| 655 | X Y |
| 656 | aX |
| 657 | abX |
| 658 | abcX |
| 659 | abX c |
| 660 | abXY |
| 661 | END |
| 662 | exe "normal gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgq" |
| 663 | exe "normal o\n" . join(t, "\n") |
| 664 | |
| 665 | let expected =<< trim END |
| 666 | { |
| 667 | X |
| 668 | X |
| 669 | a |
| 670 | X |
| 671 | a |
| 672 | X |
| 673 | Y |
| 674 | X |
| 675 | Y |
| 676 | a |
| 677 | X |
| 678 | ab |
| 679 | X |
| 680 | abc |
| 681 | X |
| 682 | ab |
| 683 | X |
| 684 | c |
| 685 | ab |
| 686 | X |
| 687 | Y |
| 688 | |
| 689 | X |
| 690 | X |
| 691 | a |
| 692 | X |
| 693 | a |
| 694 | X |
| 695 | Y |
| 696 | X |
| 697 | Y |
| 698 | a |
| 699 | X |
| 700 | ab |
| 701 | X |
| 702 | abc |
| 703 | X |
| 704 | ab |
| 705 | X |
| 706 | c |
| 707 | ab |
| 708 | X |
| 709 | Y |
| 710 | } |
| 711 | END |
| 712 | call assert_equal(expected, getline(1, '$')) |
| 713 | |
| 714 | set tw& fo& |
| 715 | bwipe! |
| 716 | endfunc |
| 717 | |
| 718 | " Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'autoindent'. |
| 719 | func Test_tw_2_fo_tm_ai() |
| 720 | new |
| 721 | let t =<< trim END |
| 722 | { |
| 723 | X |
| 724 | Xa |
| 725 | } |
| 726 | END |
| 727 | call setline(1, t) |
| 728 | call cursor(2, 1) |
| 729 | |
| 730 | set ai tw=2 fo=tm |
| 731 | let t =<< trim END |
| 732 | X |
| 733 | Xa |
| 734 | END |
| 735 | exe "normal gqgqjgqgq" |
| 736 | exe "normal o\n" . join(t, "\n") |
| 737 | |
| 738 | let expected =<< trim END |
| 739 | { |
| 740 | X |
| 741 | X |
| 742 | a |
| 743 | |
| 744 | X |
| 745 | X |
| 746 | a |
| 747 | } |
| 748 | END |
| 749 | call assert_equal(expected, getline(1, '$')) |
| 750 | |
| 751 | set tw& fo& ai& |
| 752 | bwipe! |
| 753 | endfunc |
| 754 | |
| 755 | " Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'noai'. |
| 756 | func Test_tw_2_fo_tm_noai() |
| 757 | new |
| 758 | let t =<< trim END |
| 759 | { |
| 760 | X |
| 761 | Xa |
| 762 | } |
| 763 | END |
| 764 | call setline(1, t) |
| 765 | call cursor(2, 1) |
| 766 | |
| 767 | set noai tw=2 fo=tm |
| 768 | exe "normal gqgqjgqgqo\n X\n Xa" |
| 769 | |
| 770 | let expected =<< trim END |
| 771 | { |
| 772 | X |
| 773 | X |
| 774 | a |
| 775 | |
| 776 | X |
| 777 | X |
| 778 | a |
| 779 | } |
| 780 | END |
| 781 | call assert_equal(expected, getline(1, '$')) |
| 782 | |
| 783 | set tw& fo& ai& |
| 784 | bwipe! |
| 785 | endfunc |
| 786 | |
Bram Moolenaar | afc13bd | 2019-12-16 22:43:31 +0100 | [diff] [blame] | 787 | func Test_tw_2_fo_tm_replace() |
| 788 | new |
| 789 | let t =<< trim END |
| 790 | { |
| 791 | |
| 792 | } |
| 793 | END |
| 794 | call setline(1, t) |
| 795 | call cursor(2, 1) |
| 796 | |
| 797 | set tw=2 fo=tm |
| 798 | exe "normal RXa" |
| 799 | |
| 800 | let expected =<< trim END |
| 801 | { |
| 802 | X |
| 803 | a |
| 804 | } |
| 805 | END |
| 806 | call assert_equal(expected, getline(1, '$')) |
| 807 | |
| 808 | set tw& fo& |
| 809 | bwipe! |
| 810 | endfunc |
| 811 | |
| 812 | " Test for 'matchpairs' with multibyte chars |
Bram Moolenaar | 224a5f1 | 2020-04-28 20:29:07 +0200 | [diff] [blame] | 813 | func Test_mps_multibyte() |
Bram Moolenaar | afc13bd | 2019-12-16 22:43:31 +0100 | [diff] [blame] | 814 | new |
| 815 | let t =<< trim END |
| 816 | { |
| 817 | ‘ two three ’ four |
| 818 | } |
| 819 | END |
| 820 | call setline(1, t) |
| 821 | call cursor(2, 1) |
| 822 | |
| 823 | exe "set mps+=\u2018:\u2019" |
| 824 | normal d% |
| 825 | |
| 826 | let expected =<< trim END |
| 827 | { |
| 828 | four |
| 829 | } |
| 830 | END |
| 831 | call assert_equal(expected, getline(1, '$')) |
| 832 | |
| 833 | set mps& |
| 834 | bwipe! |
| 835 | endfunc |
| 836 | |
Bram Moolenaar | 224a5f1 | 2020-04-28 20:29:07 +0200 | [diff] [blame] | 837 | " Test for 'matchpairs' in latin1 encoding |
| 838 | func Test_mps_latin1() |
| 839 | new |
| 840 | let save_enc = &encoding |
| 841 | set encoding=latin1 |
| 842 | call setline(1, 'abc(def)ghi') |
| 843 | normal % |
| 844 | call assert_equal(8, col('.')) |
| 845 | normal % |
| 846 | call assert_equal(4, col('.')) |
| 847 | call cursor(1, 6) |
| 848 | normal [( |
| 849 | call assert_equal(4, col('.')) |
| 850 | normal % |
| 851 | call assert_equal(8, col('.')) |
| 852 | call cursor(1, 6) |
| 853 | normal ]) |
| 854 | call assert_equal(8, col('.')) |
| 855 | normal % |
| 856 | call assert_equal(4, col('.')) |
| 857 | let &encoding = save_enc |
| 858 | close! |
| 859 | endfunc |
| 860 | |
Bram Moolenaar | 5b8cabf | 2021-04-02 18:55:57 +0200 | [diff] [blame^] | 861 | func Test_empty_matchpairs() |
| 862 | split |
| 863 | set matchpairs= showmatch |
| 864 | call assert_nobeep('call feedkeys("ax\tx\t\<Esc>", "xt")') |
| 865 | set matchpairs& noshowmatch |
| 866 | bwipe! |
| 867 | endfunc |
| 868 | |
Bram Moolenaar | 6fd367a | 2021-03-13 13:14:04 +0100 | [diff] [blame] | 869 | func Test_mps_error() |
| 870 | let encoding_save = &encoding |
| 871 | |
| 872 | for e in ['utf-8', 'latin1'] |
| 873 | exe 'set encoding=' .. e |
| 874 | |
| 875 | call assert_fails('set mps=<:', 'E474:', e) |
| 876 | call assert_fails('set mps=:>', 'E474:', e) |
| 877 | call assert_fails('set mps=<>', 'E474:', e) |
| 878 | call assert_fails('set mps=<:>_', 'E474:', e) |
| 879 | endfor |
| 880 | |
| 881 | let &encoding = encoding_save |
| 882 | endfunc |
| 883 | |
Bram Moolenaar | afc13bd | 2019-12-16 22:43:31 +0100 | [diff] [blame] | 884 | " Test for ra on multi-byte characters |
| 885 | func Test_ra_multibyte() |
| 886 | new |
| 887 | let t =<< trim END |
| 888 | ra test |
| 889 | abba |
| 890 | aab |
| 891 | END |
| 892 | call setline(1, t) |
| 893 | call cursor(1, 1) |
| 894 | |
| 895 | normal jVjra |
| 896 | |
| 897 | let expected =<< trim END |
| 898 | ra test |
| 899 | aaaa |
| 900 | aaa |
| 901 | END |
| 902 | call assert_equal(expected, getline(1, '$')) |
| 903 | |
| 904 | bwipe! |
| 905 | endfunc |
| 906 | |
| 907 | " Test for 'whichwrap' with multi-byte character |
Bram Moolenaar | a48e78e | 2019-12-17 20:29:26 +0100 | [diff] [blame] | 908 | func Test_whichwrap_multi_byte() |
Bram Moolenaar | afc13bd | 2019-12-16 22:43:31 +0100 | [diff] [blame] | 909 | new |
| 910 | let t =<< trim END |
| 911 | á |
| 912 | x |
| 913 | END |
| 914 | call setline(1, t) |
| 915 | call cursor(2, 1) |
| 916 | |
| 917 | set whichwrap+=h |
| 918 | normal dh |
Bram Moolenaar | a48e78e | 2019-12-17 20:29:26 +0100 | [diff] [blame] | 919 | set whichwrap& |
Bram Moolenaar | afc13bd | 2019-12-16 22:43:31 +0100 | [diff] [blame] | 920 | |
| 921 | let expected =<< trim END |
| 922 | áx |
| 923 | END |
| 924 | call assert_equal(expected, getline(1, '$')) |
| 925 | |
| 926 | bwipe! |
| 927 | endfunc |
| 928 | |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 929 | " Test for 'a' and 'w' flags in 'formatoptions' |
| 930 | func Test_fo_a_w() |
| 931 | new |
| 932 | setlocal fo+=aw tw=10 |
| 933 | call feedkeys("iabc abc a abc\<Esc>k0weade", 'xt') |
| 934 | call assert_equal(['abc abcde ', 'a abc'], getline(1, '$')) |
Bram Moolenaar | 2eaeaf3 | 2020-05-03 16:04:43 +0200 | [diff] [blame] | 935 | |
Bram Moolenaar | 845e0ee | 2020-06-20 16:05:32 +0200 | [diff] [blame] | 936 | " when a line ends with space, it is not broken up. |
| 937 | %d |
| 938 | call feedkeys("ione two to ", 'xt') |
| 939 | call assert_equal('one two to ', getline(1)) |
| 940 | |
| 941 | " when a line ends with spaces and backspace is used in the next line, the |
| 942 | " last space in the previous line should be removed. |
| 943 | %d |
| 944 | set backspace=indent,eol,start |
| 945 | call setline(1, ['one ', 'two']) |
| 946 | exe "normal 2Gi\<BS>" |
| 947 | call assert_equal(['one two'], getline(1, '$')) |
| 948 | set backspace& |
| 949 | |
Bram Moolenaar | 2eaeaf3 | 2020-05-03 16:04:43 +0200 | [diff] [blame] | 950 | " Test for 'a', 'w' and '1' options. |
| 951 | setlocal textwidth=0 |
| 952 | setlocal fo=1aw |
| 953 | %d |
| 954 | call setline(1, '. foo') |
| 955 | normal 72ig |
| 956 | call feedkeys('a uu uu uu', 'xt') |
| 957 | call assert_equal('g uu uu ', getline(1)[-8:]) |
| 958 | call assert_equal(['uu. foo'], getline(2, '$')) |
| 959 | |
Bram Moolenaar | d0a1dee | 2020-12-20 13:07:48 +0100 | [diff] [blame] | 960 | " using backspace or "x" triggers reformat |
| 961 | call setline(1, ['1 2 3 4 5 ', '6 7 8 9']) |
| 962 | set tw=10 |
| 963 | set fo=taw |
| 964 | set bs=indent,eol,start |
| 965 | exe "normal 1G4la\<BS>\<BS>\<Esc>" |
| 966 | call assert_equal(['1 2 4 5 6 ', '7 8 9'], getline(1, 2)) |
| 967 | exe "normal f4xx" |
| 968 | call assert_equal(['1 2 5 6 7 ', '8 9'], getline(1, 2)) |
| 969 | |
| 970 | set tw=0 |
| 971 | set fo& |
Bram Moolenaar | ca68ae1 | 2020-03-30 19:32:53 +0200 | [diff] [blame] | 972 | %bw! |
Bram Moolenaar | afc13bd | 2019-12-16 22:43:31 +0100 | [diff] [blame] | 973 | endfunc |
| 974 | |
Bram Moolenaar | 2eaeaf3 | 2020-05-03 16:04:43 +0200 | [diff] [blame] | 975 | " Test for formatting lines using gq in visual mode |
| 976 | func Test_visual_gq_format() |
| 977 | new |
| 978 | call setline(1, ['one two three four', 'five six', 'one two']) |
| 979 | setl textwidth=10 |
| 980 | call feedkeys('ggv$jj', 'xt') |
| 981 | redraw! |
| 982 | normal gq |
| 983 | %d |
| 984 | call setline(1, ['one two three four', 'five six', 'one two']) |
| 985 | normal G$ |
| 986 | call feedkeys('v0kk', 'xt') |
| 987 | redraw! |
| 988 | normal gq |
| 989 | setl textwidth& |
| 990 | close! |
| 991 | endfunc |
| 992 | |
| 993 | " Test for 'n' flag in 'formatoptions' to format numbered lists |
| 994 | func Test_fo_n() |
| 995 | new |
| 996 | setlocal autoindent |
| 997 | setlocal textwidth=12 |
| 998 | setlocal fo=n |
| 999 | call setline(1, [' 1) one two three four', ' 2) two']) |
| 1000 | normal gggqG |
| 1001 | call assert_equal([' 1) one two', ' three', ' four', ' 2) two'], |
| 1002 | \ getline(1, '$')) |
| 1003 | close! |
| 1004 | endfunc |
| 1005 | |
| 1006 | " Test for 'formatlistpat' option |
| 1007 | func Test_formatlistpat() |
| 1008 | new |
| 1009 | setlocal autoindent |
| 1010 | setlocal textwidth=10 |
| 1011 | setlocal fo=n |
| 1012 | setlocal formatlistpat=^\\s*-\\s* |
| 1013 | call setline(1, [' - one two three', ' - two']) |
| 1014 | normal gggqG |
| 1015 | call assert_equal([' - one', ' two', ' three', ' - two'], |
| 1016 | \ getline(1, '$')) |
| 1017 | close! |
| 1018 | endfunc |
| 1019 | |
| 1020 | " Test for the 'b' and 'v' flags in 'formatoptions' |
| 1021 | " Text should wrap only if a space character is inserted at or before |
| 1022 | " 'textwidth' |
| 1023 | func Test_fo_b() |
| 1024 | new |
| 1025 | setlocal textwidth=20 |
| 1026 | |
| 1027 | setlocal formatoptions=t |
| 1028 | call setline(1, 'one two three four') |
| 1029 | call feedkeys('Amore', 'xt') |
| 1030 | call assert_equal(['one two three', 'fourmore'], getline(1, '$')) |
| 1031 | |
| 1032 | setlocal formatoptions=bt |
| 1033 | %d |
| 1034 | call setline(1, 'one two three four') |
| 1035 | call feedkeys('Amore five', 'xt') |
| 1036 | call assert_equal(['one two three fourmore five'], getline(1, '$')) |
| 1037 | |
| 1038 | setlocal formatoptions=bt |
| 1039 | %d |
| 1040 | call setline(1, 'one two three four') |
| 1041 | call feedkeys('A five', 'xt') |
| 1042 | call assert_equal(['one two three four', 'five'], getline(1, '$')) |
| 1043 | |
| 1044 | setlocal formatoptions=vt |
| 1045 | %d |
| 1046 | call setline(1, 'one two three four') |
| 1047 | call feedkeys('Amore five', 'xt') |
| 1048 | call assert_equal(['one two three fourmore', 'five'], getline(1, '$')) |
| 1049 | |
| 1050 | close! |
| 1051 | endfunc |
| 1052 | |
| 1053 | " Test for the '1' flag in 'formatoptions'. Don't wrap text after a one letter |
| 1054 | " word. |
| 1055 | func Test_fo_1() |
| 1056 | new |
| 1057 | setlocal textwidth=20 |
| 1058 | |
| 1059 | setlocal formatoptions=t |
| 1060 | call setline(1, 'one two three four') |
| 1061 | call feedkeys('A a bird', 'xt') |
| 1062 | call assert_equal(['one two three four a', 'bird'], getline(1, '$')) |
| 1063 | |
| 1064 | %d |
| 1065 | setlocal formatoptions=t1 |
| 1066 | call setline(1, 'one two three four') |
| 1067 | call feedkeys('A a bird', 'xt') |
| 1068 | call assert_equal(['one two three four', 'a bird'], getline(1, '$')) |
| 1069 | |
| 1070 | close! |
| 1071 | endfunc |
| 1072 | |
| 1073 | " Test for 'l' flag in 'formatoptions'. When starting insert mode, if a line |
| 1074 | " is longer than 'textwidth', then it is not broken. |
| 1075 | func Test_fo_l() |
| 1076 | new |
| 1077 | setlocal textwidth=20 |
| 1078 | |
| 1079 | setlocal formatoptions=t |
| 1080 | call setline(1, 'one two three four five') |
| 1081 | call feedkeys('A six', 'xt') |
| 1082 | call assert_equal(['one two three four', 'five six'], getline(1, '$')) |
| 1083 | |
| 1084 | %d |
| 1085 | setlocal formatoptions=tl |
| 1086 | call setline(1, 'one two three four five') |
| 1087 | call feedkeys('A six', 'xt') |
| 1088 | call assert_equal(['one two three four five six'], getline(1, '$')) |
| 1089 | |
| 1090 | close! |
| 1091 | endfunc |
| 1092 | |
| 1093 | " Test for the '2' flag in 'formatoptions' |
| 1094 | func Test_fo_2() |
| 1095 | new |
| 1096 | setlocal autoindent |
| 1097 | setlocal formatoptions=t2 |
| 1098 | setlocal textwidth=30 |
| 1099 | call setline(1, ["\tfirst line of a paragraph.", |
| 1100 | \ "second line of the same paragraph.", |
| 1101 | \ "third line."]) |
| 1102 | normal gggqG |
| 1103 | call assert_equal(["\tfirst line of a", |
| 1104 | \ "paragraph. second line of the", |
| 1105 | \ "same paragraph. third line."], getline(1, '$')) |
| 1106 | close! |
| 1107 | endfunc |
| 1108 | |
Bram Moolenaar | afc13bd | 2019-12-16 22:43:31 +0100 | [diff] [blame] | 1109 | " vim: shiftwidth=2 sts=2 expandtab |