From b8c8dc2484156d8f6f0cbb1dd1db0a1da3bd5503 Mon Sep 17 00:00:00 2001 From: Pei Yang Ching <59727193+horriblename@users.noreply.github.com> Date: Sun, 18 Aug 2024 14:16:44 +0200 Subject: [PATCH 1/6] maps: allow same key on multiple mode --- modules/neovim/mappings/config.nix | 70 +++++++++++++++++++---------- modules/neovim/mappings/options.nix | 36 ++++++++------- modules/wrapper/rc/config.nix | 45 +++++-------------- 3 files changed, 78 insertions(+), 73 deletions(-) diff --git a/modules/neovim/mappings/config.nix b/modules/neovim/mappings/config.nix index 365e124..b7e274b 100644 --- a/modules/neovim/mappings/config.nix +++ b/modules/neovim/mappings/config.nix @@ -3,32 +3,56 @@ lib, ... }: let - inherit (lib.modules) mkIf; + inherit (lib.modules) mkIf mkMerge; + inherit (builtins) mapAttrs; + + processLegacyMap = modes: legacyMap: [(legacyMap // {mode = modes;})]; cfg = config.vim; in { config = { - vim.maps = mkIf cfg.disableArrows { - "" = { - mode = ["n" "i"]; - action = ""; - noremap = false; - }; - "" = { - mode = ["n" "i"]; - action = ""; - noremap = false; - }; - "" = { - mode = ["n" "i"]; - action = ""; - noremap = false; - }; - "" = { - mode = ["n" "i"]; - action = ""; - noremap = false; - }; - }; + vim.keymaps = mkMerge [ + (mkIf cfg.disableArrows { + "" = [ + { + mode = ["n" "i"]; + action = ""; + noremap = false; + } + ]; + "" = [ + { + mode = ["n" "i"]; + action = ""; + noremap = false; + } + ]; + "" = [ + { + mode = ["n" "i"]; + action = ""; + noremap = false; + } + ]; + "" = [ + { + mode = ["n" "i"]; + action = ""; + 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) + ]; }; } diff --git a/modules/neovim/mappings/options.nix b/modules/neovim/mappings/options.nix index f422991..e16b787 100644 --- a/modules/neovim/mappings/options.nix +++ b/modules/neovim/mappings/options.nix @@ -38,6 +38,7 @@ See `:help map-modes` for a list of modes. ''; + example = ''"nvc" for normal, visual and command mode''; }; }; }; @@ -55,25 +56,10 @@ }; in { options.vim = { - maps = mkOption { + keymaps = mkOption { type = submodule { - freeformType = attrsOf 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"; - }; + freeformType = attrsOf (listOf mapType); }; - default = {}; description = "Custom keybindings."; example = '' maps = { @@ -84,6 +70,22 @@ in { }; # Same as nnoremap m make }; ''; + 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"; }; }; } diff --git a/modules/wrapper/rc/config.nix b/modules/wrapper/rc/config.nix index b3fd2e4..549c3fd 100644 --- a/modules/wrapper/rc/config.nix +++ b/modules/wrapper/rc/config.nix @@ -5,8 +5,8 @@ }: let inherit (builtins) map mapAttrs filter removeAttrs attrNames; inherit (lib.attrsets) mapAttrsToList filterAttrs attrsToList; - inherit (lib.strings) concatLines concatMapStringsSep; - inherit (lib.trivial) showWarnings; + inherit (lib.strings) concatLines concatMapStringsSep optionalString; + inherit (lib.trivial) showWarnings pipe; inherit (lib.generators) mkLuaInline; inherit (lib.nvim.dag) entryAfter mkLuarcSection resolveDag entryAnywhere; inherit (lib.nvim.lua) toLuaObject; @@ -40,40 +40,19 @@ 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)})"; - - 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"]; - }; + toLuaKeymap = key: bind: "vim.keymap.set(${toLuaObject bind.mode}, ${toLuaObject key}, ${toLuaObject (getAction bind)}, ${toLuaObject (getOpts bind)})"; 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; + pipe + # attrsOf (listOf mapOption) + cfg.keymaps + [ + (mapAttrsToList (key: binds: + concatLines (map (toLuaKeymap key) binds))) + concatLines + ]; - keymaps = concatLines (map toLuaKeymap (attrsToList (filterNonNull maps))); + keymaps = maps; in { vim = { luaConfigRC = { From fd4ddbdd39e34b0f07fc94768f66bacfbb56d050 Mon Sep 17 00:00:00 2001 From: Pei Yang Ching <59727193+horriblename@users.noreply.github.com> Date: Sun, 18 Aug 2024 15:04:32 +0200 Subject: [PATCH 2/6] maps: fix bad mode names --- modules/neovim/mappings/config.nix | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/modules/neovim/mappings/config.nix b/modules/neovim/mappings/config.nix index b7e274b..a620e37 100644 --- a/modules/neovim/mappings/config.nix +++ b/modules/neovim/mappings/config.nix @@ -42,17 +42,17 @@ in { } ]; }) - (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) + (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) ]; }; } From 34b462a744fe4705bde099948c9b27189792944e Mon Sep 17 00:00:00 2001 From: Pei Yang Ching <59727193+horriblename@users.noreply.github.com> Date: Fri, 23 Aug 2024 14:16:07 +0200 Subject: [PATCH 3/6] keymaps: use listOf mapOption instead --- modules/neovim/mappings/config.nix | 65 +++++++++++++++++++---------- modules/neovim/mappings/options.nix | 14 ++++--- modules/wrapper/rc/config.nix | 7 ++-- 3 files changed, 54 insertions(+), 32 deletions(-) diff --git a/modules/neovim/mappings/config.nix b/modules/neovim/mappings/config.nix index a620e37..4d7f241 100644 --- a/modules/neovim/mappings/config.nix +++ b/modules/neovim/mappings/config.nix @@ -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 { - "" = [ + ( + mkIf cfg.disableArrows [ { + key = ""; mode = ["n" "i"]; action = ""; noremap = false; } - ]; - "" = [ { + key = ""; mode = ["n" "i"]; action = ""; noremap = false; } - ]; - "" = [ { + key = ""; mode = ["n" "i"]; action = ""; noremap = false; } - ]; - "" = [ { + key = ""; mode = ["n" "i"]; action = ""; 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 + ] + ) ]; }; } diff --git a/modules/neovim/mappings/options.nix b/modules/neovim/mappings/options.nix index e16b787..5538d98 100644 --- a/modules/neovim/mappings/options.nix +++ b/modules/neovim/mappings/options.nix @@ -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,22 +50,20 @@ }; # 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 = { diff --git a/modules/wrapper/rc/config.nix b/modules/wrapper/rc/config.nix index 549c3fd..41db014 100644 --- a/modules/wrapper/rc/config.nix +++ b/modules/wrapper/rc/config.nix @@ -40,15 +40,14 @@ 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) + # listOf mapOption cfg.keymaps [ - (mapAttrsToList (key: binds: - concatLines (map (toLuaKeymap key) binds))) + (map toLuaKeymap) concatLines ]; From 71727e5378e4e01ae80d2dbc976ef51deb4355d8 Mon Sep 17 00:00:00 2001 From: Pei Yang Ching <59727193+horriblename@users.noreply.github.com> Date: Fri, 23 Aug 2024 14:16:07 +0200 Subject: [PATCH 4/6] keymaps: update example --- modules/neovim/mappings/options.nix | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/modules/neovim/mappings/options.nix b/modules/neovim/mappings/options.nix index 5538d98..49e7249 100644 --- a/modules/neovim/mappings/options.nix +++ b/modules/neovim/mappings/options.nix @@ -66,13 +66,20 @@ in { type = listOf mapType; description = "Custom keybindings."; example = '' - maps = { - "m" = { + vim.keymaps = [ + { + key = "m"; mode = "n"; silent = true; - action = "make"; - }; # Same as nnoremap m make - }; + action = ":make"; + } + { + key = "l"; + mode = ["n" "x"]; + silent = true; + action = "cnext"; + } + ]; ''; default = {}; }; From aa040ab53865e2eb2f39cbc3b0da8d822c0c2776 Mon Sep 17 00:00:00 2001 From: Pei Yang Ching <59727193+horriblename@users.noreply.github.com> Date: Fri, 23 Aug 2024 14:16:07 +0200 Subject: [PATCH 5/6] remove unneeded pipe --- modules/wrapper/rc/config.nix | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/modules/wrapper/rc/config.nix b/modules/wrapper/rc/config.nix index 41db014..586243a 100644 --- a/modules/wrapper/rc/config.nix +++ b/modules/wrapper/rc/config.nix @@ -6,7 +6,7 @@ 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 (lib.trivial) showWarnings; inherit (lib.generators) mkLuaInline; inherit (lib.nvim.dag) entryAfter mkLuarcSection resolveDag entryAnywhere; inherit (lib.nvim.lua) toLuaObject; @@ -42,14 +42,7 @@ in { toLuaKeymap = bind: "vim.keymap.set(${toLuaObject bind.mode}, ${toLuaObject bind.key}, ${toLuaObject (getAction bind)}, ${toLuaObject (getOpts bind)})"; - maps = - pipe - # listOf mapOption - cfg.keymaps - [ - (map toLuaKeymap) - concatLines - ]; + maps = concatLines (map toLuaKeymap cfg.keymaps); keymaps = maps; in { From feccaf477a302e5d154f9728eead642d9a8bc860 Mon Sep 17 00:00:00 2001 From: Pei Yang Ching <59727193+horriblename@users.noreply.github.com> Date: Fri, 23 Aug 2024 14:16:07 +0200 Subject: [PATCH 6/6] cleanup unused import --- modules/wrapper/rc/config.nix | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/wrapper/rc/config.nix b/modules/wrapper/rc/config.nix index 586243a..14ef8f0 100644 --- a/modules/wrapper/rc/config.nix +++ b/modules/wrapper/rc/config.nix @@ -3,9 +3,9 @@ lib, ... }: let - inherit (builtins) map mapAttrs filter removeAttrs attrNames; - inherit (lib.attrsets) mapAttrsToList filterAttrs attrsToList; - inherit (lib.strings) concatLines concatMapStringsSep optionalString; + 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;