keys.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. */
  59. // Happy compiler
  60. tinyrl = tinyrl;
  61. key = key;
  62. return BOOL_TRUE;
  63. }
  64. bool_t tinyrl_key_yank(tinyrl_t *tinyrl, unsigned char key)
  65. {
  66. bool_t result = BOOL_FALSE;
  67. /*
  68. if (tinyrl->kill_string) {
  69. // insert the kill string at the current insertion point
  70. result = tinyrl_insert_text(tinyrl, tinyrl->kill_string);
  71. }
  72. */
  73. // Happy compiler
  74. tinyrl = tinyrl;
  75. key = key;
  76. return result;
  77. }
  78. // Default handler for crlf
  79. bool_t tinyrl_key_crlf(tinyrl_t *tinyrl, unsigned char key)
  80. {
  81. tinyrl_multi_crlf(tinyrl);
  82. tinyrl_reset_line_state(tinyrl);
  83. tinyrl_reset_line(tinyrl);
  84. tinyrl_reset_hist_pos(tinyrl);
  85. key = key; // Happy compiler
  86. return BOOL_TRUE;
  87. }
  88. bool_t tinyrl_key_up(tinyrl_t *tinyrl, unsigned char key)
  89. {
  90. const char *str = NULL;
  91. str = hist_pos(tinyrl->hist);
  92. // Up key is pressed for the first time and current line is new typed one
  93. if (!str) {
  94. hist_add(tinyrl->hist, tinyrl->line.str, BOOL_TRUE); // Temp entry
  95. hist_pos_up(tinyrl->hist); // Skip newly added temp entry
  96. }
  97. hist_pos_up(tinyrl->hist);
  98. str = hist_pos(tinyrl->hist);
  99. // Empty history
  100. if (!str)
  101. return BOOL_TRUE;
  102. tinyrl_line_replace(tinyrl, str);
  103. key = key; // Happy compiler
  104. return BOOL_TRUE;
  105. }
  106. bool_t tinyrl_key_down(tinyrl_t *tinyrl, unsigned char key)
  107. {
  108. const char *str = NULL;
  109. hist_pos_down(tinyrl->hist);
  110. str = hist_pos(tinyrl->hist);
  111. // Empty history
  112. if (!str)
  113. return BOOL_TRUE;
  114. tinyrl_line_replace(tinyrl, str);
  115. key = key; // Happy compiler
  116. return BOOL_TRUE;
  117. }
  118. bool_t tinyrl_key_left(tinyrl_t *tinyrl, unsigned char key)
  119. {
  120. if (tinyrl->line.pos == 0)
  121. return BOOL_TRUE;
  122. if (tinyrl->utf8)
  123. tinyrl->line.pos = utf8_move_left(tinyrl->line.str, tinyrl->line.pos);
  124. else
  125. tinyrl->line.pos--;
  126. // Happy compiler
  127. key = key;
  128. return BOOL_TRUE;
  129. }
  130. bool_t tinyrl_key_right(tinyrl_t *tinyrl, unsigned char key)
  131. {
  132. if (tinyrl->line.pos == tinyrl->line.len)
  133. return BOOL_TRUE;
  134. if (tinyrl->utf8)
  135. tinyrl->line.pos = utf8_move_right(tinyrl->line.str, tinyrl->line.pos);
  136. else
  137. tinyrl->line.pos++;
  138. // Happy compiler
  139. key = key;
  140. return BOOL_TRUE;
  141. }
  142. bool_t tinyrl_key_backspace(tinyrl_t *tinyrl, unsigned char key)
  143. {
  144. if (tinyrl->line.pos == 0)
  145. return BOOL_TRUE;
  146. if (tinyrl->utf8) {
  147. off_t new_pos = 0;
  148. new_pos = utf8_move_left(tinyrl->line.str, tinyrl->line.pos);
  149. tinyrl_line_delete(tinyrl, new_pos, tinyrl->line.pos - new_pos);
  150. } else {
  151. tinyrl_line_delete(tinyrl, tinyrl->line.pos - 1, 1);
  152. }
  153. // Happy compiler
  154. key = key;
  155. return BOOL_TRUE;
  156. }
  157. bool_t tinyrl_key_delete(tinyrl_t *tinyrl, unsigned char key)
  158. {
  159. if (tinyrl->line.pos == tinyrl->line.len)
  160. return BOOL_TRUE;
  161. if (tinyrl->utf8) {
  162. off_t new_pos = 0;
  163. new_pos = utf8_move_right(tinyrl->line.str, tinyrl->line.pos);
  164. tinyrl_line_delete(tinyrl, tinyrl->line.pos, new_pos - tinyrl->line.pos);
  165. } else {
  166. tinyrl_line_delete(tinyrl, tinyrl->line.pos, 1);
  167. }
  168. // Happy compiler
  169. key = key;
  170. return BOOL_TRUE;
  171. }
  172. bool_t tinyrl_key_backword(tinyrl_t *tinyrl, unsigned char key)
  173. {
  174. bool_t result = BOOL_FALSE;
  175. /*
  176. // remove current whitespace before cursor
  177. while (tinyrl->point > 0 && isspace(tinyrl->line[tinyrl->point - 1]))
  178. tinyrl_key_backspace(tinyrl, KEY_BS);
  179. // delete word before cusor
  180. while (tinyrl->point > 0 && !isspace(tinyrl->line[tinyrl->point - 1]))
  181. tinyrl_key_backspace(tinyrl, KEY_BS);
  182. result = BOOL_TRUE;
  183. */
  184. // Happy compiler
  185. tinyrl = tinyrl;
  186. key = key;
  187. return result;
  188. }
  189. bool_t tinyrl_key_clear_screen(tinyrl_t *tinyrl, unsigned char key)
  190. {
  191. vt100_clear_screen(tinyrl->term);
  192. vt100_cursor_home(tinyrl->term);
  193. tinyrl_reset_line_state(tinyrl);
  194. key = key; // Happy compiler
  195. return BOOL_TRUE;
  196. }
  197. bool_t tinyrl_key_erase_line(tinyrl_t *tinyrl, unsigned char key)
  198. {
  199. /* unsigned int end;
  200. // release any old kill string
  201. lub_string_free(tinyrl->kill_string);
  202. if (!tinyrl->point) {
  203. tinyrl->kill_string = NULL;
  204. return BOOL_TRUE;
  205. }
  206. end = tinyrl->point - 1;
  207. // store the killed string
  208. tinyrl->kill_string = malloc(tinyrl->point + 1);
  209. memcpy(tinyrl->kill_string, tinyrl->buffer, tinyrl->point);
  210. tinyrl->kill_string[tinyrl->point] = '\0';
  211. // delete the text from the start of the line
  212. tinyrl_delete_text(tinyrl, 0, end);
  213. tinyrl->point = 0;
  214. */
  215. // Happy compiler
  216. tinyrl = tinyrl;
  217. key = key;
  218. return BOOL_TRUE;
  219. }
  220. bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  221. {
  222. bool_t result = BOOL_FALSE;
  223. /*
  224. tinyrl_match_e status = tinyrl_complete_with_extensions(tinyrl);
  225. switch (status) {
  226. case TINYRL_COMPLETED_MATCH:
  227. case TINYRL_MATCH:
  228. // everything is OK with the world...
  229. result = tinyrl_insert_text(tinyrl, " ");
  230. break;
  231. case TINYRL_NO_MATCH:
  232. case TINYRL_MATCH_WITH_EXTENSIONS:
  233. case TINYRL_AMBIGUOUS:
  234. case TINYRL_COMPLETED_AMBIGUOUS:
  235. // oops don't change the result and let the bell ring
  236. break;
  237. }
  238. */
  239. // Happy compiler
  240. tinyrl = tinyrl;
  241. key = key;
  242. return result;
  243. }