Skip to content

Commit

Permalink
Make scroll-up/scroll-down move cursor at margins (even if non-zero)
Browse files Browse the repository at this point in the history
Also add a new `-M` flag to both commands, which makes them never
move the cursor (thus becoming no-ops when at the scroll margins).

Previously there was an inconsistency where a non-zero `scroll-margin`
would produce the no-op behavior, whereas with `set scroll-margin 0`
the cursor would always be moved when at the margins. This commit makes
these 2 commands behave the same way, regardless of the `scroll-margin`
value.

The `scroll-pgup` and `scroll-pgdown` commands will be amended in a
similar fashion, in a follow-up commit.

Note: this feature has been left undocumented for now, which implies
it may still receive breaking changes in the future.
  • Loading branch information
craigbarnes committed Dec 20, 2024
1 parent 33d8677 commit 658e65d
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -1917,9 +1917,17 @@ static bool cmd_scroll_down(EditorState *e, const CommandArgs *a)
BUG_ON(a->nr_args);
View *view = e->view;
view->vy++;
if (view->cy < view->vy) {

bool never_move_cursor = has_flag(a, 'M');
if (never_move_cursor) {
return true;
}

unsigned int margin = window_get_scroll_margin(e->window);
if (view->cy < view->vy + margin) {
move_down(view, 1);
}

return true;
}

Expand Down Expand Up @@ -1974,14 +1982,22 @@ static bool cmd_scroll_pgup(EditorState *e, const CommandArgs *a)
static bool cmd_scroll_up(EditorState *e, const CommandArgs *a)
{
BUG_ON(a->nr_args);
Window *window = e->window;
View *view = e->view;
if (view->vy) {
view->vy--;
}
if (view->vy + window->edit_h <= view->cy) {

bool never_move_cursor = has_flag(a, 'M');
if (never_move_cursor) {
return true;
}

const Window *window = e->window;
unsigned int margin = window_get_scroll_margin(window);
if (view->vy + (window->edit_h - margin) <= view->cy) {
move_up(view, 1);
}

return true;
}

Expand Down Expand Up @@ -2583,10 +2599,10 @@ static const Command cmds[] = {
{"replace", "bcegi", NA, 1, 2, cmd_replace},
{"right", "cl", NA, 0, 0, cmd_right},
{"save", "Bbde=fpu", NA, 0, 1, cmd_save},
{"scroll-down", "", NA, 0, 0, cmd_scroll_down},
{"scroll-down", "M", NA, 0, 0, cmd_scroll_down},
{"scroll-pgdown", "h", NA, 0, 0, cmd_scroll_pgdown},
{"scroll-pgup", "h", NA, 0, 0, cmd_scroll_pgup},
{"scroll-up", "", NA, 0, 0, cmd_scroll_up},
{"scroll-up", "M", NA, 0, 0, cmd_scroll_up},
{"search", "Henprw", NA, 0, 1, cmd_search},
{"select", "kl", NA, 0, 0, cmd_select},
{"select-block", "", NA, 0, 0, cmd_select_block},
Expand Down

0 comments on commit 658e65d

Please sign in to comment.