liuchengxu/vim-which-key

feedkeys function add mode string (like 'n') to avoid remap keys

KnightCS opened this issue · 2 comments

Environment (please complete the following information):

  • OS: wsl
  • (Neo)Vim version: 8.2
  • vim-which-key version: lastest
  • Have you reproduced with a minimal vimrc: no

Describe the bug
nnoremap z key recursive_mapping

To Reproduce
Steps to reproduce the behavior:

  1. Create the minimal vimrc min.vim:
let g:key = { 'name': 'z', '+': ['z+', 'redraw'],}
nnoremap <silent> z :<C-U>WhichKey! g:key.z<CR>
  1. Start (neo)vim with command: vim -u min.vim

  2. Type 'z+'

  3. bug: flicker

Expected behavior
nnoremap z key to display some fold commands

** reason **
nnoremap key recursive, when I input z+, as i input :<C-U>WhichKey! g:key.z<CR>:<C-U>WhichKey! g:key.z<CR>:<C-U>WhichKey! g:key.z<CR>:<C-U>WhichKey! g:key.z<CR>...+

** how to fix **
Feedkeys has a arg {mode} , the flags 'n' will "Do not remap keys."
It will fix this program.

At a glance, two problems, and one thought:
Problem 1:

nnoremap <silent> z :<C-U>WhichKey! g:key.z<CR>

Should use the form :WhichKey! {dict}:

nnoremap <silent> z :<C-U>WhichKey! g:key<CR>

I'm not sure how which-key would interpret that back then but shouldn't work now.

Problem 2:

let g:key = { 'name': 'z', '+': ['z+', 'redraw'],}

Should be:

let g:which_key_fallback_to_native_key = 1
let g:key = { 'name': 'z', '+': 'redraw',}

And z+ will work, with the 'redraw' label, and regardless of if the user delays long enough for the popup window. Bug with the native fallback on current master that I accidentally introduced but I'll fix that in PR.

One thought:
Doesn't look like the suggestion is in place. Not sure of all implications. Though I think the above methodology I wrote is probably what is desirable by everyone, as the end-user doesn't have to exhaustively enumerate the many out-of-the-box mappings as described in #150. So, closing this. Any reader is free to re-open if there is indeed a use-case.

Oh, I solve this problem by another config way.

Actually, the above config is problematic, it should be:

let g:key_z = {
            \ 'name': 'z',
            \ '+': ['z+', 'top redraw'],
            \ }

nnoremap <silent> z :<C-U>WhichKey! g:key_z<CR>

And when i input z+, it will call feedkeys('z+'), and 'z' is map to 'WhichKey',
so it will loop in show 'z' window.

So i fix this by this config:

let g:key_z = {
            \ 'name': 'z',
            \ '+': ['call feedkeys("z+", "n")', 'top redraw'],
            \ }

nnoremap <silent> z :<C-U>WhichKey! g:key_z<CR>

And this will on map to 'z', it work fine for me now.