test_edit.vim 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382
  1. " Test for edit functions
  2. if exists("+t_kD")
  3. let &t_kD="[3;*~"
  4. endif
  5. source check.vim
  6. source screendump.vim
  7. source view_util.vim
  8. " Needs to come first until the bug in getchar() is
  9. " fixed: https://groups.google.com/d/msg/vim_dev/fXL9yme4H4c/bOR-U6_bAQAJ
  10. func Test_edit_00b()
  11. new
  12. call setline(1, ['abc '])
  13. inoreabbr <buffer> h here some more
  14. call cursor(1, 4)
  15. " <esc> expands the abbreviation and ends insert mode
  16. " call feedkeys(":set im\<cr> h\<c-l>:set noim\<cr>", 'tix')
  17. call feedkeys("i h\<esc>", 'tix')
  18. call assert_equal(['abc here some more '], getline(1,'$'))
  19. iunabbr <buffer> h
  20. bw!
  21. endfunc
  22. func Test_edit_01()
  23. " set for Travis CI?
  24. " set nocp noesckeys
  25. new
  26. " 1) empty buffer
  27. call assert_equal([''], getline(1,'$'))
  28. " 2) delete in an empty line
  29. call feedkeys("i\<del>\<esc>", 'tnix')
  30. call assert_equal([''], getline(1,'$'))
  31. %d
  32. " 3) delete one character
  33. call setline(1, 'a')
  34. call feedkeys("i\<del>\<esc>", 'tnix')
  35. call assert_equal([''], getline(1,'$'))
  36. %d
  37. " 4) delete a multibyte character
  38. call setline(1, "\u0401")
  39. call feedkeys("i\<del>\<esc>", 'tnix')
  40. call assert_equal([''], getline(1,'$'))
  41. %d
  42. " 5.1) delete linebreak with 'bs' option containing eol
  43. let _bs=&bs
  44. set bs=eol
  45. call setline(1, ["abc def", "ghi jkl"])
  46. call cursor(1, 1)
  47. call feedkeys("A\<del>\<esc>", 'tnix')
  48. call assert_equal(['abc defghi jkl'], getline(1, 2))
  49. %d
  50. " 5.2) delete linebreak with backspace option w/out eol
  51. set bs=
  52. call setline(1, ["abc def", "ghi jkl"])
  53. call cursor(1, 1)
  54. call feedkeys("A\<del>\<esc>", 'tnix')
  55. call assert_equal(["abc def", "ghi jkl"], getline(1, 2))
  56. let &bs=_bs
  57. bw!
  58. endfunc
  59. func Test_edit_02()
  60. " Change cursor position in InsertCharPre command
  61. new
  62. call setline(1, 'abc')
  63. call cursor(1, 1)
  64. fu! DoIt(...)
  65. call cursor(1, 4)
  66. if len(a:000)
  67. let v:char=a:1
  68. endif
  69. endfu
  70. au InsertCharPre <buffer> :call DoIt('y')
  71. call feedkeys("ix\<esc>", 'tnix')
  72. call assert_equal(['abcy'], getline(1, '$'))
  73. " Setting <Enter> in InsertCharPre
  74. au! InsertCharPre <buffer> :call DoIt("\n")
  75. call setline(1, 'abc')
  76. call cursor(1, 1)
  77. call feedkeys("ix\<esc>", 'tnix')
  78. call assert_equal(['abc', ''], getline(1, '$'))
  79. %d
  80. au! InsertCharPre
  81. " Change cursor position in InsertEnter command
  82. " 1) when setting v:char, keeps changed cursor position
  83. au! InsertEnter <buffer> :call DoIt('y')
  84. call setline(1, 'abc')
  85. call cursor(1, 1)
  86. call feedkeys("ix\<esc>", 'tnix')
  87. call assert_equal(['abxc'], getline(1, '$'))
  88. " 2) when not setting v:char, restores changed cursor position
  89. au! InsertEnter <buffer> :call DoIt()
  90. call setline(1, 'abc')
  91. call cursor(1, 1)
  92. call feedkeys("ix\<esc>", 'tnix')
  93. call assert_equal(['xabc'], getline(1, '$'))
  94. au! InsertEnter
  95. delfu DoIt
  96. bw!
  97. endfunc
  98. func Test_edit_03()
  99. " Change cursor after <c-o> command to end of line
  100. new
  101. call setline(1, 'abc')
  102. call cursor(1, 1)
  103. call feedkeys("i\<c-o>$y\<esc>", 'tnix')
  104. call assert_equal(['abcy'], getline(1, '$'))
  105. %d
  106. call setline(1, 'abc')
  107. call cursor(1, 1)
  108. call feedkeys("i\<c-o>80|y\<esc>", 'tnix')
  109. call assert_equal(['abcy'], getline(1, '$'))
  110. %d
  111. call setline(1, 'abc')
  112. call feedkeys("Ad\<c-o>:s/$/efg/\<cr>hij", 'tnix')
  113. call assert_equal(['hijabcdefg'], getline(1, '$'))
  114. bw!
  115. endfunc
  116. func Test_edit_04()
  117. " test for :stopinsert
  118. new
  119. call setline(1, 'abc')
  120. call cursor(1, 1)
  121. call feedkeys("i\<c-o>:stopinsert\<cr>$", 'tnix')
  122. call feedkeys("aX\<esc>", 'tnix')
  123. call assert_equal(['abcX'], getline(1, '$'))
  124. %d
  125. bw!
  126. endfunc
  127. func Test_edit_05()
  128. " test for folds being opened
  129. new
  130. call setline(1, ['abcX', 'abcX', 'zzzZ'])
  131. call cursor(1, 1)
  132. set foldmethod=manual foldopen+=insert
  133. " create fold for those two lines
  134. norm! Vjzf
  135. call feedkeys("$ay\<esc>", 'tnix')
  136. call assert_equal(['abcXy', 'abcX', 'zzzZ'], getline(1, '$'))
  137. %d
  138. call setline(1, ['abcX', 'abcX', 'zzzZ'])
  139. call cursor(1, 1)
  140. set foldmethod=manual foldopen-=insert
  141. " create fold for those two lines
  142. norm! Vjzf
  143. call feedkeys("$ay\<esc>", 'tnix')
  144. call assert_equal(['abcXy', 'abcX', 'zzzZ'], getline(1, '$'))
  145. %d
  146. bw!
  147. endfunc
  148. func Test_edit_06()
  149. " Test in diff mode
  150. if !has("diff") || !executable("diff")
  151. return
  152. endif
  153. new
  154. call setline(1, ['abc', 'xxx', 'yyy'])
  155. vnew
  156. call setline(1, ['abc', 'zzz', 'xxx', 'yyy'])
  157. wincmd p
  158. diffthis
  159. wincmd p
  160. diffthis
  161. wincmd p
  162. call cursor(2, 1)
  163. norm! zt
  164. call feedkeys("Ozzz\<esc>", 'tnix')
  165. call assert_equal(['abc', 'zzz', 'xxx', 'yyy'], getline(1,'$'))
  166. bw!
  167. bw!
  168. endfunc
  169. func Test_edit_07()
  170. " 1) Test with completion <c-l> when popupmenu is visible
  171. new
  172. call setline(1, 'J')
  173. func! ListMonths()
  174. call complete(col('.')-1, ['January', 'February', 'March',
  175. \ 'April', 'May', 'June', 'July', 'August', 'September',
  176. \ 'October', 'November', 'December'])
  177. return ''
  178. endfunc
  179. inoremap <buffer> <F5> <C-R>=ListMonths()<CR>
  180. call feedkeys("A\<f5>\<c-p>". repeat("\<down>", 6)."\<c-l>\<down>\<c-l>\<cr>", 'tx')
  181. call assert_equal(['July'], getline(1,'$'))
  182. " 1) Test completion when InsertCharPre kicks in
  183. %d
  184. call setline(1, 'J')
  185. fu! DoIt()
  186. if v:char=='u'
  187. let v:char='an'
  188. endif
  189. endfu
  190. au InsertCharPre <buffer> :call DoIt()
  191. call feedkeys("A\<f5>\<c-p>u\<C-Y>\<c-l>\<cr>", 'tx')
  192. call assert_equal(["Jan\<c-l>",''], 1->getline('$'))
  193. %d
  194. call setline(1, 'J')
  195. call feedkeys("A\<f5>\<c-p>u\<down>\<c-l>\<cr>", 'tx')
  196. call assert_equal(["January"], 1->getline('$'))
  197. delfu ListMonths
  198. delfu DoIt
  199. iunmap <buffer> <f5>
  200. bw!
  201. endfunc
  202. func Test_edit_08()
  203. throw 'skipped: moved to test/functional/legacy/edit_spec.lua'
  204. " reset insertmode from i_ctrl-r_=
  205. let g:bufnr = bufnr('%')
  206. new
  207. call setline(1, ['abc'])
  208. call cursor(1, 4)
  209. call feedkeys(":set im\<cr>ZZZ\<c-r>=setbufvar(g:bufnr,'&im', 0)\<cr>",'tnix')
  210. call assert_equal(['abZZZc'], getline(1,'$'))
  211. call assert_equal([0, 1, 1, 0], getpos('.'))
  212. call assert_false(0, '&im')
  213. bw!
  214. unlet g:bufnr
  215. endfunc
  216. func Test_edit_09()
  217. " test i_CTRL-\ combinations
  218. new
  219. call setline(1, ['abc', 'def', 'ghi'])
  220. call cursor(1, 1)
  221. " 1) CTRL-\ CTLR-N
  222. " call feedkeys(":set im\<cr>\<c-\>\<c-n>ccABC\<c-l>", 'txin')
  223. call feedkeys("i\<c-\>\<c-n>ccABC\<esc>", 'txin')
  224. call assert_equal(['ABC', 'def', 'ghi'], getline(1,'$'))
  225. call setline(1, ['ABC', 'def', 'ghi'])
  226. " 2) CTRL-\ CTLR-G
  227. " CTRL-\ CTRL-G goes to Insert mode when 'insertmode' is set, but
  228. " 'insertmode' is now removed so skip this test
  229. " call feedkeys("j0\<c-\>\<c-g>ZZZ\<cr>\<esc>", 'txin')
  230. " call assert_equal(['ABC', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
  231. " call feedkeys("I\<c-\>\<c-g>YYY\<c-l>", 'txin')
  232. " call assert_equal(['ABC', 'ZZZ', 'YYYdef', 'ghi'], getline(1,'$'))
  233. " set noinsertmode
  234. " 3) CTRL-\ CTRL-O
  235. call setline(1, ['ABC', 'ZZZ', 'def', 'ghi'])
  236. call cursor(1, 1)
  237. call feedkeys("A\<c-o>ix", 'txin')
  238. call assert_equal(['ABxC', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
  239. call feedkeys("A\<c-\>\<c-o>ix", 'txin')
  240. call assert_equal(['ABxCx', 'ZZZ', 'def', 'ghi'], getline(1,'$'))
  241. " 4) CTRL-\ a (should be inserted literally, not special after <c-\>
  242. call setline(1, ['ABC', 'ZZZ', 'def', 'ghi'])
  243. call cursor(1, 1)
  244. call feedkeys("A\<c-\>a", 'txin')
  245. call assert_equal(["ABC\<c-\>a", 'ZZZ', 'def', 'ghi'], getline(1, '$'))
  246. bw!
  247. endfunc
  248. func Test_edit_11()
  249. " Test that indenting kicks in
  250. new
  251. set cindent
  252. call setline(1, ['{', '', ''])
  253. call cursor(2, 1)
  254. call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
  255. call cursor(3, 1)
  256. call feedkeys("\<Insert>/* comment */", 'tnix')
  257. call assert_equal(['{', "\<tab>int c;", "/* comment */"], getline(1, '$'))
  258. " added changed cindentkeys slightly
  259. set cindent cinkeys+=*/
  260. call setline(1, ['{', '', ''])
  261. call cursor(2, 1)
  262. call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
  263. call cursor(3, 1)
  264. call feedkeys("i/* comment */", 'tnix')
  265. call assert_equal(['{', "\<tab>int c;", "\<tab>/* comment */"], getline(1, '$'))
  266. set cindent cinkeys+==end
  267. call feedkeys("oend\<cr>\<esc>", 'tnix')
  268. call assert_equal(['{', "\<tab>int c;", "\<tab>/* comment */", "\tend", ''], getline(1, '$'))
  269. set cinkeys-==end
  270. %d
  271. " Use indentexpr instead of cindenting
  272. func! Do_Indent()
  273. if v:lnum == 3
  274. return 3*shiftwidth()
  275. else
  276. return 2*shiftwidth()
  277. endif
  278. endfunc
  279. setl indentexpr=Do_Indent() indentkeys+=*/
  280. call setline(1, ['{', '', ''])
  281. call cursor(2, 1)
  282. call feedkeys("i\<c-f>int c;\<esc>", 'tnix')
  283. call cursor(3, 1)
  284. call feedkeys("i/* comment */", 'tnix')
  285. call assert_equal(['{', "\<tab>\<tab>int c;", "\<tab>\<tab>\<tab>/* comment */"], getline(1, '$'))
  286. set cinkeys&vim indentkeys&vim
  287. set nocindent indentexpr=
  288. delfu Do_Indent
  289. bw!
  290. endfunc
  291. func Test_edit_11_indentexpr()
  292. " Test that indenting kicks in
  293. new
  294. " Use indentexpr instead of cindenting
  295. func! Do_Indent()
  296. let pline=prevnonblank(v:lnum)
  297. if empty(getline(v:lnum))
  298. if getline(pline) =~ 'if\|then'
  299. return shiftwidth()
  300. else
  301. return 0
  302. endif
  303. else
  304. return 0
  305. endif
  306. endfunc
  307. setl indentexpr=Do_Indent() indentkeys+=0=then,0=fi
  308. call setline(1, ['if [ $this ]'])
  309. call cursor(1, 1)
  310. call feedkeys("othen\<cr>that\<cr>fi", 'tnix')
  311. call assert_equal(['if [ $this ]', "then", "\<tab>that", "fi"], getline(1, '$'))
  312. set cinkeys&vim indentkeys&vim
  313. set nocindent indentexpr=
  314. delfu Do_Indent
  315. " Using a script-local function
  316. func s:NewIndentExpr()
  317. endfunc
  318. set indentexpr=s:NewIndentExpr()
  319. call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
  320. call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &g:indentexpr)
  321. set indentexpr=<SID>NewIndentExpr()
  322. call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
  323. call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &g:indentexpr)
  324. setlocal indentexpr=
  325. setglobal indentexpr=s:NewIndentExpr()
  326. call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &g:indentexpr)
  327. call assert_equal('', &indentexpr)
  328. new
  329. call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
  330. bw!
  331. setglobal indentexpr=<SID>NewIndentExpr()
  332. call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &g:indentexpr)
  333. call assert_equal('', &indentexpr)
  334. new
  335. call assert_equal(expand('<SID>') .. 'NewIndentExpr()', &indentexpr)
  336. bw!
  337. set indentexpr&
  338. bw!
  339. endfunc
  340. " Test changing indent in replace mode
  341. func Test_edit_12()
  342. new
  343. call setline(1, ["\tabc", "\tdef"])
  344. call cursor(2, 4)
  345. call feedkeys("R^\<c-d>", 'tnix')
  346. call assert_equal(["\tabc", "def"], getline(1, '$'))
  347. call assert_equal([0, 2, 2, 0], '.'->getpos())
  348. %d
  349. call setline(1, ["\tabc", "\t\tdef"])
  350. call cursor(2, 2)
  351. call feedkeys("R^\<c-d>", 'tnix')
  352. call assert_equal(["\tabc", "def"], getline(1, '$'))
  353. call assert_equal([0, 2, 1, 0], getpos('.'))
  354. %d
  355. call setline(1, ["\tabc", "\t\tdef"])
  356. call cursor(2, 2)
  357. call feedkeys("R\<c-t>", 'tnix')
  358. call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
  359. call assert_equal([0, 2, 2, 0], getpos('.'))
  360. bw!
  361. 10vnew
  362. call setline(1, ["\tabc", "\t\tdef"])
  363. call cursor(2, 2)
  364. call feedkeys("R\<c-t>", 'tnix')
  365. call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
  366. call assert_equal([0, 2, 2, 0], getpos('.'))
  367. %d
  368. set sw=4
  369. call setline(1, ["\tabc", "\t\tdef"])
  370. call cursor(2, 2)
  371. call feedkeys("R\<c-t>\<c-t>", 'tnix')
  372. call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
  373. call assert_equal([0, 2, 2, 0], getpos('.'))
  374. %d
  375. call setline(1, ["\tabc", "\t\tdef"])
  376. call cursor(2, 2)
  377. call feedkeys("R\<c-t>\<c-t>", 'tnix')
  378. call assert_equal(["\tabc", "\t\t\tdef"], getline(1, '$'))
  379. call assert_equal([0, 2, 2, 0], getpos('.'))
  380. set sw&
  381. " In replace mode, after hitting enter in a line with tab characters,
  382. " pressing backspace should restore the tab characters.
  383. %d
  384. setlocal autoindent backspace=2
  385. call setline(1, "\tone\t\ttwo")
  386. exe "normal ggRred\<CR>six" .. repeat("\<BS>", 8)
  387. call assert_equal(["\tone\t\ttwo"], getline(1, '$'))
  388. bw!
  389. endfunc
  390. func Test_edit_13()
  391. " Test smartindenting
  392. new
  393. set smartindent autoindent
  394. call setline(1, ["\tabc"])
  395. call feedkeys("A {\<cr>more\<cr>}\<esc>", 'tnix')
  396. call assert_equal(["\tabc {", "\t\tmore", "\t}"], getline(1, '$'))
  397. set smartindent& autoindent&
  398. bwipe!
  399. " Test autoindent removing indent of blank line.
  400. new
  401. call setline(1, ' foo bar baz')
  402. set autoindent
  403. exe "normal 0eea\<CR>\<CR>\<Esc>"
  404. call assert_equal(" foo bar", getline(1))
  405. call assert_equal("", getline(2))
  406. call assert_equal(" baz", getline(3))
  407. set autoindent&
  408. " pressing <C-U> to erase line should keep the indent with 'autoindent'
  409. set backspace=2 autoindent
  410. %d
  411. exe "normal i\tone\<CR>three\<C-U>two"
  412. call assert_equal(["\tone", "\ttwo"], getline(1, '$'))
  413. set backspace& autoindent&
  414. bwipe!
  415. endfunc
  416. " Test for autoindent removing indent when insert mode is stopped. Some parts
  417. " of the code is exercised only when interactive mode is used. So use Vim in a
  418. " terminal.
  419. func Test_autoindent_remove_indent()
  420. CheckRunVimInTerminal
  421. let buf = RunVimInTerminal('-N Xarifile', {'rows': 6, 'cols' : 20})
  422. call TermWait(buf)
  423. call term_sendkeys(buf, ":set autoindent\n")
  424. " leaving insert mode in a new line with indent added by autoindent, should
  425. " remove the indent.
  426. call term_sendkeys(buf, "i\<Tab>foo\<CR>\<Esc>")
  427. " Need to delay for some time, otherwise the code in getchar.c will not be
  428. " exercised.
  429. call TermWait(buf, 50)
  430. " when a line is wrapped and the cursor is at the start of the second line,
  431. " leaving insert mode, should move the cursor back to the first line.
  432. call term_sendkeys(buf, "o" .. repeat('x', 20) .. "\<Esc>")
  433. " Need to delay for some time, otherwise the code in getchar.c will not be
  434. " exercised.
  435. call TermWait(buf, 50)
  436. call term_sendkeys(buf, ":w\n")
  437. call TermWait(buf)
  438. call StopVimInTerminal(buf)
  439. call assert_equal(["\tfoo", '', repeat('x', 20)], readfile('Xarifile'))
  440. call delete('Xarifile')
  441. endfunc
  442. func Test_edit_esc_after_CR_autoindent()
  443. new
  444. setlocal autoindent
  445. autocmd InsertLeavePre * let g:prev_cursor = getpos('.')
  446. call setline(1, 'foobar')
  447. exe "normal! $hi\<CR>\<Esc>"
  448. call assert_equal(['foob', 'ar'], getline(1, '$'))
  449. call assert_equal([0, 2, 1, 0], getpos('.'))
  450. call assert_equal([0, 2, 1, 0], getpos("'^"))
  451. call assert_equal([0, 2, 1, 0], g:prev_cursor)
  452. %d
  453. call setline(1, 'foobar')
  454. exe "normal! $i\<CR>\<Esc>"
  455. call assert_equal(['fooba', 'r'], getline(1, '$'))
  456. call assert_equal([0, 2, 1, 0], getpos('.'))
  457. call assert_equal([0, 2, 1, 0], getpos("'^"))
  458. call assert_equal([0, 2, 1, 0], g:prev_cursor)
  459. %d
  460. call setline(1, 'foobar')
  461. exe "normal! A\<CR>\<Esc>"
  462. call assert_equal(['foobar', ''], getline(1, '$'))
  463. call assert_equal([0, 2, 1, 0], getpos('.'))
  464. call assert_equal([0, 2, 1, 0], getpos("'^"))
  465. call assert_equal([0, 2, 1, 0], g:prev_cursor)
  466. %d
  467. call setline(1, ' foobar')
  468. exe "normal! $hi\<CR>\<Esc>"
  469. call assert_equal([' foob', ' ar'], getline(1, '$'))
  470. call assert_equal([0, 2, 2, 0], getpos('.'))
  471. call assert_equal([0, 2, 3, 0], getpos("'^"))
  472. call assert_equal([0, 2, 3, 0], g:prev_cursor)
  473. %d
  474. call setline(1, ' foobar')
  475. exe "normal! $i\<CR>\<Esc>"
  476. call assert_equal([' fooba', ' r'], getline(1, '$'))
  477. call assert_equal([0, 2, 2, 0], getpos('.'))
  478. call assert_equal([0, 2, 3, 0], getpos("'^"))
  479. call assert_equal([0, 2, 3, 0], g:prev_cursor)
  480. %d
  481. call setline(1, ' foobar')
  482. exe "normal! A\<CR>\<Esc>"
  483. call assert_equal([' foobar', ''], getline(1, '$'))
  484. call assert_equal([0, 2, 1, 0], getpos('.'))
  485. call assert_equal([0, 2, 1, 0], getpos("'^"))
  486. call assert_equal([0, 2, 1, 0], g:prev_cursor)
  487. %d
  488. autocmd! InsertLeavePre
  489. unlet g:prev_cursor
  490. bwipe!
  491. endfunc
  492. func Test_edit_CR()
  493. " Test for <CR> in insert mode
  494. " basically only in quickfix mode it's tested, the rest
  495. " has been taken care of by other tests
  496. CheckFeature quickfix
  497. botright new
  498. call writefile(range(1, 10), 'Xqflist.txt', 'D')
  499. call setqflist([{'filename': 'Xqflist.txt', 'lnum': 2}])
  500. copen
  501. set modifiable
  502. call feedkeys("A\<cr>", 'tnix')
  503. call assert_equal('Xqflist.txt', bufname(''))
  504. call assert_equal(2, line('.'))
  505. cclose
  506. botright new
  507. call setloclist(0, [{'filename': 'Xqflist.txt', 'lnum': 10}])
  508. lopen
  509. set modifiable
  510. call feedkeys("A\<cr>", 'tnix')
  511. call assert_equal('Xqflist.txt', bufname(''))
  512. call assert_equal(10, line('.'))
  513. call feedkeys("A\<Enter>", 'tnix')
  514. call feedkeys("A\<kEnter>", 'tnix')
  515. call feedkeys("A\n", 'tnix')
  516. call feedkeys("A\r", 'tnix')
  517. call assert_equal(map(range(1, 10), 'string(v:val)') + ['', '', '', ''], getline(1, '$'))
  518. bw!
  519. lclose
  520. endfunc
  521. func Test_edit_CTRL_()
  522. CheckFeature rightleft
  523. " disabled for Windows builds, why?
  524. CheckNotMSWindows
  525. let _encoding=&encoding
  526. set encoding=utf-8
  527. " Test for CTRL-_
  528. new
  529. call setline(1, ['abc'])
  530. call cursor(1, 1)
  531. call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
  532. call assert_equal(["\<C-_>xyzabc"], getline(1, '$'))
  533. call assert_false(&revins)
  534. set ari
  535. call setline(1, ['abc'])
  536. call cursor(1, 1)
  537. call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
  538. " call assert_equal(["æèñabc"], getline(1, '$'))
  539. call assert_equal(["zyxabc"], getline(1, '$'))
  540. call assert_true(&revins)
  541. call setline(1, ['abc'])
  542. call cursor(1, 1)
  543. call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
  544. call assert_equal(["xyzabc"], getline(1, '$'))
  545. call assert_false(&revins)
  546. set noari
  547. let &encoding=_encoding
  548. bw!
  549. endfunc
  550. " needs to come first, to have the @. register empty
  551. func Test_edit_00a_CTRL_A()
  552. " Test pressing CTRL-A
  553. new
  554. call setline(1, repeat([''], 5))
  555. call cursor(1, 1)
  556. try
  557. call feedkeys("A\<NUL>", 'tnix')
  558. catch /^Vim\%((\a\+)\)\=:E29/
  559. call assert_true(1, 'E29 error caught')
  560. endtry
  561. call cursor(1, 1)
  562. call feedkeys("Afoobar \<esc>", 'tnix')
  563. call cursor(2, 1)
  564. call feedkeys("A\<c-a>more\<esc>", 'tnix')
  565. call cursor(3, 1)
  566. call feedkeys("A\<NUL>and more\<esc>", 'tnix')
  567. call assert_equal(['foobar ', 'foobar more', 'foobar morend more', '', ''], getline(1, '$'))
  568. bw!
  569. endfunc
  570. func Test_edit_CTRL_EY()
  571. " Ctrl-E/ Ctrl-Y in insert mode completion to scroll
  572. 10new
  573. call setline(1, range(1, 100))
  574. call cursor(30, 1)
  575. norm! z.
  576. call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
  577. call assert_equal(30, winsaveview()['topline'])
  578. call assert_equal([0, 30, 2, 0], getpos('.'))
  579. call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
  580. call feedkeys("A\<c-x>".repeat("\<c-y>", 10), 'tnix')
  581. call assert_equal(21, winsaveview()['topline'])
  582. call assert_equal([0, 30, 2, 0], getpos('.'))
  583. bw!
  584. endfunc
  585. func Test_edit_CTRL_G()
  586. new
  587. call setline(1, ['foobar', 'foobar', 'foobar'])
  588. call cursor(2, 4)
  589. call feedkeys("ioooooooo\<c-g>k\<c-r>.\<esc>", 'tnix')
  590. call assert_equal(['foooooooooobar', 'foooooooooobar', 'foobar'], getline(1, '$'))
  591. call assert_equal([0, 1, 11, 0], getpos('.'))
  592. call feedkeys("i\<c-g>k\<esc>", 'tnix')
  593. call assert_equal([0, 1, 10, 0], getpos('.'))
  594. call cursor(2, 4)
  595. call feedkeys("i\<c-g>jzzzz\<esc>", 'tnix')
  596. call assert_equal(['foooooooooobar', 'foooooooooobar', 'foozzzzbar'], getline(1, '$'))
  597. call assert_equal([0, 3, 7, 0], getpos('.'))
  598. call feedkeys("i\<c-g>j\<esc>", 'tnix')
  599. call assert_equal([0, 3, 6, 0], getpos('.'))
  600. call assert_nobeep("normal! i\<c-g>\<esc>")
  601. bw!
  602. endfunc
  603. func Test_edit_CTRL_I()
  604. " Tab in completion mode
  605. let path=expand("%:p:h")
  606. new
  607. call setline(1, [path. "/", ''])
  608. call feedkeys("Arunt\<c-x>\<c-f>\<tab>\<cr>\<esc>", 'tnix')
  609. call assert_match('runtest\.vim', getline(1))
  610. %d
  611. call writefile(['one', 'two', 'three'], 'Xinclude.txt', 'D')
  612. let include='#include Xinclude.txt'
  613. call setline(1, [include, ''])
  614. call cursor(2, 1)
  615. call feedkeys("A\<c-x>\<tab>\<cr>\<esc>", 'tnix')
  616. call assert_equal([include, 'one', ''], getline(1, '$'))
  617. call feedkeys("2ggC\<c-x>\<tab>\<down>\<cr>\<esc>", 'tnix')
  618. call assert_equal([include, 'two', ''], getline(1, '$'))
  619. call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<cr>\<esc>", 'tnix')
  620. call assert_equal([include, 'three', ''], getline(1, '$'))
  621. call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
  622. call assert_equal([include, '', ''], getline(1, '$'))
  623. bw!
  624. endfunc
  625. func Test_edit_CTRL_K()
  626. " Test pressing CTRL-K (basically only dictionary completion and digraphs
  627. " the rest is already covered
  628. call writefile(['A', 'AA', 'AAA', 'AAAA'], 'Xdictionary.txt', 'D')
  629. set dictionary=Xdictionary.txt
  630. new
  631. call setline(1, 'A')
  632. call cursor(1, 1)
  633. call feedkeys("A\<c-x>\<c-k>\<cr>\<esc>", 'tnix')
  634. call assert_equal(['AA', ''], getline(1, '$'))
  635. %d
  636. call setline(1, 'A')
  637. call cursor(1, 1)
  638. call feedkeys("A\<c-x>\<c-k>\<down>\<cr>\<esc>", 'tnix')
  639. call assert_equal(['AAA'], getline(1, '$'))
  640. %d
  641. call setline(1, 'A')
  642. call cursor(1, 1)
  643. call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<cr>\<esc>", 'tnix')
  644. call assert_equal(['AAAA'], getline(1, '$'))
  645. %d
  646. call setline(1, 'A')
  647. call cursor(1, 1)
  648. call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
  649. call assert_equal(['A'], getline(1, '$'))
  650. %d
  651. call setline(1, 'A')
  652. call cursor(1, 1)
  653. call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
  654. call assert_equal(['AA'], getline(1, '$'))
  655. " press an unexpected key after dictionary completion
  656. %d
  657. call setline(1, 'A')
  658. call cursor(1, 1)
  659. call feedkeys("A\<c-x>\<c-k>\<c-]>\<cr>\<esc>", 'tnix')
  660. call assert_equal(['AA', ''], getline(1, '$'))
  661. %d
  662. call setline(1, 'A')
  663. call cursor(1, 1)
  664. call feedkeys("A\<c-x>\<c-k>\<c-s>\<cr>\<esc>", 'tnix')
  665. call assert_equal(["AA\<c-s>", ''], getline(1, '$'))
  666. %d
  667. call setline(1, 'A')
  668. call cursor(1, 1)
  669. call feedkeys("A\<c-x>\<c-k>\<c-f>\<cr>\<esc>", 'tnix')
  670. call assert_equal(["AA\<c-f>", ''], getline(1, '$'))
  671. set dictionary=
  672. %d
  673. call setline(1, 'A')
  674. call cursor(1, 1)
  675. let v:testing = 1
  676. try
  677. call feedkeys("A\<c-x>\<c-k>\<esc>", 'tnix')
  678. catch
  679. " error sleeps 2 seconds, when v:testing is not set
  680. let v:testing = 0
  681. endtry
  682. if exists('*test_override')
  683. call test_override("char_avail", 1)
  684. set showcmd
  685. %d
  686. call feedkeys("A\<c-k>a:\<esc>", 'tnix')
  687. call assert_equal(['ä'], getline(1, '$'))
  688. call test_override("char_avail", 0)
  689. set noshowcmd
  690. endif
  691. bw!
  692. endfunc
  693. func Test_edit_CTRL_L()
  694. " Test Ctrl-X Ctrl-L (line completion)
  695. new
  696. set complete=.
  697. call setline(1, ['one', 'two', 'three', '', '', '', ''])
  698. call cursor(4, 1)
  699. call feedkeys("A\<c-x>\<c-l>\<esc>", 'tnix')
  700. call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
  701. call feedkeys("cct\<c-x>\<c-l>\<c-n>\<esc>", 'tnix')
  702. call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
  703. call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<esc>", 'tnix')
  704. call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
  705. call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
  706. call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
  707. call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
  708. call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
  709. call feedkeys("cct\<c-x>\<c-l>\<c-p>\<esc>", 'tnix')
  710. call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
  711. call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<esc>", 'tnix')
  712. call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
  713. call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<c-p>\<esc>", 'tnix')
  714. call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
  715. set complete=
  716. call cursor(5, 1)
  717. call feedkeys("A\<c-x>\<c-l>\<c-p>\<c-n>\<esc>", 'tnix')
  718. call assert_equal(['one', 'two', 'three', 'three', "\<c-l>\<c-p>\<c-n>", '', ''], getline(1, '$'))
  719. set complete&
  720. %d
  721. if has("conceal") && has("syntax") && !has("nvim")
  722. call setline(1, ['foo', 'bar', 'foobar'])
  723. call test_override("char_avail", 1)
  724. set conceallevel=2 concealcursor=n
  725. syn on
  726. syn match ErrorMsg "^bar"
  727. call matchadd("Conceal", 'oo', 10, -1, {'conceal': 'X'})
  728. func! DoIt()
  729. let g:change=1
  730. endfunc
  731. au! TextChangedI <buffer> :call DoIt()
  732. call cursor(2, 1)
  733. call assert_false(exists("g:change"))
  734. call feedkeys("A \<esc>", 'tnix')
  735. call assert_equal(['foo', 'bar ', 'foobar'], getline(1, '$'))
  736. call assert_equal(1, g:change)
  737. call test_override("char_avail", 0)
  738. call clearmatches()
  739. syn off
  740. au! TextChangedI
  741. delfu DoIt
  742. unlet! g:change
  743. endif
  744. bw!
  745. endfunc
  746. func Test_edit_CTRL_N()
  747. " Check keyword completion
  748. " for e in ['latin1', 'utf-8']
  749. for e in ['utf-8']
  750. exe 'set encoding=' .. e
  751. new
  752. set complete=.
  753. call setline(1, ['INFER', 'loWER', '', '', ])
  754. call cursor(3, 1)
  755. call feedkeys("Ai\<c-n>\<cr>\<esc>", "tnix")
  756. call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
  757. call assert_equal(['INFER', 'loWER', 'i', 'LO', '', ''], getline(1, '$'), e)
  758. %d
  759. call setline(1, ['INFER', 'loWER', '', '', ])
  760. call cursor(3, 1)
  761. set ignorecase infercase
  762. call feedkeys("Ii\<c-n>\<cr>\<esc>", "tnix")
  763. call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
  764. call assert_equal(['INFER', 'loWER', 'infer', 'LOWER', '', ''], getline(1, '$'), e)
  765. set noignorecase noinfercase
  766. %d
  767. call setline(1, ['one word', 'two word'])
  768. exe "normal! Goo\<C-P>\<C-X>\<C-P>"
  769. call assert_equal('one word', getline(3))
  770. %d
  771. set complete&
  772. bw!
  773. endfor
  774. endfunc
  775. func Test_edit_CTRL_O()
  776. " Check for CTRL-O in insert mode
  777. new
  778. inoreabbr <buffer> h here some more
  779. call setline(1, ['abc', 'def'])
  780. call cursor(1, 1)
  781. " Ctrl-O after an abbreviation
  782. exe "norm A h\<c-o>:set nu\<cr> text"
  783. call assert_equal(['abc here some more text', 'def'], getline(1, '$'))
  784. call assert_true(&nu)
  785. set nonu
  786. iunabbr <buffer> h
  787. " Ctrl-O at end of line with 've'=onemore
  788. call cursor(1, 1)
  789. call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
  790. call assert_equal([0, 1, 23, 0], g:a)
  791. call cursor(1, 1)
  792. set ve=onemore
  793. call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
  794. call assert_equal([0, 1, 24, 0], g:a)
  795. set ve=
  796. unlet! g:a
  797. bw!
  798. endfunc
  799. func Test_edit_CTRL_R()
  800. " Insert Register
  801. new
  802. " call test_override("ALL", 1)
  803. set showcmd
  804. call feedkeys("AFOOBAR eins zwei\<esc>", 'tnix')
  805. call feedkeys("O\<c-r>.", 'tnix')
  806. call feedkeys("O\<c-r>=10*500\<cr>\<esc>", 'tnix')
  807. call feedkeys("O\<c-r>=getreg('=', 1)\<cr>\<esc>", 'tnix')
  808. call assert_equal(["getreg('=', 1)", '5000', "FOOBAR eins zwei", "FOOBAR eins zwei"], getline(1, '$'))
  809. " call test_override("ALL", 0)
  810. set noshowcmd
  811. bw!
  812. endfunc
  813. func Test_edit_CTRL_S()
  814. " Test pressing CTRL-S (basically only spellfile completion)
  815. " the rest is already covered
  816. new
  817. if !has("spell")
  818. call setline(1, 'vim')
  819. call feedkeys("A\<c-x>ss\<cr>\<esc>", 'tnix')
  820. call assert_equal(['vims', ''], getline(1, '$'))
  821. bw!
  822. return
  823. endif
  824. call setline(1, 'vim')
  825. " spell option not yet set
  826. try
  827. call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
  828. catch /^Vim\%((\a\+)\)\=:E756/
  829. call assert_true(1, 'error caught')
  830. endtry
  831. call assert_equal(['vim', ''], getline(1, '$'))
  832. %d
  833. setl spell spelllang=en
  834. call setline(1, 'vim')
  835. call cursor(1, 1)
  836. call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
  837. call assert_equal(['Vim', ''], getline(1, '$'))
  838. %d
  839. call setline(1, 'vim')
  840. call cursor(1, 1)
  841. call feedkeys("A\<c-x>\<c-s>\<down>\<cr>\<esc>", 'tnix')
  842. call assert_equal(['Aim'], getline(1, '$'))
  843. %d
  844. call setline(1, 'vim')
  845. call cursor(1, 1)
  846. call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
  847. call assert_equal(['vim', ''], getline(1, '$'))
  848. %d
  849. " empty buffer
  850. call cursor(1, 1)
  851. call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
  852. call assert_equal(['', ''], getline(1, '$'))
  853. setl nospell
  854. bw!
  855. endfunc
  856. func Test_edit_CTRL_T()
  857. " Check for CTRL-T and CTRL-X CTRL-T in insert mode
  858. " 1) increase indent
  859. new
  860. call setline(1, "abc")
  861. call cursor(1, 1)
  862. call feedkeys("A\<c-t>xyz", 'tnix')
  863. call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
  864. " 2) also when paste option is set
  865. set paste
  866. call setline(1, "abc")
  867. call cursor(1, 1)
  868. call feedkeys("A\<c-t>xyz", 'tnix')
  869. call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
  870. set nopaste
  871. " CTRL-X CTRL-T (thesaurus complete)
  872. call writefile(['angry furious mad enraged'], 'Xthesaurus', 'D')
  873. set thesaurus=Xthesaurus
  874. call setline(1, 'mad')
  875. call cursor(1, 1)
  876. call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
  877. call assert_equal(['mad', ''], getline(1, '$'))
  878. %d
  879. call setline(1, 'mad')
  880. call cursor(1, 1)
  881. call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
  882. call assert_equal(['angry', ''], getline(1, '$'))
  883. %d
  884. call setline(1, 'mad')
  885. call cursor(1, 1)
  886. call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
  887. call assert_equal(['furious', ''], getline(1, '$'))
  888. %d
  889. call setline(1, 'mad')
  890. call cursor(1, 1)
  891. call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
  892. call assert_equal(['enraged', ''], getline(1, '$'))
  893. %d
  894. call setline(1, 'mad')
  895. call cursor(1, 1)
  896. call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
  897. call assert_equal(['mad', ''], getline(1, '$'))
  898. %d
  899. call setline(1, 'mad')
  900. call cursor(1, 1)
  901. call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
  902. call assert_equal(['mad', ''], getline(1, '$'))
  903. " Using <c-p> <c-n> when 'complete' is empty
  904. set complete=
  905. %d
  906. call setline(1, 'mad')
  907. call cursor(1, 1)
  908. call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
  909. call assert_equal(['angry', ''], getline(1, '$'))
  910. %d
  911. call setline(1, 'mad')
  912. call cursor(1, 1)
  913. call feedkeys("A\<c-x>\<c-t>\<c-p>\<cr>\<esc>", 'tnix')
  914. call assert_equal(['mad', ''], getline(1, '$'))
  915. set complete&
  916. set thesaurus=
  917. %d
  918. call setline(1, 'mad')
  919. call cursor(1, 1)
  920. let v:testing = 1
  921. try
  922. call feedkeys("A\<c-x>\<c-t>\<esc>", 'tnix')
  923. catch
  924. " error sleeps 2 seconds, when v:testing is not set
  925. let v:testing = 0
  926. endtry
  927. call assert_equal(['mad'], getline(1, '$'))
  928. bw!
  929. endfunc
  930. " Test thesaurus completion with different encodings
  931. func Test_thesaurus_complete_with_encoding()
  932. call writefile(['angry furious mad enraged'], 'Xthesaurus', 'D')
  933. set thesaurus=Xthesaurus
  934. " for e in ['latin1', 'utf-8']
  935. for e in ['utf-8']
  936. exe 'set encoding=' .. e
  937. new
  938. call setline(1, 'mad')
  939. call cursor(1, 1)
  940. call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
  941. call assert_equal(['mad', ''], getline(1, '$'))
  942. bw!
  943. endfor
  944. set thesaurus=
  945. endfunc
  946. " Test 'thesaurusfunc'
  947. func MyThesaurus(findstart, base)
  948. let mythesaurus = [
  949. \ #{word: "happy",
  950. \ synonyms: "cheerful,blissful,flying high,looking good,peppy"},
  951. \ #{word: "kind",
  952. \ synonyms: "amiable,bleeding-heart,heart in right place"}]
  953. if a:findstart
  954. " locate the start of the word
  955. let line = getline('.')
  956. let start = col('.') - 1
  957. while start > 0 && line[start - 1] =~ '\a'
  958. let start -= 1
  959. endwhile
  960. return start
  961. else
  962. " find strings matching with "a:base"
  963. let res = []
  964. for w in mythesaurus
  965. if w.word =~ '^' . a:base
  966. call add(res, w.word)
  967. call extend(res, split(w.synonyms, ","))
  968. endif
  969. endfor
  970. return res
  971. endif
  972. endfunc
  973. func Test_thesaurus_func()
  974. new
  975. set thesaurus=notused
  976. set thesaurusfunc=NotUsed
  977. setlocal thesaurusfunc=MyThesaurus
  978. call setline(1, "an ki")
  979. call cursor(1, 1)
  980. call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
  981. call assert_equal(['an amiable', ''], getline(1, '$'))
  982. setlocal thesaurusfunc=NonExistingFunc
  983. call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
  984. setlocal thesaurusfunc=
  985. set thesaurusfunc=NonExistingFunc
  986. call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
  987. %bw!
  988. set thesaurusfunc=
  989. set thesaurus=
  990. endfunc
  991. func Test_edit_CTRL_U()
  992. " Test 'completefunc'
  993. new
  994. " -1, -2 and -3 are special return values
  995. let g:special=0
  996. fun! CompleteMonths(findstart, base)
  997. if a:findstart
  998. " locate the start of the word
  999. return g:special
  1000. else
  1001. " find months matching with "a:base"
  1002. let res = []
  1003. for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
  1004. if m =~ '^\c'.a:base
  1005. call add(res, {'word': m, 'abbr': m.' Month', 'icase': 0})
  1006. endif
  1007. endfor
  1008. return {'words': res, 'refresh': 'always'}
  1009. endif
  1010. endfun
  1011. set completefunc=CompleteMonths
  1012. call setline(1, ['', ''])
  1013. call cursor(1, 1)
  1014. call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
  1015. call assert_equal(['X', '', ''], getline(1, '$'))
  1016. %d
  1017. let g:special=-1
  1018. call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
  1019. call assert_equal(['XJan', ''], getline(1, '$'))
  1020. %d
  1021. let g:special=-2
  1022. call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
  1023. call assert_equal(['X', ''], getline(1, '$'))
  1024. %d
  1025. let g:special=-3
  1026. call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
  1027. call assert_equal(['X', ''], getline(1, '$'))
  1028. %d
  1029. let g:special=0
  1030. call feedkeys("AM\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
  1031. call assert_equal(['Mar', ''], getline(1, '$'))
  1032. %d
  1033. call feedkeys("AM\<c-x>\<c-u>\<c-n>\<cr>\<esc>", 'tnix')
  1034. call assert_equal(['May', ''], getline(1, '$'))
  1035. %d
  1036. call feedkeys("AM\<c-x>\<c-u>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
  1037. call assert_equal(['M', ''], getline(1, '$'))
  1038. delfu CompleteMonths
  1039. %d
  1040. try
  1041. call feedkeys("A\<c-x>\<c-u>", 'tnix')
  1042. call assert_fails(1, 'unknown completion function')
  1043. catch /^Vim\%((\a\+)\)\=:E117/
  1044. call assert_true(1, 'E117 error caught')
  1045. endtry
  1046. set completefunc=
  1047. bw!
  1048. endfunc
  1049. func Test_edit_completefunc_delete()
  1050. func CompleteFunc(findstart, base)
  1051. if a:findstart == 1
  1052. return col('.') - 1
  1053. endif
  1054. normal dd
  1055. return ['a', 'b']
  1056. endfunc
  1057. new
  1058. set completefunc=CompleteFunc
  1059. call setline(1, ['', 'abcd', ''])
  1060. 2d
  1061. call assert_fails("normal 2G$a\<C-X>\<C-U>", 'E565:')
  1062. bwipe!
  1063. endfunc
  1064. func Test_edit_CTRL_Z()
  1065. " Ctrl-Z when insertmode is not set inserts it literally
  1066. new
  1067. call setline(1, 'abc')
  1068. call feedkeys("A\<c-z>\<esc>", 'tnix')
  1069. call assert_equal(["abc\<c-z>"], getline(1,'$'))
  1070. bw!
  1071. " TODO: How to Test Ctrl-Z in insert mode, e.g. suspend?
  1072. endfunc
  1073. func Test_edit_DROP()
  1074. if !has("dnd")
  1075. return
  1076. endif
  1077. new
  1078. call setline(1, ['abc def ghi'])
  1079. call cursor(1, 1)
  1080. try
  1081. call feedkeys("i\<Drop>\<Esc>", 'tnix')
  1082. call assert_fails(1, 'Invalid register name')
  1083. catch /^Vim\%((\a\+)\)\=:E353/
  1084. call assert_true(1, 'error caught')
  1085. endtry
  1086. bw!
  1087. endfunc
  1088. func Test_edit_CTRL_V()
  1089. new
  1090. call setline(1, ['abc'])
  1091. call cursor(2, 1)
  1092. " force some redraws
  1093. set showmode showcmd
  1094. " call test_override('char_avail', 1)
  1095. call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
  1096. call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
  1097. if has("rightleft") && exists("+rl")
  1098. set rl
  1099. call setline(1, ['abc'])
  1100. call cursor(2, 1)
  1101. call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
  1102. call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
  1103. set norl
  1104. endif
  1105. set noshowmode showcmd
  1106. " call test_override('char_avail', 0)
  1107. " No modifiers should be applied to the char typed using i_CTRL-V_digit.
  1108. call feedkeys(":append\<CR>\<C-V>76c\<C-V>76\<C-F2>\<C-V>u3c0j\<C-V>u3c0\<M-F3>\<CR>.\<CR>", 'tnix')
  1109. call assert_equal('LcL<C-F2>πjπ<M-F3>', getline(2))
  1110. if has('osx')
  1111. " A char with a modifier should not be a valid char for i_CTRL-V_digit.
  1112. call feedkeys("o\<C-V>\<D-j>\<C-V>\<D-1>\<C-V>\<D-o>\<C-V>\<D-x>\<C-V>\<D-u>", 'tnix')
  1113. call assert_equal('<D-j><D-1><D-o><D-x><D-u>', getline(3))
  1114. endif
  1115. bw!
  1116. endfunc
  1117. func Test_edit_F1()
  1118. CheckFeature quickfix
  1119. " Pressing <f1>
  1120. new
  1121. " call feedkeys(":set im\<cr>\<f1>\<c-l>", 'tnix')
  1122. call feedkeys("i\<f1>\<esc>", 'tnix')
  1123. set noinsertmode
  1124. call assert_equal('help', &buftype)
  1125. bw
  1126. bw
  1127. endfunc
  1128. func Test_edit_F21()
  1129. " Pressing <f21>
  1130. " sends a netbeans command
  1131. if has("netbeans_intg")
  1132. new
  1133. " I have no idea what this is supposed to do :)
  1134. call feedkeys("A\<F21>\<F1>\<esc>", 'tnix')
  1135. bw
  1136. endif
  1137. endfunc
  1138. func Test_edit_HOME_END()
  1139. " Test Home/End Keys
  1140. new
  1141. set foldopen+=hor
  1142. call setline(1, ['abc', 'def'])
  1143. call cursor(1, 1)
  1144. call feedkeys("AX\<Home>Y\<esc>", 'tnix')
  1145. call cursor(2, 1)
  1146. call feedkeys("iZ\<End>Y\<esc>", 'tnix')
  1147. call assert_equal(['YabcX', 'ZdefY'], getline(1, '$'))
  1148. set foldopen-=hor
  1149. bw!
  1150. endfunc
  1151. func Test_edit_INS()
  1152. " Test for Pressing <Insert>
  1153. new
  1154. call setline(1, ['abc', 'def'])
  1155. call cursor(1, 1)
  1156. call feedkeys("i\<Insert>ZYX>", 'tnix')
  1157. call assert_equal(['ZYX>', 'def'], getline(1, '$'))
  1158. call setline(1, ['abc', 'def'])
  1159. call cursor(1, 1)
  1160. call feedkeys("i\<Insert>Z\<Insert>YX>", 'tnix')
  1161. call assert_equal(['ZYX>bc', 'def'], getline(1, '$'))
  1162. bw!
  1163. endfunc
  1164. func Test_edit_LEFT_RIGHT()
  1165. " Left, Shift-Left, Right, Shift-Right
  1166. new
  1167. call setline(1, ['abc def ghi', 'ABC DEF GHI', 'ZZZ YYY XXX'])
  1168. let _ww=&ww
  1169. set ww=
  1170. call cursor(2, 1)
  1171. call feedkeys("i\<left>\<esc>", 'tnix')
  1172. call assert_equal([0, 2, 1, 0], getpos('.'))
  1173. " Is this a bug, <s-left> does not respect whichwrap option
  1174. call feedkeys("i\<s-left>\<esc>", 'tnix')
  1175. call assert_equal([0, 1, 8, 0], getpos('.'))
  1176. call feedkeys("i". repeat("\<s-left>", 3). "\<esc>", 'tnix')
  1177. call assert_equal([0, 1, 1, 0], getpos('.'))
  1178. call feedkeys("i\<right>\<esc>", 'tnix')
  1179. call assert_equal([0, 1, 1, 0], getpos('.'))
  1180. call feedkeys("i\<right>\<right>\<esc>", 'tnix')
  1181. call assert_equal([0, 1, 2, 0], getpos('.'))
  1182. call feedkeys("A\<right>\<esc>", 'tnix')
  1183. call assert_equal([0, 1, 11, 0], getpos('.'))
  1184. call feedkeys("A\<s-right>\<esc>", 'tnix')
  1185. call assert_equal([0, 2, 1, 0], getpos('.'))
  1186. call feedkeys("i\<s-right>\<esc>", 'tnix')
  1187. call assert_equal([0, 2, 4, 0], getpos('.'))
  1188. call cursor(3, 11)
  1189. call feedkeys("A\<right>\<esc>", 'tnix')
  1190. call feedkeys("A\<s-right>\<esc>", 'tnix')
  1191. call assert_equal([0, 3, 11, 0], getpos('.'))
  1192. call cursor(2, 11)
  1193. " <S-Right> does not respect 'whichwrap' option
  1194. call feedkeys("A\<s-right>\<esc>", 'tnix')
  1195. call assert_equal([0, 3, 1, 0], getpos('.'))
  1196. " Check motion when 'whichwrap' contains cursor keys for insert mode
  1197. set ww+=[,]
  1198. call cursor(2, 1)
  1199. call feedkeys("i\<left>\<esc>", 'tnix')
  1200. call assert_equal([0, 1, 11, 0], getpos('.'))
  1201. call cursor(2, 11)
  1202. call feedkeys("A\<right>\<esc>", 'tnix')
  1203. call assert_equal([0, 3, 1, 0], getpos('.'))
  1204. call cursor(2, 11)
  1205. call feedkeys("A\<s-right>\<esc>", 'tnix')
  1206. call assert_equal([0, 3, 1, 0], getpos('.'))
  1207. let &ww = _ww
  1208. bw!
  1209. endfunc
  1210. func Test_edit_MOUSE()
  1211. " This is a simple test, since we're not really using the mouse here
  1212. CheckFeature mouse
  1213. 10new
  1214. call setline(1, range(1, 100))
  1215. call cursor(1, 1)
  1216. call assert_equal(1, line('w0'))
  1217. call assert_equal(10, line('w$'))
  1218. set mouse=a
  1219. " One scroll event moves three lines.
  1220. call feedkeys("A\<ScrollWheelDown>\<esc>", 'tnix')
  1221. call assert_equal(4, line('w0'))
  1222. call assert_equal(13, line('w$'))
  1223. " This should move by one page down.
  1224. call feedkeys("A\<S-ScrollWheelDown>\<esc>", 'tnix')
  1225. call assert_equal(14, line('w0'))
  1226. set nostartofline
  1227. " Another page down.
  1228. call feedkeys("A\<C-ScrollWheelDown>\<esc>", 'tnix')
  1229. call assert_equal(24, line('w0'))
  1230. call assert_equal([0, 24, 2, 0], getpos('.'))
  1231. call Ntest_setmouse(4, 3)
  1232. call feedkeys("A\<LeftMouse>\<esc>", 'tnix')
  1233. call assert_equal([0, 27, 2, 0], getpos('.'))
  1234. set mousemodel=extend
  1235. call Ntest_setmouse(5, 3)
  1236. call feedkeys("A\<RightMouse>\<esc>\<esc>", 'tnix')
  1237. call assert_equal([0, 28, 2, 0], getpos('.'))
  1238. set mousemodel&
  1239. call cursor(1, 100)
  1240. norm! zt
  1241. " this should move by a screen up, but when the test
  1242. " is run, it moves up to the top of the buffer...
  1243. call feedkeys("A\<ScrollWheelUp>\<esc>", 'tnix')
  1244. call assert_equal([0, 1, 1, 0], getpos('.'))
  1245. call cursor(1, 30)
  1246. norm! zt
  1247. call feedkeys("A\<S-ScrollWheelUp>\<esc>", 'tnix')
  1248. call assert_equal([0, 1, 1, 0], getpos('.'))
  1249. call cursor(1, 30)
  1250. norm! zt
  1251. call feedkeys("A\<C-ScrollWheelUp>\<esc>", 'tnix')
  1252. call assert_equal([0, 1, 1, 0], getpos('.'))
  1253. %d
  1254. call setline(1, repeat(["12345678901234567890"], 100))
  1255. call cursor(2, 1)
  1256. call feedkeys("A\<ScrollWheelRight>\<esc>", 'tnix')
  1257. call assert_equal([0, 2, 20, 0], getpos('.'))
  1258. call feedkeys("A\<ScrollWheelLeft>\<esc>", 'tnix')
  1259. call assert_equal([0, 2, 20, 0], getpos('.'))
  1260. call feedkeys("A\<S-ScrollWheelRight>\<esc>", 'tnix')
  1261. call assert_equal([0, 2, 20, 0], getpos('.'))
  1262. call feedkeys("A\<S-ScrollWheelLeft>\<esc>", 'tnix')
  1263. call assert_equal([0, 2, 20, 0], getpos('.'))
  1264. call feedkeys("A\<C-ScrollWheelRight>\<esc>", 'tnix')
  1265. call assert_equal([0, 2, 20, 0], getpos('.'))
  1266. call feedkeys("A\<C-ScrollWheelLeft>\<esc>", 'tnix')
  1267. call assert_equal([0, 2, 20, 0], getpos('.'))
  1268. set mouse& startofline
  1269. bw!
  1270. endfunc
  1271. func Test_edit_PAGEUP_PAGEDOWN()
  1272. 10new
  1273. call setline(1, repeat(['abc def ghi'], 30))
  1274. call cursor(1, 1)
  1275. call feedkeys("i\<PageDown>\<esc>", 'tnix')
  1276. call assert_equal([0, 9, 1, 0], getpos('.'))
  1277. call feedkeys("i\<PageDown>\<esc>", 'tnix')
  1278. call assert_equal([0, 17, 1, 0], getpos('.'))
  1279. call feedkeys("i\<PageDown>\<esc>", 'tnix')
  1280. call assert_equal([0, 25, 1, 0], getpos('.'))
  1281. call feedkeys("i\<PageDown>\<esc>", 'tnix')
  1282. call assert_equal([0, 30, 1, 0], getpos('.'))
  1283. call feedkeys("i\<PageDown>\<esc>", 'tnix')
  1284. call assert_equal([0, 30, 1, 0], getpos('.'))
  1285. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1286. call assert_equal([0, 29, 1, 0], getpos('.'))
  1287. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1288. call assert_equal([0, 21, 1, 0], getpos('.'))
  1289. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1290. call assert_equal([0, 13, 1, 0], getpos('.'))
  1291. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1292. call assert_equal([0, 10, 1, 0], getpos('.'))
  1293. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1294. call assert_equal([0, 10, 11, 0], getpos('.'))
  1295. " <S-Up> is the same as <PageUp>
  1296. " <S-Down> is the same as <PageDown>
  1297. call cursor(1, 1)
  1298. call feedkeys("i\<S-Down>\<esc>", 'tnix')
  1299. call assert_equal([0, 9, 1, 0], getpos('.'))
  1300. call feedkeys("i\<S-Down>\<esc>", 'tnix')
  1301. call assert_equal([0, 17, 1, 0], getpos('.'))
  1302. call feedkeys("i\<S-Down>\<esc>", 'tnix')
  1303. call assert_equal([0, 25, 1, 0], getpos('.'))
  1304. call feedkeys("i\<S-Down>\<esc>", 'tnix')
  1305. call assert_equal([0, 30, 1, 0], getpos('.'))
  1306. call feedkeys("i\<S-Down>\<esc>", 'tnix')
  1307. call assert_equal([0, 30, 1, 0], getpos('.'))
  1308. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1309. call assert_equal([0, 29, 1, 0], getpos('.'))
  1310. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1311. call assert_equal([0, 21, 1, 0], getpos('.'))
  1312. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1313. call assert_equal([0, 13, 1, 0], getpos('.'))
  1314. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1315. call assert_equal([0, 10, 1, 0], getpos('.'))
  1316. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1317. call assert_equal([0, 10, 11, 0], getpos('.'))
  1318. set nostartofline
  1319. call cursor(30, 11)
  1320. norm! zt
  1321. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1322. call assert_equal([0, 29, 11, 0], getpos('.'))
  1323. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1324. call assert_equal([0, 21, 11, 0], getpos('.'))
  1325. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1326. call assert_equal([0, 13, 11, 0], getpos('.'))
  1327. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1328. call assert_equal([0, 10, 11, 0], getpos('.'))
  1329. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1330. call assert_equal([0, 10, 11, 0], getpos('.'))
  1331. call cursor(1, 1)
  1332. call feedkeys("A\<PageDown>\<esc>", 'tnix')
  1333. call assert_equal([0, 9, 11, 0], getpos('.'))
  1334. call feedkeys("A\<PageDown>\<esc>", 'tnix')
  1335. call assert_equal([0, 17, 11, 0], getpos('.'))
  1336. call feedkeys("A\<PageDown>\<esc>", 'tnix')
  1337. call assert_equal([0, 25, 11, 0], getpos('.'))
  1338. call feedkeys("A\<PageDown>\<esc>", 'tnix')
  1339. call assert_equal([0, 30, 11, 0], getpos('.'))
  1340. call feedkeys("A\<PageDown>\<esc>", 'tnix')
  1341. call assert_equal([0, 30, 11, 0], getpos('.'))
  1342. " <S-Up> is the same as <PageUp>
  1343. " <S-Down> is the same as <PageDown>
  1344. call cursor(30, 11)
  1345. norm! zt
  1346. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1347. call assert_equal([0, 29, 11, 0], getpos('.'))
  1348. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1349. call assert_equal([0, 21, 11, 0], getpos('.'))
  1350. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1351. call assert_equal([0, 13, 11, 0], getpos('.'))
  1352. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1353. call assert_equal([0, 10, 11, 0], getpos('.'))
  1354. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1355. call assert_equal([0, 10, 11, 0], getpos('.'))
  1356. call cursor(1, 1)
  1357. call feedkeys("A\<S-Down>\<esc>", 'tnix')
  1358. call assert_equal([0, 9, 11, 0], getpos('.'))
  1359. call feedkeys("A\<S-Down>\<esc>", 'tnix')
  1360. call assert_equal([0, 17, 11, 0], getpos('.'))
  1361. call feedkeys("A\<S-Down>\<esc>", 'tnix')
  1362. call assert_equal([0, 25, 11, 0], getpos('.'))
  1363. call feedkeys("A\<S-Down>\<esc>", 'tnix')
  1364. call assert_equal([0, 30, 11, 0], getpos('.'))
  1365. call feedkeys("A\<S-Down>\<esc>", 'tnix')
  1366. call assert_equal([0, 30, 11, 0], getpos('.'))
  1367. bw!
  1368. endfunc
  1369. func Test_edit_forbidden()
  1370. new
  1371. " 1) edit in the sandbox is not allowed
  1372. call setline(1, 'a')
  1373. com! Sandbox :sandbox call feedkeys("i\<del>\<esc>", 'tnix')
  1374. call assert_fails(':Sandbox', 'E48:')
  1375. com! Sandbox :sandbox exe "norm! i\<del>"
  1376. call assert_fails(':Sandbox', 'E48:')
  1377. delcom Sandbox
  1378. call assert_equal(['a'], getline(1,'$'))
  1379. " 2) edit with textlock set
  1380. fu! DoIt()
  1381. call feedkeys("i\<del>\<esc>", 'tnix')
  1382. endfu
  1383. au InsertCharPre <buffer> :call DoIt()
  1384. try
  1385. call feedkeys("ix\<esc>", 'tnix')
  1386. call assert_fails(1, 'textlock')
  1387. catch /^Vim\%((\a\+)\)\=:E565/ " catch E565: not allowed here
  1388. endtry
  1389. " TODO: Might be a bug: should x really be inserted here
  1390. call assert_equal(['xa'], getline(1, '$'))
  1391. delfu DoIt
  1392. try
  1393. call feedkeys("ix\<esc>", 'tnix')
  1394. call assert_fails(1, 'unknown function')
  1395. catch /^Vim\%((\a\+)\)\=:E117/ " catch E117: unknown function
  1396. endtry
  1397. au! InsertCharPre
  1398. " 3) edit when completion is shown
  1399. fun! Complete(findstart, base)
  1400. if a:findstart
  1401. return col('.')
  1402. else
  1403. call feedkeys("i\<del>\<esc>", 'tnix')
  1404. return []
  1405. endif
  1406. endfun
  1407. set completefunc=Complete
  1408. try
  1409. call feedkeys("i\<c-x>\<c-u>\<esc>", 'tnix')
  1410. call assert_fails(1, 'change in complete function')
  1411. catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
  1412. endtry
  1413. delfu Complete
  1414. set completefunc=
  1415. if has("rightleft") && exists("+fkmap")
  1416. " 4) 'R' when 'fkmap' and 'revins' is set.
  1417. set revins fkmap
  1418. try
  1419. normal Ri
  1420. call assert_fails(1, "R with 'fkmap' and 'ri' set")
  1421. catch
  1422. finally
  1423. set norevins nofkmap
  1424. endtry
  1425. endif
  1426. bw!
  1427. endfunc
  1428. func Test_edit_rightleft()
  1429. " Cursor in rightleft mode moves differently
  1430. CheckFeature rightleft
  1431. call NewWindow(10, 20)
  1432. call setline(1, ['abc', 'def', 'ghi'])
  1433. call cursor(1, 2)
  1434. set rightleft
  1435. " Screen looks as expected
  1436. let lines = ScreenLines([1, 4], winwidth(0))
  1437. let expect = [
  1438. \" cba",
  1439. \" fed",
  1440. \" ihg",
  1441. \" ~"]
  1442. call assert_equal(join(expect, "\n"), join(lines, "\n"))
  1443. " 2) right moves to the left
  1444. call feedkeys("i\<right>\<esc>x", 'txin')
  1445. call assert_equal(['bc', 'def', 'ghi'], getline(1,'$'))
  1446. call cursor(1, 2)
  1447. call feedkeys("i\<s-right>\<esc>", 'txin')
  1448. call cursor(1, 2)
  1449. call feedkeys("i\<c-right>\<esc>", 'txin')
  1450. " Screen looks as expected
  1451. let lines = ScreenLines([1, 4], winwidth(0))
  1452. let expect = [
  1453. \" cb",
  1454. \" fed",
  1455. \" ihg",
  1456. \" ~"]
  1457. call assert_equal(join(expect, "\n"), join(lines, "\n"))
  1458. " 2) left moves to the right
  1459. call setline(1, ['abc', 'def', 'ghi'])
  1460. call cursor(1, 2)
  1461. call feedkeys("i\<left>\<esc>x", 'txin')
  1462. call assert_equal(['ac', 'def', 'ghi'], getline(1,'$'))
  1463. call cursor(1, 2)
  1464. call feedkeys("i\<s-left>\<esc>", 'txin')
  1465. call cursor(1, 2)
  1466. call feedkeys("i\<c-left>\<esc>", 'txin')
  1467. " Screen looks as expected
  1468. let lines = ScreenLines([1, 4], winwidth(0))
  1469. let expect = [
  1470. \" ca",
  1471. \" fed",
  1472. \" ihg",
  1473. \" ~"]
  1474. call assert_equal(join(expect, "\n"), join(lines, "\n"))
  1475. %d _
  1476. " call test_override('redraw_flag', 1)
  1477. " call test_override('char_avail', 1)
  1478. call feedkeys("a\<C-V>x41", "xt")
  1479. redraw!
  1480. call assert_equal(repeat(' ', 19) .. 'A', Screenline(1))
  1481. " call test_override('ALL', 0)
  1482. set norightleft
  1483. bw!
  1484. endfunc
  1485. func Test_edit_complete_very_long_name()
  1486. " Long directory names only work on Unix.
  1487. CheckUnix
  1488. let dirname = getcwd() . "/Xlongdir"
  1489. let longdirname = dirname . repeat('/' . repeat('d', 255), 4)
  1490. try
  1491. call mkdir(longdirname, 'pR')
  1492. catch /E739:/
  1493. " Long directory name probably not supported.
  1494. call delete(dirname, 'rf')
  1495. return
  1496. endtry
  1497. " Try to get the Vim window position before setting 'columns', so that we can
  1498. " move the window back to where it was.
  1499. let winposx = getwinposx()
  1500. let winposy = getwinposy()
  1501. if winposx >= 0 && winposy >= 0 && !has('gui_running')
  1502. " We did get the window position, but xterm may report the wrong numbers.
  1503. " Move the window to the reported position and compute any offset.
  1504. exe 'winpos ' . winposx . ' ' . winposy
  1505. sleep 100m
  1506. let x = getwinposx()
  1507. if x >= 0
  1508. let winposx += winposx - x
  1509. endif
  1510. let y = getwinposy()
  1511. if y >= 0
  1512. let winposy += winposy - y
  1513. endif
  1514. endif
  1515. let save_columns = &columns
  1516. " Need at least about 1100 columns to reproduce the problem.
  1517. set columns=2000
  1518. set noswapfile
  1519. let longfilename = longdirname . '/' . repeat('a', 255)
  1520. call writefile(['Totum', 'Table'], longfilename)
  1521. new
  1522. exe "next Xnofile " . longfilename
  1523. exe "normal iT\<C-N>"
  1524. bwipe!
  1525. exe 'bwipe! ' . longfilename
  1526. let &columns = save_columns
  1527. if winposx >= 0 && winposy >= 0
  1528. exe 'winpos ' . winposx . ' ' . winposy
  1529. endif
  1530. set swapfile&
  1531. endfunc
  1532. func Test_edit_backtick()
  1533. next a\`b c
  1534. call assert_equal('a`b', expand('%'))
  1535. next
  1536. call assert_equal('c', expand('%'))
  1537. call assert_equal('a\`b c', expand('##'))
  1538. endfunc
  1539. func Test_edit_quit()
  1540. edit foo.txt
  1541. split
  1542. new
  1543. call setline(1, 'hello')
  1544. 3wincmd w
  1545. redraw!
  1546. call assert_fails('1q', 'E37:')
  1547. bwipe! foo.txt
  1548. only
  1549. endfunc
  1550. func Test_edit_alt()
  1551. " Keeping the cursor line didn't happen when the first line has indent.
  1552. new
  1553. call setline(1, [' one', 'two', 'three'])
  1554. w XAltFile
  1555. $
  1556. call assert_equal(3, line('.'))
  1557. e Xother
  1558. e #
  1559. call assert_equal(3, line('.'))
  1560. bwipe XAltFile
  1561. call delete('XAltFile')
  1562. endfunc
  1563. func Test_edit_InsertLeave()
  1564. new
  1565. au InsertLeavePre * let g:did_au_pre = 1
  1566. au InsertLeave * let g:did_au = 1
  1567. let g:did_au_pre = 0
  1568. let g:did_au = 0
  1569. call feedkeys("afoo\<Esc>", 'tx')
  1570. call assert_equal(1, g:did_au_pre)
  1571. call assert_equal(1, g:did_au)
  1572. call assert_equal('foo', getline(1))
  1573. let g:did_au_pre = 0
  1574. let g:did_au = 0
  1575. call feedkeys("Sbar\<C-C>", 'tx')
  1576. call assert_equal(1, g:did_au_pre)
  1577. call assert_equal(0, g:did_au)
  1578. call assert_equal('bar', getline(1))
  1579. inoremap x xx<Esc>
  1580. let g:did_au_pre = 0
  1581. let g:did_au = 0
  1582. call feedkeys("Saax", 'tx')
  1583. call assert_equal(1, g:did_au_pre)
  1584. call assert_equal(1, g:did_au)
  1585. call assert_equal('aaxx', getline(1))
  1586. inoremap x xx<C-C>
  1587. let g:did_au_pre = 0
  1588. let g:did_au = 0
  1589. call feedkeys("Sbbx", 'tx')
  1590. call assert_equal(1, g:did_au_pre)
  1591. call assert_equal(0, g:did_au)
  1592. call assert_equal('bbxx', getline(1))
  1593. bwipe!
  1594. au! InsertLeave InsertLeavePre
  1595. iunmap x
  1596. endfunc
  1597. func Test_edit_InsertLeave_undo()
  1598. new XtestUndo
  1599. set undofile
  1600. au InsertLeave * wall
  1601. exe "normal ofoo\<Esc>"
  1602. call assert_equal(2, line('$'))
  1603. normal u
  1604. call assert_equal(1, line('$'))
  1605. bwipe!
  1606. au! InsertLeave
  1607. call delete('XtestUndo')
  1608. call delete(undofile('XtestUndo'))
  1609. set undofile&
  1610. endfunc
  1611. " Test for inserting characters using CTRL-V followed by a number.
  1612. func Test_edit_special_chars()
  1613. new
  1614. let t = "o\<C-V>65\<C-V>x42\<C-V>o103 \<C-V>33a\<C-V>xfg\<C-V>o78\<Esc>"
  1615. exe "normal " . t
  1616. call assert_equal("ABC !a\<C-O>g\<C-G>8", getline(2))
  1617. close!
  1618. endfunc
  1619. func Test_edit_startinsert()
  1620. new
  1621. set backspace+=start
  1622. call setline(1, 'foobar')
  1623. call feedkeys("A\<C-U>\<Esc>", 'xt')
  1624. call assert_equal('', getline(1))
  1625. call setline(1, 'foobar')
  1626. call feedkeys(":startinsert!\<CR>\<C-U>\<Esc>", 'xt')
  1627. call assert_equal('', getline(1))
  1628. set backspace&
  1629. bwipe!
  1630. endfunc
  1631. " Test for :startreplace and :startgreplace
  1632. func Test_edit_startreplace()
  1633. new
  1634. call setline(1, 'abc')
  1635. call feedkeys("l:startreplace\<CR>xyz\e", 'xt')
  1636. call assert_equal('axyz', getline(1))
  1637. call feedkeys("0:startreplace!\<CR>abc\e", 'xt')
  1638. call assert_equal('axyzabc', getline(1))
  1639. call setline(1, "a\tb")
  1640. call feedkeys("0l:startgreplace\<CR>xyz\e", 'xt')
  1641. call assert_equal("axyz\tb", getline(1))
  1642. call feedkeys("0i\<C-R>=execute('startreplace')\<CR>12\e", 'xt')
  1643. call assert_equal("12axyz\tb", getline(1))
  1644. close!
  1645. endfunc
  1646. func Test_edit_noesckeys()
  1647. CheckNotGui
  1648. new
  1649. " <Left> moves cursor when 'esckeys' is set
  1650. exe "set t_kl=\<Esc>OD"
  1651. " set esckeys
  1652. call feedkeys("axyz\<Esc>ODX", "xt")
  1653. " call assert_equal("xyXz", getline(1))
  1654. " <Left> exits Insert mode when 'esckeys' is off
  1655. " set noesckeys
  1656. call setline(1, '')
  1657. call feedkeys("axyz\<Esc>ODX", "xt")
  1658. call assert_equal(["DX", "xyz"], getline(1, 2))
  1659. bwipe!
  1660. " set esckeys
  1661. endfunc
  1662. " Test for running an invalid ex command in insert mode using CTRL-O
  1663. func Test_edit_ctrl_o_invalid_cmd()
  1664. new
  1665. set showmode showcmd
  1666. " Avoid a sleep of 3 seconds. Zero might have side effects.
  1667. " call test_override('ui_delay', 50)
  1668. let caught_e492 = 0
  1669. try
  1670. call feedkeys("i\<C-O>:invalid\<CR>abc\<Esc>", "xt")
  1671. catch /E492:/
  1672. let caught_e492 = 1
  1673. endtry
  1674. call assert_equal(1, caught_e492)
  1675. call assert_equal('abc', getline(1))
  1676. set showmode& showcmd&
  1677. " call test_override('ui_delay', 0)
  1678. close!
  1679. endfunc
  1680. " Test for editing a file with a very long name
  1681. func Test_edit_illegal_filename()
  1682. CheckEnglish
  1683. new
  1684. redir => msg
  1685. exe 'edit ' . repeat('f', 5000)
  1686. redir END
  1687. call assert_match("Illegal file name$", split(msg, "\n")[0])
  1688. close!
  1689. endfunc
  1690. " Test for editing a directory
  1691. func Test_edit_is_a_directory()
  1692. CheckEnglish
  1693. let dirname = getcwd() . "/Xeditdir"
  1694. call mkdir(dirname, 'p')
  1695. new
  1696. redir => msg
  1697. exe 'edit' dirname
  1698. redir END
  1699. call assert_match("is a directory$", split(msg, "\n")[0])
  1700. bwipe!
  1701. let dirname .= '/'
  1702. new
  1703. redir => msg
  1704. exe 'edit' dirname
  1705. redir END
  1706. call assert_match("is a directory$", split(msg, "\n")[0])
  1707. bwipe!
  1708. call delete(dirname, 'rf')
  1709. endfunc
  1710. " Test for editing a file using invalid file encoding
  1711. func Test_edit_invalid_encoding()
  1712. CheckEnglish
  1713. call writefile([], 'Xinvfile', 'D')
  1714. redir => msg
  1715. new ++enc=axbyc Xinvfile
  1716. redir END
  1717. call assert_match('\[NOT converted\]', msg)
  1718. close!
  1719. endfunc
  1720. " Test for the "charconvert" option
  1721. func Test_edit_charconvert()
  1722. CheckEnglish
  1723. call writefile(['one', 'two'], 'Xccfile', 'D')
  1724. " set 'charconvert' to a non-existing function
  1725. set charconvert=NonExitingFunc()
  1726. new
  1727. let caught_e117 = v:false
  1728. try
  1729. redir => msg
  1730. edit ++enc=axbyc Xccfile
  1731. catch /E117:/
  1732. let caught_e117 = v:true
  1733. finally
  1734. redir END
  1735. endtry
  1736. call assert_true(caught_e117)
  1737. call assert_equal(['one', 'two'], getline(1, '$'))
  1738. call assert_match("Conversion with 'charconvert' failed", msg)
  1739. close!
  1740. set charconvert&
  1741. " 'charconvert' function doesn't create an output file
  1742. func Cconv1()
  1743. endfunc
  1744. set charconvert=Cconv1()
  1745. new
  1746. redir => msg
  1747. edit ++enc=axbyc Xccfile
  1748. redir END
  1749. call assert_equal(['one', 'two'], getline(1, '$'))
  1750. call assert_match("can't read output of 'charconvert'", msg)
  1751. close!
  1752. delfunc Cconv1
  1753. set charconvert&
  1754. " 'charconvert' function to convert to upper case
  1755. func Cconv2()
  1756. let data = readfile(v:fname_in)
  1757. call map(data, 'toupper(v:val)')
  1758. call writefile(data, v:fname_out)
  1759. endfunc
  1760. set charconvert=Cconv2()
  1761. new Xccfile
  1762. write ++enc=ucase Xccfile1
  1763. call assert_equal(['ONE', 'TWO'], readfile('Xccfile1'))
  1764. call delete('Xccfile1')
  1765. close!
  1766. delfunc Cconv2
  1767. set charconvert&
  1768. " 'charconvert' function removes the input file
  1769. func Cconv3()
  1770. call delete(v:fname_in)
  1771. endfunc
  1772. set charconvert=Cconv3()
  1773. new
  1774. call assert_fails('edit ++enc=lcase Xccfile', 'E202:')
  1775. call assert_equal([''], getline(1, '$'))
  1776. close!
  1777. delfunc Cconv3
  1778. set charconvert&
  1779. endfunc
  1780. " Test for editing a file without read permission
  1781. func Test_edit_file_no_read_perm()
  1782. CheckUnix
  1783. CheckNotRoot
  1784. call writefile(['one', 'two'], 'Xnrpfile', 'D')
  1785. call setfperm('Xnrpfile', '-w-------')
  1786. new
  1787. redir => msg
  1788. edit Xnrpfile
  1789. redir END
  1790. call assert_equal(1, &readonly)
  1791. call assert_equal([''], getline(1, '$'))
  1792. call assert_match('\[Permission Denied\]', msg)
  1793. close!
  1794. endfunc
  1795. " Using :edit without leaving 'insertmode' should not cause Insert mode to be
  1796. " re-entered immediately after <C-L>
  1797. func Test_edit_insertmode_ex_edit()
  1798. CheckRunVimInTerminal
  1799. let lines =<< trim END
  1800. set insertmode noruler
  1801. inoremap <C-B> <Cmd>edit Xfoo<CR>
  1802. END
  1803. call writefile(lines, 'Xtest_edit_insertmode_ex_edit', 'D')
  1804. let buf = RunVimInTerminal('-S Xtest_edit_insertmode_ex_edit', #{rows: 6, wait_for_ruler: 0})
  1805. " Somehow when using valgrind "INSERT" does not show up unless we send
  1806. " something to the terminal.
  1807. for i in range(30)
  1808. if term_getline(buf, 6) =~ 'INSERT'
  1809. break
  1810. endif
  1811. call term_sendkeys(buf, "-")
  1812. sleep 100m
  1813. endfor
  1814. call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, 6))})
  1815. call term_sendkeys(buf, "\<C-B>\<C-L>")
  1816. call WaitForAssert({-> assert_notmatch('^-- INSERT --\s*$', term_getline(buf, 6))})
  1817. " clean up
  1818. call StopVimInTerminal(buf)
  1819. endfunc
  1820. " Pressing escape in 'insertmode' should beep
  1821. " FIXME: Execute this later, when using valgrind it makes the next test
  1822. " Test_edit_insertmode_ex_edit() fail.
  1823. func Test_z_edit_insertmode_esc_beeps()
  1824. new
  1825. " set insertmode
  1826. " call assert_beeps("call feedkeys(\"one\<Esc>\", 'xt')")
  1827. set insertmode&
  1828. " unsupported "CTRL-G l" command should beep in insert mode.
  1829. call assert_beeps("normal i\<C-G>l")
  1830. bwipe!
  1831. endfunc
  1832. " Test for 'hkmap' and 'hkmapp'
  1833. func Test_edit_hkmap()
  1834. throw "Skipped: Nvim does not support 'hkmap'"
  1835. CheckFeature rightleft
  1836. if has('win32') && !has('gui')
  1837. " Test fails on the MS-Windows terminal version
  1838. return
  1839. endif
  1840. new
  1841. set revins hkmap
  1842. let str = 'abcdefghijklmnopqrstuvwxyz'
  1843. let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  1844. let str ..= '`/'',.;'
  1845. call feedkeys('i' .. str, 'xt')
  1846. let expected = "óõú,.;"
  1847. let expected ..= "ZYXWVUTSRQPONMLKJIHGFEDCBA"
  1848. let expected ..= "æèñ'äåàãø/ôíîöêìçïéòë÷âáðù"
  1849. call assert_equal(expected, getline(1))
  1850. %d
  1851. set revins hkmap hkmapp
  1852. let str = 'abcdefghijklmnopqrstuvwxyz'
  1853. let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  1854. call feedkeys('i' .. str, 'xt')
  1855. let expected = "õYXWVUTSRQóOïíLKJIHGFEDêBA"
  1856. let expected ..= "öòXùåèúæø'ôñðîì÷çéäâóǟãëáà"
  1857. call assert_equal(expected, getline(1))
  1858. set revins& hkmap& hkmapp&
  1859. close!
  1860. endfunc
  1861. " Test for 'allowrevins' and using CTRL-_ in insert mode
  1862. func Test_edit_allowrevins()
  1863. CheckFeature rightleft
  1864. new
  1865. set allowrevins
  1866. call feedkeys("iABC\<C-_>DEF\<C-_>GHI", 'xt')
  1867. call assert_equal('ABCFEDGHI', getline(1))
  1868. set allowrevins&
  1869. close!
  1870. endfunc
  1871. " Test for inserting a register in insert mode using CTRL-R
  1872. func Test_edit_insert_reg()
  1873. throw 'Skipped: use test/functional/legacy/edit_spec.lua'
  1874. new
  1875. let g:Line = ''
  1876. func SaveFirstLine()
  1877. let g:Line = Screenline(1)
  1878. return 'r'
  1879. endfunc
  1880. inoremap <expr> <buffer> <F2> SaveFirstLine()
  1881. call test_override('redraw_flag', 1)
  1882. call test_override('char_avail', 1)
  1883. let @r = 'sample'
  1884. call feedkeys("a\<C-R>=SaveFirstLine()\<CR>", "xt")
  1885. call assert_equal('"', g:Line)
  1886. " Test for inserting an null and an empty list
  1887. call feedkeys("a\<C-R>=test_null_list()\<CR>", "xt")
  1888. call feedkeys("a\<C-R>=[]\<CR>", "xt")
  1889. call assert_equal(['r'], getbufline('', 1, '$'))
  1890. call test_override('ALL', 0)
  1891. close!
  1892. endfunc
  1893. " Test for positioning cursor after CTRL-R expression failed
  1894. func Test_edit_ctrl_r_failed()
  1895. CheckRunVimInTerminal
  1896. let buf = RunVimInTerminal('', #{rows: 6, cols: 60})
  1897. " trying to insert a blob produces an error
  1898. call term_sendkeys(buf, "i\<C-R>=0z\<CR>")
  1899. " ending Insert mode should put the cursor back on the ':'
  1900. call term_sendkeys(buf, ":\<Esc>")
  1901. call VerifyScreenDump(buf, 'Test_edit_ctlr_r_failed_1', {})
  1902. call StopVimInTerminal(buf)
  1903. endfunc
  1904. " When a character is inserted at the last position of the last line in a
  1905. " window, the window contents should be scrolled one line up. If the top line
  1906. " is part of a fold, then the entire fold should be scrolled up.
  1907. func Test_edit_lastline_scroll()
  1908. new
  1909. let h = winheight(0)
  1910. let lines = ['one', 'two', 'three']
  1911. let lines += repeat(['vim'], h - 4)
  1912. call setline(1, lines)
  1913. call setline(h, repeat('x', winwidth(0) - 1))
  1914. call feedkeys("GAx", 'xt')
  1915. redraw!
  1916. call assert_equal(h - 1, winline())
  1917. call assert_equal(2, line('w0'))
  1918. " scroll with a fold
  1919. 1,2fold
  1920. normal gg
  1921. call setline(h + 1, repeat('x', winwidth(0) - 1))
  1922. call feedkeys("GAx", 'xt')
  1923. redraw!
  1924. call assert_equal(h - 1, winline())
  1925. call assert_equal(3, line('w0'))
  1926. close!
  1927. endfunc
  1928. func Test_edit_browse()
  1929. " in the GUI this opens a file picker, we only test the terminal behavior
  1930. CheckNotGui
  1931. " ":browse xxx" checks for the FileExplorer augroup and assumes editing "."
  1932. " works then.
  1933. augroup FileExplorer
  1934. au!
  1935. augroup END
  1936. " When the CASE_INSENSITIVE_FILENAME is defined this used to cause a crash.
  1937. browse enew
  1938. bwipe!
  1939. browse split
  1940. bwipe!
  1941. endfunc
  1942. func Test_read_invalid()
  1943. " set encoding=latin1
  1944. " This was not properly checking for going past the end.
  1945. call assert_fails('r`=', 'E484')
  1946. set encoding=utf-8
  1947. endfunc
  1948. " Test for the 'revins' option
  1949. func Test_edit_revins()
  1950. CheckFeature rightleft
  1951. new
  1952. set revins
  1953. exe "normal! ione\ttwo three"
  1954. call assert_equal("eerht owt\teno", getline(1))
  1955. call setline(1, "one\ttwo three")
  1956. normal! gg$bi a
  1957. call assert_equal("one\ttwo a three", getline(1))
  1958. exe "normal! $bi\<BS>\<BS>"
  1959. call assert_equal("one\ttwo a ree", getline(1))
  1960. exe "normal! 0wi\<C-W>"
  1961. call assert_equal("one\t a ree", getline(1))
  1962. exe "normal! 0wi\<C-U>"
  1963. call assert_equal("one\t ", getline(1))
  1964. " newline in insert mode starts at the end of the line
  1965. call setline(1, 'one two three')
  1966. exe "normal! wi\nfour"
  1967. call assert_equal(['one two three', 'ruof'], getline(1, '$'))
  1968. set backspace=indent,eol,start
  1969. exe "normal! ggA\<BS>:"
  1970. call assert_equal(['one two three:ruof'], getline(1, '$'))
  1971. set revins& backspace&
  1972. bw!
  1973. endfunc
  1974. " Test for getting the character of the line below after "p"
  1975. func Test_edit_put_CTRL_E()
  1976. " set encoding=latin1
  1977. new
  1978. let @" = ''
  1979. sil! norm orggRx
  1980. sil! norm pr
  1981. call assert_equal(['r', 'r'], getline(1, 2))
  1982. bwipe!
  1983. set encoding=utf-8
  1984. endfunc
  1985. " Test toggling of input method. See :help i_CTRL-^
  1986. func Test_edit_CTRL_hat()
  1987. CheckFeature xim
  1988. " FIXME: test fails with Motif GUI.
  1989. " test also fails when running in the GUI.
  1990. CheckFeature gui_gtk
  1991. CheckNotGui
  1992. new
  1993. call assert_equal(0, &iminsert)
  1994. call feedkeys("i\<C-^>", 'xt')
  1995. call assert_equal(2, &iminsert)
  1996. call feedkeys("i\<C-^>", 'xt')
  1997. call assert_equal(0, &iminsert)
  1998. bwipe!
  1999. endfunc
  2000. " Weird long file name was going over the end of NameBuff
  2001. func Test_edit_overlong_file_name()
  2002. CheckUnix
  2003. file 0000000000000000000000000000
  2004. file %%%%%%%%%%%%%%%%%%%%%%%%%%
  2005. file %%%%%%
  2006. set readonly
  2007. set ls=2
  2008. redraw!
  2009. set noreadonly ls&
  2010. bwipe!
  2011. endfunc
  2012. func Test_edit_shift_bs()
  2013. CheckMSWindows
  2014. " FIXME: this works interactively, but the test fails
  2015. throw 'Skipped: Shift-Backspace Test not working correctly :('
  2016. " Need to run this in Win32 Terminal, do not use CheckRunVimInTerminal
  2017. if !has("terminal")
  2018. return
  2019. endif
  2020. " Shift Backspace should work like Backspace in insert mode
  2021. let lines =<< trim END
  2022. call setline(1, ['abc'])
  2023. END
  2024. call writefile(lines, 'Xtest_edit_shift_bs', 'D')
  2025. let buf = RunVimInTerminal('-S Xtest_edit_shift_bs', #{rows: 3})
  2026. call term_sendkeys(buf, "A\<S-BS>-\<esc>")
  2027. call TermWait(buf, 50)
  2028. call assert_equal('ab-', term_getline(buf, 1))
  2029. " clean up
  2030. call StopVimInTerminal(buf)
  2031. endfunc
  2032. func Test_edit_Ctrl_RSB()
  2033. new
  2034. let g:triggered = []
  2035. autocmd InsertCharPre <buffer> let g:triggered += [v:char]
  2036. " i_CTRL-] should not trigger InsertCharPre
  2037. exe "normal! A\<C-]>"
  2038. call assert_equal([], g:triggered)
  2039. " i_CTRL-] should expand abbreviations but not trigger InsertCharPre
  2040. inoreabbr <buffer> f foo
  2041. exe "normal! Af\<C-]>a"
  2042. call assert_equal(['f', 'f', 'o', 'o', 'a'], g:triggered)
  2043. call assert_equal('fooa', getline(1))
  2044. " CTRL-] followed by i_CTRL-V should not expand abbreviations
  2045. " i_CTRL-V doesn't trigger InsertCharPre
  2046. call setline(1, '')
  2047. exe "normal! Af\<C-V>\<C-]>"
  2048. call assert_equal("f\<C-]>", getline(1))
  2049. let g:triggered = []
  2050. call setline(1, '')
  2051. " Also test assigning to v:char
  2052. autocmd InsertCharPre <buffer> let v:char = 'f'
  2053. exe "normal! Ag\<C-]>h"
  2054. call assert_equal(['g', 'f', 'o', 'o', 'h'], g:triggered)
  2055. call assert_equal('ffff', getline(1))
  2056. autocmd! InsertCharPre
  2057. unlet g:triggered
  2058. bwipe!
  2059. endfunc
  2060. func s:check_backspace(expected)
  2061. let g:actual = []
  2062. inoremap <buffer> <F2> <Cmd>let g:actual += [getline('.')]<CR>
  2063. set backspace=indent,eol,start
  2064. exe "normal i" .. repeat("\<BS>\<F2>", len(a:expected))
  2065. call assert_equal(a:expected, g:actual)
  2066. set backspace&
  2067. iunmap <buffer> <F2>
  2068. unlet g:actual
  2069. endfunc
  2070. " Test that backspace works with 'smarttab' and mixed Tabs and spaces.
  2071. func Test_edit_backspace_smarttab_mixed()
  2072. set smarttab
  2073. call NewWindow(1, 30)
  2074. setlocal tabstop=4 shiftwidth=4
  2075. call setline(1, "\t \t \t a")
  2076. normal! $
  2077. call s:check_backspace([
  2078. \ "\t \t \ta",
  2079. \ "\t \t a",
  2080. \ "\t \t a",
  2081. \ "\t \ta",
  2082. \ "\t a",
  2083. \ "\ta",
  2084. \ "a",
  2085. \ ])
  2086. call CloseWindow()
  2087. set smarttab&
  2088. endfunc
  2089. " Test that backspace works with 'smarttab' and 'varsofttabstop'.
  2090. func Test_edit_backspace_smarttab_varsofttabstop()
  2091. CheckFeature vartabs
  2092. set smarttab
  2093. call NewWindow(1, 30)
  2094. setlocal tabstop=8 varsofttabstop=6,2,5,3
  2095. call setline(1, "a\t \t a")
  2096. normal! $
  2097. call s:check_backspace([
  2098. \ "a\t \ta",
  2099. \ "a\t a",
  2100. \ "a\ta",
  2101. \ "a a",
  2102. \ "aa",
  2103. \ "a",
  2104. \ ])
  2105. call CloseWindow()
  2106. set smarttab&
  2107. endfunc
  2108. " Test that backspace works with 'smarttab' when a Tab is shown as "^I".
  2109. func Test_edit_backspace_smarttab_list()
  2110. set smarttab
  2111. call NewWindow(1, 30)
  2112. setlocal tabstop=4 shiftwidth=4 list listchars=
  2113. call setline(1, "\t \t \t a")
  2114. normal! $
  2115. call s:check_backspace([
  2116. \ "\t \t a",
  2117. \ "\t \t a",
  2118. \ "\t \ta",
  2119. \ "\t a",
  2120. \ "a",
  2121. \ ])
  2122. call CloseWindow()
  2123. set smarttab&
  2124. endfunc
  2125. " Test that backspace works with 'smarttab' and 'breakindent'.
  2126. func Test_edit_backspace_smarttab_breakindent()
  2127. CheckFeature linebreak
  2128. set smarttab
  2129. call NewWindow(3, 17)
  2130. setlocal tabstop=4 shiftwidth=4 breakindent breakindentopt=min:5
  2131. call setline(1, "\t \t \t a")
  2132. normal! $
  2133. call s:check_backspace([
  2134. \ "\t \t \ta",
  2135. \ "\t \t a",
  2136. \ "\t \t a",
  2137. \ "\t \ta",
  2138. \ "\t a",
  2139. \ "\ta",
  2140. \ "a",
  2141. \ ])
  2142. call CloseWindow()
  2143. set smarttab&
  2144. endfunc
  2145. " Test that backspace works with 'smarttab' and virtual text.
  2146. func Test_edit_backspace_smarttab_virtual_text()
  2147. CheckFeature textprop
  2148. set smarttab
  2149. call NewWindow(1, 50)
  2150. setlocal tabstop=4 shiftwidth=4
  2151. call setline(1, "\t \t \t a")
  2152. call prop_type_add('theprop', {})
  2153. call prop_add(1, 3, {'type': 'theprop', 'text': 'text'})
  2154. normal! $
  2155. call s:check_backspace([
  2156. \ "\t \t \ta",
  2157. \ "\t \t a",
  2158. \ "\t \t a",
  2159. \ "\t \ta",
  2160. \ "\t a",
  2161. \ "\ta",
  2162. \ "a",
  2163. \ ])
  2164. call CloseWindow()
  2165. call prop_type_delete('theprop')
  2166. set smarttab&
  2167. endfunc
  2168. func Test_edit_CAR()
  2169. set cot=menu,menuone,noselect
  2170. new
  2171. call feedkeys("Shello hero\<CR>h\<C-x>\<C-N>e\<CR>", 'tx')
  2172. call assert_equal(['hello hero', 'he', ''], getline(1, '$'))
  2173. bw!
  2174. set cot&
  2175. endfunc
  2176. " vim: shiftwidth=2 sts=2 expandtab