Skip to content

Commit

Permalink
Add support for mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
pta2002 committed Jan 5, 2021
1 parent 7122ccd commit 0586bed
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
.tmp/
.tmp
1 change: 0 additions & 1 deletion .tmp

This file was deleted.

6 changes: 3 additions & 3 deletions example.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Equivalent to nnoremap ; :
";" = ":";
# 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...
};

Expand All @@ -25,12 +25,12 @@
options = {
tabstop = 4;
shiftwidth = 4;
noexpandtab = true;
expandtab = false;

mouse = "a";

# etc...
}
};

# Of course, we can still use comfy vimscript:
extraConfigVim = builtins.readFile ./init.vim;
Expand Down
2 changes: 2 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@

options.number = true;

maps.normalVisualOp."ç" = ":";

plugins.airline = {
enable = true;
powerline = true;
Expand Down
107 changes: 106 additions & 1 deletion nixvim.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
in
{
Expand Down Expand Up @@ -74,6 +123,41 @@ in
default = {};
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>
};
'';
};
};
};

Expand All @@ -95,6 +179,20 @@ in
wrappedNeovim = pkgs.wrapNeovimUnstable cfg.package (neovimConfig // {
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 {
environment.systemPackages = [ wrappedNeovim ];
programs.nixvim = {
Expand All @@ -104,7 +202,6 @@ in
${cfg.extraConfigLua}
EOF
'' + cfg.extraConfigVim + (optionalString (cfg.colorscheme != "") ''
colorscheme ${cfg.colorscheme}
'');
packages.nixvim = {
Expand Down Expand Up @@ -143,6 +240,14 @@ in
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
-- }}}
'';
};

Expand Down
24 changes: 24 additions & 0 deletions plugins/helpers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,28 @@ rec {
else if isNull args then
"nil"
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);
}
14 changes: 6 additions & 8 deletions plugins/statuslines/airline.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ in {
sections = mkOption {
default = null;
type = with types; nullOr (submodule {
options = {
a = sectionOption;
b = sectionOption;
c = sectionOption;
x = sectionOption;
y = sectionOption;
z = sectionOption;
};
a = sectionOption;
b = sectionOption;
c = sectionOption;
x = sectionOption;
y = sectionOption;
z = sectionOption;
});
};

Expand Down

0 comments on commit 0586bed

Please sign in to comment.