@@ -1166,6 +1166,25 @@ local restore_altbuf = function(buf)
11661166 end
11671167end
11681168
1169+ local stopinsert = function (ctx )
1170+ _G .fzf_lua_stopinsert_hack = nil
1171+ if not (ctx .mode == " nt" and api .nvim_get_mode () ~= " nt" ) then return end
1172+ vim .cmd " stopinsert"
1173+ local pat = " FzfLuaStopInsertHack"
1174+ _G .fzf_lua_stopinsert_hack = function (cb )
1175+ cb = vim .schedule_wrap (cb ) -- nvim_get_mode don't seem get correct mode without this
1176+ api .nvim_create_autocmd (" User" , { pattern = pat , once = true , callback = cb })
1177+ end
1178+ api .nvim_create_autocmd (" ModeChanged" , {
1179+ pattern = " *:nt" ,
1180+ once = true ,
1181+ callback = function ()
1182+ api .nvim_exec_autocmds (" User" , { pattern = pat , modeline = false })
1183+ _G .fzf_lua_stopinsert_hack = nil
1184+ end ,
1185+ })
1186+ end
1187+
11691188--- @param fzf_bufnr ? integer
11701189--- @param hide ? boolean
11711190function FzfWin :close (fzf_bufnr , hide )
@@ -1179,8 +1198,7 @@ function FzfWin:close(fzf_bufnr, hide)
11791198 -- switching from a term win to another term win preserves terminal mode
11801199 -- even if the target window was in normal terminal mode (#2054 #2419)
11811200 local ctx = utils .__CTX () or {}
1182- -- if ctx.mode and ctx.mode:match("^n") then vim.cmd "stopinsert" end
1183- if ctx .mode == " nt" then vim .cmd " stopinsert" end
1201+ stopinsert (ctx )
11841202 if self .fzf_winid and api .nvim_win_is_valid (self .fzf_winid ) then
11851203 -- restore the original last window
11861204 restore_lastwin (ctx .winid , ctx .last_winid )
0 commit comments