diff --git a/lib/binds.nix b/lib/binds.nix new file mode 100644 index 0000000..c52e255 --- /dev/null +++ b/lib/binds.nix @@ -0,0 +1,70 @@ +{lib}: let + inherit (lib.options) mkOption; + inherit (lib.modules) mkIf; + inherit (lib.types) nullOr str; + inherit (lib.attrsets) isAttrs mapAttrs; + + binds = rec { + mkLuaBinding = key: action: desc: + mkIf (key != null) { + "${key}" = { + inherit action desc; + lua = true; + silent = true; + }; + }; + + mkExprBinding = key: action: desc: + mkIf (key != null) { + "${key}" = { + inherit action desc; + lua = true; + silent = true; + expr = true; + }; + }; + + mkBinding = key: action: desc: + mkIf (key != null) { + "${key}" = { + inherit action desc; + silent = true; + }; + }; + + mkMappingOption = description: default: + mkOption { + type = nullOr str; + inherit default description; + }; + + # Utility function that takes two attrsets: + # { someKey = "some_value" } and + # { someKey = { description = "Some Description"; }; } + # and merges them into + # { someKey = { value = "some_value"; description = "Some Description"; }; } + addDescriptionsToMappings = actualMappings: mappingDefinitions: + mapAttrs (name: value: let + isNested = isAttrs value; + returnedValue = + if isNested + then addDescriptionsToMappings actualMappings."${name}" mappingDefinitions."${name}" + else { + inherit value; + inherit (mappingDefinitions."${name}") description; + }; + in + returnedValue) + actualMappings; + + mkSetBinding = binding: action: + mkBinding binding.value action binding.description; + + mkSetExprBinding = binding: action: + mkExprBinding binding.value action binding.description; + + mkSetLuaBinding = binding: action: + mkLuaBinding binding.value action binding.description; + }; +in + binds diff --git a/lib/default.nix b/lib/default.nix index d82ea5f..0e0b73f 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -1,6 +1,8 @@ {lib}: { - dag = import ./dag.nix {inherit lib;}; types = import ./types {inherit lib;}; + + binds = import ./binds.nix {inherit lib;}; + dag = import ./dag.nix {inherit lib;}; languages = import ./languages.nix {inherit lib;}; lua = import ./lua.nix {inherit lib;}; vim = import ./vim.nix {inherit lib;}; diff --git a/lib/vim.nix b/lib/vim.nix index b00d7ca..c81dcc3 100644 --- a/lib/vim.nix +++ b/lib/vim.nix @@ -1,4 +1,4 @@ -{lib}: let +let inherit (builtins) isInt isBool toJSON; in rec { # yes? no.