Repeatable Actions
Thanks to whichkey the structure of my mappings have taken a path of increasing complexity.
The more fine-grained control provided by which-key lead to an increased length of my mapped key sequences. This makes repeating actions for those longer key sequences tedious and gave birth to the need to shortcut those repetitions like good old repeat.vim does. Unfortunately repeat.vim is a pre-lua project.
As which-key maps actions to keybindings: wouldn't it be consistent with the concept of which-key to introduce a flag that marks actions as repeatable? I have in mind that after invoking an action flagged as repeatable whichkey repeats the action by merely pressing . or alternatively a user defined keybinding.
Maybe anyone wants to share thoughts on this feature or discuss ways of how to implement repeatable actions.
The following code may illustrates the idea of repeatable actions. The script generate actions a,...,k mapped to <leader><leader><leader><leader>a...k which could be repeated by pressing <F5> once they are invoked.
local r = function() end
local rmaps = {
name = 'repeatable actions'
}
for i=97, 97+10 do
local k = string.char(i)
-- Generate ith test mapping
rmaps[k] = {
function() print('You pressed ' .. k) end,
[[Action ']] .. k .. [[' (F5 to repeat)]]
}
-- Make ith mapping repeatable
local action = rmaps[k][1]
rmaps[k][1] = function()
r = function()
action()
return({ key = k })
end
action()
end
end
-- map dummy actions a,b,...,k to
-- long key sequences beginning
-- with <leader><leader><leader><leader> and
-- ending with a,b,...,k
--
-- long key sequences were chosen in order to
-- emphasize the concept
require('which-key').register( {
['<leader>'] = {
name = 'level 2',
['<leader>'] = {
name = 'level 3',
['<leader>'] = rmaps
}
}
}, {
buffer = 0,
prefix = '<leader>'
})
-- bind the repititon of the
-- last action to <F5>
require('which-key').register({
['<F5>'] = { function() print("Repeat: ", r().key) end, 'repeat last repeatable action'}
}, {
buffer = 0
})
#48 describes alternate solution to your problem. Also you might be interested in my (a bit dirty but functional) solution https://github.com/folke/which-key.nvim/issues/48#issuecomment-973155851.