test_edit.vim 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292
  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\<cr>\<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_CR()
  443. " Test for <CR> in insert mode
  444. " basically only in quickfix mode it's tested, the rest
  445. " has been taken care of by other tests
  446. CheckFeature quickfix
  447. botright new
  448. call writefile(range(1, 10), 'Xqflist.txt')
  449. call setqflist([{'filename': 'Xqflist.txt', 'lnum': 2}])
  450. copen
  451. set modifiable
  452. call feedkeys("A\<cr>", 'tnix')
  453. call assert_equal('Xqflist.txt', bufname(''))
  454. call assert_equal(2, line('.'))
  455. cclose
  456. botright new
  457. call setloclist(0, [{'filename': 'Xqflist.txt', 'lnum': 10}])
  458. lopen
  459. set modifiable
  460. call feedkeys("A\<cr>", 'tnix')
  461. call assert_equal('Xqflist.txt', bufname(''))
  462. call assert_equal(10, line('.'))
  463. call feedkeys("A\<Enter>", 'tnix')
  464. call feedkeys("A\<kEnter>", 'tnix')
  465. call feedkeys("A\n", 'tnix')
  466. call feedkeys("A\r", 'tnix')
  467. call assert_equal(map(range(1, 10), 'string(v:val)') + ['', '', '', ''], getline(1, '$'))
  468. bw!
  469. lclose
  470. call delete('Xqflist.txt')
  471. endfunc
  472. func Test_edit_CTRL_()
  473. " disabled for Windows builds, why?
  474. if !has("rightleft") || has("win32")
  475. return
  476. endif
  477. let _encoding=&encoding
  478. set encoding=utf-8
  479. " Test for CTRL-_
  480. new
  481. call setline(1, ['abc'])
  482. call cursor(1, 1)
  483. call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
  484. call assert_equal(["\<C-_>xyzabc"], getline(1, '$'))
  485. call assert_false(&revins)
  486. set ari
  487. call setline(1, ['abc'])
  488. call cursor(1, 1)
  489. call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
  490. " call assert_equal(["æèñabc"], getline(1, '$'))
  491. call assert_equal(["zyxabc"], getline(1, '$'))
  492. call assert_true(&revins)
  493. call setline(1, ['abc'])
  494. call cursor(1, 1)
  495. call feedkeys("i\<c-_>xyz\<esc>", 'tnix')
  496. call assert_equal(["xyzabc"], getline(1, '$'))
  497. call assert_false(&revins)
  498. set noari
  499. let &encoding=_encoding
  500. bw!
  501. endfunc
  502. " needs to come first, to have the @. register empty
  503. func Test_edit_00a_CTRL_A()
  504. " Test pressing CTRL-A
  505. new
  506. call setline(1, repeat([''], 5))
  507. call cursor(1, 1)
  508. try
  509. call feedkeys("A\<NUL>", 'tnix')
  510. catch /^Vim\%((\a\+)\)\=:E29/
  511. call assert_true(1, 'E29 error caught')
  512. endtry
  513. call cursor(1, 1)
  514. call feedkeys("Afoobar \<esc>", 'tnix')
  515. call cursor(2, 1)
  516. call feedkeys("A\<c-a>more\<esc>", 'tnix')
  517. call cursor(3, 1)
  518. call feedkeys("A\<NUL>and more\<esc>", 'tnix')
  519. call assert_equal(['foobar ', 'foobar more', 'foobar morend more', '', ''], getline(1, '$'))
  520. bw!
  521. endfunc
  522. func Test_edit_CTRL_EY()
  523. " Ctrl-E/ Ctrl-Y in insert mode completion to scroll
  524. 10new
  525. call setline(1, range(1, 100))
  526. call cursor(30, 1)
  527. norm! z.
  528. call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
  529. call assert_equal(30, winsaveview()['topline'])
  530. call assert_equal([0, 30, 2, 0], getpos('.'))
  531. call feedkeys("A\<c-x>\<c-e>\<c-e>\<c-e>\<c-e>\<c-e>", 'tnix')
  532. call feedkeys("A\<c-x>".repeat("\<c-y>", 10), 'tnix')
  533. call assert_equal(21, winsaveview()['topline'])
  534. call assert_equal([0, 30, 2, 0], getpos('.'))
  535. bw!
  536. endfunc
  537. func Test_edit_CTRL_G()
  538. new
  539. call setline(1, ['foobar', 'foobar', 'foobar'])
  540. call cursor(2, 4)
  541. call feedkeys("ioooooooo\<c-g>k\<c-r>.\<esc>", 'tnix')
  542. call assert_equal(['foooooooooobar', 'foooooooooobar', 'foobar'], getline(1, '$'))
  543. call assert_equal([0, 1, 11, 0], getpos('.'))
  544. call feedkeys("i\<c-g>k\<esc>", 'tnix')
  545. call assert_equal([0, 1, 10, 0], getpos('.'))
  546. call cursor(2, 4)
  547. call feedkeys("i\<c-g>jzzzz\<esc>", 'tnix')
  548. call assert_equal(['foooooooooobar', 'foooooooooobar', 'foozzzzbar'], getline(1, '$'))
  549. call assert_equal([0, 3, 7, 0], getpos('.'))
  550. call feedkeys("i\<c-g>j\<esc>", 'tnix')
  551. call assert_equal([0, 3, 6, 0], getpos('.'))
  552. call assert_nobeep("normal! i\<c-g>\<esc>")
  553. bw!
  554. endfunc
  555. func Test_edit_CTRL_I()
  556. " Tab in completion mode
  557. let path=expand("%:p:h")
  558. new
  559. call setline(1, [path. "/", ''])
  560. call feedkeys("Arunt\<c-x>\<c-f>\<tab>\<cr>\<esc>", 'tnix')
  561. call assert_match('runtest\.vim', getline(1))
  562. %d
  563. call writefile(['one', 'two', 'three'], 'Xinclude.txt')
  564. let include='#include Xinclude.txt'
  565. call setline(1, [include, ''])
  566. call cursor(2, 1)
  567. call feedkeys("A\<c-x>\<tab>\<cr>\<esc>", 'tnix')
  568. call assert_equal([include, 'one', ''], getline(1, '$'))
  569. call feedkeys("2ggC\<c-x>\<tab>\<down>\<cr>\<esc>", 'tnix')
  570. call assert_equal([include, 'two', ''], getline(1, '$'))
  571. call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<cr>\<esc>", 'tnix')
  572. call assert_equal([include, 'three', ''], getline(1, '$'))
  573. call feedkeys("2ggC\<c-x>\<tab>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
  574. call assert_equal([include, '', ''], getline(1, '$'))
  575. call delete("Xinclude.txt")
  576. bw!
  577. endfunc
  578. func Test_edit_CTRL_K()
  579. " Test pressing CTRL-K (basically only dictionary completion and digraphs
  580. " the rest is already covered
  581. call writefile(['A', 'AA', 'AAA', 'AAAA'], 'Xdictionary.txt')
  582. set dictionary=Xdictionary.txt
  583. new
  584. call setline(1, 'A')
  585. call cursor(1, 1)
  586. call feedkeys("A\<c-x>\<c-k>\<cr>\<esc>", 'tnix')
  587. call assert_equal(['AA', ''], getline(1, '$'))
  588. %d
  589. call setline(1, 'A')
  590. call cursor(1, 1)
  591. call feedkeys("A\<c-x>\<c-k>\<down>\<cr>\<esc>", 'tnix')
  592. call assert_equal(['AAA'], getline(1, '$'))
  593. %d
  594. call setline(1, 'A')
  595. call cursor(1, 1)
  596. call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<cr>\<esc>", 'tnix')
  597. call assert_equal(['AAAA'], getline(1, '$'))
  598. %d
  599. call setline(1, 'A')
  600. call cursor(1, 1)
  601. call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
  602. call assert_equal(['A'], getline(1, '$'))
  603. %d
  604. call setline(1, 'A')
  605. call cursor(1, 1)
  606. call feedkeys("A\<c-x>\<c-k>\<down>\<down>\<down>\<down>\<cr>\<esc>", 'tnix')
  607. call assert_equal(['AA'], getline(1, '$'))
  608. " press an unexpected key after dictionary completion
  609. %d
  610. call setline(1, 'A')
  611. call cursor(1, 1)
  612. call feedkeys("A\<c-x>\<c-k>\<c-]>\<cr>\<esc>", 'tnix')
  613. call assert_equal(['AA', ''], getline(1, '$'))
  614. %d
  615. call setline(1, 'A')
  616. call cursor(1, 1)
  617. call feedkeys("A\<c-x>\<c-k>\<c-s>\<cr>\<esc>", 'tnix')
  618. call assert_equal(["AA\<c-s>", ''], getline(1, '$'))
  619. %d
  620. call setline(1, 'A')
  621. call cursor(1, 1)
  622. call feedkeys("A\<c-x>\<c-k>\<c-f>\<cr>\<esc>", 'tnix')
  623. call assert_equal(["AA\<c-f>", ''], getline(1, '$'))
  624. set dictionary=
  625. %d
  626. call setline(1, 'A')
  627. call cursor(1, 1)
  628. let v:testing = 1
  629. try
  630. call feedkeys("A\<c-x>\<c-k>\<esc>", 'tnix')
  631. catch
  632. " error sleeps 2 seconds, when v:testing is not set
  633. let v:testing = 0
  634. endtry
  635. call delete('Xdictionary.txt')
  636. if exists('*test_override')
  637. call test_override("char_avail", 1)
  638. set showcmd
  639. %d
  640. call feedkeys("A\<c-k>a:\<esc>", 'tnix')
  641. call assert_equal(['ä'], getline(1, '$'))
  642. call test_override("char_avail", 0)
  643. set noshowcmd
  644. endif
  645. bw!
  646. endfunc
  647. func Test_edit_CTRL_L()
  648. " Test Ctrl-X Ctrl-L (line completion)
  649. new
  650. set complete=.
  651. call setline(1, ['one', 'two', 'three', '', '', '', ''])
  652. call cursor(4, 1)
  653. call feedkeys("A\<c-x>\<c-l>\<esc>", 'tnix')
  654. call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
  655. call feedkeys("cct\<c-x>\<c-l>\<c-n>\<esc>", 'tnix')
  656. call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
  657. call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<esc>", 'tnix')
  658. call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
  659. call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
  660. call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
  661. call feedkeys("cct\<c-x>\<c-l>\<c-n>\<c-n>\<c-n>\<c-n>\<esc>", 'tnix')
  662. call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
  663. call feedkeys("cct\<c-x>\<c-l>\<c-p>\<esc>", 'tnix')
  664. call assert_equal(['one', 'two', 'three', 'two', '', '', ''], getline(1, '$'))
  665. call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<esc>", 'tnix')
  666. call assert_equal(['one', 'two', 'three', 't', '', '', ''], getline(1, '$'))
  667. call feedkeys("cct\<c-x>\<c-l>\<c-p>\<c-p>\<c-p>\<esc>", 'tnix')
  668. call assert_equal(['one', 'two', 'three', 'three', '', '', ''], getline(1, '$'))
  669. set complete=
  670. call cursor(5, 1)
  671. call feedkeys("A\<c-x>\<c-l>\<c-p>\<c-n>\<esc>", 'tnix')
  672. call assert_equal(['one', 'two', 'three', 'three', "\<c-l>\<c-p>\<c-n>", '', ''], getline(1, '$'))
  673. set complete&
  674. %d
  675. if has("conceal") && has("syntax") && !has("nvim")
  676. call setline(1, ['foo', 'bar', 'foobar'])
  677. call test_override("char_avail", 1)
  678. set conceallevel=2 concealcursor=n
  679. syn on
  680. syn match ErrorMsg "^bar"
  681. call matchadd("Conceal", 'oo', 10, -1, {'conceal': 'X'})
  682. func! DoIt()
  683. let g:change=1
  684. endfunc
  685. au! TextChangedI <buffer> :call DoIt()
  686. call cursor(2, 1)
  687. call assert_false(exists("g:change"))
  688. call feedkeys("A \<esc>", 'tnix')
  689. call assert_equal(['foo', 'bar ', 'foobar'], getline(1, '$'))
  690. call assert_equal(1, g:change)
  691. call test_override("char_avail", 0)
  692. call clearmatches()
  693. syn off
  694. au! TextChangedI
  695. delfu DoIt
  696. unlet! g:change
  697. endif
  698. bw!
  699. endfunc
  700. func Test_edit_CTRL_N()
  701. " Check keyword completion
  702. " for e in ['latin1', 'utf-8']
  703. for e in ['utf-8']
  704. exe 'set encoding=' .. e
  705. new
  706. set complete=.
  707. call setline(1, ['INFER', 'loWER', '', '', ])
  708. call cursor(3, 1)
  709. call feedkeys("Ai\<c-n>\<cr>\<esc>", "tnix")
  710. call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
  711. call assert_equal(['INFER', 'loWER', 'i', 'LO', '', ''], getline(1, '$'), e)
  712. %d
  713. call setline(1, ['INFER', 'loWER', '', '', ])
  714. call cursor(3, 1)
  715. set ignorecase infercase
  716. call feedkeys("Ii\<c-n>\<cr>\<esc>", "tnix")
  717. call feedkeys("ILO\<c-n>\<cr>\<esc>", 'tnix')
  718. call assert_equal(['INFER', 'loWER', 'infer', 'LOWER', '', ''], getline(1, '$'), e)
  719. set noignorecase noinfercase
  720. %d
  721. call setline(1, ['one word', 'two word'])
  722. exe "normal! Goo\<C-P>\<C-X>\<C-P>"
  723. call assert_equal('one word', getline(3))
  724. %d
  725. set complete&
  726. bw!
  727. endfor
  728. endfunc
  729. func Test_edit_CTRL_O()
  730. " Check for CTRL-O in insert mode
  731. new
  732. inoreabbr <buffer> h here some more
  733. call setline(1, ['abc', 'def'])
  734. call cursor(1, 1)
  735. " Ctrl-O after an abbreviation
  736. exe "norm A h\<c-o>:set nu\<cr> text"
  737. call assert_equal(['abc here some more text', 'def'], getline(1, '$'))
  738. call assert_true(&nu)
  739. set nonu
  740. iunabbr <buffer> h
  741. " Ctrl-O at end of line with 've'=onemore
  742. call cursor(1, 1)
  743. call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
  744. call assert_equal([0, 1, 23, 0], g:a)
  745. call cursor(1, 1)
  746. set ve=onemore
  747. call feedkeys("A\<c-o>:let g:a=getpos('.')\<cr>\<esc>", 'tnix')
  748. call assert_equal([0, 1, 24, 0], g:a)
  749. set ve=
  750. unlet! g:a
  751. bw!
  752. endfunc
  753. func Test_edit_CTRL_R()
  754. " Insert Register
  755. new
  756. " call test_override("ALL", 1)
  757. set showcmd
  758. call feedkeys("AFOOBAR eins zwei\<esc>", 'tnix')
  759. call feedkeys("O\<c-r>.", 'tnix')
  760. call feedkeys("O\<c-r>=10*500\<cr>\<esc>", 'tnix')
  761. call feedkeys("O\<c-r>=getreg('=', 1)\<cr>\<esc>", 'tnix')
  762. call assert_equal(["getreg('=', 1)", '5000', "FOOBAR eins zwei", "FOOBAR eins zwei"], getline(1, '$'))
  763. " call test_override("ALL", 0)
  764. set noshowcmd
  765. bw!
  766. endfunc
  767. func Test_edit_CTRL_S()
  768. " Test pressing CTRL-S (basically only spellfile completion)
  769. " the rest is already covered
  770. new
  771. if !has("spell")
  772. call setline(1, 'vim')
  773. call feedkeys("A\<c-x>ss\<cr>\<esc>", 'tnix')
  774. call assert_equal(['vims', ''], getline(1, '$'))
  775. bw!
  776. return
  777. endif
  778. call setline(1, 'vim')
  779. " spell option not yet set
  780. try
  781. call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
  782. catch /^Vim\%((\a\+)\)\=:E756/
  783. call assert_true(1, 'error caught')
  784. endtry
  785. call assert_equal(['vim', ''], getline(1, '$'))
  786. %d
  787. setl spell spelllang=en
  788. call setline(1, 'vim')
  789. call cursor(1, 1)
  790. call feedkeys("A\<c-x>\<c-s>\<cr>\<esc>", 'tnix')
  791. call assert_equal(['Vim', ''], getline(1, '$'))
  792. %d
  793. call setline(1, 'vim')
  794. call cursor(1, 1)
  795. call feedkeys("A\<c-x>\<c-s>\<down>\<cr>\<esc>", 'tnix')
  796. call assert_equal(['Aim'], getline(1, '$'))
  797. %d
  798. call setline(1, 'vim')
  799. call cursor(1, 1)
  800. call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
  801. call assert_equal(['vim', ''], getline(1, '$'))
  802. %d
  803. " empty buffer
  804. call cursor(1, 1)
  805. call feedkeys("A\<c-x>\<c-s>\<c-p>\<cr>\<esc>", 'tnix')
  806. call assert_equal(['', ''], getline(1, '$'))
  807. setl nospell
  808. bw!
  809. endfunc
  810. func Test_edit_CTRL_T()
  811. " Check for CTRL-T and CTRL-X CTRL-T in insert mode
  812. " 1) increase indent
  813. new
  814. call setline(1, "abc")
  815. call cursor(1, 1)
  816. call feedkeys("A\<c-t>xyz", 'tnix')
  817. call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
  818. " 2) also when paste option is set
  819. set paste
  820. call setline(1, "abc")
  821. call cursor(1, 1)
  822. call feedkeys("A\<c-t>xyz", 'tnix')
  823. call assert_equal(["\<tab>abcxyz"], getline(1, '$'))
  824. set nopaste
  825. " CTRL-X CTRL-T (thesaurus complete)
  826. call writefile(['angry furious mad enraged'], 'Xthesaurus')
  827. set thesaurus=Xthesaurus
  828. call setline(1, 'mad')
  829. call cursor(1, 1)
  830. call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
  831. call assert_equal(['mad', ''], getline(1, '$'))
  832. %d
  833. call setline(1, 'mad')
  834. call cursor(1, 1)
  835. call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
  836. call assert_equal(['angry', ''], getline(1, '$'))
  837. %d
  838. call setline(1, 'mad')
  839. call cursor(1, 1)
  840. call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
  841. call assert_equal(['furious', ''], getline(1, '$'))
  842. %d
  843. call setline(1, 'mad')
  844. call cursor(1, 1)
  845. call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
  846. call assert_equal(['enraged', ''], getline(1, '$'))
  847. %d
  848. call setline(1, 'mad')
  849. call cursor(1, 1)
  850. call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
  851. call assert_equal(['mad', ''], getline(1, '$'))
  852. %d
  853. call setline(1, 'mad')
  854. call cursor(1, 1)
  855. call feedkeys("A\<c-x>\<c-t>\<c-n>\<c-n>\<c-n>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
  856. call assert_equal(['mad', ''], getline(1, '$'))
  857. " Using <c-p> <c-n> when 'complete' is empty
  858. set complete=
  859. %d
  860. call setline(1, 'mad')
  861. call cursor(1, 1)
  862. call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
  863. call assert_equal(['angry', ''], getline(1, '$'))
  864. %d
  865. call setline(1, 'mad')
  866. call cursor(1, 1)
  867. call feedkeys("A\<c-x>\<c-t>\<c-p>\<cr>\<esc>", 'tnix')
  868. call assert_equal(['mad', ''], getline(1, '$'))
  869. set complete&
  870. set thesaurus=
  871. %d
  872. call setline(1, 'mad')
  873. call cursor(1, 1)
  874. let v:testing = 1
  875. try
  876. call feedkeys("A\<c-x>\<c-t>\<esc>", 'tnix')
  877. catch
  878. " error sleeps 2 seconds, when v:testing is not set
  879. let v:testing = 0
  880. endtry
  881. call assert_equal(['mad'], getline(1, '$'))
  882. call delete('Xthesaurus')
  883. bw!
  884. endfunc
  885. " Test thesaurus completion with different encodings
  886. func Test_thesaurus_complete_with_encoding()
  887. call writefile(['angry furious mad enraged'], 'Xthesaurus')
  888. set thesaurus=Xthesaurus
  889. " for e in ['latin1', 'utf-8']
  890. for e in ['utf-8']
  891. exe 'set encoding=' .. e
  892. new
  893. call setline(1, 'mad')
  894. call cursor(1, 1)
  895. call feedkeys("A\<c-x>\<c-t>\<cr>\<esc>", 'tnix')
  896. call assert_equal(['mad', ''], getline(1, '$'))
  897. bw!
  898. endfor
  899. set thesaurus=
  900. call delete('Xthesaurus')
  901. endfunc
  902. " Test 'thesaurusfunc'
  903. func MyThesaurus(findstart, base)
  904. let mythesaurus = [
  905. \ #{word: "happy",
  906. \ synonyms: "cheerful,blissful,flying high,looking good,peppy"},
  907. \ #{word: "kind",
  908. \ synonyms: "amiable,bleeding-heart,heart in right place"}]
  909. if a:findstart
  910. " locate the start of the word
  911. let line = getline('.')
  912. let start = col('.') - 1
  913. while start > 0 && line[start - 1] =~ '\a'
  914. let start -= 1
  915. endwhile
  916. return start
  917. else
  918. " find strings matching with "a:base"
  919. let res = []
  920. for w in mythesaurus
  921. if w.word =~ '^' . a:base
  922. call add(res, w.word)
  923. call extend(res, split(w.synonyms, ","))
  924. endif
  925. endfor
  926. return res
  927. endif
  928. endfunc
  929. func Test_thesaurus_func()
  930. new
  931. set thesaurus=notused
  932. set thesaurusfunc=NotUsed
  933. setlocal thesaurusfunc=MyThesaurus
  934. call setline(1, "an ki")
  935. call cursor(1, 1)
  936. call feedkeys("A\<c-x>\<c-t>\<c-n>\<cr>\<esc>", 'tnix')
  937. call assert_equal(['an amiable', ''], getline(1, '$'))
  938. setlocal thesaurusfunc=NonExistingFunc
  939. call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
  940. setlocal thesaurusfunc=
  941. set thesaurusfunc=NonExistingFunc
  942. call assert_fails("normal $a\<C-X>\<C-T>", 'E117:')
  943. %bw!
  944. set thesaurusfunc=
  945. set thesaurus=
  946. endfunc
  947. func Test_edit_CTRL_U()
  948. " Test 'completefunc'
  949. new
  950. " -1, -2 and -3 are special return values
  951. let g:special=0
  952. fun! CompleteMonths(findstart, base)
  953. if a:findstart
  954. " locate the start of the word
  955. return g:special
  956. else
  957. " find months matching with "a:base"
  958. let res = []
  959. for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
  960. if m =~ '^\c'.a:base
  961. call add(res, {'word': m, 'abbr': m.' Month', 'icase': 0})
  962. endif
  963. endfor
  964. return {'words': res, 'refresh': 'always'}
  965. endif
  966. endfun
  967. set completefunc=CompleteMonths
  968. call setline(1, ['', ''])
  969. call cursor(1, 1)
  970. call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
  971. call assert_equal(['X', '', ''], getline(1, '$'))
  972. %d
  973. let g:special=-1
  974. call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
  975. call assert_equal(['XJan', ''], getline(1, '$'))
  976. %d
  977. let g:special=-2
  978. call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
  979. call assert_equal(['X', ''], getline(1, '$'))
  980. %d
  981. let g:special=-3
  982. call feedkeys("AX\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
  983. call assert_equal(['X', ''], getline(1, '$'))
  984. %d
  985. let g:special=0
  986. call feedkeys("AM\<c-x>\<c-u>\<cr>\<esc>", 'tnix')
  987. call assert_equal(['Mar', ''], getline(1, '$'))
  988. %d
  989. call feedkeys("AM\<c-x>\<c-u>\<c-n>\<cr>\<esc>", 'tnix')
  990. call assert_equal(['May', ''], getline(1, '$'))
  991. %d
  992. call feedkeys("AM\<c-x>\<c-u>\<c-n>\<c-n>\<cr>\<esc>", 'tnix')
  993. call assert_equal(['M', ''], getline(1, '$'))
  994. delfu CompleteMonths
  995. %d
  996. try
  997. call feedkeys("A\<c-x>\<c-u>", 'tnix')
  998. call assert_fails(1, 'unknown completion function')
  999. catch /^Vim\%((\a\+)\)\=:E117/
  1000. call assert_true(1, 'E117 error caught')
  1001. endtry
  1002. set completefunc=
  1003. bw!
  1004. endfunc
  1005. func Test_edit_completefunc_delete()
  1006. func CompleteFunc(findstart, base)
  1007. if a:findstart == 1
  1008. return col('.') - 1
  1009. endif
  1010. normal dd
  1011. return ['a', 'b']
  1012. endfunc
  1013. new
  1014. set completefunc=CompleteFunc
  1015. call setline(1, ['', 'abcd', ''])
  1016. 2d
  1017. call assert_fails("normal 2G$a\<C-X>\<C-U>", 'E565:')
  1018. bwipe!
  1019. endfunc
  1020. func Test_edit_CTRL_Z()
  1021. " Ctrl-Z when insertmode is not set inserts it literally
  1022. new
  1023. call setline(1, 'abc')
  1024. call feedkeys("A\<c-z>\<esc>", 'tnix')
  1025. call assert_equal(["abc\<c-z>"], getline(1,'$'))
  1026. bw!
  1027. " TODO: How to Test Ctrl-Z in insert mode, e.g. suspend?
  1028. endfunc
  1029. func Test_edit_DROP()
  1030. if !has("dnd")
  1031. return
  1032. endif
  1033. new
  1034. call setline(1, ['abc def ghi'])
  1035. call cursor(1, 1)
  1036. try
  1037. call feedkeys("i\<Drop>\<Esc>", 'tnix')
  1038. call assert_fails(1, 'Invalid register name')
  1039. catch /^Vim\%((\a\+)\)\=:E353/
  1040. call assert_true(1, 'error caught')
  1041. endtry
  1042. bw!
  1043. endfunc
  1044. func Test_edit_CTRL_V()
  1045. new
  1046. call setline(1, ['abc'])
  1047. call cursor(2, 1)
  1048. " force some redraws
  1049. set showmode showcmd
  1050. " call test_override('char_avail', 1)
  1051. call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
  1052. call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
  1053. if has("rightleft") && exists("+rl")
  1054. set rl
  1055. call setline(1, ['abc'])
  1056. call cursor(2, 1)
  1057. call feedkeys("A\<c-v>\<c-n>\<c-v>\<c-l>\<c-v>\<c-b>\<esc>", 'tnix')
  1058. call assert_equal(["abc\x0e\x0c\x02"], getline(1, '$'))
  1059. set norl
  1060. endif
  1061. set noshowmode showcmd
  1062. " call test_override('char_avail', 0)
  1063. " No modifiers should be applied to the char typed using i_CTRL-V_digit.
  1064. call feedkeys(":append\<CR>\<C-V>76c\<C-V>76\<C-F2>\<C-V>u3c0j\<C-V>u3c0\<M-F3>\<CR>.\<CR>", 'tnix')
  1065. call assert_equal('LcL<C-F2>πjπ<M-F3>', getline(2))
  1066. if has('osx')
  1067. " A char with a modifier should not be a valid char for i_CTRL-V_digit.
  1068. call feedkeys("o\<C-V>\<D-j>\<C-V>\<D-1>\<C-V>\<D-o>\<C-V>\<D-x>\<C-V>\<D-u>", 'tnix')
  1069. call assert_equal('<D-j><D-1><D-o><D-x><D-u>', getline(3))
  1070. endif
  1071. bw!
  1072. endfunc
  1073. func Test_edit_F1()
  1074. CheckFeature quickfix
  1075. " Pressing <f1>
  1076. new
  1077. " call feedkeys(":set im\<cr>\<f1>\<c-l>", 'tnix')
  1078. call feedkeys("i\<f1>\<esc>", 'tnix')
  1079. set noinsertmode
  1080. call assert_equal('help', &buftype)
  1081. bw
  1082. bw
  1083. endfunc
  1084. func Test_edit_F21()
  1085. " Pressing <f21>
  1086. " sends a netbeans command
  1087. if has("netbeans_intg")
  1088. new
  1089. " I have no idea what this is supposed to do :)
  1090. call feedkeys("A\<F21>\<F1>\<esc>", 'tnix')
  1091. bw
  1092. endif
  1093. endfunc
  1094. func Test_edit_HOME_END()
  1095. " Test Home/End Keys
  1096. new
  1097. set foldopen+=hor
  1098. call setline(1, ['abc', 'def'])
  1099. call cursor(1, 1)
  1100. call feedkeys("AX\<Home>Y\<esc>", 'tnix')
  1101. call cursor(2, 1)
  1102. call feedkeys("iZ\<End>Y\<esc>", 'tnix')
  1103. call assert_equal(['YabcX', 'ZdefY'], getline(1, '$'))
  1104. set foldopen-=hor
  1105. bw!
  1106. endfunc
  1107. func Test_edit_INS()
  1108. " Test for Pressing <Insert>
  1109. new
  1110. call setline(1, ['abc', 'def'])
  1111. call cursor(1, 1)
  1112. call feedkeys("i\<Insert>ZYX>", 'tnix')
  1113. call assert_equal(['ZYX>', 'def'], getline(1, '$'))
  1114. call setline(1, ['abc', 'def'])
  1115. call cursor(1, 1)
  1116. call feedkeys("i\<Insert>Z\<Insert>YX>", 'tnix')
  1117. call assert_equal(['ZYX>bc', 'def'], getline(1, '$'))
  1118. bw!
  1119. endfunc
  1120. func Test_edit_LEFT_RIGHT()
  1121. " Left, Shift-Left, Right, Shift-Right
  1122. new
  1123. call setline(1, ['abc def ghi', 'ABC DEF GHI', 'ZZZ YYY XXX'])
  1124. let _ww=&ww
  1125. set ww=
  1126. call cursor(2, 1)
  1127. call feedkeys("i\<left>\<esc>", 'tnix')
  1128. call assert_equal([0, 2, 1, 0], getpos('.'))
  1129. " Is this a bug, <s-left> does not respect whichwrap option
  1130. call feedkeys("i\<s-left>\<esc>", 'tnix')
  1131. call assert_equal([0, 1, 8, 0], getpos('.'))
  1132. call feedkeys("i". repeat("\<s-left>", 3). "\<esc>", 'tnix')
  1133. call assert_equal([0, 1, 1, 0], getpos('.'))
  1134. call feedkeys("i\<right>\<esc>", 'tnix')
  1135. call assert_equal([0, 1, 1, 0], getpos('.'))
  1136. call feedkeys("i\<right>\<right>\<esc>", 'tnix')
  1137. call assert_equal([0, 1, 2, 0], getpos('.'))
  1138. call feedkeys("A\<right>\<esc>", 'tnix')
  1139. call assert_equal([0, 1, 11, 0], getpos('.'))
  1140. call feedkeys("A\<s-right>\<esc>", 'tnix')
  1141. call assert_equal([0, 2, 1, 0], getpos('.'))
  1142. call feedkeys("i\<s-right>\<esc>", 'tnix')
  1143. call assert_equal([0, 2, 4, 0], getpos('.'))
  1144. call cursor(3, 11)
  1145. call feedkeys("A\<right>\<esc>", 'tnix')
  1146. call feedkeys("A\<s-right>\<esc>", 'tnix')
  1147. call assert_equal([0, 3, 11, 0], getpos('.'))
  1148. call cursor(2, 11)
  1149. " <S-Right> does not respect 'whichwrap' option
  1150. call feedkeys("A\<s-right>\<esc>", 'tnix')
  1151. call assert_equal([0, 3, 1, 0], getpos('.'))
  1152. " Check motion when 'whichwrap' contains cursor keys for insert mode
  1153. set ww+=[,]
  1154. call cursor(2, 1)
  1155. call feedkeys("i\<left>\<esc>", 'tnix')
  1156. call assert_equal([0, 1, 11, 0], getpos('.'))
  1157. call cursor(2, 11)
  1158. call feedkeys("A\<right>\<esc>", 'tnix')
  1159. call assert_equal([0, 3, 1, 0], getpos('.'))
  1160. call cursor(2, 11)
  1161. call feedkeys("A\<s-right>\<esc>", 'tnix')
  1162. call assert_equal([0, 3, 1, 0], getpos('.'))
  1163. let &ww = _ww
  1164. bw!
  1165. endfunc
  1166. func Test_edit_MOUSE()
  1167. " This is a simple test, since we're not really using the mouse here
  1168. CheckFeature mouse
  1169. 10new
  1170. call setline(1, range(1, 100))
  1171. call cursor(1, 1)
  1172. call assert_equal(1, line('w0'))
  1173. call assert_equal(10, line('w$'))
  1174. set mouse=a
  1175. " One scroll event moves three lines.
  1176. call feedkeys("A\<ScrollWheelDown>\<esc>", 'tnix')
  1177. call assert_equal(4, line('w0'))
  1178. call assert_equal(13, line('w$'))
  1179. " This should move by one page down.
  1180. call feedkeys("A\<S-ScrollWheelDown>\<esc>", 'tnix')
  1181. call assert_equal(14, line('w0'))
  1182. set nostartofline
  1183. " Another page down.
  1184. call feedkeys("A\<C-ScrollWheelDown>\<esc>", 'tnix')
  1185. call assert_equal(24, line('w0'))
  1186. call assert_equal([0, 24, 2, 0], getpos('.'))
  1187. call Ntest_setmouse(4, 3)
  1188. call feedkeys("A\<LeftMouse>\<esc>", 'tnix')
  1189. call assert_equal([0, 27, 2, 0], getpos('.'))
  1190. set mousemodel=extend
  1191. call Ntest_setmouse(5, 3)
  1192. call feedkeys("A\<RightMouse>\<esc>\<esc>", 'tnix')
  1193. call assert_equal([0, 28, 2, 0], getpos('.'))
  1194. set mousemodel&
  1195. call cursor(1, 100)
  1196. norm! zt
  1197. " this should move by a screen up, but when the test
  1198. " is run, it moves up to the top of the buffer...
  1199. call feedkeys("A\<ScrollWheelUp>\<esc>", 'tnix')
  1200. call assert_equal([0, 1, 1, 0], getpos('.'))
  1201. call cursor(1, 30)
  1202. norm! zt
  1203. call feedkeys("A\<S-ScrollWheelUp>\<esc>", 'tnix')
  1204. call assert_equal([0, 1, 1, 0], getpos('.'))
  1205. call cursor(1, 30)
  1206. norm! zt
  1207. call feedkeys("A\<C-ScrollWheelUp>\<esc>", 'tnix')
  1208. call assert_equal([0, 1, 1, 0], getpos('.'))
  1209. %d
  1210. call setline(1, repeat(["12345678901234567890"], 100))
  1211. call cursor(2, 1)
  1212. call feedkeys("A\<ScrollWheelRight>\<esc>", 'tnix')
  1213. call assert_equal([0, 2, 20, 0], getpos('.'))
  1214. call feedkeys("A\<ScrollWheelLeft>\<esc>", 'tnix')
  1215. call assert_equal([0, 2, 20, 0], getpos('.'))
  1216. call feedkeys("A\<S-ScrollWheelRight>\<esc>", 'tnix')
  1217. call assert_equal([0, 2, 20, 0], getpos('.'))
  1218. call feedkeys("A\<S-ScrollWheelLeft>\<esc>", 'tnix')
  1219. call assert_equal([0, 2, 20, 0], getpos('.'))
  1220. call feedkeys("A\<C-ScrollWheelRight>\<esc>", 'tnix')
  1221. call assert_equal([0, 2, 20, 0], getpos('.'))
  1222. call feedkeys("A\<C-ScrollWheelLeft>\<esc>", 'tnix')
  1223. call assert_equal([0, 2, 20, 0], getpos('.'))
  1224. set mouse& startofline
  1225. bw!
  1226. endfunc
  1227. func Test_edit_PAGEUP_PAGEDOWN()
  1228. 10new
  1229. call setline(1, repeat(['abc def ghi'], 30))
  1230. call cursor(1, 1)
  1231. call feedkeys("i\<PageDown>\<esc>", 'tnix')
  1232. call assert_equal([0, 9, 1, 0], getpos('.'))
  1233. call feedkeys("i\<PageDown>\<esc>", 'tnix')
  1234. call assert_equal([0, 17, 1, 0], getpos('.'))
  1235. call feedkeys("i\<PageDown>\<esc>", 'tnix')
  1236. call assert_equal([0, 25, 1, 0], getpos('.'))
  1237. call feedkeys("i\<PageDown>\<esc>", 'tnix')
  1238. call assert_equal([0, 30, 1, 0], getpos('.'))
  1239. call feedkeys("i\<PageDown>\<esc>", 'tnix')
  1240. call assert_equal([0, 30, 1, 0], getpos('.'))
  1241. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1242. call assert_equal([0, 29, 1, 0], getpos('.'))
  1243. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1244. call assert_equal([0, 21, 1, 0], getpos('.'))
  1245. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1246. call assert_equal([0, 13, 1, 0], getpos('.'))
  1247. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1248. call assert_equal([0, 10, 1, 0], getpos('.'))
  1249. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1250. call assert_equal([0, 10, 11, 0], getpos('.'))
  1251. " <S-Up> is the same as <PageUp>
  1252. " <S-Down> is the same as <PageDown>
  1253. call cursor(1, 1)
  1254. call feedkeys("i\<S-Down>\<esc>", 'tnix')
  1255. call assert_equal([0, 9, 1, 0], getpos('.'))
  1256. call feedkeys("i\<S-Down>\<esc>", 'tnix')
  1257. call assert_equal([0, 17, 1, 0], getpos('.'))
  1258. call feedkeys("i\<S-Down>\<esc>", 'tnix')
  1259. call assert_equal([0, 25, 1, 0], getpos('.'))
  1260. call feedkeys("i\<S-Down>\<esc>", 'tnix')
  1261. call assert_equal([0, 30, 1, 0], getpos('.'))
  1262. call feedkeys("i\<S-Down>\<esc>", 'tnix')
  1263. call assert_equal([0, 30, 1, 0], getpos('.'))
  1264. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1265. call assert_equal([0, 29, 1, 0], getpos('.'))
  1266. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1267. call assert_equal([0, 21, 1, 0], getpos('.'))
  1268. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1269. call assert_equal([0, 13, 1, 0], getpos('.'))
  1270. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1271. call assert_equal([0, 10, 1, 0], getpos('.'))
  1272. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1273. call assert_equal([0, 10, 11, 0], getpos('.'))
  1274. set nostartofline
  1275. call cursor(30, 11)
  1276. norm! zt
  1277. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1278. call assert_equal([0, 29, 11, 0], getpos('.'))
  1279. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1280. call assert_equal([0, 21, 11, 0], getpos('.'))
  1281. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1282. call assert_equal([0, 13, 11, 0], getpos('.'))
  1283. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1284. call assert_equal([0, 10, 11, 0], getpos('.'))
  1285. call feedkeys("A\<PageUp>\<esc>", 'tnix')
  1286. call assert_equal([0, 10, 11, 0], getpos('.'))
  1287. call cursor(1, 1)
  1288. call feedkeys("A\<PageDown>\<esc>", 'tnix')
  1289. call assert_equal([0, 9, 11, 0], getpos('.'))
  1290. call feedkeys("A\<PageDown>\<esc>", 'tnix')
  1291. call assert_equal([0, 17, 11, 0], getpos('.'))
  1292. call feedkeys("A\<PageDown>\<esc>", 'tnix')
  1293. call assert_equal([0, 25, 11, 0], getpos('.'))
  1294. call feedkeys("A\<PageDown>\<esc>", 'tnix')
  1295. call assert_equal([0, 30, 11, 0], getpos('.'))
  1296. call feedkeys("A\<PageDown>\<esc>", 'tnix')
  1297. call assert_equal([0, 30, 11, 0], getpos('.'))
  1298. " <S-Up> is the same as <PageUp>
  1299. " <S-Down> is the same as <PageDown>
  1300. call cursor(30, 11)
  1301. norm! zt
  1302. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1303. call assert_equal([0, 29, 11, 0], getpos('.'))
  1304. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1305. call assert_equal([0, 21, 11, 0], getpos('.'))
  1306. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1307. call assert_equal([0, 13, 11, 0], getpos('.'))
  1308. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1309. call assert_equal([0, 10, 11, 0], getpos('.'))
  1310. call feedkeys("A\<S-Up>\<esc>", 'tnix')
  1311. call assert_equal([0, 10, 11, 0], getpos('.'))
  1312. call cursor(1, 1)
  1313. call feedkeys("A\<S-Down>\<esc>", 'tnix')
  1314. call assert_equal([0, 9, 11, 0], getpos('.'))
  1315. call feedkeys("A\<S-Down>\<esc>", 'tnix')
  1316. call assert_equal([0, 17, 11, 0], getpos('.'))
  1317. call feedkeys("A\<S-Down>\<esc>", 'tnix')
  1318. call assert_equal([0, 25, 11, 0], getpos('.'))
  1319. call feedkeys("A\<S-Down>\<esc>", 'tnix')
  1320. call assert_equal([0, 30, 11, 0], getpos('.'))
  1321. call feedkeys("A\<S-Down>\<esc>", 'tnix')
  1322. call assert_equal([0, 30, 11, 0], getpos('.'))
  1323. bw!
  1324. endfunc
  1325. func Test_edit_forbidden()
  1326. new
  1327. " 1) edit in the sandbox is not allowed
  1328. call setline(1, 'a')
  1329. com! Sandbox :sandbox call feedkeys("i\<del>\<esc>", 'tnix')
  1330. call assert_fails(':Sandbox', 'E48:')
  1331. com! Sandbox :sandbox exe "norm! i\<del>"
  1332. call assert_fails(':Sandbox', 'E48:')
  1333. delcom Sandbox
  1334. call assert_equal(['a'], getline(1,'$'))
  1335. " 2) edit with textlock set
  1336. fu! DoIt()
  1337. call feedkeys("i\<del>\<esc>", 'tnix')
  1338. endfu
  1339. au InsertCharPre <buffer> :call DoIt()
  1340. try
  1341. call feedkeys("ix\<esc>", 'tnix')
  1342. call assert_fails(1, 'textlock')
  1343. catch /^Vim\%((\a\+)\)\=:E565/ " catch E565: not allowed here
  1344. endtry
  1345. " TODO: Might be a bug: should x really be inserted here
  1346. call assert_equal(['xa'], getline(1, '$'))
  1347. delfu DoIt
  1348. try
  1349. call feedkeys("ix\<esc>", 'tnix')
  1350. call assert_fails(1, 'unknown function')
  1351. catch /^Vim\%((\a\+)\)\=:E117/ " catch E117: unknown function
  1352. endtry
  1353. au! InsertCharPre
  1354. " 3) edit when completion is shown
  1355. fun! Complete(findstart, base)
  1356. if a:findstart
  1357. return col('.')
  1358. else
  1359. call feedkeys("i\<del>\<esc>", 'tnix')
  1360. return []
  1361. endif
  1362. endfun
  1363. set completefunc=Complete
  1364. try
  1365. call feedkeys("i\<c-x>\<c-u>\<esc>", 'tnix')
  1366. call assert_fails(1, 'change in complete function')
  1367. catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
  1368. endtry
  1369. delfu Complete
  1370. set completefunc=
  1371. if has("rightleft") && exists("+fkmap")
  1372. " 4) 'R' when 'fkmap' and 'revins' is set.
  1373. set revins fkmap
  1374. try
  1375. normal Ri
  1376. call assert_fails(1, "R with 'fkmap' and 'ri' set")
  1377. catch
  1378. finally
  1379. set norevins nofkmap
  1380. endtry
  1381. endif
  1382. bw!
  1383. endfunc
  1384. func Test_edit_rightleft()
  1385. " Cursor in rightleft mode moves differently
  1386. CheckFeature rightleft
  1387. call NewWindow(10, 20)
  1388. call setline(1, ['abc', 'def', 'ghi'])
  1389. call cursor(1, 2)
  1390. set rightleft
  1391. " Screen looks as expected
  1392. let lines = ScreenLines([1, 4], winwidth(0))
  1393. let expect = [
  1394. \" cba",
  1395. \" fed",
  1396. \" ihg",
  1397. \" ~"]
  1398. call assert_equal(join(expect, "\n"), join(lines, "\n"))
  1399. " 2) right moves to the left
  1400. call feedkeys("i\<right>\<esc>x", 'txin')
  1401. call assert_equal(['bc', 'def', 'ghi'], getline(1,'$'))
  1402. call cursor(1, 2)
  1403. call feedkeys("i\<s-right>\<esc>", 'txin')
  1404. call cursor(1, 2)
  1405. call feedkeys("i\<c-right>\<esc>", 'txin')
  1406. " Screen looks as expected
  1407. let lines = ScreenLines([1, 4], winwidth(0))
  1408. let expect = [
  1409. \" cb",
  1410. \" fed",
  1411. \" ihg",
  1412. \" ~"]
  1413. call assert_equal(join(expect, "\n"), join(lines, "\n"))
  1414. " 2) left moves to the right
  1415. call setline(1, ['abc', 'def', 'ghi'])
  1416. call cursor(1, 2)
  1417. call feedkeys("i\<left>\<esc>x", 'txin')
  1418. call assert_equal(['ac', 'def', 'ghi'], getline(1,'$'))
  1419. call cursor(1, 2)
  1420. call feedkeys("i\<s-left>\<esc>", 'txin')
  1421. call cursor(1, 2)
  1422. call feedkeys("i\<c-left>\<esc>", 'txin')
  1423. " Screen looks as expected
  1424. let lines = ScreenLines([1, 4], winwidth(0))
  1425. let expect = [
  1426. \" ca",
  1427. \" fed",
  1428. \" ihg",
  1429. \" ~"]
  1430. call assert_equal(join(expect, "\n"), join(lines, "\n"))
  1431. %d _
  1432. " call test_override('redraw_flag', 1)
  1433. " call test_override('char_avail', 1)
  1434. call feedkeys("a\<C-V>x41", "xt")
  1435. redraw!
  1436. call assert_equal(repeat(' ', 19) .. 'A', Screenline(1))
  1437. " call test_override('ALL', 0)
  1438. set norightleft
  1439. bw!
  1440. endfunc
  1441. func Test_edit_complete_very_long_name()
  1442. " Long directory names only work on Unix.
  1443. CheckUnix
  1444. let dirname = getcwd() . "/Xlongdir"
  1445. let longdirname = dirname . repeat('/' . repeat('d', 255), 4)
  1446. try
  1447. call mkdir(longdirname, 'p')
  1448. catch /E739:/
  1449. " Long directory name probably not supported.
  1450. call delete(dirname, 'rf')
  1451. return
  1452. endtry
  1453. " Try to get the Vim window position before setting 'columns', so that we can
  1454. " move the window back to where it was.
  1455. let winposx = getwinposx()
  1456. let winposy = getwinposy()
  1457. if winposx >= 0 && winposy >= 0 && !has('gui_running')
  1458. " We did get the window position, but xterm may report the wrong numbers.
  1459. " Move the window to the reported position and compute any offset.
  1460. exe 'winpos ' . winposx . ' ' . winposy
  1461. sleep 100m
  1462. let x = getwinposx()
  1463. if x >= 0
  1464. let winposx += winposx - x
  1465. endif
  1466. let y = getwinposy()
  1467. if y >= 0
  1468. let winposy += winposy - y
  1469. endif
  1470. endif
  1471. let save_columns = &columns
  1472. " Need at least about 1100 columns to reproduce the problem.
  1473. set columns=2000
  1474. set noswapfile
  1475. let longfilename = longdirname . '/' . repeat('a', 255)
  1476. call writefile(['Totum', 'Table'], longfilename)
  1477. new
  1478. exe "next Xnofile " . longfilename
  1479. exe "normal iT\<C-N>"
  1480. bwipe!
  1481. exe 'bwipe! ' . longfilename
  1482. call delete(dirname, 'rf')
  1483. let &columns = save_columns
  1484. if winposx >= 0 && winposy >= 0
  1485. exe 'winpos ' . winposx . ' ' . winposy
  1486. endif
  1487. set swapfile&
  1488. endfunc
  1489. func Test_edit_backtick()
  1490. next a\`b c
  1491. call assert_equal('a`b', expand('%'))
  1492. next
  1493. call assert_equal('c', expand('%'))
  1494. call assert_equal('a\`b c', expand('##'))
  1495. endfunc
  1496. func Test_edit_quit()
  1497. edit foo.txt
  1498. split
  1499. new
  1500. call setline(1, 'hello')
  1501. 3wincmd w
  1502. redraw!
  1503. call assert_fails('1q', 'E37:')
  1504. bwipe! foo.txt
  1505. only
  1506. endfunc
  1507. func Test_edit_alt()
  1508. " Keeping the cursor line didn't happen when the first line has indent.
  1509. new
  1510. call setline(1, [' one', 'two', 'three'])
  1511. w XAltFile
  1512. $
  1513. call assert_equal(3, line('.'))
  1514. e Xother
  1515. e #
  1516. call assert_equal(3, line('.'))
  1517. bwipe XAltFile
  1518. call delete('XAltFile')
  1519. endfunc
  1520. func Test_edit_InsertLeave()
  1521. new
  1522. au InsertLeavePre * let g:did_au_pre = 1
  1523. au InsertLeave * let g:did_au = 1
  1524. let g:did_au_pre = 0
  1525. let g:did_au = 0
  1526. call feedkeys("afoo\<Esc>", 'tx')
  1527. call assert_equal(1, g:did_au_pre)
  1528. call assert_equal(1, g:did_au)
  1529. call assert_equal('foo', getline(1))
  1530. let g:did_au_pre = 0
  1531. let g:did_au = 0
  1532. call feedkeys("Sbar\<C-C>", 'tx')
  1533. call assert_equal(1, g:did_au_pre)
  1534. call assert_equal(0, g:did_au)
  1535. call assert_equal('bar', getline(1))
  1536. inoremap x xx<Esc>
  1537. let g:did_au_pre = 0
  1538. let g:did_au = 0
  1539. call feedkeys("Saax", 'tx')
  1540. call assert_equal(1, g:did_au_pre)
  1541. call assert_equal(1, g:did_au)
  1542. call assert_equal('aaxx', getline(1))
  1543. inoremap x xx<C-C>
  1544. let g:did_au_pre = 0
  1545. let g:did_au = 0
  1546. call feedkeys("Sbbx", 'tx')
  1547. call assert_equal(1, g:did_au_pre)
  1548. call assert_equal(0, g:did_au)
  1549. call assert_equal('bbxx', getline(1))
  1550. bwipe!
  1551. au! InsertLeave InsertLeavePre
  1552. iunmap x
  1553. endfunc
  1554. func Test_edit_InsertLeave_undo()
  1555. new XtestUndo
  1556. set undofile
  1557. au InsertLeave * wall
  1558. exe "normal ofoo\<Esc>"
  1559. call assert_equal(2, line('$'))
  1560. normal u
  1561. call assert_equal(1, line('$'))
  1562. bwipe!
  1563. au! InsertLeave
  1564. call delete('XtestUndo')
  1565. call delete(undofile('XtestUndo'))
  1566. set undofile&
  1567. endfunc
  1568. " Test for inserting characters using CTRL-V followed by a number.
  1569. func Test_edit_special_chars()
  1570. new
  1571. let t = "o\<C-V>65\<C-V>x42\<C-V>o103 \<C-V>33a\<C-V>xfg\<C-V>o78\<Esc>"
  1572. exe "normal " . t
  1573. call assert_equal("ABC !a\<C-O>g\<C-G>8", getline(2))
  1574. close!
  1575. endfunc
  1576. func Test_edit_startinsert()
  1577. new
  1578. set backspace+=start
  1579. call setline(1, 'foobar')
  1580. call feedkeys("A\<C-U>\<Esc>", 'xt')
  1581. call assert_equal('', getline(1))
  1582. call setline(1, 'foobar')
  1583. call feedkeys(":startinsert!\<CR>\<C-U>\<Esc>", 'xt')
  1584. call assert_equal('', getline(1))
  1585. set backspace&
  1586. bwipe!
  1587. endfunc
  1588. " Test for :startreplace and :startgreplace
  1589. func Test_edit_startreplace()
  1590. new
  1591. call setline(1, 'abc')
  1592. call feedkeys("l:startreplace\<CR>xyz\e", 'xt')
  1593. call assert_equal('axyz', getline(1))
  1594. call feedkeys("0:startreplace!\<CR>abc\e", 'xt')
  1595. call assert_equal('axyzabc', getline(1))
  1596. call setline(1, "a\tb")
  1597. call feedkeys("0l:startgreplace\<CR>xyz\e", 'xt')
  1598. call assert_equal("axyz\tb", getline(1))
  1599. call feedkeys("0i\<C-R>=execute('startreplace')\<CR>12\e", 'xt')
  1600. call assert_equal("12axyz\tb", getline(1))
  1601. close!
  1602. endfunc
  1603. func Test_edit_noesckeys()
  1604. CheckNotGui
  1605. new
  1606. " <Left> moves cursor when 'esckeys' is set
  1607. exe "set t_kl=\<Esc>OD"
  1608. " set esckeys
  1609. call feedkeys("axyz\<Esc>ODX", "xt")
  1610. " call assert_equal("xyXz", getline(1))
  1611. " <Left> exits Insert mode when 'esckeys' is off
  1612. " set noesckeys
  1613. call setline(1, '')
  1614. call feedkeys("axyz\<Esc>ODX", "xt")
  1615. call assert_equal(["DX", "xyz"], getline(1, 2))
  1616. bwipe!
  1617. " set esckeys
  1618. endfunc
  1619. " Test for running an invalid ex command in insert mode using CTRL-O
  1620. func Test_edit_ctrl_o_invalid_cmd()
  1621. new
  1622. set showmode showcmd
  1623. " Avoid a sleep of 3 seconds. Zero might have side effects.
  1624. " call test_override('ui_delay', 50)
  1625. let caught_e492 = 0
  1626. try
  1627. call feedkeys("i\<C-O>:invalid\<CR>abc\<Esc>", "xt")
  1628. catch /E492:/
  1629. let caught_e492 = 1
  1630. endtry
  1631. call assert_equal(1, caught_e492)
  1632. call assert_equal('abc', getline(1))
  1633. set showmode& showcmd&
  1634. " call test_override('ui_delay', 0)
  1635. close!
  1636. endfunc
  1637. " Test for editing a file with a very long name
  1638. func Test_edit_illegal_filename()
  1639. CheckEnglish
  1640. new
  1641. redir => msg
  1642. exe 'edit ' . repeat('f', 5000)
  1643. redir END
  1644. call assert_match("Illegal file name$", split(msg, "\n")[0])
  1645. close!
  1646. endfunc
  1647. " Test for editing a directory
  1648. func Test_edit_is_a_directory()
  1649. CheckEnglish
  1650. let dirname = getcwd() . "/Xeditdir"
  1651. call mkdir(dirname, 'p')
  1652. new
  1653. redir => msg
  1654. exe 'edit' dirname
  1655. redir END
  1656. call assert_match("is a directory$", split(msg, "\n")[0])
  1657. bwipe!
  1658. let dirname .= '/'
  1659. new
  1660. redir => msg
  1661. exe 'edit' dirname
  1662. redir END
  1663. call assert_match("is a directory$", split(msg, "\n")[0])
  1664. bwipe!
  1665. call delete(dirname, 'rf')
  1666. endfunc
  1667. " Test for editing a file using invalid file encoding
  1668. func Test_edit_invalid_encoding()
  1669. CheckEnglish
  1670. call writefile([], 'Xinvfile')
  1671. redir => msg
  1672. new ++enc=axbyc Xinvfile
  1673. redir END
  1674. call assert_match('\[NOT converted\]', msg)
  1675. call delete('Xinvfile')
  1676. close!
  1677. endfunc
  1678. " Test for the "charconvert" option
  1679. func Test_edit_charconvert()
  1680. CheckEnglish
  1681. call writefile(['one', 'two'], 'Xccfile')
  1682. " set 'charconvert' to a non-existing function
  1683. set charconvert=NonExitingFunc()
  1684. new
  1685. let caught_e117 = v:false
  1686. try
  1687. redir => msg
  1688. edit ++enc=axbyc Xccfile
  1689. catch /E117:/
  1690. let caught_e117 = v:true
  1691. finally
  1692. redir END
  1693. endtry
  1694. call assert_true(caught_e117)
  1695. call assert_equal(['one', 'two'], getline(1, '$'))
  1696. call assert_match("Conversion with 'charconvert' failed", msg)
  1697. close!
  1698. set charconvert&
  1699. " 'charconvert' function doesn't create an output file
  1700. func Cconv1()
  1701. endfunc
  1702. set charconvert=Cconv1()
  1703. new
  1704. redir => msg
  1705. edit ++enc=axbyc Xccfile
  1706. redir END
  1707. call assert_equal(['one', 'two'], getline(1, '$'))
  1708. call assert_match("can't read output of 'charconvert'", msg)
  1709. close!
  1710. delfunc Cconv1
  1711. set charconvert&
  1712. " 'charconvert' function to convert to upper case
  1713. func Cconv2()
  1714. let data = readfile(v:fname_in)
  1715. call map(data, 'toupper(v:val)')
  1716. call writefile(data, v:fname_out)
  1717. endfunc
  1718. set charconvert=Cconv2()
  1719. new Xccfile
  1720. write ++enc=ucase Xccfile1
  1721. call assert_equal(['ONE', 'TWO'], readfile('Xccfile1'))
  1722. call delete('Xccfile1')
  1723. close!
  1724. delfunc Cconv2
  1725. set charconvert&
  1726. " 'charconvert' function removes the input file
  1727. func Cconv3()
  1728. call delete(v:fname_in)
  1729. endfunc
  1730. set charconvert=Cconv3()
  1731. new
  1732. call assert_fails('edit ++enc=lcase Xccfile', 'E202:')
  1733. call assert_equal([''], getline(1, '$'))
  1734. close!
  1735. delfunc Cconv3
  1736. set charconvert&
  1737. call delete('Xccfile')
  1738. endfunc
  1739. " Test for editing a file without read permission
  1740. func Test_edit_file_no_read_perm()
  1741. CheckUnix
  1742. CheckNotRoot
  1743. call writefile(['one', 'two'], 'Xnrpfile')
  1744. call setfperm('Xnrpfile', '-w-------')
  1745. new
  1746. redir => msg
  1747. edit Xnrpfile
  1748. redir END
  1749. call assert_equal(1, &readonly)
  1750. call assert_equal([''], getline(1, '$'))
  1751. call assert_match('\[Permission Denied\]', msg)
  1752. close!
  1753. call delete('Xnrpfile')
  1754. endfunc
  1755. " Using :edit without leaving 'insertmode' should not cause Insert mode to be
  1756. " re-entered immediately after <C-L>
  1757. func Test_edit_insertmode_ex_edit()
  1758. CheckRunVimInTerminal
  1759. let lines =<< trim END
  1760. set insertmode noruler
  1761. inoremap <C-B> <Cmd>edit Xfoo<CR>
  1762. END
  1763. call writefile(lines, 'Xtest_edit_insertmode_ex_edit')
  1764. let buf = RunVimInTerminal('-S Xtest_edit_insertmode_ex_edit', #{rows: 6})
  1765. " Somehow this can be very slow with valgrind. A separate TermWait() works
  1766. " better than a longer time with WaitForAssert() (why?)
  1767. call TermWait(buf, 1000)
  1768. call WaitForAssert({-> assert_match('^-- INSERT --\s*$', term_getline(buf, 6))})
  1769. call term_sendkeys(buf, "\<C-B>\<C-L>")
  1770. call WaitForAssert({-> assert_notmatch('^-- INSERT --\s*$', term_getline(buf, 6))})
  1771. " clean up
  1772. call StopVimInTerminal(buf)
  1773. call delete('Xtest_edit_insertmode_ex_edit')
  1774. endfunc
  1775. " Pressing escape in 'insertmode' should beep
  1776. " FIXME: Execute this later, when using valgrind it makes the next test
  1777. " Test_edit_insertmode_ex_edit() fail.
  1778. func Test_z_edit_insertmode_esc_beeps()
  1779. new
  1780. " set insertmode
  1781. " call assert_beeps("call feedkeys(\"one\<Esc>\", 'xt')")
  1782. set insertmode&
  1783. " unsupported "CTRL-G l" command should beep in insert mode.
  1784. call assert_beeps("normal i\<C-G>l")
  1785. bwipe!
  1786. endfunc
  1787. " Test for 'hkmap' and 'hkmapp'
  1788. func Test_edit_hkmap()
  1789. throw "Skipped: Nvim does not support 'hkmap'"
  1790. CheckFeature rightleft
  1791. if has('win32') && !has('gui')
  1792. " Test fails on the MS-Windows terminal version
  1793. return
  1794. endif
  1795. new
  1796. set revins hkmap
  1797. let str = 'abcdefghijklmnopqrstuvwxyz'
  1798. let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  1799. let str ..= '`/'',.;'
  1800. call feedkeys('i' .. str, 'xt')
  1801. let expected = "óõú,.;"
  1802. let expected ..= "ZYXWVUTSRQPONMLKJIHGFEDCBA"
  1803. let expected ..= "æèñ'äåàãø/ôíîöêìçïéòë÷âáðù"
  1804. call assert_equal(expected, getline(1))
  1805. %d
  1806. set revins hkmap hkmapp
  1807. let str = 'abcdefghijklmnopqrstuvwxyz'
  1808. let str ..= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  1809. call feedkeys('i' .. str, 'xt')
  1810. let expected = "õYXWVUTSRQóOïíLKJIHGFEDêBA"
  1811. let expected ..= "öòXùåèúæø'ôñðîì÷çéäâóǟãëáà"
  1812. call assert_equal(expected, getline(1))
  1813. set revins& hkmap& hkmapp&
  1814. close!
  1815. endfunc
  1816. " Test for 'allowrevins' and using CTRL-_ in insert mode
  1817. func Test_edit_allowrevins()
  1818. CheckFeature rightleft
  1819. new
  1820. set allowrevins
  1821. call feedkeys("iABC\<C-_>DEF\<C-_>GHI", 'xt')
  1822. call assert_equal('ABCFEDGHI', getline(1))
  1823. set allowrevins&
  1824. close!
  1825. endfunc
  1826. " Test for inserting a register in insert mode using CTRL-R
  1827. func Test_edit_insert_reg()
  1828. throw 'Skipped: use test/functional/legacy/edit_spec.lua'
  1829. new
  1830. let g:Line = ''
  1831. func SaveFirstLine()
  1832. let g:Line = Screenline(1)
  1833. return 'r'
  1834. endfunc
  1835. inoremap <expr> <buffer> <F2> SaveFirstLine()
  1836. call test_override('redraw_flag', 1)
  1837. call test_override('char_avail', 1)
  1838. let @r = 'sample'
  1839. call feedkeys("a\<C-R>=SaveFirstLine()\<CR>", "xt")
  1840. call assert_equal('"', g:Line)
  1841. " Test for inserting an null and an empty list
  1842. call feedkeys("a\<C-R>=test_null_list()\<CR>", "xt")
  1843. call feedkeys("a\<C-R>=[]\<CR>", "xt")
  1844. call assert_equal(['r'], getbufline('', 1, '$'))
  1845. call test_override('ALL', 0)
  1846. close!
  1847. endfunc
  1848. " Test for positioning cursor after CTRL-R expression failed
  1849. func Test_edit_ctrl_r_failed()
  1850. CheckRunVimInTerminal
  1851. let buf = RunVimInTerminal('', #{rows: 6, cols: 60})
  1852. " trying to insert a blob produces an error
  1853. call term_sendkeys(buf, "i\<C-R>=0z\<CR>")
  1854. " ending Insert mode should put the cursor back on the ':'
  1855. call term_sendkeys(buf, ":\<Esc>")
  1856. call VerifyScreenDump(buf, 'Test_edit_ctlr_r_failed_1', {})
  1857. call StopVimInTerminal(buf)
  1858. endfunc
  1859. " When a character is inserted at the last position of the last line in a
  1860. " window, the window contents should be scrolled one line up. If the top line
  1861. " is part of a fold, then the entire fold should be scrolled up.
  1862. func Test_edit_lastline_scroll()
  1863. new
  1864. let h = winheight(0)
  1865. let lines = ['one', 'two', 'three']
  1866. let lines += repeat(['vim'], h - 4)
  1867. call setline(1, lines)
  1868. call setline(h, repeat('x', winwidth(0) - 1))
  1869. call feedkeys("GAx", 'xt')
  1870. redraw!
  1871. call assert_equal(h - 1, winline())
  1872. call assert_equal(2, line('w0'))
  1873. " scroll with a fold
  1874. 1,2fold
  1875. normal gg
  1876. call setline(h + 1, repeat('x', winwidth(0) - 1))
  1877. call feedkeys("GAx", 'xt')
  1878. redraw!
  1879. call assert_equal(h - 1, winline())
  1880. call assert_equal(3, line('w0'))
  1881. close!
  1882. endfunc
  1883. func Test_edit_browse()
  1884. " in the GUI this opens a file picker, we only test the terminal behavior
  1885. CheckNotGui
  1886. " ":browse xxx" checks for the FileExplorer augroup and assumes editing "."
  1887. " works then.
  1888. augroup FileExplorer
  1889. au!
  1890. augroup END
  1891. " When the CASE_INSENSITIVE_FILENAME is defined this used to cause a crash.
  1892. browse enew
  1893. bwipe!
  1894. browse split
  1895. bwipe!
  1896. endfunc
  1897. func Test_read_invalid()
  1898. " set encoding=latin1
  1899. " This was not properly checking for going past the end.
  1900. call assert_fails('r`=', 'E484')
  1901. set encoding=utf-8
  1902. endfunc
  1903. " Test for the 'revins' option
  1904. func Test_edit_revins()
  1905. CheckFeature rightleft
  1906. new
  1907. set revins
  1908. exe "normal! ione\ttwo three"
  1909. call assert_equal("eerht owt\teno", getline(1))
  1910. call setline(1, "one\ttwo three")
  1911. normal! gg$bi a
  1912. call assert_equal("one\ttwo a three", getline(1))
  1913. exe "normal! $bi\<BS>\<BS>"
  1914. call assert_equal("one\ttwo a ree", getline(1))
  1915. exe "normal! 0wi\<C-W>"
  1916. call assert_equal("one\t a ree", getline(1))
  1917. exe "normal! 0wi\<C-U>"
  1918. call assert_equal("one\t ", getline(1))
  1919. " newline in insert mode starts at the end of the line
  1920. call setline(1, 'one two three')
  1921. exe "normal! wi\nfour"
  1922. call assert_equal(['one two three', 'ruof'], getline(1, '$'))
  1923. set backspace=indent,eol,start
  1924. exe "normal! ggA\<BS>:"
  1925. call assert_equal(['one two three:ruof'], getline(1, '$'))
  1926. set revins& backspace&
  1927. bw!
  1928. endfunc
  1929. " Test for getting the character of the line below after "p"
  1930. func Test_edit_put_CTRL_E()
  1931. " set encoding=latin1
  1932. new
  1933. let @" = ''
  1934. sil! norm orggRx
  1935. sil! norm pr
  1936. call assert_equal(['r', 'r'], getline(1, 2))
  1937. bwipe!
  1938. set encoding=utf-8
  1939. endfunc
  1940. " Test toggling of input method. See :help i_CTRL-^
  1941. func Test_edit_CTRL_hat()
  1942. CheckFeature xim
  1943. " FIXME: test fails with Motif GUI.
  1944. " test also fails when running in the GUI.
  1945. CheckFeature gui_gtk
  1946. CheckNotGui
  1947. new
  1948. call assert_equal(0, &iminsert)
  1949. call feedkeys("i\<C-^>", 'xt')
  1950. call assert_equal(2, &iminsert)
  1951. call feedkeys("i\<C-^>", 'xt')
  1952. call assert_equal(0, &iminsert)
  1953. bwipe!
  1954. endfunc
  1955. " Weird long file name was going over the end of NameBuff
  1956. func Test_edit_overlong_file_name()
  1957. CheckUnix
  1958. file 0000000000000000000000000000
  1959. file %%%%%%%%%%%%%%%%%%%%%%%%%%
  1960. file %%%%%%
  1961. set readonly
  1962. set ls=2
  1963. redraw!
  1964. set noreadonly ls&
  1965. bwipe!
  1966. endfunc
  1967. func Test_edit_Ctrl_RSB()
  1968. new
  1969. let g:triggered = []
  1970. autocmd InsertCharPre <buffer> let g:triggered += [v:char]
  1971. " i_CTRL-] should not trigger InsertCharPre
  1972. exe "normal! A\<C-]>"
  1973. call assert_equal([], g:triggered)
  1974. " i_CTRL-] should expand abbreviations but not trigger InsertCharPre
  1975. inoreabbr <buffer> f foo
  1976. exe "normal! Af\<C-]>a"
  1977. call assert_equal(['f', 'f', 'o', 'o', 'a'], g:triggered)
  1978. call assert_equal('fooa', getline(1))
  1979. " CTRL-] followed by i_CTRL-V should not expand abbreviations
  1980. " i_CTRL-V doesn't trigger InsertCharPre
  1981. call setline(1, '')
  1982. exe "normal! Af\<C-V>\<C-]>"
  1983. call assert_equal("f\<C-]>", getline(1))
  1984. let g:triggered = []
  1985. call setline(1, '')
  1986. " Also test assigning to v:char
  1987. autocmd InsertCharPre <buffer> let v:char = 'f'
  1988. exe "normal! Ag\<C-]>h"
  1989. call assert_equal(['g', 'f', 'o', 'o', 'h'], g:triggered)
  1990. call assert_equal('ffff', getline(1))
  1991. autocmd! InsertCharPre
  1992. unlet g:triggered
  1993. bwipe!
  1994. endfunc
  1995. func s:check_backspace(expected)
  1996. let g:actual = []
  1997. inoremap <buffer> <F2> <Cmd>let g:actual += [getline('.')]<CR>
  1998. set backspace=indent,eol,start
  1999. exe "normal i" .. repeat("\<BS>\<F2>", len(a:expected))
  2000. call assert_equal(a:expected, g:actual)
  2001. set backspace&
  2002. iunmap <buffer> <F2>
  2003. unlet g:actual
  2004. endfunc
  2005. " Test that backspace works with 'smarttab' and mixed Tabs and spaces.
  2006. func Test_edit_backspace_smarttab_mixed()
  2007. set smarttab
  2008. call NewWindow(1, 30)
  2009. setlocal tabstop=4 shiftwidth=4
  2010. call setline(1, "\t \t \t a")
  2011. normal! $
  2012. call s:check_backspace([
  2013. \ "\t \t \ta",
  2014. \ "\t \t a",
  2015. \ "\t \t a",
  2016. \ "\t \ta",
  2017. \ "\t a",
  2018. \ "\ta",
  2019. \ "a",
  2020. \ ])
  2021. call CloseWindow()
  2022. set smarttab&
  2023. endfunc
  2024. " Test that backspace works with 'smarttab' and 'varsofttabstop'.
  2025. func Test_edit_backspace_smarttab_varsofttabstop()
  2026. CheckFeature vartabs
  2027. set smarttab
  2028. call NewWindow(1, 30)
  2029. setlocal tabstop=8 varsofttabstop=6,2,5,3
  2030. call setline(1, "a\t \t a")
  2031. normal! $
  2032. call s:check_backspace([
  2033. \ "a\t \ta",
  2034. \ "a\t a",
  2035. \ "a\ta",
  2036. \ "a a",
  2037. \ "aa",
  2038. \ "a",
  2039. \ ])
  2040. call CloseWindow()
  2041. set smarttab&
  2042. endfunc
  2043. " Test that backspace works with 'smarttab' when a Tab is shown as "^I".
  2044. func Test_edit_backspace_smarttab_list()
  2045. set smarttab
  2046. call NewWindow(1, 30)
  2047. setlocal tabstop=4 shiftwidth=4 list listchars=
  2048. call setline(1, "\t \t \t a")
  2049. normal! $
  2050. call s:check_backspace([
  2051. \ "\t \t a",
  2052. \ "\t \t a",
  2053. \ "\t \ta",
  2054. \ "\t a",
  2055. \ "a",
  2056. \ ])
  2057. call CloseWindow()
  2058. set smarttab&
  2059. endfunc
  2060. " Test that backspace works with 'smarttab' and 'breakindent'.
  2061. func Test_edit_backspace_smarttab_breakindent()
  2062. CheckFeature linebreak
  2063. set smarttab
  2064. call NewWindow(3, 17)
  2065. setlocal tabstop=4 shiftwidth=4 breakindent breakindentopt=min:5
  2066. call setline(1, "\t \t \t a")
  2067. normal! $
  2068. call s:check_backspace([
  2069. \ "\t \t \ta",
  2070. \ "\t \t a",
  2071. \ "\t \t a",
  2072. \ "\t \ta",
  2073. \ "\t a",
  2074. \ "\ta",
  2075. \ "a",
  2076. \ ])
  2077. call CloseWindow()
  2078. set smarttab&
  2079. endfunc
  2080. " Test that backspace works with 'smarttab' and virtual text.
  2081. func Test_edit_backspace_smarttab_virtual_text()
  2082. CheckFeature textprop
  2083. set smarttab
  2084. call NewWindow(1, 50)
  2085. setlocal tabstop=4 shiftwidth=4
  2086. call setline(1, "\t \t \t a")
  2087. call prop_type_add('theprop', {})
  2088. call prop_add(1, 3, {'type': 'theprop', 'text': 'text'})
  2089. normal! $
  2090. call s:check_backspace([
  2091. \ "\t \t \ta",
  2092. \ "\t \t a",
  2093. \ "\t \t a",
  2094. \ "\t \ta",
  2095. \ "\t a",
  2096. \ "\ta",
  2097. \ "a",
  2098. \ ])
  2099. call CloseWindow()
  2100. call prop_type_delete('theprop')
  2101. set smarttab&
  2102. endfunc
  2103. " vim: shiftwidth=2 sts=2 expandtab