table.go 13 KB

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