keys.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. printf("key=%u, pos=%lu, len=%lu\n", (unsigned char)key, tinyrl->line.pos, tinyrl->line.len);
  22. return BOOL_TRUE;
  23. }
  24. bool_t tinyrl_key_interrupt(tinyrl_t *tinyrl, unsigned char key)
  25. {
  26. // tinyrl_crlf(tinyrl);
  27. // tinyrl->done = BOOL_TRUE;
  28. tinyrl_line_delete(tinyrl, 0, tinyrl->line.len);
  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. bool_t tinyrl_key_crlf(tinyrl_t *tinyrl, unsigned char key)
  77. {
  78. /*
  79. tinyrl_crlf(tinyrl);
  80. tinyrl->done = BOOL_TRUE;
  81. // keep the compiler happy
  82. key = key;
  83. */
  84. return BOOL_TRUE;
  85. }
  86. bool_t tinyrl_key_up(tinyrl_t *tinyrl, unsigned char key)
  87. {
  88. bool_t result = BOOL_FALSE;
  89. /*
  90. tinyrl_history_entry_t *entry = NULL;
  91. if (tinyrl->line == tinyrl->buffer) {
  92. // go to the last history entry
  93. entry = tinyrl_history_getlast(tinyrl->history, &tinyrl->hist_iter);
  94. } else {
  95. // already traversing the history list so get previous
  96. entry = tinyrl_history_getprevious(&tinyrl->hist_iter);
  97. }
  98. if (entry) {
  99. // display the entry moving the insertion point
  100. * to the end of the line
  101. tinyrl->line = tinyrl_history_entry__get_line(entry);
  102. tinyrl->point = tinyrl->end = strlen(tinyrl->line);
  103. result = BOOL_TRUE;
  104. }
  105. // keep the compiler happy
  106. key = key;
  107. */
  108. return result;
  109. }
  110. bool_t tinyrl_key_down(tinyrl_t *tinyrl, unsigned char key)
  111. {
  112. bool_t result = BOOL_FALSE;
  113. /*
  114. if (tinyrl->line != tinyrl->buffer) {
  115. // we are not already at the bottom
  116. // the iterator will have been set up by the key_up() function
  117. tinyrl_history_entry_t *entry =
  118. tinyrl_history_getnext(&tinyrl->hist_iter);
  119. if (!entry) {
  120. // nothing more in the history list
  121. tinyrl->line = tinyrl->buffer;
  122. } else {
  123. tinyrl->line = tinyrl_history_entry__get_line(entry);
  124. }
  125. // display the entry moving the insertion point
  126. // to the end of the line
  127. tinyrl->point = tinyrl->end = strlen(tinyrl->line);
  128. result = BOOL_TRUE;
  129. }
  130. // keep the compiler happy
  131. key = key;
  132. */
  133. return result;
  134. }
  135. bool_t tinyrl_key_left(tinyrl_t *tinyrl, unsigned char key)
  136. {
  137. if (tinyrl->line.pos == 0)
  138. return BOOL_TRUE;
  139. if (tinyrl->utf8)
  140. tinyrl->line.pos = utf8_move_left(tinyrl->line.str, tinyrl->line.pos);
  141. else
  142. tinyrl->line.pos--;
  143. // Happy compiler
  144. key = key;
  145. return BOOL_TRUE;
  146. }
  147. bool_t tinyrl_key_right(tinyrl_t *tinyrl, unsigned char key)
  148. {
  149. if (tinyrl->line.pos == tinyrl->line.len)
  150. return BOOL_TRUE;
  151. if (tinyrl->utf8)
  152. tinyrl->line.pos = utf8_move_right(tinyrl->line.str, tinyrl->line.pos);
  153. else
  154. tinyrl->line.pos++;
  155. // Happy compiler
  156. key = key;
  157. return BOOL_TRUE;
  158. }
  159. bool_t tinyrl_key_backspace(tinyrl_t *tinyrl, unsigned char key)
  160. {
  161. if (tinyrl->line.pos == 0)
  162. return BOOL_TRUE;
  163. if (tinyrl->utf8) {
  164. off_t new_pos = 0;
  165. new_pos = utf8_move_left(tinyrl->line.str, tinyrl->line.pos);
  166. tinyrl_line_delete(tinyrl, new_pos, tinyrl->line.pos - new_pos);
  167. } else {
  168. tinyrl_line_delete(tinyrl, tinyrl->line.pos - 1, 1);
  169. }
  170. // Happy compiler
  171. key = key;
  172. return BOOL_TRUE;
  173. }
  174. bool_t tinyrl_key_delete(tinyrl_t *tinyrl, unsigned char key)
  175. {
  176. if (tinyrl->line.pos == tinyrl->line.len)
  177. return BOOL_TRUE;
  178. if (tinyrl->utf8) {
  179. off_t new_pos = 0;
  180. new_pos = utf8_move_right(tinyrl->line.str, tinyrl->line.pos);
  181. tinyrl_line_delete(tinyrl, tinyrl->line.pos, new_pos - tinyrl->line.pos);
  182. } else {
  183. tinyrl_line_delete(tinyrl, tinyrl->line.pos, 1);
  184. }
  185. // Happy compiler
  186. key = key;
  187. return BOOL_TRUE;
  188. }
  189. bool_t tinyrl_key_backword(tinyrl_t *tinyrl, unsigned char key)
  190. {
  191. bool_t result = BOOL_FALSE;
  192. /*
  193. // remove current whitespace before cursor
  194. while (tinyrl->point > 0 && isspace(tinyrl->line[tinyrl->point - 1]))
  195. tinyrl_key_backspace(tinyrl, KEY_BS);
  196. // delete word before cusor
  197. while (tinyrl->point > 0 && !isspace(tinyrl->line[tinyrl->point - 1]))
  198. tinyrl_key_backspace(tinyrl, KEY_BS);
  199. result = BOOL_TRUE;
  200. // keep the compiler happy
  201. key = key;
  202. */
  203. return result;
  204. }
  205. bool_t tinyrl_key_clear_screen(tinyrl_t *tinyrl, unsigned char key)
  206. {
  207. /*
  208. tinyrl_vt100_clear_screen(tinyrl->term);
  209. tinyrl_vt100_cursor_home(tinyrl->term);
  210. tinyrl_reset_line_state(tinyrl);
  211. // keep the compiler happy
  212. key = key;
  213. tinyrl = tinyrl;
  214. */
  215. return BOOL_TRUE;
  216. }
  217. bool_t tinyrl_key_erase_line(tinyrl_t *tinyrl, unsigned char key)
  218. {
  219. /* unsigned int end;
  220. // release any old kill string
  221. lub_string_free(tinyrl->kill_string);
  222. if (!tinyrl->point) {
  223. tinyrl->kill_string = NULL;
  224. return BOOL_TRUE;
  225. }
  226. end = tinyrl->point - 1;
  227. // store the killed string
  228. tinyrl->kill_string = malloc(tinyrl->point + 1);
  229. memcpy(tinyrl->kill_string, tinyrl->buffer, tinyrl->point);
  230. tinyrl->kill_string[tinyrl->point] = '\0';
  231. // delete the text from the start of the line
  232. tinyrl_delete_text(tinyrl, 0, end);
  233. tinyrl->point = 0;
  234. // keep the compiler happy
  235. key = key;
  236. tinyrl = tinyrl;
  237. */
  238. return BOOL_TRUE;
  239. }
  240. bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  241. {
  242. bool_t result = BOOL_FALSE;
  243. /*
  244. tinyrl_match_e status = tinyrl_complete_with_extensions(tinyrl);
  245. switch (status) {
  246. case TINYRL_COMPLETED_MATCH:
  247. case TINYRL_MATCH:
  248. // everything is OK with the world...
  249. result = tinyrl_insert_text(tinyrl, " ");
  250. break;
  251. case TINYRL_NO_MATCH:
  252. case TINYRL_MATCH_WITH_EXTENSIONS:
  253. case TINYRL_AMBIGUOUS:
  254. case TINYRL_COMPLETED_AMBIGUOUS:
  255. // oops don't change the result and let the bell ring
  256. break;
  257. }
  258. // keep the compiler happy
  259. key = key;
  260. */
  261. return result;
  262. }