-
Notifications
You must be signed in to change notification settings - Fork 0
/
kanata.nix
261 lines (231 loc) · 6.69 KB
/
kanata.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
{
config,
lib,
...
}:
with lib; let
defCfg = concatStringsSep "\n" [
(optionalString (cfg.magic.timeout != 0) "rapid-event-delay ${toString cfg.rapidEventDelay}")
(optionalString (cfg.magic.timeout != 0) "sequence-timeout ${toString cfg.magic.timeout}")
(optionalString cfg.magic.enable "sequence-always-on true")
(optionalString (cfg.magic.mode != "") "sequence-input-mode ${cfg.magic.mode}")
cfg.extraDefCfg
];
capitalizeFirst = s: "S-${s}";
generateRule = template: rules: lib.concatMapStringsSep "\n" template rules;
ruleTemplate = {
name,
inputs,
outputs,
}: ''
(t! seq ${name} (${inputs}) (macro ${outputs}))
'';
rptRuleTemplate = {
name,
inputs,
outputs,
}: ''
(t! seq ${name} (${inputs}) (macro bspc ${outputs}))
'';
shiftRuleTemplate = {
name,
inputs,
outputs,
}: ''
(t! seq ${name}-shift (lsft ${inputs}) (macro ${capitalizeFirst outputs}))
'';
shiftRptRuleTemplate = {
name,
inputs,
outputs,
}: ''
(t! seq ${name}-shift-rpt (lsft ${inputs}) (macro bspc ${capitalizeFirst outputs}))
'';
rules = generateRule ruleTemplate cfg.magic.rules;
rptRules = generateRule rptRuleTemplate cfg.magic.rptRules;
shiftRules = generateRule shiftRuleTemplate cfg.magic.rules;
shiftRptRules = generateRule shiftRptRuleTemplate cfg.magic.rptRules;
# S-9 -> (
# S-' -> "
# S-; -> :
# S-. -> <
# S-[ -> {
# S-6 -> ^
# S-3 -> #
startingKeys = ["spc" "tab" "." "`" ";" "/" "-" "S-;" "S-." "S-3" "S-9" "S-[" "S-'" "S-6" "🔢₊" "S-ret" "ret" "Home" "End"];
isExcludedKey = startingKey:
(startingKey == "tab" && !cfg.magic.includeTab)
|| (startingKey == "ret" && !cfg.magic.includeReturn)
|| (startingKey == "/" && !cfg.magic.includeSlash)
|| (startingKey == "Home")
|| (startingKey == "End");
generateWordStartingRules = {
templateFunc,
ruleSet,
shiftMode ? false,
}:
lib.concatStringsSep "\n" (map (
startingKey:
lib.concatMapStringsSep "\n" (
rule: let
baseInputs =
if shiftMode
then "lsft ${rule.inputs}"
else rule.inputs;
baseOutputs =
if shiftMode
then capitalizeFirst rule.outputs
else rule.outputs;
in
templateFunc {
name = "${rule.name}-${startingKey}${
if shiftMode
then "-shift"
else ""
}";
inputs = "${startingKey} ${baseInputs}";
outputs =
if isExcludedKey startingKey
then baseOutputs
else "${startingKey} ${baseOutputs}";
}
)
ruleSet
)
startingKeys);
wordStartingRules = generateWordStartingRules {
templateFunc = ruleTemplate;
ruleSet = cfg.magic.wordStartingRules;
};
wordStartingRptRules = generateWordStartingRules {
templateFunc = rptRuleTemplate;
ruleSet = cfg.magic.wordStartingRptRules;
};
shiftWordStartingRules = generateWordStartingRules {
templateFunc = ruleTemplate;
ruleSet = cfg.magic.wordStartingRules;
shiftMode = true;
};
shiftWordStartingRptRules = generateWordStartingRules {
templateFunc = rptRuleTemplate;
ruleSet = cfg.magic.wordStartingRptRules;
shiftMode = true;
};
magic = ''
(deftemplate seq (vk-name input-keys output-action)
(deffakekeys $vk-name $output-action)
(defseq $vk-name $input-keys)
)
${wordStartingRules}
${wordStartingRptRules}
${shiftWordStartingRules}
${shiftWordStartingRptRules}
${rules}
${rptRules}
${shiftRules}
${shiftRptRules}
'';
cfg = config.psilocybin;
in {
imports = [./layout];
options.psilocybin = {
enable = mkEnableOption "psilocybin";
devices = mkOption {
type = types.listOf types.str;
default = [];
description = "List of input devices for kanata to intercept. Empty list means detect all keyboards.";
};
magic = {
enable = mkOption {
type = types.bool;
default = true;
};
rules = mkOption {
type = types.listOf types.attrs;
example = literalExpression [
{
name = "mp";
inputs = "m *}";
outputs = "m p";
}
{
name = "was";
inputs = "w a *";
outputs = "w a s";
}
];
};
rptRules = mkOption {
type = types.listOf types.attrs;
description = "Magic rules that activate via the repeat key, automatically deleting the repeated character.";
};
wordStartingRules = mkOption {
type = types.listOf types.attrs;
};
wordStartingRptRules = mkOption {
type = types.listOf types.attrs;
};
includeReturn = mkEnableOption "include return in outputs";
includeTab = mkEnableOption "include tab in outputs";
includeSlash = mkEnableOption "include slash in outputs";
mode = mkOption {
type = types.enum ["visible-backspaced" "hidden-suppressed" "hidden-delay-type"];
default = "visible-backspaced";
description = "Sequence input mode for kanata.";
};
timeout = mkOption {
type = types.int;
default = 2000;
description = "Timeout for key sequences in milliseconds.";
};
};
rapidEventDelay = mkOption {
type = types.int;
default = 20;
description = "Delay for rapid events to handle chords and one-shot shifts.";
};
ansi = {
enable = mkOption {
type = types.bool;
default = !cfg.jis.enable;
};
config = mkOption {
type = types.str;
description = "Configuration for ANSI layout.";
};
};
jis = {
enable = mkEnableOption "JIS keyboard layout";
config = mkOption {
type = types.str;
description = "Configuration for JIS layout.";
};
};
config = mkOption {
type = types.str;
description = "Additional kanata configuration (e.g., defalias, defchord).";
};
extraDefCfg = mkOption {
type = types.str;
default = "";
description = "Extra kanata defcfg options.";
};
};
config = {
services.kanata = mkIf cfg.enable {
enable = true;
keyboards = {
psilocybin = mkIf cfg.ansi.enable {
extraDefCfg = defCfg;
config = cfg.ansi.config + cfg.config + magic;
inherit (cfg) devices;
};
psilocybinjis = mkIf cfg.jis.enable {
extraDefCfg = defCfg;
config = cfg.jis.config + cfg.config + magic;
inherit (cfg) devices;
};
};
};
};
}