From 4cc9de194fdd5cd7b2a0dda6db3af3d0123f58f9 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Tue, 30 Apr 2024 16:54:48 +0300 Subject: [PATCH] modules: add fn module to define functions --- lib/types.nix | 2 +- module-list.nix | 1 + modules/fn.nix | 59 ++++++++++++++++++++++++++++++++++++++++++ modules/vim/keymap.nix | 2 +- 4 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 modules/fn.nix diff --git a/lib/types.nix b/lib/types.nix index 5b4ae7f..c4e941b 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -43,7 +43,7 @@ let ''; }; buffer = mkOption { - type = with types; nullOr str; + type = with types; nullOr attrs; default = null; description = '' The mapping will be effective in the current buffer only. diff --git a/module-list.nix b/module-list.nix index 57664b6..f797d67 100644 --- a/module-list.nix +++ b/module-list.nix @@ -9,6 +9,7 @@ ./modules/filetype.nix ./modules/input.nix ./modules/plugin.nix + ./modules/fn.nix ./modules/plugins/theme/catppuccin.nix ./modules/plugins/style/neoformat.nix diff --git a/modules/fn.nix b/modules/fn.nix new file mode 100644 index 0000000..3c21f4d --- /dev/null +++ b/modules/fn.nix @@ -0,0 +1,59 @@ +{ lib, ... }: + +let + + fnOpts = { name, config, ... }: { + options = with lib; { + args = mkOption { + type = with types; listOf str; + default = [ ]; + }; + content = mkOption { + type = with types; nullOr attrs; + default = null; + }; + settings = mkOption { + type = types.functionTo (lib.types.submoduleWith { + modules = [ + ./vim/keymap.nix + # ./vim/opts.nix + ]; + specialArgs = { inherit lib; }; + }); + default = { }; + }; + + lambda = mkOption { + type = types.attrs; + readOnly = true; + internal = true; + }; + }; + + config = { + lambda = with lib.nix2lua; + let + fnArgs = builtins.listToAttrs (map (v: { name = v; value = raw v; }) config.args); + innerCfg = config.settings fnArgs; + in + lambda config.args (lib.flatten [ + config.content + + (lib.flip map innerCfg.vim.keymap.set ({ mode, lhs, rhs, ... } @ vars: + let + buf = lib.optionalString (vars.buffer != null) vars.buffer.raw; + m = if builtins.isList mode then lib.head mode else mode; + in + if innerCfg.vim.keymap._validate."${buf}"."${m}"."${lhs}" == rhs then vars.genConfig + else abort "This case should never happen." + )) + ]); + }; + }; +in +{ + options.fn = with lib; mkOption { + type = with types; attrsOf (submodule fnOpts); + default = { }; + }; +} diff --git a/modules/vim/keymap.nix b/modules/vim/keymap.nix index 01cc8eb..ce13076 100644 --- a/modules/vim/keymap.nix +++ b/modules/vim/keymap.nix @@ -30,7 +30,7 @@ let cfg = config.vim.keymap; in else m ))); - buf = lib.optionalString (buffer != null) buffer; + buf = lib.optionalString (buffer != null) buffer.raw; in lib.foldl lib.recursiveUpdate { } (lib.flip map unwrappedModes (m: {