maps: allow same key on multiple mode

This commit is contained in:
Pei Yang Ching 2024-08-18 14:16:44 +02:00
parent e40cce5653
commit b8c8dc2484
3 changed files with 78 additions and 73 deletions

View file

@ -3,32 +3,56 @@
lib, lib,
... ...
}: let }: let
inherit (lib.modules) mkIf; inherit (lib.modules) mkIf mkMerge;
inherit (builtins) mapAttrs;
processLegacyMap = modes: legacyMap: [(legacyMap // {mode = modes;})];
cfg = config.vim; cfg = config.vim;
in { in {
config = { config = {
vim.maps = mkIf cfg.disableArrows { vim.keymaps = mkMerge [
"<up>" = { (mkIf cfg.disableArrows {
mode = ["n" "i"]; "<up>" = [
action = "<nop>"; {
noremap = false; mode = ["n" "i"];
}; action = "<nop>";
"<down>" = { noremap = false;
mode = ["n" "i"]; }
action = "<nop>"; ];
noremap = false; "<down>" = [
}; {
"<left>" = { mode = ["n" "i"];
mode = ["n" "i"]; action = "<nop>";
action = "<nop>"; noremap = false;
noremap = false; }
}; ];
"<right>" = { "<left>" = [
mode = ["n" "i"]; {
action = "<nop>"; mode = ["n" "i"];
noremap = false; action = "<nop>";
}; noremap = false;
}; }
];
"<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 "nvo") cfg.maps.normalVisualOp)
(mapAttrs (_key: processLegacyMap "nx") cfg.maps.visualOnly)
(mapAttrs (_key: processLegacyMap "o") cfg.maps.operator)
(mapAttrs (_key: processLegacyMap "ic") cfg.maps.insertCommand)
(mapAttrs (_key: processLegacyMap "l") cfg.maps.lang)
(mapAttrs (_key: processLegacyMap "c") cfg.maps.command)
];
}; };
} }

View file

@ -38,6 +38,7 @@
See `:help map-modes` for a list of modes. See `:help map-modes` for a list of modes.
''; '';
example = ''"nvc" for normal, visual and command mode'';
}; };
}; };
}; };
@ -55,25 +56,10 @@
}; };
in { in {
options.vim = { options.vim = {
maps = mkOption { keymaps = mkOption {
type = submodule { type = submodule {
freeformType = attrsOf mapType; freeformType = attrsOf (listOf mapType);
options = {
normal = mapOptions "normal";
insert = mapOptions "insert";
select = mapOptions "select";
visual = mapOptions "visual and select";
terminal = mapOptions "terminal";
normalVisualOp = mapOptions "normal, visual, select and operator-pending (same as plain 'map')";
visualOnly = mapOptions "visual only";
operator = mapOptions "operator-pending";
insertCommand = mapOptions "insert and command-line";
lang = mapOptions "insert, command-line and lang-arg";
command = mapOptions "command-line";
};
}; };
default = {};
description = "Custom keybindings."; description = "Custom keybindings.";
example = '' example = ''
maps = { maps = {
@ -84,6 +70,22 @@ in {
}; # Same as nnoremap <leader>m <silent> <cmd>make<CR> }; # Same as nnoremap <leader>m <silent> <cmd>make<CR>
}; };
''; '';
default = {};
};
maps = {
normal = mapOptions "normal";
insert = mapOptions "insert";
select = mapOptions "select";
visual = mapOptions "visual and select";
terminal = mapOptions "terminal";
normalVisualOp = mapOptions "normal, visual, select and operator-pending (same as plain 'map')";
visualOnly = mapOptions "visual only";
operator = mapOptions "operator-pending";
insertCommand = mapOptions "insert and command-line";
lang = mapOptions "insert, command-line and lang-arg";
command = mapOptions "command-line";
}; };
}; };
} }

View file

@ -5,8 +5,8 @@
}: let }: let
inherit (builtins) map mapAttrs filter removeAttrs attrNames; inherit (builtins) map mapAttrs filter removeAttrs attrNames;
inherit (lib.attrsets) mapAttrsToList filterAttrs attrsToList; inherit (lib.attrsets) mapAttrsToList filterAttrs attrsToList;
inherit (lib.strings) concatLines concatMapStringsSep; inherit (lib.strings) concatLines concatMapStringsSep optionalString;
inherit (lib.trivial) showWarnings; inherit (lib.trivial) showWarnings pipe;
inherit (lib.generators) mkLuaInline; inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.dag) entryAfter mkLuarcSection resolveDag entryAnywhere; inherit (lib.nvim.dag) entryAfter mkLuarcSection resolveDag entryAnywhere;
inherit (lib.nvim.lua) toLuaObject; inherit (lib.nvim.lua) toLuaObject;
@ -40,40 +40,19 @@ in {
inherit (keymap) desc silent nowait script expr unique noremap; inherit (keymap) desc silent nowait script expr unique noremap;
}; };
toLuaKeymap = { toLuaKeymap = key: bind: "vim.keymap.set(${toLuaObject bind.mode}, ${toLuaObject key}, ${toLuaObject (getAction bind)}, ${toLuaObject (getOpts bind)})";
name,
value,
}: "vim.keymap.set(${toLuaObject value.mode}, ${toLuaObject name}, ${toLuaObject (getAction value)}, ${toLuaObject (getOpts value)})";
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 = maps =
removeAttrs cfg.maps (attrNames namedModes) pipe
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.normal;}) cfg.maps.normal # attrsOf (listOf mapOption)
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.insert;}) cfg.maps.insert cfg.keymaps
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.select;}) cfg.maps.select [
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.visual;}) cfg.maps.visual (mapAttrsToList (key: binds:
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.terminal;}) cfg.maps.terminal concatLines (map (toLuaKeymap key) binds)))
// mapAttrs (_: legacyMap: legacyMap // {mode = namedModes.normalVisualOp;}) cfg.maps.normalVisualOp concatLines
// 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 { in {
vim = { vim = {
luaConfigRC = { luaConfigRC = {