From d9573f13ffda6164a37d070963f6690412f454a4 Mon Sep 17 00:00:00 2001 From: Evan New-Schmidt Date: Mon, 6 May 2019 03:20:01 -0400 Subject: [PATCH] Initial line-return functionality --- src/fe_modes.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/fe_modes.c b/src/fe_modes.c index 07a9d4d..49d2ea6 100644 --- a/src/fe_modes.c +++ b/src/fe_modes.c @@ -49,6 +49,12 @@ mode_brush_config_t mode_brush_config = { .state = PAINT_OFF, }; +typedef struct { + Cursor *last_dir_change; +} mode_insert_config_t; + +mode_insert_config_t mode_insert_config = {NULL}; + /////////////////////// // GENERAL FUNCTIONS // /////////////////////// @@ -267,11 +273,22 @@ int mode_insert(reason_t reason, State *state) { return 0; } + mode_insert_config_t *mode_cfg = &mode_insert_config; + // init config cursor + if (mode_cfg->last_dir_change == NULL) { + mode_cfg->last_dir_change = cursor_copy(state->cursor); + } + // insert mode behavior if ((state->ch_in == KEY_LEFT) || (state->ch_in == KEY_RIGHT) || (state->ch_in == KEY_UP) || (state->ch_in == KEY_DOWN)) { cursor_key_to_move(state->ch_in, state->cursor, state->view); state->last_arrow_direction = state->ch_in; + + // update direction change cursor + Cursor *old = mode_cfg->last_dir_change; + mode_cfg->last_dir_change = cursor_copy(state->cursor); + cursor_free(old); } else { if (' ' <= state->ch_in && state->ch_in <= '~') { // check if ch is printable @@ -284,6 +301,22 @@ int mode_insert(reason_t reason, State *state) { front_setcharcursor(' '); } else if (state->ch_in == KEY_DC) { front_setcharcursor(' '); + } else if (state->ch_in == KEY_ENTER) { + // return to beginning of "line" on ENTER + // TODO: check if out of view and recenter + if (state->last_arrow_direction == KEY_RIGHT || + state->last_arrow_direction == KEY_LEFT) { + // return down if left/right + state->cursor->x = mode_cfg->last_dir_change->x; + state->cursor->y++; + mode_cfg->last_dir_change->y = state->cursor->y; + } else if (state->last_arrow_direction == KEY_UP || + state->last_arrow_direction == KEY_DOWN) { + // return right if up/down + state->cursor->y = mode_cfg->last_dir_change->y; + state->cursor->x++; + mode_cfg->last_dir_change->x = state->cursor->x; + } } } return 0;