Patch 8.2.4932
Problem:    Not easy to filter the output of maplist().
Solution:   Add mode_bits to the dictionary. (Ernie Rael, closes #10356)
Files:      runtime/doc/builtin.txt, src/map.c,
            src/testdir/test_map_functions.vim,
            src/testdir/test_vim9_builtin.vim


*** ../vim-8.2.4931/runtime/doc/builtin.txt     2022-05-07 12:48:24.070194799 
+0100
--- runtime/doc/builtin.txt     2022-05-10 17:45:41.571792573 +0100
***************
*** 5311,5317 ****
                  "lnum"     The line number in "sid", zero if unknown.
                  "nowait"   Do not wait for other, longer mappings.
                             (|:map-<nowait>|).
!                 "abbr"     True if this is an |abbreviation|.
  
                The dictionary can be used to restore a mapping with
                |mapset()|.
--- 5330,5340 ----
                  "lnum"     The line number in "sid", zero if unknown.
                  "nowait"   Do not wait for other, longer mappings.
                             (|:map-<nowait>|).
!                 "abbr"     True if this is an abbreviation |abbreviations|.
!                 "mode_bits" Vim's internal binary representation of "mode".
!                            |mapset()| ignores this; only "mode" is used.
!                            See |maplist()| for usage examples. The values
!                            are from src/vim.h and may change in the future.
  
                The dictionary can be used to restore a mapping with
                |mapset()|.
***************
*** 5372,5377 ****
--- 5395,5422 ----
                        vim9script
                        echo maplist()->filter(
                                (_, m) => match(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: >
+                       vim9script
+                       var saved_maps = []
+                       for m in maplist()
+                           if and(m.mode_bits, 0x19) != 0
+                               saved_maps->add(m)
+                           endif
+                       endfor
+                       echo saved_maps->mapnew((_, m) => m.lhs)
+ <             The values of the mode_bits are defined in Vim's src/vim.h
+               file and they can be discovered at runtime using
+               |:map-commands| and "maplist()". Example: >
+                       vim9script
+                       omap xyzzy <Nop>
+                       var 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()*
*** ../vim-8.2.4931/src/map.c   2022-05-07 20:01:10.058731693 +0100
--- src/map.c   2022-05-10 17:41:47.335778859 +0100
***************
*** 2303,2308 ****
--- 2303,2309 ----
      dict_add_number(dict, "nowait", mp->m_nowait ? 1L : 0L);
      dict_add_string(dict, "mode", mapmode);
      dict_add_number(dict, "abbr", abbr ? 1L : 0L);
+     dict_add_number(dict, "mode_bits", mp->m_mode);
  
      vim_free(mapmode);
  }
*** ../vim-8.2.4931/src/testdir/test_map_functions.vim  2022-05-09 
12:16:14.761073336 +0100
--- src/testdir/test_map_functions.vim  2022-05-10 17:41:47.335778859 +0100
***************
*** 19,31 ****
          \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16",
          \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
          \ 'lnum': lnum + 1, 
!       \ 'rhs': 'is<F4>foo', 'buffer': 0, 'abbr': 0},
        \ maparg('foo<C-V>', '', 0, 1))
    call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar',
          \ 'lhsraw': 'bar', 'mode': 'v',
          \ 'nowait': 0, 'expr': 1, 'sid': sid, 'scriptversion': 1,
          \ 'lnum': lnum + 2,
!       \ 'rhs': 'isbar', 'buffer': 1, 'abbr': 0},
          \ 'bar'->maparg('', 0, 1))
    let lnum = expand('<sflnum>')
    map <buffer> <nowait> foo bar
--- 19,31 ----
          \ 'lhsraw': "foo\x80\xfc\x04V", 'lhsrawalt': "foo\x16",
          \ 'mode': ' ', 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
          \ 'lnum': lnum + 1, 
!       \ 'rhs': 'is<F4>foo', 'buffer': 0, 'abbr': 0, 'mode_bits': 0x47},
        \ maparg('foo<C-V>', '', 0, 1))
    call assert_equal({'silent': 1, 'noremap': 1, 'script': 1, 'lhs': 'bar',
          \ 'lhsraw': 'bar', 'mode': 'v',
          \ 'nowait': 0, 'expr': 1, 'sid': sid, 'scriptversion': 1,
          \ 'lnum': lnum + 2,
!       \ 'rhs': 'isbar', 'buffer': 1, 'abbr': 0, 'mode_bits': 0x42},
          \ 'bar'->maparg('', 0, 1))
    let lnum = expand('<sflnum>')
    map <buffer> <nowait> foo bar
***************
*** 33,39 ****
          \ 'lhsraw': 'foo', 'mode': ' ',
          \ 'nowait': 1, 'expr': 0, 'sid': sid, 'scriptversion': 1,
          \ 'lnum': lnum + 1, 'rhs': 'bar',
!       \ 'buffer': 1, 'abbr': 0},
          \ maparg('foo', '', 0, 1))
    let lnum = expand('<sflnum>')
    tmap baz foo
--- 33,39 ----
          \ 'lhsraw': 'foo', 'mode': ' ',
          \ 'nowait': 1, 'expr': 0, 'sid': sid, 'scriptversion': 1,
          \ 'lnum': lnum + 1, 'rhs': 'bar',
!       \ 'buffer': 1, 'abbr': 0, 'mode_bits': 0x47},
          \ maparg('foo', '', 0, 1))
    let lnum = expand('<sflnum>')
    tmap baz foo
***************
*** 41,47 ****
          \ 'lhsraw': 'baz', 'mode': 't',
          \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
          \ 'lnum': lnum + 1, 'rhs': 'foo',
!       \ 'buffer': 0, 'abbr': 0},
          \ maparg('baz', 't', 0, 1))
    let lnum = expand('<sflnum>')
    iab A B
--- 41,47 ----
          \ 'lhsraw': 'baz', 'mode': 't',
          \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
          \ 'lnum': lnum + 1, 'rhs': 'foo',
!         \ 'buffer': 0, 'abbr': 0, 'mode_bits': 0x80},
          \ maparg('baz', 't', 0, 1))
    let lnum = expand('<sflnum>')
    iab A B
***************
*** 49,55 ****
          \ 'lhsraw': 'A', 'mode': 'i',
          \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
          \ 'lnum': lnum + 1, 'rhs': 'B',
!       \ 'buffer': 0, 'abbr': 1},
          \ maparg('A', 'i', 1, 1))
    iuna A
  
--- 49,55 ----
          \ 'lhsraw': 'A', 'mode': 'i',
          \ 'nowait': 0, 'expr': 0, 'sid': sid, 'scriptversion': 1,
          \ 'lnum': lnum + 1, 'rhs': 'B',
!       \ 'buffer': 0, 'abbr': 1, 'mode_bits': 0x0010},
          \ maparg('A', 'i', 1, 1))
    iuna A
  
*** ../vim-8.2.4931/src/testdir/test_vim9_builtin.vim   2022-05-09 
14:12:10.712386673 +0100
--- src/testdir/test_vim9_builtin.vim   2022-05-10 17:41:47.339778858 +0100
***************
*** 2464,2470 ****
          scriptversion: 999999,
          rhs: 'bar',
          buffer: 0,
!         abbr: 0})
    unmap foo
    v9.CheckDefAndScriptFailure(['maparg(1)'], ['E1013: Argument 1: type 
mismatch, expected string but got number', 'E1174: String required for argument 
1'])
    v9.CheckDefAndScriptFailure(['maparg("a", 2)'], ['E1013: Argument 2: type 
mismatch, expected string but got number', 'E1174: String required for argument 
2'])
--- 2464,2471 ----
          scriptversion: 999999,
          rhs: 'bar',
          buffer: 0,
!         abbr: 0,
!         mode_bits: 0x47})
    unmap foo
    v9.CheckDefAndScriptFailure(['maparg(1)'], ['E1013: Argument 1: type 
mismatch, expected string but got number', 'E1174: String required for argument 
1'])
    v9.CheckDefAndScriptFailure(['maparg("a", 2)'], ['E1013: Argument 2: type 
mismatch, expected string but got number', 'E1174: String required for argument 
2'])
*** ../vim-8.2.4931/src/version.c       2022-05-10 13:32:20.456548659 +0100
--- src/version.c       2022-05-10 17:48:36.915781496 +0100
***************
*** 748,749 ****
--- 748,751 ----
  {   /* Add new patch number below this line */
+ /**/
+     4932,
  /**/

-- 
"Hit any key to continue" is very confusing when you have two keyboards.

 /// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net   \\\
///                                                                      \\\
\\\        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20220510165107.0721B1C0761%40moolenaar.net.

Raspunde prin e-mail lui