Skip to content

Commit

Permalink
Allow for setting keybinds to an array of strings (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvWilson authored Jan 4, 2025
1 parent 3d12982 commit 7da61f8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Here's the default mapping object for reference:
```

Want to not set a keybind for a given action? Set the bound value to `'NONE'` and it will skip being set.
Keybinds can be set to either individual strings or arrays of strings, so for example `window_mappings.cursor_down` above could be mapped to `{ 'j', '<leader>s' }`.

Check the mentioned help screen to see current keybinds and their use:

Expand Down
44 changes: 35 additions & 9 deletions lua/spelunk/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,53 @@ M.get_default = function(key)
return default_config[key]
end

---@param key string
---@param key string | string[]
---@param cmd string | function
---@param description string
M.set_keymap = function(key, cmd, description)
if key == skipkey then
return
---@param val string
local apply = function(val)
if val == skipkey then
return
end
vim.keymap.set('n', val, cmd,
{ desc = description, noremap = true, silent = true })
end
if type(key) == 'string' then
apply(key)
elseif type(key) == 'table' then
for _, k in pairs(key) do
apply(k)
end
else
error('[spelunk.nvim] config.set_keymap passed unsupported type: ' .. type(key))
end
vim.keymap.set('n', key, cmd,
{ desc = description, noremap = true, silent = true })
end

---@param bufnr integer
M.set_buf_keymap = function(bufnr)
---@param key string
---@param val string
---@param f string
---@param desc string
local apply = function(val, f, desc)
if val == skipkey then
return
end
vim.api.nvim_buf_set_keymap(bufnr, 'n', val, f, { noremap = true, silent = true, desc = desc })
end
---@param key string | string[]
---@param func string
---@param description string
return function(key, func, description)
if key == skipkey then
return
if type(key) == 'string' then
apply(key, func, description)
elseif type(key) == 'table' then
for _, k in pairs(key) do
apply(k, func, description)
end
else
error('[spelunk.nvim] config.set_buf_keymap passed unsupported type: ' .. type(key))
end
vim.api.nvim_buf_set_keymap(bufnr, 'n', key, func, { noremap = true, silent = true, desc = description })
end
end

Expand Down

0 comments on commit 7da61f8

Please sign in to comment.