diff options
author | zeertzjq <zeertzjq@outlook.com> | 2023-11-09 18:16:49 +0800 |
---|---|---|
committer | zeertzjq <zeertzjq@outlook.com> | 2023-11-09 21:34:04 +0800 |
commit | 2dfcd5a22b8f26091aa7398fdb8b0ea70ed7b28d (patch) | |
tree | 33540db6f3104e7844ac05fdeb332fe64b046a3e /src | |
parent | a4b80c71eae09a5f6f76bce46be46efd247c7223 (diff) | |
download | rneovim-2dfcd5a22b8f26091aa7398fdb8b0ea70ed7b28d.tar.gz rneovim-2dfcd5a22b8f26091aa7398fdb8b0ea70ed7b28d.tar.bz2 rneovim-2dfcd5a22b8f26091aa7398fdb8b0ea70ed7b28d.zip |
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 <errael@raelity.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/eval.lua | 18 | ||||
-rw-r--r-- | src/nvim/mapping.c | 43 |
2 files changed, 60 insertions, 1 deletions
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) { |