blob: 66f3479034128758434d613e8a454b81ac18cfd9 [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,
Bram Moolenaar9c652532020-05-24 13:10:18 +020022 \ 'rhs': 'is<F4>foo', 'buffer': 0},
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,
Bram Moolenaar9c652532020-05-24 13:10:18 +020028 \ 'rhs': 'isbar', 'buffer': 1},
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',
Bram Moolenaar9c652532020-05-24 13:10:18 +020036 \ 'buffer': 1},
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',
Bram Moolenaar9c652532020-05-24 13:10:18 +020044 \ 'buffer': 0},
Bram Moolenaar14371ed2019-07-27 21:05:21 +020045 \ maparg('baz', 't', 0, 1))
Bram Moolenaar292eff02017-07-11 21:46:28 +020046
47 map abc x<char-114>x
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020048 call assert_equal("xrx", maparg('abc'))
Bram Moolenaar292eff02017-07-11 21:46:28 +020049 map abc y<S-char-114>y
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020050 call assert_equal("yRy", maparg('abc'))
51
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +010052 omap { w
53 let d = maparg('{', 'o', 0, 1)
54 call assert_equal(['{', 'w', 'o'], [d.lhs, d.rhs, d.mode])
55 ounmap {
56
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +010057 lmap { w
58 let d = maparg('{', 'l', 0, 1)
59 call assert_equal(['{', 'w', 'l'], [d.lhs, d.rhs, d.mode])
60 lunmap {
61
62 nmap { w
63 let d = maparg('{', 'n', 0, 1)
64 call assert_equal(['{', 'w', 'n'], [d.lhs, d.rhs, d.mode])
65 nunmap {
66
67 xmap { w
68 let d = maparg('{', 'x', 0, 1)
69 call assert_equal(['{', 'w', 'x'], [d.lhs, d.rhs, d.mode])
70 xunmap {
71
72 smap { w
73 let d = maparg('{', 's', 0, 1)
74 call assert_equal(['{', 'w', 's'], [d.lhs, d.rhs, d.mode])
75 sunmap {
76
zeertzjq2c8a7eb2022-04-26 21:36:21 +010077 map <C-I> foo
78 unmap <Tab>
79 " This used to cause a segfault
80 call maparg('<C-I>', '', 0, 1)
81 unmap <C-I>
82
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020083 map abc <Nop>
84 call assert_equal("<Nop>", maparg('abc'))
85 unmap abc
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +010086
87 call feedkeys(":abbr esc \<C-V>\<C-V>\<C-V>\<C-V>\<C-V>\<Esc>\<CR>", "xt")
88 let d = maparg('esc', 'i', 1, 1)
89 call assert_equal(['esc', "\<C-V>\<C-V>\<Esc>", '!'], [d.lhs, d.rhs, d.mode])
90 abclear
zeertzjq2c8a7eb2022-04-26 21:36:21 +010091 unlet d
Bram Moolenaar4c9243f2020-05-22 13:10:44 +020092endfunc
Bram Moolenaar292eff02017-07-11 21:46:28 +020093
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +020094def Test_vim9_maparg()
95 nmap { w
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +020096 var one: string = maparg('{')
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +020097 assert_equal('w', one)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +020098 var two: string = maparg('{', 'n')
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +020099 assert_equal('w', two)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200100 var three: string = maparg('{', 'n', 0)
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200101 assert_equal('w', three)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +0200102 var four: dict<any> = maparg('{', 'n', 0, 1)
Bram Moolenaard2c61702020-09-06 15:58:36 +0200103 assert_equal(['{', 'w', 'n'], [four.lhs, four.rhs, four.mode])
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +0200104 nunmap {
105enddef
106
Bram Moolenaara1449832019-09-01 20:16:52 +0200107func Test_mapcheck()
108 call assert_equal('', mapcheck('a'))
109 call assert_equal('', mapcheck('abc'))
110 call assert_equal('', mapcheck('ax'))
111 call assert_equal('', mapcheck('b'))
112
113 map a something
114 call assert_equal('something', mapcheck('a'))
115 call assert_equal('something', mapcheck('a', 'n'))
116 call assert_equal('', mapcheck('a', 'c'))
117 call assert_equal('', mapcheck('a', 'i'))
118 call assert_equal('something', 'abc'->mapcheck())
119 call assert_equal('something', 'ax'->mapcheck())
120 call assert_equal('', mapcheck('b'))
121 unmap a
122
123 map ab foobar
124 call assert_equal('foobar', mapcheck('a'))
125 call assert_equal('foobar', mapcheck('abc'))
126 call assert_equal('', mapcheck('ax'))
127 call assert_equal('', mapcheck('b'))
128 unmap ab
129
130 map abc barfoo
131 call assert_equal('barfoo', mapcheck('a'))
132 call assert_equal('barfoo', mapcheck('a', 'n', 0))
133 call assert_equal('', mapcheck('a', 'n', 1))
134 call assert_equal('barfoo', mapcheck('abc'))
135 call assert_equal('', mapcheck('ax'))
136 call assert_equal('', mapcheck('b'))
137 unmap abc
138
139 abbr ab abbrev
140 call assert_equal('abbrev', mapcheck('a', 'i', 1))
141 call assert_equal('', mapcheck('a', 'n', 1))
142 call assert_equal('', mapcheck('a', 'i', 0))
143 unabbr ab
144endfunc
145
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200146func Test_range_map()
Bram Moolenaar292eff02017-07-11 21:46:28 +0200147 new
148 " Outside of the range, minimum
149 inoremap <Char-0x1040> a
150 execute "normal a\u1040\<Esc>"
151 " Inside of the range, minimum
152 inoremap <Char-0x103f> b
153 execute "normal a\u103f\<Esc>"
154 " Inside of the range, maximum
155 inoremap <Char-0xf03f> c
156 execute "normal a\uf03f\<Esc>"
157 " Outside of the range, maximum
158 inoremap <Char-0xf040> d
159 execute "normal a\uf040\<Esc>"
160 call assert_equal("abcd", getline(1))
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200161endfunc
162
163func One_mapset_test(keys)
164 exe 'nnoremap ' .. a:keys .. ' original<CR>'
165 let orig = maparg(a:keys, 'n', 0, 1)
166 call assert_equal(a:keys, orig.lhs)
167 call assert_equal('original<CR>', orig.rhs)
168 call assert_equal('n', orig.mode)
169
170 exe 'nunmap ' .. a:keys
171 let d = maparg(a:keys, 'n', 0, 1)
172 call assert_equal({}, d)
173
174 call mapset('n', 0, orig)
175 let d = maparg(a:keys, 'n', 0, 1)
176 call assert_equal(a:keys, d.lhs)
177 call assert_equal('original<CR>', d.rhs)
178 call assert_equal('n', d.mode)
179
180 exe 'nunmap ' .. a:keys
181endfunc
182
183func Test_mapset()
184 call One_mapset_test('K')
185 call One_mapset_test('<F3>')
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200186
187 " Check <> key conversion
188 new
189 inoremap K one<Left>x
190 call feedkeys("iK\<Esc>", 'xt')
191 call assert_equal('onxe', getline(1))
192
193 let orig = maparg('K', 'i', 0, 1)
194 call assert_equal('K', orig.lhs)
195 call assert_equal('one<Left>x', orig.rhs)
196 call assert_equal('i', orig.mode)
197
198 iunmap K
199 let d = maparg('K', 'i', 0, 1)
200 call assert_equal({}, d)
201
202 call mapset('i', 0, orig)
203 call feedkeys("SK\<Esc>", 'xt')
204 call assert_equal('onxe', getline(1))
205
206 iunmap K
207
208 " Test literal <CR> using a backslash
209 let cpo_save = &cpo
210 set cpo-=B
211 inoremap K one\<CR>two
212 call feedkeys("SK\<Esc>", 'xt')
213 call assert_equal('one<CR>two', getline(1))
214
215 let orig = maparg('K', 'i', 0, 1)
216 call assert_equal('K', orig.lhs)
217 call assert_equal('one\<CR>two', orig.rhs)
218 call assert_equal('i', orig.mode)
219
220 iunmap K
221 let d = maparg('K', 'i', 0, 1)
222 call assert_equal({}, d)
223
224 call mapset('i', 0, orig)
225 call feedkeys("SK\<Esc>", 'xt')
226 call assert_equal('one<CR>two', getline(1))
227
228 iunmap K
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200229
230 " Test literal <CR> using CTRL-V
231 inoremap K one<CR>two
232 call feedkeys("SK\<Esc>", 'xt')
233 call assert_equal('one<CR>two', getline(1))
234
235 let orig = maparg('K', 'i', 0, 1)
236 call assert_equal('K', orig.lhs)
237 call assert_equal("one\x16<CR>two", orig.rhs)
238 call assert_equal('i', orig.mode)
239
240 iunmap K
241 let d = maparg('K', 'i', 0, 1)
242 call assert_equal({}, d)
243
244 call mapset('i', 0, orig)
245 call feedkeys("SK\<Esc>", 'xt')
246 call assert_equal('one<CR>two', getline(1))
247
248 iunmap K
249 let &cpo = cpo_save
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200250 bwipe!
Bram Moolenaar1b912982020-09-29 21:45:41 +0200251
252 call assert_fails('call mapset([], v:false, {})', 'E730:')
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100253 call assert_fails('call mapset("i", 0, "")', 'E716:')
254 call assert_fails('call mapset("i", 0, {})', 'E460:')
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200255endfunc
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100256
Bram Moolenaar9c652532020-05-24 13:10:18 +0200257func Check_ctrlb_map(d, check_alt)
258 call assert_equal('<C-B>', a:d.lhs)
259 if a:check_alt
260 call assert_equal("\x80\xfc\x04B", a:d.lhsraw)
261 call assert_equal("\x02", a:d.lhsrawalt)
262 else
263 call assert_equal("\x02", a:d.lhsraw)
264 endif
265endfunc
266
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +0200267func Test_map_local()
268 nmap a global
269 nmap <buffer>a local
270
271 let prev_map_list = split(execute('nmap a'), "\n")
272 call assert_match('n\s*a\s*@local', prev_map_list[0])
273 call assert_match('n\s*a\s*global', prev_map_list[1])
274
275 let mapping = maparg('a', 'n', 0, 1)
276 call assert_equal(1, mapping.buffer)
277 let mapping.rhs = 'new_local'
278 call mapset('n', 0, mapping)
279
280 " Check that the global mapping is left untouched.
281 let map_list = split(execute('nmap a'), "\n")
282 call assert_match('n\s*a\s*@new_local', map_list[0])
283 call assert_match('n\s*a\s*global', map_list[1])
284
285 nunmap a
286endfunc
287
Bram Moolenaar9c652532020-05-24 13:10:18 +0200288func Test_map_restore()
289 " Test restoring map with alternate keycode
290 nmap <C-B> back
291 let d = maparg('<C-B>', 'n', 0, 1)
292 call Check_ctrlb_map(d, 1)
293 let dsimp = maparg("\x02", 'n', 0, 1)
294 call Check_ctrlb_map(dsimp, 0)
295 nunmap <C-B>
296 call mapset('n', 0, d)
297 let d = maparg('<C-B>', 'n', 0, 1)
298 call Check_ctrlb_map(d, 1)
299 let dsimp = maparg("\x02", 'n', 0, 1)
300 call Check_ctrlb_map(dsimp, 0)
301
302 nunmap <C-B>
303
304endfunc
305
Ernie Rael09661202022-04-25 14:40:44 +0100306def Test_maplist()
Ernie Rael659c2402022-04-24 18:40:28 +0100307 new
Ernie Rael09661202022-04-25 14:40:44 +0100308 def ClearMappingsAbbreviations()
Ernie Rael659c2402022-04-24 18:40:28 +0100309 mapclear | nmapclear | vmapclear | xmapclear | smapclear | omapclear
310 mapclear! | imapclear | lmapclear | cmapclear | tmapclear
311 mapclear <buffer> | nmapclear <buffer> | vmapclear <buffer>
312 xmapclear <buffer> | smapclear <buffer> | omapclear <buffer>
313 mapclear! <buffer> | imapclear <buffer> | lmapclear <buffer>
314 cmapclear <buffer> | tmapclear <buffer>
Ernie Rael09661202022-04-25 14:40:44 +0100315 abclear | abclear <buffer>
Ernie Rael659c2402022-04-24 18:40:28 +0100316 enddef
317
318 def AddMaps(new: list<string>, accum: list<string>)
319 if len(new) > 0 && new[0] != "No mapping found"
320 accum->extend(new)
321 endif
322 enddef
323
Ernie Rael09661202022-04-25 14:40:44 +0100324 ClearMappingsAbbreviations()
325 assert_equal(0, len(maplist()))
326 assert_equal(0, len(maplist(true)))
Ernie Rael659c2402022-04-24 18:40:28 +0100327
328 # Set up some mappings.
329 map dup bar
330 map <buffer> dup bufbar
331 map foo<C-V> is<F4>foo
332 vnoremap <script> <buffer> <expr> <silent> bar isbar
333 tmap baz foo
334 omap h w
335 lmap i w
336 nmap j w
337 xmap k w
338 smap l w
339 map abc <Nop>
340 nmap <M-j> x
341 nmap <M-Space> y
Ernie Rael09661202022-04-25 14:40:44 +0100342 # And abbreviations
343 abbreviate xy he
344 abbreviate xx she
345 abbreviate <buffer> x they
Ernie Rael659c2402022-04-24 18:40:28 +0100346
347 # Get a list of the mappings with the ':map' commands.
Ernie Rael09661202022-04-25 14:40:44 +0100348 # Check maplist() return a list of the same size.
349 assert_equal(13, len(maplist()))
350 assert_equal(3, len(maplist(true)))
351 assert_equal(13, len(maplist(false)))
Ernie Rael659c2402022-04-24 18:40:28 +0100352
353 # collect all the current maps using :map commands
354 var maps_command: list<string>
355 AddMaps(split(execute('map'), '\n'), maps_command)
356 AddMaps(split(execute('map!'), '\n'), maps_command)
357 AddMaps(split(execute('tmap'), '\n'), maps_command)
358 AddMaps(split(execute('lmap'), '\n'), maps_command)
359
Ernie Rael09661202022-04-25 14:40:44 +0100360 # Use maplist to get all the maps
361 var maps_maplist = maplist()
362 assert_equal(len(maps_command), len(maps_maplist))
Ernie Rael659c2402022-04-24 18:40:28 +0100363
364 # make sure all the mode-lhs are unique, no duplicates
365 var map_set: dict<number>
Ernie Rael09661202022-04-25 14:40:44 +0100366 for d in maps_maplist
Ernie Rael659c2402022-04-24 18:40:28 +0100367 map_set[d.mode .. "-" .. d.lhs .. "-" .. d.buffer] = 0
368 endfor
Ernie Rael09661202022-04-25 14:40:44 +0100369 assert_equal(len(maps_maplist), len(map_set))
Ernie Rael659c2402022-04-24 18:40:28 +0100370
Ernie Rael09661202022-04-25 14:40:44 +0100371 # For everything returned by maplist, should be the same as from maparg.
Ernie Rael659c2402022-04-24 18:40:28 +0100372 # Except for "map dup", bacause maparg returns the <buffer> version
Ernie Rael09661202022-04-25 14:40:44 +0100373 for d in maps_maplist
Ernie Rael659c2402022-04-24 18:40:28 +0100374 if d.lhs == 'dup' && d.buffer == 0
375 continue
376 endif
377 var d_maparg = maparg(d.lhs, d.mode, false, true)
378 assert_equal(d_maparg, d)
379 endfor
380
Ernie Rael09661202022-04-25 14:40:44 +0100381 # Check abbr matches maparg
382 for d in maplist(true)
383 # Note, d.mode is '!', but can't use that with maparg
384 var d_maparg = maparg(d.lhs, 'i', true, true)
385 assert_equal(d_maparg, d)
386 endfor
387
388 ClearMappingsAbbreviations()
389 assert_equal(0, len(maplist()))
390 assert_equal(0, len(maplist(true)))
Ernie Rael659c2402022-04-24 18:40:28 +0100391enddef
392
393
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100394" vim: shiftwidth=2 sts=2 expandtab