blob: d8c079c84e5696106bdf4dfb276b19e24aedffe7 [file] [log] [blame]
Ernie Rael09661202022-04-25 14:40:44 +01001" Tests for maparg(), mapcheck(), mapset(), maplist()
Bram Moolenaar292eff02017-07-11 21:46:28 +02002" Also test utf8 map with a 0x80 byte.
Bram Moolenaar292eff02017-07-11 21:46:28 +02003
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02004func s:SID()
Bram Moolenaar292eff02017-07-11 21:46:28 +02005 return str2nr(matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$'))
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02006endfunc
Bram Moolenaar292eff02017-07-11 21:46:28 +02007
Bram Moolenaarc94c1462020-05-22 20:01:06 +02008func Test_maparg()
Bram Moolenaar292eff02017-07-11 21:46:28 +02009 new
10 set cpo-=<
11 set encoding=utf8
12 " Test maparg() with a string result
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020013 let sid = s:SID()
14 let lnum = expand('<sflnum>')
Bram Moolenaar292eff02017-07-11 21:46:28 +020015 map foo<C-V> is<F4>foo
16 vnoremap <script> <buffer> <expr> <silent> bar isbar
Bram Moolenaar292eff02017-07-11 21:46:28 +020017 call assert_equal("is<F4>foo", maparg('foo<C-V>'))
Bram Moolenaar2da0f0c2020-04-01 19:22:12 +020018 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo<C-V>',
Bram Moolenaar9c652532020-05-24 13:10:18 +020019 \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16",
Bram Moolenaara9528b32022-01-18 20:51:35 +000020 \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
21 \ 'lnum': lnum + 1,
Ernie Raeld8f5f762022-05-10 17:50:39 +010022 \ 'rhs': 'is<F4>foo', 'buffer': 0, 'abbr': 0, 'mode_bits': 0x47},
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020023 \ maparg('foo<C-V>', '', 0, 1))
Bram Moolenaar9c652532020-05-24 13:10:18 +020024 call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar',
25 \ 'lhsraw': 'bar', 'mode': 'v',
Bram Moolenaara9528b32022-01-18 20:51:35 +000026 \ 'nowait': 0, 'expr': 1, 'sid': sid, 'scriptversion': 1,
27 \ 'lnum': lnum + 2,
Ernie Raeld8f5f762022-05-10 17:50:39 +010028 \ 'rhs': 'isbar', 'buffer': 1, 'abbr': 0, 'mode_bits': 0x42},
Bram Moolenaara1449832019-09-01 20:16:52 +020029 \ 'bar'->maparg('', 0, 1))
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020030 let lnum = expand('<sflnum>')
Bram Moolenaar292eff02017-07-11 21:46:28 +020031 map <buffer> <nowait> foo bar
Bram Moolenaar9c652532020-05-24 13:10:18 +020032 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo',
33 \ 'lhsraw': 'foo', 'mode': ' ',
Bram Moolenaara9528b32022-01-18 20:51:35 +000034 \ 'nowait': 1, 'expr': 0, 'sid': sid, 'scriptversion': 1,
35 \ 'lnum': lnum + 1, 'rhs': 'bar',
Ernie Raeld8f5f762022-05-10 17:50:39 +010036 \ 'buffer': 1, 'abbr': 0, 'mode_bits': 0x47},
Bram Moolenaar292eff02017-07-11 21:46:28 +020037 \ maparg('foo', '', 0, 1))
Bram Moolenaar14371ed2019-07-27 21:05:21 +020038 let lnum = expand('<sflnum>')
39 tmap baz foo
Bram Moolenaar9c652532020-05-24 13:10:18 +020040 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz',
41 \ 'lhsraw': 'baz', 'mode': 't',
Bram Moolenaara9528b32022-01-18 20:51:35 +000042 \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
43 \ 'lnum': lnum + 1, 'rhs': 'foo',
Ernie Raeld8f5f762022-05-10 17:50:39 +010044 \ 'buffer': 0, 'abbr': 0, 'mode_bits': 0x80},
Bram Moolenaar14371ed2019-07-27 21:05:21 +020045 \ maparg('baz', 't', 0, 1))
Ernie Rael51d04d12022-05-04 15:40:22 +010046 let lnum = expand('<sflnum>')
47 iab A B
48 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'A',
49 \ 'lhsraw': 'A', 'mode': 'i',
50 \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
51 \ 'lnum': lnum + 1, 'rhs': 'B',
Ernie Raeld8f5f762022-05-10 17:50:39 +010052 \ 'buffer': 0, 'abbr': 1, 'mode_bits': 0x0010},
Ernie Rael51d04d12022-05-04 15:40:22 +010053 \ maparg('A', 'i', 1, 1))
54 iuna A
Bram Moolenaar292eff02017-07-11 21:46:28 +020055
56 map abc x<char-114>x
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020057 call assert_equal("xrx", maparg('abc'))
Bram Moolenaar292eff02017-07-11 21:46:28 +020058 map abc y<S-char-114>y
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020059 call assert_equal("yRy", maparg('abc'))
60
zeertzjq0519ce02022-05-09 12:16:19 +010061 " character with K_SPECIAL byte
62 nmap abc
63 call assert_equal('…', maparg('abc'))
64
65 " modified character with K_SPECIAL byte
66 nmap abc <M-…>
67 call assert_equal('<M-…>', maparg('abc'))
68
69 " illegal bytes
70 let str = ":\x7f:\x80:\x90:\xd0:"
71 exe 'nmap abc ' .. str
72 call assert_equal(str, maparg('abc'))
73 unlet str
74
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +010075 omap { w
76 let d = maparg('{', 'o', 0, 1)
77 call assert_equal(['{', 'w', 'o'], [d.lhs, d.rhs, d.mode])
78 ounmap {
79
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +010080 lmap { w
81 let d = maparg('{', 'l', 0, 1)
82 call assert_equal(['{', 'w', 'l'], [d.lhs, d.rhs, d.mode])
83 lunmap {
84
85 nmap { w
86 let d = maparg('{', 'n', 0, 1)
87 call assert_equal(['{', 'w', 'n'], [d.lhs, d.rhs, d.mode])
88 nunmap {
89
90 xmap { w
91 let d = maparg('{', 'x', 0, 1)
92 call assert_equal(['{', 'w', 'x'], [d.lhs, d.rhs, d.mode])
93 xunmap {
94
95 smap { w
96 let d = maparg('{', 's', 0, 1)
97 call assert_equal(['{', 'w', 's'], [d.lhs, d.rhs, d.mode])
98 sunmap {
99
zeertzjq2c8a7eb2022-04-26 21:36:21 +0100100 map <C-I> foo
101 unmap <Tab>
102 " This used to cause a segfault
103 call maparg('<C-I>', '', 0, 1)
104 unmap <C-I>
105
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +0200106 map abc <Nop>
107 call assert_equal("<Nop>", maparg('abc'))
108 unmap abc
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +0100109
110 call feedkeys(":abbr esc \<C-V>\<C-V>\<C-V>\<C-V>\<C-V>\<Esc>\<CR>", "xt")
111 let d = maparg('esc', 'i', 1, 1)
112 call assert_equal(['esc', "\<C-V>\<C-V>\<Esc>", '!'], [d.lhs, d.rhs, d.mode])
113 abclear
zeertzjq2c8a7eb2022-04-26 21:36:21 +0100114 unlet d
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200115endfunc
Bram Moolenaar292eff02017-07-11 21:46:28 +0200116
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200117def Test_vim9_maparg()
118 nmap { w
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200119 var one: string = maparg('{')
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200120 assert_equal('w', one)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200121 var two: string = maparg('{', 'n')
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200122 assert_equal('w', two)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200123 var three: string = maparg('{', 'n', 0)
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200124 assert_equal('w', three)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200125 var four: dict<any> = maparg('{', 'n', 0, 1)
Bram Moolenaard2c61702020-09-06 15:58:36 +0200126 assert_equal(['{', 'w', 'n'], [four.lhs, four.rhs, four.mode])
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200127 nunmap {
128enddef
129
Bram Moolenaara1449832019-09-01 20:16:52 +0200130func Test_mapcheck()
131 call assert_equal('', mapcheck('a'))
132 call assert_equal('', mapcheck('abc'))
133 call assert_equal('', mapcheck('ax'))
134 call assert_equal('', mapcheck('b'))
135
136 map a something
137 call assert_equal('something', mapcheck('a'))
138 call assert_equal('something', mapcheck('a', 'n'))
139 call assert_equal('', mapcheck('a', 'c'))
140 call assert_equal('', mapcheck('a', 'i'))
141 call assert_equal('something', 'abc'->mapcheck())
142 call assert_equal('something', 'ax'->mapcheck())
143 call assert_equal('', mapcheck('b'))
144 unmap a
145
146 map ab foobar
147 call assert_equal('foobar', mapcheck('a'))
148 call assert_equal('foobar', mapcheck('abc'))
149 call assert_equal('', mapcheck('ax'))
150 call assert_equal('', mapcheck('b'))
151 unmap ab
152
153 map abc barfoo
154 call assert_equal('barfoo', mapcheck('a'))
155 call assert_equal('barfoo', mapcheck('a', 'n', 0))
156 call assert_equal('', mapcheck('a', 'n', 1))
157 call assert_equal('barfoo', mapcheck('abc'))
158 call assert_equal('', mapcheck('ax'))
159 call assert_equal('', mapcheck('b'))
160 unmap abc
161
162 abbr ab abbrev
163 call assert_equal('abbrev', mapcheck('a', 'i', 1))
164 call assert_equal('', mapcheck('a', 'n', 1))
165 call assert_equal('', mapcheck('a', 'i', 0))
166 unabbr ab
167endfunc
168
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200169func Test_range_map()
Bram Moolenaar292eff02017-07-11 21:46:28 +0200170 new
171 " Outside of the range, minimum
172 inoremap <Char-0x1040> a
173 execute "normal a\u1040\<Esc>"
174 " Inside of the range, minimum
175 inoremap <Char-0x103f> b
176 execute "normal a\u103f\<Esc>"
177 " Inside of the range, maximum
178 inoremap <Char-0xf03f> c
179 execute "normal a\uf03f\<Esc>"
180 " Outside of the range, maximum
181 inoremap <Char-0xf040> d
182 execute "normal a\uf040\<Esc>"
183 call assert_equal("abcd", getline(1))
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200184endfunc
185
186func One_mapset_test(keys)
187 exe 'nnoremap ' .. a:keys .. ' original<CR>'
188 let orig = maparg(a:keys, 'n', 0, 1)
189 call assert_equal(a:keys, orig.lhs)
190 call assert_equal('original<CR>', orig.rhs)
191 call assert_equal('n', orig.mode)
192
193 exe 'nunmap ' .. a:keys
194 let d = maparg(a:keys, 'n', 0, 1)
195 call assert_equal({}, d)
196
197 call mapset('n', 0, orig)
198 let d = maparg(a:keys, 'n', 0, 1)
199 call assert_equal(a:keys, d.lhs)
200 call assert_equal('original<CR>', d.rhs)
201 call assert_equal('n', d.mode)
202
203 exe 'nunmap ' .. a:keys
204endfunc
205
206func Test_mapset()
207 call One_mapset_test('K')
208 call One_mapset_test('<F3>')
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200209
210 " Check <> key conversion
211 new
212 inoremap K one<Left>x
213 call feedkeys("iK\<Esc>", 'xt')
214 call assert_equal('onxe', getline(1))
215
216 let orig = maparg('K', 'i', 0, 1)
217 call assert_equal('K', orig.lhs)
218 call assert_equal('one<Left>x', orig.rhs)
219 call assert_equal('i', orig.mode)
220
221 iunmap K
222 let d = maparg('K', 'i', 0, 1)
223 call assert_equal({}, d)
224
225 call mapset('i', 0, orig)
226 call feedkeys("SK\<Esc>", 'xt')
227 call assert_equal('onxe', getline(1))
228
229 iunmap K
230
231 " Test literal <CR> using a backslash
232 let cpo_save = &cpo
233 set cpo-=B
234 inoremap K one\<CR>two
235 call feedkeys("SK\<Esc>", 'xt')
236 call assert_equal('one<CR>two', getline(1))
237
238 let orig = maparg('K', 'i', 0, 1)
239 call assert_equal('K', orig.lhs)
240 call assert_equal('one\<CR>two', orig.rhs)
241 call assert_equal('i', orig.mode)
242
243 iunmap K
244 let d = maparg('K', 'i', 0, 1)
245 call assert_equal({}, d)
246
247 call mapset('i', 0, orig)
248 call feedkeys("SK\<Esc>", 'xt')
249 call assert_equal('one<CR>two', getline(1))
250
251 iunmap K
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200252
253 " Test literal <CR> using CTRL-V
254 inoremap K one<CR>two
255 call feedkeys("SK\<Esc>", 'xt')
256 call assert_equal('one<CR>two', getline(1))
257
258 let orig = maparg('K', 'i', 0, 1)
259 call assert_equal('K', orig.lhs)
260 call assert_equal("one\x16<CR>two", orig.rhs)
261 call assert_equal('i', orig.mode)
262
263 iunmap K
264 let d = maparg('K', 'i', 0, 1)
265 call assert_equal({}, d)
266
267 call mapset('i', 0, orig)
268 call feedkeys("SK\<Esc>", 'xt')
269 call assert_equal('one<CR>two', getline(1))
270
271 iunmap K
272 let &cpo = cpo_save
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200273 bwipe!
Bram Moolenaar1b912982020-09-29 21:45:41 +0200274
275 call assert_fails('call mapset([], v:false, {})', 'E730:')
Ernie Rael51d04d12022-05-04 15:40:22 +0100276 call assert_fails('call mapset("i", 0, "")', 'E715:')
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100277 call assert_fails('call mapset("i", 0, {})', 'E460:')
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200278endfunc
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100279
Ernie Rael51d04d12022-05-04 15:40:22 +0100280def Test_mapset_arg1_dir()
281 # This test is mostly about get_map_mode_string.
282 # Once the code gets past that, it's common with the 3 arg mapset.
283
284 # GetModes() return list of modes for 'XZ' lhs using maplist.
285 # There is one list item per mapping
286 def GetModes(abbr: bool = false): list<string>
287 return maplist(abbr)->filter((_, m) => m.lhs == 'XZ')
288 ->mapnew((_, m) => m.mode)
289 enddef
290
291 const unmap_cmds = [ 'unmap', 'unmap!', 'tunmap', 'lunmap' ]
292 def UnmapAll(lhs: string)
293 for cmd in unmap_cmds
294 try | execute(cmd .. ' ' .. lhs) | catch /E31/ | endtry
295 endfor
296 enddef
297
298 var tmap: dict<any>
299
300 # some mapset(mode, abbr, dict) tests using get_map_mode_str
301 map XZ x
302 tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy()
303 # this splits the mapping into 2 mappings
304 mapset('ox', false, tmap)
305 assert_equal(2, len(GetModes()))
306 mapset('o', false, tmap)
307 assert_equal(3, len(GetModes()))
308 # test that '' acts like ' ', and that the 3 mappings become 1
309 mapset('', false, tmap)
310 assert_equal([' '], GetModes())
311 # dict's mode/abbr are ignored
312 UnmapAll('XZ')
313 tmap.mode = '!'
314 tmap.abbr = true
315 mapset('o', false, tmap)
316 assert_equal(['o'], GetModes())
317
318 # test the 3 arg version handles bad mode string, dict not used
319 assert_fails("mapset('vi', false, {})", 'E1276:')
320
321
322 # get the abbreviations out of the way
323 abbreviate XZ ZX
324 tmap = maplist(true)->filter((_, m) => m.lhs == 'XZ')[0]->copy()
325
326 abclear
327 # 'ic' is the default ab command, shows up as '!'
328 tmap.mode = 'ic'
329 mapset(tmap)
330 assert_equal(['!'], GetModes(true))
331
332 abclear
333 tmap.mode = 'i'
334 mapset(tmap)
335 assert_equal(['i'], GetModes(true))
336
337 abclear
338 tmap.mode = 'c'
339 mapset(tmap)
340 assert_equal(['c'], GetModes(true))
341
342 abclear
343 tmap.mode = '!'
344 mapset(tmap)
345 assert_equal(['!'], GetModes(true))
346
347 assert_fails("mapset({mode: ' !', abbr: 1})", 'E1276:')
348 assert_fails("mapset({mode: 'cl', abbr: 1})", 'E1276:')
349 assert_fails("mapset({mode: 'in', abbr: 1})", 'E1276:')
350
351 # the map commands
352 map XZ x
353 tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy()
354
355 # try the combos
356 UnmapAll('XZ')
357 # 'nxso' is ' ', the unadorned :map
358 tmap.mode = 'nxso'
359 mapset(tmap)
360 assert_equal([' '], GetModes())
361
362 UnmapAll('XZ')
363 # 'ic' is '!'
364 tmap.mode = 'ic'
365 mapset(tmap)
366 assert_equal(['!'], GetModes())
367
368 UnmapAll('XZ')
369 # 'xs' is really 'v'
370 tmap.mode = 'xs'
371 mapset(tmap)
372 assert_equal(['v'], GetModes())
373
374 # try the individual modes
375 UnmapAll('XZ')
376 tmap.mode = 'n'
377 mapset(tmap)
378 assert_equal(['n'], GetModes())
379
380 UnmapAll('XZ')
381 tmap.mode = 'x'
382 mapset(tmap)
383 assert_equal(['x'], GetModes())
384
385 UnmapAll('XZ')
386 tmap.mode = 's'
387 mapset(tmap)
388 assert_equal(['s'], GetModes())
389
390 UnmapAll('XZ')
391 tmap.mode = 'o'
392 mapset(tmap)
393 assert_equal(['o'], GetModes())
394
395 UnmapAll('XZ')
396 tmap.mode = 'i'
397 mapset(tmap)
398 assert_equal(['i'], GetModes())
399
400 UnmapAll('XZ')
401 tmap.mode = 'c'
402 mapset(tmap)
403 assert_equal(['c'], GetModes())
404
405 UnmapAll('XZ')
406 tmap.mode = 't'
407 mapset(tmap)
408 assert_equal(['t'], GetModes())
409
410 UnmapAll('XZ')
411 tmap.mode = 'l'
412 mapset(tmap)
413 assert_equal(['l'], GetModes())
414
415 UnmapAll('XZ')
416
417 # get errors for modes that can't be in one mapping
418 assert_fails("mapset({mode: 'nxsoi', abbr: 0})", 'E1276:')
419 assert_fails("mapset({mode: ' !', abbr: 0})", 'E1276:')
420 assert_fails("mapset({mode: 'ix', abbr: 0})", 'E1276:')
421 assert_fails("mapset({mode: 'tl', abbr: 0})", 'E1276:')
422 assert_fails("mapset({mode: ' l', abbr: 0})", 'E1276:')
423 assert_fails("mapset({mode: ' t', abbr: 0})", 'E1276:')
424enddef
425
Bram Moolenaar9c652532020-05-24 13:10:18 +0200426func Check_ctrlb_map(d, check_alt)
427 call assert_equal('<C-B>', a:d.lhs)
428 if a:check_alt
429 call assert_equal("\x80\xfc\x04B", a:d.lhsraw)
430 call assert_equal("\x02", a:d.lhsrawalt)
431 else
432 call assert_equal("\x02", a:d.lhsraw)
433 endif
434endfunc
435
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +0200436func Test_map_local()
437 nmap a global
438 nmap <buffer>a local
439
440 let prev_map_list = split(execute('nmap a'), "\n")
441 call assert_match('n\s*a\s*@local', prev_map_list[0])
442 call assert_match('n\s*a\s*global', prev_map_list[1])
443
444 let mapping = maparg('a', 'n', 0, 1)
445 call assert_equal(1, mapping.buffer)
446 let mapping.rhs = 'new_local'
447 call mapset('n', 0, mapping)
448
449 " Check that the global mapping is left untouched.
450 let map_list = split(execute('nmap a'), "\n")
451 call assert_match('n\s*a\s*@new_local', map_list[0])
452 call assert_match('n\s*a\s*global', map_list[1])
453
454 nunmap a
455endfunc
456
Bram Moolenaar9c652532020-05-24 13:10:18 +0200457func Test_map_restore()
458 " Test restoring map with alternate keycode
459 nmap <C-B> back
460 let d = maparg('<C-B>', 'n', 0, 1)
461 call Check_ctrlb_map(d, 1)
462 let dsimp = maparg("\x02", 'n', 0, 1)
463 call Check_ctrlb_map(dsimp, 0)
464 nunmap <C-B>
465 call mapset('n', 0, d)
466 let d = maparg('<C-B>', 'n', 0, 1)
467 call Check_ctrlb_map(d, 1)
468 let dsimp = maparg("\x02", 'n', 0, 1)
469 call Check_ctrlb_map(dsimp, 0)
470
471 nunmap <C-B>
472
473endfunc
474
Ernie Rael09661202022-04-25 14:40:44 +0100475def Test_maplist()
Ernie Rael659c2402022-04-24 18:40:28 +0100476 new
Ernie Rael09661202022-04-25 14:40:44 +0100477 def ClearMappingsAbbreviations()
Ernie Rael659c2402022-04-24 18:40:28 +0100478 mapclear | nmapclear | vmapclear | xmapclear | smapclear | omapclear
479 mapclear! | imapclear | lmapclear | cmapclear | tmapclear
480 mapclear <buffer> | nmapclear <buffer> | vmapclear <buffer>
481 xmapclear <buffer> | smapclear <buffer> | omapclear <buffer>
482 mapclear! <buffer> | imapclear <buffer> | lmapclear <buffer>
483 cmapclear <buffer> | tmapclear <buffer>
Ernie Rael09661202022-04-25 14:40:44 +0100484 abclear | abclear <buffer>
Ernie Rael659c2402022-04-24 18:40:28 +0100485 enddef
486
487 def AddMaps(new: list<string>, accum: list<string>)
488 if len(new) > 0 && new[0] != "No mapping found"
489 accum->extend(new)
490 endif
491 enddef
492
Ernie Rael09661202022-04-25 14:40:44 +0100493 ClearMappingsAbbreviations()
494 assert_equal(0, len(maplist()))
495 assert_equal(0, len(maplist(true)))
Ernie Rael659c2402022-04-24 18:40:28 +0100496
497 # Set up some mappings.
498 map dup bar
499 map <buffer> dup bufbar
500 map foo<C-V> is<F4>foo
501 vnoremap <script> <buffer> <expr> <silent> bar isbar
502 tmap baz foo
503 omap h w
504 lmap i w
505 nmap j w
506 xmap k w
507 smap l w
508 map abc <Nop>
509 nmap <M-j> x
510 nmap <M-Space> y
Ernie Rael09661202022-04-25 14:40:44 +0100511 # And abbreviations
512 abbreviate xy he
513 abbreviate xx she
514 abbreviate <buffer> x they
Ernie Rael659c2402022-04-24 18:40:28 +0100515
516 # Get a list of the mappings with the ':map' commands.
Ernie Rael09661202022-04-25 14:40:44 +0100517 # Check maplist() return a list of the same size.
518 assert_equal(13, len(maplist()))
519 assert_equal(3, len(maplist(true)))
520 assert_equal(13, len(maplist(false)))
Ernie Rael659c2402022-04-24 18:40:28 +0100521
522 # collect all the current maps using :map commands
523 var maps_command: list<string>
524 AddMaps(split(execute('map'), '\n'), maps_command)
525 AddMaps(split(execute('map!'), '\n'), maps_command)
526 AddMaps(split(execute('tmap'), '\n'), maps_command)
527 AddMaps(split(execute('lmap'), '\n'), maps_command)
528
Ernie Rael09661202022-04-25 14:40:44 +0100529 # Use maplist to get all the maps
530 var maps_maplist = maplist()
531 assert_equal(len(maps_command), len(maps_maplist))
Ernie Rael659c2402022-04-24 18:40:28 +0100532
533 # make sure all the mode-lhs are unique, no duplicates
534 var map_set: dict<number>
Ernie Rael09661202022-04-25 14:40:44 +0100535 for d in maps_maplist
Ernie Rael659c2402022-04-24 18:40:28 +0100536 map_set[d.mode .. "-" .. d.lhs .. "-" .. d.buffer] = 0
537 endfor
Ernie Rael09661202022-04-25 14:40:44 +0100538 assert_equal(len(maps_maplist), len(map_set))
Ernie Rael659c2402022-04-24 18:40:28 +0100539
Ernie Rael09661202022-04-25 14:40:44 +0100540 # For everything returned by maplist, should be the same as from maparg.
Ernie Rael659c2402022-04-24 18:40:28 +0100541 # Except for "map dup", bacause maparg returns the <buffer> version
Ernie Rael09661202022-04-25 14:40:44 +0100542 for d in maps_maplist
Ernie Rael659c2402022-04-24 18:40:28 +0100543 if d.lhs == 'dup' && d.buffer == 0
544 continue
545 endif
546 var d_maparg = maparg(d.lhs, d.mode, false, true)
547 assert_equal(d_maparg, d)
548 endfor
549
Ernie Rael09661202022-04-25 14:40:44 +0100550 # Check abbr matches maparg
551 for d in maplist(true)
552 # Note, d.mode is '!', but can't use that with maparg
553 var d_maparg = maparg(d.lhs, 'i', true, true)
554 assert_equal(d_maparg, d)
555 endfor
556
557 ClearMappingsAbbreviations()
558 assert_equal(0, len(maplist()))
559 assert_equal(0, len(maplist(true)))
Ernie Rael659c2402022-04-24 18:40:28 +0100560enddef
561
562
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100563" vim: shiftwidth=2 sts=2 expandtab