From a4b80c71eae09a5f6f76bce46be46efd247c7223 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 9 Nov 2023 15:17:53 +0800 Subject: vim-patch:8.2.4140: maparg() does not indicate the type of script Problem: maparg() does not indicate the type of script where it was defined. Solution: Add "scriptversion". https://github.com/vim/vim/commit/a9528b39a666dbaa026320f73bae4b1628a7fe51 Co-authored-by: Bram Moolenaar --- runtime/doc/builtin.txt | 1 + runtime/lua/vim/_meta/vimfn.lua | 1 + src/nvim/eval.lua | 1 + src/nvim/mapping.c | 1 + test/functional/api/keymap_spec.lua | 8 +++++++- test/functional/vimscript/map_functions_spec.lua | 4 +++- test/old/testdir/test_maparg.vim | 12 ++++++++---- 7 files changed, 22 insertions(+), 6 deletions(-) diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 35c94d6c74..06e6be41bc 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -4180,6 +4180,7 @@ maparg({name} [, {mode} [, {abbr} [, {dict}]]]) *maparg()* (|mapmode-ic|) "sid" The script local ID, used for mappings (||). Negative for special contexts. + "scriptversion" The version of the script, always 1. "lnum" The line number in "sid", zero if unknown. "nowait" Do not wait for other, longer mappings. (|:map-|). diff --git a/runtime/lua/vim/_meta/vimfn.lua b/runtime/lua/vim/_meta/vimfn.lua index 0563f9dd5f..817841eb46 100644 --- a/runtime/lua/vim/_meta/vimfn.lua +++ b/runtime/lua/vim/_meta/vimfn.lua @@ -5041,6 +5041,7 @@ function vim.fn.map(expr1, expr2) end --- (|mapmode-ic|) --- "sid" The script local ID, used for mappings --- (||). Negative for special contexts. +--- "scriptversion" The version of the script, always 1. --- "lnum" The line number in "sid", zero if unknown. --- "nowait" Do not wait for other, longer mappings. --- (|:map-|). diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 221be492af..0b81e2eb65 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -6189,6 +6189,7 @@ M.funcs = { (|mapmode-ic|) "sid" The script local ID, used for mappings (||). Negative for special contexts. + "scriptversion" The version of the script, always 1. "lnum" The line number in "sid", zero if unknown. "nowait" Do not wait for other, longer mappings. (|:map-|). diff --git a/src/nvim/mapping.c b/src/nvim/mapping.c index 4435e91993..6786b5efe9 100644 --- a/src/nvim/mapping.c +++ b/src/nvim/mapping.c @@ -2113,6 +2113,7 @@ static Dictionary mapblock_fill_dict(const mapblock_T *const mp, const char *lhs PUT(dict, "expr", INTEGER_OBJ(mp->m_expr ? 1 : 0)); PUT(dict, "silent", INTEGER_OBJ(mp->m_silent ? 1 : 0)); PUT(dict, "sid", INTEGER_OBJ(mp->m_script_ctx.sc_sid)); + PUT(dict, "scriptversion", INTEGER_OBJ(1)); PUT(dict, "lnum", INTEGER_OBJ(mp->m_script_ctx.sc_lnum)); PUT(dict, "buffer", INTEGER_OBJ(buffer_value)); PUT(dict, "nowait", INTEGER_OBJ(mp->m_nowait ? 1 : 0)); diff --git a/test/functional/api/keymap_spec.lua b/test/functional/api/keymap_spec.lua index 811c4210fa..8cb97e5ef0 100644 --- a/test/functional/api/keymap_spec.lua +++ b/test/functional/api/keymap_spec.lua @@ -32,6 +32,7 @@ describe('nvim_get_keymap', function() rhs='bar', expr=0, sid=0, + scriptversion=1, buffer=0, nowait=0, mode='n', @@ -258,6 +259,7 @@ describe('nvim_get_keymap', function() silent=0, expr=0, sid=0, + scriptversion=1, buffer=0, nowait=0, noremap=1, @@ -327,6 +329,7 @@ describe('nvim_get_keymap', function() silent=0, expr=0, sid=0, + scriptversion=1, buffer=0, nowait=0, noremap=1, @@ -365,6 +368,7 @@ describe('nvim_get_keymap', function() silent=0, expr=0, sid=sid_lua, + scriptversion=1, buffer=0, nowait=0, mode='n', @@ -373,7 +377,7 @@ describe('nvim_get_keymap', function() }, mapargs[1]) end) - it ('can handle map descriptions', function() + it('can handle map descriptions', function() meths.set_keymap('n', 'lhs', 'rhs', {desc="map description"}) eq({ lhs='lhs', @@ -383,6 +387,7 @@ describe('nvim_get_keymap', function() silent=0, expr=0, sid=sid_api_client, + scriptversion=1, buffer=0, nowait=0, mode='n', @@ -429,6 +434,7 @@ describe('nvim_set_keymap, nvim_del_keymap', function() to_return.nowait = not opts.nowait and 0 or 1 to_return.expr = not opts.expr and 0 or 1 to_return.sid = not opts.sid and sid_api_client or opts.sid + to_return.scriptversion = 1 to_return.buffer = not opts.buffer and 0 or opts.buffer to_return.lnum = not opts.lnum and 0 or opts.lnum to_return.desc = opts.desc diff --git a/test/functional/vimscript/map_functions_spec.lua b/test/functional/vimscript/map_functions_spec.lua index 2c8fe69428..3f8bf24505 100644 --- a/test/functional/vimscript/map_functions_spec.lua +++ b/test/functional/vimscript/map_functions_spec.lua @@ -26,6 +26,7 @@ describe('maparg()', function() rhs='bar', expr=0, sid=0, + scriptversion=1, buffer=0, nowait=0, mode='n', @@ -157,8 +158,9 @@ describe('maparg()', function() mode = 'n', noremap = 1, nowait = 0, - script=0, + script = 0, sid = 0, + scriptversion = 1, silent = 0, lnum = 0, } diff --git a/test/old/testdir/test_maparg.vim b/test/old/testdir/test_maparg.vim index 1837511990..17f3dcab8f 100644 --- a/test/old/testdir/test_maparg.vim +++ b/test/old/testdir/test_maparg.vim @@ -19,26 +19,30 @@ func Test_maparg() call assert_equal("isfoo", maparg('foo')) call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo', \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16", - \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'lnum': lnum + 1, + \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1, + \ 'lnum': lnum + 1, \ 'rhs': 'isfoo', 'buffer': 0}, \ maparg('foo', '', 0, 1)) call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar', \ 'lhsraw': 'bar', 'mode': 'v', - \ 'nowait': 0, 'expr': 1, 'sid': sid, 'lnum': lnum + 2, + \ 'nowait': 0, 'expr': 1, 'sid': sid, 'scriptversion': 1, + \ 'lnum': lnum + 2, \ 'rhs': 'isbar', 'buffer': 1}, \ 'bar'->maparg('', 0, 1)) let lnum = expand('') map foo bar call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'foo', \ 'lhsraw': 'foo', 'mode': ' ', - \ 'nowait': 1, 'expr': 0, 'sid': sid, 'lnum': lnum + 1, 'rhs': 'bar', + \ 'nowait': 1, 'expr': 0, 'sid': sid, 'scriptversion': 1, + \ 'lnum': lnum + 1, 'rhs': 'bar', \ 'buffer': 1}, \ maparg('foo', '', 0, 1)) let lnum = expand('') tmap baz foo call assert_equal({'silent': 0, 'noremap': 0, 'script': 0, 'lhs': 'baz', \ 'lhsraw': 'baz', 'mode': 't', - \ 'nowait': 0, 'expr': 0, 'sid': sid, 'lnum': lnum + 1, 'rhs': 'foo', + \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1, + \ 'lnum': lnum + 1, 'rhs': 'foo', \ 'buffer': 0}, \ maparg('baz', 't', 0, 1)) -- cgit From 2dfcd5a22b8f26091aa7398fdb8b0ea70ed7b28d Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 9 Nov 2023 18:16:49 +0800 Subject: vim-patch:8.2.4820: not simple programmatic way to find a specific mapping Problem: Not simple programmatic way to find a specific mapping. Solution: Add getmappings(). (Ernie Rael, closes vim/vim#10273) https://github.com/vim/vim/commit/659c240cf769925ff432b88df8719e7ace4629b0 Co-authored-by: Ernie Rael --- runtime/doc/builtin.txt | 12 ++++++- runtime/doc/usr_41.txt | 1 + runtime/lua/vim/_meta/vimfn.lua | 14 +++++++- src/nvim/eval.lua | 18 +++++++++- src/nvim/mapping.c | 43 ++++++++++++++++++++++++ test/old/testdir/test_maparg.vim | 72 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 157 insertions(+), 3 deletions(-) diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 06e6be41bc..6b34a49891 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -2612,6 +2612,16 @@ getloclist({nr} [, {what}]) *getloclist()* echo getloclist(5, {'filewinid': 0}) < +getmappings() *getmappings()* + Returns a |List| of all mappings. Each List item is a |Dict|, + the same as what is returned by |maparg()|, see + |mapping-dict|. + + Example to show all mappings with "MultiMatch" in rhs: > + echo getmappings()->filter({_, m -> + \ match(get(m, 'rhs', ''), 'MultiMatch') >= 0 + \ }) + getmarklist([{buf}]) *getmarklist()* Without the {buf} argument returns a |List| with information about all the global marks. |mark| @@ -4161,7 +4171,7 @@ maparg({name} [, {mode} [, {abbr} [, {dict}]]]) *maparg()* When {dict} is there and it is |TRUE| return a dictionary containing all the information of the mapping with the - following items: + following items: *mapping-dict* "lhs" The {lhs} of the mapping as it would be typed "lhsraw" The {lhs} of the mapping as raw bytes "lhsrawalt" The {lhs} of the mapping as raw bytes, alternate diff --git a/runtime/doc/usr_41.txt b/runtime/doc/usr_41.txt index c8b31b2d5b..9fe81f56b6 100644 --- a/runtime/doc/usr_41.txt +++ b/runtime/doc/usr_41.txt @@ -1012,6 +1012,7 @@ Mappings and Menus: *mapping-functions* digraph_getlist() get all |digraph|s digraph_set() register |digraph| digraph_setlist() register multiple |digraph|s + getmappings() get list of all mappings hasmapto() check if a mapping exists mapcheck() check if a matching mapping exists maparg() get rhs of a mapping diff --git a/runtime/lua/vim/_meta/vimfn.lua b/runtime/lua/vim/_meta/vimfn.lua index 817841eb46..f54606b225 100644 --- a/runtime/lua/vim/_meta/vimfn.lua +++ b/runtime/lua/vim/_meta/vimfn.lua @@ -3175,6 +3175,18 @@ function vim.fn.getline(lnum, end_) end --- @return any function vim.fn.getloclist(nr, what) end +--- Returns a |List| of all mappings. Each List item is a |Dict|, +--- the same as what is returned by |maparg()|, see +--- |mapping-dict|. +--- +--- Example to show all mappings with "MultiMatch" in rhs: > +--- echo getmappings()->filter({_, m -> +--- \ match(get(m, 'rhs', ''), 'MultiMatch') >= 0 +--- \ }) +--- +--- @return any +function vim.fn.getmappings() end + --- Without the {buf} argument returns a |List| with information --- about all the global marks. |mark| --- @@ -5022,7 +5034,7 @@ function vim.fn.map(expr1, expr2) end --- --- When {dict} is there and it is |TRUE| return a dictionary --- containing all the information of the mapping with the ---- following items: +--- following items: *mapping-dict* --- "lhs" The {lhs} of the mapping as it would be typed --- "lhsraw" The {lhs} of the mapping as raw bytes --- "lhsrawalt" The {lhs} of the mapping as raw bytes, alternate diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 0b81e2eb65..5aa4d65362 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -3957,6 +3957,22 @@ M.funcs = { params = { { 'nr', 'integer' }, { 'what', 'any' } }, signature = 'getloclist({nr} [, {what}])', }, + getmappings = { + args = 0, + desc = [[ + Returns a |List| of all mappings. Each List item is a |Dict|, + the same as what is returned by |maparg()|, see + |mapping-dict|. + + Example to show all mappings with "MultiMatch" in rhs: > + echo getmappings()->filter({_, m -> + \ match(get(m, 'rhs', ''), 'MultiMatch') >= 0 + \ }) + ]], + name = 'getmappings', + params = {}, + signature = 'getmappings()' + }, getmarklist = { args = { 0, 1 }, base = 1, @@ -6170,7 +6186,7 @@ M.funcs = { When {dict} is there and it is |TRUE| return a dictionary containing all the information of the mapping with the - following items: + following items: *mapping-dict* "lhs" The {lhs} of the mapping as it would be typed "lhsraw" The {lhs} of the mapping as raw bytes "lhsrawalt" The {lhs} of the mapping as raw bytes, alternate diff --git a/src/nvim/mapping.c b/src/nvim/mapping.c index 6786b5efe9..2cc469feec 100644 --- a/src/nvim/mapping.c +++ b/src/nvim/mapping.c @@ -2281,6 +2281,49 @@ void f_mapset(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) sid, lnum, false); } +/// "maplist()" function +void f_getmappings(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +{ + const int flags = REPTERM_FROM_PART | REPTERM_DO_LT; + + tv_list_alloc_ret(rettv, kListLenUnknown); + + // Do it twice: once for global maps and once for local maps. + for (int buffer_local = 0; buffer_local <= 1; buffer_local++) { + for (int hash = 0; hash < 256; hash++) { + mapblock_T *mp; + if (buffer_local) { + mp = curbuf->b_maphash[hash]; + } else { + mp = maphash[hash]; + } + for (; mp; mp = mp->m_next) { + if (mp->m_simplified) { + continue; + } + + char *keys_buf = NULL; + bool did_simplify = false; + + char *lhs = str2special_save(mp->m_keys, true, false); + (void)replace_termcodes(lhs, strlen(lhs), &keys_buf, 0, flags, &did_simplify, + CPO_TO_CPO_FLAGS); + xfree(lhs); + + Dictionary dict = mapblock_fill_dict(mp, + did_simplify ? keys_buf : NULL, + buffer_local, true); + typval_T d = TV_INITIAL_VALUE; + (void)object_to_vim(DICTIONARY_OBJ(dict), &d, NULL); + assert(d.v_type == VAR_DICT); + tv_list_append_dict(rettv->vval.v_list, d.vval.v_dict); + api_free_dictionary(dict); + xfree(keys_buf); + } + } + } +} + /// "maparg()" function void f_maparg(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { diff --git a/test/old/testdir/test_maparg.vim b/test/old/testdir/test_maparg.vim index 17f3dcab8f..b0671d3b31 100644 --- a/test/old/testdir/test_maparg.vim +++ b/test/old/testdir/test_maparg.vim @@ -372,4 +372,76 @@ func Test_map_restore_negative_sid() call delete('Xresult') endfunc +func Test_getmappings() + new + func s:ClearMaps() + mapclear | nmapclear | vmapclear | xmapclear | smapclear | omapclear + mapclear! | imapclear | lmapclear | cmapclear | tmapclear + mapclear | nmapclear | vmapclear + xmapclear | smapclear | omapclear + mapclear! | imapclear | lmapclear + cmapclear | tmapclear + endfunc + + func s:AddMaps(new, accum) + if len(a:new) > 0 && a:new[0] != "No mapping found" + eval a:accum->extend(a:new) + endif + endfunc + + call s:ClearMaps() + call assert_equal(0, len(getmappings())) + + " Set up some mappings. + map dup bar + map dup bufbar + map foo isfoo + vnoremap