From 81473c8ab241286aafe8724d173df2c6b692e119 Mon Sep 17 00:00:00 2001
From: zeertzjq <zeertzjq@outlook.com>
Date: Sat, 14 Jan 2023 19:31:11 +0800
Subject: [PATCH] vim-patch:8.2.4382: a custom 'tabline' may cause Esc to work
 like Enter

Problem:    A custom 'tabline' may cause Esc to work like Enter on the
            command line when the popup menu is displayed.
Solution:   Save and restore KeyTyped. (closes vim/vim#9776)

https://github.com/vim/vim/commit/e4835bf34001471a102528659af009bc46361141

Co-authored-by: Bram Moolenaar <Bram@vim.org>
---
 src/nvim/statusline.c             |  8 ++++----
 src/nvim/testdir/test_cmdline.vim | 15 +++++++++++++++
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/nvim/statusline.c b/src/nvim/statusline.c
index 4f1453a815..ca157ba697 100644
--- a/src/nvim/statusline.c
+++ b/src/nvim/statusline.c
@@ -268,6 +268,7 @@ static void win_redr_custom(win_T *wp, bool draw_winbar, bool draw_ruler)
   StlClickRecord *tabtab;
   win_T *ewp;
   int p_crb_save;
+  bool save_KeyTyped = KeyTyped;
   bool is_stl_global = global_stl_height() > 0;
 
   ScreenGrid *grid = &default_grid;
@@ -422,6 +423,9 @@ static void win_redr_custom(win_T *wp, bool draw_winbar, bool draw_ruler)
 
 theend:
   entered = false;
+
+  // A user function may reset KeyTyped, restore it.
+  KeyTyped = save_KeyTyped;
 }
 
 void win_redr_winbar(win_T *wp)
@@ -639,7 +643,6 @@ int fillchar_status(int *attr, win_T *wp)
 void redraw_custom_statusline(win_T *wp)
 {
   static bool entered = false;
-  bool saved_KeyTyped = KeyTyped;
 
   // When called recursively return.  This can happen when the statusline
   // contains an expression that triggers a redraw.
@@ -650,9 +653,6 @@ void redraw_custom_statusline(win_T *wp)
 
   win_redr_custom(wp, false, false);
   entered = false;
-
-  // A user function may reset KeyTyped, restore it.
-  KeyTyped = saved_KeyTyped;
 }
 
 static void ui_ext_tabline_update(void)
diff --git a/src/nvim/testdir/test_cmdline.vim b/src/nvim/testdir/test_cmdline.vim
index 68560d5d49..8fc6e9847d 100644
--- a/src/nvim/testdir/test_cmdline.vim
+++ b/src/nvim/testdir/test_cmdline.vim
@@ -2312,6 +2312,15 @@ func Test_wildmenu_pum()
       set statusline=%!MyStatusLine()
       set laststatus=2
     endfunc
+
+    func MyTabLine()
+      return 'my tab line'
+    endfunc
+    func SetupTabline()
+      set statusline=
+      set tabline=%!MyTabLine()
+      set showtabline=2
+    endfunc
   [CODE]
   call writefile(commands, 'Xtest')
 
@@ -2503,6 +2512,12 @@ func Test_wildmenu_pum()
   call term_sendkeys(buf, "\<Esc>")
   call VerifyScreenDump(buf, 'Test_wildmenu_pum_39', {})
 
+  " Esc still works to abort the command when 'tabline' is set
+  call term_sendkeys(buf, ":call SetupTabline()\<CR>")
+  call term_sendkeys(buf, ":si\<Tab>")
+  call term_sendkeys(buf, "\<Esc>")
+  call VerifyScreenDump(buf, 'Test_wildmenu_pum_40', {})
+
   call term_sendkeys(buf, "\<C-U>\<CR>")
   call StopVimInTerminal(buf)
   call delete('Xtest')