Skip to content

Commit 3760875

Browse files
committed
Fix <0 error with paging
1 parent 8444011 commit 3760875

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/fe_modes.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -160,26 +160,35 @@ int master_handler(State *state, WINDOW *canvas_win, WINDOW *status_win) {
160160
// shift view down/up/left/right
161161
// renaming for ease of use
162162
const int h = getmaxy(canvas_win) - 2; // height of visible canvas
163-
const int w = getmaxx(canvas_win) - 2; // width
163+
const int w = getmaxx(canvas_win) - 2; // width of visible canvas
164164
const int vy = state->view->y;
165165
const int vx = state->view->x;
166-
const int ch = state->view->canvas->num_rows;
167-
const int cw = state->view->canvas->num_cols;
166+
const int ch = state->view->canvas->num_rows; // height of total canvas
167+
const int cw = state->view->canvas->num_cols; // width of total canvas
168168
int new_vy = vy;
169169
int new_vx = vx;
170170
// calc shift based on key
171171
switch (c) {
172172
case KEY_NPAGE: // down
173-
new_vy = min(vy + h, ch - h);
173+
// b/c canvas snaps to the top left corner, we need to be more careful
174+
// when moving down or right. Move to either:
175+
// another window height
176+
// | or the end of the visible canvas
177+
// | | (when the canvas is smaller than the window, this
178+
// v v is less than zero, so get the max of the two)
179+
new_vy = min(vy + h, max(ch - h, 0));
174180
break;
175181
case KEY_PPAGE: // up
176182
new_vy = max(0, vy - h);
177183
break;
178-
case KEY_SRIGHT:
179-
new_vx = min(vx + w, cw - w);
184+
case KEY_SRIGHT: // right
185+
// similar situation to moving down - jump to another window width or
186+
// the end of the canvas, and check for negative values
187+
new_vx = min(vx + w, max(cw - w, 0));
180188
break;
181-
case KEY_SLEFT:
189+
case KEY_SLEFT: // left
182190
new_vx = max(0, vx - w);
191+
break;
183192
}
184193
// shift view
185194
state->view->y = new_vy;

0 commit comments

Comments
 (0)