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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
/// Parsed representation of a keybinding string from the HJSON config such as
/// `"Ctrl+s"`, `"Ctrl+Shift+c"`, `"Tab"`, `"PageUp"`, `"F2"`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct KeyChord {
pub code: KeyCode,
pub modifiers: KeyModifiers,
}
impl KeyChord {
/// Format the chord in the same `Ctrl+key` / `Shift+F1` /
/// `PageUp` shape the parser accepts. Used by `ink.key.list`
/// and the auto-generated status-bar hints so the user can
/// copy-paste an output chord back into HJSON unchanged.
pub fn to_display_string(&self) -> String {
let mut parts: Vec<String> = Vec::new();
if self.modifiers.contains(KeyModifiers::CONTROL) {
parts.push("Ctrl".into());
}
if self.modifiers.contains(KeyModifiers::ALT) {
parts.push("Alt".into());
}
if self.modifiers.contains(KeyModifiers::SHIFT) {
parts.push("Shift".into());
}
if self.modifiers.contains(KeyModifiers::SUPER) {
parts.push("Super".into());
}
let key = match self.code {
KeyCode::Char(' ') => "Space".to_string(),
KeyCode::Char(c) => c.to_string(),
KeyCode::Tab => "Tab".to_string(),
KeyCode::BackTab => "Tab".to_string(),
KeyCode::Enter => "Enter".to_string(),
KeyCode::Esc => "Esc".to_string(),
KeyCode::Backspace => "Backspace".to_string(),
KeyCode::Delete => "Delete".to_string(),
KeyCode::Insert => "Insert".to_string(),
KeyCode::Home => "Home".to_string(),
KeyCode::End => "End".to_string(),
KeyCode::PageUp => "PageUp".to_string(),
KeyCode::PageDown => "PageDown".to_string(),
KeyCode::Up => "Up".to_string(),
KeyCode::Down => "Down".to_string(),
KeyCode::Left => "Left".to_string(),
KeyCode::Right => "Right".to_string(),
KeyCode::F(n) => format!("F{n}"),
KeyCode::Null => "Null".to_string(),
other => format!("{other:?}"),
};
parts.push(key);
parts.join("+")
}
pub fn parse(s: &str) -> Result<Self, String> {
let mut mods = KeyModifiers::empty();
let mut code: Option<KeyCode> = None;
let mut shift_present = false;
for raw in s.split('+') {
let token = raw.trim();
if token.is_empty() {
continue;
}
match token.to_ascii_lowercase().as_str() {
"ctrl" | "control" => mods.insert(KeyModifiers::CONTROL),
"shift" => {
shift_present = true;
}
"alt" | "meta" | "option" => mods.insert(KeyModifiers::ALT),
"super" | "cmd" | "command" => mods.insert(KeyModifiers::SUPER),
_ => {
if code.is_some() {
return Err(format!("more than one key in `{s}`"));
}
code = Some(parse_code(token)?);
}
}
}
let mut code = code.ok_or_else(|| format!("no key code in `{s}`"))?;
// Normalize: a single-letter chord like "Shift+a" stores Char('A') in
// many terminals, while "Ctrl+Shift+a" stores Char('a') with both
// CONTROL and SHIFT. Make matching predictable by always upper-casing
// a Char when Shift is part of the chord and lower-casing otherwise.
//
// Non-alphabetic Char codes (`|`, `}`, `?`, `>`, …) need different
// handling: the glyph itself encodes the Shift on US-layout
// keyboards, and modern terminals (Kitty / iTerm2 disambiguation)
// report `Shift+\` as `Char('|') + SHIFT` while legacy terminals
// send `Char('|')` without SHIFT. Storing SHIFT in the chord
// would lock the binding to one terminal mode; strip it so both
// styles normalise onto the same canonical `Char(glyph)` without
// a SHIFT modifier. `matches()` mirrors the strip on the event
// side.
if let KeyCode::Char(c) = code {
if shift_present {
if c.is_ascii_alphabetic() {
mods.insert(KeyModifiers::SHIFT);
code = KeyCode::Char(c.to_ascii_uppercase());
}
// Non-alphabetic + Shift: SHIFT is implicit; drop.
} else if c.is_ascii_alphabetic() {
code = KeyCode::Char(c.to_ascii_lowercase());
}
} else if shift_present {
mods.insert(KeyModifiers::SHIFT);
}
Ok(Self { code, modifiers: mods })
}
pub fn matches(&self, ev: &KeyEvent) -> bool {
// BackTab arrives when Shift+Tab is pressed in some terminals.
let mut ev_mods = ev.modifiers;
let ev_code = match ev.code {
KeyCode::BackTab => {
ev_mods.insert(KeyModifiers::SHIFT);
KeyCode::Tab
}
KeyCode::Char(c) if c.is_ascii_alphabetic() => {
// Different terminals diverge:
// * Kitty / iTerm2 disambiguation: Shift+p → Char('P') + SHIFT.
// * Most TTYs: Shift+p → Char('P'), no SHIFT modifier flag
// (case carries the info; SHIFT is implicit).
// * `Ctrl+Shift+p` in any terminal: Char('p') + CONTROL+SHIFT.
// Normalize all three onto "Char(uppercase) + SHIFT" when the
// letter is upper, "Char(lowercase) without SHIFT" otherwise,
// so chord lookup is deterministic.
if ev_mods.contains(KeyModifiers::SHIFT) {
KeyCode::Char(c.to_ascii_uppercase())
} else if c.is_ascii_uppercase() {
// Terminal sent a capital letter without the explicit
// SHIFT bit (the common legacy path). Promote so a
// chord parsed as `Shift+p` (stored as Char('P') + SHIFT)
// still matches.
ev_mods.insert(KeyModifiers::SHIFT);
KeyCode::Char(c)
} else {
KeyCode::Char(c.to_ascii_lowercase())
}
}
KeyCode::Char(c) => {
// Non-alphabetic glyph (`|`, `}`, `?`, `>`, `:`, …).
// Modern terminals report `Shift+\` as
// `Char('|') + SHIFT`; legacy terminals send
// `Char('|')` without SHIFT. The glyph itself
// already encodes the Shift on US-layout keyboards,
// so strip the SHIFT bit when comparing — `parse()`
// does the same for chord registration, so both
// styles meet on the canonical "Char(glyph) without
// SHIFT" form. Ctrl / Alt stay as-is so `Ctrl+|`
// remains distinguishable from `|`.
ev_mods.remove(KeyModifiers::SHIFT);
KeyCode::Char(c)
}
other => other,
};
// Restrict to modifiers we care about (ignore NUM_LOCK, etc.).
let mask = KeyModifiers::CONTROL
| KeyModifiers::SHIFT
| KeyModifiers::ALT
| KeyModifiers::SUPER;
ev_code == self.code && (ev_mods & mask) == (self.modifiers & mask)
}
}
fn parse_code(name: &str) -> Result<KeyCode, String> {
let lower = name.to_ascii_lowercase();
Ok(match lower.as_str() {
"tab" => KeyCode::Tab,
"enter" | "return" => KeyCode::Enter,
"esc" | "escape" => KeyCode::Esc,
"space" => KeyCode::Char(' '),
"backspace" => KeyCode::Backspace,
"delete" | "del" => KeyCode::Delete,
"insert" | "ins" => KeyCode::Insert,
"home" => KeyCode::Home,
"end" => KeyCode::End,
"pageup" | "pgup" => KeyCode::PageUp,
"pagedown" | "pgdown" | "pgdn" => KeyCode::PageDown,
"up" => KeyCode::Up,
"down" => KeyCode::Down,
"left" => KeyCode::Left,
"right" => KeyCode::Right,
// `fN` where N is 1..24. Length bound 2..=3 excludes the
// bare letter "f" (which is a normal Char chord) but
// includes "f1" through "f24".
s if s.starts_with('f') && s.len() >= 2 && s.len() <= 3 => {
let n: u8 = s[1..]
.parse()
.map_err(|_| format!("bad function key `{name}`"))?;
if !(1..=24).contains(&n) {
return Err(format!("function key {n} out of range"));
}
KeyCode::F(n)
}
s if s.chars().count() == 1 => {
let c = s.chars().next().unwrap();
KeyCode::Char(c)
}
_ => return Err(format!("unknown key `{name}`")),
})
}
#[cfg(test)]
mod tests {
use super::*;
fn ev(code: KeyCode, mods: KeyModifiers) -> KeyEvent {
KeyEvent::new(code, mods)
}
#[test]
fn parse_ctrl_s() {
let k = KeyChord::parse("Ctrl+s").unwrap();
assert!(k.matches(&ev(KeyCode::Char('s'), KeyModifiers::CONTROL)));
assert!(!k.matches(&ev(KeyCode::Char('s'), KeyModifiers::NONE)));
}
#[test]
fn parse_ctrl_slash() {
let k = KeyChord::parse("Ctrl+/").unwrap();
assert!(k.matches(&ev(KeyCode::Char('/'), KeyModifiers::CONTROL)));
}
#[test]
fn parse_shift_tab() {
let k = KeyChord::parse("Shift+Tab").unwrap();
assert!(k.matches(&ev(KeyCode::Tab, KeyModifiers::SHIFT)));
assert!(k.matches(&ev(KeyCode::BackTab, KeyModifiers::NONE)));
}
#[test]
fn shifted_symbol_matches_in_modern_and_legacy_terminals() {
// `|` is `Shift+\` on US layout. Modern terminals
// (Kitty / iTerm2 disambiguation) send `Char('|') +
// SHIFT`; legacy terminals send `Char('|')` without
// SHIFT. A chord written as just `"|"` must match
// both. Same for `}`, `?`, `>`, `:`, `"`.
let pipe = KeyChord::parse("|").unwrap();
assert!(pipe.matches(&ev(KeyCode::Char('|'), KeyModifiers::SHIFT)));
assert!(pipe.matches(&ev(KeyCode::Char('|'), KeyModifiers::NONE)));
// Writing `"Shift+|"` should normalize to the same
// canonical chord — both forms accept both event
// shapes. This is what the Ctrl+B | binding for the
// HJSON editor uses.
let pipe_shift = KeyChord::parse("Shift+|").unwrap();
assert_eq!(pipe, pipe_shift);
// Other shifted glyphs.
let q = KeyChord::parse("?").unwrap();
assert!(q.matches(&ev(KeyCode::Char('?'), KeyModifiers::SHIFT)));
assert!(q.matches(&ev(KeyCode::Char('?'), KeyModifiers::NONE)));
// Ctrl+| stays distinguishable from `|` even when
// SHIFT is auto-stripped.
let ctrl_pipe = KeyChord::parse("Ctrl+|").unwrap();
assert!(ctrl_pipe.matches(&ev(
KeyCode::Char('|'),
KeyModifiers::CONTROL | KeyModifiers::SHIFT,
)));
assert!(ctrl_pipe.matches(&ev(KeyCode::Char('|'), KeyModifiers::CONTROL)));
assert!(!ctrl_pipe.matches(&ev(KeyCode::Char('|'), KeyModifiers::NONE)));
}
#[test]
fn parse_pageup() {
let k = KeyChord::parse("PageUp").unwrap();
assert!(k.matches(&ev(KeyCode::PageUp, KeyModifiers::NONE)));
}
#[test]
fn parse_ctrl_shift_letter() {
let k = KeyChord::parse("Ctrl+Shift+c").unwrap();
let mods = KeyModifiers::CONTROL | KeyModifiers::SHIFT;
assert!(k.matches(&ev(KeyCode::Char('C'), mods)));
// Some terminals send lowercase + SHIFT instead of uppercase.
assert!(k.matches(&ev(KeyCode::Char('c'), mods)));
}
/// 1.2.8 regression test for the case that exposed the
/// `Ctrl+V Shift+P` ≡ `Ctrl+V p` bug: legacy terminals
/// without the kitty disambiguation send Shift+letter
/// as an uppercase Char WITHOUT the SHIFT modifier flag
/// — case carries the info instead. The chord matcher
/// has to treat that as implicit-Shift or the dispatch
/// table collapses the two chords onto whichever entry
/// is checked first.
#[test]
fn shift_letter_matches_uppercase_without_shift_flag() {
let k = KeyChord::parse("Shift+p").unwrap();
// The terminal-as-modern case: explicit SHIFT bit.
assert!(k.matches(&ev(KeyCode::Char('P'), KeyModifiers::SHIFT)));
// The legacy case: uppercase letter with no modifier.
assert!(k.matches(&ev(KeyCode::Char('P'), KeyModifiers::NONE)));
// Bare lowercase 'p' must NOT match — otherwise the
// distinct `p` chord can't be bound separately.
assert!(!k.matches(&ev(KeyCode::Char('p'), KeyModifiers::NONE)));
}
#[test]
fn plain_letter_chord_rejects_uppercase() {
// Symmetric guard: `p` chord must NOT match Shift+P.
let k = KeyChord::parse("p").unwrap();
assert!(k.matches(&ev(KeyCode::Char('p'), KeyModifiers::NONE)));
assert!(!k.matches(&ev(KeyCode::Char('P'), KeyModifiers::NONE)));
assert!(!k.matches(&ev(KeyCode::Char('P'), KeyModifiers::SHIFT)));
}
}