test_textformat.vim 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. " Tests for the various 'formatoptions' settings
  2. source check.vim
  3. func Test_text_format()
  4. enew!
  5. setl noai tw=2 fo=t
  6. call append('$', [
  7. \ '{',
  8. \ ' ',
  9. \ '',
  10. \ '}'])
  11. exe "normal /^{/+1\n0"
  12. normal gRa b
  13. let lnum = line('.')
  14. call assert_equal([
  15. \ 'a',
  16. \ 'b'], getline(lnum - 1, lnum))
  17. normal ggdG
  18. setl ai tw=2 fo=tw
  19. call append('$', [
  20. \ '{',
  21. \ 'a b ',
  22. \ '',
  23. \ 'a ',
  24. \ '}'])
  25. exe "normal /^{/+1\n0"
  26. normal gqgqjjllab
  27. let lnum = line('.')
  28. call assert_equal([
  29. \ 'a ',
  30. \ 'b ',
  31. \ '',
  32. \ 'a ',
  33. \ 'b'], getline(lnum - 4, lnum))
  34. normal ggdG
  35. setl tw=3 fo=t
  36. call append('$', [
  37. \ '{',
  38. \ "a \<C-A>",
  39. \ '}'])
  40. exe "normal /^{/+1\n0"
  41. exe "normal gqgqo\na \<C-V>\<C-A>"
  42. let lnum = line('.')
  43. call assert_equal([
  44. \ 'a',
  45. \ "\<C-A>",
  46. \ '',
  47. \ 'a',
  48. \ "\<C-A>"], getline(lnum - 4, lnum))
  49. normal ggdG
  50. setl tw=2 fo=tcq1 comments=:#
  51. call append('$', [
  52. \ '{',
  53. \ 'a b',
  54. \ '#a b',
  55. \ '}'])
  56. exe "normal /^{/+1\n0"
  57. exe "normal gqgqjgqgqo\na b\n#a b"
  58. let lnum = line('.')
  59. call assert_equal([
  60. \ 'a b',
  61. \ '#a b',
  62. \ '',
  63. \ 'a b',
  64. \ '#a b'], getline(lnum - 4, lnum))
  65. normal ggdG
  66. setl tw=5 fo=tcn comments=:#
  67. call append('$', [
  68. \ '{',
  69. \ ' 1 a',
  70. \ '# 1 a',
  71. \ '}'])
  72. exe "normal /^{/+1\n0"
  73. exe "normal A b\<Esc>jA b"
  74. let lnum = line('.')
  75. call assert_equal([
  76. \ ' 1 a',
  77. \ ' b',
  78. \ '# 1 a',
  79. \ '# b'], getline(lnum - 3, lnum))
  80. normal ggdG
  81. setl tw=5 fo=t2a si
  82. call append('$', [
  83. \ '{',
  84. \ '',
  85. \ ' x a',
  86. \ ' b',
  87. \ ' c',
  88. \ '',
  89. \ '}'])
  90. exe "normal /^{/+3\n0"
  91. exe "normal i \<Esc>A_"
  92. let lnum = line('.')
  93. call assert_equal([
  94. \ '',
  95. \ ' x a',
  96. \ ' b_',
  97. \ ' c',
  98. \ ''], getline(lnum - 2, lnum + 2))
  99. normal ggdG
  100. setl tw=5 fo=qn comments=:#
  101. call append('$', [
  102. \ '{',
  103. \ '# 1 a b',
  104. \ '}'])
  105. exe "normal /^{/+1\n5|"
  106. normal gwap
  107. call assert_equal(5, col('.'))
  108. let lnum = line('.')
  109. call assert_equal([
  110. \ '# 1 a',
  111. \ '# b'], getline(lnum, lnum + 1))
  112. normal ggdG
  113. setl tw=5 fo=q2 comments=:#
  114. call append('$', [
  115. \ '{',
  116. \ '# x',
  117. \ '# a b',
  118. \ '}'])
  119. exe "normal /^{/+1\n0"
  120. normal gwap
  121. let lnum = line('.')
  122. call assert_equal([
  123. \ '# x a',
  124. \ '# b'], getline(lnum, lnum + 1))
  125. normal ggdG
  126. setl tw& fo=a
  127. call append('$', [
  128. \ '{',
  129. \ ' 1aa',
  130. \ ' 2bb',
  131. \ '}'])
  132. exe "normal /^{/+2\n0"
  133. normal I^^
  134. call assert_equal('{ 1aa ^^2bb }', getline('.'))
  135. normal ggdG
  136. setl tw=20 fo=an12wcq comments=s1:/*,mb:*,ex:*/
  137. call append('$', [
  138. \ '/* abc def ghi jkl ',
  139. \ ' * mno pqr stu',
  140. \ ' */'])
  141. exe "normal /mno pqr/\n"
  142. normal A vwx yz
  143. let lnum = line('.')
  144. call assert_equal([
  145. \ ' * mno pqr stu ',
  146. \ ' * vwx yz',
  147. \ ' */'], getline(lnum - 1, lnum + 1))
  148. normal ggdG
  149. setl tw=12 fo=tqnc comments=:#
  150. call setline('.', '# 1 xxxxx')
  151. normal A foobar
  152. call assert_equal([
  153. \ '# 1 xxxxx',
  154. \ '# foobar'], getline(1, 2))
  155. " Test the 'p' flag for 'formatoptions'
  156. " First test without the flag: that it will break "Mr. Feynman" at the space
  157. normal ggdG
  158. setl tw=28 fo=tcq
  159. call setline('.', 'Surely you''re joking, Mr. Feynman!')
  160. normal gqq
  161. call assert_equal([
  162. \ 'Surely you''re joking, Mr.',
  163. \ 'Feynman!'], getline(1, 2))
  164. " Now test with the flag: that it will push the name with the title onto the
  165. " next line
  166. normal ggdG
  167. setl fo+=p
  168. call setline('.', 'Surely you''re joking, Mr. Feynman!')
  169. normal gqq
  170. call assert_equal([
  171. \ 'Surely you''re joking,',
  172. \ 'Mr. Feynman!'], getline(1, 2))
  173. " Ensure that it will still break if two spaces are entered
  174. normal ggdG
  175. call setline('.', 'Surely you''re joking, Mr. Feynman!')
  176. normal gqq
  177. call assert_equal([
  178. \ 'Surely you''re joking, Mr.',
  179. \ 'Feynman!'], getline(1, 2))
  180. setl ai& tw& fo& si& comments&
  181. enew!
  182. endfunc
  183. func Test_format_c_comment()
  184. new
  185. setl ai cindent tw=40 et fo=croql
  186. let text =<< trim END
  187. var = 2345; // asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf
  188. END
  189. call setline(1, text)
  190. normal gql
  191. let expected =<< trim END
  192. var = 2345; // asdf asdf asdf asdf asdf
  193. // asdf asdf asdf asdf asdf
  194. END
  195. call assert_equal(expected, getline(1, '$'))
  196. %del
  197. let text =<< trim END
  198. var = 2345; // asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf asdf
  199. END
  200. call setline(1, text)
  201. normal gql
  202. let expected =<< trim END
  203. var = 2345; // asdf asdf asdf asdf asdf
  204. // asdf asdf asdf asdf asdf
  205. // asdf asdf
  206. END
  207. call assert_equal(expected, getline(1, '$'))
  208. %del
  209. let text =<< trim END
  210. #if 0 // This is another long end of
  211. // line comment that
  212. // wraps.
  213. END
  214. call setline(1, text)
  215. normal gq2j
  216. let expected =<< trim END
  217. #if 0 // This is another long
  218. // end of line comment
  219. // that wraps.
  220. END
  221. call assert_equal(expected, getline(1, '$'))
  222. " Using either "o" or "O" repeats a line comment occupying a whole line.
  223. %del
  224. let text =<< trim END
  225. nop;
  226. // This is a comment
  227. val = val;
  228. END
  229. call setline(1, text)
  230. normal 2Go
  231. let expected =<< trim END
  232. nop;
  233. // This is a comment
  234. //
  235. val = val;
  236. END
  237. call assert_equal(expected, getline(1, '$'))
  238. normal 2GO
  239. let expected =<< trim END
  240. nop;
  241. //
  242. // This is a comment
  243. //
  244. val = val;
  245. END
  246. call assert_equal(expected, getline(1, '$'))
  247. " Using "o" repeats a line comment after a statement, "O" does not.
  248. %del
  249. let text =<< trim END
  250. nop;
  251. val = val; // This is a comment
  252. END
  253. call setline(1, text)
  254. normal 2Go
  255. let expected =<< trim END
  256. nop;
  257. val = val; // This is a comment
  258. //
  259. END
  260. call assert_equal(expected, getline(1, '$'))
  261. 3delete
  262. " No comment repeated with a slash in 'formatoptions'
  263. set fo+=/
  264. normal 2Gox
  265. let expected =<< trim END
  266. nop;
  267. val = val; // This is a comment
  268. x
  269. END
  270. call assert_equal(expected, getline(1, '$'))
  271. " Comment is formatted when it wraps
  272. normal 2GA with some more text added
  273. let expected =<< trim END
  274. nop;
  275. val = val; // This is a comment
  276. // with some more text
  277. // added
  278. x
  279. END
  280. call assert_equal(expected, getline(1, '$'))
  281. set fo-=/
  282. " using 'indentexpr' instead of 'cindent' does not repeat a comment
  283. setl nocindent indentexpr=2
  284. %del
  285. let text =<< trim END
  286. nop;
  287. val = val; // This is a comment
  288. END
  289. call setline(1, text)
  290. normal 2Gox
  291. let expected =<< trim END
  292. nop;
  293. val = val; // This is a comment
  294. x
  295. END
  296. call assert_equal(expected, getline(1, '$'))
  297. setl cindent indentexpr=
  298. 3delete
  299. normal 2GO
  300. let expected =<< trim END
  301. nop;
  302. val = val; // This is a comment
  303. END
  304. call assert_equal(expected, getline(1, '$'))
  305. " Using "o" does not repeat a comment in a string
  306. %del
  307. let text =<< trim END
  308. nop;
  309. val = " // This is not a comment";
  310. END
  311. call setline(1, text)
  312. normal 2Gox
  313. let expected =<< trim END
  314. nop;
  315. val = " // This is not a comment";
  316. x
  317. END
  318. call assert_equal(expected, getline(1, '$'))
  319. " Using CTRL-U after "o" fixes the indent
  320. %del
  321. let text =<< trim END
  322. {
  323. val = val; // This is a comment
  324. END
  325. call setline(1, text)
  326. exe "normal! 2Go\<C-U>x\<Esc>"
  327. let expected =<< trim END
  328. {
  329. val = val; // This is a comment
  330. x
  331. END
  332. call assert_equal(expected, getline(1, '$'))
  333. " typing comment text auto-wraps
  334. %del
  335. call setline(1, text)
  336. exe "normal! 2GA blah more text blah.\<Esc>"
  337. let expected =<< trim END
  338. {
  339. val = val; // This is a comment
  340. // blah more text
  341. // blah.
  342. END
  343. call assert_equal(expected, getline(1, '$'))
  344. bwipe!
  345. endfunc
  346. " Tests for :right, :center and :left on text with embedded TAB.
  347. func Test_format_align()
  348. enew!
  349. set tw=65
  350. " :left alignment
  351. call append(0, [
  352. \ " test for :left",
  353. \ " a a",
  354. \ " fa a",
  355. \ " dfa a",
  356. \ " sdfa a",
  357. \ " asdfa a",
  358. \ " xasdfa a",
  359. \ "asxxdfa a",
  360. \ ])
  361. %left
  362. call assert_equal([
  363. \ "test for :left",
  364. \ "a a",
  365. \ "fa a",
  366. \ "dfa a",
  367. \ "sdfa a",
  368. \ "asdfa a",
  369. \ "xasdfa a",
  370. \ "asxxdfa a",
  371. \ ""
  372. \ ], getline(1, '$'))
  373. enew!
  374. " :center alignment
  375. call append(0, [
  376. \ " test for :center",
  377. \ " a a",
  378. \ " fa afd asdf",
  379. \ " dfa a",
  380. \ " sdfa afd asdf",
  381. \ " asdfa a",
  382. \ " xasdfa asdfasdfasdfasdfasdf",
  383. \ "asxxdfa a"
  384. \ ])
  385. %center
  386. call assert_equal([
  387. \ " test for :center",
  388. \ " a a",
  389. \ " fa afd asdf",
  390. \ " dfa a",
  391. \ " sdfa afd asdf",
  392. \ " asdfa a",
  393. \ " xasdfa asdfasdfasdfasdfasdf",
  394. \ " asxxdfa a",
  395. \ ""
  396. \ ], getline(1, '$'))
  397. enew!
  398. " :right alignment
  399. call append(0, [
  400. \ " test for :right",
  401. \ " a a",
  402. \ " fa a",
  403. \ " dfa a",
  404. \ " sdfa a",
  405. \ " asdfa a",
  406. \ " xasdfa a",
  407. \ " asxxdfa a",
  408. \ " asxa;ofa a",
  409. \ " asdfaqwer a",
  410. \ " a ax",
  411. \ " fa ax",
  412. \ " dfa ax",
  413. \ " sdfa ax",
  414. \ " asdfa ax",
  415. \ " xasdfa ax",
  416. \ " asxxdfa ax",
  417. \ " asxa;ofa ax",
  418. \ " asdfaqwer ax",
  419. \ " a axx",
  420. \ " fa axx",
  421. \ " dfa axx",
  422. \ " sdfa axx",
  423. \ " asdfa axx",
  424. \ " xasdfa axx",
  425. \ " asxxdfa axx",
  426. \ " asxa;ofa axx",
  427. \ " asdfaqwer axx",
  428. \ " a axxx",
  429. \ " fa axxx",
  430. \ " dfa axxx",
  431. \ " sdfa axxx",
  432. \ " asdfa axxx",
  433. \ " xasdfa axxx",
  434. \ " asxxdfa axxx",
  435. \ " asxa;ofa axxx",
  436. \ " asdfaqwer axxx",
  437. \ " a axxxo",
  438. \ " fa axxxo",
  439. \ " dfa axxxo",
  440. \ " sdfa axxxo",
  441. \ " asdfa axxxo",
  442. \ " xasdfa axxxo",
  443. \ " asxxdfa axxxo",
  444. \ " asxa;ofa axxxo",
  445. \ " asdfaqwer axxxo",
  446. \ " a axxxoi",
  447. \ " fa axxxoi",
  448. \ " dfa axxxoi",
  449. \ " sdfa axxxoi",
  450. \ " asdfa axxxoi",
  451. \ " xasdfa axxxoi",
  452. \ " asxxdfa axxxoi",
  453. \ " asxa;ofa axxxoi",
  454. \ " asdfaqwer axxxoi",
  455. \ " a axxxoik",
  456. \ " fa axxxoik",
  457. \ " dfa axxxoik",
  458. \ " sdfa axxxoik",
  459. \ " asdfa axxxoik",
  460. \ " xasdfa axxxoik",
  461. \ " asxxdfa axxxoik",
  462. \ " asxa;ofa axxxoik",
  463. \ " asdfaqwer axxxoik",
  464. \ " a axxxoike",
  465. \ " fa axxxoike",
  466. \ " dfa axxxoike",
  467. \ " sdfa axxxoike",
  468. \ " asdfa axxxoike",
  469. \ " xasdfa axxxoike",
  470. \ " asxxdfa axxxoike",
  471. \ " asxa;ofa axxxoike",
  472. \ " asdfaqwer axxxoike",
  473. \ " a axxxoikey",
  474. \ " fa axxxoikey",
  475. \ " dfa axxxoikey",
  476. \ " sdfa axxxoikey",
  477. \ " asdfa axxxoikey",
  478. \ " xasdfa axxxoikey",
  479. \ " asxxdfa axxxoikey",
  480. \ " asxa;ofa axxxoikey",
  481. \ " asdfaqwer axxxoikey",
  482. \ ])
  483. %right
  484. call assert_equal([
  485. \ "\t\t\t\t test for :right",
  486. \ "\t\t\t\t a a",
  487. \ "\t\t\t\t fa a",
  488. \ "\t\t\t\t dfa a",
  489. \ "\t\t\t\t sdfa a",
  490. \ "\t\t\t\t asdfa a",
  491. \ "\t\t\t\t xasdfa a",
  492. \ "\t\t\t\t asxxdfa a",
  493. \ "\t\t\t\t asxa;ofa a",
  494. \ "\t\t\t\t asdfaqwer a",
  495. \ "\t\t\t\t a ax",
  496. \ "\t\t\t\t fa ax",
  497. \ "\t\t\t\t dfa ax",
  498. \ "\t\t\t\t sdfa ax",
  499. \ "\t\t\t\t asdfa ax",
  500. \ "\t\t\t\t xasdfa ax",
  501. \ "\t\t\t\t asxxdfa ax",
  502. \ "\t\t\t\t asxa;ofa ax",
  503. \ "\t\t\t\t asdfaqwer ax",
  504. \ "\t\t\t\t a axx",
  505. \ "\t\t\t\t fa axx",
  506. \ "\t\t\t\t dfa axx",
  507. \ "\t\t\t\t sdfa axx",
  508. \ "\t\t\t\t asdfa axx",
  509. \ "\t\t\t\t xasdfa axx",
  510. \ "\t\t\t\t asxxdfa axx",
  511. \ "\t\t\t\t asxa;ofa axx",
  512. \ "\t\t\t\t asdfaqwer axx",
  513. \ "\t\t\t\t a axxx",
  514. \ "\t\t\t\t fa axxx",
  515. \ "\t\t\t\t dfa axxx",
  516. \ "\t\t\t\t sdfa axxx",
  517. \ "\t\t\t\t asdfa axxx",
  518. \ "\t\t\t\t xasdfa axxx",
  519. \ "\t\t\t\t asxxdfa axxx",
  520. \ "\t\t\t\t asxa;ofa axxx",
  521. \ "\t\t\t\t asdfaqwer axxx",
  522. \ "\t\t\t\t a axxxo",
  523. \ "\t\t\t\t fa axxxo",
  524. \ "\t\t\t\t dfa axxxo",
  525. \ "\t\t\t\t sdfa axxxo",
  526. \ "\t\t\t\t asdfa axxxo",
  527. \ "\t\t\t\t xasdfa axxxo",
  528. \ "\t\t\t\t asxxdfa axxxo",
  529. \ "\t\t\t\t asxa;ofa axxxo",
  530. \ "\t\t\t\t asdfaqwer axxxo",
  531. \ "\t\t\t\t a axxxoi",
  532. \ "\t\t\t\t fa axxxoi",
  533. \ "\t\t\t\t dfa axxxoi",
  534. \ "\t\t\t\t sdfa axxxoi",
  535. \ "\t\t\t\t asdfa axxxoi",
  536. \ "\t\t\t\t xasdfa axxxoi",
  537. \ "\t\t\t\t asxxdfa axxxoi",
  538. \ "\t\t\t\t asxa;ofa axxxoi",
  539. \ "\t\t\t\t asdfaqwer axxxoi",
  540. \ "\t\t\t\t a axxxoik",
  541. \ "\t\t\t\t fa axxxoik",
  542. \ "\t\t\t\t dfa axxxoik",
  543. \ "\t\t\t\t sdfa axxxoik",
  544. \ "\t\t\t\t asdfa axxxoik",
  545. \ "\t\t\t\t xasdfa axxxoik",
  546. \ "\t\t\t\t asxxdfa axxxoik",
  547. \ "\t\t\t\t asxa;ofa axxxoik",
  548. \ "\t\t\t\t asdfaqwer axxxoik",
  549. \ "\t\t\t\t a axxxoike",
  550. \ "\t\t\t\t fa axxxoike",
  551. \ "\t\t\t\t dfa axxxoike",
  552. \ "\t\t\t\t sdfa axxxoike",
  553. \ "\t\t\t\t asdfa axxxoike",
  554. \ "\t\t\t\t xasdfa axxxoike",
  555. \ "\t\t\t\t asxxdfa axxxoike",
  556. \ "\t\t\t\t asxa;ofa axxxoike",
  557. \ "\t\t\t\t asdfaqwer axxxoike",
  558. \ "\t\t\t\t a axxxoikey",
  559. \ "\t\t\t\t fa axxxoikey",
  560. \ "\t\t\t\t dfa axxxoikey",
  561. \ "\t\t\t\t sdfa axxxoikey",
  562. \ "\t\t\t\t asdfa axxxoikey",
  563. \ "\t\t\t\t xasdfa axxxoikey",
  564. \ "\t\t\t\t asxxdfa axxxoikey",
  565. \ "\t\t\t\t asxa;ofa axxxoikey",
  566. \ "\t\t\t\t asdfaqwer axxxoikey",
  567. \ ""
  568. \ ], getline(1, '$'))
  569. enew!
  570. " align text with 'wrapmargin'
  571. 50vnew
  572. call setline(1, ['Vim'])
  573. setl textwidth=0
  574. setl wrapmargin=30
  575. right
  576. call assert_equal("\t\t Vim", getline(1))
  577. q!
  578. " align text with 'rightleft'
  579. if has('rightleft')
  580. new
  581. call setline(1, 'Vim')
  582. setlocal rightleft
  583. left 20
  584. setlocal norightleft
  585. call assert_equal("\t\t Vim", getline(1))
  586. setlocal rightleft
  587. right
  588. setlocal norightleft
  589. call assert_equal("Vim", getline(1))
  590. close!
  591. endif
  592. set tw&
  593. endfunc
  594. " Test formatting a paragraph.
  595. func Test_format_para()
  596. enew!
  597. set fo+=tcroql tw=72
  598. call append(0, [
  599. \ "xxxxx xx xxxxxx ",
  600. \ "xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx",
  601. \ "xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx",
  602. \ "xx xxxxxxx. xxxx xxxx.",
  603. \ "",
  604. \ "> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx",
  605. \ "> xxxxxx xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx"
  606. \ ])
  607. exe "normal /xxxxxxxx$\<CR>"
  608. normal 0gq6kk
  609. call assert_equal([
  610. \ "xxxxx xx xxxxxx xxxxxxx xxxxxxxxx xxx xxxx xxxxx xxxxx xxx xx",
  611. \ "xxxxxxxxxxxxxxxxxx xxxxx xxxx, xxxx xxxx xxxx xxxx xxx xx xx xx xxxxxxx.",
  612. \ "xxxx xxxx.",
  613. \ "",
  614. \ "> xx xx, xxxx xxxx xxx xxxx xxx xxxxx xxx xxx xxxxxxx xxx xxxxx xxxxxx",
  615. \ "> xxxxxxx: xxxx xxxxxxx, xx xxxxxx xxxx xxxxxxxxxx",
  616. \ ""
  617. \ ], getline(1, '$'))
  618. set fo& tw&
  619. enew!
  620. endfunc
  621. " Test undo after ":%s" and formatting.
  622. func Test_format_undo()
  623. enew!
  624. map gg :.,.+2s/^/x/<CR>kk:set tw=3<CR>gqq
  625. call append(0, [
  626. \ "aa aa aa aa",
  627. \ "bb bb bb bb",
  628. \ "cc cc cc cc"
  629. \ ])
  630. " undo/redo here to make the next undo only work on the following changes
  631. exe "normal i\<C-G>u"
  632. call cursor(1,1)
  633. normal ggu
  634. call assert_equal([
  635. \ "aa aa aa aa",
  636. \ "bb bb bb bb",
  637. \ "cc cc cc cc",
  638. \ ""
  639. \ ], getline(1, '$'))
  640. unmap gg
  641. set tw&
  642. enew!
  643. endfunc
  644. func Test_format_list_auto()
  645. new
  646. call setline(1, ['1. abc', '2. def', '3. ghi'])
  647. set fo=tan ai bs=2
  648. call feedkeys("3G0lli\<BS>\<BS>x\<Esc>", 'tx')
  649. call assert_equal('2. defx ghi', getline(2))
  650. bwipe!
  651. set fo& ai& bs&
  652. endfunc
  653. func Test_crash_github_issue_5095()
  654. CheckFeature autocmd
  655. " This used to segfault, see https://github.com/vim/vim/issues/5095
  656. augroup testing
  657. au BufNew x center
  658. augroup END
  659. next! x
  660. bw
  661. augroup testing
  662. au!
  663. augroup END
  664. augroup! testing
  665. endfunc
  666. " Test for formatting multi-byte text with 'fo=t'
  667. func Test_tw_2_fo_t()
  668. new
  669. let t =<< trim END
  670. {
  671. XYZ
  672. abc XYZ
  673. }
  674. END
  675. call setline(1, t)
  676. call cursor(2, 1)
  677. set tw=2 fo=t
  678. let t =<< trim END
  679. XYZ
  680. abc XYZ
  681. END
  682. exe "normal gqgqjgqgq"
  683. exe "normal o\n" . join(t, "\n")
  684. let expected =<< trim END
  685. {
  686. XYZ
  687. abc
  688. XYZ
  689. XYZ
  690. abc
  691. XYZ
  692. }
  693. END
  694. call assert_equal(expected, getline(1, '$'))
  695. set tw& fo&
  696. bwipe!
  697. endfunc
  698. " Test for formatting multi-byte text with 'fo=tm' and 'tw=1'
  699. func Test_tw_1_fo_tm()
  700. new
  701. let t =<< trim END
  702. {
  703. Xa
  704. X a
  705. XY
  706. X Y
  707. }
  708. END
  709. call setline(1, t)
  710. call cursor(2, 1)
  711. set tw=1 fo=tm
  712. let t =<< trim END
  713. Xa
  714. X a
  715. XY
  716. X Y
  717. END
  718. exe "normal gqgqjgqgqjgqgqjgqgqjgqgq"
  719. exe "normal o\n" . join(t, "\n")
  720. let expected =<< trim END
  721. {
  722. a
  723. a
  724. a
  725. a
  726. }
  727. END
  728. call assert_equal(expected, getline(1, '$'))
  729. set tw& fo&
  730. bwipe!
  731. endfunc
  732. " Test for formatting multi-byte text with 'fo=tm' and 'tw=2'
  733. func Test_tw_2_fo_tm()
  734. new
  735. let t =<< trim END
  736. {
  737. Xa
  738. X a
  739. XY
  740. X Y
  741. aX
  742. abX
  743. abcX
  744. abX c
  745. abXY
  746. }
  747. END
  748. call setline(1, t)
  749. call cursor(2, 1)
  750. set tw=2 fo=tm
  751. let t =<< trim END
  752. Xa
  753. X a
  754. XY
  755. X Y
  756. aX
  757. abX
  758. abcX
  759. abX c
  760. abXY
  761. END
  762. exe "normal gqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgqjgqgq"
  763. exe "normal o\n" . join(t, "\n")
  764. let expected =<< trim END
  765. {
  766. a
  767. a
  768. a
  769. ab
  770. abc
  771. ab
  772. c
  773. ab
  774. a
  775. a
  776. a
  777. ab
  778. abc
  779. ab
  780. c
  781. ab
  782. }
  783. END
  784. call assert_equal(expected, getline(1, '$'))
  785. set tw& fo&
  786. bwipe!
  787. endfunc
  788. " Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'autoindent'.
  789. func Test_tw_2_fo_tm_ai()
  790. new
  791. let t =<< trim END
  792. {
  793. Xa
  794. }
  795. END
  796. call setline(1, t)
  797. call cursor(2, 1)
  798. set ai tw=2 fo=tm
  799. let t =<< trim END
  800. Xa
  801. END
  802. exe "normal gqgqjgqgq"
  803. exe "normal o\n" . join(t, "\n")
  804. let expected =<< trim END
  805. {
  806. a
  807. a
  808. }
  809. END
  810. call assert_equal(expected, getline(1, '$'))
  811. set tw& fo& ai&
  812. bwipe!
  813. endfunc
  814. " Test for formatting multi-byte text with 'fo=tm', 'tw=2' and 'noai'.
  815. func Test_tw_2_fo_tm_noai()
  816. new
  817. let t =<< trim END
  818. {
  819. Xa
  820. }
  821. END
  822. call setline(1, t)
  823. call cursor(2, 1)
  824. set noai tw=2 fo=tm
  825. exe "normal gqgqjgqgqo\n X\n Xa"
  826. let expected =<< trim END
  827. {
  828. a
  829. a
  830. }
  831. END
  832. call assert_equal(expected, getline(1, '$'))
  833. set tw& fo& ai&
  834. bwipe!
  835. endfunc
  836. func Test_tw_2_fo_tm_replace()
  837. new
  838. let t =<< trim END
  839. {
  840. }
  841. END
  842. call setline(1, t)
  843. call cursor(2, 1)
  844. set tw=2 fo=tm
  845. exe "normal RXa"
  846. let expected =<< trim END
  847. {
  848. a
  849. }
  850. END
  851. call assert_equal(expected, getline(1, '$'))
  852. set tw& fo&
  853. bwipe!
  854. endfunc
  855. " Test for 'matchpairs' with multibyte chars
  856. func Test_mps_multibyte()
  857. new
  858. let t =<< trim END
  859. {
  860. ‘ two three ’ four
  861. }
  862. END
  863. call setline(1, t)
  864. call cursor(2, 1)
  865. exe "set mps+=\u2018:\u2019"
  866. normal d%
  867. let expected =<< trim END
  868. {
  869. four
  870. }
  871. END
  872. call assert_equal(expected, getline(1, '$'))
  873. set mps&
  874. bwipe!
  875. endfunc
  876. " Test for 'matchpairs' in latin1 encoding
  877. func Test_mps_latin1()
  878. new
  879. let save_enc = &encoding
  880. " set encoding=latin1
  881. call setline(1, 'abc(def)ghi')
  882. normal %
  883. call assert_equal(8, col('.'))
  884. normal %
  885. call assert_equal(4, col('.'))
  886. call cursor(1, 6)
  887. normal [(
  888. call assert_equal(4, col('.'))
  889. normal %
  890. call assert_equal(8, col('.'))
  891. call cursor(1, 6)
  892. normal ])
  893. call assert_equal(8, col('.'))
  894. normal %
  895. call assert_equal(4, col('.'))
  896. let &encoding = save_enc
  897. close!
  898. endfunc
  899. func Test_empty_matchpairs()
  900. split
  901. set matchpairs= showmatch
  902. call assert_nobeep('call feedkeys("ax\tx\t\<Esc>", "xt")')
  903. set matchpairs& noshowmatch
  904. bwipe!
  905. endfunc
  906. func Test_mps_error()
  907. let encoding_save = &encoding
  908. " for e in ['utf-8', 'latin1']
  909. for e in ['utf-8']
  910. exe 'set encoding=' .. e
  911. call assert_fails('set mps=<:', 'E474:', e)
  912. call assert_fails('set mps=:>', 'E474:', e)
  913. call assert_fails('set mps=<>', 'E474:', e)
  914. call assert_fails('set mps=<:>_', 'E474:', e)
  915. endfor
  916. let &encoding = encoding_save
  917. endfunc
  918. " Test for ra on multi-byte characters
  919. func Test_ra_multibyte()
  920. new
  921. let t =<< trim END
  922. ra test
  923. abba
  924. aab
  925. END
  926. call setline(1, t)
  927. call cursor(1, 1)
  928. normal jVjra
  929. let expected =<< trim END
  930. ra test
  931. aaaa
  932. aaa
  933. END
  934. call assert_equal(expected, getline(1, '$'))
  935. bwipe!
  936. endfunc
  937. " Test for 'whichwrap' with multi-byte character
  938. func Test_whichwrap_multi_byte()
  939. new
  940. let t =<< trim END
  941. á
  942. x
  943. END
  944. call setline(1, t)
  945. call cursor(2, 1)
  946. set whichwrap+=h
  947. normal dh
  948. set whichwrap&
  949. let expected =<< trim END
  950. áx
  951. END
  952. call assert_equal(expected, getline(1, '$'))
  953. bwipe!
  954. endfunc
  955. " Test for 'a' and 'w' flags in 'formatoptions'
  956. func Test_fo_a_w()
  957. new
  958. setlocal fo+=aw tw=10
  959. call feedkeys("iabc abc a abc\<Esc>k0weade", 'xt')
  960. call assert_equal(['abc abcde ', 'a abc'], getline(1, '$'))
  961. " when a line ends with space, it is not broken up.
  962. %d
  963. call feedkeys("ione two to ", 'xt')
  964. call assert_equal('one two to ', getline(1))
  965. " when a line ends with spaces and backspace is used in the next line, the
  966. " last space in the previous line should be removed.
  967. %d
  968. set backspace=indent,eol,start
  969. call setline(1, ['one ', 'two'])
  970. exe "normal 2Gi\<BS>"
  971. call assert_equal(['one two'], getline(1, '$'))
  972. set backspace&
  973. " Test for 'a', 'w' and '1' options.
  974. setlocal textwidth=0
  975. setlocal fo=1aw
  976. %d
  977. call setline(1, '. foo')
  978. normal 72ig
  979. call feedkeys('a uu uu uu', 'xt')
  980. call assert_equal('g uu uu ', getline(1)[-8:])
  981. call assert_equal(['uu. foo'], getline(2, '$'))
  982. " using backspace or "x" triggers reformat
  983. call setline(1, ['1 2 3 4 5 ', '6 7 8 9'])
  984. set tw=10
  985. set fo=taw
  986. set bs=indent,eol,start
  987. exe "normal 1G4la\<BS>\<BS>\<Esc>"
  988. call assert_equal(['1 2 4 5 6 ', '7 8 9'], getline(1, 2))
  989. exe "normal f4xx"
  990. call assert_equal(['1 2 5 6 7 ', '8 9'], getline(1, 2))
  991. " using "cw" leaves cursor in right spot
  992. call setline(1, ['Now we g whether that nation, or',
  993. \ 'any nation so conceived and,'])
  994. set fo=tcqa tw=35
  995. exe "normal 2G0cwx\<Esc>"
  996. call assert_equal(['Now we g whether that nation, or x', 'nation so conceived and,'], getline(1, 2))
  997. set tw=0
  998. set fo&
  999. %bw!
  1000. endfunc
  1001. " Test for formatting lines using gq in visual mode
  1002. func Test_visual_gq_format()
  1003. new
  1004. call setline(1, ['one two three four', 'five six', 'one two'])
  1005. setl textwidth=10
  1006. call feedkeys('ggv$jj', 'xt')
  1007. redraw!
  1008. normal gq
  1009. %d
  1010. call setline(1, ['one two three four', 'five six', 'one two'])
  1011. normal G$
  1012. call feedkeys('v0kk', 'xt')
  1013. redraw!
  1014. normal gq
  1015. setl textwidth&
  1016. close!
  1017. endfunc
  1018. " Test for 'n' flag in 'formatoptions' to format numbered lists
  1019. func Test_fo_n()
  1020. new
  1021. setlocal autoindent
  1022. setlocal textwidth=12
  1023. setlocal fo=n
  1024. call setline(1, [' 1) one two three four', ' 2) two'])
  1025. normal gggqG
  1026. call assert_equal([' 1) one two', ' three', ' four', ' 2) two'],
  1027. \ getline(1, '$'))
  1028. close!
  1029. endfunc
  1030. " Test for 'formatlistpat' option
  1031. func Test_formatlistpat()
  1032. new
  1033. setlocal autoindent
  1034. setlocal textwidth=10
  1035. setlocal fo=n
  1036. setlocal formatlistpat=^\\s*-\\s*
  1037. call setline(1, [' - one two three', ' - two'])
  1038. normal gggqG
  1039. call assert_equal([' - one', ' two', ' three', ' - two'],
  1040. \ getline(1, '$'))
  1041. close!
  1042. endfunc
  1043. " Test for the 'b' and 'v' flags in 'formatoptions'
  1044. " Text should wrap only if a space character is inserted at or before
  1045. " 'textwidth'
  1046. func Test_fo_b()
  1047. new
  1048. setlocal textwidth=20
  1049. setlocal formatoptions=t
  1050. call setline(1, 'one two three four')
  1051. call feedkeys('Amore', 'xt')
  1052. call assert_equal(['one two three', 'fourmore'], getline(1, '$'))
  1053. setlocal formatoptions=bt
  1054. %d
  1055. call setline(1, 'one two three four')
  1056. call feedkeys('Amore five', 'xt')
  1057. call assert_equal(['one two three fourmore five'], getline(1, '$'))
  1058. setlocal formatoptions=bt
  1059. %d
  1060. call setline(1, 'one two three four')
  1061. call feedkeys('A five', 'xt')
  1062. call assert_equal(['one two three four', 'five'], getline(1, '$'))
  1063. setlocal formatoptions=vt
  1064. %d
  1065. call setline(1, 'one two three four')
  1066. call feedkeys('Amore five', 'xt')
  1067. call assert_equal(['one two three fourmore', 'five'], getline(1, '$'))
  1068. close!
  1069. endfunc
  1070. " Test for the '1' flag in 'formatoptions'. Don't wrap text after a one letter
  1071. " word.
  1072. func Test_fo_1()
  1073. new
  1074. setlocal textwidth=20
  1075. setlocal formatoptions=t
  1076. call setline(1, 'one two three four')
  1077. call feedkeys('A a bird', 'xt')
  1078. call assert_equal(['one two three four a', 'bird'], getline(1, '$'))
  1079. %d
  1080. setlocal formatoptions=t1
  1081. call setline(1, 'one two three four')
  1082. call feedkeys('A a bird', 'xt')
  1083. call assert_equal(['one two three four', 'a bird'], getline(1, '$'))
  1084. close!
  1085. endfunc
  1086. " Test for 'l' flag in 'formatoptions'. When starting insert mode, if a line
  1087. " is longer than 'textwidth', then it is not broken.
  1088. func Test_fo_l()
  1089. new
  1090. setlocal textwidth=20
  1091. setlocal formatoptions=t
  1092. call setline(1, 'one two three four five')
  1093. call feedkeys('A six', 'xt')
  1094. call assert_equal(['one two three four', 'five six'], getline(1, '$'))
  1095. %d
  1096. setlocal formatoptions=tl
  1097. call setline(1, 'one two three four five')
  1098. call feedkeys('A six', 'xt')
  1099. call assert_equal(['one two three four five six'], getline(1, '$'))
  1100. close!
  1101. endfunc
  1102. " Test for the '2' flag in 'formatoptions'
  1103. func Test_fo_2()
  1104. new
  1105. setlocal autoindent
  1106. setlocal formatoptions=t2
  1107. setlocal textwidth=30
  1108. call setline(1, ["\tfirst line of a paragraph.",
  1109. \ "second line of the same paragraph.",
  1110. \ "third line."])
  1111. normal gggqG
  1112. call assert_equal(["\tfirst line of a",
  1113. \ "paragraph. second line of the",
  1114. \ "same paragraph. third line."], getline(1, '$'))
  1115. close!
  1116. endfunc
  1117. " This was leaving the cursor after the end of a line. Complicated way to
  1118. " have the problem show up with valgrind.
  1119. func Test_correct_cursor_position()
  1120. " set encoding=iso8859
  1121. new
  1122. norm a000“0
  1123. sil! norm gggg0i0gw0gg
  1124. bwipe!
  1125. set encoding=utf8
  1126. endfunc
  1127. " vim: shiftwidth=2 sts=2 expandtab