Skip to content

Commit 9bcee3d

Browse files
committed
fix: use :startinsert instead of feedkeys to switch to terminal mode
If user has their own `startinsert`, e.g. in the following case, `startinsert` is trigger by `:term`, but later fzf term is focused. ``` nvim \ +"au TermOpen * startinsert" \ +"term" \ +"lua vim.defer_fn(function() vim.cmd [[FzfLua files]] end, 100)" ``` Technically user should not `startinsert` on an incorrect window, but currently there's no way to avoid it since builtin `startinsert` seems defered but `nvim_get_mode` is fast (return actual mode immediately).
1 parent 52a2d2b commit 9bcee3d

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

lua/fzf-lua/fzf.lua

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ function M.raw_fzf(contents, fzf_cli_args, opts)
222222
vim.keymap.set("t", "<C-c>", "<Esc>", { buffer = 0 })
223223
end
224224

225+
vim.api.nvim_create_autocmd("TermOpen", { once = true, buffer = 0, command = "startinsert" })
225226
-- A more robust way of entering TERMINAL mode "t". We had quite a few issues
226227
-- sending `feedkeys|startinsert` after the term job is started, this approach
227228
-- seems more consistent as it triggers when entering terminal normal mode "nt"
@@ -333,14 +334,8 @@ function M.raw_fzf(contents, fzf_cli_args, opts)
333334
-- fzf-tmux spawns outside neovim, don't set filetype/insert mode
334335
if not opts.is_fzf_tmux then
335336
vim.bo.filetype = "fzf"
336-
337337
-- See note in "ModeChanged" above
338-
if vim.api.nvim_get_mode().mode == "t" then
339-
-- Called from another fzf-win most likely
340-
utils.feed_keys_termcodes("i")
341-
else
342-
vim.cmd [[startinsert]]
343-
end
338+
-- utils.ensure_startinsert()
344339
end
345340

346341
return coroutine.yield()

lua/fzf-lua/utils.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,26 @@ function M.setmetatable__gc(t, mt)
13171317
return setmetatable(t, mt)
13181318
end
13191319

1320+
---@param retry integer?
1321+
---@param timeout integer?
1322+
function M.ensure_startinsert(retry, timeout)
1323+
local bufnr = vim.api.nvim_get_current_buf()
1324+
local function ensure_startinsert(_retry, _timeout)
1325+
-- abort for some reason we switch to another buffer
1326+
if vim.api.nvim_get_current_buf() ~= bufnr then return end
1327+
if vim.api.nvim_get_mode().mode ~= "t" or retry == 0 then
1328+
vim.cmd [[startinsert]]
1329+
return
1330+
end
1331+
-- Called from another fzf-win most likely
1332+
-- don't startinsert until we enter new terminal mode
1333+
return vim.defer_fn(function()
1334+
return ensure_startinsert(_retry - 1, math.min(100, _timeout * 2))
1335+
end, _timeout)
1336+
end
1337+
return ensure_startinsert(retry or 5, timeout or 10)
1338+
end
1339+
13201340
--- Checks if treesitter parser for language is installed
13211341
---@param lang string
13221342
function M.has_ts_parser(lang)

tests/file/ui_spec.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,4 +206,25 @@ T["files()"]["preview should work after chdir #1864"] = function()
206206
child.expect_screen_lines(screen_opts)
207207
end
208208

209+
T["files()"]["perioldly reset mode"] = function()
210+
child.cmd [[exe 'au TermOpen * startinsert' | term]]
211+
sleep(10)
212+
child.lua [=[
213+
FzfLua.files {
214+
cwd_prompt = false,
215+
fzf_opts = { ["--no-info"] = "", ["--info"] = false },
216+
query = "foo",
217+
}
218+
]=]
219+
child.wait_until(function() return child.lua_get([[_G._fzf_load_called]]) == true end)
220+
-- child.expect_screen_lines({ start_line = 1, end_line = 3 })
221+
-- print("\n")
222+
-- print(child.get_screen_lines({ start_line = 1, end_line = 3 }))
223+
child.assert_screen_lines([[
224+
╭─────────────────── Files h ────────────────────╮
225+
│> foo │
226+
│──────────────────────────────────────────────── │
227+
]], { start_line = 3, end_line = 5 })
228+
end
229+
209230
return T

0 commit comments

Comments
 (0)