keybind.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #ifndef KEYBIND_H
  2. #define KEYBIND_H
  3. #include <stdint.h>
  4. #include "app.h"
  5. void keybind_save_buffer(struct app *app, enum keybind_dir dir);
  6. void keybind_mv_cur_char(struct app *app, enum keybind_dir dir);
  7. void keybind_forward_char(struct app *app, enum keybind_dir dir);
  8. void keybind_delete_char(struct app *app, enum keybind_dir dir);
  9. void keybind_mv_cur_word(struct app *app, enum keybind_dir dir);
  10. void keybind_delete_word(struct app *app, enum keybind_dir dir);
  11. void keybind_mv_cur_line(struct app *app, enum keybind_dir dir);
  12. void keybind_mv_cur_edg_line(struct app *app, enum keybind_dir dir);
  13. void keybind_delete_line(struct app *app, enum keybind_dir dir);
  14. void keybind_delete_to_edg_line(struct app *app, enum keybind_dir dir);
  15. void keybind_insert_newline(struct app *app, enum keybind_dir dir);
  16. void keybind_toggle_fps(struct app *app, enum keybind_dir dir);
  17. void keybind_mv_cur_edg_buffer(struct app *app, enum keybind_dir dir);
  18. void keybind_insert_last_pressed_key(struct app *app, enum keybind_dir dir);
  19. void keybind_list_buffers(struct app *app, enum keybind_dir dir);
  20. #if defined(KEYBIND_IMP) || defined(IMP)
  21. void
  22. keybind_save_buffer(struct app *app, enum keybind_dir dir)
  23. {
  24. (void) dir; (void) app;
  25. /* buffer_save(app.buf); */
  26. }
  27. void
  28. keybind_mv_cur_char(struct app *app, enum keybind_dir dir)
  29. {
  30. if ( dir == DIR_FORWARD ) {
  31. buffer_mv_cur_right(app->cbuf);
  32. }
  33. if ( dir == DIR_BACKWARD ) {
  34. buffer_mv_cur_left(app->cbuf);
  35. }
  36. app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
  37. }
  38. void
  39. keybind_delete_char(struct app *app, enum keybind_dir dir)
  40. {
  41. if ( dir == DIR_FORWARD ) {
  42. buffer_remove_char(app->cbuf, app->cbuf->cur);
  43. }
  44. if ( dir == DIR_BACKWARD ) {
  45. if ( app->cbuf->cur == 0 ) {
  46. return;
  47. }
  48. buffer_remove_char(app->cbuf, --app->cbuf->cur);
  49. }
  50. app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
  51. }
  52. void
  53. keybind_mv_cur_word(struct app *app, enum keybind_dir dir)
  54. {
  55. size_t index;
  56. enum buffer_err err;
  57. if ( dir == DIR_FORWARD ) {
  58. index = buffer_index_fw_word(app->cbuf, &err);
  59. if ( err != BUFFER_ERR_OK ) {
  60. if ( err == BUFFER_ERR_INVALID_CUR_POS ) {
  61. app->cbuf->cur = app->cbuf->data.size;
  62. }
  63. return;
  64. }
  65. app->cbuf->cur = index;
  66. }
  67. if ( dir == DIR_BACKWARD ) {
  68. index = buffer_index_bw_word(app->cbuf, &err);
  69. if ( err != BUFFER_ERR_OK ) {
  70. if ( err == BUFFER_ERR_INVALID_CUR_POS ) {
  71. app->cbuf->cur = app->cbuf->data.size;
  72. }
  73. return;
  74. }
  75. app->cbuf->cur = index;
  76. }
  77. app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
  78. }
  79. void
  80. keybind_delete_word(struct app *app, enum keybind_dir dir)
  81. {
  82. size_t index, n_rm;
  83. size_t start, end ;
  84. enum buffer_err err;
  85. switch ( dir ) {
  86. case DIR_BACKWARD: {
  87. index = buffer_index_bw_word(app->cbuf, &err);
  88. if ( err != BUFFER_ERR_OK ) {
  89. if ( err == BUFFER_ERR_INVALID_CUR_POS ) {
  90. app->cbuf->cur = app->cbuf->data.size;
  91. }
  92. return;
  93. }
  94. start = index;
  95. end = app->cbuf->cur;
  96. } break;
  97. case DIR_FORWARD: {
  98. index = buffer_index_fw_word(app->cbuf, &err);
  99. if ( err != BUFFER_ERR_OK ) {
  100. if ( err == BUFFER_ERR_INVALID_CUR_POS ) {
  101. app->cbuf->cur = app->cbuf->data.size;
  102. }
  103. return;
  104. }
  105. start = app->cbuf->cur;
  106. end = index;
  107. } break;
  108. default: {
  109. fprintf(stderr, "Got invalid direction in %s: %d",
  110. __func__, dir);
  111. exit(EXIT_FAILURE);
  112. } break;
  113. }
  114. n_rm = buffer_remove_between(app->cbuf, start, end, &err);
  115. if ( err != BUFFER_ERR_OK ) {
  116. return;
  117. }
  118. app->cbuf->cur -= ( dir == DIR_BACKWARD ) * n_rm;
  119. app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
  120. }
  121. void
  122. keybind_mv_cur_line(struct app *app, enum keybind_dir dir)
  123. {
  124. (void) dir;
  125. switch ( dir ) {
  126. case DIR_FORWARD: {
  127. buffer_mv_cur_down(app->cbuf);
  128. } break;
  129. case DIR_BACKWARD: {
  130. buffer_mv_cur_up(app->cbuf);
  131. } break;
  132. default: {
  133. fprintf(stderr, "Got invalid direction in %s: %d",
  134. __func__, dir);
  135. exit(EXIT_FAILURE);
  136. } break;
  137. }
  138. }
  139. void
  140. keybind_mv_cur_edg_line(struct app *app, enum keybind_dir dir)
  141. {
  142. switch ( dir ) {
  143. case DIR_FORWARD: {
  144. struct buffer_line l = buffer_find_line(
  145. app->cbuf, app->cbuf->cur+1);
  146. app->cbuf->cur = l.end;
  147. } break;
  148. case DIR_BACKWARD: {
  149. struct buffer_line l = buffer_find_line(
  150. app->cbuf, app->cbuf->cur-1);
  151. app->cbuf->cur = l.start;
  152. } break;
  153. default: {
  154. fprintf(stderr, "Got invalid direction in %s: %d",
  155. __func__, dir);
  156. exit(EXIT_FAILURE);
  157. } break;
  158. }
  159. app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
  160. }
  161. void
  162. keybind_delete_line(struct app *app, enum keybind_dir dir)
  163. {
  164. (void) dir;
  165. struct buffer_line l = buffer_find_line(app->cbuf, app->cbuf->cur);
  166. enum buffer_err err;
  167. l.end += (l.end+1 < app->cbuf->data.size);
  168. buffer_remove_between(app->cbuf, l.start, l.end, &err);
  169. if ( err != BUFFER_ERR_OK ) {
  170. return;
  171. }
  172. app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
  173. }
  174. void
  175. keybind_delete_to_edg_line(struct app *app, enum keybind_dir dir)
  176. {
  177. struct buffer_line l = buffer_find_line(app->cbuf, app->cbuf->cur);
  178. size_t start;
  179. size_t end;
  180. switch ( dir ) {
  181. case DIR_FORWARD: {
  182. start = app->cbuf->cur;
  183. end = l.end;
  184. } break;
  185. case DIR_BACKWARD: {
  186. start = l.start;
  187. end = app->cbuf->cur;
  188. app->cbuf->cur = l.start;
  189. } break;
  190. default: {
  191. fprintf(stderr, "Got invalid direction in %s: %d",
  192. __func__, dir);
  193. exit(EXIT_FAILURE);
  194. } break;
  195. }
  196. enum buffer_err err;
  197. buffer_remove_between(app->cbuf, start, end, &err);
  198. if ( err != BUFFER_ERR_OK ) {
  199. return;
  200. }
  201. app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
  202. }
  203. void
  204. keybind_insert_newline(struct app *app, enum keybind_dir dir)
  205. {
  206. (void) dir;
  207. buffer_insert_char(app->cbuf, '\n', app->cbuf->cur);
  208. app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
  209. }
  210. void
  211. keybind_toggle_fps(struct app *app, enum keybind_dir dir)
  212. {
  213. (void) dir;
  214. app->show_fps = ! app->show_fps;
  215. }
  216. void
  217. keybind_mv_cur_edg_buffer(struct app *app, enum keybind_dir dir)
  218. {
  219. switch ( dir ) {
  220. case DIR_FORWARD: {
  221. app->cbuf->cur = app->cbuf->data.size;
  222. } break;
  223. case DIR_BACKWARD: {
  224. app->cbuf->cur = 0;
  225. } break;
  226. default: {
  227. fprintf(stderr, "Got invalid direction in %s: %d",
  228. __func__, dir);
  229. exit(EXIT_FAILURE);
  230. } break;
  231. }
  232. app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
  233. }
  234. void
  235. keybind_insert_last_pressed_key(struct app *app, enum keybind_dir dir)
  236. {
  237. (void)dir;
  238. char c = (char)app->last_pressed_key;
  239. if ( (c < 32 || c > 126) && c != '\t' ) {
  240. return;
  241. }
  242. buffer_insert_char(app->cbuf, c, app->cbuf->cur);
  243. app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
  244. }
  245. void
  246. keybind_list_buffers(struct app *app, enum keybind_dir dir)
  247. {
  248. (void) dir;
  249. app->cbuf = app_create_buffer_list(app);
  250. }
  251. #endif /* defined(KEYBIND_IMP) || defined(IMP) */
  252. #endif