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.38k stars 177 forks source link

feature: <expr> mappings #452

Closed tmillr closed 1 year ago

tmillr commented 1 year ago

Did you check the docs?

Is your feature request related to a problem? Please describe.

Regular vim/nvim supports expression mappings. For one, they can be useful with conditional mappings (e.g. noremap back to the default keys/action depending upon some runtime condition, otherwise do a different action/keys). Providing this ability would make it so that you don't have to use :normal or feedkeys() within the function (i.e. the fn passed as part of the mapping definition) as a workaround.

Describe the solution you'd like

Allow passing expr = true in mapping definition, in which case the string returned by the fn at runtime becomes the result of the mapping.

Describe alternatives you've considered

using :normal or nvim_feedkeys() inside the lua fn

I suppose you could also resort to a vimscript command string mapping (using <Cmd>{logic}<CR>), but it'd be nicer to be able to write the all of the logic within an/the actual lua function instead (where syntax hl and lsp works, formatting works, you can write multiple lines easily, etc.), returning only the string/keys as the result. This probably still requires the use of :normal or feedkeys as well. Plus, v:lua.* within the mapping string can only be used to call global or required functions.

TLDR: providing this feature would provide a cleaner, nicer and more straightforward solution, vim.keymap.set() allows passing expr = true

Additional context

No response

tmillr commented 1 year ago

I'm not sure if which-key already allows passing expr = true? (which seems to be the case)

If so, it doesn't appear to be documented along with the other options.

folke commented 1 year ago

That's already supported

tmillr commented 1 year ago

Oh ok, I guess it's just not documented then

thomastthai commented 1 month ago

For anyone coming from the future, expr = true support is unofficially documented in issue #67.