From faa0b1028ab1888da368b5dcff6a66d91bfb9060 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 29 Oct 2023 08:37:37 +0800 Subject: [PATCH] fix(terminal): avoid Insert mode in Terminal buffer (cherry picked from commit 9a73cac99c6d2553c210864a68633c30406b9e34) --- src/nvim/edit.c | 7 +++++++ test/functional/terminal/ex_terminal_spec.lua | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 07885039e1..1aaf66fb65 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -393,6 +393,13 @@ static int insert_check(VimState *state) Insstart_orig = Insstart; } + if (curbuf->terminal) { + // Exit Insert mode and go to Terminal mode. + stop_insert_mode = true; + restart_edit = 'I'; + stuffcharReadbuff(K_NOP); + } + if (stop_insert_mode && !ins_compl_active()) { // ":stopinsert" used s->count = 0; diff --git a/test/functional/terminal/ex_terminal_spec.lua b/test/functional/terminal/ex_terminal_spec.lua index ebb0253caa..dac193b051 100644 --- a/test/functional/terminal/ex_terminal_spec.lua +++ b/test/functional/terminal/ex_terminal_spec.lua @@ -125,6 +125,20 @@ describe(':terminal', function() feed('a') eq({ blocking=false, mode='t' }, nvim('get_mode')) end) + + it('switching to terminal buffer in Insert mode goes to Terminal mode #7164', function() + command('terminal') + command('vnew') + feed('i') + command('let g:events = []') + command('autocmd InsertLeave * let g:events += ["InsertLeave"]') + command('autocmd TermEnter * let g:events += ["TermEnter"]') + command('inoremap wincmd p') + eq({ blocking=false, mode='i' }, nvim('get_mode')) + feed('') + eq({ blocking=false, mode='t' }, nvim('get_mode')) + eq({'InsertLeave', 'TermEnter'}, eval('g:events')) + end) end) describe(':terminal (with fake shell)', function()