blob: 6e73b39553135cfd6e5b2810d71e177222f9099d [file] [log] [blame]
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02001" Tests for maparg(), mapcheck() and mapset().
Bram Moolenaar292eff02017-07-11 21:46:28 +02002" Also test utf8 map with a 0x80 byte.
Bram Moolenaara1449832019-09-01 20:16:52 +02003" Also test mapcheck()
Bram Moolenaar292eff02017-07-11 21:46:28 +02004
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02005func s:SID()
Bram Moolenaar292eff02017-07-11 21:46:28 +02006 return str2nr(matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$'))
Bram Moolenaar4c9243f2020-05-22 13:10:44 +02007endfunc
Bram Moolenaar292eff02017-07-11 21:46:28 +02008
Bram Moolenaarc94c1462020-05-22 20:01:06 +02009func Test_maparg()
Bram Moolenaar292eff02017-07-11 21:46:28 +020010 new
11 set cpo-=<
12 set encoding=utf8
13 " Test maparg() with a string result
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020014 let sid = s:SID()
15 let lnum = expand('<sflnum>')
Bram Moolenaar292eff02017-07-11 21:46:28 +020016 map foo<C-V> is<F4>foo
17 vnoremap <script> <buffer> <expr> <silent> bar isbar
Bram Moolenaar292eff02017-07-11 21:46:28 +020018 call assert_equal("is<F4>foo", maparg('foo<C-V>'))
Bram Moolenaar2da0f0c2020-04-01 19:22:12 +020019 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo<C-V>',
Bram Moolenaar9c652532020-05-24 13:10:18 +020020 \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16",
Bram Moolenaara9528b32022-01-18 20:51:35 +000021 \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
22 \ 'lnum': lnum + 1,
Bram Moolenaar9c652532020-05-24 13:10:18 +020023 \ 'rhs': 'is<F4>foo', 'buffer': 0},
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020024 \ maparg('foo<C-V>', '', 0, 1))
Bram Moolenaar9c652532020-05-24 13:10:18 +020025 call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar',
26 \ 'lhsraw': 'bar', 'mode': 'v',
Bram Moolenaara9528b32022-01-18 20:51:35 +000027 \ 'nowait': 0, 'expr': 1, 'sid': sid, 'scriptversion': 1,
28 \ 'lnum': lnum + 2,
Bram Moolenaar9c652532020-05-24 13:10:18 +020029 \ 'rhs': 'isbar', 'buffer': 1},
Bram Moolenaara1449832019-09-01 20:16:52 +020030 \ 'bar'->maparg('', 0, 1))
Bram Moolenaarf29c1c62018-09-10 21:05:02 +020031 let lnum = expand('<sflnum>')
Bram Moolenaar292eff02017-07-11 21:46:28 +020032 map <buffer> <nowait> foo bar
Bram Moolenaar9c652532020-05-24 13:10:18 +020033 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo',
34 \ 'lhsraw': 'foo', 'mode': ' ',
Bram Moolenaara9528b32022-01-18 20:51:35 +000035 \ 'nowait': 1, 'expr': 0, 'sid': sid, 'scriptversion': 1,
36 \ 'lnum': lnum + 1, 'rhs': 'bar',
Bram Moolenaar9c652532020-05-24 13:10:18 +020037 \ 'buffer': 1},
Bram Moolenaar292eff02017-07-11 21:46:28 +020038 \ maparg('foo', '', 0, 1))
Bram Moolenaar14371ed2019-07-27 21:05:21 +020039 let lnum = expand('<sflnum>')
40 tmap baz foo
Bram Moolenaar9c652532020-05-24 13:10:18 +020041 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz',
42 \ 'lhsraw': 'baz', 'mode': 't',
Bram Moolenaara9528b32022-01-18 20:51:35 +000043 \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
44 \ 'lnum': lnum + 1, 'rhs': 'foo',
Bram Moolenaar9c652532020-05-24 13:10:18 +020045 \ 'buffer': 0},
Bram Moolenaar14371ed2019-07-27 21:05:21 +020046 \ maparg('baz', 't', 0, 1))
Bram Moolenaar292eff02017-07-11 21:46:28 +020047
48 map abc x<char-114>x
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020049 call assert_equal("xrx", maparg('abc'))
Bram Moolenaar292eff02017-07-11 21:46:28 +020050 map abc y<S-char-114>y
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020051 call assert_equal("yRy", maparg('abc'))
52
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +010053 omap { w
54 let d = maparg('{', 'o', 0, 1)
55 call assert_equal(['{', 'w', 'o'], [d.lhs, d.rhs, d.mode])
56 ounmap {
57
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +010058 lmap { w
59 let d = maparg('{', 'l', 0, 1)
60 call assert_equal(['{', 'w', 'l'], [d.lhs, d.rhs, d.mode])
61 lunmap {
62
63 nmap { w
64 let d = maparg('{', 'n', 0, 1)
65 call assert_equal(['{', 'w', 'n'], [d.lhs, d.rhs, d.mode])
66 nunmap {
67
68 xmap { w
69 let d = maparg('{', 'x', 0, 1)
70 call assert_equal(['{', 'w', 'x'], [d.lhs, d.rhs, d.mode])
71 xunmap {
72
73 smap { w
74 let d = maparg('{', 's', 0, 1)
75 call assert_equal(['{', 'w', 's'], [d.lhs, d.rhs, d.mode])
76 sunmap {
77
Bram Moolenaarf88a5bc2018-05-21 13:28:44 +020078 map abc <Nop>
79 call assert_equal("<Nop>", maparg('abc'))
80 unmap abc
Bram Moolenaarc2a60ae2020-01-23 16:19:54 +010081
82 call feedkeys(":abbr esc \<C-V>\<C-V>\<C-V>\<C-V>\<C-V>\<Esc>\<CR>", "xt")
83 let d = maparg('esc', 'i', 1, 1)
84 call assert_equal(['esc', "\<C-V>\<C-V>\<Esc>", '!'], [d.lhs, d.rhs, d.mode])
85 abclear
Bram Moolenaar4c9243f2020-05-22 13:10:44 +020086endfunc
Bram Moolenaar292eff02017-07-11 21:46:28 +020087
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +020088def Test_vim9_maparg()
89 nmap { w
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +020090 var one: string = maparg('{')
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +020091 assert_equal('w', one)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +020092 var two: string = maparg('{', 'n')
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +020093 assert_equal('w', two)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +020094 var three: string = maparg('{', 'n', 0)
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +020095 assert_equal('w', three)
Bram Moolenaar7a9cbca2020-09-27 22:47:05 +020096 var four: dict<any> = maparg('{', 'n', 0, 1)
Bram Moolenaard2c61702020-09-06 15:58:36 +020097 assert_equal(['{', 'w', 'n'], [four.lhs, four.rhs, four.mode])
Bram Moolenaar4a6d1b62020-08-08 17:55:49 +020098 nunmap {
99enddef
100
Bram Moolenaara1449832019-09-01 20:16:52 +0200101func Test_mapcheck()
102 call assert_equal('', mapcheck('a'))
103 call assert_equal('', mapcheck('abc'))
104 call assert_equal('', mapcheck('ax'))
105 call assert_equal('', mapcheck('b'))
106
107 map a something
108 call assert_equal('something', mapcheck('a'))
109 call assert_equal('something', mapcheck('a', 'n'))
110 call assert_equal('', mapcheck('a', 'c'))
111 call assert_equal('', mapcheck('a', 'i'))
112 call assert_equal('something', 'abc'->mapcheck())
113 call assert_equal('something', 'ax'->mapcheck())
114 call assert_equal('', mapcheck('b'))
115 unmap a
116
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
124 map abc barfoo
125 call assert_equal('barfoo', mapcheck('a'))
126 call assert_equal('barfoo', mapcheck('a', 'n', 0))
127 call assert_equal('', mapcheck('a', 'n', 1))
128 call assert_equal('barfoo', mapcheck('abc'))
129 call assert_equal('', mapcheck('ax'))
130 call assert_equal('', mapcheck('b'))
131 unmap abc
132
133 abbr ab abbrev
134 call assert_equal('abbrev', mapcheck('a', 'i', 1))
135 call assert_equal('', mapcheck('a', 'n', 1))
136 call assert_equal('', mapcheck('a', 'i', 0))
137 unabbr ab
138endfunc
139
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200140func Test_range_map()
Bram Moolenaar292eff02017-07-11 21:46:28 +0200141 new
142 " Outside of the range, minimum
143 inoremap <Char-0x1040> a
144 execute "normal a\u1040\<Esc>"
145 " Inside of the range, minimum
146 inoremap <Char-0x103f> b
147 execute "normal a\u103f\<Esc>"
148 " Inside of the range, maximum
149 inoremap <Char-0xf03f> c
150 execute "normal a\uf03f\<Esc>"
151 " Outside of the range, maximum
152 inoremap <Char-0xf040> d
153 execute "normal a\uf040\<Esc>"
154 call assert_equal("abcd", getline(1))
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200155endfunc
156
157func One_mapset_test(keys)
158 exe 'nnoremap ' .. a:keys .. ' original<CR>'
159 let orig = maparg(a:keys, 'n', 0, 1)
160 call assert_equal(a:keys, orig.lhs)
161 call assert_equal('original<CR>', orig.rhs)
162 call assert_equal('n', orig.mode)
163
164 exe 'nunmap ' .. a:keys
165 let d = maparg(a:keys, 'n', 0, 1)
166 call assert_equal({}, d)
167
168 call mapset('n', 0, orig)
169 let d = maparg(a:keys, 'n', 0, 1)
170 call assert_equal(a:keys, d.lhs)
171 call assert_equal('original<CR>', d.rhs)
172 call assert_equal('n', d.mode)
173
174 exe 'nunmap ' .. a:keys
175endfunc
176
177func Test_mapset()
178 call One_mapset_test('K')
179 call One_mapset_test('<F3>')
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200180
181 " Check <> key conversion
182 new
183 inoremap K one<Left>x
184 call feedkeys("iK\<Esc>", 'xt')
185 call assert_equal('onxe', getline(1))
186
187 let orig = maparg('K', 'i', 0, 1)
188 call assert_equal('K', orig.lhs)
189 call assert_equal('one<Left>x', orig.rhs)
190 call assert_equal('i', orig.mode)
191
192 iunmap K
193 let d = maparg('K', 'i', 0, 1)
194 call assert_equal({}, d)
195
196 call mapset('i', 0, orig)
197 call feedkeys("SK\<Esc>", 'xt')
198 call assert_equal('onxe', getline(1))
199
200 iunmap K
201
202 " Test literal <CR> using a backslash
203 let cpo_save = &cpo
204 set cpo-=B
205 inoremap K one\<CR>two
206 call feedkeys("SK\<Esc>", 'xt')
207 call assert_equal('one<CR>two', getline(1))
208
209 let orig = maparg('K', 'i', 0, 1)
210 call assert_equal('K', orig.lhs)
211 call assert_equal('one\<CR>two', orig.rhs)
212 call assert_equal('i', orig.mode)
213
214 iunmap K
215 let d = maparg('K', 'i', 0, 1)
216 call assert_equal({}, d)
217
218 call mapset('i', 0, orig)
219 call feedkeys("SK\<Esc>", 'xt')
220 call assert_equal('one<CR>two', getline(1))
221
222 iunmap K
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200223
224 " Test literal <CR> using CTRL-V
225 inoremap K one<CR>two
226 call feedkeys("SK\<Esc>", 'xt')
227 call assert_equal('one<CR>two', getline(1))
228
229 let orig = maparg('K', 'i', 0, 1)
230 call assert_equal('K', orig.lhs)
231 call assert_equal("one\x16<CR>two", orig.rhs)
232 call assert_equal('i', orig.mode)
233
234 iunmap K
235 let d = maparg('K', 'i', 0, 1)
236 call assert_equal({}, d)
237
238 call mapset('i', 0, orig)
239 call feedkeys("SK\<Esc>", 'xt')
240 call assert_equal('one<CR>two', getline(1))
241
242 iunmap K
243 let &cpo = cpo_save
Bram Moolenaarc94c1462020-05-22 20:01:06 +0200244 bwipe!
Bram Moolenaar1b912982020-09-29 21:45:41 +0200245
246 call assert_fails('call mapset([], v:false, {})', 'E730:')
Bram Moolenaara1070ea2021-02-20 19:21:36 +0100247 call assert_fails('call mapset("i", 0, "")', 'E716:')
248 call assert_fails('call mapset("i", 0, {})', 'E460:')
Bram Moolenaar4c9243f2020-05-22 13:10:44 +0200249endfunc
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100250
Bram Moolenaar9c652532020-05-24 13:10:18 +0200251func Check_ctrlb_map(d, check_alt)
252 call assert_equal('<C-B>', a:d.lhs)
253 if a:check_alt
254 call assert_equal("\x80\xfc\x04B", a:d.lhsraw)
255 call assert_equal("\x02", a:d.lhsrawalt)
256 else
257 call assert_equal("\x02", a:d.lhsraw)
258 endif
259endfunc
260
Bram Moolenaar7ba1e4d2021-04-24 13:12:38 +0200261func Test_map_local()
262 nmap a global
263 nmap <buffer>a local
264
265 let prev_map_list = split(execute('nmap a'), "\n")
266 call assert_match('n\s*a\s*@local', prev_map_list[0])
267 call assert_match('n\s*a\s*global', prev_map_list[1])
268
269 let mapping = maparg('a', 'n', 0, 1)
270 call assert_equal(1, mapping.buffer)
271 let mapping.rhs = 'new_local'
272 call mapset('n', 0, mapping)
273
274 " Check that the global mapping is left untouched.
275 let map_list = split(execute('nmap a'), "\n")
276 call assert_match('n\s*a\s*@new_local', map_list[0])
277 call assert_match('n\s*a\s*global', map_list[1])
278
279 nunmap a
280endfunc
281
Bram Moolenaar9c652532020-05-24 13:10:18 +0200282func Test_map_restore()
283 " Test restoring map with alternate keycode
284 nmap <C-B> back
285 let d = maparg('<C-B>', 'n', 0, 1)
286 call Check_ctrlb_map(d, 1)
287 let dsimp = maparg("\x02", 'n', 0, 1)
288 call Check_ctrlb_map(dsimp, 0)
289 nunmap <C-B>
290 call mapset('n', 0, d)
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
296 nunmap <C-B>
297
298endfunc
299
Ernie Rael659c2402022-04-24 18:40:28 +0100300def Test_getmappings()
301 new
302 def ClearMaps()
303 mapclear | nmapclear | vmapclear | xmapclear | smapclear | omapclear
304 mapclear! | imapclear | lmapclear | cmapclear | tmapclear
305 mapclear <buffer> | nmapclear <buffer> | vmapclear <buffer>
306 xmapclear <buffer> | smapclear <buffer> | omapclear <buffer>
307 mapclear! <buffer> | imapclear <buffer> | lmapclear <buffer>
308 cmapclear <buffer> | tmapclear <buffer>
309 enddef
310
311 def AddMaps(new: list<string>, accum: list<string>)
312 if len(new) > 0 && new[0] != "No mapping found"
313 accum->extend(new)
314 endif
315 enddef
316
317 ClearMaps()
318 assert_equal(0, len(getmappings()))
319
320 # Set up some mappings.
321 map dup bar
322 map <buffer> dup bufbar
323 map foo<C-V> is<F4>foo
324 vnoremap <script> <buffer> <expr> <silent> bar isbar
325 tmap baz foo
326 omap h w
327 lmap i w
328 nmap j w
329 xmap k w
330 smap l w
331 map abc <Nop>
332 nmap <M-j> x
333 nmap <M-Space> y
334
335 # Get a list of the mappings with the ':map' commands.
336 # Check getmappings() return a list of the same size.
337 assert_equal(13, len(getmappings()))
338
339 # collect all the current maps using :map commands
340 var maps_command: list<string>
341 AddMaps(split(execute('map'), '\n'), maps_command)
342 AddMaps(split(execute('map!'), '\n'), maps_command)
343 AddMaps(split(execute('tmap'), '\n'), maps_command)
344 AddMaps(split(execute('lmap'), '\n'), maps_command)
345
346 # Use getmappings to get all the maps
347 var maps_getmappings = getmappings()
348 assert_equal(len(maps_command), len(maps_getmappings))
349
350 # make sure all the mode-lhs are unique, no duplicates
351 var map_set: dict<number>
352 for d in maps_getmappings
353 map_set[d.mode .. "-" .. d.lhs .. "-" .. d.buffer] = 0
354 endfor
355 assert_equal(len(maps_getmappings), len(map_set))
356
357 # For everything returned by getmappings, should be the same as from maparg.
358 # Except for "map dup", bacause maparg returns the <buffer> version
359 for d in maps_getmappings
360 if d.lhs == 'dup' && d.buffer == 0
361 continue
362 endif
363 var d_maparg = maparg(d.lhs, d.mode, false, true)
364 assert_equal(d_maparg, d)
365 endfor
366
367 ClearMaps()
368 assert_equal(0, len(getmappings()))
369enddef
370
371
Bram Moolenaar8ba6bb72020-01-20 20:41:42 +0100372" vim: shiftwidth=2 sts=2 expandtab