-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.ts
183 lines (157 loc) · 3.23 KB
/
base.ts
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
import { kb } from "./deps.ts";
export const MODES = [
"normal",
"insert",
"native",
"passthrough",
"visual",
"search",
] as const;
export type Mode = (typeof MODES)[number];
export const LAYERS = [
"none",
"application",
"window",
"resize",
"move",
] as const;
export type Layer = (typeof LAYERS)[number];
export const OPERATORS = ["normal", "delete", "change", "yank"] as const;
export type Operator = (typeof OPERATORS)[number];
export const SPECIFIERS = ["none", "in", "around"] as const;
export type Specifier = (typeof SPECIFIERS)[number];
export const VAR = {
mode: "ultra_mode",
layer: "ultra_layer",
operator: "ultra_operator",
specifier: "ultra_specifier",
} as const;
export type Var = (typeof VAR)[keyof typeof VAR];
export type Application = string;
export interface Command {
kind: "command";
command: string;
}
export interface SetMode {
kind: "set-mode";
mode: Mode;
}
export interface SetLayer {
kind: "set-layer";
layer: Layer;
}
export interface SetOperator {
kind: "set-operator";
operator: Operator;
}
export interface SetSpecifier {
kind: "set-specifier";
specifier: Specifier;
}
export type Action =
| Binding
| Command
| SetMode
| SetLayer
| SetOperator
| SetSpecifier;
export interface Binding {
kind: "binding";
key: ExtendedKey;
modifiers?: readonly ExtendedKey[];
anyModifer?: boolean;
specifier: Specifier;
}
export type ExtendedKey =
| kb.Key
| "vk_none"
| "page_up"
| "page_down"
| "home"
| "end"
| "control"
| "command"
| "shift"
| "equal_sign"
| "f12";
// Here null means "doesn't matter", i.e. perform the target action regardless
// of the current value of the context variable.
export interface Context {
mode?: Mode | null;
notMode?: Mode | readonly Mode[];
operator?: Operator | null;
layer?: Layer | null;
application?: Application | null;
specifier?: Specifier | null;
extra?: kb.Condition | kb.Condition[];
}
// This is a shortcut for matching any context.
export const ANY_CONTEXT: Context = {
mode: null,
notMode: [],
operator: null,
layer: null,
application: null,
specifier: null,
extra: [],
};
export function fillContextDefaults({
mode = null,
notMode = "passthrough",
operator = "normal",
layer = "none",
application = null,
specifier = "none",
extra = [],
}: Context): Context {
return {
mode,
notMode,
operator,
layer,
application,
specifier,
extra,
};
}
export function context(context: Context): Context {
return context;
}
export interface ContextMapping {
context: Context;
targets: readonly (Action | readonly Action[])[];
}
export interface Mapping {
binding: Binding;
byContext: ContextMapping[];
}
export function command(command: string): Command {
return {
kind: "command",
command,
};
}
export function setMode(mode: Mode): SetMode {
return {
kind: "set-mode",
mode,
};
}
export function setLayer(layer: Layer): SetLayer {
return {
kind: "set-layer",
layer,
};
}
export function setOperator(operator: Operator): SetOperator {
return {
kind: "set-operator",
operator,
};
}
export function setSpecifier(specifier: Specifier): SetSpecifier {
return {
kind: "set-specifier",
specifier,
};
}