]> granicus.if.org Git - vim/commitdiff
patch 8.2.2804: setting buffer local mapping with mapset() changes global v8.2.2804
authorBram Moolenaar <Bram@vim.org>
Sat, 24 Apr 2021 11:12:38 +0000 (13:12 +0200)
committerBram Moolenaar <Bram@vim.org>
Sat, 24 Apr 2021 11:12:38 +0000 (13:12 +0200)
Problem:    Setting buffer local mapping with mapset() changes global mapping.
Solution:   Only set the local mapping. (closes #8143)

src/map.c
src/testdir/test_maparg.vim
src/version.c

index 917f1a244ff86ab938a4513b16c08d8b49b9082c..f142db09c43285421c5a785a08745c21b61d26ad 100644 (file)
--- a/src/map.c
+++ b/src/map.c
@@ -2295,6 +2295,7 @@ f_mapset(typval_T *argvars, typval_T *rettv UNUSED)
     int                noremap;
     int                expr;
     int                silent;
+    int                buffer;
     scid_T     sid;
     linenr_T   lnum;
     mapblock_T **map_table = maphash;
@@ -2336,18 +2337,31 @@ f_mapset(typval_T *argvars, typval_T *rettv UNUSED)
     silent = dict_get_number(d, (char_u *)"silent") != 0;
     sid = dict_get_number(d, (char_u *)"sid");
     lnum = dict_get_number(d, (char_u *)"lnum");
-    if (dict_get_number(d, (char_u *)"buffer"))
+    buffer = dict_get_number(d, (char_u *)"buffer");
+    nowait = dict_get_number(d, (char_u *)"nowait") != 0;
+    // mode from the dict is not used
+
+    if (buffer)
     {
        map_table = curbuf->b_maphash;
        abbr_table = &curbuf->b_first_abbr;
     }
-    nowait = dict_get_number(d, (char_u *)"nowait") != 0;
-    // mode from the dict is not used
 
     // Delete any existing mapping for this lhs and mode.
-    arg = vim_strsave(lhs);
-    if (arg == NULL)
-       return;
+    if (buffer)
+    {
+       arg = alloc(STRLEN(lhs) + STRLEN("<buffer>") + 1);
+       if (arg == NULL)
+           return;
+       STRCPY(arg, "<buffer>");
+       STRCPY(arg + 8, lhs);
+    }
+    else
+    {
+       arg = vim_strsave(lhs);
+       if (arg == NULL)
+           return;
+    }
     do_map(1, arg, mode, is_abbr);
     vim_free(arg);
 
index d0f2ec23f2a9ee273feca94f53e7d06ded504f9e..4cd32a5d3c8273bf9221e116d57421895c35fbaf 100644 (file)
@@ -254,6 +254,27 @@ func Check_ctrlb_map(d, check_alt)
   endif
 endfunc
 
+func Test_map_local()
+  nmap a global
+  nmap <buffer>a local
+
+  let prev_map_list = split(execute('nmap a'), "\n")
+  call assert_match('n\s*a\s*@local', prev_map_list[0])
+  call assert_match('n\s*a\s*global', prev_map_list[1])
+
+  let mapping = maparg('a', 'n', 0, 1)
+  call assert_equal(1, mapping.buffer)
+  let mapping.rhs = 'new_local'
+  call mapset('n', 0, mapping)
+
+  " Check that the global mapping is left untouched.
+  let map_list = split(execute('nmap a'), "\n")
+  call assert_match('n\s*a\s*@new_local', map_list[0])
+  call assert_match('n\s*a\s*global', map_list[1])
+
+  nunmap a
+endfunc
+
 func Test_map_restore()
   " Test restoring map with alternate keycode
   nmap <C-B> back
index a6667e97183da53b50ad4f15a57569e546402648..c5c3feeb3e8bd289d9573549dea3f058c3e1e540 100644 (file)
@@ -750,6 +750,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    2804,
 /**/
     2803,
 /**/