fish: add binds option (#7121)
binds options is a wrapper of fish_user_key_bindings that contains custom binds
This commit is contained in:
parent
13ed57aaa6
commit
85a27991d5
3 changed files with 155 additions and 0 deletions
|
|
@ -10,6 +10,7 @@ let
|
|||
literalExpression
|
||||
mkIf
|
||||
mkOption
|
||||
mkEnableOption
|
||||
optional
|
||||
types
|
||||
;
|
||||
|
|
@ -211,6 +212,57 @@ let
|
|||
};
|
||||
};
|
||||
|
||||
bindModule = types.submodule {
|
||||
options = {
|
||||
enable = mkEnableOption "enable the bind" // {
|
||||
default = true;
|
||||
};
|
||||
mode = mkOption {
|
||||
description = "Specify the bind mode that the bind is used in";
|
||||
type =
|
||||
with types;
|
||||
nullOr (enum [
|
||||
"default"
|
||||
"insert"
|
||||
"paste"
|
||||
]);
|
||||
default = null;
|
||||
};
|
||||
command = mkOption {
|
||||
description = "command that will be execute";
|
||||
type =
|
||||
with types;
|
||||
oneOf [
|
||||
str
|
||||
(listOf str)
|
||||
];
|
||||
};
|
||||
setsMode = mkOption {
|
||||
description = "Change current mode after bind is executed";
|
||||
type =
|
||||
with types;
|
||||
nullOr (enum [
|
||||
"default"
|
||||
"insert"
|
||||
"paste"
|
||||
]);
|
||||
default = null;
|
||||
};
|
||||
erase = mkEnableOption "remove bind";
|
||||
silent = mkEnableOption "Operate silently";
|
||||
operate = mkOption {
|
||||
description = "Operate on preset bindings or user bindings";
|
||||
type =
|
||||
with types;
|
||||
nullOr (enum [
|
||||
"preset"
|
||||
"user"
|
||||
]);
|
||||
default = null;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
abbrsStr = lib.concatStringsSep "\n" (
|
||||
lib.mapAttrsToList (
|
||||
name: def:
|
||||
|
|
@ -251,6 +303,44 @@ let
|
|||
lib.mapAttrsToList (k: v: "alias ${k} ${lib.escapeShellArg v}") cfg.shellAliases
|
||||
);
|
||||
|
||||
filteredBinds = lib.filterAttrs (_: { enable, ... }: enable) cfg.binds;
|
||||
|
||||
bindsStr = lib.concatStringsSep "\n" (
|
||||
lib.mapAttrsToList (
|
||||
k:
|
||||
{
|
||||
silent,
|
||||
erase,
|
||||
operate,
|
||||
mode,
|
||||
setsMode,
|
||||
command,
|
||||
...
|
||||
}:
|
||||
let
|
||||
opts =
|
||||
lib.optionals silent [ "-s" ]
|
||||
++ (
|
||||
if erase then
|
||||
[ "-e" ]
|
||||
else
|
||||
lib.optionals (!isNull operate) [ "--${operate}" ]
|
||||
++ lib.optionals (!isNull mode) [
|
||||
"--mode"
|
||||
mode
|
||||
]
|
||||
++ lib.optionals (!isNull setsMode) [
|
||||
"--sets-mode"
|
||||
setsMode
|
||||
]
|
||||
);
|
||||
in
|
||||
lib.concatStringsSep " " (
|
||||
[ "bind" ] ++ opts ++ [ k ] ++ map lib.escapeShellArg (lib.flatten [ command ])
|
||||
)
|
||||
) filteredBinds
|
||||
);
|
||||
|
||||
fishIndent =
|
||||
name: text:
|
||||
pkgs.runCommand name {
|
||||
|
|
@ -336,6 +426,20 @@ in
|
|||
'';
|
||||
};
|
||||
|
||||
binds = mkOption {
|
||||
type = types.attrsOf bindModule;
|
||||
default = { };
|
||||
description = "Manage key bindings";
|
||||
example =
|
||||
lib.literalExpression # nix
|
||||
''
|
||||
{
|
||||
"alt-s".command = "fish_commandline_prepend sudo";
|
||||
"alt-shift-b".command = "fish_commandline_append bat";
|
||||
}
|
||||
'';
|
||||
};
|
||||
|
||||
shellInit = mkOption {
|
||||
type = types.lines;
|
||||
default = "";
|
||||
|
|
@ -528,6 +632,10 @@ in
|
|||
}
|
||||
))
|
||||
|
||||
(mkIf (filteredBinds != { }) {
|
||||
programs.fish.functions.fish_user_key_bindings = bindsStr;
|
||||
})
|
||||
|
||||
{
|
||||
xdg.configFile."fish/config.fish".source = fishIndent "config.fish" ''
|
||||
# ~/.config/fish/config.fish: DO NOT EDIT -- this file has been generated
|
||||
|
|
|
|||
46
tests/modules/programs/fish/binds.nix
Normal file
46
tests/modules/programs/fish/binds.nix
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
{ lib, ... }:
|
||||
{
|
||||
config = {
|
||||
programs.fish = {
|
||||
enable = true;
|
||||
|
||||
binds = {
|
||||
"ctrl-d".command = "exit";
|
||||
|
||||
"ctrl-c" = {
|
||||
mode = "insert";
|
||||
command = [
|
||||
"kill-whole-line"
|
||||
"repaint"
|
||||
];
|
||||
};
|
||||
|
||||
"ctrl-g" = {
|
||||
command = [
|
||||
"git diff"
|
||||
"repaint"
|
||||
];
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
# Needed to avoid error with dummy fish package.
|
||||
xdg.dataFile."fish/home-manager_generated_completions".source = lib.mkForce (
|
||||
builtins.toFile "empty" ""
|
||||
);
|
||||
|
||||
nmt = {
|
||||
description = "if fish.binds is set, check function exists and contains valid binds";
|
||||
script = ''
|
||||
assertFileExists home-files/.config/fish/functions/fish_user_key_bindings.fish
|
||||
|
||||
assertFileContains home-files/.config/fish/functions/fish_user_key_bindings.fish \
|
||||
"bind ctrl-d exit"
|
||||
assertFileContains home-files/.config/fish/functions/fish_user_key_bindings.fish \
|
||||
"bind --mode insert ctrl-c kill-whole-line repaint"
|
||||
assertFileContains home-files/.config/fish/functions/fish_user_key_bindings.fish \
|
||||
"bind ctrl-g 'git diff' repaint"
|
||||
'';
|
||||
};
|
||||
};
|
||||
}
|
||||
|
|
@ -5,4 +5,5 @@
|
|||
fish-no-functions = ./no-functions.nix;
|
||||
fish-plugins = ./plugins.nix;
|
||||
fish-manpage = ./manpage.nix;
|
||||
fish-binds = ./binds.nix;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue