keybind.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #ifndef KEYBIND_H
  2. #define KEYBIND_H
  3. #include <stdint.h>
  4. #include "app.h"
  5. enum direction {
  6. DIR_FORWARD,
  7. DIR_BACKWARD,
  8. };
  9. union keybind_args {
  10. enum direction dir;
  11. void *ptr;
  12. };
  13. typedef struct app (*keybind_func)(struct app, union keybind_args);
  14. struct bind {
  15. const SDL_Keymod mod;
  16. keybind_func func;
  17. union keybind_args args;
  18. };
  19. #define BINDS_SIZE 10
  20. struct keybinds {
  21. uint8_t size;
  22. const struct bind binds[BINDS_SIZE];
  23. };
  24. struct app keybind_mv_cur_char(struct app app, union keybind_args args);
  25. struct app keybind_forward_char(struct app app, union keybind_args args);
  26. struct app keybind_delete_char(struct app app, union keybind_args args);
  27. struct app keybind_mv_cur_word(struct app app, union keybind_args args);
  28. struct app keybind_delete_word(struct app app, union keybind_args args);
  29. struct app keybind_insert_newline(struct app app, union keybind_args args);
  30. #if defined(KEYBIND_IMP) || defined(IMP)
  31. struct app
  32. keybind_mv_cur_char(struct app app, union keybind_args args)
  33. {
  34. if ( args.dir == DIR_FORWARD ) {
  35. app.buf = buffer_mv_cur_right(app.buf);
  36. }
  37. if ( args.dir == DIR_BACKWARD ) {
  38. app.buf = buffer_mv_cur_left(app.buf);
  39. }
  40. return app;
  41. }
  42. struct app
  43. keybind_delete_char(struct app app, union keybind_args args)
  44. {
  45. if ( args.dir == DIR_FORWARD ) {
  46. app.buf = buffer_remove_char(app.buf, app.buf.cur);
  47. }
  48. if ( args.dir == DIR_BACKWARD ) {
  49. app.buf = buffer_remove_char(app.buf, --app.buf.cur);
  50. }
  51. return app;
  52. }
  53. struct app
  54. keybind_mv_cur_word(struct app app, union keybind_args args)
  55. {
  56. size_t index;
  57. enum buffer_err err;
  58. if ( args.dir == DIR_FORWARD ) {
  59. RET_UNWRAP2(index, err,
  60. struct ret_size_t_err,
  61. buffer_index_fw_word(app.buf));
  62. if ( err != BUFFER_ERR_OK ) {
  63. if ( err == BUFFER_ERR_INVALID_CUR_POS ) {
  64. app.buf.cur = app.buf.data.size;
  65. }
  66. return app;
  67. }
  68. app.buf.cur = index;
  69. }
  70. if ( args.dir == DIR_BACKWARD ) {
  71. RET_UNWRAP2(index, err,
  72. struct ret_size_t_err,
  73. buffer_index_bw_word(app.buf));
  74. if ( err != BUFFER_ERR_OK ) {
  75. if ( err == BUFFER_ERR_INVALID_CUR_POS ) {
  76. app.buf.cur = app.buf.data.size;
  77. }
  78. return app;
  79. }
  80. app.buf.cur = index;
  81. }
  82. return app;
  83. }
  84. struct app
  85. keybind_delete_word(struct app app, union keybind_args args)
  86. {
  87. size_t index, n_rm;
  88. enum buffer_err err;
  89. if ( args.dir == DIR_BACKWARD ) {
  90. RET_UNWRAP2(index, err,
  91. struct ret_size_t_err,
  92. buffer_index_bw_word(app.buf));
  93. if ( err != BUFFER_ERR_OK ) {
  94. if ( err == BUFFER_ERR_INVALID_CUR_POS ) {
  95. app.buf.cur = app.buf.data.size;
  96. }
  97. return app;
  98. }
  99. RET_UNWRAP3(app.buf, n_rm, err,
  100. struct ret_buffer_size_t_err,
  101. buffer_remove_between(app.buf, index, app.buf.cur));
  102. if ( err != BUFFER_ERR_OK ) {
  103. return app;
  104. }
  105. app.buf.cur -= n_rm;
  106. }
  107. if ( args.dir == DIR_FORWARD ) {
  108. RET_UNWRAP2(index, err,
  109. struct ret_size_t_err,
  110. buffer_index_fw_word(app.buf));
  111. if ( err != BUFFER_ERR_OK ) {
  112. if ( err == BUFFER_ERR_INVALID_CUR_POS ) {
  113. app.buf.cur = app.buf.data.size;
  114. }
  115. return app;
  116. }
  117. RET_UNWRAP3(app.buf, n_rm, err,
  118. struct ret_buffer_size_t_err,
  119. buffer_remove_between(app.buf, app.buf.cur, index));
  120. if ( err != BUFFER_ERR_OK ) {
  121. return app;
  122. }
  123. }
  124. return app;
  125. }
  126. struct app
  127. keybind_insert_newline(struct app app, union keybind_args args)
  128. {
  129. (void) args;
  130. app.buf = buffer_insert_char(app.buf, app.buf.cur, '\n');
  131. return app;
  132. }
  133. #endif /* defined(KEYBIND_IMP) || defined(IMP) */
  134. #endif