2023-02-20 11:42:13 +01:00
|
|
|
{
|
|
|
|
lib,
|
2023-11-06 15:04:08 +01:00
|
|
|
helpers,
|
|
|
|
config,
|
2023-02-20 11:42:13 +01:00
|
|
|
...
|
|
|
|
}:
|
2024-05-05 19:39:35 +02:00
|
|
|
with lib;
|
|
|
|
{
|
2023-12-02 20:02:53 +01:00
|
|
|
options = {
|
|
|
|
keymaps = mkOption {
|
2024-05-05 19:39:35 +02:00
|
|
|
type = types.listOf helpers.keymaps.mapOptionSubmodule;
|
|
|
|
default = [ ];
|
2023-12-02 20:02:53 +01:00
|
|
|
example = [
|
|
|
|
{
|
|
|
|
key = "<C-m>";
|
|
|
|
action = "<cmd>make<CR>";
|
|
|
|
options.silent = true;
|
|
|
|
}
|
|
|
|
];
|
|
|
|
};
|
2023-09-10 09:59:22 +02:00
|
|
|
|
2024-03-18 19:41:12 +00:00
|
|
|
keymapsOnEvents = mkOption {
|
|
|
|
type = types.attrsOf (types.listOf helpers.keymaps.mapOptionSubmodule);
|
2024-05-05 19:39:35 +02:00
|
|
|
default = { };
|
2024-03-18 19:41:12 +00:00
|
|
|
example = {
|
|
|
|
"InsertEnter" = [
|
|
|
|
{
|
|
|
|
key = "<C-y>";
|
|
|
|
action = ''require("cmp").mapping.confirm()'';
|
|
|
|
lua = true;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
key = "<C-n>";
|
|
|
|
action = ''require("cmp").mapping.select_next_item()'';
|
|
|
|
lua = true;
|
|
|
|
}
|
|
|
|
];
|
2023-12-02 20:02:53 +01:00
|
|
|
};
|
2024-03-18 19:41:12 +00:00
|
|
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
2023-09-10 09:59:22 +02:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
config =
|
|
|
|
let
|
|
|
|
normalizeMapping = keyMapping: {
|
|
|
|
inherit (keyMapping) mode key options;
|
2024-03-18 19:41:12 +00:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
action = if keyMapping.lua then helpers.mkRaw keyMapping.action else keyMapping.action;
|
|
|
|
};
|
|
|
|
in
|
|
|
|
{
|
|
|
|
extraConfigLua = optionalString (config.keymaps != [ ]) ''
|
2023-09-26 22:41:21 +02:00
|
|
|
-- Set up keybinds {{{
|
|
|
|
do
|
2024-03-18 19:41:12 +00:00
|
|
|
local __nixvim_binds = ${helpers.toLuaObject (map normalizeMapping config.keymaps)}
|
2023-09-26 22:41:21 +02:00
|
|
|
for i, map in ipairs(__nixvim_binds) do
|
|
|
|
vim.keymap.set(map.mode, map.key, map.action, map.options)
|
2023-05-20 16:26:36 +05:30
|
|
|
end
|
2023-09-26 22:41:21 +02:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
'';
|
2024-03-18 19:41:12 +00:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
autoGroups = mapAttrs' (
|
|
|
|
event: mappings: nameValuePair "nixvim_binds_${event}" { clear = true; }
|
|
|
|
) config.keymapsOnEvents;
|
2024-03-18 19:41:12 +00:00
|
|
|
|
2024-05-05 19:39:35 +02:00
|
|
|
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)
|
2024-03-18 19:41:12 +00:00
|
|
|
end
|
|
|
|
end
|
2024-05-05 19:39:35 +02:00
|
|
|
end
|
|
|
|
'';
|
|
|
|
desc = "Load keymaps for ${event}";
|
|
|
|
}) config.keymapsOnEvents;
|
|
|
|
};
|
2022-09-18 11:19:23 +01:00
|
|
|
}
|