diff --git a/src/fe_modes.c b/src/fe_modes.c index 4950c2c..4bf478f 100644 --- a/src/fe_modes.c +++ b/src/fe_modes.c @@ -160,26 +160,35 @@ int master_handler(State *state, WINDOW *canvas_win, WINDOW *status_win) { // shift view down/up/left/right // renaming for ease of use const int h = getmaxy(canvas_win) - 2; // height of visible canvas - const int w = getmaxx(canvas_win) - 2; // width + const int w = getmaxx(canvas_win) - 2; // width of visible canvas const int vy = state->view->y; const int vx = state->view->x; - const int ch = state->view->canvas->num_rows; - const int cw = state->view->canvas->num_cols; + const int ch = state->view->canvas->num_rows; // height of total canvas + const int cw = state->view->canvas->num_cols; // width of total canvas int new_vy = vy; int new_vx = vx; // calc shift based on key switch (c) { case KEY_NPAGE: // down - new_vy = min(vy + h, ch - h); + // b/c canvas snaps to the top left corner, we need to be more careful + // when moving down or right. Move to either: + // another window height + // | or the end of the visible canvas + // | | (when the canvas is smaller than the window, this + // v v is less than zero, so get the max of the two) + new_vy = min(vy + h, max(ch - h, 0)); break; case KEY_PPAGE: // up new_vy = max(0, vy - h); break; - case KEY_SRIGHT: - new_vx = min(vx + w, cw - w); + case KEY_SRIGHT: // right + // similar situation to moving down - jump to another window width or + // the end of the canvas, and check for negative values + new_vx = min(vx + w, max(cw - w, 0)); break; - case KEY_SLEFT: + case KEY_SLEFT: // left new_vx = max(0, vx - w); + break; } // shift view state->view->y = new_vy;