blob: 13e61f207b93edbec3a07c1466a71f86ab2bdff4 [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
zeertzjq92a3d202022-08-31 16:40:17 +0100186func One_mapset_test(keys, rhs)
187 exe 'nnoremap ' .. a:keys .. ' ' .. a:rhs
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200188 let orig = maparg(a:keys, 'n', 0, 1)
189 call assert_equal(a:keys, orig.lhs)
zeertzjq92a3d202022-08-31 16:40:17 +0100190 call assert_equal(a:rhs, orig.rhs)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200191 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)
zeertzjq92a3d202022-08-31 16:40:17 +0100200 call assert_equal(a:rhs, d.rhs)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200201 call assert_equal('n', d.mode)
202
203 exe 'nunmap ' .. a:keys
204endfunc
205
206func Test_mapset()
zeertzjq92a3d202022-08-31 16:40:17 +0100207 call One_mapset_test('K', 'original<CR>')
208 call One_mapset_test('<F3>', 'original<CR>')
209 call One_mapset_test('<F3>', '<lt>Nop>')
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200210
211 " Check <> key conversion
212 new
213 inoremap K one<Left>x
214 call feedkeys("iK\<Esc>", 'xt')
215 call assert_equal('onxe', getline(1))
216
217 let orig = maparg('K', 'i', 0, 1)
218 call assert_equal('K', orig.lhs)
219 call assert_equal('one<Left>x', orig.rhs)
220 call assert_equal('i', orig.mode)
221
222 iunmap K
223 let d = maparg('K', 'i', 0, 1)
224 call assert_equal({}, d)
225
226 call mapset('i', 0, orig)
227 call feedkeys("SK\<Esc>", 'xt')
228 call assert_equal('onxe', getline(1))
229
230 iunmap K
231
zeertzjq92a3d202022-08-31 16:40:17 +0100232 " Test that <Nop> is restored properly
233 inoremap K <Nop>
234 call feedkeys("SK\<Esc>", 'xt')
235 call assert_equal('', getline(1))
236
237 let orig = maparg('K', 'i', 0, 1)
238 call assert_equal('K', orig.lhs)
239 call assert_equal('<Nop>', orig.rhs)
240 call assert_equal('i', orig.mode)
241
242 inoremap K foo
243 call feedkeys("SK\<Esc>", 'xt')
244 call assert_equal('foo', getline(1))
245
246 call mapset('i', 0, orig)
247 call feedkeys("SK\<Esc>", 'xt')
248 call assert_equal('', getline(1))
249
250 iunmap K
251
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200252 " Test literal <CR> using a backslash
253 let cpo_save = &cpo
254 set cpo-=B
255 inoremap K one\<CR>two
256 call feedkeys("SK\<Esc>", 'xt')
257 call assert_equal('one<CR>two', getline(1))
258
259 let orig = maparg('K', 'i', 0, 1)
260 call assert_equal('K', orig.lhs)
261 call assert_equal('one\<CR>two', orig.rhs)
262 call assert_equal('i', orig.mode)
263
264 iunmap K
265 let d = maparg('K', 'i', 0, 1)
266 call assert_equal({}, d)
267
268 call mapset('i', 0, orig)
269 call feedkeys("SK\<Esc>", 'xt')
270 call assert_equal('one<CR>two', getline(1))
271
272 iunmap K
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200273
274 " Test literal <CR> using CTRL-V
275 inoremap K one<CR>two
276 call feedkeys("SK\<Esc>", 'xt')
277 call assert_equal('one<CR>two', getline(1))
278
279 let orig = maparg('K', 'i', 0, 1)
280 call assert_equal('K', orig.lhs)
281 call assert_equal("one\x16<CR>two", orig.rhs)
282 call assert_equal('i', orig.mode)
283
284 iunmap K
285 let d = maparg('K', 'i', 0, 1)
286 call assert_equal({}, d)
287
288 call mapset('i', 0, orig)
289 call feedkeys("SK\<Esc>", 'xt')
290 call assert_equal('one<CR>two', getline(1))
291
292 iunmap K
293 let &cpo = cpo_save
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200294 bwipe!
Bram Moolenaar1b912982020-09-29 21:45:41 +0200295
296 call assert_fails('call mapset([], v:false, {})', 'E730:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100297 call assert_fails('call mapset("i", 0, "")', 'E1206:')
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100298 call assert_fails('call mapset("i", 0, {})', 'E460:')
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200299endfunc
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100300
Ernie Rael51d04d12022-05-04 15:40:22 +0100301def Test_mapset_arg1_dir()
302 # This test is mostly about get_map_mode_string.
303 # Once the code gets past that, it's common with the 3 arg mapset.
304
305 # GetModes() return list of modes for 'XZ' lhs using maplist.
306 # There is one list item per mapping
307 def GetModes(abbr: bool = false): list<string>
308 return maplist(abbr)->filter((_, m) => m.lhs == 'XZ')
309 ->mapnew((_, m) => m.mode)
310 enddef
311
312 const unmap_cmds = [ 'unmap', 'unmap!', 'tunmap', 'lunmap' ]
313 def UnmapAll(lhs: string)
314 for cmd in unmap_cmds
315 try | execute(cmd .. ' ' .. lhs) | catch /E31/ | endtry
316 endfor
317 enddef
318
319 var tmap: dict<any>
320
321 # some mapset(mode, abbr, dict) tests using get_map_mode_str
322 map XZ x
323 tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy()
324 # this splits the mapping into 2 mappings
325 mapset('ox', false, tmap)
326 assert_equal(2, len(GetModes()))
327 mapset('o', false, tmap)
328 assert_equal(3, len(GetModes()))
329 # test that '' acts like ' ', and that the 3 mappings become 1
330 mapset('', false, tmap)
331 assert_equal([' '], GetModes())
332 # dict's mode/abbr are ignored
333 UnmapAll('XZ')
334 tmap.mode = '!'
335 tmap.abbr = true
336 mapset('o', false, tmap)
337 assert_equal(['o'], GetModes())
338
339 # test the 3 arg version handles bad mode string, dict not used
340 assert_fails("mapset('vi', false, {})", 'E1276:')
341
342
343 # get the abbreviations out of the way
344 abbreviate XZ ZX
345 tmap = maplist(true)->filter((_, m) => m.lhs == 'XZ')[0]->copy()
346
347 abclear
348 # 'ic' is the default ab command, shows up as '!'
349 tmap.mode = 'ic'
350 mapset(tmap)
351 assert_equal(['!'], GetModes(true))
352
353 abclear
354 tmap.mode = 'i'
355 mapset(tmap)
356 assert_equal(['i'], GetModes(true))
357
358 abclear
359 tmap.mode = 'c'
360 mapset(tmap)
361 assert_equal(['c'], GetModes(true))
362
363 abclear
364 tmap.mode = '!'
365 mapset(tmap)
366 assert_equal(['!'], GetModes(true))
367
368 assert_fails("mapset({mode: ' !', abbr: 1})", 'E1276:')
369 assert_fails("mapset({mode: 'cl', abbr: 1})", 'E1276:')
370 assert_fails("mapset({mode: 'in', abbr: 1})", 'E1276:')
371
372 # the map commands
373 map XZ x
374 tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy()
375
376 # try the combos
377 UnmapAll('XZ')
378 # 'nxso' is ' ', the unadorned :map
379 tmap.mode = 'nxso'
380 mapset(tmap)
381 assert_equal([' '], GetModes())
382
383 UnmapAll('XZ')
384 # 'ic' is '!'
385 tmap.mode = 'ic'
386 mapset(tmap)
387 assert_equal(['!'], GetModes())
388
389 UnmapAll('XZ')
390 # 'xs' is really 'v'
391 tmap.mode = 'xs'
392 mapset(tmap)
393 assert_equal(['v'], GetModes())
394
395 # try the individual modes
396 UnmapAll('XZ')
397 tmap.mode = 'n'
398 mapset(tmap)
399 assert_equal(['n'], GetModes())
400
401 UnmapAll('XZ')
402 tmap.mode = 'x'
403 mapset(tmap)
404 assert_equal(['x'], GetModes())
405
406 UnmapAll('XZ')
407 tmap.mode = 's'
408 mapset(tmap)
409 assert_equal(['s'], GetModes())
410
411 UnmapAll('XZ')
412 tmap.mode = 'o'
413 mapset(tmap)
414 assert_equal(['o'], GetModes())
415
416 UnmapAll('XZ')
417 tmap.mode = 'i'
418 mapset(tmap)
419 assert_equal(['i'], GetModes())
420
421 UnmapAll('XZ')
422 tmap.mode = 'c'
423 mapset(tmap)
424 assert_equal(['c'], GetModes())
425
426 UnmapAll('XZ')
427 tmap.mode = 't'
428 mapset(tmap)
429 assert_equal(['t'], GetModes())
430
431 UnmapAll('XZ')
432 tmap.mode = 'l'
433 mapset(tmap)
434 assert_equal(['l'], GetModes())
435
436 UnmapAll('XZ')
437
438 # get errors for modes that can't be in one mapping
439 assert_fails("mapset({mode: 'nxsoi', abbr: 0})", 'E1276:')
440 assert_fails("mapset({mode: ' !', abbr: 0})", 'E1276:')
441 assert_fails("mapset({mode: 'ix', abbr: 0})", 'E1276:')
442 assert_fails("mapset({mode: 'tl', abbr: 0})", 'E1276:')
443 assert_fails("mapset({mode: ' l', abbr: 0})", 'E1276:')
444 assert_fails("mapset({mode: ' t', abbr: 0})", 'E1276:')
445enddef
446
Bram Moolenaar9c652532020-05-24 13:10:18 +0200447func Check_ctrlb_map(d, check_alt)
448 call assert_equal('<C-B>', a:d.lhs)
449 if a:check_alt
450 call assert_equal("\x80\xfc\x04B", a:d.lhsraw)
451 call assert_equal("\x02", a:d.lhsrawalt)
452 else
453 call assert_equal("\x02", a:d.lhsraw)
454 endif
455endfunc
456
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +0200457func Test_map_local()
458 nmap a global
459 nmap <buffer>a local
460
461 let prev_map_list = split(execute('nmap a'), "\n")
462 call assert_match('n\s*a\s*@local', prev_map_list[0])
463 call assert_match('n\s*a\s*global', prev_map_list[1])
464
465 let mapping = maparg('a', 'n', 0, 1)
466 call assert_equal(1, mapping.buffer)
467 let mapping.rhs = 'new_local'
468 call mapset('n', 0, mapping)
469
470 " Check that the global mapping is left untouched.
471 let map_list = split(execute('nmap a'), "\n")
472 call assert_match('n\s*a\s*@new_local', map_list[0])
473 call assert_match('n\s*a\s*global', map_list[1])
474
475 nunmap a
476endfunc
477
Bram Moolenaar9c652532020-05-24 13:10:18 +0200478func Test_map_restore()
479 " Test restoring map with alternate keycode
480 nmap <C-B> back
481 let d = maparg('<C-B>', 'n', 0, 1)
482 call Check_ctrlb_map(d, 1)
483 let dsimp = maparg("\x02", 'n', 0, 1)
484 call Check_ctrlb_map(dsimp, 0)
485 nunmap <C-B>
486 call mapset('n', 0, d)
487 let d = maparg('<C-B>', 'n', 0, 1)
488 call Check_ctrlb_map(d, 1)
489 let dsimp = maparg("\x02", 'n', 0, 1)
490 call Check_ctrlb_map(dsimp, 0)
491
492 nunmap <C-B>
493
494endfunc
495
Ernie Rael09661202022-04-25 14:40:44 +0100496def Test_maplist()
Ernie Rael659c2402022-04-24 18:40:28 +0100497 new
Ernie Rael09661202022-04-25 14:40:44 +0100498 def ClearMappingsAbbreviations()
Ernie Rael659c2402022-04-24 18:40:28 +0100499 mapclear | nmapclear | vmapclear | xmapclear | smapclear | omapclear
500 mapclear! | imapclear | lmapclear | cmapclear | tmapclear
501 mapclear <buffer> | nmapclear <buffer> | vmapclear <buffer>
502 xmapclear <buffer> | smapclear <buffer> | omapclear <buffer>
503 mapclear! <buffer> | imapclear <buffer> | lmapclear <buffer>
504 cmapclear <buffer> | tmapclear <buffer>
Ernie Rael09661202022-04-25 14:40:44 +0100505 abclear | abclear <buffer>
Ernie Rael659c2402022-04-24 18:40:28 +0100506 enddef
507
508 def AddMaps(new: list<string>, accum: list<string>)
509 if len(new) > 0 && new[0] != "No mapping found"
510 accum->extend(new)
511 endif
512 enddef
513
Ernie Rael09661202022-04-25 14:40:44 +0100514 ClearMappingsAbbreviations()
515 assert_equal(0, len(maplist()))
516 assert_equal(0, len(maplist(true)))
Ernie Rael659c2402022-04-24 18:40:28 +0100517
518 # Set up some mappings.
519 map dup bar
520 map <buffer> dup bufbar
521 map foo<C-V> is<F4>foo
522 vnoremap <script> <buffer> <expr> <silent> bar isbar
523 tmap baz foo
524 omap h w
525 lmap i w
526 nmap j w
527 xmap k w
528 smap l w
529 map abc <Nop>
530 nmap <M-j> x
531 nmap <M-Space> y
Ernie Rael09661202022-04-25 14:40:44 +0100532 # And abbreviations
533 abbreviate xy he
534 abbreviate xx she
535 abbreviate <buffer> x they
Ernie Rael659c2402022-04-24 18:40:28 +0100536
537 # Get a list of the mappings with the ':map' commands.
Ernie Rael09661202022-04-25 14:40:44 +0100538 # Check maplist() return a list of the same size.
539 assert_equal(13, len(maplist()))
540 assert_equal(3, len(maplist(true)))
541 assert_equal(13, len(maplist(false)))
Ernie Rael659c2402022-04-24 18:40:28 +0100542
543 # collect all the current maps using :map commands
544 var maps_command: list<string>
545 AddMaps(split(execute('map'), '\n'), maps_command)
546 AddMaps(split(execute('map!'), '\n'), maps_command)
547 AddMaps(split(execute('tmap'), '\n'), maps_command)
548 AddMaps(split(execute('lmap'), '\n'), maps_command)
549
Ernie Rael09661202022-04-25 14:40:44 +0100550 # Use maplist to get all the maps
551 var maps_maplist = maplist()
552 assert_equal(len(maps_command), len(maps_maplist))
Ernie Rael659c2402022-04-24 18:40:28 +0100553
554 # make sure all the mode-lhs are unique, no duplicates
555 var map_set: dict<number>
Ernie Rael09661202022-04-25 14:40:44 +0100556 for d in maps_maplist
Ernie Rael659c2402022-04-24 18:40:28 +0100557 map_set[d.mode .. "-" .. d.lhs .. "-" .. d.buffer] = 0
558 endfor
Ernie Rael09661202022-04-25 14:40:44 +0100559 assert_equal(len(maps_maplist), len(map_set))
Ernie Rael659c2402022-04-24 18:40:28 +0100560
Ernie Rael09661202022-04-25 14:40:44 +0100561 # For everything returned by maplist, should be the same as from maparg.
dundargocc57b5bc2022-11-02 13:30:51 +0000562 # Except for "map dup", because maparg returns the <buffer> version
Ernie Rael09661202022-04-25 14:40:44 +0100563 for d in maps_maplist
Ernie Rael659c2402022-04-24 18:40:28 +0100564 if d.lhs == 'dup' && d.buffer == 0
565 continue
566 endif
567 var d_maparg = maparg(d.lhs, d.mode, false, true)
568 assert_equal(d_maparg, d)
569 endfor
570
Ernie Rael09661202022-04-25 14:40:44 +0100571 # Check abbr matches maparg
572 for d in maplist(true)
573 # Note, d.mode is '!', but can't use that with maparg
574 var d_maparg = maparg(d.lhs, 'i', true, true)
575 assert_equal(d_maparg, d)
576 endfor
577
578 ClearMappingsAbbreviations()
579 assert_equal(0, len(maplist()))
580 assert_equal(0, len(maplist(true)))
Ernie Rael659c2402022-04-24 18:40:28 +0100581enddef
582
583
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100584" vim: shiftwidth=2 sts=2 expandtab