keymap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. #include <stdbool.h>
  2. #include "nvim/types.h"
  3. #include "nvim/keycodes.h"
  4. #include "nvim/ascii.h"
  5. #include "nvim/eval/typval.h"
  6. #define MOD_KEYS_ENTRY_SIZE 5
  7. static char_u modifier_keys_table[] =
  8. {
  9. MOD_MASK_SHIFT, '&', '9', '@', '1',
  10. MOD_MASK_SHIFT, '&', '0', '@', '2',
  11. MOD_MASK_SHIFT, '*', '1', '@', '4',
  12. MOD_MASK_SHIFT, '*', '2', '@', '5',
  13. MOD_MASK_SHIFT, '*', '3', '@', '6',
  14. MOD_MASK_SHIFT, '*', '4', 'k', 'D',
  15. MOD_MASK_SHIFT, '*', '5', 'k', 'L',
  16. MOD_MASK_SHIFT, '*', '7', '@', '7',
  17. MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_END, '@', '7',
  18. MOD_MASK_SHIFT, '*', '9', '@', '9',
  19. MOD_MASK_SHIFT, '*', '0', '@', '0',
  20. MOD_MASK_SHIFT, '#', '1', '%', '1',
  21. MOD_MASK_SHIFT, '#', '2', 'k', 'h',
  22. MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_HOME, 'k', 'h',
  23. MOD_MASK_SHIFT, '#', '3', 'k', 'I',
  24. MOD_MASK_SHIFT, '#', '4', 'k', 'l',
  25. MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_LEFT, 'k', 'l',
  26. MOD_MASK_SHIFT, '%', 'a', '%', '3',
  27. MOD_MASK_SHIFT, '%', 'b', '%', '4',
  28. MOD_MASK_SHIFT, '%', 'c', '%', '5',
  29. MOD_MASK_SHIFT, '%', 'd', '%', '7',
  30. MOD_MASK_SHIFT, '%', 'e', '%', '8',
  31. MOD_MASK_SHIFT, '%', 'f', '%', '9',
  32. MOD_MASK_SHIFT, '%', 'g', '%', '0',
  33. MOD_MASK_SHIFT, '%', 'h', '&', '3',
  34. MOD_MASK_SHIFT, '%', 'i', 'k', 'r',
  35. MOD_MASK_CTRL, KS_EXTRA, (int)KE_C_RIGHT, 'k', 'r',
  36. MOD_MASK_SHIFT, '%', 'j', '&', '5',
  37. MOD_MASK_SHIFT, '!', '1', '&', '6',
  38. MOD_MASK_SHIFT, '!', '2', '&', '7',
  39. MOD_MASK_SHIFT, '!', '3', '&', '8',
  40. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_UP, 'k', 'u',
  41. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_DOWN, 'k', 'd',
  42. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF1, KS_EXTRA, (int)KE_XF1,
  43. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF2, KS_EXTRA, (int)KE_XF2,
  44. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF3, KS_EXTRA, (int)KE_XF3,
  45. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_XF4, KS_EXTRA, (int)KE_XF4,
  46. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F1, 'k', '1',
  47. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F2, 'k', '2',
  48. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F3, 'k', '3',
  49. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F4, 'k', '4',
  50. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F5, 'k', '5',
  51. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F6, 'k', '6',
  52. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F7, 'k', '7',
  53. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F8, 'k', '8',
  54. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F9, 'k', '9',
  55. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F10, 'k', ';',
  56. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F11, 'F', '1',
  57. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F12, 'F', '2',
  58. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F13, 'F', '3',
  59. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F14, 'F', '4',
  60. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F15, 'F', '5',
  61. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F16, 'F', '6',
  62. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F17, 'F', '7',
  63. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F18, 'F', '8',
  64. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F19, 'F', '9',
  65. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F20, 'F', 'A',
  66. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F21, 'F', 'B',
  67. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F22, 'F', 'C',
  68. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F23, 'F', 'D',
  69. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F24, 'F', 'E',
  70. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F25, 'F', 'F',
  71. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F26, 'F', 'G',
  72. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F27, 'F', 'H',
  73. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F28, 'F', 'I',
  74. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F29, 'F', 'J',
  75. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F30, 'F', 'K',
  76. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F31, 'F', 'L',
  77. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F32, 'F', 'M',
  78. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F33, 'F', 'N',
  79. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F34, 'F', 'O',
  80. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F35, 'F', 'P',
  81. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F36, 'F', 'Q',
  82. MOD_MASK_SHIFT, KS_EXTRA, (int)KE_S_F37, 'F', 'R',
  83. MOD_MASK_SHIFT, 'k', 'B', KS_EXTRA, (int)KE_TAB,
  84. NUL
  85. };
  86. int simplify_key(const int key, int *modifiers)
  87. {
  88. if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT)) {
  89. // TAB is a special case.
  90. if (key == TAB && (*modifiers & MOD_MASK_SHIFT)) {
  91. *modifiers &= ~MOD_MASK_SHIFT;
  92. return K_S_TAB;
  93. }
  94. const int key0 = KEY2TERMCAP0(key);
  95. const int key1 = KEY2TERMCAP1(key);
  96. for (int i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE) {
  97. if (key0 == modifier_keys_table[i + 3]
  98. && key1 == modifier_keys_table[i + 4]
  99. && (*modifiers & modifier_keys_table[i])) {
  100. *modifiers &= ~modifier_keys_table[i];
  101. return TERMCAP2KEY(modifier_keys_table[i + 1],
  102. modifier_keys_table[i + 2]);
  103. }
  104. }
  105. }
  106. return key;
  107. }
  108. int handle_x_keys(const int key)
  109. FUNC_ATTR_CONST FUNC_ATTR_WARN_UNUSED_RESULT
  110. {
  111. switch (key) {
  112. case K_XUP: return K_UP;
  113. case K_XDOWN: return K_DOWN;
  114. case K_XLEFT: return K_LEFT;
  115. case K_XRIGHT: return K_RIGHT;
  116. case K_XHOME: return K_HOME;
  117. case K_ZHOME: return K_HOME;
  118. case K_XEND: return K_END;
  119. case K_ZEND: return K_END;
  120. case K_XF1: return K_F1;
  121. case K_XF2: return K_F2;
  122. case K_XF3: return K_F3;
  123. case K_XF4: return K_F4;
  124. case K_S_XF1: return K_S_F1;
  125. case K_S_XF2: return K_S_F2;
  126. case K_S_XF3: return K_S_F3;
  127. case K_S_XF4: return K_S_F4;
  128. }
  129. return key;
  130. }
  131. static const struct key_name_entry {
  132. int key; // Special key code or ascii value
  133. const char *name; // Name of key
  134. } key_names_table[] = {
  135. { ' ', "Space" },
  136. { TAB, "Tab" },
  137. { K_TAB, "Tab" },
  138. { NL, "NL" },
  139. { NL, "NewLine" }, // Alternative name
  140. { NL, "LineFeed" }, // Alternative name
  141. { NL, "LF" }, // Alternative name
  142. { CAR, "CR" },
  143. { CAR, "Return" }, // Alternative name
  144. { CAR, "Enter" }, // Alternative name
  145. { K_BS, "BS" },
  146. { K_BS, "BackSpace" }, // Alternative name
  147. { ESC, "Esc" },
  148. { CSI, "CSI" },
  149. { K_CSI, "xCSI" },
  150. { '|', "Bar" },
  151. { '\\', "Bslash" },
  152. { K_DEL, "Del" },
  153. { K_DEL, "Delete" }, // Alternative name
  154. { K_KDEL, "kDel" },
  155. { K_KDEL, "KPPeriod" }, // termkey KPPeriod value
  156. { K_UP, "Up" },
  157. { K_DOWN, "Down" },
  158. { K_LEFT, "Left" },
  159. { K_RIGHT, "Right" },
  160. { K_XUP, "xUp" },
  161. { K_XDOWN, "xDown" },
  162. { K_XLEFT, "xLeft" },
  163. { K_XRIGHT, "xRight" },
  164. { K_KUP, "kUp" },
  165. { K_KUP, "KP8" },
  166. { K_KDOWN, "kDown" },
  167. { K_KDOWN, "KP2" },
  168. { K_KLEFT, "kLeft" },
  169. { K_KLEFT, "KP4" },
  170. { K_KRIGHT, "kRight" },
  171. { K_KRIGHT, "KP6" },
  172. { K_F1, "F1" },
  173. { K_F2, "F2" },
  174. { K_F3, "F3" },
  175. { K_F4, "F4" },
  176. { K_F5, "F5" },
  177. { K_F6, "F6" },
  178. { K_F7, "F7" },
  179. { K_F8, "F8" },
  180. { K_F9, "F9" },
  181. { K_F10, "F10" },
  182. { K_F11, "F11" },
  183. { K_F12, "F12" },
  184. { K_F13, "F13" },
  185. { K_F14, "F14" },
  186. { K_F15, "F15" },
  187. { K_F16, "F16" },
  188. { K_F17, "F17" },
  189. { K_F18, "F18" },
  190. { K_F19, "F19" },
  191. { K_F20, "F20" },
  192. { K_F21, "F21" },
  193. { K_F22, "F22" },
  194. { K_F23, "F23" },
  195. { K_F24, "F24" },
  196. { K_F25, "F25" },
  197. { K_F26, "F26" },
  198. { K_F27, "F27" },
  199. { K_F28, "F28" },
  200. { K_F29, "F29" },
  201. { K_F30, "F30" },
  202. { K_F31, "F31" },
  203. { K_F32, "F32" },
  204. { K_F33, "F33" },
  205. { K_F34, "F34" },
  206. { K_F35, "F35" },
  207. { K_F36, "F36" },
  208. { K_F37, "F37" },
  209. { K_XF1, "xF1" },
  210. { K_XF2, "xF2" },
  211. { K_XF3, "xF3" },
  212. { K_XF4, "xF4" },
  213. { K_HELP, "Help" },
  214. { K_UNDO, "Undo" },
  215. { K_INS, "Insert" },
  216. { K_INS, "Ins" }, // Alternative name
  217. { K_KINS, "kInsert" },
  218. { K_KINS, "KP0" },
  219. { K_HOME, "Home" },
  220. { K_KHOME, "kHome" },
  221. { K_KHOME, "KP7" },
  222. { K_XHOME, "xHome" },
  223. { K_ZHOME, "zHome" },
  224. { K_END, "End" },
  225. { K_KEND, "kEnd" },
  226. { K_XEND, "xEnd" },
  227. { K_ZEND, "zEnd" },
  228. { K_PAGEUP, "PageUp" },
  229. { K_PAGEDOWN, "PageDown" },
  230. { K_KPAGEUP, "kPageUp" },
  231. { K_KPAGEUP, "KP9" },
  232. { K_KPAGEDOWN, "kPageDown" },
  233. { K_KPAGEDOWN, "KP3" },
  234. { K_KORIGIN, "kOrigin" },
  235. { K_KORIGIN, "KP5" },
  236. { K_KPLUS, "kPlus" },
  237. { K_KPLUS, "KPPlus" },
  238. { K_KMINUS, "kMinus" },
  239. { K_KMINUS, "KPMinus" },
  240. { K_KDIVIDE, "kDivide" },
  241. { K_KDIVIDE, "KPDiv" },
  242. { K_KMULTIPLY, "kMultiply" },
  243. { K_KMULTIPLY, "KPMult" },
  244. { K_KENTER, "kEnter" },
  245. { K_KENTER, "KPEnter" },
  246. { K_KPOINT, "kPoint" },
  247. { K_K0, "k0" },
  248. { K_K1, "k1" },
  249. { K_K2, "k2" },
  250. { K_K3, "k3" },
  251. { K_K4, "k4" },
  252. { K_K5, "k5" },
  253. { K_K6, "k6" },
  254. { K_K7, "k7" },
  255. { K_K8, "k8" },
  256. { K_K9, "k9" },
  257. { '<', "lt" },
  258. { K_MOUSE, "Mouse" },
  259. { K_LEFTMOUSE, "LeftMouse" },
  260. { K_LEFTMOUSE_NM, "LeftMouseNM" },
  261. { K_LEFTDRAG, "LeftDrag" },
  262. { K_LEFTRELEASE, "LeftRelease" },
  263. { K_LEFTRELEASE_NM, "LeftReleaseNM" },
  264. { K_MIDDLEMOUSE, "MiddleMouse" },
  265. { K_MIDDLEDRAG, "MiddleDrag" },
  266. { K_MIDDLERELEASE, "MiddleRelease" },
  267. { K_RIGHTMOUSE, "RightMouse" },
  268. { K_RIGHTDRAG, "RightDrag" },
  269. { K_RIGHTRELEASE, "RightRelease" },
  270. { K_MOUSEDOWN, "ScrollWheelUp" },
  271. { K_MOUSEUP, "ScrollWheelDown" },
  272. { K_MOUSELEFT, "ScrollWheelRight" },
  273. { K_MOUSERIGHT, "ScrollWheelLeft" },
  274. { K_MOUSEDOWN, "MouseDown" }, // OBSOLETE: Use
  275. { K_MOUSEUP, "MouseUp" }, // ScrollWheelXXX instead
  276. { K_X1MOUSE, "X1Mouse" },
  277. { K_X1DRAG, "X1Drag" },
  278. { K_X1RELEASE, "X1Release" },
  279. { K_X2MOUSE, "X2Mouse" },
  280. { K_X2DRAG, "X2Drag" },
  281. { K_X2RELEASE, "X2Release" },
  282. { K_DROP, "Drop" },
  283. { K_ZERO, "Nul" },
  284. { K_SNR, "SNR" },
  285. { K_PLUG, "Plug" },
  286. { K_PASTE, "Paste" },
  287. { 0, NULL }
  288. };
  289. int get_special_key_code(const char_u *name)
  290. {
  291. for (int i = 0; key_names_table[i].name != NULL; i++) {
  292. const char *const table_name = key_names_table[i].name;
  293. int j;
  294. for (j = 0; ascii_isident(name[j]) && table_name[j] != NUL; j++) {
  295. if (TOLOWER_ASC(table_name[j]) != TOLOWER_ASC(name[j])) {
  296. break;
  297. }
  298. }
  299. if (!ascii_isident(name[j]) && table_name[j] == NUL) {
  300. return key_names_table[i].key;
  301. }
  302. }
  303. return 0;
  304. }
  305. static const struct modmasktable {
  306. short mod_mask; ///< Bit-mask for particular key modifier.
  307. short mod_flag; ///< Bit(s) for particular key modifier.
  308. char_u name; ///< Single letter name of modifier.
  309. } mod_mask_table[] = {
  310. {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'M'},
  311. {MOD_MASK_META, MOD_MASK_META, (char_u)'T'},
  312. {MOD_MASK_CTRL, MOD_MASK_CTRL, (char_u)'C'},
  313. {MOD_MASK_SHIFT, MOD_MASK_SHIFT, (char_u)'S'},
  314. {MOD_MASK_MULTI_CLICK, MOD_MASK_2CLICK, (char_u)'2'},
  315. {MOD_MASK_MULTI_CLICK, MOD_MASK_3CLICK, (char_u)'3'},
  316. {MOD_MASK_MULTI_CLICK, MOD_MASK_4CLICK, (char_u)'4'},
  317. {MOD_MASK_CMD, MOD_MASK_CMD, (char_u)'D'},
  318. // 'A' must be the last one
  319. {MOD_MASK_ALT, MOD_MASK_ALT, (char_u)'A'},
  320. {0, 0, NUL}
  321. };
  322. int name_to_mod_mask(int c)
  323. {
  324. c = TOUPPER_ASC(c);
  325. for (size_t i = 0; mod_mask_table[i].mod_mask != 0; i++) {
  326. if (c == mod_mask_table[i].name) {
  327. return mod_mask_table[i].mod_flag;
  328. }
  329. }
  330. return 0;
  331. }
  332. static int extract_modifiers(int key, int *modp)
  333. {
  334. int modifiers = *modp;
  335. if (!(modifiers & MOD_MASK_CMD)) { // Command-key is special
  336. if ((modifiers & MOD_MASK_SHIFT) && ASCII_ISALPHA(key)) {
  337. key = TOUPPER_ASC(key);
  338. modifiers &= ~MOD_MASK_SHIFT;
  339. }
  340. }
  341. if ((modifiers & MOD_MASK_CTRL)
  342. && ((key >= '?' && key <= '_') || ASCII_ISALPHA(key))) {
  343. key = Ctrl_chr(key);
  344. modifiers &= ~MOD_MASK_CTRL;
  345. if (key == 0) { // <C-@> is <Nul>
  346. key = K_ZERO;
  347. }
  348. }
  349. *modp = modifiers;
  350. return key;
  351. }
  352. int find_special_key(const char_u **srcp, const size_t src_len, int *const modp,
  353. const bool keycode, const bool keep_x_key,
  354. const bool in_string)
  355. {
  356. const char_u *last_dash;
  357. const char_u *end_of_name;
  358. const char_u *src;
  359. const char_u *bp;
  360. const char_u *const end = *srcp + src_len - 1;
  361. int modifiers;
  362. int bit;
  363. int key;
  364. uvarnumber_T n;
  365. int l;
  366. if (src_len == 0) {
  367. return 0;
  368. }
  369. src = *srcp;
  370. if (src[0] != '<') {
  371. return 0;
  372. }
  373. // Find end of modifier list
  374. last_dash = src;
  375. for (bp = src + 1; bp <= end && (*bp == '-' || ascii_isident(*bp)); bp++) {
  376. if (*bp == '-') {
  377. last_dash = bp;
  378. if (bp + 1 <= end) {
  379. l = utfc_ptr2len_len(bp + 1, (int)(end - bp) + 1);
  380. // Anything accepted, like <C-?>.
  381. // <C-"> or <M-"> are not special in strings as " is
  382. // the string delimiter. With a backslash it works: <M-\">
  383. if (end - bp > l && !(in_string && bp[1] == '"') && bp[2] == '>') {
  384. bp += l;
  385. } else if (end - bp > 2 && in_string && bp[1] == '\\'
  386. && bp[2] == '"' && bp[3] == '>') {
  387. bp += 2;
  388. }
  389. }
  390. }
  391. if (end - bp > 3 && bp[0] == 't' && bp[1] == '_') {
  392. bp += 3; // skip t_xx, xx may be '-' or '>'
  393. } else if (end - bp > 4 && STRNICMP(bp, "char-", 5) == 0) {
  394. vim_str2nr(bp + 5, NULL, &l, STR2NR_ALL, NULL, NULL, 0);
  395. bp += l + 5;
  396. break;
  397. }
  398. }
  399. if (bp <= end && *bp == '>') { // found matching '>'
  400. end_of_name = bp + 1;
  401. /* Which modifiers are given? */
  402. modifiers = 0x0;
  403. for (bp = src + 1; bp < last_dash; bp++) {
  404. if (*bp != '-') {
  405. bit = name_to_mod_mask(*bp);
  406. if (bit == 0x0) {
  407. break; // Illegal modifier name
  408. }
  409. modifiers |= bit;
  410. }
  411. }
  412. // Legal modifier name.
  413. if (bp >= last_dash) {
  414. if (STRNICMP(last_dash + 1, "char-", 5) == 0
  415. && ascii_isdigit(last_dash[6])) {
  416. // <Char-123> or <Char-033> or <Char-0x33>
  417. vim_str2nr(last_dash + 6, NULL, NULL, STR2NR_ALL, NULL, &n, 0);
  418. key = (int)n;
  419. } else {
  420. int off = 1;
  421. // Modifier with single letter, or special key name.
  422. if (in_string && last_dash[1] == '\\' && last_dash[2] == '"') {
  423. off = 2;
  424. }
  425. l = mb_ptr2len(last_dash + 1);
  426. if (modifiers != 0 && last_dash[l + 1] == '>') {
  427. key = PTR2CHAR(last_dash + off);
  428. } else {
  429. key = get_special_key_code(last_dash + off);
  430. if (!keep_x_key) {
  431. key = handle_x_keys(key);
  432. }
  433. }
  434. }
  435. // get_special_key_code() may return NUL for invalid
  436. // special key name.
  437. if (key != NUL) {
  438. // Only use a modifier when there is no special key code that
  439. // includes the modifier.
  440. key = simplify_key(key, &modifiers);
  441. if (!keycode) {
  442. // don't want keycode, use single byte code
  443. if (key == K_BS) {
  444. key = BS;
  445. } else if (key == K_DEL || key == K_KDEL) {
  446. key = DEL;
  447. }
  448. }
  449. // Normal Key with modifier:
  450. // Try to make a single byte code (except for Alt/Meta modifiers).
  451. if (!IS_SPECIAL(key)) {
  452. key = extract_modifiers(key, &modifiers);
  453. }
  454. *modp = modifiers;
  455. *srcp = end_of_name;
  456. return key;
  457. }
  458. }
  459. }
  460. return 0;
  461. }
  462. char_u *add_char2buf(int c, char_u *s)
  463. {
  464. char_u temp[MB_MAXBYTES + 1];
  465. const int len = utf_char2bytes(c, temp);
  466. for (int i = 0; i < len; ++i) {
  467. c = temp[i];
  468. // Need to escape K_SPECIAL and CSI like in the typeahead buffer.
  469. if (c == K_SPECIAL) {
  470. *s++ = K_SPECIAL;
  471. *s++ = KS_SPECIAL;
  472. *s++ = KE_FILLER;
  473. } else {
  474. *s++ = c;
  475. }
  476. }
  477. return s;
  478. }
  479. unsigned int trans_special(const char_u **srcp, const size_t src_len,
  480. char_u *const dst, const bool keycode,
  481. const bool in_string)
  482. {
  483. int modifiers = 0;
  484. int key;
  485. unsigned int dlen = 0;
  486. key = find_special_key(srcp, src_len, &modifiers, keycode, false, in_string);
  487. if (key == 0) {
  488. return 0;
  489. }
  490. // Put the appropriate modifier in a string.
  491. if (modifiers != 0) {
  492. dst[dlen++] = K_SPECIAL;
  493. dst[dlen++] = KS_MODIFIER;
  494. dst[dlen++] = (char_u)modifiers;
  495. }
  496. if (IS_SPECIAL(key)) {
  497. dst[dlen++] = K_SPECIAL;
  498. dst[dlen++] = (char_u)KEY2TERMCAP0(key);
  499. dst[dlen++] = KEY2TERMCAP1(key);
  500. } else if (!keycode) {
  501. dlen += (unsigned int)(*mb_char2bytes)(key, dst + dlen);
  502. } else if (keycode) {
  503. char_u *after = add_char2buf(key, dst + dlen);
  504. assert(after >= dst && (uintmax_t)(after - dst) <= UINT_MAX);
  505. dlen = (unsigned int)(after - dst);
  506. } else {
  507. dst[dlen++] = (char_u)key;
  508. }
  509. return dlen;
  510. }