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 + 2 files changed, 2 insertions(+) (limited to 'runtime') 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-|). -- 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 +++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) (limited to 'runtime') 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 -- cgit From d4dbfb092b370bced6728e07a38661a579ff5e4b Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 9 Nov 2023 19:36:58 +0800 Subject: vim-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 vim/vim#10277) Rename to maplist(). Rename test file. https://github.com/vim/vim/commit/09661203ecefbee6a6f09438afcff1843e9dbfb4 Co-authored-by: Ernie Rael --- runtime/doc/builtin.txt | 24 +++++++++++++----------- runtime/doc/usr_41.txt | 2 +- runtime/lua/vim/_meta/vimfn.lua | 28 +++++++++++++++------------- 3 files changed, 29 insertions(+), 25 deletions(-) (limited to 'runtime') diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 6b34a49891..5d21b405df 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -2612,16 +2612,6 @@ 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| @@ -4143,7 +4133,8 @@ maparg({name} [, {mode} [, {abbr} [, {dict}]]]) *maparg()* 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 if {dict} is FALSE, otherwise returns an empty Dict. @@ -4237,6 +4228,17 @@ mapcheck({name} [, {mode} [, {abbr}]]) *mapcheck()* < This avoids adding the "_vv" mapping when there already is a mapping for "_v" or for "_vvv". +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: >vim + echo maplist()->filter({_, m -> + \ match(get(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 index 9fe81f56b6..e206a804f4 100644 --- a/runtime/doc/usr_41.txt +++ b/runtime/doc/usr_41.txt @@ -1012,10 +1012,10 @@ 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 + 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/runtime/lua/vim/_meta/vimfn.lua b/runtime/lua/vim/_meta/vimfn.lua index f54606b225..3293b71977 100644 --- a/runtime/lua/vim/_meta/vimfn.lua +++ b/runtime/lua/vim/_meta/vimfn.lua @@ -3175,18 +3175,6 @@ 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| --- @@ -5006,7 +4994,8 @@ function vim.fn.map(expr1, expr2) end --- 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 if {dict} is FALSE, otherwise returns an empty Dict. @@ -5112,6 +5101,19 @@ function vim.fn.maparg(name, mode, abbr, dict) end --- @return any function vim.fn.mapcheck(name, mode, abbr) end +--- 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: >vim +--- echo maplist()->filter({_, m -> +--- \ match(get(m, 'rhs', ''), 'MultiMatch') >= 0 +--- \ }) +--- +--- @return any +function vim.fn.maplist() end + --- Like |map()| but instead of replacing items in {expr1} a new --- List or Dictionary is created and returned. {expr1} remains --- unchanged. Items can still be changed by {expr2}, if you -- cgit From f748a73a357710db6094d4a428cd056f5df226a9 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 9 Nov 2023 19:48:50 +0800 Subject: vim-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 vim/vim#10295) https://github.com/vim/vim/commit/51d04d16f21e19d6eded98f9530d84089102f925 Co-authored-by: Ernie Rael --- runtime/doc/builtin.txt | 32 ++++++++++++++++--- runtime/doc/map.txt | 2 +- runtime/lua/vim/_meta/vimfn.lua | 36 +++++++++++++++++----- .../pack/dist/opt/termdebug/plugin/termdebug.vim | 9 ++---- 4 files changed, 60 insertions(+), 19 deletions(-) (limited to 'runtime') diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 5d21b405df..1eb173af58 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -4185,6 +4185,7 @@ maparg({name} [, {mode} [, {abbr} [, {dict}]]]) *maparg()* "lnum" The line number in "sid", zero if unknown. "nowait" Do not wait for other, longer mappings. (|:map-|). + "abbr" True if this is an |abbreviation|. The dictionary can be used to restore a mapping with |mapset()|. @@ -4246,9 +4247,17 @@ mapnew({expr1}, {expr2}) *mapnew()* don't want that use |deepcopy()| first. mapset({mode}, {abbr}, {dict}) *mapset()* - Restore a mapping from a dictionary returned by |maparg()|. - {mode} and {abbr} should be the same as for the call to - |maparg()|. *E460* + Restore a mapping from a dictionary, possibly returned by + |maparg()| or |maplist()|. A buffer mapping, when dict.buffer + is true, is set on the current buffer; it is up to the caller + to ensure that the intended buffer is the current buffer. This + feature allows copying mappings from one buffer to another. + The dict.mode value may restore a single mapping that covers + more than one mode, like with mode values of '!', ' ', "nox", + or 'v'. *E1276* + + In the first form, {mode} and {abbr} should be the same as + for the call to |maparg()|. *E460* {mode} is used to define the mode in which the mapping is set, not the "mode" entry in {dict}. Example for saving and restoring a mapping: >vim @@ -4257,8 +4266,21 @@ mapset({mode}, {abbr}, {dict}) *mapset()* " ... call mapset('n', 0, save_map) < Note that if you are going to replace a map in several modes, - e.g. with `:map!`, you need to save the mapping for all of - them, since they can differ. + e.g. with `:map!`, you need to save/restore the mapping for + all of them, when they might differ. + + In the second form, with {dict} as the only argument, mode + and abbr are taken from the dict. + Example: >vim + let save_maps = maplist()->filter( + \ {_, m -> m.lhs == 'K'}) + nnoremap K somethingelse + cnoremap K somethingelse2 + " ... + unmap K + for d in save_maps + call mapset(d) + endfor match({expr}, {pat} [, {start} [, {count}]]) *match()* When {expr} is a |List| then this returns the index of the diff --git a/runtime/doc/map.txt b/runtime/doc/map.txt index 56e58baad8..6f61259af0 100644 --- a/runtime/doc/map.txt +++ b/runtime/doc/map.txt @@ -955,7 +955,7 @@ operator to add quotes around text in the current line: > \ ->setline(".")}'g@ ============================================================================== -2. Abbreviations *abbreviations* *Abbreviations* +2. Abbreviations *abbreviation* *abbreviations* *Abbreviations* Abbreviations are used in Insert mode, Replace mode and Command-line mode. If you enter a word that is an abbreviation, it is replaced with the word it diff --git a/runtime/lua/vim/_meta/vimfn.lua b/runtime/lua/vim/_meta/vimfn.lua index 3293b71977..dea2e476b6 100644 --- a/runtime/lua/vim/_meta/vimfn.lua +++ b/runtime/lua/vim/_meta/vimfn.lua @@ -5046,6 +5046,7 @@ function vim.fn.map(expr1, expr2) end --- "lnum" The line number in "sid", zero if unknown. --- "nowait" Do not wait for other, longer mappings. --- (|:map-|). +--- "abbr" True if this is an |abbreviation|. --- --- The dictionary can be used to restore a mapping with --- |mapset()|. @@ -5124,9 +5125,17 @@ function vim.fn.maplist() end --- @return any function vim.fn.mapnew(expr1, expr2) end ---- Restore a mapping from a dictionary returned by |maparg()|. ---- {mode} and {abbr} should be the same as for the call to ---- |maparg()|. *E460* +--- Restore a mapping from a dictionary, possibly returned by +--- |maparg()| or |maplist()|. A buffer mapping, when dict.buffer +--- is true, is set on the current buffer; it is up to the caller +--- to ensure that the intended buffer is the current buffer. This +--- feature allows copying mappings from one buffer to another. +--- The dict.mode value may restore a single mapping that covers +--- more than one mode, like with mode values of '!', ' ', "nox", +--- or 'v'. *E1276* +--- +--- In the first form, {mode} and {abbr} should be the same as +--- for the call to |maparg()|. *E460* --- {mode} is used to define the mode in which the mapping is set, --- not the "mode" entry in {dict}. --- Example for saving and restoring a mapping: >vim @@ -5135,12 +5144,25 @@ function vim.fn.mapnew(expr1, expr2) end --- " ... --- call mapset('n', 0, save_map) --- vim +--- let save_maps = maplist()->filter( +--- \ {_, m -> m.lhs == 'K'}) +--- nnoremap K somethingelse +--- cnoremap K somethingelse2 +--- " ... +--- unmap K +--- for d in save_maps +--- call mapset(d) +--- endfor --- --- @param mode string ---- @param abbr any ---- @param dict any +--- @param abbr? any +--- @param dict? any --- @return any function vim.fn.mapset(mode, abbr, dict) end diff --git a/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim b/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim index 73fbc8c922..8beca8ffb3 100644 --- a/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim +++ b/runtime/pack/dist/opt/termdebug/plugin/termdebug.vim @@ -1116,8 +1116,7 @@ func s:DeleteCommands() if exists('s:k_map_saved') if !empty(s:k_map_saved) && !s:k_map_saved.buffer nunmap K - " call mapset(s:k_map_saved) - call mapset('n', 0, s:k_map_saved) + call mapset(s:k_map_saved) elseif empty(s:k_map_saved) nunmap K endif @@ -1126,8 +1125,7 @@ func s:DeleteCommands() if exists('s:plus_map_saved') if !empty(s:plus_map_saved) && !s:plus_map_saved.buffer nunmap + - " call mapset(s:plus_map_saved) - call mapset('n', 0, s:plus_map_saved) + call mapset(s:plus_map_saved) elseif empty(s:plus_map_saved) nunmap + endif @@ -1136,8 +1134,7 @@ func s:DeleteCommands() if exists('s:minus_map_saved') if !empty(s:minus_map_saved) && !s:minus_map_saved.buffer nunmap - - " call mapset(s:minus_map_saved) - call mapset('n', 0, s:minus_map_saved) + call mapset(s:minus_map_saved) elseif empty(s:minus_map_saved) nunmap - endif -- cgit From 04d299c17014b0802c79613252e4de26da84a7c9 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 9 Nov 2023 20:42:55 +0800 Subject: vim-patch:8.2.4932: not easy to filter the output of maplist() Problem: Not easy to filter the output of maplist(). Solution: Add mode_bits to the dictionary. (Ernie Rael, closes vim/vim#10356) https://github.com/vim/vim/commit/d8f5f766219273a8579947cc80b92580b6988a4b Co-authored-by: Ernie Rael --- runtime/doc/builtin.txt | 25 +++++++++++++++++++++++++ runtime/lua/vim/_meta/vimfn.lua | 25 +++++++++++++++++++++++++ 2 files changed, 50 insertions(+) (limited to 'runtime') diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 1eb173af58..bc4d1f30c9 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -4186,6 +4186,11 @@ maparg({name} [, {mode} [, {abbr} [, {dict}]]]) *maparg()* "nowait" Do not wait for other, longer mappings. (|:map-|). "abbr" True if this is an |abbreviation|. + "mode_bits" Nvim's internal binary representation of "mode". + |mapset()| ignores this; only "mode" is used. + See |maplist()| for usage examples. The values + are from src/nvim/vim.h and may change in the + future. The dictionary can be used to restore a mapping with |mapset()|. @@ -4239,6 +4244,26 @@ maplist([{abbr}]) *maplist()* echo maplist()->filter({_, m -> \ match(get(m, 'rhs', ''), 'MultiMatch') >= 0 \ }) +< It can be tricky to find mappings for particular |:map-modes|. + |mapping-dict|'s "mode_bits" can simplify this. For example, + the mode_bits for Normal, Insert or Command-line modes are + 0x19. To find all the mappings available in those modes you + can do: >vim + let saved_maps = [] + for m in maplist() + if and(m.mode_bits, 0x19) != 0 + eval saved_maps->add(m) + endif + endfor + echo saved_maps->mapnew({_, m -> m.lhs}) +< The values of the mode_bits are defined in Nvim's + src/nvim/vim.h file and they can be discovered at runtime + using |:map-commands| and "maplist()". Example: >vim + omap xyzzy + let op_bit = maplist()->filter( + \ {_, m -> m.lhs == 'xyzzy'})[0].mode_bits + ounmap xyzzy + echo printf("Operator-pending mode bit: 0x%x", op_bit) mapnew({expr1}, {expr2}) *mapnew()* Like |map()| but instead of replacing items in {expr1} a new diff --git a/runtime/lua/vim/_meta/vimfn.lua b/runtime/lua/vim/_meta/vimfn.lua index dea2e476b6..d1e676ef70 100644 --- a/runtime/lua/vim/_meta/vimfn.lua +++ b/runtime/lua/vim/_meta/vimfn.lua @@ -5047,6 +5047,11 @@ function vim.fn.map(expr1, expr2) end --- "nowait" Do not wait for other, longer mappings. --- (|:map-|). --- "abbr" True if this is an |abbreviation|. +--- "mode_bits" Nvim's internal binary representation of "mode". +--- |mapset()| ignores this; only "mode" is used. +--- See |maplist()| for usage examples. The values +--- are from src/nvim/vim.h and may change in the +--- future. --- --- The dictionary can be used to restore a mapping with --- |mapset()|. @@ -5111,6 +5116,26 @@ function vim.fn.mapcheck(name, mode, abbr) end --- echo maplist()->filter({_, m -> --- \ match(get(m, 'rhs', ''), 'MultiMatch') >= 0 --- \ }) +--- vim +--- let saved_maps = [] +--- for m in maplist() +--- if and(m.mode_bits, 0x19) != 0 +--- eval saved_maps->add(m) +--- endif +--- endfor +--- echo saved_maps->mapnew({_, m -> m.lhs}) +--- vim +--- omap xyzzy +--- let op_bit = maplist()->filter( +--- \ {_, m -> m.lhs == 'xyzzy'})[0].mode_bits +--- ounmap xyzzy +--- echo printf("Operator-pending mode bit: 0x%x", op_bit) --- --- @return any function vim.fn.maplist() end -- cgit