This commit is contained in:
Ching Pei Yang 2024-08-23 12:34:42 +00:00 committed by GitHub
commit 47a1dd7f62
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 110 additions and 83 deletions

View file

@ -3,32 +3,75 @@
lib,
...
}: let
inherit (lib.modules) mkIf;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.trivial) pipe;
inherit (lib.attrsets) mapAttrsToList;
inherit (lib.lists) flatten;
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.maps = mkIf cfg.disableArrows {
"<up>" = {
vim.keymaps = mkMerge [
(
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;
};
};
}
]
)
(
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 = ''
@ -38,27 +44,47 @@
See `:help map-modes` for a list of modes.
'';
example = ''"nvc" for normal, visual and command mode'';
};
};
};
# 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 = {
maps = mkOption {
type = submodule {
freeformType = attrsOf mapType;
options = {
keymaps = mkOption {
type = listOf mapType;
description = "Custom keybindings.";
example = ''
vim.keymaps = [
{
key = "<leader>m";
mode = "n";
silent = true;
action = ":make<CR>";
}
{
key = "<leader>l";
mode = ["n" "x"];
silent = true;
action = "<cmd>cnext<CR>";
}
];
'';
default = {};
};
maps = {
normal = mapOptions "normal";
insert = mapOptions "insert";
select = mapOptions "select";
@ -73,17 +99,4 @@ in {
command = mapOptions "command-line";
};
};
default = {};
description = "Custom keybindings.";
example = ''
maps = {
"<leader>m" = {
mode = "n";
silent = true;
action = "<cmd>make<CR>";
}; # Same as nnoremap <leader>m <silent> <cmd>make<CR>
};
'';
};
};
}

View file

@ -3,8 +3,8 @@
lib,
...
}: let
inherit (builtins) map mapAttrs filter removeAttrs attrNames;
inherit (lib.attrsets) mapAttrsToList filterAttrs attrsToList;
inherit (builtins) map mapAttrs filter;
inherit (lib.attrsets) mapAttrsToList filterAttrs;
inherit (lib.strings) concatLines concatMapStringsSep;
inherit (lib.trivial) showWarnings;
inherit (lib.generators) mkLuaInline;
@ -40,40 +40,11 @@ in {
inherit (keymap) desc silent nowait script expr unique noremap;
};
toLuaKeymap = {
name,
value,
}: "vim.keymap.set(${toLuaObject value.mode}, ${toLuaObject name}, ${toLuaObject (getAction value)}, ${toLuaObject (getOpts value)})";
toLuaKeymap = bind: "vim.keymap.set(${toLuaObject bind.mode}, ${toLuaObject bind.key}, ${toLuaObject (getAction bind)}, ${toLuaObject (getOpts bind)})";
namedModes = {
"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"];
};
maps = concatLines (map toLuaKeymap cfg.keymaps);
maps =
removeAttrs cfg.maps (attrNames namedModes)
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.normal;}) cfg.maps.normal
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.insert;}) cfg.maps.insert
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.select;}) cfg.maps.select
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.visual;}) cfg.maps.visual
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.terminal;}) cfg.maps.terminal
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.normalVisualOp;}) cfg.maps.normalVisualOp
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.visualOnly;}) cfg.maps.visualOnly
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.operator;}) cfg.maps.operator
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.insertCommand;}) cfg.maps.insertCommand
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.lang;}) cfg.maps.lang
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.command;}) cfg.maps.command;
keymaps = concatLines (map toLuaKeymap (attrsToList (filterNonNull maps)));
keymaps = maps;
in {
vim = {
luaConfigRC = {