Kak non-recursive mapping (vim's noremap, inoremap...)

I have a mapping map global user ( a)<esc>i(<esc>H so that ,( wraps the selection in (), but I’m also using GitHub - alexherbo2/auto-pairs.kak: Auto-pairing of characters for Kakoune which binds ( to ()

So pressing ,( when selecting a word turns it into ()word) instead of (word). How do I do vim’s equivalent inoremap command that won’t evaluate the auto-pairs mapping?

Kakoune doesn’t have recursive mappings, so all Kakoune mappings are non-recursive. However, when a hook event occurs, all the hooks registered to that event will fire. Looking at the source to auto-pairs, we see that it uses the InsertChar and InsertDelete hooks:

define-command -override -hidden auto-close-pair -params 2 %{
  hook -group auto-pairs global InsertChar "\Q%arg{1}" "handle-inserted-opening-pair %%<%arg{1}> %%<%arg{2}>"
  hook -group auto-pairs global InsertDelete "\Q%arg{1}" "handle-deleted-opening-pair %%<%arg{1}> %%<%arg{2}>"
}

Because these are hooks, not mappings, they’ll trigger every time a matching character is inserted or deleted in insert mode, even if that character was produced by a mapping. However, Kakoune provides a way to temporarily disable hooks when entering insert mode, with the \ prefix. If you enter insert mode with i, hooks work as normal, but if you enter insert mode with \i then hooks are disabled (you’ll see [no-hooks] in the status bar).

Therefore, a mapping like this will probably do what you want:

map global user ( \a)<esc>\i(<esc>H
2 Likes

Huh. I even knew about \ for editing but I figured its one use was pasting. Thank you for the great explanation, it makes sense and works perfectly.