folke / which-key.nvim

💥 Create key bindings that stick. WhichKey helps you remember your Neovim keymaps, by showing available keybindings in a popup as you type.
Apache License 2.0
5.4k stars 176 forks source link

bug: i_ctrl-o mappings not executed, chars are inserted instead #827

Closed bmesot closed 8 hours ago

bmesot commented 2 months ago

Did you check docs and existing issues?

Neovim version (nvim -v)

v0.10.0

Operating system/version

Debian GNU/Linux trixie/sid

Describe the bug

This issue is a remake of issue 141. The mapping chars are inserted in the buffer instead of being processed in normal mode.

I managed to fix the problem by applying the following patch.

diff --git a/lua/which-key/state.lua b/lua/which-key/state.lua
index 83ffd71..d6f8edf 100644
--- a/lua/which-key/state.lua
+++ b/lua/which-key/state.lua
@@ -234,6 +234,12 @@ function M.execute(state, key, node)
     if vim.v.register ~= Util.reg() and state.mode.mode ~= "i" and state.mode.mode ~= "c" then
       keystr = '"' .. vim.v.register .. keystr
     end
+
+    local curr_mode = vim.api.nvim_get_mode().mode
+
+    if curr_mode:find("ni[IRV]") ~= nil then
+      keystr = "<C-O>" .. keystr
+    end
   end
   Util.debug("feedkeys", tostring(state.mode), keystr)
   local feed = vim.api.nvim_replace_termcodes(keystr, true, true, true)

Steps To Reproduce

  1. Open a buffer and enter insert mode (i).
  2. Press <c-o> followed by zz.
  3. zz is written in the buffer.

Expected Behavior

  1. zz is executed as a mapping in normal mode.

Health

which-key: require("which-key.health").check()

- OK Most of these checks are for informational purposes only.
  WARNINGS should be treated as a warning, and don't necessarily indicate a problem with your config.
  Please |DON't| report these warnings as an issue.

Checking your config ~
- WARNING |mini.icons| is not installed
- WARNING |nvim-web-devicons| is not installed
- WARNING Keymap icon support will be limited.

Checking for issues with your mappings ~
- OK No issues reported

checking for overlapping keymaps ~
- WARNING In mode `n`, <gc> overlaps with <gcc>:
  - <gc>: Toggle comment
  - <gcc>: Toggle comment line
- OK Overlapping keymaps are only reported for informational purposes.
  This doesn't necessarily mean there is a problem with your config.

Checking for duplicate mappings ~
- OK No duplicate mappings found

Log

Debug Started for v3.13.2
{
  branch = "main",
  commit = "6c1584eb76b55629702716995cca4ae2798a9cca"
}
new Mode(n:1)
Trigger(add) Mode(n:1) ' ` " z= g' g` ] [ <C-W> z g
on_key: i
ModeChanged(n:i)
  new Mode(i:1)
  Safe(true)
Trigger(add) Mode(i:1) <C-R>
on_key: <C-O>
ModeChanged(i:niI)
  Safe(true)
on_key: z
State(start): Mode(n:0) Node(z) { keys = "z" }
  update Mode(n:1)
  continue: z Mode(n:1)
  getchar
  on_key: z
  got: z
  suspend: Mode(n:1)
  Trigger(del) Mode(n:1) z= g' g` ] [ <C-W> z ' ` " g
  feedkeys: Mode(n:1) zz
ModeChanged(niI:i)
  Unsafe(pending "z")
  suspend: Mode(i:1)
  Trigger(del) Mode(i:1) <C-R>
on_key: z
on_key: z
Trigger(add) Mode(n:1) ' ` " z= g' g` ] [ <C-W> z g
Trigger(add) Mode(i:1) <C-R>
on_key: <Esc>
ModeChanged(i:n)
  Safe(true)
on_key: :
ModeChanged(n:c)
  new Mode(c:1)
  Safe(true)
Trigger(add) Mode(c:1) <C-R>
on_key: q
on_key: !
on_key: <CR>
ModeChanged(c:n)
  Unsafe(command-mode)
  suspend: Mode(n:1)
  Trigger(del) Mode(n:1) z= g' g` ] [ <C-W> z ' ` " g
Trigger(add) Mode(n:1) ' ` " z= g' g` ] [ <C-W> z g

Repro

vim.env.LAZY_STDPATH = ".repro"
load(vim.fn.system("curl -s https://raw.githubusercontent.com/folke/lazy.nvim/main/bootstrap.lua"))()

require("lazy.minit").repro({
  spec = {
    { "folke/which-key.nvim", opts = {} },
    -- add any other plugins here
  },
})
blueberry-zero commented 2 months ago

Ran into the same issue.

You can implement a work around with the following mapping.

vim.keymap.set("i", "<YOUR_BINDING_HERE>", function()
    vim.cmd("startinsert!")
    vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, true, true), 'n', false) 
end, { expr = true })
github-actions[bot] commented 1 month ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

EdwinRy commented 1 month ago

I ran into the same issue, the issue patch fixed the problem for me.

github-actions[bot] commented 1 week ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] commented 8 hours ago

This issue was closed because it has been stalled for 7 days with no activity.