key.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* key.c, Ait, BSD 3-Clause, Kevin Bloom, 2023,
  2. Derived from: Atto January 2017
  3. Derived from: Anthony's Editor January 93
  4. */
  5. #include "header.h"
  6. #include "termbox.h"
  7. /* desc, keys, func */
  8. keymap_t keymap[] = {
  9. {"C-a beginning-of-line ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_A, -1}, lnbegin, UA_REPEAT },
  10. {"C-b backward-char ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_B, -1}, left, UA_REPEAT },
  11. {"C-d delete ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_D, -1}, delete, UA_REPEAT },
  12. {"C-e end-of-line ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_E, -1}, lnend, UA_REPEAT },
  13. {"C-f foward-char ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_F, -1}, right, UA_REPEAT },
  14. {"C-h backspace ", TB_MOD_CTRL, (int[2]){TB_KEY_BACKSPACE, -1}, backsp, UA_REPEAT },
  15. {"C-h backspace ", TB_MOD_CTRL, (int[2]){TB_KEY_BACKSPACE2, -1}, backsp, UA_REPEAT },
  16. {"C-i indent ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_I, -1}, inserttabasspace, UA_REPEAT },
  17. {"C-k kill-to-eol ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_K, -1}, killtoeol, UA_REPEAT },
  18. {"C-l refresh ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_L, -1}, recenter, UA_NOTHING },
  19. {"C-x u undo ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'u', -1}, undo, UA_NOTHING },
  20. {"C-/ undo ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_SLASH, -1}, undo, UA_NOTHING },
  21. {"C-n next-line ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_N, -1}, down, UA_REPEAT },
  22. {"C-m newline ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_M, -1}, insertnewline, UA_REPEAT },
  23. {"C-p previous-line ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_P, -1}, up, UA_REPEAT },
  24. {"C-r search ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_R, -1}, search_rev, UA_NOTHING },
  25. {"C-o newline-below ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_O, -1}, insertnewlinebelow, UA_REPEAT },
  26. {"C-s search ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_S, -1}, search_fwd, UA_NOTHING },
  27. {"C-t transpose ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_T, -1}, transpose, UA_REPEAT },
  28. {"C-u universal-argument ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_U, -1}, universal_argument_load, UA_NOTHING },
  29. {"C-v forward-page ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_V, -1}, pgdown, UA_REPEAT },
  30. {"C-w kill-region ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_W, -1}, cut, UA_CUSTOM },
  31. {"C-y yank ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_Y, -1}, paste, UA_CUSTOM },
  32. {"C-z suspend ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_Z, -1}, suspend, UA_NOTHING },
  33. {"C-space set-mark ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_TILDE, -1}, iblock, UA_NOTHING },
  34. {"C-g remove-mark ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_G, -1}, unmark, UA_NOTHING },
  35. {"C-x 1 delete-other-window", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '1', -1}, delete_other_windows, UA_NOTHING },
  36. {"C-x 2 split-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '2', -1}, split_window, UA_NOTHING },
  37. {"C-x 3 chop-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '3', -1}, chop_window, UA_NOTHING },
  38. {"C-x 0 close-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '0', -1}, close_window, UA_NOTHING },
  39. {"C-x o other-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'o', -1}, next_window, UA_NOTHING },
  40. {"C-x = cursor-position ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '=', -1}, showpos, UA_NOTHING },
  41. {"C-x i insert-file ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'i', -1}, insertfile, UA_NOTHING },
  42. {"C-x k kill-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'k', -1}, killbuffer, UA_NOTHING },
  43. {"C-x C-n next-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_N, -1}, next_buffer, UA_NOTHING },
  44. {"C-x n next-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'n', -1}, next_buffer, UA_NOTHING },
  45. {"C-x b switch-to-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'b', -1}, switch_buffer, UA_NOTHING },
  46. {"C-x ( start-kbd-macro ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '(', -1}, start_kbd_macro, UA_NOTHING },
  47. {"C-x ) start-kbd-macro ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, ')', -1}, end_kbd_macro, UA_NOTHING },
  48. /* UA_PREVENT is misleading here, since it uses the numeric_argument in the get_key function */
  49. {"C-x e run-kbd-macro ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'e', -1}, run_kbd_macro, UA_PREVENT },
  50. {"C-x C-f find-file ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_F, -1}, readfile, UA_NOTHING },
  51. {"C-x C-s save-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_S, -1}, savebuffer, UA_NOTHING },
  52. {"C-x C-g umark ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_G, -1}, unmark, UA_NOTHING },
  53. {"C-x C-w write-file ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_W, -1}, writefile, UA_NOTHING },
  54. {"C-x C-c exit ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_C, -1}, quit_ask, UA_NOTHING },
  55. {"C-x C-x pop-to-mark ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_X, -1}, poptomark, UA_NOTHING },
  56. {"esc 0 numeric-arg-0 ", TB_MOD_ALT, (int[2]){'0', -1}, numeric_argument_load, UA_PREVENT },
  57. {"esc 1 numeric-arg-1 ", TB_MOD_ALT, (int[2]){'1', -1}, numeric_argument_load, UA_PREVENT },
  58. {"esc 2 numeric-arg-2 ", TB_MOD_ALT, (int[2]){'2', -1}, numeric_argument_load, UA_PREVENT },
  59. {"esc 3 numeric-arg-3 ", TB_MOD_ALT, (int[2]){'3', -1}, numeric_argument_load, UA_PREVENT },
  60. {"esc 4 numeric-arg-4 ", TB_MOD_ALT, (int[2]){'4', -1}, numeric_argument_load, UA_PREVENT },
  61. {"esc 5 numeric-arg-5 ", TB_MOD_ALT, (int[2]){'5', -1}, numeric_argument_load, UA_PREVENT },
  62. {"esc 6 numeric-arg-6 ", TB_MOD_ALT, (int[2]){'6', -1}, numeric_argument_load, UA_PREVENT },
  63. {"esc 7 numeric-arg-7 ", TB_MOD_ALT, (int[2]){'7', -1}, numeric_argument_load, UA_PREVENT },
  64. {"esc 8 numeric-arg-8 ", TB_MOD_ALT, (int[2]){'8', -1}, numeric_argument_load, UA_PREVENT },
  65. {"esc 9 numeric-arg-9 ", TB_MOD_ALT, (int[2]){'9', -1}, numeric_argument_load, UA_PREVENT },
  66. {"esc b back-word ", TB_MOD_ALT, (int[2]){'b', -1}, wleft, UA_REPEAT },
  67. {"esc b back-word-delete ", TB_MOD_CTRL | TB_MOD_ALT, (int[2]){TB_KEY_BACKSPACE2, -1}, wleftdelete, UA_REPEAT },
  68. {"esc b back-word-delete ", TB_MOD_CTRL | TB_MOD_ALT, (int[2]){TB_KEY_BACKSPACE, -1}, wleftdelete, UA_REPEAT },
  69. {"esc f fwd-word ", TB_MOD_ALT, (int[2]){'f', -1}, wright, UA_REPEAT },
  70. {"esc d fwd-word-delete ", TB_MOD_ALT, (int[2]){'d', -1}, wrightdelete, UA_REPEAT },
  71. {"esc x execute-shell-cmd ", TB_MOD_ALT, (int[2]){'x', -1}, insert_from_shell, UA_NOTHING },
  72. {"esc g gotoline ", TB_MOD_ALT, (int[2]){'g', -1}, gotoline, UA_NOTHING },
  73. {"esc i indent ", TB_MOD_ALT, (int[2]){'i', -1}, inserttab, UA_REPEAT },
  74. {"esc k kill-region ", TB_MOD_ALT, (int[2]){'k', -1}, cut, UA_NOTHING },
  75. {"esc m back-to-indentation", TB_MOD_ALT, (int[2]){'m', -1}, back_to_indentation, UA_NOTHING },
  76. {"esc n negate ", TB_MOD_ALT, (int[2]){'-', -1}, negate, UA_NOTHING },
  77. {"esc o open-shell-cmd ", TB_MOD_ALT, (int[2]){'o', -1}, open_file_from_shell, UA_NOTHING },
  78. {"esc % query-replace ", TB_MOD_ALT, (int[2]){'%', -1}, query_replace, UA_NOTHING },
  79. {"esc v backward-page ", TB_MOD_ALT, (int[2]){'v', -1}, pgup, UA_REPEAT },
  80. {"esc w copy-region ", TB_MOD_ALT, (int[2]){'w', -1}, copy, UA_NOTHING },
  81. {"esc @ set-mark ", TB_MOD_ALT, (int[2]){'@', -1}, iblock, UA_NOTHING },
  82. {"esc < beg-of-buf ", TB_MOD_ALT, (int[2]){'<', -1}, top, UA_NOTHING },
  83. {"esc > end-of-buf ", TB_MOD_ALT, (int[2]){'>', -1}, bottom, UA_NOTHING },
  84. {"esc / delete-whitespace ", TB_MOD_ALT, (int[2]){'\\', -1}, deletewhitespacebetween, UA_NOTHING },
  85. {"esc t query-replace ", TB_MOD_ALT, (int[2]){'t', -1}, transposeword, UA_REPEAT },
  86. {"esc l lowercase-word ", TB_MOD_ALT, (int[2]){'l', -1}, lowercaseword, UA_REPEAT },
  87. {"esc c capitalize-word ", TB_MOD_ALT, (int[2]){'c', -1}, capitalizeword, UA_REPEAT },
  88. {"esc u uppercase-word ", TB_MOD_ALT, (int[2]){'u', -1}, uppercaseword, UA_REPEAT },
  89. {"esc ; jump-to-char ", TB_MOD_ALT, (int[2]){';', -1}, jumptochar, UA_REPEAT },
  90. {"esc z zap-to-char ", TB_MOD_ALT, (int[2]){'z', -1}, zaptochar, UA_REPEAT },
  91. {"esc home, beg-of-buf ", 0, (int[2]){TB_KEY_HOME, -1}, top, UA_NOTHING },
  92. {"esc end, end-of-buf ", 0, (int[2]){TB_KEY_END, -1}, bottom, UA_NOTHING },
  93. {"esc esc show-version ", 0, (int[2]){TB_KEY_ESC, -1}, version, UA_NOTHING },
  94. {"ins toggle-overwrite-mode", 0, (int[2]){TB_KEY_INSERT, -1}, toggle_overwrite_mode , UA_NOTHING },
  95. {"del forward-delete-char ", 0, (int[2]){TB_KEY_DELETE, -1}, delete, UA_REPEAT },
  96. {"backspace delete-left ", 0, (int[2]){TB_KEY_BACKSPACE, -1}, backsp, UA_REPEAT },
  97. {"backspace delete-left ", 0, (int[2]){TB_KEY_BACKSPACE2, -1}, backsp, UA_REPEAT },
  98. {"up previous-line ", 0, (int[2]){TB_KEY_ARROW_UP, -1}, up, UA_REPEAT },
  99. {"down next-line ", 0, (int[2]){TB_KEY_ARROW_DOWN, -1}, down, UA_REPEAT },
  100. {"left backward-character ", 0, (int[2]){TB_KEY_ARROW_LEFT, -1}, left, UA_REPEAT },
  101. {"right forward-character ", 0, (int[2]){TB_KEY_ARROW_RIGHT, -1}, right, UA_REPEAT },
  102. {"pgup backward-page ", 0, (int[2]){TB_KEY_PGUP, -1}, pgup , UA_REPEAT },
  103. {"pgdn forward-page ", 0, (int[2]){TB_KEY_PGDN, -1}, pgdown, UA_REPEAT },
  104. // {"resize resize-terminal ", 0, (int[2]){TB_KEY_}, resize_terminal , UA_NOTHING },
  105. {"C-M-f foward-bracket ", TB_MOD_CTRL | TB_MOD_ALT, (int[2]){TB_KEY_CTRL_F, -1}, forward_bracket, UA_REPEAT },
  106. {"C-M-b backward-bracket ", TB_MOD_CTRL | TB_MOD_ALT, (int[2]){TB_KEY_CTRL_B, -1}, backward_bracket, UA_REPEAT },
  107. {"K_ERROR ", 0, NULL, NULL, UA_NOTHING }
  108. };
  109. char_t *get_key(keymap_t *keys, keymap_t **key_return)
  110. {
  111. keymap_t *k;
  112. int ret;
  113. static char_t buffer[K_BUFFER_LENGTH];
  114. static char_t *record = buffer;
  115. struct tb_event ev;
  116. *key_return = NULL;
  117. /* if recorded bytes remain, return next recorded byte. */
  118. if (*record != '\0') {
  119. *key_return = NULL;
  120. return record++;
  121. }
  122. /* reset record buffer. */
  123. record = buffer;
  124. if(execute_kbd_macro) {
  125. ev = record_buffer[run_buffer_index];
  126. ret = TB_OK;
  127. if(run_buffer_index > record_buffer_index) {
  128. if(numeric_argument > 0) {
  129. numeric_argument--;
  130. } else {
  131. execute_kbd_macro = FALSE;
  132. }
  133. run_buffer_index = 0;
  134. } else {
  135. run_buffer_index++;
  136. }
  137. } else {
  138. ret = tb_poll_event(&ev);
  139. }
  140. if(ret == TB_OK) {
  141. assert(K_BUFFER_LENGTH > record - buffer);
  142. if(ev.type == TB_EVENT_RESIZE) {
  143. resize_terminal();
  144. *key_return = NULL;
  145. return (record++);
  146. } else if(ev.type == TB_EVENT_MOUSE) {
  147. *key_return = NULL;
  148. return (record++);
  149. }
  150. *record++ = (unsigned)ev.ch;
  151. *record = '\0';
  152. if(record_input) {
  153. record_buffer[record_buffer_index] = ev;
  154. record_buffer_index++;
  155. }
  156. /* if recorded bytes match any multi-byte sequence... */
  157. for (k = keys; k->key_bytes != NULL; ++k) {
  158. if(k->key_mod == ev.mod &&
  159. (k->key_bytes[submatch] == ev.key ||
  160. (ev.ch != 0 && k->key_bytes[submatch] == (int)ev.ch))) {
  161. if(k->key_bytes[submatch+1] != -1) {
  162. submatch++;
  163. msg("C-x");
  164. break;
  165. } else {
  166. record = buffer;
  167. *record = '\0';
  168. *key_return = k;
  169. input_char = ev.ch;
  170. return record;
  171. }
  172. }
  173. if(submatch > 0 && k->key_bytes[submatch] == (int)ev.ch) {
  174. record = buffer;
  175. *record = '\0';
  176. *key_return = k;
  177. input_char = ev.ch;
  178. return record;
  179. }
  180. }
  181. unicode_buf[tb_utf8_unicode_to_char(unicode_buf, ev.ch)] = '\0';
  182. /* If no match is found, kill the loop. */
  183. if(key_return == NULL) {
  184. submatch = 0;
  185. }
  186. }
  187. /* nothing matched, return recorded bytes. */
  188. record = buffer;
  189. return (record++);
  190. }
  191. int getinput(char *prompt, char *buf, int nbuf, int flag)
  192. {
  193. int cpos = 0;
  194. int c;
  195. int start_col = strlen(prompt);
  196. struct tb_event ev;
  197. print_to_msgline(prompt);
  198. clrtoeol(prompt, MSGLINE);
  199. if (flag == F_CLEAR) buf[0] = '\0';
  200. /* if we have a default value print it and go to end of it */
  201. if (buf[0] != '\0') {
  202. addstr(buf);
  203. cpos = strlen(buf);
  204. }
  205. for (;;) {
  206. tb_present();
  207. if(tb_poll_event(&ev) != TB_OK) return 0;
  208. if(!ev.mod)
  209. c = ev.ch;
  210. else
  211. c = ev.key;
  212. /* ignore control keys other than backspace, cr, lf */
  213. if (c < 32 &&
  214. c != TB_KEY_CTRL_G &&
  215. c != TB_KEY_BACKSPACE &&
  216. c != TB_KEY_CTRL_J &&
  217. c != TB_KEY_ENTER)
  218. continue;
  219. switch(c) {
  220. case TB_KEY_CTRL_J: /* cr, lf */
  221. case TB_KEY_ENTER:
  222. buf[cpos] = '\0';
  223. return (cpos > 0 ? TRUE : FALSE);
  224. case TB_KEY_CTRL_G: /* ctrl-g */
  225. return FALSE;
  226. case TB_KEY_BACKSPACE2: /* del, erase */
  227. case TB_KEY_BACKSPACE: /* backspace */
  228. if (cpos == 0)
  229. continue;
  230. tb_set_cursor(start_col + cpos - 1, MSGLINE);
  231. addch(' ');
  232. tb_set_cursor(start_col + cpos - 1, MSGLINE);
  233. buf[--cpos] = '\0';
  234. break;
  235. default:
  236. if (cpos < nbuf -1) {
  237. addch(c);
  238. buf[cpos++] = c;
  239. buf[cpos] ='\0';
  240. }
  241. break;
  242. }
  243. }
  244. }