]> granicus.if.org Git - vim/commitdiff
patch 8.2.4338: an error from an expression mapping messes up the display v8.2.4338
authorBram Moolenaar <Bram@vim.org>
Thu, 10 Feb 2022 14:07:41 +0000 (14:07 +0000)
committerBram Moolenaar <Bram@vim.org>
Thu, 10 Feb 2022 14:07:41 +0000 (14:07 +0000)
Problem:    An error from an expression mapping messes up the display.
Solution:   When the expression results in an empty string return K_IGNORE.
            In cmdline mode redraw the command line. (closes #9726)

src/getchar.c
src/testdir/dumps/Test_map_expr_2.dump [new file with mode: 0644]
src/testdir/dumps/Test_map_expr_3.dump [new file with mode: 0644]
src/testdir/dumps/Test_map_expr_4.dump [new file with mode: 0644]
src/testdir/test_mapping.vim
src/version.c

index c7a1cca1a6093103e885886f31dd807aada8b721..8513679de810b087ff48d1931845560f14f1b3e9 100644 (file)
@@ -2840,6 +2840,7 @@ handle_mapping(
            int save_may_garbage_collect = may_garbage_collect;
            int was_screen_col = screen_cur_col;
            int was_screen_row = screen_cur_row;
+           int prev_did_emsg = did_emsg;
 
            vgetc_busy = 0;
            may_garbage_collect = FALSE;
@@ -2852,6 +2853,29 @@ handle_mapping(
            windgoto(was_screen_row, was_screen_col);
            out_flush();
 
+           // If an error was displayed and the expression returns an empty
+           // string, generate a <Nop> to allow for a redraw.
+           if (prev_did_emsg != did_emsg
+                                      && (map_str == NULL || *map_str == NUL))
+           {
+               char_u  buf[4];
+
+               vim_free(map_str);
+               buf[0] = K_SPECIAL;
+               buf[1] = KS_EXTRA;
+               buf[2] = KE_IGNORE;
+               buf[3] = NUL;
+               map_str = vim_strsave(buf);
+               if (State & CMDLINE)
+               {
+                   // redraw the command below the error
+                   msg_didout = TRUE;
+                   if (msg_row < cmdline_row)
+                       msg_row = cmdline_row;
+                   redrawcmd();
+               }
+           }
+
            vgetc_busy = save_vgetc_busy;
            may_garbage_collect = save_may_garbage_collect;
        }
diff --git a/src/testdir/dumps/Test_map_expr_2.dump b/src/testdir/dumps/Test_map_expr_2.dump
new file mode 100644 (file)
index 0000000..270a233
--- /dev/null
@@ -0,0 +1,10 @@
+> +0&#ffffff0@74
+|~+0#4040ff13&| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+| +0#0000000&@56|0|,|0|-|1| @8|A|l@1| 
diff --git a/src/testdir/dumps/Test_map_expr_3.dump b/src/testdir/dumps/Test_map_expr_3.dump
new file mode 100644 (file)
index 0000000..6404e8c
--- /dev/null
@@ -0,0 +1,10 @@
+|~+0#4040ff13#ffffff0| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|:+0#0000000&|a|b|c| @70
+|E+0#ffffff16#e000002|r@1|o|r| |d|e|t|e|c|t|e|d| |w|h|i|l|e| |p|r|o|c|e|s@1|i|n|g| |f|u|n|c|t|i|o|n| |F|u|n|c|[|1|]|.@1|f|u|n|c|t|i|o|n| |F|u|n|c|:| +0#0000000#ffffff0@10
+|l+0#af5f00255&|i|n|e| @3|1|:| +0#0000000&@64
+|E+0#ffffff16#e000002|6|0|5|:| |E|x|c|e|p|t|i|o|n| |n|o|t| |c|a|u|g|h|t|:| |t|e|s|t| +0#0000000#ffffff0@42
+|:|a|b|c> @70
diff --git a/src/testdir/dumps/Test_map_expr_4.dump b/src/testdir/dumps/Test_map_expr_4.dump
new file mode 100644 (file)
index 0000000..270a233
--- /dev/null
@@ -0,0 +1,10 @@
+> +0&#ffffff0@74
+|~+0#4040ff13&| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+| +0#0000000&@56|0|,|0|-|1| @8|A|l@1| 
index b9c9fa528e6d34dabba985f1fb8bc022bc187d83..e18927cfcfd8ac5551f374823814ae5d5bb75322 100644 (file)
@@ -549,6 +549,38 @@ func Test_expr_map_restore_cursor()
   call delete('XtestExprMap')
 endfunc
 
+func Test_expr_map_error()
+  CheckScreendump
+
+  let lines =<< trim END
+      func Func()
+        throw 'test'
+        return ''
+      endfunc
+
+      nnoremap <expr> <F2> Func()
+      cnoremap <expr> <F2> Func()
+
+      call test_override('ui_delay', 10)
+  END
+  call writefile(lines, 'XtestExprMap')
+  let buf = RunVimInTerminal('-S XtestExprMap', #{rows: 10})
+  call TermWait(buf)
+  call term_sendkeys(buf, "\<F2>")
+  call TermWait(buf)
+  call term_sendkeys(buf, "\<CR>")
+  call VerifyScreenDump(buf, 'Test_map_expr_2', {})
+
+  call term_sendkeys(buf, ":abc\<F2>")
+  call VerifyScreenDump(buf, 'Test_map_expr_3', {})
+  call term_sendkeys(buf, "\<Esc>0")
+  call VerifyScreenDump(buf, 'Test_map_expr_4', {})
+
+  " clean up
+  call StopVimInTerminal(buf)
+  call delete('XtestExprMap')
+endfunc
+
 " Test for mapping errors
 func Test_map_error()
   call assert_fails('unmap', 'E474:')
index a02fefdc4c4e78d2329dd65a9b270358210bf5b7..db2a9239211d700839e825e5c9befa3c9da411ea 100644 (file)
@@ -746,6 +746,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    4338,
 /**/
     4337,
 /**/