keys.c 6.7 KB

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