keys.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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, char key)
  12. {
  13. if (key > 31) {
  14. // Inject new char to the line
  15. tinyrl_line_insert(tinyrl, &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, 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, 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, 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, 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, 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, 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, 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, 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, char key)
  135. {
  136. bool_t result = BOOL_FALSE;
  137. /*
  138. if (tinyrl->point > 0) {
  139. tinyrl->point--;
  140. utf8_point_left(tinyrl);
  141. result = BOOL_TRUE;
  142. }
  143. // keep the compiler happy
  144. key = key;
  145. */
  146. return result;
  147. }
  148. bool_t tinyrl_key_right(tinyrl_t *tinyrl, char key)
  149. {
  150. bool_t result = BOOL_FALSE;
  151. /*
  152. if (tinyrl->point < tinyrl->end) {
  153. tinyrl->point++;
  154. utf8_point_right(tinyrl);
  155. result = BOOL_TRUE;
  156. }
  157. // keep the compiler happy
  158. key = key;
  159. */
  160. return result;
  161. }
  162. bool_t tinyrl_key_backspace(tinyrl_t *tinyrl, char key)
  163. {
  164. if (tinyrl->line.pos == 0)
  165. return BOOL_TRUE;
  166. if (tinyrl->utf8) {
  167. off_t new_pos = 0;
  168. new_pos = utf8_move_left(tinyrl->line.str, tinyrl->line.pos);
  169. tinyrl_line_delete(tinyrl, new_pos, tinyrl->line.pos - new_pos);
  170. } else {
  171. tinyrl_line_delete(tinyrl, tinyrl->line.pos - 1, 1);
  172. }
  173. // Happy compiler
  174. key = key;
  175. return BOOL_TRUE;
  176. }
  177. bool_t tinyrl_key_delete(tinyrl_t *tinyrl, char key)
  178. {
  179. if (tinyrl->line.pos == tinyrl->line.len)
  180. return BOOL_TRUE;
  181. if (tinyrl->utf8) {
  182. off_t new_pos = 0;
  183. new_pos = utf8_move_right(tinyrl->line.str, tinyrl->line.pos);
  184. tinyrl_line_delete(tinyrl, tinyrl->line.pos, new_pos - tinyrl->line.pos);
  185. } else {
  186. tinyrl_line_delete(tinyrl, tinyrl->line.pos, 1);
  187. }
  188. // Happy compiler
  189. key = key;
  190. return BOOL_TRUE;
  191. }
  192. bool_t tinyrl_key_backword(tinyrl_t *tinyrl, char key)
  193. {
  194. bool_t result = BOOL_FALSE;
  195. /*
  196. // remove current whitespace before cursor
  197. while (tinyrl->point > 0 && isspace(tinyrl->line[tinyrl->point - 1]))
  198. tinyrl_key_backspace(tinyrl, KEY_BS);
  199. // delete word before cusor
  200. while (tinyrl->point > 0 && !isspace(tinyrl->line[tinyrl->point - 1]))
  201. tinyrl_key_backspace(tinyrl, KEY_BS);
  202. result = BOOL_TRUE;
  203. // keep the compiler happy
  204. key = key;
  205. */
  206. return result;
  207. }
  208. bool_t tinyrl_key_clear_screen(tinyrl_t *tinyrl, char key)
  209. {
  210. /*
  211. tinyrl_vt100_clear_screen(tinyrl->term);
  212. tinyrl_vt100_cursor_home(tinyrl->term);
  213. tinyrl_reset_line_state(tinyrl);
  214. // keep the compiler happy
  215. key = key;
  216. tinyrl = tinyrl;
  217. */
  218. return BOOL_TRUE;
  219. }
  220. bool_t tinyrl_key_erase_line(tinyrl_t *tinyrl, char key)
  221. {
  222. /* unsigned int end;
  223. // release any old kill string
  224. lub_string_free(tinyrl->kill_string);
  225. if (!tinyrl->point) {
  226. tinyrl->kill_string = NULL;
  227. return BOOL_TRUE;
  228. }
  229. end = tinyrl->point - 1;
  230. // store the killed string
  231. tinyrl->kill_string = malloc(tinyrl->point + 1);
  232. memcpy(tinyrl->kill_string, tinyrl->buffer, tinyrl->point);
  233. tinyrl->kill_string[tinyrl->point] = '\0';
  234. // delete the text from the start of the line
  235. tinyrl_delete_text(tinyrl, 0, end);
  236. tinyrl->point = 0;
  237. // keep the compiler happy
  238. key = key;
  239. tinyrl = tinyrl;
  240. */
  241. return BOOL_TRUE;
  242. }
  243. bool_t tinyrl_key_tab(tinyrl_t *tinyrl, char key)
  244. {
  245. bool_t result = BOOL_FALSE;
  246. /*
  247. tinyrl_match_e status = tinyrl_complete_with_extensions(tinyrl);
  248. switch (status) {
  249. case TINYRL_COMPLETED_MATCH:
  250. case TINYRL_MATCH:
  251. // everything is OK with the world...
  252. result = tinyrl_insert_text(tinyrl, " ");
  253. break;
  254. case TINYRL_NO_MATCH:
  255. case TINYRL_MATCH_WITH_EXTENSIONS:
  256. case TINYRL_AMBIGUOUS:
  257. case TINYRL_COMPLETED_AMBIGUOUS:
  258. // oops don't change the result and let the bell ring
  259. break;
  260. }
  261. // keep the compiler happy
  262. key = key;
  263. */
  264. return result;
  265. }