Add support for mappings

This commit is contained in:
Pedro Alves 2021-01-05 16:11:56 +00:00
parent 7122ccd208
commit 0586bed5ad
7 changed files with 142 additions and 14 deletions

2
.gitignore vendored
View file

@ -1 +1 @@
.tmp/ .tmp

1
.tmp
View file

@ -1 +0,0 @@
/nix/store/r2p2qi63zznjdf0gjif7v8qmsm5g72d7-nixos-system-nixos-21.03.20201227.2f47650

View file

@ -10,7 +10,7 @@
# Equivalent to nnoremap ; : # Equivalent to nnoremap ; :
";" = ":"; ";" = ":";
# Equivalent to nmap <silent> <buffer> <leader>gg <cmd>Man<CR> # Equivalent to nmap <silent> <buffer> <leader>gg <cmd>Man<CR>
"<leader>gg" = { silent = true; buffer = true; remap = false; action = "<cmd>Man<CR>" "<leader>gg" = { silent = true; remap = false; action = "<cmd>Man<CR>"
# Etc... # Etc...
}; };
@ -25,12 +25,12 @@
options = { options = {
tabstop = 4; tabstop = 4;
shiftwidth = 4; shiftwidth = 4;
noexpandtab = true; expandtab = false;
mouse = "a"; mouse = "a";
# etc... # etc...
} };
# Of course, we can still use comfy vimscript: # Of course, we can still use comfy vimscript:
extraConfigVim = builtins.readFile ./init.vim; extraConfigVim = builtins.readFile ./init.vim;

View file

@ -37,6 +37,8 @@
options.number = true; options.number = true;
maps.normalVisualOp."ç" = ":";
plugins.airline = { plugins.airline = {
enable = true; enable = true;
powerline = true; powerline = true;

View file

@ -22,6 +22,55 @@ let
}; };
}; };
mapOption = types.oneOf [ types.str (types.submodule {
silent = mkOption {
type = types.bool;
description = "Whether this mapping should be silent. Equivalent to adding <silent> to a map.";
default = false;
};
nowait = mkOption {
type = types.bool;
description = "Whether to wait for extra input on ambiguous mappings. Equivalent to adding <nowait> to a map.";
default = false;
};
script = mkOption {
type = types.bool;
description = "Equivalent to adding <script> to a map.";
default = false;
};
expr = mkOption {
type = types.bool;
description = "Means that the action is actually an expression. Equivalent to adding <expr> to a map.";
default = false;
};
unique = mkOption {
type = types.bool;
description = "Whether to fail if the map is already defined. Equivalent to adding <unique> to a map.";
default = false;
};
noremap = mkOption {
type = types.bool;
description = "Whether to use the 'noremap' variant of the command, ignoring any custom mappings on the defined action. It is highly advised to keep this on, which is the default.";
default = true;
};
action = mkOption {
type = types.str;
description = "The action to execute.";
};
}) ];
mapOptions = mode: mkOption {
description = "Mappings for ${mode} mode";
type = types.attrsOf mapOption;
default = {};
};
helpers = import ./plugins/helpers.nix { lib = lib; }; helpers = import ./plugins/helpers.nix { lib = lib; };
in in
{ {
@ -74,6 +123,41 @@ in
default = {}; default = {};
description = "Global variables"; description = "Global variables";
}; };
maps = mkOption {
type = types.submodule {
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 for any mode.
For plain maps (e.g. just 'map' or 'remap') use maps.normalVisualOp.
'';
example = ''
maps = {
normalVisualOp.";" = ":"; # Same as noremap ; :
normal."<leader>m" = {
silent = true;
action = "<cmd>make<CR>";
}; # Same as nnoremap <leader>m <silent> <cmd>make<CR>
};
'';
};
}; };
}; };
@ -95,6 +179,20 @@ in
wrappedNeovim = pkgs.wrapNeovimUnstable cfg.package (neovimConfig // { wrappedNeovim = pkgs.wrapNeovimUnstable cfg.package (neovimConfig // {
wrapperArgs = lib.escapeShellArgs neovimConfig.wrapperArgs; wrapperArgs = lib.escapeShellArgs neovimConfig.wrapperArgs;
}); });
mappings =
(helpers.genMaps "" cfg.maps.normalVisualOp) ++
(helpers.genMaps "n" cfg.maps.normal) ++
(helpers.genMaps "i" cfg.maps.insert) ++
(helpers.genMaps "v" cfg.maps.visual) ++
(helpers.genMaps "x" cfg.maps.visualOnly) ++
(helpers.genMaps "s" cfg.maps.select) ++
(helpers.genMaps "t" cfg.maps.terminal) ++
(helpers.genMaps "o" cfg.maps.operator) ++
(helpers.genMaps "l" cfg.maps.lang) ++
(helpers.genMaps "!" cfg.maps.insertCommand) ++
(helpers.genMaps "c" cfg.maps.command);
in mkIf cfg.enable { in mkIf cfg.enable {
environment.systemPackages = [ wrappedNeovim ]; environment.systemPackages = [ wrappedNeovim ];
programs.nixvim = { programs.nixvim = {
@ -104,7 +202,6 @@ in
${cfg.extraConfigLua} ${cfg.extraConfigLua}
EOF EOF
'' + cfg.extraConfigVim + (optionalString (cfg.colorscheme != "") '' '' + cfg.extraConfigVim + (optionalString (cfg.colorscheme != "") ''
colorscheme ${cfg.colorscheme} colorscheme ${cfg.colorscheme}
''); '');
packages.nixvim = { packages.nixvim = {
@ -143,6 +240,14 @@ in
end end
end end
-- }}} -- }}}
'' + optionalString (mappings != []) ''
-- Set up keybinds {{{
local __nixvim_binds = ${helpers.toLuaObject mappings}
for i, map in ipairs(__nixvim_binds) do
vim.api.nvim_set_keymap(map.mode, map.key, map.action, map.config)
end
-- }}}
''; '';
}; };

View file

@ -27,4 +27,28 @@ rec {
else if isNull args then else if isNull args then
"nil" "nil"
else ""; else "";
# Generates maps for a lua config
genMaps = mode: maps: let
normalized = builtins.mapAttrs (key: action:
if builtins.isString action then
{
silent = false;
expr = false;
unique = false;
noremap = true;
script = false;
nowait = false;
action = action;
}
else action) maps;
in builtins.attrValues (builtins.mapAttrs (key: action:
{
action = action.action;
config = lib.filterAttrs (_: v: v) {
inherit (action) silent expr unique noremap script nowait;
};
key = key;
mode = mode;
}) normalized);
} }

View file

@ -30,14 +30,12 @@ in {
sections = mkOption { sections = mkOption {
default = null; default = null;
type = with types; nullOr (submodule { type = with types; nullOr (submodule {
options = { a = sectionOption;
a = sectionOption; b = sectionOption;
b = sectionOption; c = sectionOption;
c = sectionOption; x = sectionOption;
x = sectionOption; y = sectionOption;
y = sectionOption; z = sectionOption;
z = sectionOption;
};
}); });
}; };