key.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. #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-r search ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_R, -1}, search_rev, UA_NOTHING },
  26. {"C-o newline-below ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_O, -1}, insertnewlinebelow, UA_REPEAT },
  27. {"C-s search ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_S, -1}, search_fwd, UA_NOTHING },
  28. {"C-t transpose ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_T, -1}, transpose, UA_REPEAT },
  29. {"C-u universal-argument ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_U, -1}, universal_argument_load, UA_NOTHING },
  30. {"C-v forward-page ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_V, -1}, pgdown, UA_REPEAT },
  31. {"C-w kill-region ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_W, -1}, cut, UA_CUSTOM },
  32. {"C-y yank ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_Y, -1}, paste, UA_CUSTOM },
  33. {"C-z suspend ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_Z, -1}, suspend, UA_NOTHING },
  34. {"C-space set-mark ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_TILDE, -1}, iblock, UA_NOTHING },
  35. {"C-g remove-mark ", TB_MOD_CTRL, (int[2]){TB_KEY_CTRL_G, -1}, unmark, UA_NOTHING },
  36. {"C-x 0 delete-other-window", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '0', -1}, delete_other_windows, UA_NOTHING },
  37. {"C-x 1 delete-other-window", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '1', -1}, delete_other_windows, UA_NOTHING },
  38. {"C-x 2 split-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '2', -1}, split_window, UA_NOTHING },
  39. {"C-x 3 chop-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '3', -1}, chop_window, UA_NOTHING },
  40. {"C-x 4 tri-split-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '4', -1}, tri_split_window, UA_NOTHING },
  41. {"C-x 5 tri-chop-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '5', -1}, tri_chop_window, UA_NOTHING },
  42. {"C-x 6 fib-right-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '6', -1}, fib_right, UA_NOTHING },
  43. {"C-x 7 fib-left-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '7', -1}, fib_left, UA_NOTHING },
  44. {"C-x 8 quad-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '8', -1}, quad_window, UA_NOTHING },
  45. {"C-x o other-window ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'o', -1}, next_window, UA_NOTHING },
  46. {"C-x = cursor-position ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '=', -1}, showpos, UA_NOTHING },
  47. {"C-x i insert-file ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'i', -1}, insertfile, UA_NOTHING },
  48. {"C-x k kill-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'k', -1}, killbuffer, UA_NOTHING },
  49. {"C-x C-n next-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_N, -1}, next_buffer, UA_NOTHING },
  50. {"C-x n next-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'n', -1}, next_buffer, UA_NOTHING },
  51. {"C-x l last-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'l', -1}, last_buffer, UA_NOTHING },
  52. {"C-x b switch-to-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'b', -1}, switch_buffer, UA_NOTHING },
  53. {"C-x ( start-kbd-macro ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, '(', -1}, start_kbd_macro, UA_NOTHING },
  54. {"C-x ) start-kbd-macro ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, ')', -1}, end_kbd_macro, UA_NOTHING },
  55. /* UA_PREVENT is misleading here, since it uses the numeric_argument in the get_key function */
  56. {"C-x e run-kbd-macro ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, 'e', -1}, run_kbd_macro, UA_PREVENT },
  57. {"C-x C-f find-file ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_F, -1}, readfile, UA_NOTHING },
  58. {"C-x C-s save-buffer ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_S, -1}, savebuffer, UA_NOTHING },
  59. {"C-x C-g umark ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_G, -1}, unmark, UA_NOTHING },
  60. {"C-x C-w write-file ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_W, -1}, writefile, UA_NOTHING },
  61. {"C-x C-c exit ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_C, -1}, quit_ask, UA_NOTHING },
  62. {"C-x C-x pop-to-mark ", TB_MOD_CTRL, (int[3]){TB_KEY_CTRL_X, TB_KEY_CTRL_X, -1}, poptomark, UA_NOTHING },
  63. {"esc 0 numeric-arg-0 ", TB_MOD_ALT, (int[2]){'0', -1}, numeric_argument_load, UA_PREVENT },
  64. {"esc 1 numeric-arg-1 ", TB_MOD_ALT, (int[2]){'1', -1}, numeric_argument_load, UA_PREVENT },
  65. {"esc 2 numeric-arg-2 ", TB_MOD_ALT, (int[2]){'2', -1}, numeric_argument_load, UA_PREVENT },
  66. {"esc 3 numeric-arg-3 ", TB_MOD_ALT, (int[2]){'3', -1}, numeric_argument_load, UA_PREVENT },
  67. {"esc 4 numeric-arg-4 ", TB_MOD_ALT, (int[2]){'4', -1}, numeric_argument_load, UA_PREVENT },
  68. {"esc 5 numeric-arg-5 ", TB_MOD_ALT, (int[2]){'5', -1}, numeric_argument_load, UA_PREVENT },
  69. {"esc 6 numeric-arg-6 ", TB_MOD_ALT, (int[2]){'6', -1}, numeric_argument_load, UA_PREVENT },
  70. {"esc 7 numeric-arg-7 ", TB_MOD_ALT, (int[2]){'7', -1}, numeric_argument_load, UA_PREVENT },
  71. {"esc 8 numeric-arg-8 ", TB_MOD_ALT, (int[2]){'8', -1}, numeric_argument_load, UA_PREVENT },
  72. {"esc 9 numeric-arg-9 ", TB_MOD_ALT, (int[2]){'9', -1}, numeric_argument_load, UA_PREVENT },
  73. {"esc b back-word ", TB_MOD_ALT, (int[2]){'b', -1}, wleft, UA_REPEAT },
  74. {"esc bksp back-word-delete", TB_MOD_CTRL | TB_MOD_ALT, (int[2]){TB_KEY_BACKSPACE2, -1}, wleftdelete, UA_REPEAT },
  75. {"esc bksp back-word-delete", TB_MOD_CTRL | TB_MOD_ALT, (int[2]){TB_KEY_BACKSPACE, -1}, wleftdelete, UA_REPEAT },
  76. {"esc f fwd-word ", TB_MOD_ALT, (int[2]){'f', -1}, wright, UA_REPEAT },
  77. {"esc d fwd-word-delete ", TB_MOD_ALT, (int[2]){'d', -1}, wrightdelete, UA_REPEAT },
  78. {"esc x execute-shell-cmd ", TB_MOD_ALT, (int[2]){'x', -1}, insert_from_shell, UA_NOTHING },
  79. {"esc g gotoline ", TB_MOD_ALT, (int[2]){'g', -1}, gotoline, UA_NOTHING },
  80. {"esc i indent ", TB_MOD_ALT, (int[2]){'i', -1}, inserttab, UA_REPEAT },
  81. {"esc k kill-region ", TB_MOD_ALT, (int[2]){'k', -1}, cut, UA_NOTHING },
  82. {"esc m back-to-indentation", TB_MOD_ALT, (int[2]){'m', -1}, back_to_indentation, UA_NOTHING },
  83. {"esc n negate ", TB_MOD_ALT, (int[2]){'-', -1}, negate, UA_NOTHING },
  84. {"esc o open-shell-cmd ", TB_MOD_ALT, (int[2]){'o', -1}, open_file_from_shell, UA_NOTHING },
  85. {"esc % query-replace ", TB_MOD_ALT, (int[2]){'%', -1}, query_replace, UA_NOTHING },
  86. {"esc v backward-page ", TB_MOD_ALT, (int[2]){'v', -1}, pgup, UA_REPEAT },
  87. {"esc w copy-region ", TB_MOD_ALT, (int[2]){'w', -1}, copy, UA_NOTHING },
  88. {"esc @ set-mark ", TB_MOD_ALT, (int[2]){'@', -1}, iblock, UA_NOTHING },
  89. {"esc < beg-of-buf ", TB_MOD_ALT, (int[2]){'<', -1}, top, UA_NOTHING },
  90. {"esc > end-of-buf ", TB_MOD_ALT, (int[2]){'>', -1}, bottom, UA_NOTHING },
  91. {"esc \\ delete-whitespace ", TB_MOD_ALT, (int[2]){'\\', -1}, deletewhitespacebetween, UA_NOTHING },
  92. {"esc / redo ", TB_MOD_ALT, (int[2]){'/', -1}, redo, UA_NOTHING },
  93. {"esc t transpose word ", TB_MOD_ALT, (int[2]){'t', -1}, transposeword, UA_REPEAT },
  94. {"esc l lowercase-word ", TB_MOD_ALT, (int[2]){'l', -1}, lowercaseword, UA_REPEAT },
  95. {"esc c capitalize-word ", TB_MOD_ALT, (int[2]){'c', -1}, capitalizeword, UA_REPEAT },
  96. {"esc u uppercase-word ", TB_MOD_ALT, (int[2]){'u', -1}, uppercaseword, UA_REPEAT },
  97. {"esc ; jump-to-char ", TB_MOD_ALT, (int[2]){';', -1}, jumptochar, UA_REPEAT },
  98. {"esc : jump-to-char ", TB_MOD_ALT, (int[2]){':', -1}, negated_jumptochar, UA_REPEAT },
  99. {"esc z zap-to-char ", TB_MOD_ALT, (int[2]){'z', -1}, zaptochar, UA_REPEAT },
  100. {"esc Z zap-to-char ", TB_MOD_ALT, (int[2]){'Z', -1}, negated_zaptochar, UA_REPEAT },
  101. {"esc home, beg-of-buf ", 0, (int[2]){TB_KEY_HOME, -1}, top, UA_NOTHING },
  102. {"esc end, end-of-buf ", 0, (int[2]){TB_KEY_END, -1}, bottom, UA_NOTHING },
  103. {"esc esc show-version ", 0, (int[2]){TB_KEY_ESC, -1}, version, UA_NOTHING },
  104. {"ins toggle-overwrite-mode", 0, (int[2]){TB_KEY_INSERT, -1}, toggle_overwrite_mode , UA_NOTHING },
  105. {"del forward-delete-char ", 0, (int[2]){TB_KEY_DELETE, -1}, delete, UA_REPEAT },
  106. {"backspace delete-left ", 0, (int[2]){TB_KEY_BACKSPACE, -1}, backsp, UA_REPEAT },
  107. {"backspace delete-left ", 0, (int[2]){TB_KEY_BACKSPACE2, -1}, backsp, UA_REPEAT },
  108. {"up previous-line ", 0, (int[2]){TB_KEY_ARROW_UP, -1}, up, UA_REPEAT },
  109. {"down next-line ", 0, (int[2]){TB_KEY_ARROW_DOWN, -1}, down, UA_REPEAT },
  110. {"left backward-character ", 0, (int[2]){TB_KEY_ARROW_LEFT, -1}, left, UA_REPEAT },
  111. {"right forward-character ", 0, (int[2]){TB_KEY_ARROW_RIGHT, -1}, right, UA_REPEAT },
  112. {"pgup backward-page ", 0, (int[2]){TB_KEY_PGUP, -1}, pgup , UA_REPEAT },
  113. {"pgdn forward-page ", 0, (int[2]){TB_KEY_PGDN, -1}, pgdown, UA_REPEAT },
  114. // {"resize resize-terminal ", 0, (int[2]){TB_KEY_}, resize_terminal , UA_NOTHING },
  115. {"C-M-f forward-bracket ", TB_MOD_CTRL | TB_MOD_ALT, (int[2]){TB_KEY_CTRL_F, -1}, forward_bracket, UA_REPEAT },
  116. {"C-M-b backward-bracket ", TB_MOD_CTRL | TB_MOD_ALT, (int[2]){TB_KEY_CTRL_B, -1}, backward_bracket, UA_REPEAT },
  117. {"K_ERROR ", 0, NULL, NULL, UA_NOTHING }
  118. };
  119. char_t *get_key(keymap_t *keys, keymap_t **key_return)
  120. {
  121. keymap_t *k;
  122. int ret;
  123. static char_t buffer[K_BUFFER_LENGTH];
  124. static char_t *record = buffer;
  125. struct tb_event ev;
  126. *key_return = NULL;
  127. /* if recorded bytes remain, return next recorded byte. */
  128. if (*record != '\0') {
  129. *key_return = NULL;
  130. return record++;
  131. }
  132. /* reset record buffer. */
  133. record = buffer;
  134. if(execute_kbd_macro) {
  135. ev = record_buffer[run_buffer_index];
  136. ret = TB_OK;
  137. if(run_buffer_index > record_buffer_index) {
  138. if(numeric_argument > 0) {
  139. numeric_argument--;
  140. } else {
  141. execute_kbd_macro = FALSE;
  142. }
  143. run_buffer_index = 0;
  144. } else {
  145. run_buffer_index++;
  146. }
  147. } else {
  148. ret = tb_poll_event(&ev);
  149. }
  150. if(ret == TB_OK) {
  151. assert(K_BUFFER_LENGTH > record - buffer);
  152. if(ev.type == TB_EVENT_RESIZE) {
  153. resize_terminal();
  154. *key_return = NULL;
  155. return (record++);
  156. } else if(ev.type == TB_EVENT_MOUSE) {
  157. *key_return = NULL;
  158. return (record++);
  159. }
  160. *record++ = (unsigned)ev.ch;
  161. *record = '\0';
  162. if(record_input) {
  163. record_buffer[record_buffer_index] = ev;
  164. record_buffer_index++;
  165. }
  166. /* if recorded bytes match any multi-byte sequence... */
  167. for (k = keys; k->key_bytes != NULL; ++k) {
  168. if(k->key_mod == ev.mod &&
  169. (k->key_bytes[submatch] == ev.key ||
  170. (ev.ch != 0 && k->key_bytes[submatch] == (int)ev.ch))) {
  171. if(k->key_bytes[submatch+1] != -1) {
  172. submatch++;
  173. msg("C-x");
  174. break;
  175. } else {
  176. record = buffer;
  177. *record = '\0';
  178. *key_return = k;
  179. input_char = ev.ch;
  180. return record;
  181. }
  182. }
  183. if(submatch > 0 && k->key_bytes[submatch] == (int)ev.ch) {
  184. record = buffer;
  185. *record = '\0';
  186. *key_return = k;
  187. input_char = ev.ch;
  188. return record;
  189. }
  190. }
  191. unicode_buf[tb_utf8_unicode_to_char(unicode_buf, ev.ch)] = '\0';
  192. /* If no match is found, kill the loop. */
  193. if(key_return == NULL) {
  194. submatch = 0;
  195. }
  196. }
  197. /* nothing matched, return recorded bytes. */
  198. record = buffer;
  199. return (record++);
  200. }
  201. int getinput(char *prompt, char *buf, int nbuf, int flag, int allowblank)
  202. {
  203. int cpos = 0;
  204. int c;
  205. int start_col = strlen(prompt);
  206. struct tb_event ev;
  207. print_to_msgline(prompt);
  208. clrtoeol(prompt, MSGLINE);
  209. if (flag == F_CLEAR) buf[0] = '\0';
  210. /* if we have a default value print it and go to end of it */
  211. if (buf[0] != '\0') {
  212. addstr(buf);
  213. cpos = strlen(buf);
  214. }
  215. for (;;) {
  216. tb_present();
  217. if(tb_poll_event(&ev) != TB_OK) return 0;
  218. if(msgline_editor(ev, prompt, buf, &cpos))
  219. continue;
  220. if(!ev.mod)
  221. c = ev.ch;
  222. else
  223. c = ev.key;
  224. /* ignore control keys other than backspace, cr, lf */
  225. if (c < 32 &&
  226. c != TB_KEY_CTRL_G &&
  227. c != TB_KEY_BACKSPACE &&
  228. c != TB_KEY_CTRL_J &&
  229. c != TB_KEY_ENTER)
  230. continue;
  231. switch(c) {
  232. case TB_KEY_CTRL_J: /* cr, lf */
  233. case TB_KEY_ENTER:
  234. buf[cpos] = '\0';
  235. return (cpos > 0 ? TRUE : allowblank);
  236. case TB_KEY_CTRL_G: /* ctrl-g */
  237. return FALSE;
  238. case TB_KEY_BACKSPACE2: /* del, erase */
  239. case TB_KEY_BACKSPACE: /* backspace */
  240. if (cpos == 0)
  241. continue;
  242. tb_set_cursor(start_col + cpos - 1, MSGLINE);
  243. addch(' ');
  244. tb_set_cursor(start_col + cpos - 1, MSGLINE);
  245. buf[--cpos] = '\0';
  246. break;
  247. default:
  248. if (cpos < nbuf -1) {
  249. for(int i = strlen(buf); i > cpos; i--) {
  250. buf[i] = buf[i - 1];
  251. }
  252. buf[cpos] = c;
  253. tb_set_cursor(start_col, MSGLINE);
  254. addstr(buf);
  255. cpos++;
  256. tb_set_cursor(start_col + cpos, MSGLINE);
  257. }
  258. break;
  259. }
  260. }
  261. }
  262. int msgline_editor(struct tb_event ev, char *prompt, char *buf, int *cpos)
  263. {
  264. char full[NAME_MAX*2];
  265. int k = 0, i = 0, start_col = strlen(prompt);
  266. strcpy(full, prompt);
  267. if(ev.mod == TB_MOD_ALT || ev.mod == (TB_MOD_CTRL | TB_MOD_ALT)) {
  268. k = ev.ch;
  269. if(k == 'b') {
  270. if ((!isspace(buf[*cpos]) || !is_symbol(buf[*cpos])) && *cpos != 0)
  271. *cpos = *cpos -1;
  272. while ((isspace(buf[*cpos]) || is_symbol(buf[*cpos])) && *cpos != 0)
  273. *cpos = *cpos -1;
  274. while (!isspace(buf[*cpos]) && !is_symbol(buf[*cpos]) && *cpos != 0)
  275. *cpos = *cpos -1;
  276. tb_set_cursor(start_col + *cpos, MSGLINE);
  277. return 1;
  278. } else if(k == 'f') {
  279. if ((!isspace(buf[*cpos]) || !is_symbol(buf[*cpos])) && *cpos != strlen(buf))
  280. *cpos = *cpos + 1;
  281. while ((isspace(buf[*cpos]) || is_symbol(buf[*cpos])) && *cpos != strlen(buf))
  282. *cpos = *cpos + 1;
  283. while (!isspace(buf[*cpos]) && !is_symbol(buf[*cpos]) && *cpos != strlen(buf))
  284. *cpos = *cpos + 1;
  285. tb_set_cursor(start_col + *cpos, MSGLINE);
  286. return 1;
  287. } else if(k == 'd') {
  288. if ((!isspace(buf[*cpos]) || !is_symbol(buf[*cpos])) && *cpos != strlen(buf)) {
  289. for(i = *cpos; buf[i] != '\0'; i++) {
  290. buf[i] = buf[i+1];
  291. }
  292. }
  293. while ((isspace(buf[*cpos]) || is_symbol(buf[*cpos])) && *cpos != strlen(buf)) {
  294. for(i = *cpos; buf[i] != '\0'; i++) {
  295. buf[i] = buf[i+1];
  296. }
  297. }
  298. while (!isspace(buf[*cpos]) && !is_symbol(buf[*cpos]) && *cpos != strlen(buf)) {
  299. for(i = *cpos; buf[i] != '\0'; i++) {
  300. buf[i] = buf[i+1];
  301. }
  302. }
  303. strcat(full, buf);
  304. tb_set_cursor(start_col, MSGLINE);
  305. addstr(buf);
  306. clrtoeol(full, MSGLINE);
  307. tb_set_cursor(start_col + *cpos, MSGLINE);
  308. full[strlen(prompt)] = '\0';
  309. return 1;
  310. } else if(ev.key == TB_KEY_BACKSPACE || ev.key == TB_KEY_BACKSPACE2) {
  311. if ((!isspace(buf[*cpos-1]) || !is_symbol(buf[*cpos-1])) && *cpos != 0) {
  312. *cpos = *cpos -1;
  313. for(i = *cpos; buf[i] != '\0'; i++) {
  314. buf[i] = buf[i+1];
  315. }
  316. }
  317. while ((isspace(buf[*cpos-1]) || is_symbol(buf[*cpos-1])) && *cpos != 0) {
  318. *cpos = *cpos -1;
  319. for(i = *cpos; buf[i] != '\0'; i++) {
  320. buf[i] = buf[i+1];
  321. }
  322. }
  323. while (!isspace(buf[*cpos-1]) && !is_symbol(buf[*cpos-1]) && *cpos != 0) {
  324. *cpos = *cpos -1;
  325. for(i = *cpos; buf[i] != '\0'; i++) {
  326. buf[i] = buf[i+1];
  327. }
  328. }
  329. strcat(full, buf);
  330. tb_set_cursor(start_col, MSGLINE);
  331. addstr(buf);
  332. clrtoeol(full, MSGLINE);
  333. tb_set_cursor(start_col + *cpos, MSGLINE);
  334. full[strlen(prompt)] = '\0';
  335. return 1;
  336. }
  337. } else
  338. k = ev.key;
  339. switch(k) {
  340. case TB_KEY_CTRL_B:
  341. if(*cpos != 0)
  342. *cpos = *cpos - 1;
  343. tb_set_cursor(start_col + *cpos, MSGLINE);
  344. return 1;
  345. case TB_KEY_CTRL_F:
  346. if(*cpos != strlen(buf))
  347. *cpos = *cpos + 1;
  348. tb_set_cursor(start_col + *cpos, MSGLINE);
  349. return 1;
  350. case TB_KEY_HOME:
  351. case TB_KEY_CTRL_A:
  352. *cpos = 0;
  353. tb_set_cursor(start_col + *cpos, MSGLINE);
  354. return 1;
  355. case TB_KEY_END:
  356. case TB_KEY_CTRL_E:
  357. *cpos = strlen(buf);
  358. tb_set_cursor(start_col + *cpos, MSGLINE);
  359. return 1;
  360. case TB_KEY_CTRL_K:
  361. for(i = *cpos; buf[i] != '\0'; i++)
  362. buf[i] = '\0';
  363. strcat(full, buf);
  364. clrtoeol(full, MSGLINE);
  365. return 1;
  366. case TB_KEY_DELETE:
  367. case TB_KEY_CTRL_D:
  368. if(*cpos == strlen(buf))
  369. break;
  370. for(i = *cpos; buf[i] != '\0'; i++) {
  371. buf[i] = buf[i+1];
  372. }
  373. strcat(full, buf);
  374. tb_set_cursor(start_col, MSGLINE);
  375. addstr(buf);
  376. clrtoeol(full, MSGLINE);
  377. tb_set_cursor(start_col + *cpos, MSGLINE);
  378. full[strlen(prompt)] = '\0';
  379. return 1;
  380. case TB_KEY_BACKSPACE2: /* del, erase */
  381. case TB_KEY_BACKSPACE: /* backspace */
  382. if(*cpos == 0)
  383. return 1;
  384. *cpos = *cpos - 1;
  385. for(i = *cpos; buf[i] != '\0'; i++) {
  386. buf[i] = buf[i+1];
  387. }
  388. strcat(full, buf);
  389. tb_set_cursor(start_col, MSGLINE);
  390. addstr(buf);
  391. clrtoeol(full, MSGLINE);
  392. tb_set_cursor(start_col + *cpos, MSGLINE);
  393. full[strlen(prompt)] = '\0';
  394. return 1;
  395. case TB_KEY_CTRL_Y:
  396. if(scrap == NULL)
  397. return 0;
  398. *cpos += nscrap;
  399. strncat(buf, (const char *)scrap, nscrap);
  400. strcat(full, buf);
  401. tb_set_cursor(start_col, MSGLINE);
  402. addstr(buf);
  403. clrtoeol(full, MSGLINE);
  404. tb_set_cursor(start_col + *cpos, MSGLINE);
  405. full[strlen(prompt)] = '\0';
  406. return 1;
  407. default:
  408. return 0;
  409. }
  410. return 0;
  411. }