summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/keymappings.lua
diff options
context:
space:
mode:
Diffstat (limited to '.config/nvim/lua/keymappings.lua')
-rw-r--r--.config/nvim/lua/keymappings.lua29
1 files changed, 27 insertions, 2 deletions
diff --git a/.config/nvim/lua/keymappings.lua b/.config/nvim/lua/keymappings.lua
index 5d71b97..74f4233 100644
--- a/.config/nvim/lua/keymappings.lua
+++ b/.config/nvim/lua/keymappings.lua
@@ -24,13 +24,34 @@ local mode_adapters = {
-- Append key mappings to lunarvim's defaults for a given mode
-- @param keymaps The table of key mappings containing a list per mode (normal_mode, insert_mode, ..)
function M.append_to_defaults(keymaps)
+ local default = M.get_defaults()
+ options.keys = options.keys or default
for mode, mappings in pairs(keymaps) do
- for k, v in ipairs(mappings) do
+ options.keys[mode] = options.keys[mode] or default[mode]
+ for k, v in pairs(mappings) do
options.keys[mode][k] = v
end
end
end
+-- Unsets all keybindings defined in keymaps
+-- @param keymaps The table of key mappings containing a list per mode (normal_mode, insert_mode, ..)
+function M.clear(keymaps)
+ local default = M.get_defaults()
+ for mode, mappings in pairs(keymaps) do
+ local translated_mode = mode_adapters[mode] and mode_adapters[mode] or mode
+ for key, _ in pairs(mappings) do
+ -- some plugins may override default bindings that the user hasn't manually overriden
+ if
+ default[mode][key] ~= nil
+ or (default[translated_mode] ~= nil and default[translated_mode][key] ~= nil)
+ then
+ pcall(vim.api.nvim_del_keymap, translated_mode, key)
+ end
+ end
+ end
+end
+
-- Set key mappings individually
-- @param mode The keymap mode, can be one of the keys of mode_adapters
-- @param key The key of keymap
@@ -41,7 +62,11 @@ function M.set_keymaps(mode, key, val)
opt = val[2]
val = val[1]
end
- vim.api.nvim_set_keymap(mode, key, val, opt)
+ if val then
+ vim.api.nvim_set_keymap(mode, key, val, opt)
+ else
+ pcall(vim.api.nvim_del_keymap, mode, key)
+ end
end
-- Load key mappings for a given mode