Skip to content

Commit

Permalink
feat: better INSERT mode detection (#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibhagwan committed Mar 16, 2024
1 parent 875d521 commit e8b2093
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 45 deletions.
26 changes: 0 additions & 26 deletions lua/fzf-lua/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -429,32 +429,6 @@ M.toggle_bg = function(_, _)
utils.info(string.format([[background set to "%s"]], vim.o.background))
end

M.ensure_insert_mode = function()
-- not sure what is causing this, tested with
-- 'NVIM v0.6.0-dev+575-g2ef9d2a66'
-- vim.cmd("startinsert") doesn't start INSERT mode
-- 'mode' returns { blocking = false, mode = "t" }
-- manually input 'i' seems to workaround this issue
-- **only if fzf term window was succefully opened (#235)
-- this is only required after the 'nt' (normal-terminal)
-- mode was introduced along with the 'ModeChanged' event
-- https://github.com/neovim/neovim/pull/15878
-- https://github.com/neovim/neovim/pull/15840
-- local has_mode_nt = not vim.tbl_isempty(
-- vim.fn.getcompletion('ModeChanged', 'event'))
-- or vim.fn.has('nvim-0.6') == 1
-- if has_mode_nt then
-- local mode = vim.api.nvim_get_mode()
-- local wininfo = vim.fn.getwininfo(vim.api.nvim_get_current_win())[1]
-- if vim.bo.ft == 'fzf'
-- and wininfo.terminal == 1
-- and mode and mode.mode == 't' then
-- vim.cmd[[noautocmd lua vim.api.nvim_feedkeys('i', 'n', true)]]
-- end
-- end
utils.warn("calling 'ensure_insert_mode' is no longer required and can be safely omitted.")
end

M.run_builtin = function(selected)
local method = selected[1]
pcall(loadstring(string.format("require'fzf-lua'.%s()", method)))
Expand Down
28 changes: 9 additions & 19 deletions lua/fzf-lua/fzf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -294,26 +294,16 @@ function M.raw_fzf(contents, fzf_cli_args, opts)
if not opts.is_fzf_tmux then
vim.cmd [[set ft=fzf]]

-- terminal behavior seems to have changed after the introduction
-- of 'nt' mode (terminal-normal mode) which is included in 0.6
-- https://github.com/neovim/neovim/pull/15878
-- Preferably I'd like to check if the vim patch is included using
-- vim.fn.has('patch-8.2.3461')
-- but this doesn't work for vim patches > 8.1 as explained in:
-- https://github.com/neovim/neovim/issues/9635
-- However, since this patch was included in 0.6 we can test
-- for neovim version 0.6
-- Beats me why 'nvim_get_mode().mode' still returns 'nt' even
-- after we're clearly in insert mode or why `:startinsert`
-- won't change the mode from 'nt' to 't' so we use feedkeys()
-- instead.
-- This "retires" 'actions.ensure_insert_mode' and solves the
-- issue of calling an fzf-lua mapping from insert mode (#429)

if vim.fn.has("nvim-0.6") == 1 and vim.api.nvim_get_mode().mode ~= "i" then
vim.cmd([[noautocmd lua vim.api.nvim_feedkeys(]]
.. [[vim.api.nvim_replace_termcodes("<Esc>i", true, false, true)]]
.. [[, 'n', true)]])
-- Since patch-8.2.3461 which was released with 0.6 neovim distinguishes between
-- Normal mode and Terminal-Normal mode. However, this seems to have also introduced
-- a bug with `startinsert`: When fzf-lua reuses interfaces (e.g. called from "builtin"
-- or grep<->live_grep toggle) the current mode will be "t" which is Terminal (INSERT)
-- mode but our interface is still opened in NORMAL mode, either `startinsert` is not
-- working (as it's technically already in INSERT) or creating a new terminal buffer
-- within the same window starts in NORMAL mode while returning the wrong `nvim_get_mode
if utils.__HAS_NVIM_06 and vim.api.nvim_get_mode().mode == "t" then
utils.feed_keys_termcodes("i")
else
vim.cmd [[startinsert]]
end
Expand Down
1 change: 1 addition & 0 deletions lua/fzf-lua/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ end

local M = {}

M.__HAS_NVIM_06 = vim.fn.has("nvim-0.6") == 1
M.__HAS_NVIM_07 = vim.fn.has("nvim-0.7") == 1
M.__HAS_NVIM_08 = vim.fn.has("nvim-0.8") == 1
M.__HAS_NVIM_09 = vim.fn.has("nvim-0.9") == 1
Expand Down

0 comments on commit e8b2093

Please sign in to comment.