module: Add shortcuts module

This commit is contained in:
Peter Jones 2022-06-22 16:53:37 -07:00
parent 9e29bcddf3
commit 65e4ed1364
No known key found for this signature in database
GPG Key ID: 9DAFAA8D01941E49
4 changed files with 96 additions and 0 deletions

View File

@ -6,6 +6,21 @@
# A high-level setting:
workspace.clickItemTo = "select";
# A mid-level setting:
shortcuts = {
ksmserver = {
"Lock Session" = [ "Screensaver" "Meta+Ctrl+Alt+L" ];
};
kwin = {
"Expose" = "Meta+,";
"Switch Window Down" = "Meta+J";
"Switch Window Left" = "Meta+H";
"Switch Window Right" = "Meta+L";
"Switch Window Up" = "Meta+K";
};
};
# A low-level setting:
files."baloofilerc"."Basic Settings"."Indexing-Enabled" = false;
};

View File

@ -3,6 +3,7 @@
{
imports = [
./files.nix
./shortcuts.nix
./windows.nix
./workspace.nix
];

46
modules/shortcuts.nix Normal file
View File

@ -0,0 +1,46 @@
# Global keyboard shortcuts:
{ config, lib, ... }:
let
cfg = config.programs.plasma;
# Convert one shortcut into a settings attribute set.
shortcutToNameValuePair = action: skey:
let
# Keys are expected to be a list:
keys =
if builtins.isList skey
then (if builtins.length skey == 0 then [ "none" ] else skey)
else [ skey ];
# Don't allow un-escaped commas:
escape = lib.escape [ "," ];
in
{
name = action;
value = lib.concatStringsSep "," [
(lib.concatStringsSep "\t" (map escape keys))
"" # List of default keys, not needed.
"" # Display string, not needed.
];
};
shortcutsToSettings = groups:
lib.mapAttrs (_group: lib.mapAttrs' shortcutToNameValuePair) groups;
in
{
options.programs.plasma.shortcuts = lib.mkOption {
type = with lib.types; attrsOf (attrsOf (oneOf [ (listOf str) str ]));
default = { };
description = ''
An attribute set where the keys are application groups and the
values are shortcuts.
'';
};
config = lib.mkIf cfg.enable {
programs.plasma.files."kglobalshortcutsrc" =
shortcutsToSettings cfg.shortcuts;
};
}

View File

@ -131,6 +131,9 @@ module Rc2Nix
puts("{")
puts(" programs.plasma = {")
puts(" enable = true;")
puts(" shortcuts = {")
pp_shortcuts(settings["kglobalshortcutsrc"], 6)
puts(" };")
puts(" files = {")
pp_settings(settings, 6)
puts(" };")
@ -143,6 +146,8 @@ module Rc2Nix
settings.keys.sort.each do |file|
settings[file].keys.sort.each do |group|
settings[file][group].keys.sort.each do |key|
next if file == "kglobalshortcutsrc" && key != "_k_friendly_name"
print(" " * indent)
print("\"#{file}\".")
print("\"#{group}\".")
@ -154,6 +159,35 @@ module Rc2Nix
end
end
############################################################################
def pp_shortcuts(groups, indent)
groups&.keys.sort.each do |group|
groups[group].keys.sort.each do |action|
next if action == "_k_friendly_name"
print(" " * indent)
print("\"#{group}\".")
print("\"#{action}\" = ")
keys = groups[group][action].
split(/(?<!\\),/).first.
gsub(/\\?\\,/, ',').
gsub(/\\t/, "\t").
split(/\t/)
if keys.size > 1
print("[" + keys.map {|k| nix_val(k)}.join(" ") + "]")
elsif keys.first == "none"
print("[ ]")
else
print(nix_val(keys.first))
end
print(";\n")
end
end
end
############################################################################
def nix_val(str)
case str