Compare commits

..

4 commits

Author SHA1 Message Date
Pei Yang Ching
feccaf477a cleanup unused import 2024-08-23 14:33:32 +02:00
Pei Yang Ching
aa040ab538 remove unneeded pipe 2024-08-23 14:33:16 +02:00
Pei Yang Ching
71727e5378 keymaps: update example 2024-08-23 14:26:32 +02:00
Pei Yang Ching
34b462a744 keymaps: use listOf mapOption instead 2024-08-23 14:17:24 +02:00
3 changed files with 69 additions and 47 deletions

View file

@ -4,55 +4,74 @@
...
}: let
inherit (lib.modules) mkIf mkMerge;
inherit (builtins) mapAttrs;
inherit (lib.trivial) pipe;
inherit (lib.attrsets) mapAttrsToList;
inherit (lib.lists) flatten;
processLegacyMap = modes: legacyMap: [(legacyMap // {mode = modes;})];
legacyMapModes = {
normal = ["n"];
insert = ["i"];
select = ["s"];
visual = ["v"];
terminal = ["t"];
normalVisualOp = ["n" "v" "o"];
visualOnly = ["n" "x"];
operator = ["o"];
insertCommand = ["i" "c"];
lang = ["l"];
command = ["c"];
};
cfg = config.vim;
in {
config = {
vim.keymaps = mkMerge [
(mkIf cfg.disableArrows {
"<up>" = [
(
mkIf cfg.disableArrows [
{
key = "<up>";
mode = ["n" "i"];
action = "<nop>";
noremap = false;
}
];
"<down>" = [
{
key = "<down>";
mode = ["n" "i"];
action = "<nop>";
noremap = false;
}
];
"<left>" = [
{
key = "<left>";
mode = ["n" "i"];
action = "<nop>";
noremap = false;
}
];
"<right>" = [
{
key = "<right>";
mode = ["n" "i"];
action = "<nop>";
noremap = false;
}
];
})
(mapAttrs (_key: processLegacyMap ["n"]) cfg.maps.normal)
(mapAttrs (_key: processLegacyMap ["i"]) cfg.maps.insert)
(mapAttrs (_key: processLegacyMap ["s"]) cfg.maps.select)
(mapAttrs (_key: processLegacyMap ["v"]) cfg.maps.visual)
(mapAttrs (_key: processLegacyMap ["t"]) cfg.maps.terminal)
(mapAttrs (_key: processLegacyMap ["n" "v" "o"]) cfg.maps.normalVisualOp)
(mapAttrs (_key: processLegacyMap ["n" "x"]) cfg.maps.visualOnly)
(mapAttrs (_key: processLegacyMap ["o"]) cfg.maps.operator)
(mapAttrs (_key: processLegacyMap ["i" "c"]) cfg.maps.insertCommand)
(mapAttrs (_key: processLegacyMap ["l"]) cfg.maps.lang)
(mapAttrs (_key: processLegacyMap ["c"]) cfg.maps.command)
]
)
(
pipe cfg.maps
[
(mapAttrsToList (
oldMode: keybinds:
mapAttrsToList (
key: bind:
bind
// {
inherit key;
mode = legacyMapModes.${oldMode};
}
)
keybinds
))
flatten
]
)
];
};
}

View file

@ -31,6 +31,12 @@
options =
mapConfigOptions
// {
key = mkOption {
type = str;
description = ''
Key that triggers this keybind.
'';
};
mode = mkOption {
type = either str (listOf str);
description = ''
@ -44,31 +50,36 @@
};
# legacy stuff
mapOption = submodule {
legacyMapOption = submodule {
options = mapConfigOptions;
};
mapOptions = mode:
mkOption {
description = "Mappings for ${mode} mode";
type = attrsOf mapOption;
type = attrsOf legacyMapOption;
default = {};
};
in {
options.vim = {
keymaps = mkOption {
type = submodule {
freeformType = attrsOf (listOf mapType);
};
type = listOf mapType;
description = "Custom keybindings.";
example = ''
maps = {
"<leader>m" = {
vim.keymaps = [
{
key = "<leader>m";
mode = "n";
silent = true;
action = "<cmd>make<CR>";
}; # Same as nnoremap <leader>m <silent> <cmd>make<CR>
};
action = ":make<CR>";
}
{
key = "<leader>l";
mode = ["n" "x"];
silent = true;
action = "<cmd>cnext<CR>";
}
];
'';
default = {};
};

View file

@ -3,10 +3,10 @@
lib,
...
}: let
inherit (builtins) map mapAttrs filter removeAttrs attrNames;
inherit (lib.attrsets) mapAttrsToList filterAttrs attrsToList;
inherit (lib.strings) concatLines concatMapStringsSep optionalString;
inherit (lib.trivial) showWarnings pipe;
inherit (builtins) map mapAttrs filter;
inherit (lib.attrsets) mapAttrsToList filterAttrs;
inherit (lib.strings) concatLines concatMapStringsSep;
inherit (lib.trivial) showWarnings;
inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.dag) entryAfter mkLuarcSection resolveDag entryAnywhere;
inherit (lib.nvim.lua) toLuaObject;
@ -40,17 +40,9 @@ in {
inherit (keymap) desc silent nowait script expr unique noremap;
};
toLuaKeymap = key: bind: "vim.keymap.set(${toLuaObject bind.mode}, ${toLuaObject key}, ${toLuaObject (getAction bind)}, ${toLuaObject (getOpts bind)})";
toLuaKeymap = bind: "vim.keymap.set(${toLuaObject bind.mode}, ${toLuaObject bind.key}, ${toLuaObject (getAction bind)}, ${toLuaObject (getOpts bind)})";
maps =
pipe
# attrsOf (listOf mapOption)
cfg.keymaps
[
(mapAttrsToList (key: binds:
concatLines (map (toLuaKeymap key) binds)))
concatLines
];
maps = concatLines (map toLuaKeymap cfg.keymaps);
keymaps = maps;
in {