keys.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. // Free old buffered string
  52. faux_str_free(tinyrl->buffer);
  53. tinyrl->buffer = NULL;
  54. // Nothing to kill
  55. if (tinyrl->line.pos == tinyrl->line.len)
  56. return BOOL_TRUE;
  57. // Store killed string
  58. tinyrl->buffer = faux_str_dup(tinyrl->line.str + tinyrl->line.pos);
  59. // Delete text to the end of the line
  60. tinyrl_line_delete(tinyrl, tinyrl->line.pos, tinyrl->line.len);
  61. // Happy compiler
  62. key = key;
  63. return BOOL_TRUE;
  64. }
  65. bool_t tinyrl_key_yank(tinyrl_t *tinyrl, unsigned char key)
  66. {
  67. if (!tinyrl->buffer)
  68. return BOOL_TRUE;
  69. tinyrl_line_insert(tinyrl, tinyrl->buffer, strlen(tinyrl->buffer));
  70. // Happy compiler
  71. key = key;
  72. return BOOL_TRUE;
  73. }
  74. // Default handler for crlf
  75. bool_t tinyrl_key_crlf(tinyrl_t *tinyrl, unsigned char key)
  76. {
  77. tinyrl_multi_crlf(tinyrl);
  78. tinyrl_reset_line_state(tinyrl);
  79. tinyrl_reset_line(tinyrl);
  80. tinyrl_reset_hist_pos(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. const char *str = NULL;
  87. str = hist_pos(tinyrl->hist);
  88. // Up key is pressed for the first time and current line is new typed one
  89. if (!str) {
  90. hist_add(tinyrl->hist, tinyrl->line.str, BOOL_TRUE); // Temp entry
  91. hist_pos_up(tinyrl->hist); // Skip newly added temp entry
  92. }
  93. hist_pos_up(tinyrl->hist);
  94. str = hist_pos(tinyrl->hist);
  95. // Empty history
  96. if (!str)
  97. return BOOL_TRUE;
  98. tinyrl_line_replace(tinyrl, str);
  99. key = key; // Happy compiler
  100. return BOOL_TRUE;
  101. }
  102. bool_t tinyrl_key_down(tinyrl_t *tinyrl, unsigned char key)
  103. {
  104. const char *str = NULL;
  105. hist_pos_down(tinyrl->hist);
  106. str = hist_pos(tinyrl->hist);
  107. // Empty history
  108. if (!str)
  109. return BOOL_TRUE;
  110. tinyrl_line_replace(tinyrl, str);
  111. key = key; // Happy compiler
  112. return BOOL_TRUE;
  113. }
  114. bool_t tinyrl_key_left(tinyrl_t *tinyrl, unsigned char key)
  115. {
  116. if (tinyrl->line.pos == 0)
  117. return BOOL_TRUE;
  118. if (tinyrl->utf8)
  119. tinyrl->line.pos = utf8_move_left(tinyrl->line.str, tinyrl->line.pos);
  120. else
  121. tinyrl->line.pos--;
  122. // Happy compiler
  123. key = key;
  124. return BOOL_TRUE;
  125. }
  126. bool_t tinyrl_key_right(tinyrl_t *tinyrl, unsigned char key)
  127. {
  128. if (tinyrl->line.pos == tinyrl->line.len)
  129. return BOOL_TRUE;
  130. if (tinyrl->utf8)
  131. tinyrl->line.pos = utf8_move_right(tinyrl->line.str, tinyrl->line.pos);
  132. else
  133. tinyrl->line.pos++;
  134. // Happy compiler
  135. key = key;
  136. return BOOL_TRUE;
  137. }
  138. bool_t tinyrl_key_backspace(tinyrl_t *tinyrl, unsigned char key)
  139. {
  140. if (tinyrl->line.pos == 0)
  141. return BOOL_TRUE;
  142. if (tinyrl->utf8) {
  143. off_t new_pos = 0;
  144. new_pos = utf8_move_left(tinyrl->line.str, tinyrl->line.pos);
  145. tinyrl_line_delete(tinyrl, new_pos, tinyrl->line.pos - new_pos);
  146. } else {
  147. tinyrl_line_delete(tinyrl, tinyrl->line.pos - 1, 1);
  148. }
  149. // Happy compiler
  150. key = key;
  151. return BOOL_TRUE;
  152. }
  153. bool_t tinyrl_key_delete(tinyrl_t *tinyrl, unsigned char key)
  154. {
  155. if (tinyrl->line.pos == tinyrl->line.len)
  156. return BOOL_TRUE;
  157. if (tinyrl->utf8) {
  158. off_t new_pos = 0;
  159. new_pos = utf8_move_right(tinyrl->line.str, tinyrl->line.pos);
  160. tinyrl_line_delete(tinyrl, tinyrl->line.pos, new_pos - tinyrl->line.pos);
  161. } else {
  162. tinyrl_line_delete(tinyrl, tinyrl->line.pos, 1);
  163. }
  164. // Happy compiler
  165. key = key;
  166. return BOOL_TRUE;
  167. }
  168. bool_t tinyrl_key_backword(tinyrl_t *tinyrl, unsigned char key)
  169. {
  170. size_t new_pos = tinyrl->line.pos;
  171. // Free old buffered string
  172. faux_str_free(tinyrl->buffer);
  173. tinyrl->buffer = NULL;
  174. if (tinyrl->line.pos == 0)
  175. return BOOL_TRUE;
  176. // Remove spaces before cursor
  177. while (new_pos > 0) {
  178. size_t prev_pos = tinyrl->utf8 ?
  179. utf8_move_left(tinyrl->line.str, new_pos) : (new_pos - 1);
  180. if (!isspace(tinyrl->line.str[prev_pos]))
  181. break;
  182. new_pos = prev_pos;
  183. }
  184. // Delete word before cusor
  185. while (new_pos > 0) {
  186. size_t prev_pos = tinyrl->utf8 ?
  187. utf8_move_left(tinyrl->line.str, new_pos) : (new_pos - 1);
  188. if (isspace(tinyrl->line.str[prev_pos]))
  189. break;
  190. new_pos = prev_pos;
  191. }
  192. if (new_pos == tinyrl->line.pos)
  193. return BOOL_TRUE;
  194. // Store string
  195. tinyrl->buffer = faux_str_dupn(tinyrl->line.str + new_pos,
  196. tinyrl->line.pos - new_pos);
  197. tinyrl_line_delete(tinyrl, new_pos, tinyrl->line.pos - new_pos);
  198. // Happy compiler
  199. key = key;
  200. return BOOL_TRUE;
  201. }
  202. bool_t tinyrl_key_clear_screen(tinyrl_t *tinyrl, unsigned char key)
  203. {
  204. vt100_clear_screen(tinyrl->term);
  205. vt100_cursor_home(tinyrl->term);
  206. tinyrl_reset_line_state(tinyrl);
  207. key = key; // Happy compiler
  208. return BOOL_TRUE;
  209. }
  210. bool_t tinyrl_key_erase_line(tinyrl_t *tinyrl, unsigned char key)
  211. {
  212. // Free old buffered string
  213. faux_str_free(tinyrl->buffer);
  214. tinyrl->buffer = NULL;
  215. // Nothing to erase
  216. if (tinyrl->line.len == 0)
  217. return BOOL_TRUE;
  218. // Store string
  219. tinyrl->buffer = faux_str_dup(tinyrl->line.str);
  220. // Delete text to the end of the line
  221. tinyrl_line_delete(tinyrl, 0, tinyrl->line.len);
  222. // Happy compiler
  223. key = key;
  224. return BOOL_TRUE;
  225. }
  226. // Key tab handler is needed to mask real <tab> output
  227. bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
  228. {
  229. // Happy compiler
  230. tinyrl = tinyrl;
  231. key = key;
  232. return BOOL_TRUE;
  233. }