From 0bd07ee0914361bc4a729b92c0bfc4ecf97ce7e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=A9tan=20Lepage?= <33058747+GaetanLepage@users.noreply.github.com> Date: Fri, 10 Mar 2023 13:00:52 +0100 Subject: [PATCH] helpers: add mkMaps and mkModeMaps (#236) --- lib/helpers.nix | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/lib/helpers.nix b/lib/helpers.nix index 97596c27..16ecaea4 100644 --- a/lib/helpers.nix +++ b/lib/helpers.nix @@ -75,6 +75,54 @@ with lib; rec { }) normalized); + # Given an attrs of key mappings (for a single mode), applies the defaults to each one of them. + # + # Example: + # mkModeMaps { silent = true; } { + # Y = "y$"; + # "" = { action = ":b#"; silent = false; }; + # }; + # + # would give: + # { + # Y = { + # action = "y$"; + # silent = true; + # }; + # "" = { + # action = ":b#"; + # silent = false; + # }; + # }; + mkModeMaps = defaults: modeMaps: + mapAttrs + ( + shortcut: action: let + actionAttrs = + if isString action + then {inherit action;} + else value; + in + defaults // actionAttrs + ) + modeMaps; + + # Applies some default mapping options to a set of mappings + # + # Example: + # maps = mkMaps { silent = true; expr = true; } { + # normal = { + # ... + # }; + # visual = { + # ... + # }; + # } + mkMaps = defaults: maps: + mapAttrs + (name: modeMaps: (mkModeMaps defaults modeMaps)) + maps; + # Creates an option with a nullable type that defaults to null. mkNullOrOption = type: desc: lib.mkOption {