Skip to content

Commit

Permalink
Watch view for changes
Browse files Browse the repository at this point in the history
  • Loading branch information
newsch committed May 6, 2019
1 parent f37d9fd commit 056e0ca
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/fe_modes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
Expand Down

0 comments on commit 056e0ca

Please sign in to comment.