test_map_functions.vim 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. " Tests for maparg(), mapcheck(), mapset(), maplist()
  2. " Also test utf8 map with a 0x80 byte.
  3. source shared.vim
  4. func s:SID()
  5. return str2nr(matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$'))
  6. endfunc
  7. func Test_maparg()
  8. new
  9. set cpo-=<
  10. set encoding=utf8
  11. " Test maparg() with a string result
  12. let sid = s:SID()
  13. let lnum = expand('<sflnum>')
  14. map foo<C-V> is<F4>foo
  15. vnoremap <script> <buffer> <expr> <silent> bar isbar
  16. call assert_equal("is<F4>foo", maparg('foo<C-V>'))
  17. call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo<C-V>',
  18. \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16",
  19. \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
  20. \ 'lnum': lnum + 1,
  21. \ 'rhs': 'is<F4>foo', 'buffer': 0, 'abbr': 0, 'mode_bits': 0x47},
  22. \ maparg('foo<C-V>', '', 0, 1))
  23. call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar',
  24. \ 'lhsraw': 'bar', 'mode': 'v',
  25. \ 'nowait': 0, 'expr': 1, 'sid': sid, 'scriptversion': 1,
  26. \ 'lnum': lnum + 2,
  27. \ 'rhs': 'isbar', 'buffer': 1, 'abbr': 0, 'mode_bits': 0x42},
  28. \ 'bar'->maparg('', 0, 1))
  29. let lnum = expand('<sflnum>')
  30. map <buffer> <nowait> foo bar
  31. call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo',
  32. \ 'lhsraw': 'foo', 'mode': ' ',
  33. \ 'nowait': 1, 'expr': 0, 'sid': sid, 'scriptversion': 1,
  34. \ 'lnum': lnum + 1, 'rhs': 'bar',
  35. \ 'buffer': 1, 'abbr': 0, 'mode_bits': 0x47},
  36. \ maparg('foo', '', 0, 1))
  37. let lnum = expand('<sflnum>')
  38. tmap baz foo
  39. call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz',
  40. \ 'lhsraw': 'baz', 'mode': 't',
  41. \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
  42. \ 'lnum': lnum + 1, 'rhs': 'foo',
  43. \ 'buffer': 0, 'abbr': 0, 'mode_bits': 0x80},
  44. \ maparg('baz', 't', 0, 1))
  45. let lnum = expand('<sflnum>')
  46. iab A B
  47. call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'A',
  48. \ 'lhsraw': 'A', 'mode': 'i',
  49. \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
  50. \ 'lnum': lnum + 1, 'rhs': 'B',
  51. \ 'buffer': 0, 'abbr': 1, 'mode_bits': 0x0010},
  52. \ maparg('A', 'i', 1, 1))
  53. iuna A
  54. map abc x<char-114>x
  55. call assert_equal("xrx", maparg('abc'))
  56. map abc y<S-char-114>y
  57. call assert_equal("yRy", maparg('abc'))
  58. " character with K_SPECIAL byte
  59. nmap abc …
  60. call assert_equal('…', maparg('abc'))
  61. " modified character with K_SPECIAL byte
  62. nmap abc <M-…>
  63. call assert_equal('<M-…>', maparg('abc'))
  64. " illegal bytes
  65. let str = ":\x7f:\x80:\x90:\xd0:"
  66. exe 'nmap abc ' .. str
  67. call assert_equal(str, maparg('abc'))
  68. unlet str
  69. omap { w
  70. let d = maparg('{', 'o', 0, 1)
  71. call assert_equal(['{', 'w', 'o'], [d.lhs, d.rhs, d.mode])
  72. ounmap {
  73. lmap { w
  74. let d = maparg('{', 'l', 0, 1)
  75. call assert_equal(['{', 'w', 'l'], [d.lhs, d.rhs, d.mode])
  76. lunmap {
  77. nmap { w
  78. let d = maparg('{', 'n', 0, 1)
  79. call assert_equal(['{', 'w', 'n'], [d.lhs, d.rhs, d.mode])
  80. nunmap {
  81. xmap { w
  82. let d = maparg('{', 'x', 0, 1)
  83. call assert_equal(['{', 'w', 'x'], [d.lhs, d.rhs, d.mode])
  84. xunmap {
  85. smap { w
  86. let d = maparg('{', 's', 0, 1)
  87. call assert_equal(['{', 'w', 's'], [d.lhs, d.rhs, d.mode])
  88. sunmap {
  89. map <C-I> foo
  90. unmap <Tab>
  91. " This used to cause a segfault
  92. call maparg('<C-I>', '', 0, 1)
  93. unmap <C-I>
  94. map abc <Nop>
  95. call assert_equal("<Nop>", maparg('abc'))
  96. unmap abc
  97. call feedkeys(":abbr esc \<C-V>\<C-V>\<C-V>\<C-V>\<C-V>\<Esc>\<CR>", "xt")
  98. let d = maparg('esc', 'i', 1, 1)
  99. call assert_equal(['esc', "\<C-V>\<C-V>\<Esc>", '!'], [d.lhs, d.rhs, d.mode])
  100. abclear
  101. unlet d
  102. endfunc
  103. func Test_mapcheck()
  104. call assert_equal('', mapcheck('a'))
  105. call assert_equal('', mapcheck('abc'))
  106. call assert_equal('', mapcheck('ax'))
  107. call assert_equal('', mapcheck('b'))
  108. map a something
  109. call assert_equal('something', mapcheck('a'))
  110. call assert_equal('something', mapcheck('a', 'n'))
  111. call assert_equal('', mapcheck('a', 'c'))
  112. call assert_equal('', mapcheck('a', 'i'))
  113. call assert_equal('something', 'abc'->mapcheck())
  114. call assert_equal('something', 'ax'->mapcheck())
  115. call assert_equal('', mapcheck('b'))
  116. unmap a
  117. map ab foobar
  118. call assert_equal('foobar', mapcheck('a'))
  119. call assert_equal('foobar', mapcheck('abc'))
  120. call assert_equal('', mapcheck('ax'))
  121. call assert_equal('', mapcheck('b'))
  122. unmap ab
  123. map abc barfoo
  124. call assert_equal('barfoo', mapcheck('a'))
  125. call assert_equal('barfoo', mapcheck('a', 'n', 0))
  126. call assert_equal('', mapcheck('a', 'n', 1))
  127. call assert_equal('barfoo', mapcheck('abc'))
  128. call assert_equal('', mapcheck('ax'))
  129. call assert_equal('', mapcheck('b'))
  130. unmap abc
  131. abbr ab abbrev
  132. call assert_equal('abbrev', mapcheck('a', 'i', 1))
  133. call assert_equal('', mapcheck('a', 'n', 1))
  134. call assert_equal('', mapcheck('a', 'i', 0))
  135. unabbr ab
  136. endfunc
  137. func Test_range_map()
  138. new
  139. " Outside of the range, minimum
  140. inoremap <Char-0x1040> a
  141. execute "normal a\u1040\<Esc>"
  142. " Inside of the range, minimum
  143. inoremap <Char-0x103f> b
  144. execute "normal a\u103f\<Esc>"
  145. " Inside of the range, maximum
  146. inoremap <Char-0xf03f> c
  147. execute "normal a\uf03f\<Esc>"
  148. " Outside of the range, maximum
  149. inoremap <Char-0xf040> d
  150. execute "normal a\uf040\<Esc>"
  151. call assert_equal("abcd", getline(1))
  152. endfunc
  153. func One_mapset_test(keys, rhs)
  154. exe 'nnoremap ' .. a:keys .. ' ' .. a:rhs
  155. let orig = maparg(a:keys, 'n', 0, 1)
  156. call assert_equal(a:keys, orig.lhs)
  157. call assert_equal(a:rhs, orig.rhs)
  158. call assert_equal('n', orig.mode)
  159. exe 'nunmap ' .. a:keys
  160. let d = maparg(a:keys, 'n', 0, 1)
  161. call assert_equal({}, d)
  162. call mapset('n', 0, orig)
  163. let d = maparg(a:keys, 'n', 0, 1)
  164. call assert_equal(a:keys, d.lhs)
  165. call assert_equal(a:rhs, d.rhs)
  166. call assert_equal('n', d.mode)
  167. exe 'nunmap ' .. a:keys
  168. endfunc
  169. func Test_mapset()
  170. call One_mapset_test('K', 'original<CR>')
  171. call One_mapset_test('<F3>', 'original<CR>')
  172. call One_mapset_test('<F3>', '<lt>Nop>')
  173. " Check <> key conversion
  174. new
  175. inoremap K one<Left>x
  176. call feedkeys("iK\<Esc>", 'xt')
  177. call assert_equal('onxe', getline(1))
  178. let orig = maparg('K', 'i', 0, 1)
  179. call assert_equal('K', orig.lhs)
  180. call assert_equal('one<Left>x', orig.rhs)
  181. call assert_equal('i', orig.mode)
  182. iunmap K
  183. let d = maparg('K', 'i', 0, 1)
  184. call assert_equal({}, d)
  185. call mapset('i', 0, orig)
  186. call feedkeys("SK\<Esc>", 'xt')
  187. call assert_equal('onxe', getline(1))
  188. iunmap K
  189. " Test that <Nop> is restored properly
  190. inoremap K <Nop>
  191. call feedkeys("SK\<Esc>", 'xt')
  192. call assert_equal('', getline(1))
  193. let orig = maparg('K', 'i', 0, 1)
  194. call assert_equal('K', orig.lhs)
  195. call assert_equal('<Nop>', orig.rhs)
  196. call assert_equal('i', orig.mode)
  197. inoremap K foo
  198. call feedkeys("SK\<Esc>", 'xt')
  199. call assert_equal('foo', getline(1))
  200. call mapset('i', 0, orig)
  201. call feedkeys("SK\<Esc>", 'xt')
  202. call assert_equal('', getline(1))
  203. iunmap K
  204. " Test literal <CR> using a backslash
  205. let cpo_save = &cpo
  206. set cpo-=B
  207. inoremap K one\<CR>two
  208. call feedkeys("SK\<Esc>", 'xt')
  209. call assert_equal('one<CR>two', getline(1))
  210. let orig = maparg('K', 'i', 0, 1)
  211. call assert_equal('K', orig.lhs)
  212. call assert_equal('one\<CR>two', orig.rhs)
  213. call assert_equal('i', orig.mode)
  214. iunmap K
  215. let d = maparg('K', 'i', 0, 1)
  216. call assert_equal({}, d)
  217. call mapset('i', 0, orig)
  218. call feedkeys("SK\<Esc>", 'xt')
  219. call assert_equal('one<CR>two', getline(1))
  220. iunmap K
  221. " Test literal <CR> using CTRL-V
  222. inoremap K one<CR>two
  223. call feedkeys("SK\<Esc>", 'xt')
  224. call assert_equal('one<CR>two', getline(1))
  225. let orig = maparg('K', 'i', 0, 1)
  226. call assert_equal('K', orig.lhs)
  227. call assert_equal("one\x16<CR>two", orig.rhs)
  228. call assert_equal('i', orig.mode)
  229. iunmap K
  230. let d = maparg('K', 'i', 0, 1)
  231. call assert_equal({}, d)
  232. call mapset('i', 0, orig)
  233. call feedkeys("SK\<Esc>", 'xt')
  234. call assert_equal('one<CR>two', getline(1))
  235. iunmap K
  236. let &cpo = cpo_save
  237. bwipe!
  238. call assert_fails('call mapset([], v:false, {})', 'E730:')
  239. call assert_fails('call mapset("i", 0, "")', 'E1206:')
  240. call assert_fails('call mapset("i", 0, {})', 'E460:')
  241. endfunc
  242. func Test_mapset_arg1_dir()
  243. " This test is mostly about get_map_mode_string.
  244. " Once the code gets past that, it's common with the 3 arg mapset.
  245. " GetModes() return list of modes for 'XZ' lhs using maplist.
  246. " There is one list item per mapping
  247. func s:GetModes(abbr = v:false)
  248. return maplist(a:abbr)->filter({_, m -> m.lhs == 'XZ'})
  249. \ ->mapnew({_, m -> m.mode})
  250. endfunc
  251. func s:UnmapAll(lhs)
  252. const unmap_cmds = [ 'unmap', 'unmap!', 'tunmap', 'lunmap' ]
  253. for cmd in unmap_cmds
  254. try | call execute(cmd .. ' ' .. a:lhs) | catch /E31/ | endtry
  255. endfor
  256. endfunc
  257. let tmap = {}
  258. " some mapset(mode, abbr, dict) tests using get_map_mode_str
  259. map XZ x
  260. let tmap = maplist()->filter({_, m -> m.lhs == 'XZ'})[0]->copy()
  261. " this splits the mapping into 2 mappings
  262. call mapset('ox', v:false, tmap)
  263. call assert_equal(2, len(s:GetModes()))
  264. call mapset('o', v:false, tmap)
  265. call assert_equal(3, len(s:GetModes()))
  266. " test that '' acts like ' ', and that the 3 mappings become 1
  267. call mapset('', v:false, tmap)
  268. call assert_equal([' '], s:GetModes())
  269. " dict's mode/abbr are ignored
  270. call s:UnmapAll('XZ')
  271. let tmap.mode = '!'
  272. let tmap.abbr = v:true
  273. call mapset('o', v:false, tmap)
  274. call assert_equal(['o'], s:GetModes())
  275. " test the 3 arg version handles bad mode string, dict not used
  276. call assert_fails("call mapset('vi', v:false, {})", 'E1276:')
  277. " get the abbreviations out of the way
  278. abbreviate XZ ZX
  279. let tmap = maplist(v:true)->filter({_, m -> m.lhs == 'XZ'})[0]->copy()
  280. abclear
  281. " 'ic' is the default ab command, shows up as '!'
  282. let tmap.mode = 'ic'
  283. call mapset(tmap)
  284. call assert_equal(['!'], s:GetModes(v:true))
  285. abclear
  286. let tmap.mode = 'i'
  287. call mapset(tmap)
  288. call assert_equal(['i'], s:GetModes(v:true))
  289. abclear
  290. let tmap.mode = 'c'
  291. call mapset(tmap)
  292. call assert_equal(['c'], s:GetModes(v:true))
  293. abclear
  294. let tmap.mode = '!'
  295. call mapset(tmap)
  296. call assert_equal(['!'], s:GetModes(v:true))
  297. call assert_fails("call mapset(#{mode: ' !', abbr: 1})", 'E1276:')
  298. call assert_fails("call mapset(#{mode: 'cl', abbr: 1})", 'E1276:')
  299. call assert_fails("call mapset(#{mode: 'in', abbr: 1})", 'E1276:')
  300. " the map commands
  301. map XZ x
  302. let tmap = maplist()->filter({_, m -> m.lhs == 'XZ'})[0]->copy()
  303. " try the combos
  304. call s:UnmapAll('XZ')
  305. " 'nxso' is ' ', the unadorned :map
  306. let tmap.mode = 'nxso'
  307. call mapset(tmap)
  308. call assert_equal([' '], s:GetModes())
  309. cal s:UnmapAll('XZ')
  310. " 'ic' is '!'
  311. let tmap.mode = 'ic'
  312. call mapset(tmap)
  313. call assert_equal(['!'], s:GetModes())
  314. call s:UnmapAll('XZ')
  315. " 'xs' is really 'v'
  316. let tmap.mode = 'xs'
  317. call mapset(tmap)
  318. call assert_equal(['v'], s:GetModes())
  319. " try the individual modes
  320. call s:UnmapAll('XZ')
  321. let tmap.mode = 'n'
  322. call mapset(tmap)
  323. call assert_equal(['n'], s:GetModes())
  324. call s:UnmapAll('XZ')
  325. let tmap.mode = 'x'
  326. call mapset(tmap)
  327. call assert_equal(['x'], s:GetModes())
  328. call s:UnmapAll('XZ')
  329. let tmap.mode = 's'
  330. call mapset(tmap)
  331. call assert_equal(['s'], s:GetModes())
  332. call s:UnmapAll('XZ')
  333. let tmap.mode = 'o'
  334. call mapset(tmap)
  335. call assert_equal(['o'], s:GetModes())
  336. call s:UnmapAll('XZ')
  337. let tmap.mode = 'i'
  338. call mapset(tmap)
  339. call assert_equal(['i'], s:GetModes())
  340. call s:UnmapAll('XZ')
  341. let tmap.mode = 'c'
  342. call mapset(tmap)
  343. call assert_equal(['c'], s:GetModes())
  344. call s:UnmapAll('XZ')
  345. let tmap.mode = 't'
  346. call mapset(tmap)
  347. call assert_equal(['t'], s:GetModes())
  348. call s:UnmapAll('XZ')
  349. let tmap.mode = 'l'
  350. call mapset(tmap)
  351. call assert_equal(['l'], s:GetModes())
  352. call s:UnmapAll('XZ')
  353. " get errors for modes that can't be in one mapping
  354. call assert_fails("call mapset(#{mode: 'nxsoi', abbr: 0})", 'E1276:')
  355. call assert_fails("call mapset(#{mode: ' !', abbr: 0})", 'E1276:')
  356. call assert_fails("call mapset(#{mode: 'ix', abbr: 0})", 'E1276:')
  357. call assert_fails("call mapset(#{mode: 'tl', abbr: 0})", 'E1276:')
  358. call assert_fails("call mapset(#{mode: ' l', abbr: 0})", 'E1276:')
  359. call assert_fails("call mapset(#{mode: ' t', abbr: 0})", 'E1276:')
  360. endfunc
  361. func Check_ctrlb_map(d, check_alt)
  362. call assert_equal('<C-B>', a:d.lhs)
  363. if a:check_alt
  364. call assert_equal("\x80\xfc\x04B", a:d.lhsraw)
  365. call assert_equal("\x02", a:d.lhsrawalt)
  366. else
  367. call assert_equal("\x02", a:d.lhsraw)
  368. endif
  369. endfunc
  370. func Test_map_local()
  371. nmap a global
  372. nmap <buffer>a local
  373. let prev_map_list = split(execute('nmap a'), "\n")
  374. call assert_match('n\s*a\s*@local', prev_map_list[0])
  375. call assert_match('n\s*a\s*global', prev_map_list[1])
  376. let mapping = maparg('a', 'n', 0, 1)
  377. call assert_equal(1, mapping.buffer)
  378. let mapping.rhs = 'new_local'
  379. call mapset('n', 0, mapping)
  380. " Check that the global mapping is left untouched.
  381. let map_list = split(execute('nmap a'), "\n")
  382. call assert_match('n\s*a\s*@new_local', map_list[0])
  383. call assert_match('n\s*a\s*global', map_list[1])
  384. nunmap a
  385. endfunc
  386. func Test_map_restore()
  387. " Test restoring map with alternate keycode
  388. nmap <C-B> back
  389. let d = maparg('<C-B>', 'n', 0, 1)
  390. call Check_ctrlb_map(d, 1)
  391. let dsimp = maparg("\x02", 'n', 0, 1)
  392. call Check_ctrlb_map(dsimp, 0)
  393. nunmap <C-B>
  394. call mapset('n', 0, d)
  395. let d = maparg('<C-B>', 'n', 0, 1)
  396. call Check_ctrlb_map(d, 1)
  397. let dsimp = maparg("\x02", 'n', 0, 1)
  398. call Check_ctrlb_map(dsimp, 0)
  399. nunmap <C-B>
  400. endfunc
  401. " Test restoring an <SID> mapping
  402. func Test_map_restore_sid()
  403. func RestoreMap()
  404. const d = maparg('<CR>', 'i', v:false, v:true)
  405. iunmap <buffer> <CR>
  406. call mapset('i', v:false, d)
  407. endfunc
  408. let mapscript =<< trim [CODE]
  409. inoremap <silent><buffer> <SID>Return <C-R>=42<CR>
  410. inoremap <script><buffer> <CR> <CR><SID>Return
  411. [CODE]
  412. call writefile(mapscript, 'Xmapscript', 'D')
  413. new
  414. source Xmapscript
  415. inoremap <buffer> <C-B> <Cmd>call RestoreMap()<CR>
  416. call feedkeys("i\<CR>\<*C-B>\<CR>", 'xt')
  417. call assert_equal(['', '42', '42'], getline(1, '$'))
  418. bwipe!
  419. delfunc RestoreMap
  420. endfunc
  421. " Test restoring a mapping with a negative script ID
  422. func Test_map_restore_negative_sid()
  423. let after =<< trim [CODE]
  424. call assert_equal("\tLast set from --cmd argument",
  425. \ execute('verbose nmap ,n')->trim()->split("\n")[-1])
  426. let d = maparg(',n', 'n', 0, 1)
  427. nunmap ,n
  428. call assert_equal('No mapping found',
  429. \ execute('verbose nmap ,n')->trim()->split("\n")[-1])
  430. call mapset('n', 0, d)
  431. call assert_equal("\tLast set from --cmd argument",
  432. \ execute('verbose nmap ,n')->trim()->split("\n")[-1])
  433. call writefile(v:errors, 'Xresult')
  434. qall!
  435. [CODE]
  436. if RunVim([], after, '--clean --cmd "nmap ,n <Nop>"')
  437. call assert_equal([], readfile('Xresult'))
  438. endif
  439. call delete('Xresult')
  440. endfunc
  441. " Check that restoring a mapping doesn't remove a mapping whose {rhs} matches
  442. " the restored mapping's {lhs}.
  443. func Test_map_restore_with_rhs_match_lhs()
  444. nnoremap <F2> <F3>
  445. nnoremap <F3> <F4>
  446. call assert_equal('<F3>', maparg('<F2>', 'n'))
  447. call assert_equal('<F4>', maparg('<F3>', 'n'))
  448. let d = maparg('<F3>', 'n', v:false, v:true)
  449. nunmap <F3>
  450. call assert_equal('<F3>', maparg('<F2>', 'n'))
  451. call assert_equal('', maparg('<F3>', 'n'))
  452. call mapset(d)
  453. call assert_equal('<F3>', maparg('<F2>', 'n'))
  454. call assert_equal('<F4>', maparg('<F3>', 'n'))
  455. nunmap <F2>
  456. nunmap <F3>
  457. endfunc
  458. func Test_maplist()
  459. new
  460. func s:ClearMappingsAbbreviations()
  461. mapclear | nmapclear | vmapclear | xmapclear | smapclear | omapclear
  462. mapclear! | imapclear | lmapclear | cmapclear | tmapclear
  463. mapclear <buffer> | nmapclear <buffer> | vmapclear <buffer>
  464. xmapclear <buffer> | smapclear <buffer> | omapclear <buffer>
  465. mapclear! <buffer> | imapclear <buffer> | lmapclear <buffer>
  466. cmapclear <buffer> | tmapclear <buffer>
  467. abclear | abclear <buffer>
  468. endfunc
  469. func s:AddMaps(new, accum)
  470. if len(a:new) > 0 && a:new[0] != "No mapping found"
  471. eval a:accum->extend(a:new)
  472. endif
  473. endfunc
  474. call s:ClearMappingsAbbreviations()
  475. call assert_equal(0, len(maplist()))
  476. call assert_equal(0, len(maplist(v:true)))
  477. " Set up some mappings.
  478. map dup bar
  479. map <buffer> dup bufbar
  480. map foo<C-V> is<F4>foo
  481. vnoremap <script> <buffer> <expr> <silent> bar isbar
  482. tmap baz foo
  483. omap h w
  484. lmap i w
  485. nmap j w
  486. xmap k w
  487. smap l w
  488. map abc <Nop>
  489. nmap <M-j> x
  490. nmap <M-Space> y
  491. " And abbreviations
  492. abbreviate xy he
  493. abbreviate xx she
  494. abbreviate <buffer> x they
  495. " Get a list of the mappings with the ':map' commands.
  496. " Check maplist() return a list of the same size.
  497. call assert_equal(13, len(maplist()))
  498. call assert_equal(3, len(maplist(v:true)))
  499. call assert_equal(13, len(maplist(v:false)))
  500. " collect all the current maps using :map commands
  501. let maps_command = []
  502. call s:AddMaps(split(execute('map'), '\n'), maps_command)
  503. call s:AddMaps(split(execute('map!'), '\n'), maps_command)
  504. call s:AddMaps(split(execute('tmap'), '\n'), maps_command)
  505. call s:AddMaps(split(execute('lmap'), '\n'), maps_command)
  506. " Use maplist to get all the maps
  507. let maps_maplist = maplist()
  508. call assert_equal(len(maps_command), len(maps_maplist))
  509. " make sure all the mode-lhs are unique, no duplicates
  510. let map_set = {}
  511. for d in maps_maplist
  512. let map_set[d.mode .. "-" .. d.lhs .. "-" .. d.buffer] = 0
  513. endfor
  514. call assert_equal(len(maps_maplist), len(map_set))
  515. " For everything returned by maplist, should be the same as from maparg.
  516. " Except for "map dup", because maparg returns the <buffer> version
  517. for d in maps_maplist
  518. if d.lhs == 'dup' && d.buffer == 0
  519. continue
  520. endif
  521. let d_maparg = maparg(d.lhs, d.mode, v:false, v:true)
  522. call assert_equal(d_maparg, d)
  523. endfor
  524. " Check abbr matches maparg
  525. for d in maplist(v:true)
  526. " Note, d.mode is '!', but can't use that with maparg
  527. let d_maparg = maparg(d.lhs, 'i', v:true, v:true)
  528. call assert_equal(d_maparg, d)
  529. endfor
  530. call s:ClearMappingsAbbreviations()
  531. call assert_equal(0, len(maplist()))
  532. call assert_equal(0, len(maplist(v:true)))
  533. endfunc
  534. " vim: shiftwidth=2 sts=2 expandtab