Skip to content

Commit 194a3d2

Browse files
committed
fix: wrong mode after :term then quickly stopi->close->new picker->starti
1 parent 423533d commit 194a3d2

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

lua/fzf-lua/actions.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,9 @@ end
515515
M.run_builtin = function(selected)
516516
if not selected[1] then return end
517517
local method = selected[1]
518-
pcall(require "fzf-lua"[method])
518+
local func = vim.F.nil_wrap(require "fzf-lua"[method])
519+
if not _G.fzf_lua_stopinsert_hack then return func() end
520+
_G.fzf_lua_stopinsert_hack(func)
519521
end
520522

521523
M.ex_run = function(selected)

lua/fzf-lua/win.lua

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,6 +1166,25 @@ local restore_altbuf = function(buf)
11661166
end
11671167
end
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+
api.nvim_create_autocmd("User", { pattern = pat, once = true, callback = cb })
1176+
end
1177+
api.nvim_create_autocmd("ModeChanged", {
1178+
pattern = "*:nt",
1179+
once = true,
1180+
callback = function()
1181+
-- nvim_get_mode don't get correct mode without schedule_wrap
1182+
vim.schedule_wrap(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
11711190
function 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

Comments
 (0)