| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- #ifndef KEYBIND_H
- #define KEYBIND_H
- #include <stdint.h>
- #include "app.h"
- void keybind_save_buffer(struct app *app, enum keybind_dir dir);
- void keybind_mv_cur_char(struct app *app, enum keybind_dir dir);
- void keybind_forward_char(struct app *app, enum keybind_dir dir);
- void keybind_delete_char(struct app *app, enum keybind_dir dir);
- void keybind_mv_cur_word(struct app *app, enum keybind_dir dir);
- void keybind_delete_word(struct app *app, enum keybind_dir dir);
- void keybind_mv_cur_line(struct app *app, enum keybind_dir dir);
- void keybind_mv_cur_edg_line(struct app *app, enum keybind_dir dir);
- void keybind_delete_line(struct app *app, enum keybind_dir dir);
- void keybind_delete_to_edg_line(struct app *app, enum keybind_dir dir);
- void keybind_insert_newline(struct app *app, enum keybind_dir dir);
- void keybind_toggle_fps(struct app *app, enum keybind_dir dir);
- void keybind_mv_cur_edg_buffer(struct app *app, enum keybind_dir dir);
- void keybind_insert_last_pressed_key(struct app *app, enum keybind_dir dir);
- void keybind_list_buffers(struct app *app, enum keybind_dir dir);
- #if defined(KEYBIND_IMP) || defined(IMP)
- void
- keybind_save_buffer(struct app *app, enum keybind_dir dir)
- {
- (void) dir; (void) app;
- /* buffer_save(app.buf); */
- }
- void
- keybind_mv_cur_char(struct app *app, enum keybind_dir dir)
- {
- if ( dir == DIR_FORWARD ) {
- buffer_mv_cur_right(app->cbuf);
- }
- if ( dir == DIR_BACKWARD ) {
- buffer_mv_cur_left(app->cbuf);
- }
- app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
- }
- void
- keybind_delete_char(struct app *app, enum keybind_dir dir)
- {
- if ( dir == DIR_FORWARD ) {
- buffer_remove_char(app->cbuf, app->cbuf->cur);
- }
- if ( dir == DIR_BACKWARD ) {
- if ( app->cbuf->cur == 0 ) {
- return;
- }
- buffer_remove_char(app->cbuf, --app->cbuf->cur);
- }
- app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
- }
- void
- keybind_mv_cur_word(struct app *app, enum keybind_dir dir)
- {
- size_t index;
- enum buffer_err err;
- if ( dir == DIR_FORWARD ) {
- index = buffer_index_fw_word(app->cbuf, &err);
- if ( err != BUFFER_ERR_OK ) {
- if ( err == BUFFER_ERR_INVALID_CUR_POS ) {
- app->cbuf->cur = app->cbuf->data.size;
- }
- return;
- }
- app->cbuf->cur = index;
- }
- if ( dir == DIR_BACKWARD ) {
- index = buffer_index_bw_word(app->cbuf, &err);
- if ( err != BUFFER_ERR_OK ) {
- if ( err == BUFFER_ERR_INVALID_CUR_POS ) {
- app->cbuf->cur = app->cbuf->data.size;
- }
- return;
- }
- app->cbuf->cur = index;
- }
- app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
- }
- void
- keybind_delete_word(struct app *app, enum keybind_dir dir)
- {
- size_t index, n_rm;
- size_t start, end ;
- enum buffer_err err;
- switch ( dir ) {
- case DIR_BACKWARD: {
- index = buffer_index_bw_word(app->cbuf, &err);
- if ( err != BUFFER_ERR_OK ) {
- if ( err == BUFFER_ERR_INVALID_CUR_POS ) {
- app->cbuf->cur = app->cbuf->data.size;
- }
- return;
- }
- start = index;
- end = app->cbuf->cur;
- } break;
- case DIR_FORWARD: {
- index = buffer_index_fw_word(app->cbuf, &err);
- if ( err != BUFFER_ERR_OK ) {
- if ( err == BUFFER_ERR_INVALID_CUR_POS ) {
- app->cbuf->cur = app->cbuf->data.size;
- }
- return;
- }
- start = app->cbuf->cur;
- end = index;
- } break;
- default: {
- fprintf(stderr, "Got invalid direction in %s: %d",
- __func__, dir);
- exit(EXIT_FAILURE);
- } break;
- }
- n_rm = buffer_remove_between(app->cbuf, start, end, &err);
- if ( err != BUFFER_ERR_OK ) {
- return;
- }
- app->cbuf->cur -= ( dir == DIR_BACKWARD ) * n_rm;
- app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
- }
- void
- keybind_mv_cur_line(struct app *app, enum keybind_dir dir)
- {
- (void) dir;
- switch ( dir ) {
- case DIR_FORWARD: {
- buffer_mv_cur_down(app->cbuf);
- } break;
- case DIR_BACKWARD: {
- buffer_mv_cur_up(app->cbuf);
- } break;
- default: {
- fprintf(stderr, "Got invalid direction in %s: %d",
- __func__, dir);
- exit(EXIT_FAILURE);
- } break;
- }
- }
- void
- keybind_mv_cur_edg_line(struct app *app, enum keybind_dir dir)
- {
- switch ( dir ) {
- case DIR_FORWARD: {
- struct buffer_line l = buffer_find_line(
- app->cbuf, app->cbuf->cur+1);
- app->cbuf->cur = l.end;
- } break;
- case DIR_BACKWARD: {
- struct buffer_line l = buffer_find_line(
- app->cbuf, app->cbuf->cur-1);
- app->cbuf->cur = l.start;
- } break;
- default: {
- fprintf(stderr, "Got invalid direction in %s: %d",
- __func__, dir);
- exit(EXIT_FAILURE);
- } break;
- }
- app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
- }
- void
- keybind_delete_line(struct app *app, enum keybind_dir dir)
- {
- (void) dir;
- struct buffer_line l = buffer_find_line(app->cbuf, app->cbuf->cur);
- enum buffer_err err;
- l.end += (l.end+1 < app->cbuf->data.size);
- buffer_remove_between(app->cbuf, l.start, l.end, &err);
- if ( err != BUFFER_ERR_OK ) {
- return;
- }
- app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
- }
- void
- keybind_delete_to_edg_line(struct app *app, enum keybind_dir dir)
- {
- struct buffer_line l = buffer_find_line(app->cbuf, app->cbuf->cur);
- size_t start;
- size_t end;
- switch ( dir ) {
- case DIR_FORWARD: {
- start = app->cbuf->cur;
- end = l.end;
- } break;
- case DIR_BACKWARD: {
- start = l.start;
- end = app->cbuf->cur;
- app->cbuf->cur = l.start;
- } break;
- default: {
- fprintf(stderr, "Got invalid direction in %s: %d",
- __func__, dir);
- exit(EXIT_FAILURE);
- } break;
- }
- enum buffer_err err;
- buffer_remove_between(app->cbuf, start, end, &err);
- if ( err != BUFFER_ERR_OK ) {
- return;
- }
- app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
- }
- void
- keybind_insert_newline(struct app *app, enum keybind_dir dir)
- {
- (void) dir;
- buffer_insert_char(app->cbuf, '\n', app->cbuf->cur);
- app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
- }
- void
- keybind_toggle_fps(struct app *app, enum keybind_dir dir)
- {
- (void) dir;
- app->show_fps = ! app->show_fps;
- }
- void
- keybind_mv_cur_edg_buffer(struct app *app, enum keybind_dir dir)
- {
- switch ( dir ) {
- case DIR_FORWARD: {
- app->cbuf->cur = app->cbuf->data.size;
- } break;
- case DIR_BACKWARD: {
- app->cbuf->cur = 0;
- } break;
- default: {
- fprintf(stderr, "Got invalid direction in %s: %d",
- __func__, dir);
- exit(EXIT_FAILURE);
- } break;
- }
- app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
- }
- void
- keybind_insert_last_pressed_key(struct app *app, enum keybind_dir dir)
- {
- (void)dir;
- char c = (char)app->last_pressed_key;
- if ( (c < 32 || c > 126) && c != '\t' ) {
- return;
- }
- buffer_insert_char(app->cbuf, c, app->cbuf->cur);
- app->cbuf->high_col = buffer_calc_cur_col(app->cbuf);
- }
- void
- keybind_list_buffers(struct app *app, enum keybind_dir dir)
- {
- (void) dir;
- app->cbuf = app_create_buffer_list(app);
- }
- #endif /* defined(KEYBIND_IMP) || defined(IMP) */
- #endif
|