comparison src/testdir/test_map_functions.vim @ 28674:38f7a132bba3 v8.2.4861

patch 8.2.4861: it is not easy to restore saved mappings Commit: https://github.com/vim/vim/commit/51d04d16f21e19d6eded98f9530d84089102f925 Author: Ernie Rael <errael@raelity.com> Date: Wed May 4 15:40:22 2022 +0100 patch 8.2.4861: it is not easy to restore saved mappings Problem: It is not easy to restore saved mappings. Solution: Make mapset() accept a dict argument. (Ernie Rael, closes https://github.com/vim/vim/issues/10295)
author Bram Moolenaar <Bram@vim.org>
date Wed, 04 May 2022 16:45:03 +0200
parents 9ae2c32841fb
children fea88e555652
comparison
equal deleted inserted replaced
28673:1bb2e8f5cc4b 28674:38f7a132bba3
17 call assert_equal("is<F4>foo", maparg('foo<C-V>')) 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>', 18 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo<C-V>',
19 \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16", 19 \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16",
20 \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1, 20 \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
21 \ 'lnum': lnum + 1, 21 \ 'lnum': lnum + 1,
22 \ 'rhs': 'is<F4>foo', 'buffer': 0}, 22 \ 'rhs': 'is<F4>foo', 'buffer': 0, 'abbr': 0},
23 \ maparg('foo<C-V>', '', 0, 1)) 23 \ maparg('foo<C-V>', '', 0, 1))
24 call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar', 24 call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar',
25 \ 'lhsraw': 'bar', 'mode': 'v', 25 \ 'lhsraw': 'bar', 'mode': 'v',
26 \ 'nowait': 0, 'expr': 1, 'sid': sid, 'scriptversion': 1, 26 \ 'nowait': 0, 'expr': 1, 'sid': sid, 'scriptversion': 1,
27 \ 'lnum': lnum + 2, 27 \ 'lnum': lnum + 2,
28 \ 'rhs': 'isbar', 'buffer': 1}, 28 \ 'rhs': 'isbar', 'buffer': 1, 'abbr': 0},
29 \ 'bar'->maparg('', 0, 1)) 29 \ 'bar'->maparg('', 0, 1))
30 let lnum = expand('<sflnum>') 30 let lnum = expand('<sflnum>')
31 map <buffer> <nowait> foo bar 31 map <buffer> <nowait> foo bar
32 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo', 32 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo',
33 \ 'lhsraw': 'foo', 'mode': ' ', 33 \ 'lhsraw': 'foo', 'mode': ' ',
34 \ 'nowait': 1, 'expr': 0, 'sid': sid, 'scriptversion': 1, 34 \ 'nowait': 1, 'expr': 0, 'sid': sid, 'scriptversion': 1,
35 \ 'lnum': lnum + 1, 'rhs': 'bar', 35 \ 'lnum': lnum + 1, 'rhs': 'bar',
36 \ 'buffer': 1}, 36 \ 'buffer': 1, 'abbr': 0},
37 \ maparg('foo', '', 0, 1)) 37 \ maparg('foo', '', 0, 1))
38 let lnum = expand('<sflnum>') 38 let lnum = expand('<sflnum>')
39 tmap baz foo 39 tmap baz foo
40 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz', 40 call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz',
41 \ 'lhsraw': 'baz', 'mode': 't', 41 \ 'lhsraw': 'baz', 'mode': 't',
42 \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1, 42 \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
43 \ 'lnum': lnum + 1, 'rhs': 'foo', 43 \ 'lnum': lnum + 1, 'rhs': 'foo',
44 \ 'buffer': 0}, 44 \ 'buffer': 0, 'abbr': 0},
45 \ maparg('baz', 't', 0, 1)) 45 \ maparg('baz', 't', 0, 1))
46 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',
52 \ 'buffer': 0, 'abbr': 1},
53 \ maparg('A', 'i', 1, 1))
54 iuna A
46 55
47 map abc x<char-114>x 56 map abc x<char-114>x
48 call assert_equal("xrx", maparg('abc')) 57 call assert_equal("xrx", maparg('abc'))
49 map abc y<S-char-114>y 58 map abc y<S-char-114>y
50 call assert_equal("yRy", maparg('abc')) 59 call assert_equal("yRy", maparg('abc'))
248 iunmap K 257 iunmap K
249 let &cpo = cpo_save 258 let &cpo = cpo_save
250 bwipe! 259 bwipe!
251 260
252 call assert_fails('call mapset([], v:false, {})', 'E730:') 261 call assert_fails('call mapset([], v:false, {})', 'E730:')
253 call assert_fails('call mapset("i", 0, "")', 'E716:') 262 call assert_fails('call mapset("i", 0, "")', 'E715:')
254 call assert_fails('call mapset("i", 0, {})', 'E460:') 263 call assert_fails('call mapset("i", 0, {})', 'E460:')
255 endfunc 264 endfunc
265
266 def Test_mapset_arg1_dir()
267 # This test is mostly about get_map_mode_string.
268 # Once the code gets past that, it's common with the 3 arg mapset.
269
270 # GetModes() return list of modes for 'XZ' lhs using maplist.
271 # There is one list item per mapping
272 def GetModes(abbr: bool = false): list<string>
273 return maplist(abbr)->filter((_, m) => m.lhs == 'XZ')
274 ->mapnew((_, m) => m.mode)
275 enddef
276
277 const unmap_cmds = [ 'unmap', 'unmap!', 'tunmap', 'lunmap' ]
278 def UnmapAll(lhs: string)
279 for cmd in unmap_cmds
280 try | execute(cmd .. ' ' .. lhs) | catch /E31/ | endtry
281 endfor
282 enddef
283
284 var tmap: dict<any>
285
286 # some mapset(mode, abbr, dict) tests using get_map_mode_str
287 map XZ x
288 tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy()
289 # this splits the mapping into 2 mappings
290 mapset('ox', false, tmap)
291 assert_equal(2, len(GetModes()))
292 mapset('o', false, tmap)
293 assert_equal(3, len(GetModes()))
294 # test that '' acts like ' ', and that the 3 mappings become 1
295 mapset('', false, tmap)
296 assert_equal([' '], GetModes())
297 # dict's mode/abbr are ignored
298 UnmapAll('XZ')
299 tmap.mode = '!'
300 tmap.abbr = true
301 mapset('o', false, tmap)
302 assert_equal(['o'], GetModes())
303
304 # test the 3 arg version handles bad mode string, dict not used
305 assert_fails("mapset('vi', false, {})", 'E1276:')
306
307
308 # get the abbreviations out of the way
309 abbreviate XZ ZX
310 tmap = maplist(true)->filter((_, m) => m.lhs == 'XZ')[0]->copy()
311
312 abclear
313 # 'ic' is the default ab command, shows up as '!'
314 tmap.mode = 'ic'
315 mapset(tmap)
316 assert_equal(['!'], GetModes(true))
317
318 abclear
319 tmap.mode = 'i'
320 mapset(tmap)
321 assert_equal(['i'], GetModes(true))
322
323 abclear
324 tmap.mode = 'c'
325 mapset(tmap)
326 assert_equal(['c'], GetModes(true))
327
328 abclear
329 tmap.mode = '!'
330 mapset(tmap)
331 assert_equal(['!'], GetModes(true))
332
333 assert_fails("mapset({mode: ' !', abbr: 1})", 'E1276:')
334 assert_fails("mapset({mode: 'cl', abbr: 1})", 'E1276:')
335 assert_fails("mapset({mode: 'in', abbr: 1})", 'E1276:')
336
337 # the map commands
338 map XZ x
339 tmap = maplist()->filter((_, m) => m.lhs == 'XZ')[0]->copy()
340
341 # try the combos
342 UnmapAll('XZ')
343 # 'nxso' is ' ', the unadorned :map
344 tmap.mode = 'nxso'
345 mapset(tmap)
346 assert_equal([' '], GetModes())
347
348 UnmapAll('XZ')
349 # 'ic' is '!'
350 tmap.mode = 'ic'
351 mapset(tmap)
352 assert_equal(['!'], GetModes())
353
354 UnmapAll('XZ')
355 # 'xs' is really 'v'
356 tmap.mode = 'xs'
357 mapset(tmap)
358 assert_equal(['v'], GetModes())
359
360 # try the individual modes
361 UnmapAll('XZ')
362 tmap.mode = 'n'
363 mapset(tmap)
364 assert_equal(['n'], GetModes())
365
366 UnmapAll('XZ')
367 tmap.mode = 'x'
368 mapset(tmap)
369 assert_equal(['x'], GetModes())
370
371 UnmapAll('XZ')
372 tmap.mode = 's'
373 mapset(tmap)
374 assert_equal(['s'], GetModes())
375
376 UnmapAll('XZ')
377 tmap.mode = 'o'
378 mapset(tmap)
379 assert_equal(['o'], GetModes())
380
381 UnmapAll('XZ')
382 tmap.mode = 'i'
383 mapset(tmap)
384 assert_equal(['i'], GetModes())
385
386 UnmapAll('XZ')
387 tmap.mode = 'c'
388 mapset(tmap)
389 assert_equal(['c'], GetModes())
390
391 UnmapAll('XZ')
392 tmap.mode = 't'
393 mapset(tmap)
394 assert_equal(['t'], GetModes())
395
396 UnmapAll('XZ')
397 tmap.mode = 'l'
398 mapset(tmap)
399 assert_equal(['l'], GetModes())
400
401 UnmapAll('XZ')
402
403 # get errors for modes that can't be in one mapping
404 assert_fails("mapset({mode: 'nxsoi', abbr: 0})", 'E1276:')
405 assert_fails("mapset({mode: ' !', abbr: 0})", 'E1276:')
406 assert_fails("mapset({mode: 'ix', abbr: 0})", 'E1276:')
407 assert_fails("mapset({mode: 'tl', abbr: 0})", 'E1276:')
408 assert_fails("mapset({mode: ' l', abbr: 0})", 'E1276:')
409 assert_fails("mapset({mode: ' t', abbr: 0})", 'E1276:')
410 enddef
256 411
257 func Check_ctrlb_map(d, check_alt) 412 func Check_ctrlb_map(d, check_alt)
258 call assert_equal('<C-B>', a:d.lhs) 413 call assert_equal('<C-B>', a:d.lhs)
259 if a:check_alt 414 if a:check_alt
260 call assert_equal("\x80\xfc\x04B", a:d.lhsraw) 415 call assert_equal("\x80\xfc\x04B", a:d.lhsraw)