comparison src/testdir/test_map_functions.vim @ 28602:398c5b3211f9 v8.2.4825

patch 8.2.4825: can only get a list of mappings Commit: https://github.com/vim/vim/commit/09661203ecefbee6a6f09438afcff1843e9dbfb4 Author: Ernie Rael <errael@raelity.com> Date: Mon Apr 25 14:40:44 2022 +0100 patch 8.2.4825: can only get a list of mappings Problem: Can only get a list of mappings. Solution: Add the optional {abbr} argument. (Ernie Rael, closes https://github.com/vim/vim/issues/10277) Rename to maplist(). Rename test file.
author Bram Moolenaar <Bram@vim.org>
date Mon, 25 Apr 2022 15:45:03 +0200
parents src/testdir/test_maparg.vim@d3c966c0cdf7
children 9ae2c32841fb
comparison
equal deleted inserted replaced
28601:a4d54a260c59 28602:398c5b3211f9
1 " Tests for maparg(), mapcheck(), mapset(), maplist()
2 " Also test utf8 map with a 0x80 byte.
3
4 func s:SID()
5 return str2nr(matchstr(expand('<sfile>'), '<SNR>\zs\d\+\ze_SID$'))
6 endfunc
7
8 func Test_maparg()
9 new
10 set cpo-=<
11 set encoding=utf8
12 " Test maparg() with a string result
13 let sid = s:SID()
14 let lnum = expand('<sflnum>')
15 map foo<C-V> is<F4>foo
16 vnoremap <script> <buffer> <expr> <silent> bar isbar
17 call assert_equal("is<F4>foo", maparg('foo<C-V>'))
18 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo<C-V>',
19 \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16",
20 \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
21 \ 'lnum': lnum + 1,
22 \ 'rhs': 'is<F4>foo', 'buffer': 0},
23 \ maparg('foo<C-V>', '', 0, 1))
24 call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar',
25 \ 'lhsraw': 'bar', 'mode': 'v',
26 \ 'nowait': 0, 'expr': 1, 'sid': sid, 'scriptversion': 1,
27 \ 'lnum': lnum + 2,
28 \ 'rhs': 'isbar', 'buffer': 1},
29 \ 'bar'->maparg('', 0, 1))
30 let lnum = expand('<sflnum>')
31 map <buffer> <nowait> foo bar
32 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo',
33 \ 'lhsraw': 'foo', 'mode': ' ',
34 \ 'nowait': 1, 'expr': 0, 'sid': sid, 'scriptversion': 1,
35 \ 'lnum': lnum + 1, 'rhs': 'bar',
36 \ 'buffer': 1},
37 \ maparg('foo', '', 0, 1))
38 let lnum = expand('<sflnum>')
39 tmap baz foo
40 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz',
41 \ 'lhsraw': 'baz', 'mode': 't',
42 \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
43 \ 'lnum': lnum + 1, 'rhs': 'foo',
44 \ 'buffer': 0},
45 \ maparg('baz', 't', 0, 1))
46
47 map abc x<char-114>x
48 call assert_equal("xrx", maparg('abc'))
49 map abc y<S-char-114>y
50 call assert_equal("yRy", maparg('abc'))
51
52 omap { w
53 let d = maparg('{', 'o', 0, 1)
54 call assert_equal(['{', 'w', 'o'], [d.lhs, d.rhs, d.mode])
55 ounmap {
56
57 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
77 map abc <Nop>
78 call assert_equal("<Nop>", maparg('abc'))
79 unmap abc
80
81 call feedkeys(":abbr esc \<C-V>\<C-V>\<C-V>\<C-V>\<C-V>\<Esc>\<CR>", "xt")
82 let d = maparg('esc', 'i', 1, 1)
83 call assert_equal(['esc', "\<C-V>\<C-V>\<Esc>", '!'], [d.lhs, d.rhs, d.mode])
84 abclear
85 endfunc
86
87 def Test_vim9_maparg()
88 nmap { w
89 var one: string = maparg('{')
90 assert_equal('w', one)
91 var two: string = maparg('{', 'n')
92 assert_equal('w', two)
93 var three: string = maparg('{', 'n', 0)
94 assert_equal('w', three)
95 var four: dict<any> = maparg('{', 'n', 0, 1)
96 assert_equal(['{', 'w', 'n'], [four.lhs, four.rhs, four.mode])
97 nunmap {
98 enddef
99
100 func Test_mapcheck()
101 call assert_equal('', mapcheck('a'))
102 call assert_equal('', mapcheck('abc'))
103 call assert_equal('', mapcheck('ax'))
104 call assert_equal('', mapcheck('b'))
105
106 map a something
107 call assert_equal('something', mapcheck('a'))
108 call assert_equal('something', mapcheck('a', 'n'))
109 call assert_equal('', mapcheck('a', 'c'))
110 call assert_equal('', mapcheck('a', 'i'))
111 call assert_equal('something', 'abc'->mapcheck())
112 call assert_equal('something', 'ax'->mapcheck())
113 call assert_equal('', mapcheck('b'))
114 unmap a
115
116 map ab foobar
117 call assert_equal('foobar', mapcheck('a'))
118 call assert_equal('foobar', mapcheck('abc'))
119 call assert_equal('', mapcheck('ax'))
120 call assert_equal('', mapcheck('b'))
121 unmap ab
122
123 map abc barfoo
124 call assert_equal('barfoo', mapcheck('a'))
125 call assert_equal('barfoo', mapcheck('a', 'n', 0))
126 call assert_equal('', mapcheck('a', 'n', 1))
127 call assert_equal('barfoo', mapcheck('abc'))
128 call assert_equal('', mapcheck('ax'))
129 call assert_equal('', mapcheck('b'))
130 unmap abc
131
132 abbr ab abbrev
133 call assert_equal('abbrev', mapcheck('a', 'i', 1))
134 call assert_equal('', mapcheck('a', 'n', 1))
135 call assert_equal('', mapcheck('a', 'i', 0))
136 unabbr ab
137 endfunc
138
139 func Test_range_map()
140 new
141 " Outside of the range, minimum
142 inoremap <Char-0x1040> a
143 execute "normal a\u1040\<Esc>"
144 " Inside of the range, minimum
145 inoremap <Char-0x103f> b
146 execute "normal a\u103f\<Esc>"
147 " Inside of the range, maximum
148 inoremap <Char-0xf03f> c
149 execute "normal a\uf03f\<Esc>"
150 " Outside of the range, maximum
151 inoremap <Char-0xf040> d
152 execute "normal a\uf040\<Esc>"
153 call assert_equal("abcd", getline(1))
154 endfunc
155
156 func One_mapset_test(keys)
157 exe 'nnoremap ' .. a:keys .. ' original<CR>'
158 let orig = maparg(a:keys, 'n', 0, 1)
159 call assert_equal(a:keys, orig.lhs)
160 call assert_equal('original<CR>', orig.rhs)
161 call assert_equal('n', orig.mode)
162
163 exe 'nunmap ' .. a:keys
164 let d = maparg(a:keys, 'n', 0, 1)
165 call assert_equal({}, d)
166
167 call mapset('n', 0, orig)
168 let d = maparg(a:keys, 'n', 0, 1)
169 call assert_equal(a:keys, d.lhs)
170 call assert_equal('original<CR>', d.rhs)
171 call assert_equal('n', d.mode)
172
173 exe 'nunmap ' .. a:keys
174 endfunc
175
176 func Test_mapset()
177 call One_mapset_test('K')
178 call One_mapset_test('<F3>')
179
180 " Check <> key conversion
181 new
182 inoremap K one<Left>x
183 call feedkeys("iK\<Esc>", 'xt')
184 call assert_equal('onxe', getline(1))
185
186 let orig = maparg('K', 'i', 0, 1)
187 call assert_equal('K', orig.lhs)
188 call assert_equal('one<Left>x', orig.rhs)
189 call assert_equal('i', orig.mode)
190
191 iunmap K
192 let d = maparg('K', 'i', 0, 1)
193 call assert_equal({}, d)
194
195 call mapset('i', 0, orig)
196 call feedkeys("SK\<Esc>", 'xt')
197 call assert_equal('onxe', getline(1))
198
199 iunmap K
200
201 " Test literal <CR> using a backslash
202 let cpo_save = &cpo
203 set cpo-=B
204 inoremap K one\<CR>two
205 call feedkeys("SK\<Esc>", 'xt')
206 call assert_equal('one<CR>two', getline(1))
207
208 let orig = maparg('K', 'i', 0, 1)
209 call assert_equal('K', orig.lhs)
210 call assert_equal('one\<CR>two', orig.rhs)
211 call assert_equal('i', orig.mode)
212
213 iunmap K
214 let d = maparg('K', 'i', 0, 1)
215 call assert_equal({}, d)
216
217 call mapset('i', 0, orig)
218 call feedkeys("SK\<Esc>", 'xt')
219 call assert_equal('one<CR>two', getline(1))
220
221 iunmap K
222
223 " Test literal <CR> using CTRL-V
224 inoremap K one<CR>two
225 call feedkeys("SK\<Esc>", 'xt')
226 call assert_equal('one<CR>two', getline(1))
227
228 let orig = maparg('K', 'i', 0, 1)
229 call assert_equal('K', orig.lhs)
230 call assert_equal("one\x16<CR>two", orig.rhs)
231 call assert_equal('i', orig.mode)
232
233 iunmap K
234 let d = maparg('K', 'i', 0, 1)
235 call assert_equal({}, d)
236
237 call mapset('i', 0, orig)
238 call feedkeys("SK\<Esc>", 'xt')
239 call assert_equal('one<CR>two', getline(1))
240
241 iunmap K
242 let &cpo = cpo_save
243 bwipe!
244
245 call assert_fails('call mapset([], v:false, {})', 'E730:')
246 call assert_fails('call mapset("i", 0, "")', 'E716:')
247 call assert_fails('call mapset("i", 0, {})', 'E460:')
248 endfunc
249
250 func Check_ctrlb_map(d, check_alt)
251 call assert_equal('<C-B>', a:d.lhs)
252 if a:check_alt
253 call assert_equal("\x80\xfc\x04B", a:d.lhsraw)
254 call assert_equal("\x02", a:d.lhsrawalt)
255 else
256 call assert_equal("\x02", a:d.lhsraw)
257 endif
258 endfunc
259
260 func Test_map_local()
261 nmap a global
262 nmap <buffer>a local
263
264 let prev_map_list = split(execute('nmap a'), "\n")
265 call assert_match('n\s*a\s*@local', prev_map_list[0])
266 call assert_match('n\s*a\s*global', prev_map_list[1])
267
268 let mapping = maparg('a', 'n', 0, 1)
269 call assert_equal(1, mapping.buffer)
270 let mapping.rhs = 'new_local'
271 call mapset('n', 0, mapping)
272
273 " Check that the global mapping is left untouched.
274 let map_list = split(execute('nmap a'), "\n")
275 call assert_match('n\s*a\s*@new_local', map_list[0])
276 call assert_match('n\s*a\s*global', map_list[1])
277
278 nunmap a
279 endfunc
280
281 func Test_map_restore()
282 " Test restoring map with alternate keycode
283 nmap <C-B> back
284 let d = maparg('<C-B>', 'n', 0, 1)
285 call Check_ctrlb_map(d, 1)
286 let dsimp = maparg("\x02", 'n', 0, 1)
287 call Check_ctrlb_map(dsimp, 0)
288 nunmap <C-B>
289 call mapset('n', 0, d)
290 let d = maparg('<C-B>', 'n', 0, 1)
291 call Check_ctrlb_map(d, 1)
292 let dsimp = maparg("\x02", 'n', 0, 1)
293 call Check_ctrlb_map(dsimp, 0)
294
295 nunmap <C-B>
296
297 endfunc
298
299 def Test_maplist()
300 new
301 def ClearMappingsAbbreviations()
302 mapclear | nmapclear | vmapclear | xmapclear | smapclear | omapclear
303 mapclear! | imapclear | lmapclear | cmapclear | tmapclear
304 mapclear <buffer> | nmapclear <buffer> | vmapclear <buffer>
305 xmapclear <buffer> | smapclear <buffer> | omapclear <buffer>
306 mapclear! <buffer> | imapclear <buffer> | lmapclear <buffer>
307 cmapclear <buffer> | tmapclear <buffer>
308 abclear | abclear <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 ClearMappingsAbbreviations()
318 assert_equal(0, len(maplist()))
319 assert_equal(0, len(maplist(true)))
320
321 # Set up some mappings.
322 map dup bar
323 map <buffer> dup bufbar
324 map foo<C-V> is<F4>foo
325 vnoremap <script> <buffer> <expr> <silent> bar isbar
326 tmap baz foo
327 omap h w
328 lmap i w
329 nmap j w
330 xmap k w
331 smap l w
332 map abc <Nop>
333 nmap <M-j> x
334 nmap <M-Space> y
335 # And abbreviations
336 abbreviate xy he
337 abbreviate xx she
338 abbreviate <buffer> x they
339
340 # Get a list of the mappings with the ':map' commands.
341 # Check maplist() return a list of the same size.
342 assert_equal(13, len(maplist()))
343 assert_equal(3, len(maplist(true)))
344 assert_equal(13, len(maplist(false)))
345
346 # collect all the current maps using :map commands
347 var maps_command: list<string>
348 AddMaps(split(execute('map'), '\n'), maps_command)
349 AddMaps(split(execute('map!'), '\n'), maps_command)
350 AddMaps(split(execute('tmap'), '\n'), maps_command)
351 AddMaps(split(execute('lmap'), '\n'), maps_command)
352
353 # Use maplist to get all the maps
354 var maps_maplist = maplist()
355 assert_equal(len(maps_command), len(maps_maplist))
356
357 # make sure all the mode-lhs are unique, no duplicates
358 var map_set: dict<number>
359 for d in maps_maplist
360 map_set[d.mode .. "-" .. d.lhs .. "-" .. d.buffer] = 0
361 endfor
362 assert_equal(len(maps_maplist), len(map_set))
363
364 # For everything returned by maplist, should be the same as from maparg.
365 # Except for "map dup", bacause maparg returns the <buffer> version
366 for d in maps_maplist
367 if d.lhs == 'dup' && d.buffer == 0
368 continue
369 endif
370 var d_maparg = maparg(d.lhs, d.mode, false, true)
371 assert_equal(d_maparg, d)
372 endfor
373
374 # Check abbr matches maparg
375 for d in maplist(true)
376 # Note, d.mode is '!', but can't use that with maparg
377 var d_maparg = maparg(d.lhs, 'i', true, true)
378 assert_equal(d_maparg, d)
379 endfor
380
381 ClearMappingsAbbreviations()
382 assert_equal(0, len(maplist()))
383 assert_equal(0, len(maplist(true)))
384 enddef
385
386
387 " vim: shiftwidth=2 sts=2 expandtab