keys.c 6.7 KB

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