mirror of
https://github.com/nix-community/nixvim.git
synced 2025-06-21 00:25:42 +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,36 +19,80 @@ 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 = {
|
||||
extraConfigLua = let
|
||||
normalizeMapping = keyMapping: {
|
||||
inherit
|
||||
(keyMapping)
|
||||
mode
|
||||
key
|
||||
options
|
||||
;
|
||||
config = let
|
||||
normalizeMapping = keyMapping: {
|
||||
inherit
|
||||
(keyMapping)
|
||||
mode
|
||||
key
|
||||
options
|
||||
;
|
||||
|
||||
action =
|
||||
if keyMapping.lua
|
||||
then helpers.mkRaw keyMapping.action
|
||||
else keyMapping.action;
|
||||
};
|
||||
|
||||
mappings = map normalizeMapping config.keymaps;
|
||||
in
|
||||
optionalString (mappings != [])
|
||||
action =
|
||||
if keyMapping.lua
|
||||
then helpers.mkRaw keyMapping.action
|
||||
else keyMapping.action;
|
||||
};
|
||||
in {
|
||||
extraConfigLua =
|
||||
optionalString (config.keymaps != [])
|
||||
''
|
||||
-- Set up keybinds {{{
|
||||
do
|
||||
local __nixvim_binds = ${helpers.toLuaObject mappings}
|
||||
local __nixvim_binds = ${helpers.toLuaObject (map normalizeMapping config.keymaps)}
|
||||
for i, map in ipairs(__nixvim_binds) do
|
||||
vim.keymap.set(map.mode, map.key, map.action, map.options)
|
||||
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