key.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. /* key.c, Ait, BSD 3-Clause, Kevin Bloom, 2023-2025,
  2. Derived from: Atto January 2017
  3. Derived from: Anthony's Editor January 93
  4. */
  5. #include "header.h"
  6. #include "termbox.h"
  7. #include "util.h"
  8. /* desc, keys, func */
  9. keymap_t keymap[] = {
  10. {"C-a beginning-of-line ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_A, -1}, lnbegin, UA_REPEAT },
  11. {"C-b backward-char ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_B, -1}, left, UA_REPEAT },
  12. {"C-d delete ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_D, -1}, delete, UA_REPEAT },
  13. {"C-e end-of-line ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_E, -1}, lnend, UA_REPEAT },
  14. {"C-f foward-char ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_F, -1}, right, UA_REPEAT },
  15. {"C-h backspace ", TB_MOD_CTRL, (int[2]){TB_KEY_BACKSPACE, -1}, backsp, UA_REPEAT },
  16. {"C-h backspace ", TB_MOD_CTRL, (int[2]){TB_KEY_BACKSPACE2, -1}, backsp, UA_REPEAT },
  17. {"C-i indent ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_I, -1}, inserttabasspace, UA_REPEAT },
  18. {"C-k kill-to-eol ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_K, -1}, killtoeol, UA_REPEAT },
  19. {"C-l refresh ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_L, -1}, recenter, UA_NOTHING },
  20. {"C-x u undo ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'u', -1}, undo, UA_NOTHING },
  21. {"C-/ undo ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_SLASH, -1}, undo, UA_NOTHING },
  22. {"C-n next-line ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_N, -1}, down, UA_REPEAT },
  23. {"C-m newline ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_M, -1}, insertnewline, UA_REPEAT },
  24. {"C-p previous-line ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_P, -1}, up, UA_REPEAT },
  25. {"C-q insert-control-char ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_Q, -1}, insert_control_char, UA_PREVENT },
  26. {"C-r search ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_R, -1}, search_rev, UA_PREVENT },
  27. {"C-o newline-below ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_O, -1}, insertnewlinebelow, UA_REPEAT },
  28. {"C-s search ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_S, -1}, search_fwd, UA_PREVENT },
  29. {"C-t transpose ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_T, -1}, transpose, UA_REPEAT },
  30. {"C-u universal-argument ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_U, -1}, universal_argument_load, UA_NOTHING },
  31. {"C-v forward-page ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_V, -1}, pgdown, UA_REPEAT },
  32. {"C-w kill-region ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_W, -1}, cut, UA_CUSTOM },
  33. {"C-y yank ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_Y, -1}, paste, UA_CUSTOM },
  34. {"C-z suspend ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_Z, -1}, suspend, UA_PREVENT },
  35. {"C-space set-mark ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_TILDE, -1}, iblock, UA_NOTHING },
  36. {"C-g remove-mark ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_G, -1}, unmark, UA_NOTHING },
  37. {"C-x 0 delete-other-window", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '0', -1}, delete_other_windows, UA_NOTHING },
  38. {"C-x 1 delete-other-window", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '1', -1}, delete_other_windows, UA_NOTHING },
  39. {"C-x 2 split-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '2', -1}, split_window, UA_NOTHING },
  40. {"C-x 3 chop-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '3', -1}, chop_window, UA_NOTHING },
  41. {"C-x 4 tri-split-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '4', -1}, tri_split_window, UA_NOTHING },
  42. {"C-x 5 tri-chop-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '5', -1}, tri_chop_window, UA_NOTHING },
  43. {"C-x 6 fib-right-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '6', -1}, fib_right, UA_NOTHING },
  44. {"C-x 7 fib-left-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '7', -1}, fib_left, UA_NOTHING },
  45. {"C-x 8 quad-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '8', -1}, quad_window, UA_NOTHING },
  46. {"C-x o other-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'o', -1}, next_window, UA_NOTHING },
  47. {"C-x = cursor-position ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '=', -1}, showpos, UA_NOTHING },
  48. {"C-x i insert-file ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'i', -1}, insertfile, UA_PREVENT },
  49. {"C-x k kill-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'k', -1}, killbuffer, UA_PREVENT },
  50. {"C-x C-n next-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_N, -1}, next_buffer, UA_NOTHING },
  51. {"C-x n next-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'n', -1}, next_buffer, UA_NOTHING },
  52. {"C-x p prev-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'p', -1}, prev_buffer, UA_NOTHING },
  53. {"C-x b switch-to-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'b', -1}, switch_buffer, UA_NOTHING },
  54. {"C-x ( start-kbd-macro ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '(', -1}, start_kbd_macro, UA_PREVENT },
  55. {"C-x ) start-kbd-macro ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, ')', -1}, end_kbd_macro, UA_PREVENT },
  56. /* UA_PREVENT is misleading here, since it uses the numeric_argument in the get_key function */
  57. {"C-x e run-kbd-macro ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'e', -1}, run_kbd_macro, UA_PREVENT },
  58. {"C-x C-f find-file ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_F, -1}, readfile, UA_NOTHING },
  59. {"C-x C-s save-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_S, -1}, savebuffer, UA_NOTHING },
  60. {"C-x C-g umark ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_G, -1}, unmark, UA_NOTHING },
  61. {"C-x C-w write-file ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_W, -1}, writefile, UA_NOTHING },
  62. {"C-x C-c exit ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_C, -1}, quit_ask, UA_NOTHING },
  63. {"C-x C-x pop-to-mark ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_X, -1}, poptomark, UA_NOTHING },
  64. {"C-x C-; comment ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, ';', -1}, comment, UA_NOTHING },
  65. {"C-x : comment-at-eol ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, ':', -1}, comment_at_eol, UA_NOTHING },
  66. {"esc 0 numeric-arg-0 ", TB_MOD_ALT, (int[2]){'0', -1}, numeric_argument_load, UA_PREVENT },
  67. {"esc 1 numeric-arg-1 ", TB_MOD_ALT, (int[2]){'1', -1}, numeric_argument_load, UA_PREVENT },
  68. {"esc 2 numeric-arg-2 ", TB_MOD_ALT, (int[2]){'2', -1}, numeric_argument_load, UA_PREVENT },
  69. {"esc 3 numeric-arg-3 ", TB_MOD_ALT, (int[2]){'3', -1}, numeric_argument_load, UA_PREVENT },
  70. {"esc 4 numeric-arg-4 ", TB_MOD_ALT, (int[2]){'4', -1}, numeric_argument_load, UA_PREVENT },
  71. {"esc 5 numeric-arg-5 ", TB_MOD_ALT, (int[2]){'5', -1}, numeric_argument_load, UA_PREVENT },
  72. {"esc 6 numeric-arg-6 ", TB_MOD_ALT, (int[2]){'6', -1}, numeric_argument_load, UA_PREVENT },
  73. {"esc 7 numeric-arg-7 ", TB_MOD_ALT, (int[2]){'7', -1}, numeric_argument_load, UA_PREVENT },
  74. {"esc 8 numeric-arg-8 ", TB_MOD_ALT, (int[2]){'8', -1}, numeric_argument_load, UA_PREVENT },
  75. {"esc 9 numeric-arg-9 ", TB_MOD_ALT, (int[2]){'9', -1}, numeric_argument_load, UA_PREVENT },
  76. {"esc b back-word ", TB_MOD_ALT, (int[2]){'b', -1}, wleft, UA_REPEAT },
  77. {"esc bksp back-word-delete", TB_MOD_CTRL | TB_MOD_ALT, (int[2]){TB_KEY_BACKSPACE2, -1}, wleftdelete, UA_REPEAT },
  78. {"esc bksp back-word-delete", TB_MOD_CTRL | TB_MOD_ALT, (int[2]){TB_KEY_BACKSPACE, -1}, wleftdelete, UA_REPEAT },
  79. {"esc f fwd-word ", TB_MOD_ALT, (int[2]){'f', -1}, wright, UA_REPEAT },
  80. {"esc d fwd-word-delete ", TB_MOD_ALT, (int[2]){'d', -1}, wrightdelete, UA_REPEAT },
  81. {"esc x execute-shell-cmd ", TB_MOD_ALT, (int[2]){'x', -1}, insert_from_shell, UA_PREVENT },
  82. {"esc g gotoline ", TB_MOD_ALT, (int[2]){'g', -1}, gotoline, UA_PREVENT },
  83. {"esc G gotocolumn ", TB_MOD_ALT, (int[2]){'G', -1}, gotocolumn, UA_PREVENT },
  84. {"esc r jumptorow ", TB_MOD_ALT, (int[2]){'r', -1}, jumptorow, UA_PREVENT },
  85. {"esc j jumpword ", TB_MOD_ALT, (int[2]){'j', -1}, jumpword, UA_PREVENT },
  86. {"esc i indent ", TB_MOD_ALT, (int[2]){'i', -1}, inserttab, UA_REPEAT },
  87. {"esc k kill-region ", TB_MOD_ALT, (int[2]){'k', -1}, cut, UA_NOTHING },
  88. {"esc m back-to-indentation", TB_MOD_ALT, (int[2]){'m', -1}, back_to_indentation, UA_NOTHING },
  89. {"esc n negate ", TB_MOD_ALT, (int[2]){'-', -1}, negate, UA_NOTHING },
  90. {"esc o open-shell-cmd ", TB_MOD_ALT, (int[2]){'o', -1}, open_file_from_shell, UA_PREVENT },
  91. {"esc % query-replace ", TB_MOD_ALT, (int[2]){'%', -1}, query_replace, UA_NOTHING },
  92. {"esc v backward-page ", TB_MOD_ALT, (int[2]){'v', -1}, pgup, UA_REPEAT },
  93. {"esc w copy-region ", TB_MOD_ALT, (int[2]){'w', -1}, copy, UA_NOTHING },
  94. {"esc @ set-mark ", TB_MOD_ALT, (int[2]){'@', -1}, iblock, UA_NOTHING },
  95. {"esc < beg-of-buf ", TB_MOD_ALT, (int[2]){'<', -1}, top, UA_NOTHING },
  96. {"esc > end-of-buf ", TB_MOD_ALT, (int[2]){'>', -1}, bottom, UA_NOTHING },
  97. {"esc \\ delete-between ", TB_MOD_ALT, (int[2]){'\\', -1}, delete_between, UA_CUSTOM },
  98. {"esc / redo ", TB_MOD_ALT, (int[2]){'/', -1}, redo, UA_NOTHING },
  99. {"esc t transpose word ", TB_MOD_ALT, (int[2]){'t', -1}, transposeword, UA_REPEAT },
  100. {"esc l lowercase-word ", TB_MOD_ALT, (int[2]){'l', -1}, lowercaseword, UA_REPEAT },
  101. {"esc c capitalize-word ", TB_MOD_ALT, (int[2]){'c', -1}, capitalizeword, UA_REPEAT },
  102. {"esc u uppercase-word ", TB_MOD_ALT, (int[2]){'u', -1}, uppercaseword, UA_REPEAT },
  103. {"esc ; jump-to-char ", TB_MOD_ALT, (int[2]){';', -1}, jumptochar, UA_REPEAT },
  104. {"esc : jump-to-char ", TB_MOD_ALT, (int[2]){':', -1}, negated_jumptochar, UA_REPEAT },
  105. {"esc z zap-to-char ", TB_MOD_ALT, (int[2]){'z', -1}, zaptochar, UA_CUSTOM },
  106. {"esc Z zap-to-char ", TB_MOD_ALT, (int[2]){'Z', -1}, negated_zaptochar, UA_CUSTOM },
  107. {"esc . dynamically-expand ", TB_MOD_ALT, (int[2]){'.', -1}, dynamically_expand, UA_NOTHING },
  108. {"esc home, beg-of-buf ", 0, (int[2]){TB_KEY_HOME, -1}, top, UA_NOTHING },
  109. {"esc end, end-of-buf ", 0, (int[2]){TB_KEY_END, -1}, bottom, UA_NOTHING },
  110. {"esc esc show-version ", 0, (int[2]){TB_KEY_ESC, -1}, version, UA_NOTHING },
  111. {"ins toggle-overwrite-mode", 0, (int[2]){TB_KEY_INSERT, -1}, toggle_overwrite_mode , UA_NOTHING },
  112. {"del forward-delete-char ", 0, (int[2]){TB_KEY_DELETE, -1}, delete, UA_REPEAT },
  113. {"backspace delete-left ", 0, (int[2]){TB_KEY_BACKSPACE, -1}, backsp, UA_REPEAT },
  114. {"backspace delete-left ", 0, (int[2]){TB_KEY_BACKSPACE2, -1}, backsp, UA_REPEAT },
  115. {"up previous-line ", 0, (int[2]){TB_KEY_ARROW_UP, -1}, up, UA_REPEAT },
  116. {"down next-line ", 0, (int[2]){TB_KEY_ARROW_DOWN, -1}, down, UA_REPEAT },
  117. {"left backward-character ", 0, (int[2]){TB_KEY_ARROW_LEFT, -1}, left, UA_REPEAT },
  118. {"right forward-character ", 0, (int[2]){TB_KEY_ARROW_RIGHT, -1}, right, UA_REPEAT },
  119. {"pgup backward-page ", 0, (int[2]){TB_KEY_PGUP, -1}, pgup , UA_REPEAT },
  120. {"pgdn forward-page ", 0, (int[2]){TB_KEY_PGDN, -1}, pgdown, UA_REPEAT },
  121. // {"resize resize-terminal ", 0, (int[2]){TB_KEY_}, resize_terminal , UA_NOTHING },
  122. {"C-M-f forward-bracket ", TB_MOD_CTRL | TB_MOD_ALT, (int[2]){TB_KEY_CTRL_F, -1}, forward_bracket, UA_REPEAT },
  123. {"C-M-b backward-bracket ", TB_MOD_CTRL | TB_MOD_ALT, (int[2]){TB_KEY_CTRL_B, -1}, backward_bracket, UA_REPEAT },
  124. {"K_ERROR ", 0, NULL, NULL, UA_NOTHING }
  125. };
  126. keymap_t extraneous_keymap[] = {
  127. {"clipboard ", 0, (int[2]){0, -1}, clipboard, UA_NOTHING},
  128. {"K_ERROR ", 0, NULL, NULL, UA_NOTHING }
  129. };
  130. char_t *get_key(keymap_t *keys, keymap_t **key_return)
  131. {
  132. keymap_t *k;
  133. int ret;
  134. static char_t buffer[K_BUFFER_LENGTH];
  135. static char_t *record = buffer;
  136. struct tb_event ev, ev1;
  137. *key_return = NULL;
  138. /* if recorded bytes remain, return next recorded byte. */
  139. if (*record != '\0') {
  140. *key_return = NULL;
  141. return record++;
  142. }
  143. /* reset record buffer. */
  144. record = buffer;
  145. if(execute_kbd_macro) {
  146. use_kbd_macro(&ev);
  147. ret = TB_OK;
  148. } else {
  149. ret = tb_poll_event(&ev);
  150. }
  151. if(ret == TB_OK) {
  152. assert(K_BUFFER_LENGTH > record - buffer);
  153. if(ev.type == TB_EVENT_RESIZE) {
  154. resize_terminal();
  155. *key_return = NULL;
  156. return (record++);
  157. } else if(ev.type == TB_EVENT_MOUSE) {
  158. *key_return = NULL;
  159. return (record++);
  160. }
  161. *record++ = (unsigned)ev.ch;
  162. *record = '\0';
  163. /* When text is input via the system clipboard it is rapidly
  164. input into the editor. Using the tb_peek_event with a very
  165. small timeout seems to work at grabbing all the contents from
  166. the clipboard in. This does two things:
  167. 1. Prevents the editor from inputting one char at a time
  168. 2. Prevents auto-indent from indenting consecutively during
  169. paste.
  170. */
  171. if(!execute_kbd_macro) {
  172. int ci = 0, size = 1;
  173. if(gtemp == NULL) {
  174. gtemp = malloc(sizeof(char) * TEMPBUF);
  175. memset(gtemp, 0, TEMPBUF);
  176. ngtemp = TEMPBUF;
  177. }
  178. unicode_buf[tb_utf8_unicode_to_char(unicode_buf, ev.ch)] = '\0';
  179. gtemp[ci] = ev1.key == TB_KEY_CTRL_M || ev1.key == TB_KEY_CTRL_J ?
  180. '\n' : unicode_buf[0];
  181. ret = tb_peek_event(&ev1, 0);
  182. if(record_input) {
  183. record_buffer[record_buffer_index] = ev1;
  184. record_buffer_index++;
  185. }
  186. ci++;
  187. while(ret == TB_OK) {
  188. int maxsize = TEMPBUF*size;
  189. if(ci > maxsize) {
  190. char *p2;
  191. int newsize = TEMPBUF*(++size);
  192. if ((p2 = realloc(gtemp, newsize)) == NULL) {
  193. if (gtemp != NULL)
  194. free(gtemp);
  195. gtemp = NULL;
  196. return NULL;
  197. }
  198. gtemp = p2;
  199. ngtemp = newsize;
  200. }
  201. int uni_size = tb_utf8_unicode_to_char(unicode_buf, ev1.ch);
  202. unicode_buf[uni_size] = '\0';
  203. unicode_buf[0] =
  204. ev1.key == TB_KEY_CTRL_M || ev1.key == TB_KEY_CTRL_J ?
  205. '\n' : unicode_buf[0];
  206. strncat(gtemp, unicode_buf, uni_size);
  207. ci += uni_size;
  208. ret = tb_peek_event(&ev1, 0);
  209. if(record_input) {
  210. record_buffer[record_buffer_index] = ev1;
  211. record_buffer_index++;
  212. }
  213. }
  214. gtemp[ci] = '\0';
  215. if(strlen(gtemp) > 2) {
  216. k = extraneous_keymap;
  217. *key_return = k;
  218. record = buffer;
  219. *record = '\0';
  220. return record;
  221. }
  222. }
  223. if(record_input) {
  224. record_buffer[record_buffer_index] = ev;
  225. record_buffer_index++;
  226. }
  227. /* if recorded bytes match any multi-byte sequence... */
  228. for (k = keys; k->key_bytes != NULL; ++k) {
  229. if(k->key_mod == ev.mod &&
  230. (k->key_bytes[submatch] == ev.key ||
  231. (ev.ch != 0 && k->key_bytes[submatch] == (int)ev.ch))) {
  232. if(k->key_bytes[submatch+1] != -1) {
  233. submatch++;
  234. msg("C-x");
  235. break;
  236. } else {
  237. record = buffer;
  238. *record = '\0';
  239. *key_return = k;
  240. input_char = ev.ch;
  241. return record;
  242. }
  243. }
  244. if(submatch > 0 && k->key_bytes[submatch] == (int)ev.ch) {
  245. record = buffer;
  246. *record = '\0';
  247. *key_return = k;
  248. input_char = ev.ch;
  249. return record;
  250. }
  251. }
  252. unicode_buf[tb_utf8_unicode_to_char(unicode_buf, ev.ch)] = '\0';
  253. /* If no match is found, kill the loop. */
  254. if(key_return == NULL) {
  255. submatch = 0;
  256. }
  257. }
  258. /* nothing matched, return recorded bytes. */
  259. record = buffer;
  260. return (record++);
  261. }
  262. int getinput(char *prompt, char *buf, int nbuf, int flag, int allowblank)
  263. {
  264. int cpos = 0;
  265. int c;
  266. int start_col = strlen(prompt);
  267. struct tb_event ev;
  268. print_to_msgline(prompt);
  269. clrtoeol(start_col, MSGLINE);
  270. if (flag == F_CLEAR) buf[0] = '\0';
  271. /* if we have a default value print it and go to end of it */
  272. if (buf[0] != '\0') {
  273. addstr(buf);
  274. cpos = strlen(buf);
  275. }
  276. for (;;) {
  277. tb_present();
  278. if(execute_kbd_macro) {
  279. use_kbd_macro(&ev);
  280. } else if(tb_poll_event(&ev) != TB_OK)
  281. return 0;
  282. if(msgline_editor(ev, prompt, buf, nbuf, &cpos))
  283. continue;
  284. if(!ev.mod)
  285. c = ev.ch;
  286. else
  287. c = ev.key;
  288. if(record_input) {
  289. record_buffer[record_buffer_index] = ev;
  290. record_buffer_index++;
  291. }
  292. /* ignore control keys other than backspace, cr, lf */
  293. if (c < 32 &&
  294. c != TB_KEY_CTRL_G &&
  295. c != TB_KEY_BACKSPACE &&
  296. c != TB_KEY_CTRL_J &&
  297. c != TB_KEY_CTRL_I &&
  298. c != TB_KEY_TAB &&
  299. c != TB_KEY_ENTER)
  300. continue;
  301. switch(c) {
  302. case TB_KEY_CTRL_J: /* cr, lf */
  303. case TB_KEY_ENTER: {
  304. int z = 0;
  305. while(buf[z] != 0)
  306. z++;
  307. buf[z] = '\0';
  308. return (cpos > 0 ? TRUE : allowblank);
  309. }
  310. case TB_KEY_CTRL_G: /* ctrl-g */
  311. clrtoeol(0, MSGLINE);
  312. return FALSE;
  313. case TB_KEY_BACKSPACE2: /* del, erase */
  314. case TB_KEY_BACKSPACE: /* backspace */
  315. if (cpos == 0)
  316. continue;
  317. tb_set_cursor(start_col + cpos - 1, MSGLINE);
  318. addch(' ');
  319. tb_set_cursor(start_col + cpos - 1, MSGLINE);
  320. buf[--cpos] = '\0';
  321. break;
  322. default:
  323. if (cpos < nbuf -1) {
  324. for(int i = strlen(buf); i > cpos; i--) {
  325. buf[i] = buf[i - 1];
  326. }
  327. buf[cpos] = c;
  328. tb_set_cursor(start_col, MSGLINE);
  329. addstr(buf);
  330. cpos++;
  331. tb_set_cursor(start_col + cpos, MSGLINE);
  332. }
  333. break;
  334. }
  335. }
  336. }
  337. int msgline_editor(
  338. struct tb_event ev, char *prompt, char *buf, int nbuf, int *cpos
  339. )
  340. {
  341. char full[PATH_MAX+1];
  342. int k = 0, i = 0, start_col = strlen(prompt);
  343. strcpy(full, prompt);
  344. if(ev.mod == TB_MOD_ALT || ev.mod == (TB_MOD_CTRL | TB_MOD_ALT)) {
  345. k = ev.ch;
  346. if(k == 'b') {
  347. if ((!isspace(buf[*cpos]) || !is_symbol(buf[*cpos])) && *cpos != 0)
  348. *cpos = *cpos -1;
  349. while ((isspace(buf[*cpos]) || is_symbol(buf[*cpos])) && *cpos != 0)
  350. *cpos = *cpos -1;
  351. while (!isspace(buf[*cpos]) && !is_symbol(buf[*cpos]) && *cpos != 0)
  352. *cpos = *cpos -1;
  353. tb_set_cursor(start_col + *cpos, MSGLINE);
  354. return 1;
  355. } else if(k == 'f') {
  356. if ((!isspace(buf[*cpos]) || !is_symbol(buf[*cpos])) && *cpos != strlen(buf))
  357. *cpos = *cpos + 1;
  358. while ((isspace(buf[*cpos]) || is_symbol(buf[*cpos])) && *cpos != strlen(buf))
  359. *cpos = *cpos + 1;
  360. while (!isspace(buf[*cpos]) && !is_symbol(buf[*cpos]) && *cpos != strlen(buf))
  361. *cpos = *cpos + 1;
  362. tb_set_cursor(start_col + *cpos, MSGLINE);
  363. return 1;
  364. } else if(k == 'd') {
  365. if ((!isspace(buf[*cpos]) || !is_symbol(buf[*cpos])) && *cpos != strlen(buf)) {
  366. for(i = *cpos; buf[i] != '\0'; i++) {
  367. buf[i] = buf[i+1];
  368. }
  369. }
  370. while ((isspace(buf[*cpos]) || is_symbol(buf[*cpos])) && *cpos != strlen(buf)) {
  371. for(i = *cpos; buf[i] != '\0'; i++) {
  372. buf[i] = buf[i+1];
  373. }
  374. }
  375. while (!isspace(buf[*cpos]) && !is_symbol(buf[*cpos]) && *cpos != strlen(buf)) {
  376. for(i = *cpos; buf[i] != '\0'; i++) {
  377. buf[i] = buf[i+1];
  378. }
  379. }
  380. strcat(full, buf);
  381. tb_set_cursor(start_col, MSGLINE);
  382. addstr(buf);
  383. clrtoeol(start_col + *cpos, MSGLINE);
  384. tb_set_cursor(start_col + *cpos, MSGLINE);
  385. full[start_col + *cpos] = '\0';
  386. return 1;
  387. } else if(k == 'y') {
  388. int ondef = FALSE, pastdef = FALSE;
  389. for(int i = 0, ii = 8; prompt[i] != '\0'; i++) {
  390. if(prompt[i] == '(')
  391. ondef = TRUE;
  392. if(pastdef) {
  393. buf[*cpos] = prompt[i];
  394. *cpos += 1;
  395. }
  396. if(ondef && !pastdef) {
  397. if(ii == 0)
  398. pastdef = TRUE;
  399. ii--;
  400. }
  401. }
  402. /* Since the default value can contain anything, we have to
  403. determine we're at the end of the default value we go all
  404. the way to the null terminator. Once there, we know that
  405. there is always a two char postfix of "):" To not copy
  406. this into the msgline buffer, we delete those by setting
  407. them to null terminator.
  408. We have to set i to 3 here because of cpos being 1 ahead
  409. of the length everything.
  410. */
  411. for(int i = 3; i > 0; i--)
  412. buf[--*cpos] = '\0';
  413. strcat(full, buf);
  414. tb_set_cursor(start_col, MSGLINE);
  415. addstr(buf);
  416. clrtoeol(start_col + *cpos, MSGLINE);
  417. tb_set_cursor(start_col + *cpos, MSGLINE);
  418. full[start_col + *cpos] = '\0';
  419. return 1;
  420. } else if(ev.key == TB_KEY_BACKSPACE || ev.key == TB_KEY_BACKSPACE2) {
  421. if ((!isspace(buf[*cpos-1]) || !is_symbol(buf[*cpos-1])) && *cpos != 0) {
  422. *cpos = *cpos -1;
  423. for(i = *cpos; buf[i] != '\0'; i++) {
  424. buf[i] = buf[i+1];
  425. }
  426. }
  427. while ((isspace(buf[*cpos-1]) || is_symbol(buf[*cpos-1])) && *cpos != 0) {
  428. *cpos = *cpos -1;
  429. for(i = *cpos; buf[i] != '\0'; i++) {
  430. buf[i] = buf[i+1];
  431. }
  432. }
  433. while (!isspace(buf[*cpos-1]) && !is_symbol(buf[*cpos-1]) && *cpos != 0) {
  434. *cpos = *cpos -1;
  435. for(i = *cpos; buf[i] != '\0'; i++) {
  436. buf[i] = buf[i+1];
  437. }
  438. }
  439. strcat(full, buf);
  440. tb_set_cursor(start_col, MSGLINE);
  441. addstr(buf);
  442. clrtoeol(start_col + *cpos, MSGLINE);
  443. tb_set_cursor(start_col + *cpos, MSGLINE);
  444. full[start_col + *cpos] = '\0';
  445. return 1;
  446. }
  447. } else
  448. k = ev.key;
  449. switch(k) {
  450. case TB_KEY_CTRL_B:
  451. if(*cpos != 0)
  452. *cpos = *cpos - 1;
  453. tb_set_cursor(start_col + *cpos, MSGLINE);
  454. return 1;
  455. case TB_KEY_CTRL_F:
  456. if(*cpos != strlen(buf))
  457. *cpos = *cpos + 1;
  458. tb_set_cursor(start_col + *cpos, MSGLINE);
  459. return 1;
  460. case TB_KEY_HOME:
  461. case TB_KEY_CTRL_A:
  462. *cpos = 0;
  463. tb_set_cursor(start_col + *cpos, MSGLINE);
  464. return 1;
  465. case TB_KEY_END:
  466. case TB_KEY_CTRL_E:
  467. *cpos = strlen(buf);
  468. tb_set_cursor(start_col + *cpos, MSGLINE);
  469. return 1;
  470. case TB_KEY_CTRL_K:
  471. for(i = *cpos; buf[i] != '\0'; i++)
  472. buf[i] = '\0';
  473. strcat(full, buf);
  474. clrtoeol(start_col + *cpos, MSGLINE);
  475. return 1;
  476. case TB_KEY_DELETE:
  477. case TB_KEY_CTRL_D:
  478. if(*cpos == strlen(buf))
  479. break;
  480. for(i = *cpos; buf[i] != '\0'; i++) {
  481. buf[i] = buf[i+1];
  482. }
  483. strcat(full, buf);
  484. tb_set_cursor(start_col, MSGLINE);
  485. addstr(buf);
  486. clrtoeol(start_col + strlen(buf), MSGLINE);
  487. tb_set_cursor(start_col + *cpos, MSGLINE);
  488. full[start_col + *cpos] = '\0';
  489. return 1;
  490. case TB_KEY_BACKSPACE2: /* del, erase */
  491. case TB_KEY_BACKSPACE: /* backspace */
  492. if(*cpos == 0)
  493. return 1;
  494. *cpos = *cpos - 1;
  495. for(i = *cpos; buf[i] != '\0'; i++) {
  496. buf[i] = buf[i+1];
  497. }
  498. strcat(full, buf);
  499. tb_set_cursor(start_col, MSGLINE);
  500. addstr(buf);
  501. clrtoeol(start_col + *cpos, MSGLINE);
  502. tb_set_cursor(start_col + *cpos, MSGLINE);
  503. full[start_col + *cpos] = '\0';
  504. return 1;
  505. case TB_KEY_CTRL_Y: {
  506. if(scrap.data == NULL)
  507. return 0;
  508. int remaining = nbuf - strlen(buf) - 1;
  509. int size = scrap.len > remaining ? remaining : scrap.len;
  510. *cpos += size;
  511. strncat(buf, (const char *)scrap.data, size);
  512. remaining = PATH_MAX - strlen(full) - 1;
  513. size = strlen(buf) > remaining ? remaining : strlen(buf);
  514. strncat(full, buf, size);
  515. tb_set_cursor(start_col, MSGLINE);
  516. addstr(buf);
  517. clrtoeol(start_col + *cpos, MSGLINE);
  518. tb_set_cursor(start_col + *cpos, MSGLINE);
  519. full[start_col + *cpos] = '\0';
  520. return 1;
  521. }
  522. case TB_KEY_CTRL_Q: {
  523. struct tb_event ev1;
  524. if(tb_poll_event(&ev1) != TB_OK) return 0;
  525. if(ev1.key > 0x1a) {
  526. return 0;
  527. }
  528. buf[*cpos] = (char_t)ev1.key;
  529. *cpos += 1;
  530. tb_set_cursor(start_col + *cpos, MSGLINE);
  531. full[strlen(prompt)] = '\0';
  532. return 1;
  533. }
  534. default:
  535. return 0;
  536. }
  537. return 0;
  538. }
  539. void use_kbd_macro(struct tb_event *ev)
  540. {
  541. *ev = record_buffer[run_buffer_index];
  542. if(run_buffer_index > record_buffer_index) {
  543. if(numeric_argument > 0) {
  544. numeric_argument--;
  545. } else {
  546. execute_kbd_macro = FALSE;
  547. }
  548. run_buffer_index = 0;
  549. } else {
  550. run_buffer_index++;
  551. }
  552. ignorenotbound = TRUE;
  553. }