blob: d76c79cb858d607edc749292f31a18f142522030 [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
zeertzjqbfc7cbd2023-04-07 22:09:46 +01004source shared.vim
5
Bram Moolenaar94722c52023-01-28 19:19:03 +00006func s:SID()
Bram Moolenaar292eff02017-07-11 21:46:28 +02007 return str2nr(matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$'))
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02008endfunc
Bram Moolenaar292eff02017-07-11 21:46:28 +02009
Bram Moolenaarc94c1462020-05-22 20:01:06 +020010func Test_maparg()
Bram Moolenaar292eff02017-07-11 21:46:28 +020011 new
12 set cpo-=<
13 set encoding=utf8
14 " Test maparg() with a string result
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020015 let sid = s:SID()
16 let lnum = expand('<sflnum>')
Bram Moolenaar292eff02017-07-11 21:46:28 +020017 map foo<C-V> is<F4>foo
18 vnoremap <script> <buffer> <expr> <silent> bar isbar
Bram Moolenaar292eff02017-07-11 21:46:28 +020019 call assert_equal("is<F4>foo", maparg('foo<C-V>'))
Bram Moolenaar2da0f0c2020-04-01 19:22:12 +020020 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo<C-V>',
Bram Moolenaar9c652532020-05-24 13:10:18 +020021 \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16",
Bram Moolenaara9528b32022-01-18 20:51:35 +000022 \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
Bram Moolenaar94722c52023-01-28 19:19:03 +000023 \ 'lnum': lnum + 1,
Ernie Raeld8f5f762022-05-10 17:50:39 +010024 \ 'rhs': 'is<F4>foo', 'buffer': 0, 'abbr': 0, 'mode_bits': 0x47},
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020025 \ maparg('foo<C-V>', '', 0, 1))
Bram Moolenaar9c652532020-05-24 13:10:18 +020026 call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar',
27 \ 'lhsraw': 'bar', 'mode': 'v',
Bram Moolenaara9528b32022-01-18 20:51:35 +000028 \ 'nowait': 0, 'expr': 1, 'sid': sid, 'scriptversion': 1,
29 \ 'lnum': lnum + 2,
Ernie Raeld8f5f762022-05-10 17:50:39 +010030 \ 'rhs': 'isbar', 'buffer': 1, 'abbr': 0, 'mode_bits': 0x42},
Bram Moolenaara1449832019-09-01 20:16:52 +020031 \ 'bar'->maparg('', 0, 1))
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020032 let lnum = expand('<sflnum>')
Bram Moolenaar292eff02017-07-11 21:46:28 +020033 map <buffer> <nowait> foo bar
Bram Moolenaar9c652532020-05-24 13:10:18 +020034 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo',
35 \ 'lhsraw': 'foo', 'mode': ' ',
Bram Moolenaara9528b32022-01-18 20:51:35 +000036 \ 'nowait': 1, 'expr': 0, 'sid': sid, 'scriptversion': 1,
37 \ 'lnum': lnum + 1, 'rhs': 'bar',
Ernie Raeld8f5f762022-05-10 17:50:39 +010038 \ 'buffer': 1, 'abbr': 0, 'mode_bits': 0x47},
Bram Moolenaar292eff02017-07-11 21:46:28 +020039 \ maparg('foo', '', 0, 1))
Bram Moolenaar14371ed2019-07-27 21:05:21 +020040 let lnum = expand('<sflnum>')
41 tmap baz foo
Bram Moolenaar9c652532020-05-24 13:10:18 +020042 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz',
43 \ 'lhsraw': 'baz', 'mode': 't',
Bram Moolenaara9528b32022-01-18 20:51:35 +000044 \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
45 \ 'lnum': lnum + 1, 'rhs': 'foo',
Ernie Raeld8f5f762022-05-10 17:50:39 +010046 \ 'buffer': 0, 'abbr': 0, 'mode_bits': 0x80},
Bram Moolenaar14371ed2019-07-27 21:05:21 +020047 \ maparg('baz', 't', 0, 1))
Ernie Rael51d04d12022-05-04 15:40:22 +010048 let lnum = expand('<sflnum>')
49 iab A B
50 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'A',
51 \ 'lhsraw': 'A', 'mode': 'i',
52 \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
53 \ 'lnum': lnum + 1, 'rhs': 'B',
Ernie Raeld8f5f762022-05-10 17:50:39 +010054 \ 'buffer': 0, 'abbr': 1, 'mode_bits': 0x0010},
Ernie Rael51d04d12022-05-04 15:40:22 +010055 \ maparg('A', 'i', 1, 1))
56 iuna A
Bram Moolenaar292eff02017-07-11 21:46:28 +020057
58 map abc x<char-114>x
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020059 call assert_equal("xrx", maparg('abc'))
Bram Moolenaar292eff02017-07-11 21:46:28 +020060 map abc y<S-char-114>y
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020061 call assert_equal("yRy", maparg('abc'))
62
zeertzjq0519ce02022-05-09 12:16:19 +010063 " character with K_SPECIAL byte
64 nmap abc
65 call assert_equal('…', maparg('abc'))
66
67 " modified character with K_SPECIAL byte
68 nmap abc <M-…>
69 call assert_equal('<M-…>', maparg('abc'))
70
71 " illegal bytes
72 let str = ":\x7f:\x80:\x90:\xd0:"
73 exe 'nmap abc ' .. str
74 call assert_equal(str, maparg('abc'))
75 unlet str
76
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +010077 omap { w
78 let d = maparg('{', 'o', 0, 1)
79 call assert_equal(['{', 'w', 'o'], [d.lhs, d.rhs, d.mode])
80 ounmap {
81
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +010082 lmap { w
83 let d = maparg('{', 'l', 0, 1)
84 call assert_equal(['{', 'w', 'l'], [d.lhs, d.rhs, d.mode])
85 lunmap {
86
87 nmap { w
88 let d = maparg('{', 'n', 0, 1)
89 call assert_equal(['{', 'w', 'n'], [d.lhs, d.rhs, d.mode])
90 nunmap {
91
92 xmap { w
93 let d = maparg('{', 'x', 0, 1)
94 call assert_equal(['{', 'w', 'x'], [d.lhs, d.rhs, d.mode])
95 xunmap {
96
97 smap { w
98 let d = maparg('{', 's', 0, 1)
99 call assert_equal(['{', 'w', 's'], [d.lhs, d.rhs, d.mode])
100 sunmap {
101
zeertzjq2c8a7eb2022-04-26 21:36:21 +0100102 map <C-I> foo
103 unmap <Tab>
104 " This used to cause a segfault
105 call maparg('<C-I>', '', 0, 1)
106 unmap <C-I>
107
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +0200108 map abc <Nop>
109 call assert_equal("<Nop>", maparg('abc'))
110 unmap abc
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +0100111
112 call feedkeys(":abbr esc \<C-V>\<C-V>\<C-V>\<C-V>\<C-V>\<Esc>\<CR>", "xt")
113 let d = maparg('esc', 'i', 1, 1)
114 call assert_equal(['esc', "\<C-V>\<C-V>\<Esc>", '!'], [d.lhs, d.rhs, d.mode])
115 abclear
zeertzjq2c8a7eb2022-04-26 21:36:21 +0100116 unlet d
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200117endfunc
Bram Moolenaar292eff02017-07-11 21:46:28 +0200118
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200119def Test_vim9_maparg()
120 nmap { w
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200121 var one: string = maparg('{')
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200122 assert_equal('w', one)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200123 var two: string = maparg('{', 'n')
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200124 assert_equal('w', two)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200125 var three: string = maparg('{', 'n', 0)
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200126 assert_equal('w', three)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200127 var four: dict<any> = maparg('{', 'n', 0, 1)
Bram Moolenaard2c61702020-09-06 15:58:36 +0200128 assert_equal(['{', 'w', 'n'], [four.lhs, four.rhs, four.mode])
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200129 nunmap {
130enddef
131
Bram Moolenaara1449832019-09-01 20:16:52 +0200132func Test_mapcheck()
133 call assert_equal('', mapcheck('a'))
134 call assert_equal('', mapcheck('abc'))
135 call assert_equal('', mapcheck('ax'))
136 call assert_equal('', mapcheck('b'))
137
138 map a something
139 call assert_equal('something', mapcheck('a'))
140 call assert_equal('something', mapcheck('a', 'n'))
141 call assert_equal('', mapcheck('a', 'c'))
142 call assert_equal('', mapcheck('a', 'i'))
143 call assert_equal('something', 'abc'->mapcheck())
144 call assert_equal('something', 'ax'->mapcheck())
145 call assert_equal('', mapcheck('b'))
146 unmap a
147
148 map ab foobar
149 call assert_equal('foobar', mapcheck('a'))
150 call assert_equal('foobar', mapcheck('abc'))
151 call assert_equal('', mapcheck('ax'))
152 call assert_equal('', mapcheck('b'))
153 unmap ab
154
155 map abc barfoo
156 call assert_equal('barfoo', mapcheck('a'))
157 call assert_equal('barfoo', mapcheck('a', 'n', 0))
158 call assert_equal('', mapcheck('a', 'n', 1))
159 call assert_equal('barfoo', mapcheck('abc'))
160 call assert_equal('', mapcheck('ax'))
161 call assert_equal('', mapcheck('b'))
162 unmap abc
163
164 abbr ab abbrev
165 call assert_equal('abbrev', mapcheck('a', 'i', 1))
166 call assert_equal('', mapcheck('a', 'n', 1))
167 call assert_equal('', mapcheck('a', 'i', 0))
168 unabbr ab
169endfunc
170
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200171func Test_range_map()
Bram Moolenaar292eff02017-07-11 21:46:28 +0200172 new
173 " Outside of the range, minimum
174 inoremap <Char-0x1040> a
175 execute "normal a\u1040\<Esc>"
176 " Inside of the range, minimum
177 inoremap <Char-0x103f> b
178 execute "normal a\u103f\<Esc>"
179 " Inside of the range, maximum
180 inoremap <Char-0xf03f> c
181 execute "normal a\uf03f\<Esc>"
182 " Outside of the range, maximum
183 inoremap <Char-0xf040> d
184 execute "normal a\uf040\<Esc>"
185 call assert_equal("abcd", getline(1))
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200186endfunc
187
zeertzjq92a3d202022-08-31 16:40:17 +0100188func One_mapset_test(keys, rhs)
189 exe 'nnoremap ' .. a:keys .. ' ' .. a:rhs
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200190 let orig = maparg(a:keys, 'n', 0, 1)
191 call assert_equal(a:keys, orig.lhs)
zeertzjq92a3d202022-08-31 16:40:17 +0100192 call assert_equal(a:rhs, orig.rhs)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200193 call assert_equal('n', orig.mode)
194
195 exe 'nunmap ' .. a:keys
196 let d = maparg(a:keys, 'n', 0, 1)
197 call assert_equal({}, d)
198
199 call mapset('n', 0, orig)
200 let d = maparg(a:keys, 'n', 0, 1)
201 call assert_equal(a:keys, d.lhs)
zeertzjq92a3d202022-08-31 16:40:17 +0100202 call assert_equal(a:rhs, d.rhs)
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200203 call assert_equal('n', d.mode)
204
205 exe 'nunmap ' .. a:keys
206endfunc
207
208func Test_mapset()
zeertzjq92a3d202022-08-31 16:40:17 +0100209 call One_mapset_test('K', 'original<CR>')
210 call One_mapset_test('<F3>', 'original<CR>')
211 call One_mapset_test('<F3>', '<lt>Nop>')
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200212
213 " Check <> key conversion
214 new
215 inoremap K one<Left>x
216 call feedkeys("iK\<Esc>", 'xt')
217 call assert_equal('onxe', getline(1))
218
219 let orig = maparg('K', 'i', 0, 1)
220 call assert_equal('K', orig.lhs)
221 call assert_equal('one<Left>x', orig.rhs)
222 call assert_equal('i', orig.mode)
223
224 iunmap K
225 let d = maparg('K', 'i', 0, 1)
226 call assert_equal({}, d)
227
228 call mapset('i', 0, orig)
229 call feedkeys("SK\<Esc>", 'xt')
230 call assert_equal('onxe', getline(1))
231
232 iunmap K
233
zeertzjq92a3d202022-08-31 16:40:17 +0100234 " Test that <Nop> is restored properly
235 inoremap K <Nop>
236 call feedkeys("SK\<Esc>", 'xt')
237 call assert_equal('', getline(1))
238
239 let orig = maparg('K', 'i', 0, 1)
240 call assert_equal('K', orig.lhs)
241 call assert_equal('<Nop>', orig.rhs)
242 call assert_equal('i', orig.mode)
243
244 inoremap K foo
245 call feedkeys("SK\<Esc>", 'xt')
246 call assert_equal('foo', getline(1))
247
248 call mapset('i', 0, orig)
249 call feedkeys("SK\<Esc>", 'xt')
250 call assert_equal('', getline(1))
251
252 iunmap K
253
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200254 " Test literal <CR> using a backslash
255 let cpo_save = &cpo
256 set cpo-=B
257 inoremap K one\<CR>two
258 call feedkeys("SK\<Esc>", 'xt')
259 call assert_equal('one<CR>two', getline(1))
260
261 let orig = maparg('K', 'i', 0, 1)
262 call assert_equal('K', orig.lhs)
263 call assert_equal('one\<CR>two', orig.rhs)
264 call assert_equal('i', orig.mode)
265
266 iunmap K
267 let d = maparg('K', 'i', 0, 1)
268 call assert_equal({}, d)
269
270 call mapset('i', 0, orig)
271 call feedkeys("SK\<Esc>", 'xt')
272 call assert_equal('one<CR>two', getline(1))
273
274 iunmap K
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200275
276 " Test literal <CR> using CTRL-V
277 inoremap K one<CR>two
278 call feedkeys("SK\<Esc>", 'xt')
279 call assert_equal('one<CR>two', getline(1))
280
281 let orig = maparg('K', 'i', 0, 1)
282 call assert_equal('K', orig.lhs)
283 call assert_equal("one\x16<CR>two", orig.rhs)
284 call assert_equal('i', orig.mode)
285
286 iunmap K
287 let d = maparg('K', 'i', 0, 1)
288 call assert_equal({}, d)
289
290 call mapset('i', 0, orig)
291 call feedkeys("SK\<Esc>", 'xt')
292 call assert_equal('one<CR>two', getline(1))
293
294 iunmap K
295 let &cpo = cpo_save
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200296 bwipe!
Bram Moolenaar1b912982020-09-29 21:45:41 +0200297
298 call assert_fails('call mapset([], v:false, {})', 'E730:')
Yegappan Lakshmanan04c4c572022-08-30 19:48:24 +0100299 call assert_fails('call mapset("i", 0, "")', 'E1206:')
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100300 call assert_fails('call mapset("i", 0, {})', 'E460:')
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200301endfunc
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100302
Ernie Rael51d04d12022-05-04 15:40:22 +0100303def Test_mapset_arg1_dir()
304 # This test is mostly about get_map_mode_string.
305 # Once the code gets past that, it's common with the 3 arg mapset.
306
307 # GetModes() return list of modes for 'XZ' lhs using maplist.
308 # There is one list item per mapping
309 def GetModes(abbr: bool = false): list<string>
310 return maplist(abbr)->filter((_, m) => m.lhs == 'XZ')
311 ->mapnew((_, m) => m.mode)
312 enddef
313
314 const unmap_cmds = [ 'unmap', 'unmap!', 'tunmap', 'lunmap' ]
315 def UnmapAll(lhs: string)
316 for cmd in unmap_cmds
317 try | execute(cmd .. ' ' .. lhs) | catch /E31/ | endtry
318 endfor
319 enddef
320
321 var tmap: dict<any>
322
323 # some mapset(mode, abbr, dict) tests using get_map_mode_str
324 map XZ x
325 tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy()
326 # this splits the mapping into 2 mappings
327 mapset('ox', false, tmap)
328 assert_equal(2, len(GetModes()))
329 mapset('o', false, tmap)
330 assert_equal(3, len(GetModes()))
331 # test that '' acts like ' ', and that the 3 mappings become 1
332 mapset('', false, tmap)
333 assert_equal([' '], GetModes())
334 # dict's mode/abbr are ignored
335 UnmapAll('XZ')
336 tmap.mode = '!'
337 tmap.abbr = true
338 mapset('o', false, tmap)
339 assert_equal(['o'], GetModes())
340
341 # test the 3 arg version handles bad mode string, dict not used
342 assert_fails("mapset('vi', false, {})", 'E1276:')
343
344
345 # get the abbreviations out of the way
346 abbreviate XZ ZX
347 tmap = maplist(true)->filter((_, m) => m.lhs == 'XZ')[0]->copy()
348
349 abclear
350 # 'ic' is the default ab command, shows up as '!'
351 tmap.mode = 'ic'
352 mapset(tmap)
353 assert_equal(['!'], GetModes(true))
354
355 abclear
356 tmap.mode = 'i'
357 mapset(tmap)
358 assert_equal(['i'], GetModes(true))
359
360 abclear
361 tmap.mode = 'c'
362 mapset(tmap)
363 assert_equal(['c'], GetModes(true))
364
365 abclear
366 tmap.mode = '!'
367 mapset(tmap)
368 assert_equal(['!'], GetModes(true))
369
370 assert_fails("mapset({mode: ' !', abbr: 1})", 'E1276:')
371 assert_fails("mapset({mode: 'cl', abbr: 1})", 'E1276:')
372 assert_fails("mapset({mode: 'in', abbr: 1})", 'E1276:')
373
374 # the map commands
375 map XZ x
376 tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy()
377
378 # try the combos
379 UnmapAll('XZ')
380 # 'nxso' is ' ', the unadorned :map
381 tmap.mode = 'nxso'
382 mapset(tmap)
383 assert_equal([' '], GetModes())
384
385 UnmapAll('XZ')
386 # 'ic' is '!'
387 tmap.mode = 'ic'
388 mapset(tmap)
389 assert_equal(['!'], GetModes())
390
391 UnmapAll('XZ')
392 # 'xs' is really 'v'
393 tmap.mode = 'xs'
394 mapset(tmap)
395 assert_equal(['v'], GetModes())
396
397 # try the individual modes
398 UnmapAll('XZ')
399 tmap.mode = 'n'
400 mapset(tmap)
401 assert_equal(['n'], GetModes())
402
403 UnmapAll('XZ')
404 tmap.mode = 'x'
405 mapset(tmap)
406 assert_equal(['x'], GetModes())
407
408 UnmapAll('XZ')
409 tmap.mode = 's'
410 mapset(tmap)
411 assert_equal(['s'], GetModes())
412
413 UnmapAll('XZ')
414 tmap.mode = 'o'
415 mapset(tmap)
416 assert_equal(['o'], GetModes())
417
418 UnmapAll('XZ')
419 tmap.mode = 'i'
420 mapset(tmap)
421 assert_equal(['i'], GetModes())
422
423 UnmapAll('XZ')
424 tmap.mode = 'c'
425 mapset(tmap)
426 assert_equal(['c'], GetModes())
427
428 UnmapAll('XZ')
429 tmap.mode = 't'
430 mapset(tmap)
431 assert_equal(['t'], GetModes())
432
433 UnmapAll('XZ')
434 tmap.mode = 'l'
435 mapset(tmap)
436 assert_equal(['l'], GetModes())
437
438 UnmapAll('XZ')
439
440 # get errors for modes that can't be in one mapping
441 assert_fails("mapset({mode: 'nxsoi', abbr: 0})", 'E1276:')
442 assert_fails("mapset({mode: ' !', abbr: 0})", 'E1276:')
443 assert_fails("mapset({mode: 'ix', abbr: 0})", 'E1276:')
444 assert_fails("mapset({mode: 'tl', abbr: 0})", 'E1276:')
445 assert_fails("mapset({mode: ' l', abbr: 0})", 'E1276:')
446 assert_fails("mapset({mode: ' t', abbr: 0})", 'E1276:')
447enddef
448
Bram Moolenaar9c652532020-05-24 13:10:18 +0200449func Check_ctrlb_map(d, check_alt)
450 call assert_equal('<C-B>', a:d.lhs)
451 if a:check_alt
452 call assert_equal("\x80\xfc\x04B", a:d.lhsraw)
453 call assert_equal("\x02", a:d.lhsrawalt)
454 else
455 call assert_equal("\x02", a:d.lhsraw)
456 endif
457endfunc
458
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +0200459func Test_map_local()
460 nmap a global
461 nmap <buffer>a local
462
463 let prev_map_list = split(execute('nmap a'), "\n")
464 call assert_match('n\s*a\s*@local', prev_map_list[0])
465 call assert_match('n\s*a\s*global', prev_map_list[1])
466
467 let mapping = maparg('a', 'n', 0, 1)
468 call assert_equal(1, mapping.buffer)
469 let mapping.rhs = 'new_local'
470 call mapset('n', 0, mapping)
471
472 " Check that the global mapping is left untouched.
473 let map_list = split(execute('nmap a'), "\n")
474 call assert_match('n\s*a\s*@new_local', map_list[0])
475 call assert_match('n\s*a\s*global', map_list[1])
476
477 nunmap a
478endfunc
479
Bram Moolenaar9c652532020-05-24 13:10:18 +0200480func Test_map_restore()
481 " Test restoring map with alternate keycode
482 nmap <C-B> back
483 let d = maparg('<C-B>', 'n', 0, 1)
484 call Check_ctrlb_map(d, 1)
485 let dsimp = maparg("\x02", 'n', 0, 1)
486 call Check_ctrlb_map(dsimp, 0)
487 nunmap <C-B>
488 call mapset('n', 0, d)
489 let d = maparg('<C-B>', 'n', 0, 1)
490 call Check_ctrlb_map(d, 1)
491 let dsimp = maparg("\x02", 'n', 0, 1)
492 call Check_ctrlb_map(dsimp, 0)
493
494 nunmap <C-B>
zeertzjqbfc7cbd2023-04-07 22:09:46 +0100495endfunc
Bram Moolenaar9c652532020-05-24 13:10:18 +0200496
zeertzjqbfc7cbd2023-04-07 22:09:46 +0100497" Test restoring the script context of a mapping
498func Test_map_restore_sid()
499 let after =<< trim [CODE]
500 call assert_equal("\tLast set from --cmd argument",
501 \ execute('verbose nmap ,n')->trim()->split("\n")[-1])
502 let d = maparg(',n', 'n', 0, 1)
503 nunmap ,n
504 call assert_equal('No mapping found',
505 \ execute('verbose nmap ,n')->trim()->split("\n")[-1])
506 call mapset('n', 0, d)
507 call assert_equal("\tLast set from --cmd argument",
508 \ execute('verbose nmap ,n')->trim()->split("\n")[-1])
509 call writefile(v:errors, 'Xresult')
510 qall!
511 [CODE]
512
513 if RunVim([], after, '--clean --cmd "nmap ,n <Nop>"')
514 call assert_equal([], readfile('Xresult'))
515 endif
516 call delete('Xresult')
Bram Moolenaar9c652532020-05-24 13:10:18 +0200517endfunc
518
Ernie Rael09661202022-04-25 14:40:44 +0100519def Test_maplist()
Ernie Rael659c2402022-04-24 18:40:28 +0100520 new
Ernie Rael09661202022-04-25 14:40:44 +0100521 def ClearMappingsAbbreviations()
Ernie Rael659c2402022-04-24 18:40:28 +0100522 mapclear | nmapclear | vmapclear | xmapclear | smapclear | omapclear
523 mapclear! | imapclear | lmapclear | cmapclear | tmapclear
524 mapclear <buffer> | nmapclear <buffer> | vmapclear <buffer>
525 xmapclear <buffer> | smapclear <buffer> | omapclear <buffer>
526 mapclear! <buffer> | imapclear <buffer> | lmapclear <buffer>
527 cmapclear <buffer> | tmapclear <buffer>
Ernie Rael09661202022-04-25 14:40:44 +0100528 abclear | abclear <buffer>
Ernie Rael659c2402022-04-24 18:40:28 +0100529 enddef
530
531 def AddMaps(new: list<string>, accum: list<string>)
532 if len(new) > 0 && new[0] != "No mapping found"
533 accum->extend(new)
534 endif
535 enddef
536
Ernie Rael09661202022-04-25 14:40:44 +0100537 ClearMappingsAbbreviations()
538 assert_equal(0, len(maplist()))
539 assert_equal(0, len(maplist(true)))
Ernie Rael659c2402022-04-24 18:40:28 +0100540
541 # Set up some mappings.
542 map dup bar
543 map <buffer> dup bufbar
544 map foo<C-V> is<F4>foo
545 vnoremap <script> <buffer> <expr> <silent> bar isbar
546 tmap baz foo
547 omap h w
548 lmap i w
549 nmap j w
550 xmap k w
551 smap l w
552 map abc <Nop>
553 nmap <M-j> x
554 nmap <M-Space> y
Ernie Rael09661202022-04-25 14:40:44 +0100555 # And abbreviations
556 abbreviate xy he
557 abbreviate xx she
558 abbreviate <buffer> x they
Ernie Rael659c2402022-04-24 18:40:28 +0100559
560 # Get a list of the mappings with the ':map' commands.
Ernie Rael09661202022-04-25 14:40:44 +0100561 # Check maplist() return a list of the same size.
562 assert_equal(13, len(maplist()))
563 assert_equal(3, len(maplist(true)))
564 assert_equal(13, len(maplist(false)))
Ernie Rael659c2402022-04-24 18:40:28 +0100565
566 # collect all the current maps using :map commands
567 var maps_command: list<string>
568 AddMaps(split(execute('map'), '\n'), maps_command)
569 AddMaps(split(execute('map!'), '\n'), maps_command)
570 AddMaps(split(execute('tmap'), '\n'), maps_command)
571 AddMaps(split(execute('lmap'), '\n'), maps_command)
572
Ernie Rael09661202022-04-25 14:40:44 +0100573 # Use maplist to get all the maps
574 var maps_maplist = maplist()
575 assert_equal(len(maps_command), len(maps_maplist))
Ernie Rael659c2402022-04-24 18:40:28 +0100576
577 # make sure all the mode-lhs are unique, no duplicates
578 var map_set: dict<number>
Ernie Rael09661202022-04-25 14:40:44 +0100579 for d in maps_maplist
Ernie Rael659c2402022-04-24 18:40:28 +0100580 map_set[d.mode .. "-" .. d.lhs .. "-" .. d.buffer] = 0
581 endfor
Ernie Rael09661202022-04-25 14:40:44 +0100582 assert_equal(len(maps_maplist), len(map_set))
Ernie Rael659c2402022-04-24 18:40:28 +0100583
Ernie Rael09661202022-04-25 14:40:44 +0100584 # For everything returned by maplist, should be the same as from maparg.
dundargocc57b5bc2022-11-02 13:30:51 +0000585 # Except for "map dup", because maparg returns the <buffer> version
Ernie Rael09661202022-04-25 14:40:44 +0100586 for d in maps_maplist
Ernie Rael659c2402022-04-24 18:40:28 +0100587 if d.lhs == 'dup' && d.buffer == 0
588 continue
589 endif
590 var d_maparg = maparg(d.lhs, d.mode, false, true)
591 assert_equal(d_maparg, d)
592 endfor
593
Ernie Rael09661202022-04-25 14:40:44 +0100594 # Check abbr matches maparg
595 for d in maplist(true)
596 # Note, d.mode is '!', but can't use that with maparg
597 var d_maparg = maparg(d.lhs, 'i', true, true)
598 assert_equal(d_maparg, d)
599 endfor
600
601 ClearMappingsAbbreviations()
602 assert_equal(0, len(maplist()))
603 assert_equal(0, len(maplist(true)))
Ernie Rael659c2402022-04-24 18:40:28 +0100604enddef
605
606
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100607" vim: shiftwidth=2 sts=2 expandtab