mirror of
https://github.com/nix-community/nixvim.git
synced 2025-06-21 08:35:43 +02:00
Keymaps: Add keymapsOnEvent to load keymap on specified events (#1260)
This commit is contained in:
parent
f876a0a2e9
commit
303b9ca2c0
2 changed files with 97 additions and 19 deletions
|
@ -19,10 +19,32 @@ with lib; {
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
keymapsOnEvents = mkOption {
|
||||||
|
type = types.attrsOf (types.listOf helpers.keymaps.mapOptionSubmodule);
|
||||||
|
default = {};
|
||||||
|
example = {
|
||||||
|
"InsertEnter" = [
|
||||||
|
{
|
||||||
|
key = "<C-y>";
|
||||||
|
action = ''require("cmp").mapping.confirm()'';
|
||||||
|
lua = true;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
key = "<C-n>";
|
||||||
|
action = ''require("cmp").mapping.select_next_item()'';
|
||||||
|
lua = true;
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
description = ''
|
||||||
|
Register keymaps on an event instead of when nvim opens.
|
||||||
|
Keys are the events to register on, and values are lists of keymaps to register on each event.
|
||||||
|
'';
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
config = {
|
config = let
|
||||||
extraConfigLua = let
|
|
||||||
normalizeMapping = keyMapping: {
|
normalizeMapping = keyMapping: {
|
||||||
inherit
|
inherit
|
||||||
(keyMapping)
|
(keyMapping)
|
||||||
|
@ -36,19 +58,41 @@ with lib; {
|
||||||
then helpers.mkRaw keyMapping.action
|
then helpers.mkRaw keyMapping.action
|
||||||
else keyMapping.action;
|
else keyMapping.action;
|
||||||
};
|
};
|
||||||
|
in {
|
||||||
mappings = map normalizeMapping config.keymaps;
|
extraConfigLua =
|
||||||
in
|
optionalString (config.keymaps != [])
|
||||||
optionalString (mappings != [])
|
|
||||||
''
|
''
|
||||||
-- Set up keybinds {{{
|
-- Set up keybinds {{{
|
||||||
do
|
do
|
||||||
local __nixvim_binds = ${helpers.toLuaObject mappings}
|
local __nixvim_binds = ${helpers.toLuaObject (map normalizeMapping config.keymaps)}
|
||||||
for i, map in ipairs(__nixvim_binds) do
|
for i, map in ipairs(__nixvim_binds) do
|
||||||
vim.keymap.set(map.mode, map.key, map.action, map.options)
|
vim.keymap.set(map.mode, map.key, map.action, map.options)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- }}}
|
-- }}}
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
autoGroups = mapAttrs' (event: mappings: nameValuePair "nixvim_binds_${event}" {clear = true;}) config.keymapsOnEvents;
|
||||||
|
|
||||||
|
autoCmd =
|
||||||
|
mapAttrsToList
|
||||||
|
(
|
||||||
|
event: mappings: {
|
||||||
|
inherit event;
|
||||||
|
group = "nixvim_binds_${event}";
|
||||||
|
callback = helpers.mkRaw ''
|
||||||
|
function()
|
||||||
|
do
|
||||||
|
local __nixvim_binds = ${helpers.toLuaObject (map normalizeMapping mappings)}
|
||||||
|
for i, map in ipairs(__nixvim_binds) do
|
||||||
|
vim.keymap.set(map.mode, map.key, map.action, map.options)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
'';
|
||||||
|
desc = "Load keymaps for ${event}";
|
||||||
|
}
|
||||||
|
)
|
||||||
|
config.keymapsOnEvents;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,4 +44,38 @@
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
mkKeymapsOnEvents = {
|
||||||
|
keymapsOnEvents = {
|
||||||
|
"InsertEnter" =
|
||||||
|
helpers.keymaps.mkKeymaps
|
||||||
|
{
|
||||||
|
mode = "x";
|
||||||
|
options.silent = true;
|
||||||
|
}
|
||||||
|
[
|
||||||
|
{
|
||||||
|
mode = "n";
|
||||||
|
key = ",";
|
||||||
|
action = "<cmd>echo \"test\"<cr>";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
# raw action using rawType
|
||||||
|
key = "<C-p>";
|
||||||
|
action.__raw = "function() print('hello') end";
|
||||||
|
}
|
||||||
|
{
|
||||||
|
key = "<C-a>";
|
||||||
|
action = "function() print('toto') end";
|
||||||
|
lua = true;
|
||||||
|
options.silent = false;
|
||||||
|
}
|
||||||
|
{
|
||||||
|
mode = ["n" "v"];
|
||||||
|
key = "<C-z>";
|
||||||
|
action = "bar";
|
||||||
|
}
|
||||||
|
];
|
||||||
|
};
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue