1/// The mappings defined in this file where created from reading the alacritty source
2use alacritty_terminal::term::TermMode;
3use gpui::keymap::Keystroke;
4
5#[derive(Debug)]
6pub enum Modifiers {
7 None,
8 Alt,
9 Ctrl,
10 Shift,
11 CtrlShift,
12 Other,
13}
14
15impl Modifiers {
16 fn new(ks: &Keystroke) -> Self {
17 match (ks.alt, ks.ctrl, ks.shift, ks.cmd) {
18 (false, false, false, false) => Modifiers::None,
19 (true, false, false, false) => Modifiers::Alt,
20 (false, true, false, false) => Modifiers::Ctrl,
21 (false, false, true, false) => Modifiers::Shift,
22 (false, true, true, false) => Modifiers::CtrlShift,
23 _ => Modifiers::Other,
24 }
25 }
26
27 fn any(&self) -> bool {
28 match &self {
29 Modifiers::None => false,
30 Modifiers::Alt => true,
31 Modifiers::Ctrl => true,
32 Modifiers::Shift => true,
33 Modifiers::CtrlShift => true,
34 Modifiers::Other => true,
35 }
36 }
37}
38
39///This function checks if to_esc_str would work, assuming all terminal settings are off.
40///Note that this function is conservative. It can fail in cases where the actual to_esc_str succeeds.
41///This is unavoidable for our use case. GPUI cannot wait until we acquire the terminal
42///lock to determine whether we could actually send the keystroke with the current settings. Therefore,
43///This conservative guess is used instead. Note that in practice the case where this method
44///Returns false when the actual terminal would consume the keystroke never happens. All keystrokes
45///that depend on terminal modes also have a mapping that doesn't depend on the terminal mode.
46///This is fragile, but as these mappings are locked up in legacy compatibility, it's probably good enough
47pub fn might_convert(keystroke: &Keystroke) -> bool {
48 to_esc_str(keystroke, &TermMode::NONE).is_some()
49}
50
51pub fn to_esc_str(keystroke: &Keystroke, mode: &TermMode) -> Option<String> {
52 let modifiers = Modifiers::new(keystroke);
53
54 // Manual Bindings including modifiers
55 let manual_esc_str = match (keystroke.key.as_ref(), &modifiers) {
56 //Basic special keys
57 ("tab", Modifiers::None) => Some("\x09".to_string()),
58 ("escape", Modifiers::None) => Some("\x1b".to_string()),
59 ("enter", Modifiers::None) => Some("\x0d".to_string()),
60 ("backspace", Modifiers::None) => Some("\x7f".to_string()),
61 //Interesting escape codes
62 ("tab", Modifiers::Shift) => Some("\x1b[Z".to_string()),
63 ("backspace", Modifiers::Alt) => Some("\x1b\x7f".to_string()),
64 ("backspace", Modifiers::Shift) => Some("\x7f".to_string()),
65 ("home", Modifiers::Shift) if mode.contains(TermMode::ALT_SCREEN) => {
66 Some("\x1b[1;2H".to_string())
67 }
68 ("end", Modifiers::Shift) if mode.contains(TermMode::ALT_SCREEN) => {
69 Some("\x1b[1;2F".to_string())
70 }
71 ("pageup", Modifiers::Shift) if mode.contains(TermMode::ALT_SCREEN) => {
72 Some("\x1b[5;2~".to_string())
73 }
74 ("pagedown", Modifiers::Shift) if mode.contains(TermMode::ALT_SCREEN) => {
75 Some("\x1b[6;2~".to_string())
76 }
77 ("home", Modifiers::None) if mode.contains(TermMode::APP_CURSOR) => {
78 Some("\x1bOH".to_string())
79 }
80 ("home", Modifiers::None) if !mode.contains(TermMode::APP_CURSOR) => {
81 Some("\x1b[H".to_string())
82 }
83 ("end", Modifiers::None) if mode.contains(TermMode::APP_CURSOR) => {
84 Some("\x1bOF".to_string())
85 }
86 ("end", Modifiers::None) if !mode.contains(TermMode::APP_CURSOR) => {
87 Some("\x1b[F".to_string())
88 }
89 ("up", Modifiers::None) if mode.contains(TermMode::APP_CURSOR) => {
90 Some("\x1bOA".to_string())
91 }
92 ("up", Modifiers::None) if !mode.contains(TermMode::APP_CURSOR) => {
93 Some("\x1b[A".to_string())
94 }
95 ("down", Modifiers::None) if mode.contains(TermMode::APP_CURSOR) => {
96 Some("\x1bOB".to_string())
97 }
98 ("down", Modifiers::None) if !mode.contains(TermMode::APP_CURSOR) => {
99 Some("\x1b[B".to_string())
100 }
101 ("right", Modifiers::None) if mode.contains(TermMode::APP_CURSOR) => {
102 Some("\x1bOC".to_string())
103 }
104 ("right", Modifiers::None) if !mode.contains(TermMode::APP_CURSOR) => {
105 Some("\x1b[C".to_string())
106 }
107 ("left", Modifiers::None) if mode.contains(TermMode::APP_CURSOR) => {
108 Some("\x1bOD".to_string())
109 }
110 ("left", Modifiers::None) if !mode.contains(TermMode::APP_CURSOR) => {
111 Some("\x1b[D".to_string())
112 }
113 ("back", Modifiers::None) => Some("\x7f".to_string()),
114 ("insert", Modifiers::None) => Some("\x1b[2~".to_string()),
115 ("delete", Modifiers::None) => Some("\x1b[3~".to_string()),
116 ("pageup", Modifiers::None) => Some("\x1b[5~".to_string()),
117 ("pagedown", Modifiers::None) => Some("\x1b[6~".to_string()),
118 ("f1", Modifiers::None) => Some("\x1bOP".to_string()),
119 ("f2", Modifiers::None) => Some("\x1bOQ".to_string()),
120 ("f3", Modifiers::None) => Some("\x1bOR".to_string()),
121 ("f4", Modifiers::None) => Some("\x1bOS".to_string()),
122 ("f5", Modifiers::None) => Some("\x1b[15~".to_string()),
123 ("f6", Modifiers::None) => Some("\x1b[17~".to_string()),
124 ("f7", Modifiers::None) => Some("\x1b[18~".to_string()),
125 ("f8", Modifiers::None) => Some("\x1b[19~".to_string()),
126 ("f9", Modifiers::None) => Some("\x1b[20~".to_string()),
127 ("f10", Modifiers::None) => Some("\x1b[21~".to_string()),
128 ("f11", Modifiers::None) => Some("\x1b[23~".to_string()),
129 ("f12", Modifiers::None) => Some("\x1b[24~".to_string()),
130 ("f13", Modifiers::None) => Some("\x1b[25~".to_string()),
131 ("f14", Modifiers::None) => Some("\x1b[26~".to_string()),
132 ("f15", Modifiers::None) => Some("\x1b[28~".to_string()),
133 ("f16", Modifiers::None) => Some("\x1b[29~".to_string()),
134 ("f17", Modifiers::None) => Some("\x1b[31~".to_string()),
135 ("f18", Modifiers::None) => Some("\x1b[32~".to_string()),
136 ("f19", Modifiers::None) => Some("\x1b[33~".to_string()),
137 ("f20", Modifiers::None) => Some("\x1b[34~".to_string()),
138 // NumpadEnter, Action::Esc("\n".into());
139 //Mappings for caret notation keys
140 ("a", Modifiers::Ctrl) => Some("\x01".to_string()), //1
141 ("A", Modifiers::CtrlShift) => Some("\x01".to_string()), //1
142 ("b", Modifiers::Ctrl) => Some("\x02".to_string()), //2
143 ("B", Modifiers::CtrlShift) => Some("\x02".to_string()), //2
144 ("c", Modifiers::Ctrl) => Some("\x03".to_string()), //3
145 ("C", Modifiers::CtrlShift) => Some("\x03".to_string()), //3
146 ("d", Modifiers::Ctrl) => Some("\x04".to_string()), //4
147 ("D", Modifiers::CtrlShift) => Some("\x04".to_string()), //4
148 ("e", Modifiers::Ctrl) => Some("\x05".to_string()), //5
149 ("E", Modifiers::CtrlShift) => Some("\x05".to_string()), //5
150 ("f", Modifiers::Ctrl) => Some("\x06".to_string()), //6
151 ("F", Modifiers::CtrlShift) => Some("\x06".to_string()), //6
152 ("g", Modifiers::Ctrl) => Some("\x07".to_string()), //7
153 ("G", Modifiers::CtrlShift) => Some("\x07".to_string()), //7
154 ("h", Modifiers::Ctrl) => Some("\x08".to_string()), //8
155 ("H", Modifiers::CtrlShift) => Some("\x08".to_string()), //8
156 ("i", Modifiers::Ctrl) => Some("\x09".to_string()), //9
157 ("I", Modifiers::CtrlShift) => Some("\x09".to_string()), //9
158 ("j", Modifiers::Ctrl) => Some("\x0a".to_string()), //10
159 ("J", Modifiers::CtrlShift) => Some("\x0a".to_string()), //10
160 ("k", Modifiers::Ctrl) => Some("\x0b".to_string()), //11
161 ("K", Modifiers::CtrlShift) => Some("\x0b".to_string()), //11
162 ("l", Modifiers::Ctrl) => Some("\x0c".to_string()), //12
163 ("L", Modifiers::CtrlShift) => Some("\x0c".to_string()), //12
164 ("m", Modifiers::Ctrl) => Some("\x0d".to_string()), //13
165 ("M", Modifiers::CtrlShift) => Some("\x0d".to_string()), //13
166 ("n", Modifiers::Ctrl) => Some("\x0e".to_string()), //14
167 ("N", Modifiers::CtrlShift) => Some("\x0e".to_string()), //14
168 ("o", Modifiers::Ctrl) => Some("\x0f".to_string()), //15
169 ("O", Modifiers::CtrlShift) => Some("\x0f".to_string()), //15
170 ("p", Modifiers::Ctrl) => Some("\x10".to_string()), //16
171 ("P", Modifiers::CtrlShift) => Some("\x10".to_string()), //16
172 ("q", Modifiers::Ctrl) => Some("\x11".to_string()), //17
173 ("Q", Modifiers::CtrlShift) => Some("\x11".to_string()), //17
174 ("r", Modifiers::Ctrl) => Some("\x12".to_string()), //18
175 ("R", Modifiers::CtrlShift) => Some("\x12".to_string()), //18
176 ("s", Modifiers::Ctrl) => Some("\x13".to_string()), //19
177 ("S", Modifiers::CtrlShift) => Some("\x13".to_string()), //19
178 ("t", Modifiers::Ctrl) => Some("\x14".to_string()), //20
179 ("T", Modifiers::CtrlShift) => Some("\x14".to_string()), //20
180 ("u", Modifiers::Ctrl) => Some("\x15".to_string()), //21
181 ("U", Modifiers::CtrlShift) => Some("\x15".to_string()), //21
182 ("v", Modifiers::Ctrl) => Some("\x16".to_string()), //22
183 ("V", Modifiers::CtrlShift) => Some("\x16".to_string()), //22
184 ("w", Modifiers::Ctrl) => Some("\x17".to_string()), //23
185 ("W", Modifiers::CtrlShift) => Some("\x17".to_string()), //23
186 ("x", Modifiers::Ctrl) => Some("\x18".to_string()), //24
187 ("X", Modifiers::CtrlShift) => Some("\x18".to_string()), //24
188 ("y", Modifiers::Ctrl) => Some("\x19".to_string()), //25
189 ("Y", Modifiers::CtrlShift) => Some("\x19".to_string()), //25
190 ("z", Modifiers::Ctrl) => Some("\x1a".to_string()), //26
191 ("Z", Modifiers::CtrlShift) => Some("\x1a".to_string()), //26
192 ("@", Modifiers::Ctrl) => Some("\x00".to_string()), //0
193 ("[", Modifiers::Ctrl) => Some("\x1b".to_string()), //27
194 ("\\", Modifiers::Ctrl) => Some("\x1c".to_string()), //28
195 ("]", Modifiers::Ctrl) => Some("\x1d".to_string()), //29
196 ("^", Modifiers::Ctrl) => Some("\x1e".to_string()), //30
197 ("_", Modifiers::Ctrl) => Some("\x1f".to_string()), //31
198 ("?", Modifiers::Ctrl) => Some("\x7f".to_string()), //127
199 _ => None,
200 };
201 if manual_esc_str.is_some() {
202 return manual_esc_str;
203 }
204
205 // Automated bindings applying modifiers
206 if modifiers.any() {
207 let modifier_code = modifier_code(keystroke);
208 let modified_esc_str = match keystroke.key.as_ref() {
209 "up" => Some(format!("\x1b[1;{}A", modifier_code)),
210 "down" => Some(format!("\x1b[1;{}B", modifier_code)),
211 "right" => Some(format!("\x1b[1;{}C", modifier_code)),
212 "left" => Some(format!("\x1b[1;{}D", modifier_code)),
213 "f1" => Some(format!("\x1b[1;{}P", modifier_code)),
214 "f2" => Some(format!("\x1b[1;{}Q", modifier_code)),
215 "f3" => Some(format!("\x1b[1;{}R", modifier_code)),
216 "f4" => Some(format!("\x1b[1;{}S", modifier_code)),
217 "F5" => Some(format!("\x1b[15;{}~", modifier_code)),
218 "f6" => Some(format!("\x1b[17;{}~", modifier_code)),
219 "f7" => Some(format!("\x1b[18;{}~", modifier_code)),
220 "f8" => Some(format!("\x1b[19;{}~", modifier_code)),
221 "f9" => Some(format!("\x1b[20;{}~", modifier_code)),
222 "f10" => Some(format!("\x1b[21;{}~", modifier_code)),
223 "f11" => Some(format!("\x1b[23;{}~", modifier_code)),
224 "f12" => Some(format!("\x1b[24;{}~", modifier_code)),
225 "f13" => Some(format!("\x1b[25;{}~", modifier_code)),
226 "f14" => Some(format!("\x1b[26;{}~", modifier_code)),
227 "f15" => Some(format!("\x1b[28;{}~", modifier_code)),
228 "f16" => Some(format!("\x1b[29;{}~", modifier_code)),
229 "f17" => Some(format!("\x1b[31;{}~", modifier_code)),
230 "f18" => Some(format!("\x1b[32;{}~", modifier_code)),
231 "f19" => Some(format!("\x1b[33;{}~", modifier_code)),
232 "f20" => Some(format!("\x1b[34;{}~", modifier_code)),
233 _ if modifier_code == 2 => None,
234 "insert" => Some(format!("\x1b[2;{}~", modifier_code)),
235 "pageup" => Some(format!("\x1b[5;{}~", modifier_code)),
236 "pagedown" => Some(format!("\x1b[6;{}~", modifier_code)),
237 "end" => Some(format!("\x1b[1;{}F", modifier_code)),
238 "home" => Some(format!("\x1b[1;{}H", modifier_code)),
239 _ => None,
240 };
241 if modified_esc_str.is_some() {
242 return modified_esc_str;
243 }
244 }
245
246 None
247}
248
249/// Code Modifiers
250/// ---------+---------------------------
251/// 2 | Shift
252/// 3 | Alt
253/// 4 | Shift + Alt
254/// 5 | Control
255/// 6 | Shift + Control
256/// 7 | Alt + Control
257/// 8 | Shift + Alt + Control
258/// ---------+---------------------------
259/// from: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-PC-Style-Function-Keys
260fn modifier_code(keystroke: &Keystroke) -> u32 {
261 let mut modifier_code = 0;
262 if keystroke.shift {
263 modifier_code |= 1;
264 }
265 if keystroke.alt {
266 modifier_code |= 1 << 1;
267 }
268 if keystroke.ctrl {
269 modifier_code |= 1 << 2;
270 }
271 modifier_code + 1
272}
273
274#[cfg(test)]
275mod test {
276 use super::*;
277
278 #[test]
279 fn test_scroll_keys() {
280 //These keys should be handled by the scrolling element directly
281 //Need to signify this by returning 'None'
282 let shift_pageup = Keystroke::parse("shift-pageup").unwrap();
283 let shift_pagedown = Keystroke::parse("shift-pagedown").unwrap();
284 let shift_home = Keystroke::parse("shift-home").unwrap();
285 let shift_end = Keystroke::parse("shift-end").unwrap();
286
287 let none = TermMode::NONE;
288 assert_eq!(to_esc_str(&shift_pageup, &none), None);
289 assert_eq!(to_esc_str(&shift_pagedown, &none), None);
290 assert_eq!(to_esc_str(&shift_home, &none), None);
291 assert_eq!(to_esc_str(&shift_end, &none), None);
292
293 let alt_screen = TermMode::ALT_SCREEN;
294 assert_eq!(
295 to_esc_str(&shift_pageup, &alt_screen),
296 Some("\x1b[5;2~".to_string())
297 );
298 assert_eq!(
299 to_esc_str(&shift_pagedown, &alt_screen),
300 Some("\x1b[6;2~".to_string())
301 );
302 assert_eq!(
303 to_esc_str(&shift_home, &alt_screen),
304 Some("\x1b[1;2H".to_string())
305 );
306 assert_eq!(
307 to_esc_str(&shift_end, &alt_screen),
308 Some("\x1b[1;2F".to_string())
309 );
310
311 let pageup = Keystroke::parse("pageup").unwrap();
312 let pagedown = Keystroke::parse("pagedown").unwrap();
313 let any = TermMode::ANY;
314
315 assert_eq!(to_esc_str(&pageup, &any), Some("\x1b[5~".to_string()));
316 assert_eq!(to_esc_str(&pagedown, &any), Some("\x1b[6~".to_string()));
317 }
318
319 #[test]
320 fn test_plain_inputs() {
321 let ks = Keystroke {
322 ctrl: false,
323 alt: false,
324 shift: false,
325 cmd: false,
326 function: false,
327 key: "🖖🏻".to_string(), //2 char string
328 };
329 assert_eq!(to_esc_str(&ks, &TermMode::NONE), None);
330 }
331
332 #[test]
333 fn test_application_mode() {
334 let app_cursor = TermMode::APP_CURSOR;
335 let none = TermMode::NONE;
336
337 let up = Keystroke::parse("up").unwrap();
338 let down = Keystroke::parse("down").unwrap();
339 let left = Keystroke::parse("left").unwrap();
340 let right = Keystroke::parse("right").unwrap();
341
342 assert_eq!(to_esc_str(&up, &none), Some("\x1b[A".to_string()));
343 assert_eq!(to_esc_str(&down, &none), Some("\x1b[B".to_string()));
344 assert_eq!(to_esc_str(&right, &none), Some("\x1b[C".to_string()));
345 assert_eq!(to_esc_str(&left, &none), Some("\x1b[D".to_string()));
346
347 assert_eq!(to_esc_str(&up, &app_cursor), Some("\x1bOA".to_string()));
348 assert_eq!(to_esc_str(&down, &app_cursor), Some("\x1bOB".to_string()));
349 assert_eq!(to_esc_str(&right, &app_cursor), Some("\x1bOC".to_string()));
350 assert_eq!(to_esc_str(&left, &app_cursor), Some("\x1bOD".to_string()));
351 }
352
353 #[test]
354 fn test_ctrl_codes() {
355 let letters_lower = 'a'..='z';
356 let letters_upper = 'A'..='Z';
357 let mode = TermMode::ANY;
358
359 for (lower, upper) in letters_lower.zip(letters_upper) {
360 assert_eq!(
361 to_esc_str(
362 &Keystroke::parse(&format!("ctrl-{}", lower)).unwrap(),
363 &mode
364 ),
365 to_esc_str(
366 &Keystroke::parse(&format!("ctrl-shift-{}", upper)).unwrap(),
367 &mode
368 ),
369 "On letter: {}/{}",
370 lower,
371 upper
372 )
373 }
374 }
375
376 #[test]
377 fn test_modifier_code_calc() {
378 // Code Modifiers
379 // ---------+---------------------------
380 // 2 | Shift
381 // 3 | Alt
382 // 4 | Shift + Alt
383 // 5 | Control
384 // 6 | Shift + Control
385 // 7 | Alt + Control
386 // 8 | Shift + Alt + Control
387 // ---------+---------------------------
388 // from: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-PC-Style-Function-Keys
389 assert_eq!(2, modifier_code(&Keystroke::parse("shift-A").unwrap()));
390 assert_eq!(3, modifier_code(&Keystroke::parse("alt-A").unwrap()));
391 assert_eq!(4, modifier_code(&Keystroke::parse("shift-alt-A").unwrap()));
392 assert_eq!(5, modifier_code(&Keystroke::parse("ctrl-A").unwrap()));
393 assert_eq!(6, modifier_code(&Keystroke::parse("shift-ctrl-A").unwrap()));
394 assert_eq!(7, modifier_code(&Keystroke::parse("alt-ctrl-A").unwrap()));
395 assert_eq!(
396 8,
397 modifier_code(&Keystroke::parse("shift-ctrl-alt-A").unwrap())
398 );
399 }
400}