From e8b2093821553a9a338f5a6c570a687934dffe70 Mon Sep 17 00:00:00 2001 From: bhagwan Date: Sat, 16 Mar 2024 10:37:43 -0700 Subject: [PATCH] feat: better INSERT mode detection (#1088) --- lua/fzf-lua/actions.lua | 26 -------------------------- lua/fzf-lua/fzf.lua | 28 +++++++++------------------- lua/fzf-lua/utils.lua | 1 + 3 files changed, 10 insertions(+), 45 deletions(-) diff --git a/lua/fzf-lua/actions.lua b/lua/fzf-lua/actions.lua index 373097f3..4bb9288b 100644 --- a/lua/fzf-lua/actions.lua +++ b/lua/fzf-lua/actions.lua @@ -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))) diff --git a/lua/fzf-lua/fzf.lua b/lua/fzf-lua/fzf.lua index f9515edc..3a5cfc85 100644 --- a/lua/fzf-lua/fzf.lua +++ b/lua/fzf-lua/fzf.lua @@ -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("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 diff --git a/lua/fzf-lua/utils.lua b/lua/fzf-lua/utils.lua index 68160bb5..7d2349f9 100644 --- a/lua/fzf-lua/utils.lua +++ b/lua/fzf-lua/utils.lua @@ -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