# HG changeset patch # User Bram Moolenaar # Date 1650894303 -7200 # Node ID 398c5b3211f9d5a351e602eacaf9fd4f17474548 # Parent a4d54a260c598e761740a7dcb801d300bd12ab63 patch 8.2.4825: can only get a list of mappings Commit: https://github.com/vim/vim/commit/09661203ecefbee6a6f09438afcff1843e9dbfb4 Author: Ernie Rael 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. diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -235,7 +235,6 @@ getline({lnum}) String line {lnum} of getline({lnum}, {end}) List lines {lnum} to {end} of current buffer getloclist({nr}) List list of location list items getloclist({nr}, {what}) Dict get specific location list properties -getmappings() List list of all mappings, a dict for each getmarklist([{buf}]) List list of global/local marks getmatches([{win}]) List list of current matches getmousepos() Dict last known mouse position @@ -338,6 +337,7 @@ maparg({name} [, {mode} [, {abbr} [, {di rhs of mapping {name} in mode {mode} mapcheck({name} [, {mode} [, {abbr}]]) String check for mappings matching {name} +maplist([{abbr}]) List list of all mappings, a dict for each mapnew({expr1}, {expr2}) List/Dict/Blob/String like |map()| but creates a new List or Dictionary @@ -3571,17 +3571,6 @@ getloclist({nr} [, {what}]) *getlocli :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: > - vim9script - echo getmappings()->filter( - (_, m) => match(m.rhs, 'MultiMatch') >= 0) - - getmarklist([{buf}]) *getmarklist()* Without the {buf} argument returns a |List| with information about all the global marks. |mark| @@ -5247,7 +5236,8 @@ maparg({name} [, {mode} [, {abbr} [, {di When {dict} is omitted or zero: Return the rhs of mapping {name} in mode {mode}. The returned String has special characters translated like in the output of the ":map" command - listing. + listing. When {dict} is TRUE a dictionary is returned, see + below. To get a list of all mappings see |maplist()|. When there is no mapping for {name}, an empty String is returned. When the mapping for {name} is empty, then "" @@ -5348,6 +5338,18 @@ mapcheck({name} [, {mode} [, {abbr}]]) GetKey()->mapcheck('n') +maplist([{abbr}]) *maplist()* + Returns a |List| of all mappings. Each List item is a |Dict|, + the same as what is returned by |maparg()|, see + |mapping-dict|. When {abbr} is there and it is |TRUE| use + abbreviations instead of mappings. + + Example to show all mappings with 'MultiMatch' in rhs: > + vim9script + echo maplist()->filter( + (_, m) => match(m.rhs, 'MultiMatch') >= 0) + + mapnew({expr1}, {expr2}) *mapnew()* Like |map()| but instead of replacing items in {expr1} a new List or Dictionary is created and returned. {expr1} remains diff --git a/runtime/doc/usr_41.txt b/runtime/doc/usr_41.txt --- a/runtime/doc/usr_41.txt +++ b/runtime/doc/usr_41.txt @@ -1089,10 +1089,10 @@ Mappings and Menus: *mapping-funct 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 + maplist() get list of all mappings mapset() restore a mapping menu_info() get information about a menu item wildmenumode() check if the wildmode is active diff --git a/src/evalfunc.c b/src/evalfunc.c --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -1871,8 +1871,6 @@ static funcentry_T global_functions[] = ret_getline, f_getline}, {"getloclist", 1, 2, 0, arg2_number_dict_any, ret_list_or_dict_1, f_getloclist}, - {"getmappings", 0, 0, 0, NULL, - ret_list_dict_any, f_getmappings}, {"getmarklist", 0, 1, FEARG_1, arg1_buffer, ret_list_dict_any, f_getmarklist}, {"getmatches", 0, 1, 0, arg1_number, @@ -2049,6 +2047,8 @@ static funcentry_T global_functions[] = ret_maparg, f_maparg}, {"mapcheck", 1, 3, FEARG_1, arg3_string_string_bool, ret_string, f_mapcheck}, + {"maplist", 0, 1, 0, arg1_bool, + ret_list_dict_any, f_maplist}, {"mapnew", 2, 2, FEARG_1, arg2_mapnew, ret_first_cont, f_mapnew}, {"mapset", 3, 3, FEARG_1, arg3_string_bool_dict, diff --git a/src/map.c b/src/map.c --- a/src/map.c +++ b/src/map.c @@ -2389,10 +2389,10 @@ get_maparg(typval_T *argvars, typval_T * } /* - * "getmappings()" function + * "maplist()" function */ void -f_getmappings(typval_T *argvars UNUSED, typval_T *rettv) +f_maplist(typval_T *argvars UNUSED, typval_T *rettv) { dict_T *d; mapblock_T *mp; @@ -2402,6 +2402,12 @@ f_getmappings(typval_T *argvars UNUSED, int hash; char_u *lhs; const int flags = REPTERM_FROM_PART | REPTERM_DO_LT; + int abbr = FALSE; + + if (in_vim9script() && check_for_opt_bool_arg(argvars, 0) == FAIL) + return; + if (argvars[0].v_type != VAR_UNKNOWN) + abbr = tv_get_bool(&argvars[0]); if (rettv_list_alloc(rettv) != OK) return; @@ -2413,7 +2419,16 @@ f_getmappings(typval_T *argvars UNUSED, { for (hash = 0; hash < 256; ++hash) { - if (buffer_local) + if (abbr) + { + if (hash > 0) // there is only one abbr list + break; + if (buffer_local) + mp = curbuf->b_first_abbr; + else + mp = first_abbr; + } + else if (buffer_local) mp = curbuf->b_maphash[hash]; else mp = maphash[hash]; diff --git a/src/proto/map.pro b/src/proto/map.pro --- a/src/proto/map.pro +++ b/src/proto/map.pro @@ -8,7 +8,7 @@ int mode_str2flags(char_u *modechars); int map_to_exists(char_u *str, char_u *modechars, int abbr); int map_to_exists_mode(char_u *rhs, int mode, int abbr); char_u *set_context_in_map_cmd(expand_T *xp, char_u *cmd, char_u *arg, int forceit, int isabbrev, int isunmap, cmdidx_T cmdidx); -int ExpandMappings(char_u *pat, regmatch_T *regmatch, int *num_file, char_u ***file); +int ExpandMappings(char_u *pat, regmatch_T *regmatch, int *numMatches, char_u ***matches); int check_abbr(int c, char_u *ptr, int col, int mincol); char_u *eval_map_expr(mapblock_T *mp, int c); char_u *vim_strsave_escape_csi(char_u *p); @@ -17,7 +17,7 @@ int makemap(FILE *fd, buf_T *buf); int put_escstr(FILE *fd, char_u *strstart, int what); void check_map_keycodes(void); char_u *check_map(char_u *keys, int mode, int exact, int ign_mod, int abbr, mapblock_T **mp_ptr, int *local_ptr); -void f_getmappings(typval_T *argvars, typval_T *rettv); +void f_maplist(typval_T *argvars, typval_T *rettv); void f_maparg(typval_T *argvars, typval_T *rettv); void f_mapcheck(typval_T *argvars, typval_T *rettv); void f_mapset(typval_T *argvars, typval_T *rettv); diff --git a/src/testdir/Make_all.mak b/src/testdir/Make_all.mak --- a/src/testdir/Make_all.mak +++ b/src/testdir/Make_all.mak @@ -184,7 +184,7 @@ NEW_TESTS = \ test_lua \ test_makeencoding \ test_man \ - test_maparg \ + test_map_functions \ test_mapping \ test_marks \ test_match \ @@ -425,7 +425,7 @@ NEW_TESTS_RES = \ test_lua.res \ test_makeencoding.res \ test_man.res \ - test_maparg.res \ + test_map_functions.res \ test_mapping.res \ test_marks.res \ test_match.res \ diff --git a/src/testdir/test_maparg.vim b/src/testdir/test_map_functions.vim rename from src/testdir/test_maparg.vim rename to src/testdir/test_map_functions.vim --- a/src/testdir/test_maparg.vim +++ b/src/testdir/test_map_functions.vim @@ -1,6 +1,5 @@ -" Tests for maparg(), mapcheck() and mapset(). +" Tests for maparg(), mapcheck(), mapset(), maplist() " Also test utf8 map with a 0x80 byte. -" Also test mapcheck() func s:SID() return str2nr(matchstr(expand(''), '\zs\d\+\ze_SID$')) @@ -297,15 +296,16 @@ func Test_map_restore() endfunc -def Test_getmappings() +def Test_maplist() new - def ClearMaps() + def ClearMappingsAbbreviations() mapclear | nmapclear | vmapclear | xmapclear | smapclear | omapclear mapclear! | imapclear | lmapclear | cmapclear | tmapclear mapclear | nmapclear | vmapclear xmapclear | smapclear | omapclear mapclear! | imapclear | lmapclear cmapclear | tmapclear + abclear | abclear enddef def AddMaps(new: list, accum: list) @@ -314,8 +314,9 @@ def Test_getmappings() endif enddef - ClearMaps() - assert_equal(0, len(getmappings())) + ClearMappingsAbbreviations() + assert_equal(0, len(maplist())) + assert_equal(0, len(maplist(true))) # Set up some mappings. map dup bar @@ -331,10 +332,16 @@ def Test_getmappings() map abc nmap x nmap y + # And abbreviations + abbreviate xy he + abbreviate xx she + abbreviate x they # Get a list of the mappings with the ':map' commands. - # Check getmappings() return a list of the same size. - assert_equal(13, len(getmappings())) + # Check maplist() return a list of the same size. + assert_equal(13, len(maplist())) + assert_equal(3, len(maplist(true))) + assert_equal(13, len(maplist(false))) # collect all the current maps using :map commands var maps_command: list @@ -343,20 +350,20 @@ def Test_getmappings() AddMaps(split(execute('tmap'), '\n'), maps_command) AddMaps(split(execute('lmap'), '\n'), maps_command) - # Use getmappings to get all the maps - var maps_getmappings = getmappings() - assert_equal(len(maps_command), len(maps_getmappings)) + # Use maplist to get all the maps + var maps_maplist = maplist() + assert_equal(len(maps_command), len(maps_maplist)) # make sure all the mode-lhs are unique, no duplicates var map_set: dict - for d in maps_getmappings + for d in maps_maplist map_set[d.mode .. "-" .. d.lhs .. "-" .. d.buffer] = 0 endfor - assert_equal(len(maps_getmappings), len(map_set)) + assert_equal(len(maps_maplist), len(map_set)) - # For everything returned by getmappings, should be the same as from maparg. + # For everything returned by maplist, should be the same as from maparg. # Except for "map dup", bacause maparg returns the version - for d in maps_getmappings + for d in maps_maplist if d.lhs == 'dup' && d.buffer == 0 continue endif @@ -364,8 +371,16 @@ def Test_getmappings() assert_equal(d_maparg, d) endfor - ClearMaps() - assert_equal(0, len(getmappings())) + # Check abbr matches maparg + for d in maplist(true) + # Note, d.mode is '!', but can't use that with maparg + var d_maparg = maparg(d.lhs, 'i', true, true) + assert_equal(d_maparg, d) + endfor + + ClearMappingsAbbreviations() + assert_equal(0, len(maplist())) + assert_equal(0, len(maplist(true))) enddef diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -747,6 +747,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 4825, +/**/ 4824, /**/ 4823,