keys.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <ctype.h>
  6. #include <errno.h>
  7. #include <unistd.h>
  8. #include <faux/faux.h>
  9. #include <faux/str.h>
  10. #include "private.h"
  11. bool_t tinyrl_key_default(tinyrl_t *tinyrl, unsigned char key)
  12. {
  13. if (key > 31) {
  14. // Inject new char to the line
  15. tinyrl_line_insert(tinyrl, (const char *)(&key), 1);
  16. } else {
  17. // Call the external hotkey analyzer
  18. if (tinyrl->hotkey_fn)
  19. tinyrl->hotkey_fn(tinyrl, key);
  20. }
  21. return BOOL_TRUE;
  22. }
  23. bool_t tinyrl_key_interrupt(tinyrl_t *tinyrl, unsigned char key)
  24. {
  25. tinyrl_multi_crlf(tinyrl);
  26. tinyrl_reset_line_state(tinyrl);
  27. tinyrl_reset_line(tinyrl);
  28. tinyrl_reset_hist_pos(tinyrl);
  29. // Happy compiler
  30. key = key;
  31. return BOOL_TRUE;
  32. }
  33. bool_t tinyrl_key_start_of_line(tinyrl_t *tinyrl, unsigned char key)
  34. {
  35. // Set current position to the start of the line
  36. tinyrl->line.pos = 0;
  37. // Happy compiler
  38. key = key;
  39. return BOOL_TRUE;
  40. }
  41. bool_t tinyrl_key_end_of_line(tinyrl_t *tinyrl, unsigned char key)
  42. {
  43. // Set current position to the end of the line
  44. tinyrl->line.pos = tinyrl->line.len;
  45. // Happy compiler
  46. key = key;
  47. return BOOL_TRUE;
  48. }
  49. bool_t tinyrl_key_kill(tinyrl_t *tinyrl, unsigned char key)
  50. {
  51. /*
  52. // release any old kill string
  53. lub_string_free(tinyrl->kill_string);
  54. // store the killed string
  55. tinyrl->kill_string = lub_string_dup(&tinyrl->buffer[tinyrl->point]);
  56. // delete the text to the end of the line
  57. tinyrl_delete_text(tinyrl, tinyrl->point, tinyrl->end);
  58. // keep the compiler happy
  59. key = key;
  60. */
  61. return BOOL_TRUE;
  62. }
  63. bool_t tinyrl_key_yank(tinyrl_t *tinyrl, unsigned char key)
  64. {
  65. bool_t result = BOOL_FALSE;
  66. /*
  67. if (tinyrl->kill_string) {
  68. // insert the kill string at the current insertion point
  69. result = tinyrl_insert_text(tinyrl, tinyrl->kill_string);
  70. }
  71. // keep the compiler happy
  72. key = key;
  73. */
  74. return result;
  75. }
  76. // Default handler for crlf
  77. bool_t tinyrl_key_crlf(tinyrl_t *tinyrl, unsigned char key)
  78. {
  79. tinyrl_multi_crlf(tinyrl);
  80. tinyrl_reset_line_state(tinyrl);
  81. tinyrl_reset_line(tinyrl);
  82. tinyrl_reset_hist_pos(tinyrl);
  83. key = key; // Happy compiler
  84. return BOOL_TRUE;
  85. }
  86. bool_t tinyrl_key_up(tinyrl_t *tinyrl, unsigned char key)
  87. {
  88. const char *str = NULL;
  89. str = hist_pos(tinyrl->hist);
  90. // Up key is pressed for the first time and current line is new typed one
  91. if (!str) {
  92. hist_add(tinyrl->hist, tinyrl->line.str, BOOL_TRUE); // Temp entry
  93. hist_pos_up(tinyrl->hist); // Skip newly added temp entry
  94. }
  95. hist_pos_up(tinyrl->hist);
  96. str = hist_pos(tinyrl->hist);
  97. // Empty history
  98. if (!str)
  99. return BOOL_TRUE;
  100. tinyrl_line_replace(tinyrl, str);
  101. return BOOL_TRUE;
  102. }
  103. bool_t tinyrl_key_down(tinyrl_t *tinyrl, unsigned char key)
  104. {
  105. const char *str = NULL;
  106. hist_pos_down(tinyrl->hist);
  107. str = hist_pos(tinyrl->hist);
  108. // Empty history
  109. if (!str)
  110. return BOOL_TRUE;
  111. tinyrl_line_replace(tinyrl, str);
  112. return BOOL_TRUE;
  113. }
  114. bool_t tinyrl_key_left(tinyrl_t *tinyrl, unsigned char key)
  115. {
  116. if (tinyrl->line.pos == 0)
  117. return BOOL_TRUE;
  118. if (tinyrl->utf8)
  119. tinyrl->line.pos = utf8_move_left(tinyrl->line.str, tinyrl->line.pos);
  120. else
  121. tinyrl->line.pos--;
  122. // Happy compiler
  123. key = key;
  124. return BOOL_TRUE;
  125. }
  126. bool_t tinyrl_key_right(tinyrl_t *tinyrl, unsigned char key)
  127. {
  128. if (tinyrl->line.pos == tinyrl->line.len)
  129. return BOOL_TRUE;
  130. if (tinyrl->utf8)
  131. tinyrl->line.pos = utf8_move_right(tinyrl->line.str, tinyrl->line.pos);
  132. else
  133. tinyrl->line.pos++;
  134. // Happy compiler
  135. key = key;
  136. return BOOL_TRUE;
  137. }
  138. bool_t tinyrl_key_backspace(tinyrl_t *tinyrl, unsigned char key)
  139. {
  140. if (tinyrl->line.pos == 0)
  141. return BOOL_TRUE;
  142. if (tinyrl->utf8) {
  143. off_t new_pos = 0;
  144. new_pos = utf8_move_left(tinyrl->line.str, tinyrl->line.pos);
  145. tinyrl_line_delete(tinyrl, new_pos, tinyrl->line.pos - new_pos);
  146. } else {
  147. tinyrl_line_delete(tinyrl, tinyrl->line.pos - 1, 1);
  148. }
  149. // Happy compiler
  150. key = key;
  151. return BOOL_TRUE;
  152. }
  153. bool_t tinyrl_key_delete(tinyrl_t *tinyrl, unsigned char key)
  154. {
  155. if (tinyrl->line.pos == tinyrl->line.len)
  156. return BOOL_TRUE;
  157. if (tinyrl->utf8) {
  158. off_t new_pos = 0;
  159. new_pos = utf8_move_right(tinyrl->line.str, tinyrl->line.pos);
  160. tinyrl_line_delete(tinyrl, tinyrl->line.pos, new_pos - tinyrl->line.pos);
  161. } else {
  162. tinyrl_line_delete(tinyrl, tinyrl->line.pos, 1);
  163. }
  164. // Happy compiler
  165. key = key;
  166. return BOOL_TRUE;
  167. }
  168. bool_t tinyrl_key_backword(tinyrl_t *tinyrl, unsigned char key)
  169. {
  170. bool_t result = BOOL_FALSE;
  171. /*
  172. // remove current whitespace before cursor
  173. while (tinyrl->point > 0 && isspace(tinyrl->line[tinyrl->point - 1]))
  174. tinyrl_key_backspace(tinyrl, KEY_BS);
  175. // delete word before cusor
  176. while (tinyrl->point > 0 && !isspace(tinyrl->line[tinyrl->point - 1]))
  177. tinyrl_key_backspace(tinyrl, KEY_BS);
  178. result = BOOL_TRUE;
  179. // keep the compiler happy
  180. key = key;
  181. */
  182. return result;
  183. }
  184. bool_t tinyrl_key_clear_screen(tinyrl_t *tinyrl, unsigned char key)
  185. {
  186. vt100_clear_screen(tinyrl->term);
  187. vt100_cursor_home(tinyrl->term);
  188. tinyrl_reset_line_state(tinyrl);
  189. key = key; // Happy compiler
  190. return BOOL_TRUE;
  191. }
  192. bool_t tinyrl_key_erase_line(tinyrl_t *tinyrl, unsigned char key)
  193. {
  194. /* unsigned int end;
  195. // release any old kill string
  196. lub_string_free(tinyrl->kill_string);
  197. if (!tinyrl->point) {
  198. tinyrl->kill_string = NULL;
  199. return BOOL_TRUE;
  200. }
  201. end = tinyrl->point - 1;
  202. // store the killed string
  203. tinyrl->kill_string = malloc(tinyrl->point + 1);
  204. memcpy(tinyrl->kill_string, tinyrl->buffer, tinyrl->point);
  205. tinyrl->kill_string[tinyrl->point] = '\0';
  206. // delete the text from the start of the line
  207. tinyrl_delete_text(tinyrl, 0, end);
  208. tinyrl->point = 0;
  209. // keep the compiler happy
  210. key = key;
  211. tinyrl = tinyrl;
  212. */
  213. return BOOL_TRUE;
  214. }
  215. bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  216. {
  217. bool_t result = BOOL_FALSE;
  218. /*
  219. tinyrl_match_e status = tinyrl_complete_with_extensions(tinyrl);
  220. switch (status) {
  221. case TINYRL_COMPLETED_MATCH:
  222. case TINYRL_MATCH:
  223. // everything is OK with the world...
  224. result = tinyrl_insert_text(tinyrl, " ");
  225. break;
  226. case TINYRL_NO_MATCH:
  227. case TINYRL_MATCH_WITH_EXTENSIONS:
  228. case TINYRL_AMBIGUOUS:
  229. case TINYRL_COMPLETED_AMBIGUOUS:
  230. // oops don't change the result and let the bell ring
  231. break;
  232. }
  233. // keep the compiler happy
  234. key = key;
  235. */
  236. return result;
  237. }