test_mapping.vim 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851
  1. " Tests for mappings and abbreviations
  2. source shared.vim
  3. source check.vim
  4. source screendump.vim
  5. source term_util.vim
  6. func Test_abbreviation()
  7. new
  8. " abbreviation with 0x80 should work
  9. inoreab чкпр vim
  10. call feedkeys("Goчкпр \<Esc>", "xt")
  11. call assert_equal('vim ', getline('$'))
  12. iunab чкпр
  13. bwipe!
  14. endfunc
  15. func Test_abbreviation_with_noremap()
  16. nnoremap <F2> :echo "cheese"
  17. cabbr cheese xxx
  18. call feedkeys(":echo \"cheese\"\<C-B>\"\<CR>", 'tx')
  19. call assert_equal('"echo "xxx"', @:)
  20. call feedkeys("\<F2>\<C-B>\"\<CR>", 'tx')
  21. call assert_equal('"echo "cheese"', @:)
  22. nnoremap <F2> :echo "cheese<C-]>"
  23. call feedkeys("\<F2>\<C-B>\"\<CR>", 'tx')
  24. call assert_equal('"echo "xxx"', @:)
  25. nunmap <F2>
  26. cunabbr cheese
  27. new
  28. inoremap <buffer> ( <C-]>()
  29. iabbr <buffer> fnu fun
  30. call feedkeys("ifnu(", 'tx')
  31. call assert_equal('fun()', getline(1))
  32. bwipe!
  33. endfunc
  34. func Test_abclear()
  35. abbrev foo foobar
  36. iabbrev fooi foobari
  37. cabbrev fooc foobarc
  38. call assert_equal("\n\n"
  39. \ .. "c fooc foobarc\n"
  40. \ .. "i fooi foobari\n"
  41. \ .. "! foo foobar", execute('abbrev'))
  42. iabclear
  43. call assert_equal("\n\n"
  44. \ .. "c fooc foobarc\n"
  45. \ .. "c foo foobar", execute('abbrev'))
  46. abbrev foo foobar
  47. iabbrev fooi foobari
  48. cabclear
  49. call assert_equal("\n\n"
  50. \ .. "i fooi foobari\n"
  51. \ .. "i foo foobar", execute('abbrev'))
  52. abbrev foo foobar
  53. cabbrev fooc foobarc
  54. abclear
  55. call assert_equal("\n\nNo abbreviation found", execute('abbrev'))
  56. call assert_fails('%abclear', 'E481:')
  57. endfunc
  58. func Test_abclear_buffer()
  59. abbrev foo foobar
  60. new X1
  61. abbrev <buffer> foo1 foobar1
  62. new X2
  63. abbrev <buffer> foo2 foobar2
  64. call assert_equal("\n\n"
  65. \ .. "! foo2 @foobar2\n"
  66. \ .. "! foo foobar", execute('abbrev'))
  67. abclear <buffer>
  68. call assert_equal("\n\n"
  69. \ .. "! foo foobar", execute('abbrev'))
  70. b X1
  71. call assert_equal("\n\n"
  72. \ .. "! foo1 @foobar1\n"
  73. \ .. "! foo foobar", execute('abbrev'))
  74. abclear <buffer>
  75. call assert_equal("\n\n"
  76. \ .. "! foo foobar", execute('abbrev'))
  77. abclear
  78. call assert_equal("\n\nNo abbreviation found", execute('abbrev'))
  79. %bwipe
  80. endfunc
  81. func Test_map_ctrl_c_insert()
  82. " mapping of ctrl-c in Insert mode
  83. set cpo-=< cpo-=k
  84. inoremap <c-c> <ctrl-c>
  85. cnoremap <c-c> dummy
  86. cunmap <c-c>
  87. call feedkeys("GoTEST2: CTRL-C |\<*C-C>A|\<Esc>", "xt")
  88. call assert_equal('TEST2: CTRL-C |<ctrl-c>A|', getline('$'))
  89. unmap! <c-c>
  90. set nomodified
  91. endfunc
  92. func Test_map_ctrl_c_visual()
  93. " mapping of ctrl-c in Visual mode
  94. vnoremap <c-c> :<C-u>$put ='vmap works'
  95. call feedkeys("GV\<*C-C>\<CR>", "xt")
  96. call assert_equal('vmap works', getline('$'))
  97. vunmap <c-c>
  98. set nomodified
  99. endfunc
  100. func Test_map_langmap()
  101. if !has('langmap')
  102. return
  103. endif
  104. " check langmap applies in normal mode
  105. set langmap=+- nolangremap
  106. new
  107. call setline(1, ['a', 'b', 'c'])
  108. 2
  109. call assert_equal('b', getline('.'))
  110. call feedkeys("+", "xt")
  111. call assert_equal('a', getline('.'))
  112. " check no remapping
  113. map x +
  114. 2
  115. call feedkeys("x", "xt")
  116. call assert_equal('c', getline('.'))
  117. " check with remapping
  118. set langremap
  119. 2
  120. call feedkeys("x", "xt")
  121. call assert_equal('a', getline('.'))
  122. unmap x
  123. bwipe!
  124. " 'langnoremap' follows 'langremap' and vice versa
  125. set langremap
  126. set langnoremap
  127. call assert_equal(0, &langremap)
  128. set langremap
  129. call assert_equal(0, &langnoremap)
  130. set nolangremap
  131. call assert_equal(1, &langnoremap)
  132. " check default values
  133. set langnoremap&
  134. call assert_equal(1, &langnoremap)
  135. call assert_equal(0, &langremap)
  136. set langremap&
  137. call assert_equal(1, &langnoremap)
  138. call assert_equal(0, &langremap)
  139. " langmap should not apply in insert mode, 'langremap' doesn't matter
  140. set langmap=+{ nolangremap
  141. call feedkeys("Go+\<Esc>", "xt")
  142. call assert_equal('+', getline('$'))
  143. set langmap=+{ langremap
  144. call feedkeys("Go+\<Esc>", "xt")
  145. call assert_equal('+', getline('$'))
  146. " langmap used for register name in insert mode.
  147. call setreg('a', 'aaaa')
  148. call setreg('b', 'bbbb')
  149. call setreg('c', 'cccc')
  150. set langmap=ab langremap
  151. call feedkeys("Go\<C-R>a\<Esc>", "xt")
  152. call assert_equal('bbbb', getline('$'))
  153. call feedkeys("Go\<C-R>\<C-R>a\<Esc>", "xt")
  154. call assert_equal('bbbb', getline('$'))
  155. " mapping does not apply
  156. imap c a
  157. call feedkeys("Go\<C-R>c\<Esc>", "xt")
  158. call assert_equal('cccc', getline('$'))
  159. imap a c
  160. call feedkeys("Go\<C-R>a\<Esc>", "xt")
  161. call assert_equal('bbbb', getline('$'))
  162. " langmap should not apply in Command-line mode
  163. set langmap=+{ nolangremap
  164. call feedkeys(":call append(line('$'), '+')\<CR>", "xt")
  165. call assert_equal('+', getline('$'))
  166. iunmap a
  167. iunmap c
  168. set nomodified
  169. endfunc
  170. func Test_map_feedkeys()
  171. " issue #212 (feedkeys insert mapping at current position)
  172. nnoremap . :call feedkeys(".", "in")<cr>
  173. call setline('$', ['a b c d', 'a b c d'])
  174. $-1
  175. call feedkeys("0qqdw.ifoo\<Esc>qj0@q\<Esc>", "xt")
  176. call assert_equal(['fooc d', 'fooc d'], getline(line('$') - 1, line('$')))
  177. nunmap .
  178. set nomodified
  179. endfunc
  180. func Test_map_cursor()
  181. " <c-g>U<cursor> works only within a single line
  182. imapclear
  183. imap ( ()<c-g>U<left>
  184. call feedkeys("G2o\<Esc>ki\<CR>Test1: text with a (here some more text\<Esc>k.", "xt")
  185. call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 2))
  186. call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 1))
  187. " test undo
  188. call feedkeys("G2o\<Esc>ki\<CR>Test2: text wit a (here some more text [und undo]\<C-G>u\<Esc>k.u", "xt")
  189. call assert_equal('', getline(line('$') - 2))
  190. call assert_equal('Test2: text wit a (here some more text [und undo])', getline(line('$') - 1))
  191. set nomodified
  192. imapclear
  193. endfunc
  194. func Test_map_cursor_ctrl_gU()
  195. " <c-g>U<cursor> works only within a single line
  196. nnoremap c<* *Ncgn<C-r>"<C-G>U<S-Left>
  197. call setline(1, ['foo', 'foobar', '', 'foo'])
  198. call cursor(1,2)
  199. call feedkeys("c<*PREFIX\<esc>.", 'xt')
  200. call assert_equal(['PREFIXfoo', 'foobar', '', 'PREFIXfoo'], getline(1,'$'))
  201. " break undo manually
  202. set ul=1000
  203. exe ":norm! uu"
  204. call assert_equal(['foo', 'foobar', '', 'foo'], getline(1,'$'))
  205. " Test that it does not work if the cursor moves to the previous line
  206. " 2 times <S-Left> move to the previous line
  207. nnoremap c<* *Ncgn<C-r>"<C-G>U<S-Left><C-G>U<S-Left>
  208. call setline(1, ['', ' foo', 'foobar', '', 'foo'])
  209. call cursor(2,3)
  210. call feedkeys("c<*PREFIX\<esc>.", 'xt')
  211. call assert_equal(['PREFIXPREFIX', ' foo', 'foobar', '', 'foo'], getline(1,'$'))
  212. nmapclear
  213. endfunc
  214. " This isn't actually testing a mapping, but similar use of CTRL-G U as above.
  215. func Test_break_undo()
  216. set whichwrap=<,>,[,]
  217. call feedkeys("G4o2k", "xt")
  218. exe ":norm! iTest3: text with a (parenthesis here\<C-G>U\<Right>new line here\<esc>\<up>\<up>."
  219. call assert_equal('new line here', getline(line('$') - 3))
  220. call assert_equal('Test3: text with a (parenthesis here', getline(line('$') - 2))
  221. call assert_equal('new line here', getline(line('$') - 1))
  222. set nomodified
  223. endfunc
  224. func Test_map_meta_quotes()
  225. imap <M-"> foo
  226. call feedkeys("Go-\<*M-\">-\<Esc>", "xt")
  227. call assert_equal("-foo-", getline('$'))
  228. set nomodified
  229. iunmap <M-">
  230. endfunc
  231. func Test_map_meta_multibyte()
  232. imap <M-á> foo
  233. call assert_match('i <M-á>\s*foo', execute('imap'))
  234. iunmap <M-á>
  235. endfunc
  236. func Test_map_super_quotes()
  237. if "\<D-j>"[-1:] == '>'
  238. throw 'Skipped: <D- modifier not supported'
  239. endif
  240. imap <D-"> foo
  241. call feedkeys("Go-\<*D-\">-\<Esc>", "xt")
  242. call assert_equal("-foo-", getline('$'))
  243. set nomodified
  244. iunmap <D-">
  245. endfunc
  246. func Test_map_super_multibyte()
  247. if "\<D-j>"[-1:] == '>'
  248. throw 'Skipped: <D- modifier not supported'
  249. endif
  250. imap <D-á> foo
  251. call assert_match('i <D-á>\s*foo', execute('imap'))
  252. iunmap <D-á>
  253. endfunc
  254. func Test_abbr_after_line_join()
  255. new
  256. abbr foo bar
  257. set backspace=indent,eol,start
  258. exe "normal o\<BS>foo "
  259. call assert_equal("bar ", getline(1))
  260. bwipe!
  261. unabbr foo
  262. set backspace&
  263. endfunc
  264. func Test_map_timeout()
  265. if !has('timers')
  266. return
  267. endif
  268. nnoremap aaaa :let got_aaaa = 1<CR>
  269. nnoremap bb :let got_bb = 1<CR>
  270. nmap b aaa
  271. new
  272. func ExitInsert(timer)
  273. let g:line = getline(1)
  274. call feedkeys("\<Esc>", "t")
  275. endfunc
  276. set timeout timeoutlen=200
  277. let timer = timer_start(300, 'ExitInsert')
  278. " After the 'b' Vim waits for another character to see if it matches 'bb'.
  279. " When it times out it is expanded to "aaa", but there is no wait for
  280. " "aaaa". Can't check that reliably though.
  281. call feedkeys("b", "xt!")
  282. call assert_equal("aa", g:line)
  283. call assert_false(exists('got_aaa'))
  284. call assert_false(exists('got_bb'))
  285. bwipe!
  286. nunmap aaaa
  287. nunmap bb
  288. nunmap b
  289. set timeoutlen&
  290. delfunc ExitInsert
  291. call timer_stop(timer)
  292. endfunc
  293. func Test_map_timeout_with_timer_interrupt()
  294. CheckFeature job
  295. CheckFeature timers
  296. let g:test_is_flaky = 1
  297. " Confirm the timer invoked in exit_cb of the job doesn't disturb mapped key
  298. " sequence.
  299. new
  300. let g:val = 0
  301. nnoremap \12 :let g:val = 1<CR>
  302. nnoremap \123 :let g:val = 2<CR>
  303. set timeout timeoutlen=200
  304. func ExitCb(job, status)
  305. let g:timer = timer_start(1, {-> feedkeys("3\<Esc>", 't')})
  306. endfunc
  307. call job_start([&shell, &shellcmdflag, 'echo'], {'exit_cb': 'ExitCb'})
  308. call feedkeys('\12', 'xt!')
  309. call assert_equal(2, g:val)
  310. bwipe!
  311. nunmap \12
  312. nunmap \123
  313. set timeoutlen&
  314. call WaitFor({-> exists('g:timer')})
  315. call timer_stop(g:timer)
  316. unlet g:timer
  317. unlet g:val
  318. delfunc ExitCb
  319. endfunc
  320. func Test_cabbr_visual_mode()
  321. cabbr s su
  322. call feedkeys(":s \<c-B>\"\<CR>", 'itx')
  323. call assert_equal('"su ', getreg(':'))
  324. call feedkeys(":'<,'>s \<c-B>\"\<CR>", 'itx')
  325. let expected = '"'. "'<,'>su "
  326. call assert_equal(expected, getreg(':'))
  327. call feedkeys(": '<,'>s \<c-B>\"\<CR>", 'itx')
  328. let expected = '" '. "'<,'>su "
  329. call assert_equal(expected, getreg(':'))
  330. call feedkeys(":'a,'bs \<c-B>\"\<CR>", 'itx')
  331. let expected = '"'. "'a,'bsu "
  332. call assert_equal(expected, getreg(':'))
  333. cunabbr s
  334. endfunc
  335. func Test_abbreviation_CR()
  336. new
  337. func Eatchar(pat)
  338. let c = nr2char(getchar(0))
  339. return (c =~ a:pat) ? '' : c
  340. endfunc
  341. iabbrev <buffer><silent> ~~7 <c-r>=repeat('~', 7)<CR><c-r>=Eatchar('\s')<cr>
  342. call feedkeys("GA~~7 \<esc>", 'xt')
  343. call assert_equal('~~~~~~~', getline('$'))
  344. %d
  345. call feedkeys("GA~~7\<cr>\<esc>", 'xt')
  346. call assert_equal(['~~~~~~~', ''], getline(1,'$'))
  347. delfunc Eatchar
  348. bw!
  349. endfunc
  350. func Test_motionforce_omap()
  351. func GetCommand()
  352. let g:m=mode(1)
  353. let [g:lnum1, g:col1] = searchpos('-', 'Wb')
  354. if g:lnum1 == 0
  355. return "\<Esc>"
  356. endif
  357. let [g:lnum2, g:col2] = searchpos('-', 'W')
  358. if g:lnum2 == 0
  359. return "\<Esc>"
  360. endif
  361. return ":call Select()\<CR>"
  362. endfunc
  363. func Select()
  364. call cursor([g:lnum1, g:col1])
  365. exe "normal! 1 ". (strlen(g:m) == 2 ? 'v' : g:m[2])
  366. call cursor([g:lnum2, g:col2])
  367. execute "normal! \<BS>"
  368. endfunc
  369. new
  370. onoremap <buffer><expr> i- GetCommand()
  371. " 1) default omap mapping
  372. %d_
  373. call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
  374. call cursor(2, 1)
  375. norm di-
  376. call assert_equal('no', g:m)
  377. call assert_equal(['aaa -- eee'], getline(1, '$'))
  378. " 2) forced characterwise operation
  379. %d_
  380. call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
  381. call cursor(2, 1)
  382. norm dvi-
  383. call assert_equal('nov', g:m)
  384. call assert_equal(['aaa -- eee'], getline(1, '$'))
  385. " 3) forced linewise operation
  386. %d_
  387. call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
  388. call cursor(2, 1)
  389. norm dVi-
  390. call assert_equal('noV', g:m)
  391. call assert_equal([''], getline(1, '$'))
  392. " 4) forced blockwise operation
  393. %d_
  394. call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
  395. call cursor(2, 1)
  396. exe "norm d\<C-V>i-"
  397. call assert_equal("no\<C-V>", g:m)
  398. call assert_equal(['aaabbb', 'x', 'dddeee'], getline(1, '$'))
  399. bwipe!
  400. delfunc Select
  401. delfunc GetCommand
  402. endfunc
  403. func Test_error_in_map_expr()
  404. " Unlike CheckRunVimInTerminal this does work in a win32 console
  405. CheckFeature terminal
  406. if has('win32') && has('gui_running')
  407. throw 'Skipped: cannot run Vim in a terminal window'
  408. endif
  409. let lines =<< trim [CODE]
  410. func Func()
  411. " fail to create list
  412. let x = [
  413. endfunc
  414. nmap <expr> ! Func()
  415. set updatetime=50
  416. [CODE]
  417. call writefile(lines, 'Xtest.vim')
  418. let buf = term_start(GetVimCommandCleanTerm() .. ' -S Xtest.vim', {'term_rows': 8})
  419. let job = term_getjob(buf)
  420. call WaitForAssert({-> assert_notequal('', term_getline(buf, 8))})
  421. " GC must not run during map-expr processing, which can make Vim crash.
  422. call term_sendkeys(buf, '!')
  423. call TermWait(buf, 50)
  424. call term_sendkeys(buf, "\<CR>")
  425. call TermWait(buf, 50)
  426. call assert_equal('run', job_status(job))
  427. call term_sendkeys(buf, ":qall!\<CR>")
  428. call WaitFor({-> job_status(job) ==# 'dead'})
  429. if has('unix')
  430. call assert_equal('', job_info(job).termsig)
  431. endif
  432. call delete('Xtest.vim')
  433. exe buf .. 'bwipe!'
  434. endfunc
  435. func Test_list_mappings()
  436. " Remove default mappings
  437. imapclear
  438. " reset 'isident' to check it isn't used
  439. set isident=
  440. inoremap <C-m> CtrlM
  441. inoremap <A-S> AltS
  442. inoremap <S-/> ShiftSlash
  443. set isident&
  444. call assert_equal([
  445. \ 'i <S-/> * ShiftSlash',
  446. \ 'i <M-S> * AltS',
  447. \ 'i <C-M> * CtrlM',
  448. \], execute('imap')->trim()->split("\n"))
  449. iunmap <C-M>
  450. iunmap <A-S>
  451. call assert_equal(['i <S-/> * ShiftSlash'], execute('imap')->trim()->split("\n"))
  452. iunmap <S-/>
  453. call assert_equal(['No mapping found'], execute('imap')->trim()->split("\n"))
  454. " List global, buffer local and script local mappings
  455. nmap ,f /^\k\+ (<CR>
  456. nmap <buffer> ,f /^\k\+ (<CR>
  457. nmap <script> ,fs /^\k\+ (<CR>
  458. call assert_equal(['n ,f @/^\k\+ (<CR>',
  459. \ 'n ,fs & /^\k\+ (<CR>',
  460. \ 'n ,f /^\k\+ (<CR>'],
  461. \ execute('nmap ,f')->trim()->split("\n"))
  462. " List <Nop> mapping
  463. nmap ,n <Nop>
  464. call assert_equal(['n ,n <Nop>'],
  465. \ execute('nmap ,n')->trim()->split("\n"))
  466. " verbose map
  467. call assert_match("\tLast set from .*/test_mapping.vim line \\d\\+$",
  468. \ execute('verbose map ,n')->trim()->split("\n")[1])
  469. " character with K_SPECIAL byte in rhs
  470. nmap foo …
  471. call assert_equal(['n foo …'],
  472. \ execute('nmap foo')->trim()->split("\n"))
  473. " modified character with K_SPECIAL byte in rhs
  474. nmap foo <M-…>
  475. call assert_equal(['n foo <M-…>'],
  476. \ execute('nmap foo')->trim()->split("\n"))
  477. " character with K_SPECIAL byte in lhs
  478. nmap … foo
  479. call assert_equal(['n … foo'],
  480. \ execute('nmap …')->trim()->split("\n"))
  481. " modified character with K_SPECIAL byte in lhs
  482. nmap <M-…> foo
  483. call assert_equal(['n <M-…> foo'],
  484. \ execute('nmap <M-…>')->trim()->split("\n"))
  485. " illegal bytes
  486. let str = ":\x7f:\x80:\x90:\xd0:"
  487. exe 'nmap foo ' .. str
  488. call assert_equal(['n foo ' .. strtrans(str)],
  489. \ execute('nmap foo')->trim()->split("\n"))
  490. unlet str
  491. " map to CTRL-V
  492. exe "nmap ,k \<C-V>"
  493. call assert_equal(['n ,k <Nop>'],
  494. \ execute('nmap ,k')->trim()->split("\n"))
  495. " map with space at the beginning
  496. exe "nmap \<C-V> w <Nop>"
  497. call assert_equal(['n <Space>w <Nop>'],
  498. \ execute("nmap \<C-V> w")->trim()->split("\n"))
  499. nmapclear
  500. endfunc
  501. func Test_expr_map_gets_cursor()
  502. new
  503. call setline(1, ['one', 'some w!rd'])
  504. func StoreColumn()
  505. let g:exprLine = line('.')
  506. let g:exprCol = col('.')
  507. return 'x'
  508. endfunc
  509. nnoremap <expr> x StoreColumn()
  510. 2
  511. nmap ! f!<Ignore>x
  512. call feedkeys("!", 'xt')
  513. call assert_equal('some wrd', getline(2))
  514. call assert_equal(2, g:exprLine)
  515. call assert_equal(7, g:exprCol)
  516. bwipe!
  517. unlet g:exprLine
  518. unlet g:exprCol
  519. delfunc StoreColumn
  520. nunmap x
  521. nunmap !
  522. endfunc
  523. func Test_expr_map_restore_cursor()
  524. CheckScreendump
  525. let lines =<< trim END
  526. call setline(1, ['one', 'two', 'three'])
  527. 2
  528. set ls=2
  529. hi! link StatusLine ErrorMsg
  530. noremap <expr> <C-B> Func()
  531. func Func()
  532. let g:on = !get(g:, 'on', 0)
  533. redraws
  534. return ''
  535. endfunc
  536. func Status()
  537. return get(g:, 'on', 0) ? '[on]' : ''
  538. endfunc
  539. set stl=%{Status()}
  540. END
  541. call writefile(lines, 'XtestExprMap')
  542. let buf = RunVimInTerminal('-S XtestExprMap', #{rows: 10})
  543. call term_sendkeys(buf, "\<C-B>")
  544. call VerifyScreenDump(buf, 'Test_map_expr_1', {})
  545. " clean up
  546. call StopVimInTerminal(buf)
  547. call delete('XtestExprMap')
  548. endfunc
  549. func Test_map_listing()
  550. CheckScreendump
  551. let lines =<< trim END
  552. nmap a b
  553. END
  554. call writefile(lines, 'XtestMapList')
  555. let buf = RunVimInTerminal('-S XtestMapList', #{rows: 6})
  556. call term_sendkeys(buf, ": nmap a\<CR>")
  557. call VerifyScreenDump(buf, 'Test_map_list_1', {})
  558. " clean up
  559. call StopVimInTerminal(buf)
  560. call delete('XtestMapList')
  561. endfunc
  562. func Test_expr_map_error()
  563. CheckScreendump
  564. let lines =<< trim END
  565. func Func()
  566. throw 'test'
  567. return ''
  568. endfunc
  569. nnoremap <expr> <F2> Func()
  570. cnoremap <expr> <F2> Func()
  571. call test_override('ui_delay', 10)
  572. END
  573. call writefile(lines, 'XtestExprMap')
  574. let buf = RunVimInTerminal('-S XtestExprMap', #{rows: 10})
  575. call term_sendkeys(buf, "\<F2>")
  576. call TermWait(buf)
  577. call term_sendkeys(buf, "\<CR>")
  578. call VerifyScreenDump(buf, 'Test_map_expr_2', {})
  579. call term_sendkeys(buf, ":abc\<F2>")
  580. call VerifyScreenDump(buf, 'Test_map_expr_3', {})
  581. call term_sendkeys(buf, "\<Esc>0")
  582. call VerifyScreenDump(buf, 'Test_map_expr_4', {})
  583. " clean up
  584. call StopVimInTerminal(buf)
  585. call delete('XtestExprMap')
  586. endfunc
  587. " Test for mapping errors
  588. func Test_map_error()
  589. call assert_fails('unmap', 'E474:')
  590. call assert_fails("exe 'map ' .. repeat('a', 51) .. ' :ls'", 'E474:')
  591. call assert_fails('unmap abc', 'E31:')
  592. call assert_fails('unabbr abc', 'E24:')
  593. call assert_equal('', maparg(''))
  594. call assert_fails('echo maparg("abc", [])', 'E730:')
  595. " unique map
  596. map ,w /[#&!]<CR>
  597. call assert_fails("map <unique> ,w /[#&!]<CR>", 'E227:')
  598. " unique buffer-local map
  599. call assert_fails("map <buffer> <unique> ,w /[.,;]<CR>", 'E225:')
  600. unmap ,w
  601. " unique abbreviation
  602. abbr SP special
  603. call assert_fails("abbr <unique> SP special", 'E226:')
  604. " unique buffer-local map
  605. call assert_fails("abbr <buffer> <unique> SP special", 'E224:')
  606. unabbr SP
  607. call assert_fails('mapclear abc', 'E474:')
  608. call assert_fails('abclear abc', 'E474:')
  609. call assert_fails('abbr $xyz abc', 'E474:')
  610. " space character in an abbreviation
  611. call assert_fails('abbr ab<space> ABC', 'E474:')
  612. " invalid <expr> map
  613. map <expr> ,f abc
  614. call assert_fails('normal ,f', 'E121:')
  615. unmap <expr> ,f
  616. " Recursive use of :normal in a map
  617. set maxmapdepth=100
  618. map gq :normal gq<CR>
  619. call assert_fails('normal gq', 'E192:')
  620. unmap gq
  621. set maxmapdepth&
  622. endfunc
  623. " Test for <special> key mapping
  624. func Test_map_special()
  625. throw 'skipped: Nvim does not support cpoptions flag "<"'
  626. new
  627. let old_cpo = &cpo
  628. set cpo+=<
  629. imap <F12> Blue
  630. call feedkeys("i\<F12>", "x")
  631. call assert_equal("<F12>", getline(1))
  632. call feedkeys("ddi<F12>", "x")
  633. call assert_equal("Blue", getline(1))
  634. iunmap <F12>
  635. imap <special> <F12> Green
  636. call feedkeys("ddi\<F12>", "x")
  637. call assert_equal("Green", getline(1))
  638. call feedkeys("ddi<F12>", "x")
  639. call assert_equal("<F12>", getline(1))
  640. iunmap <special> <F12>
  641. let &cpo = old_cpo
  642. %bwipe!
  643. endfunc
  644. " Test for hasmapto()
  645. func Test_hasmapto()
  646. call assert_equal(0, hasmapto('/^\k\+ ('))
  647. map ,f /^\k\+ (<CR>
  648. call assert_equal(1, hasmapto('/^\k\+ ('))
  649. unmap ,f
  650. " Insert mode mapping
  651. call assert_equal(0, hasmapto('/^\k\+ (', 'i'))
  652. imap ,f /^\k\+ (<CR>
  653. call assert_equal(1, hasmapto('/^\k\+ (', 'i'))
  654. iunmap ,f
  655. " Normal mode mapping
  656. call assert_equal(0, hasmapto('/^\k\+ (', 'n'))
  657. nmap ,f /^\k\+ (<CR>
  658. call assert_equal(1, hasmapto('/^\k\+ ('))
  659. call assert_equal(1, hasmapto('/^\k\+ (', 'n'))
  660. nunmap ,f
  661. " Visual and Select mode mapping
  662. call assert_equal(0, hasmapto('/^\k\+ (', 'v'))
  663. call assert_equal(0, hasmapto('/^\k\+ (', 'x'))
  664. call assert_equal(0, hasmapto('/^\k\+ (', 's'))
  665. vmap ,f /^\k\+ (<CR>
  666. call assert_equal(1, hasmapto('/^\k\+ (', 'v'))
  667. call assert_equal(1, hasmapto('/^\k\+ (', 'x'))
  668. call assert_equal(1, hasmapto('/^\k\+ (', 's'))
  669. vunmap ,f
  670. " Visual mode mapping
  671. call assert_equal(0, hasmapto('/^\k\+ (', 'x'))
  672. xmap ,f /^\k\+ (<CR>
  673. call assert_equal(1, hasmapto('/^\k\+ (', 'v'))
  674. call assert_equal(1, hasmapto('/^\k\+ (', 'x'))
  675. call assert_equal(0, hasmapto('/^\k\+ (', 's'))
  676. xunmap ,f
  677. " Select mode mapping
  678. call assert_equal(0, hasmapto('/^\k\+ (', 's'))
  679. smap ,f /^\k\+ (<CR>
  680. call assert_equal(1, hasmapto('/^\k\+ (', 'v'))
  681. call assert_equal(0, hasmapto('/^\k\+ (', 'x'))
  682. call assert_equal(1, hasmapto('/^\k\+ (', 's'))
  683. sunmap ,f
  684. " Operator-pending mode mapping
  685. call assert_equal(0, hasmapto('/^\k\+ (', 'o'))
  686. omap ,f /^\k\+ (<CR>
  687. call assert_equal(1, hasmapto('/^\k\+ (', 'o'))
  688. ounmap ,f
  689. " Language mapping
  690. call assert_equal(0, hasmapto('/^\k\+ (', 'l'))
  691. lmap ,f /^\k\+ (<CR>
  692. call assert_equal(1, hasmapto('/^\k\+ (', 'l'))
  693. lunmap ,f
  694. " Cmdline mode mapping
  695. call assert_equal(0, hasmapto('/^\k\+ (', 'c'))
  696. cmap ,f /^\k\+ (<CR>
  697. call assert_equal(1, hasmapto('/^\k\+ (', 'c'))
  698. cunmap ,f
  699. call assert_equal(0, hasmapto('/^\k\+ (', 'n', 1))
  700. endfunc
  701. " Test for command-line completion of maps
  702. func Test_mapcomplete()
  703. call assert_equal(['<buffer>', '<expr>', '<nowait>', '<script>',
  704. \ '<silent>', '<special>', '<unique>'],
  705. \ getcompletion('', 'mapping'))
  706. call assert_equal([], getcompletion(',d', 'mapping'))
  707. call feedkeys(":unmap <buf\<C-A>\<C-B>\"\<CR>", 'tx')
  708. call assert_equal('"unmap <buffer>', @:)
  709. call feedkeys(":unabbr <buf\<C-A>\<C-B>\"\<CR>", 'tx')
  710. call assert_equal('"unabbr <buffer>', @:)
  711. call feedkeys(":abbr! \<C-A>\<C-B>\"\<CR>", 'tx')
  712. call assert_equal("\"abbr! \x01", @:)
  713. " When multiple matches have the same {lhs}, it should only appear once.
  714. " The simplified form should also not be included.
  715. nmap ,<C-F> /H<CR>
  716. omap ,<C-F> /H<CR>
  717. call feedkeys(":map ,\<C-A>\<C-B>\"\<CR>", 'tx')
  718. call assert_equal('"map ,<C-F>', @:)
  719. mapclear
  720. endfunc
  721. func GetAbbrText()
  722. unabbr hola
  723. return 'hello'
  724. endfunc
  725. " Test for <expr> in abbreviation
  726. func Test_expr_abbr()
  727. new
  728. iabbr <expr> teh "the"
  729. call feedkeys("iteh ", "tx")
  730. call assert_equal('the ', getline(1))
  731. iabclear
  732. call setline(1, '')
  733. " invalid <expr> abbreviation
  734. abbr <expr> hte GetAbbr()
  735. call assert_fails('normal ihte ', 'E117:')
  736. call assert_equal('', getline(1))
  737. unabbr <expr> hte
  738. " evaluating the expression deletes the abbreviation
  739. abbr <expr> hola GetAbbrText()
  740. call assert_equal('GetAbbrText()', maparg('hola', 'i', '1'))
  741. call feedkeys("ahola \<Esc>", 'xt')
  742. call assert_equal('hello ', getline('.'))
  743. call assert_equal('', maparg('hola', 'i', '1'))
  744. bwipe!
  745. endfunc
  746. " Test for storing mappings in different modes in a vimrc file
  747. func Test_mkvimrc_mapmodes()
  748. map a1 /a1
  749. nmap a2 /a2
  750. vmap a3 /a3
  751. smap a4 /a4
  752. xmap a5 /a5
  753. omap a6 /a6
  754. map! a7 /a7
  755. imap a8 /a8
  756. lmap a9 /a9
  757. cmap a10 /a10
  758. tmap a11 /a11
  759. " Normal + Visual map
  760. map a12 /a12
  761. sunmap a12
  762. ounmap a12
  763. " Normal + Selectmode map
  764. map a13 /a13
  765. xunmap a13
  766. ounmap a13
  767. " Normal + OpPending map
  768. map a14 /a14
  769. vunmap a14
  770. " Visual + Selectmode map
  771. map a15 /a15
  772. nunmap a15
  773. ounmap a15
  774. " Visual + OpPending map
  775. map a16 /a16
  776. nunmap a16
  777. sunmap a16
  778. " Selectmode + OpPending map
  779. map a17 /a17
  780. nunmap a17
  781. xunmap a17
  782. " Normal + Visual + Selectmode map
  783. map a18 /a18
  784. ounmap a18
  785. " Normal + Visual + OpPending map
  786. map a19 /a19
  787. sunmap a19
  788. " Normal + Selectmode + OpPending map
  789. map a20 /a20
  790. xunmap a20
  791. " Visual + Selectmode + OpPending map
  792. map a21 /a21
  793. nunmap a21
  794. " Mapping to Nop
  795. map a22 <Nop>
  796. " Script local mapping
  797. map <script> a23 /a23
  798. " Newline in {lhs} and {rhs} of a map
  799. exe "map a24\<C-V>\<C-J> ia24\<C-V>\<C-J><Esc>"
  800. " Abbreviation
  801. abbr a25 A25
  802. cabbr a26 A26
  803. iabbr a27 A27
  804. mkvimrc! Xvimrc
  805. let l = readfile('Xvimrc')
  806. call assert_equal(['map a1 /a1'], filter(copy(l), 'v:val =~ " a1 "'))
  807. call assert_equal(['nmap a2 /a2'], filter(copy(l), 'v:val =~ " a2 "'))
  808. call assert_equal(['vmap a3 /a3'], filter(copy(l), 'v:val =~ " a3 "'))
  809. call assert_equal(['smap a4 /a4'], filter(copy(l), 'v:val =~ " a4 "'))
  810. call assert_equal(['xmap a5 /a5'], filter(copy(l), 'v:val =~ " a5 "'))
  811. call assert_equal(['omap a6 /a6'], filter(copy(l), 'v:val =~ " a6 "'))
  812. call assert_equal(['map! a7 /a7'], filter(copy(l), 'v:val =~ " a7 "'))
  813. call assert_equal(['imap a8 /a8'], filter(copy(l), 'v:val =~ " a8 "'))
  814. call assert_equal(['lmap a9 /a9'], filter(copy(l), 'v:val =~ " a9 "'))
  815. call assert_equal(['cmap a10 /a10'], filter(copy(l), 'v:val =~ " a10 "'))
  816. call assert_equal(['tmap a11 /a11'], filter(copy(l), 'v:val =~ " a11 "'))
  817. call assert_equal(['nmap a12 /a12', 'xmap a12 /a12'],
  818. \ filter(copy(l), 'v:val =~ " a12 "'))
  819. call assert_equal(['nmap a13 /a13', 'smap a13 /a13'],
  820. \ filter(copy(l), 'v:val =~ " a13 "'))
  821. call assert_equal(['nmap a14 /a14', 'omap a14 /a14'],
  822. \ filter(copy(l), 'v:val =~ " a14 "'))
  823. call assert_equal(['vmap a15 /a15'], filter(copy(l), 'v:val =~ " a15 "'))
  824. call assert_equal(['xmap a16 /a16', 'omap a16 /a16'],
  825. \ filter(copy(l), 'v:val =~ " a16 "'))
  826. call assert_equal(['smap a17 /a17', 'omap a17 /a17'],
  827. \ filter(copy(l), 'v:val =~ " a17 "'))
  828. call assert_equal(['nmap a18 /a18', 'vmap a18 /a18'],
  829. \ filter(copy(l), 'v:val =~ " a18 "'))
  830. call assert_equal(['nmap a19 /a19', 'xmap a19 /a19', 'omap a19 /a19'],
  831. \ filter(copy(l), 'v:val =~ " a19 "'))
  832. call assert_equal(['nmap a20 /a20', 'smap a20 /a20', 'omap a20 /a20'],
  833. \ filter(copy(l), 'v:val =~ " a20 "'))
  834. call assert_equal(['vmap a21 /a21', 'omap a21 /a21'],
  835. \ filter(copy(l), 'v:val =~ " a21 "'))
  836. call assert_equal(['map a22 <Nop>'], filter(copy(l), 'v:val =~ " a22 "'))
  837. call assert_equal([], filter(copy(l), 'v:val =~ " a23 "'))
  838. call assert_equal(["map a24<NL> ia24<NL>\x16\e"],
  839. \ filter(copy(l), 'v:val =~ " a24"'))
  840. call assert_equal(['abbr a25 A25'], filter(copy(l), 'v:val =~ " a25 "'))
  841. call assert_equal(['cabbr a26 A26'], filter(copy(l), 'v:val =~ " a26 "'))
  842. call assert_equal(['iabbr a27 A27'], filter(copy(l), 'v:val =~ " a27 "'))
  843. call delete('Xvimrc')
  844. mapclear
  845. nmapclear
  846. vmapclear
  847. xmapclear
  848. smapclear
  849. omapclear
  850. imapclear
  851. lmapclear
  852. cmapclear
  853. tmapclear
  854. endfunc
  855. " Test for recursive mapping ('maxmapdepth')
  856. func Test_map_recursive()
  857. map x y
  858. map y x
  859. call assert_fails('normal x', 'E223:')
  860. unmap x
  861. unmap y
  862. endfunc
  863. " Test for removing an abbreviation using {rhs} and with space after {lhs}
  864. func Test_abbr_remove()
  865. abbr foo bar
  866. let d = maparg('foo', 'i', 1, 1)
  867. call assert_equal(['foo', 'bar', '!'], [d.lhs, d.rhs, d.mode])
  868. unabbr bar
  869. call assert_equal({}, maparg('foo', 'i', 1, 1))
  870. abbr foo bar
  871. unabbr foo<space><tab>
  872. call assert_equal({}, maparg('foo', 'i', 1, 1))
  873. endfunc
  874. " Trigger an abbreviation using a special key
  875. func Test_abbr_trigger_special()
  876. new
  877. iabbr teh the
  878. call feedkeys("iteh\<F2>\<Esc>", 'xt')
  879. call assert_equal('the<F2>', getline(1))
  880. iunab teh
  881. close!
  882. endfunc
  883. " Test for '<' in 'cpoptions'
  884. func Test_map_cpo_special_keycode()
  885. set cpo-=<
  886. imap x<Bslash>k Test
  887. let d = maparg('x<Bslash>k', 'i', 0, 1)
  888. call assert_equal(['x\k', 'Test', 'i'], [d.lhs, d.rhs, d.mode])
  889. call feedkeys(":imap x\<C-A>\<C-B>\"\<CR>", 'tx')
  890. call assert_equal('"imap x\k', @:)
  891. iunmap x<Bslash>k
  892. " Nvim: no "<" flag in 'cpoptions'.
  893. " set cpo+=<
  894. " imap x<Bslash>k Test
  895. " let d = maparg('x<Bslash>k', 'i', 0, 1)
  896. " call assert_equal(['x<Bslash>k', 'Test', 'i'], [d.lhs, d.rhs, d.mode])
  897. " call feedkeys(":imap x\<C-A>\<C-B>\"\<CR>", 'tx')
  898. " call assert_equal('"imap x<Bslash>k', @:)
  899. " iunmap x<Bslash>k
  900. set cpo-=<
  901. " Modifying 'cpo' above adds some default mappings, remove them
  902. mapclear
  903. mapclear!
  904. endfunc
  905. " Test for <Cmd> key in maps to execute commands
  906. func Test_map_cmdkey()
  907. new
  908. " Error cases
  909. let x = 0
  910. noremap <F3> <Cmd><Cmd>let x = 1<CR>
  911. call assert_fails('call feedkeys("\<F3>", "xt")', 'E1136:')
  912. call assert_equal(0, x)
  913. noremap <F3> <Cmd>let x = 3
  914. call assert_fails('call feedkeys("\<F3>", "xt!")', 'E1255:')
  915. call assert_equal(0, x)
  916. " works in various modes and sees the correct mode()
  917. noremap <F3> <Cmd>let m = mode(1)<CR>
  918. noremap! <F3> <Cmd>let m = mode(1)<CR>
  919. " normal mode
  920. call feedkeys("\<F3>", 'xt')
  921. call assert_equal('n', m)
  922. " visual mode
  923. call feedkeys("v\<F3>", 'xt!')
  924. call assert_equal('v', m)
  925. " shouldn't leave the visual mode
  926. call assert_equal('v', mode(1))
  927. call feedkeys("\<Esc>", 'xt')
  928. call assert_equal('n', mode(1))
  929. " visual mapping in select mode
  930. call feedkeys("gh\<F3>", 'xt!')
  931. call assert_equal('v', m)
  932. " shouldn't leave select mode
  933. call assert_equal('s', mode(1))
  934. call feedkeys("\<Esc>", 'xt')
  935. call assert_equal('n', mode(1))
  936. " select mode mapping
  937. snoremap <F3> <Cmd>let m = mode(1)<cr>
  938. call feedkeys("gh\<F3>", 'xt!')
  939. call assert_equal('s', m)
  940. " shouldn't leave select mode
  941. call assert_equal('s', mode(1))
  942. call feedkeys("\<Esc>", 'xt')
  943. call assert_equal('n', mode(1))
  944. " operator-pending mode
  945. call feedkeys("d\<F3>", 'xt!')
  946. call assert_equal('no', m)
  947. " leaves the operator-pending mode
  948. call assert_equal('n', mode(1))
  949. " insert mode
  950. call feedkeys("i\<F3>abc", 'xt')
  951. call assert_equal('i', m)
  952. call assert_equal('abc', getline('.'))
  953. " replace mode
  954. call feedkeys("0R\<F3>two", 'xt')
  955. call assert_equal('R', m)
  956. call assert_equal('two', getline('.'))
  957. " virtual replace mode
  958. call setline('.', "one\ttwo")
  959. call feedkeys("4|gR\<F3>xxx", 'xt')
  960. call assert_equal('Rv', m)
  961. call assert_equal("onexxx\ttwo", getline('.'))
  962. " cmdline mode
  963. call feedkeys(":\<F3>\"xxx\<CR>", 'xt!')
  964. call assert_equal('c', m)
  965. call assert_equal('"xxx', @:)
  966. " terminal mode
  967. if CanRunVimInTerminal()
  968. tnoremap <F3> <Cmd>let m = mode(1)<CR>
  969. let buf = Run_shell_in_terminal({})
  970. call feedkeys("\<F3>", 'xt')
  971. call assert_equal('t', m)
  972. call assert_equal('t', mode(1))
  973. call StopShellInTerminal(buf)
  974. close!
  975. tunmap <F3>
  976. endif
  977. " invoke cmdline mode recursively
  978. noremap! <F2> <Cmd>norm! :foo<CR>
  979. %d
  980. call setline(1, ['some short lines', 'of test text'])
  981. call feedkeys(":bar\<F2>x\<C-B>\"\r", 'xt')
  982. call assert_equal('"barx', @:)
  983. unmap! <F2>
  984. " test for calling a <SID> function
  985. let lines =<< trim END
  986. map <F2> <Cmd>call <SID>do_it()<CR>
  987. func s:do_it()
  988. let g:x = 32
  989. endfunc
  990. END
  991. call writefile(lines, 'Xscript')
  992. source Xscript
  993. call feedkeys("\<F2>", 'xt')
  994. call assert_equal(32, g:x)
  995. call delete('Xscript')
  996. unmap <F3>
  997. unmap! <F3>
  998. %bw!
  999. endfunc
  1000. " text object enters visual mode
  1001. func TextObj()
  1002. if mode() !=# "v"
  1003. normal! v
  1004. end
  1005. call cursor(1, 3)
  1006. normal! o
  1007. call cursor(2, 4)
  1008. endfunc
  1009. func s:cmdmap(lhs, rhs)
  1010. exe 'noremap ' .. a:lhs .. ' <Cmd>' .. a:rhs .. '<CR>'
  1011. exe 'noremap! ' .. a:lhs .. ' <Cmd>' .. a:rhs .. '<CR>'
  1012. endfunc
  1013. func s:cmdunmap(lhs)
  1014. exe 'unmap ' .. a:lhs
  1015. exe 'unmap! ' .. a:lhs
  1016. endfunc
  1017. " Map various <Fx> keys used by the <Cmd> key tests
  1018. func s:setupMaps()
  1019. call s:cmdmap('<F3>', 'let m = mode(1)')
  1020. call s:cmdmap('<F4>', 'normal! ww')
  1021. call s:cmdmap('<F5>', 'normal! "ay')
  1022. call s:cmdmap('<F6>', 'throw "very error"')
  1023. call s:cmdmap('<F7>', 'call TextObj()')
  1024. call s:cmdmap('<F8>', 'startinsert')
  1025. call s:cmdmap('<F9>', 'stopinsert')
  1026. endfunc
  1027. " Remove the mappings setup by setupMaps()
  1028. func s:cleanupMaps()
  1029. call s:cmdunmap('<F3>')
  1030. call s:cmdunmap('<F4>')
  1031. call s:cmdunmap('<F5>')
  1032. call s:cmdunmap('<F6>')
  1033. call s:cmdunmap('<F7>')
  1034. call s:cmdunmap('<F8>')
  1035. call s:cmdunmap('<F9>')
  1036. endfunc
  1037. " Test for <Cmd> mapping in normal mode
  1038. func Test_map_cmdkey_normal_mode()
  1039. new
  1040. call s:setupMaps()
  1041. " check v:count and v:register works
  1042. call s:cmdmap('<F2>', 'let s = [mode(1), v:count, v:register]')
  1043. call feedkeys("\<F2>", 'xt')
  1044. call assert_equal(['n', 0, '"'], s)
  1045. call feedkeys("7\<F2>", 'xt')
  1046. call assert_equal(['n', 7, '"'], s)
  1047. call feedkeys("\"e\<F2>", 'xt')
  1048. call assert_equal(['n', 0, 'e'], s)
  1049. call feedkeys("5\"k\<F2>", 'xt')
  1050. call assert_equal(['n', 5, 'k'], s)
  1051. call s:cmdunmap('<F2>')
  1052. call setline(1, ['some short lines', 'of test text'])
  1053. call feedkeys("\<F7>y", 'xt')
  1054. call assert_equal("me short lines\nof t", @")
  1055. call assert_equal('v', getregtype('"'))
  1056. call assert_equal([0, 1, 3, 0], getpos("'<"))
  1057. call assert_equal([0, 2, 4, 0], getpos("'>"))
  1058. " startinsert
  1059. %d
  1060. call feedkeys("\<F8>abc", 'xt')
  1061. call assert_equal('abc', getline(1))
  1062. " feedkeys are not executed immediately
  1063. noremap ,a <Cmd>call feedkeys("aalpha") \| let g:a = getline(2)<CR>
  1064. %d
  1065. call setline(1, ['some short lines', 'of test text'])
  1066. call cursor(2, 3)
  1067. call feedkeys(",a\<F3>", 'xt')
  1068. call assert_equal('of test text', g:a)
  1069. call assert_equal('n', m)
  1070. call assert_equal(['some short lines', 'of alphatest text'], getline(1, '$'))
  1071. nunmap ,a
  1072. " feedkeys(..., 'x') is executed immediately, but insert mode is aborted
  1073. noremap ,b <Cmd>call feedkeys("abeta", 'x') \| let g:b = getline(2)<CR>
  1074. call feedkeys(",b\<F3>", 'xt')
  1075. call assert_equal('n', m)
  1076. call assert_equal('of alphabetatest text', g:b)
  1077. nunmap ,b
  1078. call s:cleanupMaps()
  1079. %bw!
  1080. endfunc
  1081. " Test for <Cmd> mapping with the :normal command
  1082. func Test_map_cmdkey_normal_cmd()
  1083. new
  1084. noremap ,x <Cmd>call append(1, "xx") \| call append(1, "aa")<CR>
  1085. noremap ,f <Cmd>nosuchcommand<CR>
  1086. noremap ,e <Cmd>throw "very error" \| call append(1, "yy")<CR>
  1087. noremap ,m <Cmd>echoerr "The message." \| call append(1, "zz")<CR>
  1088. noremap ,w <Cmd>for i in range(5) \| if i==1 \| echoerr "Err" \| endif \| call append(1, i) \| endfor<CR>
  1089. call setline(1, ['some short lines', 'of test text'])
  1090. exe "norm ,x\r"
  1091. call assert_equal(['some short lines', 'aa', 'xx', 'of test text'], getline(1, '$'))
  1092. call assert_fails('norm ,f', 'E492:')
  1093. call assert_fails('norm ,e', 'very error')
  1094. call assert_fails('norm ,m', 'The message.')
  1095. call assert_equal(['some short lines', 'aa', 'xx', 'of test text'], getline(1, '$'))
  1096. %d
  1097. let caught_err = 0
  1098. try
  1099. exe "normal ,w"
  1100. catch /Vim(echoerr):Err/
  1101. let caught_err = 1
  1102. endtry
  1103. call assert_equal(1, caught_err)
  1104. call assert_equal(['', '0'], getline(1, '$'))
  1105. %d
  1106. call assert_fails('normal ,w', 'Err')
  1107. call assert_equal(['', '4', '3', '2' ,'1', '0'], getline(1, '$'))
  1108. call assert_equal(1, line('.'))
  1109. nunmap ,x
  1110. nunmap ,f
  1111. nunmap ,e
  1112. nunmap ,m
  1113. nunmap ,w
  1114. %bw!
  1115. endfunc
  1116. " Test for <Cmd> mapping in visual mode
  1117. func Test_map_cmdkey_visual_mode()
  1118. new
  1119. set showmode
  1120. call s:setupMaps()
  1121. call setline(1, ['some short lines', 'of test text'])
  1122. call feedkeys("v\<F4>", 'xt!')
  1123. call assert_equal(['v', 1, 12], [mode(1), col('v'), col('.')])
  1124. " can invoke an operator, ending the visual mode
  1125. let @a = ''
  1126. call feedkeys("\<F5>", 'xt!')
  1127. call assert_equal('n', mode(1))
  1128. call assert_equal('some short l', @a)
  1129. " error doesn't interrupt visual mode
  1130. call assert_fails('call feedkeys("ggvw\<F6>", "xt!")', 'E605:')
  1131. call assert_equal(['v', 1, 6], [mode(1), col('v'), col('.')])
  1132. call feedkeys("\<F7>", 'xt!')
  1133. call assert_equal(['v', 1, 3, 2, 4], [mode(1), line('v'), col('v'), line('.'), col('.')])
  1134. " startinsert gives "-- (insert) VISUAL --" mode
  1135. call feedkeys("\<F8>", 'xt!')
  1136. call assert_equal(['v', 1, 3, 2, 4], [mode(1), line('v'), col('v'), line('.'), col('.')])
  1137. redraw!
  1138. call assert_match('^-- (insert) VISUAL --', Screenline(&lines))
  1139. call feedkeys("\<Esc>new ", 'x')
  1140. call assert_equal(['some short lines', 'of new test text'], getline(1, '$'))
  1141. call s:cleanupMaps()
  1142. set showmode&
  1143. %bw!
  1144. endfunc
  1145. " Test for <Cmd> mapping in select mode
  1146. func Test_map_cmdkey_select_mode()
  1147. new
  1148. set showmode
  1149. call s:setupMaps()
  1150. snoremap <F1> <cmd>throw "very error"<CR>
  1151. snoremap <F2> <cmd>normal! <c-g>"by<CR>
  1152. call setline(1, ['some short lines', 'of test text'])
  1153. call feedkeys("gh\<F4>", "xt!")
  1154. call assert_equal(['s', 1, 12], [mode(1), col('v'), col('.')])
  1155. redraw!
  1156. call assert_match('^-- SELECT --', Screenline(&lines))
  1157. " visual mapping in select mode restarts select mode after operator
  1158. let @a = ''
  1159. call feedkeys("\<F5>", 'xt!')
  1160. call assert_equal('s', mode(1))
  1161. call assert_equal('some short l', @a)
  1162. " select mode mapping works, and does not restart select mode
  1163. let @b = ''
  1164. call feedkeys("\<F2>", 'xt!')
  1165. call assert_equal('n', mode(1))
  1166. call assert_equal('some short l', @b)
  1167. " error doesn't interrupt temporary visual mode
  1168. call assert_fails('call feedkeys("\<Esc>ggvw\<C-G>\<F6>", "xt!")', 'E605:')
  1169. redraw!
  1170. call assert_match('^-- VISUAL --', Screenline(&lines))
  1171. " quirk: restoration of select mode is not performed
  1172. call assert_equal(['v', 1, 6], [mode(1), col('v'), col('.')])
  1173. " error doesn't interrupt select mode
  1174. call assert_fails('call feedkeys("\<Esc>ggvw\<C-G>\<F1>", "xt!")', 'E605:')
  1175. redraw!
  1176. call assert_match('^-- SELECT --', Screenline(&lines))
  1177. call assert_equal(['s', 1, 6], [mode(1), col('v'), col('.')])
  1178. call feedkeys("\<F7>", 'xt!')
  1179. redraw!
  1180. call assert_match('^-- SELECT --', Screenline(&lines))
  1181. call assert_equal(['s', 1, 3, 2, 4], [mode(1), line('v'), col('v'), line('.'), col('.')])
  1182. " startinsert gives "-- SELECT (insert) --" mode
  1183. call feedkeys("\<F8>", 'xt!')
  1184. redraw!
  1185. call assert_match('^-- (insert) SELECT --', Screenline(&lines))
  1186. call assert_equal(['s', 1, 3, 2, 4], [mode(1), line('v'), col('v'), line('.'), col('.')])
  1187. call feedkeys("\<Esc>new ", 'x')
  1188. call assert_equal(['some short lines', 'of new test text'], getline(1, '$'))
  1189. sunmap <F1>
  1190. sunmap <F2>
  1191. call s:cleanupMaps()
  1192. set showmode&
  1193. %bw!
  1194. endfunc
  1195. " Test for <Cmd> mapping in operator-pending mode
  1196. func Test_map_cmdkey_op_pending_mode()
  1197. new
  1198. call s:setupMaps()
  1199. call setline(1, ['some short lines', 'of test text'])
  1200. call feedkeys("d\<F4>", 'xt')
  1201. call assert_equal(['lines', 'of test text'], getline(1, '$'))
  1202. call assert_equal(['some short '], getreg('"', 1, 1))
  1203. " create a new undo point
  1204. let &g:undolevels = &g:undolevels
  1205. call feedkeys(".", 'xt')
  1206. call assert_equal(['test text'], getline(1, '$'))
  1207. call assert_equal(['lines', 'of '], getreg('"', 1, 1))
  1208. " create a new undo point
  1209. let &g:undolevels = &g:undolevels
  1210. call feedkeys("uu", 'xt')
  1211. call assert_equal(['some short lines', 'of test text'], getline(1, '$'))
  1212. " error aborts operator-pending, operator not performed
  1213. call assert_fails('call feedkeys("d\<F6>", "xt")', 'E605:')
  1214. call assert_equal(['some short lines', 'of test text'], getline(1, '$'))
  1215. call feedkeys("\"bd\<F7>", 'xt')
  1216. call assert_equal(['soest text'], getline(1, '$'))
  1217. call assert_equal(['me short lines', 'of t'], getreg('b', 1, 1))
  1218. " startinsert aborts operator
  1219. call feedkeys("d\<F8>cc", 'xt')
  1220. call assert_equal(['soccest text'], getline(1, '$'))
  1221. call s:cleanupMaps()
  1222. %bw!
  1223. endfunc
  1224. " Test for <Cmd> mapping in insert mode
  1225. func Test_map_cmdkey_insert_mode()
  1226. new
  1227. call s:setupMaps()
  1228. call setline(1, ['some short lines', 'of test text'])
  1229. " works the same as <C-O>w<C-O>w
  1230. call feedkeys("iindeed \<F4>little ", 'xt')
  1231. call assert_equal(['indeed some short little lines', 'of test text'], getline(1, '$'))
  1232. call assert_fails('call feedkeys("i\<F6> 2", "xt")', 'E605:')
  1233. call assert_equal(['indeed some short little 2 lines', 'of test text'], getline(1, '$'))
  1234. " Note when entering visual mode from InsertEnter autocmd, an async event,
  1235. " or a <Cmd> mapping, vim ends up in undocumented "INSERT VISUAL" mode.
  1236. call feedkeys("i\<F7>stuff ", 'xt')
  1237. call assert_equal(['indeed some short little 2 lines', 'of stuff test text'], getline(1, '$'))
  1238. call assert_equal(['v', 1, 3, 2, 9], [mode(1), line('v'), col('v'), line('.'), col('.')])
  1239. call feedkeys("\<F5>", 'xt')
  1240. call assert_equal(['deed some short little 2 lines', 'of stuff '], getreg('a', 1, 1))
  1241. " also works as part of abbreviation
  1242. abbr foo <Cmd>let g:y = 17<CR>bar
  1243. exe "normal i\<space>foo "
  1244. call assert_equal(17, g:y)
  1245. call assert_equal('in bar deed some short little 2 lines', getline(1))
  1246. unabbr foo
  1247. " :startinsert does nothing
  1248. call setline(1, 'foo bar')
  1249. call feedkeys("ggi\<F8>vim", 'xt')
  1250. call assert_equal('vimfoo bar', getline(1))
  1251. " :stopinsert works
  1252. call feedkeys("ggi\<F9>Abc", 'xt')
  1253. call assert_equal('vimfoo barbc', getline(1))
  1254. call s:cleanupMaps()
  1255. %bw!
  1256. endfunc
  1257. " Test for <Cmd> mapping in insert-completion mode
  1258. func Test_map_cmdkey_insert_complete_mode()
  1259. new
  1260. call s:setupMaps()
  1261. call setline(1, 'some short lines')
  1262. call feedkeys("os\<C-X>\<C-N>\<F3>\<C-N> ", 'xt')
  1263. call assert_equal('ic', m)
  1264. call assert_equal(['some short lines', 'short '], getline(1, '$'))
  1265. call s:cleanupMaps()
  1266. %bw!
  1267. endfunc
  1268. " Test for <Cmd> mapping in cmdline mode
  1269. func Test_map_cmdkey_cmdline_mode()
  1270. new
  1271. call s:setupMaps()
  1272. call setline(1, ['some short lines', 'of test text'])
  1273. let x = 0
  1274. call feedkeys(":let x\<F3>= 10\r", 'xt')
  1275. call assert_equal('c', m)
  1276. call assert_equal(10, x)
  1277. " exception doesn't leave cmdline mode
  1278. call assert_fails('call feedkeys(":let x\<F6>= 20\r", "xt")', 'E605:')
  1279. call assert_equal(20, x)
  1280. " move cursor in the buffer from cmdline mode
  1281. call feedkeys(":let x\<F4>= 30\r", 'xt')
  1282. call assert_equal(30, x)
  1283. call assert_equal(12, col('.'))
  1284. " :startinsert takes effect after leaving cmdline mode
  1285. call feedkeys(":let x\<F8>= 40\rnew ", 'xt')
  1286. call assert_equal(40, x)
  1287. call assert_equal('some short new lines', getline(1))
  1288. call s:cleanupMaps()
  1289. %bw!
  1290. endfunc
  1291. func Test_map_cmdkey_redo()
  1292. func SelectDash()
  1293. call search('^---\n\zs', 'bcW')
  1294. norm! V
  1295. call search('\n\ze---$', 'W')
  1296. endfunc
  1297. let text =<< trim END
  1298. ---
  1299. aaa
  1300. ---
  1301. bbb
  1302. bbb
  1303. ---
  1304. ccc
  1305. ccc
  1306. ccc
  1307. ---
  1308. END
  1309. new Xcmdtext
  1310. call setline(1, text)
  1311. onoremap <silent> i- <Cmd>call SelectDash()<CR>
  1312. call feedkeys('2Gdi-', 'xt')
  1313. call assert_equal(['---', '---'], getline(1, 2))
  1314. call feedkeys('j.', 'xt')
  1315. call assert_equal(['---', '---', '---'], getline(1, 3))
  1316. call feedkeys('j.', 'xt')
  1317. call assert_equal(['---', '---', '---', '---'], getline(1, 4))
  1318. bwipe!
  1319. call delete('Xcmdtext')
  1320. delfunc SelectDash
  1321. ounmap i-
  1322. new
  1323. call setline(1, 'aaa bbb ccc ddd')
  1324. " command can contain special keys
  1325. onoremap ix <Cmd>let g:foo ..= '…'<Bar>normal! <C-Right><CR>
  1326. let g:foo = ''
  1327. call feedkeys('0dix.', 'xt')
  1328. call assert_equal('……', g:foo)
  1329. call assert_equal('ccc ddd', getline(1))
  1330. unlet g:foo
  1331. " command line ending in "0" is handled without errors
  1332. onoremap ix <Cmd>eval 0<CR>
  1333. call feedkeys('dix.', 'xt')
  1334. ounmap ix
  1335. bwipe!
  1336. endfunc
  1337. " Test for using <script> with a map to remap characters in rhs
  1338. func Test_script_local_remap()
  1339. new
  1340. inoremap <buffer> <SID>xyz mno
  1341. inoremap <buffer> <script> abc st<SID>xyzre
  1342. normal iabc
  1343. call assert_equal('stmnore', getline(1))
  1344. bwipe!
  1345. endfunc
  1346. func Test_abbreviate_multi_byte()
  1347. new
  1348. iabbrev foo bar
  1349. call feedkeys("ifoo…\<Esc>", 'xt')
  1350. call assert_equal("bar…", getline(1))
  1351. iunabbrev foo
  1352. bwipe!
  1353. endfunc
  1354. " Test for abbreviations with 'latin1' encoding
  1355. func Test_abbreviate_latin1_encoding()
  1356. " set encoding=latin1
  1357. call assert_fails('abbr ab#$c ABC', 'E474:')
  1358. new
  1359. iabbr <buffer> #i #include
  1360. iabbr <buffer> ## #enddef
  1361. exe "normal i#i\<C-]>"
  1362. call assert_equal('#include', getline(1))
  1363. exe "normal 0Di##\<C-]>"
  1364. call assert_equal('#enddef', getline(1))
  1365. %bw!
  1366. set encoding=utf-8
  1367. endfunc
  1368. +
  1369. " Test for <Plug> always being mapped, even when used with "noremap".
  1370. func Test_plug_remap()
  1371. let g:foo = 0
  1372. nnoremap <Plug>(Increase_x) <Cmd>let g:foo += 1<CR>
  1373. nmap <F2> <Plug>(Increase_x)
  1374. nnoremap <F3> <Plug>(Increase_x)
  1375. call feedkeys("\<F2>", 'xt')
  1376. call assert_equal(1, g:foo)
  1377. call feedkeys("\<F3>", 'xt')
  1378. call assert_equal(2, g:foo)
  1379. nnoremap x <Nop>
  1380. nmap <F4> x<Plug>(Increase_x)x
  1381. nnoremap <F5> x<Plug>(Increase_x)x
  1382. call setline(1, 'Some text')
  1383. normal! gg$
  1384. call feedkeys("\<F4>", 'xt')
  1385. call assert_equal(3, g:foo)
  1386. call assert_equal('Some text', getline(1))
  1387. call feedkeys("\<F5>", 'xt')
  1388. call assert_equal(4, g:foo)
  1389. call assert_equal('Some te', getline(1))
  1390. nunmap <Plug>(Increase_x)
  1391. nunmap <F2>
  1392. nunmap <F3>
  1393. nunmap <F4>
  1394. nunmap <F5>
  1395. unlet g:foo
  1396. %bw!
  1397. endfunc
  1398. func Test_mouse_drag_mapped_start_select()
  1399. set mouse=a
  1400. set selectmode=key,mouse
  1401. func ClickExpr()
  1402. call Ntest_setmouse(1, 1)
  1403. return "\<LeftMouse>"
  1404. endfunc
  1405. func DragExpr()
  1406. call Ntest_setmouse(1, 2)
  1407. return "\<LeftDrag>"
  1408. endfunc
  1409. nnoremap <expr> <F2> ClickExpr()
  1410. nmap <expr> <F3> DragExpr()
  1411. nnoremap <LeftDrag> <LeftDrag><Cmd><CR>
  1412. exe "normal \<F2>\<F3>"
  1413. call assert_equal('s', mode())
  1414. exe "normal! \<C-\>\<C-N>"
  1415. nunmap <LeftDrag>
  1416. nunmap <F2>
  1417. nunmap <F3>
  1418. delfunc ClickExpr
  1419. delfunc DragExpr
  1420. set selectmode&
  1421. set mouse&
  1422. endfunc
  1423. func Test_mouse_drag_statusline()
  1424. set laststatus=2
  1425. set mouse=a
  1426. func ClickExpr()
  1427. call Ntest_setmouse(&lines - 1, 1)
  1428. return "\<LeftMouse>"
  1429. endfunc
  1430. func DragExpr()
  1431. call Ntest_setmouse(&lines - 2, 1)
  1432. return "\<LeftDrag>"
  1433. endfunc
  1434. nnoremap <expr> <F2> ClickExpr()
  1435. nnoremap <expr> <F3> DragExpr()
  1436. " this was causing a crash in win_drag_status_line()
  1437. call feedkeys("\<F2>:tabnew\<CR>\<F3>", 'tx')
  1438. nunmap <F2>
  1439. nunmap <F3>
  1440. delfunc ClickExpr
  1441. delfunc DragExpr
  1442. set laststatus& mouse&
  1443. endfunc
  1444. " Test for mapping <LeftDrag> in Insert mode
  1445. func Test_mouse_drag_insert_map()
  1446. set mouse=a
  1447. func ClickExpr()
  1448. call Ntest_setmouse(1, 1)
  1449. return "\<LeftMouse>"
  1450. endfunc
  1451. func DragExpr()
  1452. call Ntest_setmouse(1, 2)
  1453. return "\<LeftDrag>"
  1454. endfunc
  1455. inoremap <expr> <F2> ClickExpr()
  1456. imap <expr> <F3> DragExpr()
  1457. inoremap <LeftDrag> <LeftDrag><Cmd>let g:dragged = 1<CR>
  1458. exe "normal i\<F2>\<F3>"
  1459. call assert_equal(1, g:dragged)
  1460. call assert_equal('v', mode())
  1461. exe "normal! \<C-\>\<C-N>"
  1462. unlet g:dragged
  1463. inoremap <LeftDrag> <LeftDrag><C-\><C-N>
  1464. exe "normal i\<F2>\<F3>"
  1465. call assert_equal('n', mode())
  1466. iunmap <LeftDrag>
  1467. iunmap <F2>
  1468. iunmap <F3>
  1469. delfunc ClickExpr
  1470. delfunc DragExpr
  1471. set mouse&
  1472. endfunc
  1473. func Test_unmap_simplifiable()
  1474. map <C-I> foo
  1475. map <Tab> bar
  1476. call assert_equal('foo', maparg('<C-I>'))
  1477. call assert_equal('bar', maparg('<Tab>'))
  1478. unmap <C-I>
  1479. call assert_equal('', maparg('<C-I>'))
  1480. call assert_equal('bar', maparg('<Tab>'))
  1481. unmap <Tab>
  1482. map <C-I> foo
  1483. unmap <Tab>
  1484. " This should not error
  1485. unmap <C-I>
  1486. endfunc
  1487. " Test that the first byte of rhs is not remapped if rhs starts with lhs.
  1488. func Test_map_rhs_starts_with_lhs()
  1489. new
  1490. func MapExpr()
  1491. return "\<C-R>\<C-P>"
  1492. endfunc
  1493. for expr in [v:false, v:true]
  1494. if expr
  1495. imap <buffer><expr> <C-R> MapExpr()
  1496. else
  1497. imap <buffer> <C-R> <C-R><C-P>
  1498. endif
  1499. for restore in [v:false, v:true]
  1500. if restore
  1501. let saved = maparg('<C-R>', 'i', v:false, v:true)
  1502. iunmap <buffer> <C-R>
  1503. call mapset(saved)
  1504. endif
  1505. let @a = 'foo'
  1506. call assert_nobeep('call feedkeys("S\<C-R>a", "tx")')
  1507. call assert_equal('foo', getline('.'))
  1508. let @a = 'bar'
  1509. call assert_nobeep('call feedkeys("S\<*C-R>a", "tx")')
  1510. call assert_equal('bar', getline('.'))
  1511. endfor
  1512. endfor
  1513. " When two mappings are used for <C-I> and <Tab>, remapping should work.
  1514. imap <buffer> <C-I> <Tab>bar
  1515. imap <buffer> <Tab> foo
  1516. call feedkeys("S\<Tab>", 'xt')
  1517. call assert_equal('foo', getline('.'))
  1518. call feedkeys("S\<*C-I>", 'xt')
  1519. call assert_equal('foobar', getline('.'))
  1520. delfunc MapExpr
  1521. bwipe!
  1522. endfunc
  1523. func Test_expr_map_escape_special()
  1524. nnoremap … <Cmd>let g:got_ellipsis += 1<CR>
  1525. func Func()
  1526. return '…'
  1527. endfunc
  1528. nmap <expr> <F2> Func()
  1529. let g:got_ellipsis = 0
  1530. call feedkeys("\<F2>", 'xt')
  1531. call assert_equal(1, g:got_ellipsis)
  1532. delfunc Func
  1533. nunmap <F2>
  1534. unlet g:got_ellipsis
  1535. nunmap …
  1536. endfunc
  1537. " Testing for mapping after an <Nop> mapping is triggered on timeout.
  1538. " Test for what patch 8.1.0052 fixes.
  1539. func Test_map_after_timed_out_nop()
  1540. CheckRunVimInTerminal
  1541. let lines =<< trim END
  1542. set timeout timeoutlen=400
  1543. inoremap ab TEST
  1544. inoremap a <Nop>
  1545. END
  1546. call writefile(lines, 'Xtest_map_after_timed_out_nop', 'D')
  1547. let buf = RunVimInTerminal('-S Xtest_map_after_timed_out_nop', #{rows: 6})
  1548. " Enter Insert mode
  1549. call term_sendkeys(buf, 'i')
  1550. " Wait for the "a" mapping to timeout
  1551. call term_sendkeys(buf, 'a')
  1552. call term_wait(buf, 500)
  1553. " Send "a" and wait for a period shorter than 'timeoutlen'
  1554. call term_sendkeys(buf, 'a')
  1555. call term_wait(buf, 100)
  1556. " Send "b", should trigger the "ab" mapping
  1557. call term_sendkeys(buf, 'b')
  1558. call WaitForAssert({-> assert_equal("TEST", term_getline(buf, 1))})
  1559. " clean up
  1560. call StopVimInTerminal(buf)
  1561. endfunc
  1562. " Test 'showcmd' behavior with a partial mapping
  1563. func Test_showcmd_part_map()
  1564. CheckRunVimInTerminal
  1565. let lines =<< trim END
  1566. set notimeout showcmd
  1567. nnoremap ,a <Ignore>
  1568. nnoremap ;a <Ignore>
  1569. nnoremap Àa <Ignore>
  1570. nnoremap Ëa <Ignore>
  1571. nnoremap βa <Ignore>
  1572. nnoremap ωa <Ignore>
  1573. nnoremap …a <Ignore>
  1574. nnoremap <C-W>a <Ignore>
  1575. END
  1576. call writefile(lines, 'Xtest_showcmd_part_map', 'D')
  1577. let buf = RunVimInTerminal('-S Xtest_showcmd_part_map', #{rows: 6})
  1578. call term_sendkeys(buf, ":set noruler | echo\<CR>")
  1579. call WaitForAssert({-> assert_equal('', term_getline(buf, 6))})
  1580. for c in [',', ';', 'À', 'Ë', 'β', 'ω', '…']
  1581. call term_sendkeys(buf, c)
  1582. call WaitForAssert({-> assert_equal(c, trim(term_getline(buf, 6)))})
  1583. call term_sendkeys(buf, 'a')
  1584. call WaitForAssert({-> assert_equal('', trim(term_getline(buf, 6)))})
  1585. endfor
  1586. call term_sendkeys(buf, "\<C-W>")
  1587. call WaitForAssert({-> assert_equal('^W', trim(term_getline(buf, 6)))})
  1588. call term_sendkeys(buf, 'a')
  1589. call WaitForAssert({-> assert_equal('', trim(term_getline(buf, 6)))})
  1590. " Use feedkeys() as terminal buffer cannot forward unsimplified Ctrl-W.
  1591. " This is like typing Ctrl-W with modifyOtherKeys enabled.
  1592. call term_sendkeys(buf, ':call feedkeys("\<*C-W>", "m")' .. " | echo\<CR>")
  1593. call WaitForAssert({-> assert_equal('^W', trim(term_getline(buf, 6)))})
  1594. call term_sendkeys(buf, 'a')
  1595. call WaitForAssert({-> assert_equal('', trim(term_getline(buf, 6)))})
  1596. call StopVimInTerminal(buf)
  1597. endfunc
  1598. func Test_using_past_typeahead()
  1599. nnoremap :00 0
  1600. exe "norm :set \x80\xfb0=0\<CR>"
  1601. exe "sil norm :0\x0f\<C-U>\<CR>"
  1602. exe "norm :set \x80\xfb0=\<CR>"
  1603. nunmap :00
  1604. endfunc
  1605. " vim: shiftwidth=2 sts=2 expandtab