Skip to content

Commit dbfab99

Browse files
committed
fix: use :startinsert instead of feedkeys to switch to terminal mode
1 parent d646092 commit dbfab99

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

lua/fzf-lua/fzf.lua

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,7 @@ function M.raw_fzf(contents, fzf_cli_args, opts)
183183
if fzfwin then fzfwin:update_statusline() end
184184

185185
-- See note in "ModeChanged" above
186-
if vim.api.nvim_get_mode().mode == "t" then
187-
-- Called from another fzf-win most likely
188-
utils.feed_keys_termcodes("i")
189-
else
190-
vim.cmd [[startinsert]]
191-
end
186+
utils.ensure_startinsert()
192187
end
193188

194189
return coroutine.yield()

lua/fzf-lua/utils.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,5 +1663,27 @@ end
16631663
---@class fzf-lua.wo: vim.wo,{}
16641664
M.wo = new_win_opt_accessor()
16651665

1666+
---@param bufnr integer?
1667+
---@param retry integer?
1668+
---@param timeout integer?
1669+
---@return any
1670+
function M.ensure_startinsert(bufnr, retry, timeout)
1671+
bufnr = bufnr or vim.api.nvim_get_current_buf()
1672+
local function ensure_startinsert(_retry, _timeout)
1673+
if _retry == 0 then return end
1674+
-- abort for some reason we switch to another buffer
1675+
if vim.api.nvim_get_current_buf() ~= bufnr then return end
1676+
if vim.api.nvim_get_mode().mode ~= "t" then
1677+
pcall(vim.cmd.startinsert)
1678+
return
1679+
end
1680+
-- Called from another fzf-win most likely
1681+
-- don't startinsert until we enter new terminal mode
1682+
return vim.defer_fn(function()
1683+
return ensure_startinsert(_retry - 1, math.min(100, _timeout * 2))
1684+
end, _timeout)
1685+
end
1686+
return ensure_startinsert(retry or 5, timeout or 10)
1687+
end
16661688

16671689
return M

0 commit comments

Comments
 (0)