From 056e0ca31dc21b86350f9a045ecbef9d64660f87 Mon Sep 17 00:00:00 2001 From: Evan New-Schmidt Date: Mon, 6 May 2019 08:02:52 -0400 Subject: [PATCH] Watch view for changes --- src/fe_modes.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/fe_modes.c b/src/fe_modes.c index 96acae6..7df71b2 100644 --- a/src/fe_modes.c +++ b/src/fe_modes.c @@ -201,16 +201,19 @@ int mode_insert(State *state, WINDOW *canvas_win, WINDOW *status_win) { mode_cfg->last_dir_change = cursor_copy(state->cursor); } + // watch for view shifts + const int vx = state->view->x; + const int vy = state->view->y; // 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 @@ -230,20 +233,30 @@ int mode_insert(State *state, WINDOW *canvas_win, WINDOW *status_win) { state->last_arrow_direction == KEY_LEFT) { // return down if left/right state->cursor->x = mode_cfg->last_dir_change->x; - state->cursor->y++; + cursor_move_down(state->cursor, state->view); 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++; + cursor_move_right(state->cursor, state->view); mode_cfg->last_dir_change->x = state->cursor->x; } + // TODO: shift view if necessary } else { // Print non-print characters to bottom left in status_win bar mvwaddch(status_win, 1, COLS - 3, state->ch_in); } } + // update last_dir_change with view diffs + const int dx = vx - state->view->x; + const int dy = vy - state->view->y; + if (dx != 0) { + mode_cfg->last_dir_change->x += dx; + } + if (dy != 0) { + mode_cfg->last_dir_change->y += dy; + } // Move UI cursor to the right place wmove(canvas_win, cursor_y_to_canvas(state->cursor), cursor_x_to_canvas(state->cursor));