Kakoune Filetype Mode
The Kakoune editor has a very simple and powerful mechanism to specify a hierarchy of key bindings. It is quite common to map the space character in normal mode to commands that include many mode selectors, leading to additional mappings.
Dynamic key bindings are usually implemented with hooks. Enter a mode, and rebind keys for the current context. That seemed cumbersome to me. I wanted to change the mode used for key lookup rather than the bindings in the common mappings. I also wanted to avoid collisions in mappings with user modes that others have produced.
The solution is a mapping that enters a user mode named for the current filetype. The single quote is unmapped by default, and is close to home position on my standard US keyboard, so let’s use that:
map global normal "'" %{: enter-user-mode %opt{filetype}<ret>}
If the filetype does not have a corresponding user mode, this simply throws an error. Otherwise it gives access to your filetype-specific mappings through a consistent top-level mapping.
You can even create a dynamic hierarchy if your user mode warrants it. In the filetype mode, just map a key to enter the “parent” mode. Using the same mapping key, single quote here, seems natural to me.
And that’s it!
I have not seen anything like this elsewhere and think this is a new use of user modes. Please send me a note if you know otherwise.