Debian Codegraph @

input/table.go

raw
1 package input
2
3 import (
4 "maps"
5 "strconv"
6
7 "github.com/charmbracelet/x/ansi"
8 )
9
10 // buildKeysTable builds a table of key sequences and their corresponding key
11 // events based on the VT100/VT200, XTerm, and Urxvt terminal specs.
12 func buildKeysTable(flags int, term string) map[string]Key {
13 nul := Key{Code: KeySpace, Mod: ModCtrl} // ctrl+@ or ctrl+space
14 if flags&FlagCtrlAt != 0 {
15 nul = Key{Code: '@', Mod: ModCtrl}
16 }
17
18 tab := Key{Code: KeyTab} // ctrl+i or tab
19 if flags&FlagCtrlI != 0 {
20 tab = Key{Code: 'i', Mod: ModCtrl}
21 }
22
23 enter := Key{Code: KeyEnter} // ctrl+m or enter
24 if flags&FlagCtrlM != 0 {
25 enter = Key{Code: 'm', Mod: ModCtrl}
26 }
27
28 esc := Key{Code: KeyEscape} // ctrl+[ or escape
29 if flags&FlagCtrlOpenBracket != 0 {
30 esc = Key{Code: '[', Mod: ModCtrl} // ctrl+[ or escape
31 }
32
33 del := Key{Code: KeyBackspace}
34 if flags&FlagBackspace != 0 {
35 del.Code = KeyDelete
36 }
37
38 find := Key{Code: KeyHome}
39 if flags&FlagFind != 0 {
40 find.Code = KeyFind
41 }
42
43 sel := Key{Code: KeyEnd}
44 if flags&FlagSelect != 0 {
45 sel.Code = KeySelect
46 }
47
48 // The following is a table of key sequences and their corresponding key
49 // events based on the VT100/VT200 terminal specs.
50 //
51 // See: https://vt100.net/docs/vt100-ug/chapter3.html#S3.2
52 // See: https://vt100.net/docs/vt220-rm/chapter3.html
53 //
54 // XXX: These keys may be overwritten by other options like XTerm or
55 // Terminfo.
56 table := map[string]Key{
57 // C0 control characters
58 string(byte(ansi.NUL)): nul,
59 string(byte(ansi.SOH)): {Code: 'a', Mod: ModCtrl},
60 string(byte(ansi.STX)): {Code: 'b', Mod: ModCtrl},
61 string(byte(ansi.ETX)): {Code: 'c', Mod: ModCtrl},
62 string(byte(ansi.EOT)): {Code: 'd', Mod: ModCtrl},
63 string(byte(ansi.ENQ)): {Code: 'e', Mod: ModCtrl},
64 string(byte(ansi.ACK)): {Code: 'f', Mod: ModCtrl},
65 string(byte(ansi.BEL)): {Code: 'g', Mod: ModCtrl},
66 string(byte(ansi.BS)): {Code: 'h', Mod: ModCtrl},
67 string(byte(ansi.HT)): tab,
68 string(byte(ansi.LF)): {Code: 'j', Mod: ModCtrl},
69 string(byte(ansi.VT)): {Code: 'k', Mod: ModCtrl},
70 string(byte(ansi.FF)): {Code: 'l', Mod: ModCtrl},
71 string(byte(ansi.CR)): enter,
72 string(byte(ansi.SO)): {Code: 'n', Mod: ModCtrl},
73 string(byte(ansi.SI)): {Code: 'o', Mod: ModCtrl},
74 string(byte(ansi.DLE)): {Code: 'p', Mod: ModCtrl},
75 string(byte(ansi.DC1)): {Code: 'q', Mod: ModCtrl},
76 string(byte(ansi.DC2)): {Code: 'r', Mod: ModCtrl},
77 string(byte(ansi.DC3)): {Code: 's', Mod: ModCtrl},
78 string(byte(ansi.DC4)): {Code: 't', Mod: ModCtrl},
79 string(byte(ansi.NAK)): {Code: 'u', Mod: ModCtrl},
80 string(byte(ansi.SYN)): {Code: 'v', Mod: ModCtrl},
81 string(byte(ansi.ETB)): {Code: 'w', Mod: ModCtrl},
82 string(byte(ansi.CAN)): {Code: 'x', Mod: ModCtrl},
83 string(byte(ansi.EM)): {Code: 'y', Mod: ModCtrl},
84 string(byte(ansi.SUB)): {Code: 'z', Mod: ModCtrl},
85 string(byte(ansi.ESC)): esc,
86 string(byte(ansi.FS)): {Code: '\\', Mod: ModCtrl},
87 string(byte(ansi.GS)): {Code: ']', Mod: ModCtrl},
88 string(byte(ansi.RS)): {Code: '^', Mod: ModCtrl},
89 string(byte(ansi.US)): {Code: '_', Mod: ModCtrl},
90
91 // Special keys in G0
92 string(byte(ansi.SP)): {Code: KeySpace, Text: " "},
93 string(byte(ansi.DEL)): del,
94
95 // Special keys
96
97 "\x1b[Z": {Code: KeyTab, Mod: ModShift},
98
99 "\x1b[1~": find,
100 "\x1b[2~": {Code: KeyInsert},
101 "\x1b[3~": {Code: KeyDelete},
102 "\x1b[4~": sel,
103 "\x1b[5~": {Code: KeyPgUp},
104 "\x1b[6~": {Code: KeyPgDown},
105 "\x1b[7~": {Code: KeyHome},
106 "\x1b[8~": {Code: KeyEnd},
107
108 // Normal mode
109 "\x1b[A": {Code: KeyUp},
110 "\x1b[B": {Code: KeyDown},
111 "\x1b[C": {Code: KeyRight},
112 "\x1b[D": {Code: KeyLeft},
113 "\x1b[E": {Code: KeyBegin},
114 "\x1b[F": {Code: KeyEnd},
115 "\x1b[H": {Code: KeyHome},
116 "\x1b[P": {Code: KeyF1},
117 "\x1b[Q": {Code: KeyF2},
118 "\x1b[R": {Code: KeyF3},
119 "\x1b[S": {Code: KeyF4},
120
121 // Application Cursor Key Mode (DECCKM)
122 "\x1bOA": {Code: KeyUp},
123 "\x1bOB": {Code: KeyDown},
124 "\x1bOC": {Code: KeyRight},
125 "\x1bOD": {Code: KeyLeft},
126 "\x1bOE": {Code: KeyBegin},
127 "\x1bOF": {Code: KeyEnd},
128 "\x1bOH": {Code: KeyHome},
129 "\x1bOP": {Code: KeyF1},
130 "\x1bOQ": {Code: KeyF2},
131 "\x1bOR": {Code: KeyF3},
132 "\x1bOS": {Code: KeyF4},
133
134 // Keypad Application Mode (DECKPAM)
135
136 "\x1bOM": {Code: KeyKpEnter},
137 "\x1bOX": {Code: KeyKpEqual},
138 "\x1bOj": {Code: KeyKpMultiply},
139 "\x1bOk": {Code: KeyKpPlus},
140 "\x1bOl": {Code: KeyKpComma},
141 "\x1bOm": {Code: KeyKpMinus},
142 "\x1bOn": {Code: KeyKpDecimal},
143 "\x1bOo": {Code: KeyKpDivide},
144 "\x1bOp": {Code: KeyKp0},
145 "\x1bOq": {Code: KeyKp1},
146 "\x1bOr": {Code: KeyKp2},
147 "\x1bOs": {Code: KeyKp3},
148 "\x1bOt": {Code: KeyKp4},
149 "\x1bOu": {Code: KeyKp5},
150 "\x1bOv": {Code: KeyKp6},
151 "\x1bOw": {Code: KeyKp7},
152 "\x1bOx": {Code: KeyKp8},
153 "\x1bOy": {Code: KeyKp9},
154
155 // Function keys
156
157 "\x1b[11~": {Code: KeyF1},
158 "\x1b[12~": {Code: KeyF2},
159 "\x1b[13~": {Code: KeyF3},
160 "\x1b[14~": {Code: KeyF4},
161 "\x1b[15~": {Code: KeyF5},
162 "\x1b[17~": {Code: KeyF6},
163 "\x1b[18~": {Code: KeyF7},
164 "\x1b[19~": {Code: KeyF8},
165 "\x1b[20~": {Code: KeyF9},
166 "\x1b[21~": {Code: KeyF10},
167 "\x1b[23~": {Code: KeyF11},
168 "\x1b[24~": {Code: KeyF12},
169 "\x1b[25~": {Code: KeyF13},
170 "\x1b[26~": {Code: KeyF14},
171 "\x1b[28~": {Code: KeyF15},
172 "\x1b[29~": {Code: KeyF16},
173 "\x1b[31~": {Code: KeyF17},
174 "\x1b[32~": {Code: KeyF18},
175 "\x1b[33~": {Code: KeyF19},
176 "\x1b[34~": {Code: KeyF20},
177 }
178
179 // CSI ~ sequence keys
180 csiTildeKeys := map[string]Key{
181 "1": find, "2": {Code: KeyInsert},
182 "3": {Code: KeyDelete}, "4": sel,
183 "5": {Code: KeyPgUp}, "6": {Code: KeyPgDown},
184 "7": {Code: KeyHome}, "8": {Code: KeyEnd},
185 // There are no 9 and 10 keys
186 "11": {Code: KeyF1}, "12": {Code: KeyF2},
187 "13": {Code: KeyF3}, "14": {Code: KeyF4},
188 "15": {Code: KeyF5}, "17": {Code: KeyF6},
189 "18": {Code: KeyF7}, "19": {Code: KeyF8},
190 "20": {Code: KeyF9}, "21": {Code: KeyF10},
191 "23": {Code: KeyF11}, "24": {Code: KeyF12},
192 "25": {Code: KeyF13}, "26": {Code: KeyF14},
193 "28": {Code: KeyF15}, "29": {Code: KeyF16},
194 "31": {Code: KeyF17}, "32": {Code: KeyF18},
195 "33": {Code: KeyF19}, "34": {Code: KeyF20},
196 }
197
198 // URxvt keys
199 // See https://manpages.ubuntu.com/manpages/trusty/man7/urxvt.7.html#key%20codes
200 table["\x1b[a"] = Key{Code: KeyUp, Mod: ModShift}
201 table["\x1b[b"] = Key{Code: KeyDown, Mod: ModShift}
202 table["\x1b[c"] = Key{Code: KeyRight, Mod: ModShift}
203 table["\x1b[d"] = Key{Code: KeyLeft, Mod: ModShift}
204 table["\x1bOa"] = Key{Code: KeyUp, Mod: ModCtrl}
205 table["\x1bOb"] = Key{Code: KeyDown, Mod: ModCtrl}
206 table["\x1bOc"] = Key{Code: KeyRight, Mod: ModCtrl}
207 table["\x1bOd"] = Key{Code: KeyLeft, Mod: ModCtrl}
208 //nolint:godox
209 // TODO: invistigate if shift-ctrl arrow keys collide with DECCKM keys i.e.
210 // "\x1bOA", "\x1bOB", "\x1bOC", "\x1bOD"
211
212 // URxvt modifier CSI ~ keys
213 for k, v := range csiTildeKeys {
214 key := v
215 // Normal (no modifier) already defined part of VT100/VT200
216 // Shift modifier
217 key.Mod = ModShift
218 table["\x1b["+k+"$"] = key
219 // Ctrl modifier
220 key.Mod = ModCtrl
221 table["\x1b["+k+"^"] = key
222 // Shift-Ctrl modifier
223 key.Mod = ModShift | ModCtrl
224 table["\x1b["+k+"@"] = key
225 }
226
227 // URxvt F keys
228 // Note: Shift + F1-F10 generates F11-F20.
229 // This means Shift + F1 and Shift + F2 will generate F11 and F12, the same
230 // applies to Ctrl + Shift F1 & F2.
231 //
232 // P.S. Don't like this? Blame URxvt, configure your terminal to use
233 // different escapes like XTerm, or switch to a better terminal ¯\_(ツ)_/¯
234 //
235 // See https://manpages.ubuntu.com/manpages/trusty/man7/urxvt.7.html#key%20codes
236 table["\x1b[23$"] = Key{Code: KeyF11, Mod: ModShift}
237 table["\x1b[24$"] = Key{Code: KeyF12, Mod: ModShift}
238 table["\x1b[25$"] = Key{Code: KeyF13, Mod: ModShift}
239 table["\x1b[26$"] = Key{Code: KeyF14, Mod: ModShift}
240 table["\x1b[28$"] = Key{Code: KeyF15, Mod: ModShift}
241 table["\x1b[29$"] = Key{Code: KeyF16, Mod: ModShift}
242 table["\x1b[31$"] = Key{Code: KeyF17, Mod: ModShift}
243 table["\x1b[32$"] = Key{Code: KeyF18, Mod: ModShift}
244 table["\x1b[33$"] = Key{Code: KeyF19, Mod: ModShift}
245 table["\x1b[34$"] = Key{Code: KeyF20, Mod: ModShift}
246 table["\x1b[11^"] = Key{Code: KeyF1, Mod: ModCtrl}
247 table["\x1b[12^"] = Key{Code: KeyF2, Mod: ModCtrl}
248 table["\x1b[13^"] = Key{Code: KeyF3, Mod: ModCtrl}
249 table["\x1b[14^"] = Key{Code: KeyF4, Mod: ModCtrl}
250 table["\x1b[15^"] = Key{Code: KeyF5, Mod: ModCtrl}
251 table["\x1b[17^"] = Key{Code: KeyF6, Mod: ModCtrl}
252 table["\x1b[18^"] = Key{Code: KeyF7, Mod: ModCtrl}
253 table["\x1b[19^"] = Key{Code: KeyF8, Mod: ModCtrl}
254 table["\x1b[20^"] = Key{Code: KeyF9, Mod: ModCtrl}
255 table["\x1b[21^"] = Key{Code: KeyF10, Mod: ModCtrl}
256 table["\x1b[23^"] = Key{Code: KeyF11, Mod: ModCtrl}
257 table["\x1b[24^"] = Key{Code: KeyF12, Mod: ModCtrl}
258 table["\x1b[25^"] = Key{Code: KeyF13, Mod: ModCtrl}
259 table["\x1b[26^"] = Key{Code: KeyF14, Mod: ModCtrl}
260 table["\x1b[28^"] = Key{Code: KeyF15, Mod: ModCtrl}
261 table["\x1b[29^"] = Key{Code: KeyF16, Mod: ModCtrl}
262 table["\x1b[31^"] = Key{Code: KeyF17, Mod: ModCtrl}
263 table["\x1b[32^"] = Key{Code: KeyF18, Mod: ModCtrl}
264 table["\x1b[33^"] = Key{Code: KeyF19, Mod: ModCtrl}
265 table["\x1b[34^"] = Key{Code: KeyF20, Mod: ModCtrl}
266 table["\x1b[23@"] = Key{Code: KeyF11, Mod: ModShift | ModCtrl}
267 table["\x1b[24@"] = Key{Code: KeyF12, Mod: ModShift | ModCtrl}
268 table["\x1b[25@"] = Key{Code: KeyF13, Mod: ModShift | ModCtrl}
269 table["\x1b[26@"] = Key{Code: KeyF14, Mod: ModShift | ModCtrl}
270 table["\x1b[28@"] = Key{Code: KeyF15, Mod: ModShift | ModCtrl}
271 table["\x1b[29@"] = Key{Code: KeyF16, Mod: ModShift | ModCtrl}
272 table["\x1b[31@"] = Key{Code: KeyF17, Mod: ModShift | ModCtrl}
273 table["\x1b[32@"] = Key{Code: KeyF18, Mod: ModShift | ModCtrl}
274 table["\x1b[33@"] = Key{Code: KeyF19, Mod: ModShift | ModCtrl}
275 table["\x1b[34@"] = Key{Code: KeyF20, Mod: ModShift | ModCtrl}
276
277 // Register Alt + <key> combinations
278 // XXX: this must come after URxvt but before XTerm keys to register URxvt
279 // keys with alt modifier
280 tmap := map[string]Key{}
281 for seq, key := range table {
282 key := key
283 key.Mod |= ModAlt
284 key.Text = "" // Clear runes
285 tmap["\x1b"+seq] = key
286 }
287 maps.Copy(table, tmap)
288
289 // XTerm modifiers
290 // These are offset by 1 to be compatible with our Mod type.
291 // See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-PC-Style-Function-Keys
292 modifiers := []KeyMod{
293 ModShift, // 1
294 ModAlt, // 2
295 ModShift | ModAlt, // 3
296 ModCtrl, // 4
297 ModShift | ModCtrl, // 5
298 ModAlt | ModCtrl, // 6
299 ModShift | ModAlt | ModCtrl, // 7
300 ModMeta, // 8
301 ModMeta | ModShift, // 9
302 ModMeta | ModAlt, // 10
303 ModMeta | ModShift | ModAlt, // 11
304 ModMeta | ModCtrl, // 12
305 ModMeta | ModShift | ModCtrl, // 13
306 ModMeta | ModAlt | ModCtrl, // 14
307 ModMeta | ModShift | ModAlt | ModCtrl, // 15
308 }
309
310 // SS3 keypad function keys
311 ss3FuncKeys := map[string]Key{
312 // These are defined in XTerm
313 // Taken from Foot keymap.h and XTerm modifyOtherKeys
314 // https://codeberg.org/dnkl/foot/src/branch/master/keymap.h
315 "M": {Code: KeyKpEnter}, "X": {Code: KeyKpEqual},
316 "j": {Code: KeyKpMultiply}, "k": {Code: KeyKpPlus},
317 "l": {Code: KeyKpComma}, "m": {Code: KeyKpMinus},
318 "n": {Code: KeyKpDecimal}, "o": {Code: KeyKpDivide},
319 "p": {Code: KeyKp0}, "q": {Code: KeyKp1},
320 "r": {Code: KeyKp2}, "s": {Code: KeyKp3},
321 "t": {Code: KeyKp4}, "u": {Code: KeyKp5},
322 "v": {Code: KeyKp6}, "w": {Code: KeyKp7},
323 "x": {Code: KeyKp8}, "y": {Code: KeyKp9},
324 }
325
326 // XTerm keys
327 csiFuncKeys := map[string]Key{
328 "A": {Code: KeyUp}, "B": {Code: KeyDown},
329 "C": {Code: KeyRight}, "D": {Code: KeyLeft},
330 "E": {Code: KeyBegin}, "F": {Code: KeyEnd},
331 "H": {Code: KeyHome}, "P": {Code: KeyF1},
332 "Q": {Code: KeyF2}, "R": {Code: KeyF3},
333 "S": {Code: KeyF4},
334 }
335
336 // CSI 27 ; <modifier> ; <code> ~ keys defined in XTerm modifyOtherKeys
337 modifyOtherKeys := map[int]Key{
338 ansi.BS: {Code: KeyBackspace},
339 ansi.HT: {Code: KeyTab},
340 ansi.CR: {Code: KeyEnter},
341 ansi.ESC: {Code: KeyEscape},
342 ansi.DEL: {Code: KeyBackspace},
343 }
344
345 for _, m := range modifiers {
346 // XTerm modifier offset +1
347 xtermMod := strconv.Itoa(int(m) + 1)
348
349 // CSI 1 ; <modifier> <func>
350 for k, v := range csiFuncKeys {
351 // Functions always have a leading 1 param
352 seq := "\x1b[1;" + xtermMod + k
353 key := v
354 key.Mod = m
355 table[seq] = key
356 }
357 // SS3 <modifier> <func>
358 for k, v := range ss3FuncKeys {
359 seq := "\x1bO" + xtermMod + k
360 key := v
361 key.Mod = m
362 table[seq] = key
363 }
364 // CSI <number> ; <modifier> ~
365 for k, v := range csiTildeKeys {
366 seq := "\x1b[" + k + ";" + xtermMod + "~"
367 key := v
368 key.Mod = m
369 table[seq] = key
370 }
371 // CSI 27 ; <modifier> ; <code> ~
372 for k, v := range modifyOtherKeys {
373 code := strconv.Itoa(k)
374 seq := "\x1b[27;" + xtermMod + ";" + code + "~"
375 key := v
376 key.Mod = m
377 table[seq] = key
378 }
379 }
380
381 // Register terminfo keys
382 // XXX: this might override keys already registered in table
383 if flags&FlagTerminfo != 0 {
384 titable := buildTerminfoKeys(flags, term)
385 maps.Copy(table, titable)
386 }
387
388 return table
389 }