Skip to content

Commit

Permalink
Remove scroll_margin parameter from window_get_scroll_margin()
Browse files Browse the repository at this point in the history
The passed in value is always taken from `GlobalOptions::scroll_margin`,
so we may as well just access that via `Window::editor::options` from
within the function.
  • Loading branch information
craigbarnes committed Dec 20, 2024
1 parent 48c8afd commit 33d8677
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 19 deletions.
9 changes: 4 additions & 5 deletions src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,7 @@ static bool cmd_bolsf(EditorState *e, const CommandArgs *a)
handle_selection_flags(view, a);

if (!block_iter_bol(&view->cursor)) {
unsigned int margin = e->options.scroll_margin;
long top = view->vy + window_get_scroll_margin(e->window, margin);
long top = view->vy + window_get_scroll_margin(e->window);
if (view->cy > top) {
move_up(view, view->cy - top);
} else {
Expand Down Expand Up @@ -695,7 +694,7 @@ static bool cmd_eolsf(EditorState *e, const CommandArgs *a)

if (!block_iter_eol(&view->cursor)) {
Window *window = e->window;
long margin = window_get_scroll_margin(window, e->options.scroll_margin);
long margin = window_get_scroll_margin(window);
long bottom = view->vy + window->edit_h - 1 - margin;
if (view->cy < bottom) {
move_down(view, bottom - view->cy);
Expand Down Expand Up @@ -1341,7 +1340,7 @@ static bool cmd_pgdown(EditorState *e, const CommandArgs *a)
handle_selection_flags(view, a);

Window *window = e->window;
long margin = window_get_scroll_margin(window, e->options.scroll_margin);
long margin = window_get_scroll_margin(window);
long bottom = view->vy + window->edit_h - 1 - margin;
long count;

Expand All @@ -1361,7 +1360,7 @@ static bool cmd_pgup(EditorState *e, const CommandArgs *a)
handle_selection_flags(view, a);

Window *window = e->window;
long margin = window_get_scroll_margin(window, e->options.scroll_margin);
long margin = window_get_scroll_margin(window);
long top = view->vy + margin;
long count;

Expand Down
6 changes: 3 additions & 3 deletions src/ui-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ static void update_window_full(Window *window, void* UNUSED_ARG(data))
EditorState *e = window->editor;
View *view = window->view;
const GlobalOptions *options = &e->options;
view_update(view, options->scroll_margin);

const StyleMap *styles = &e->styles;
Terminal *term = &e->terminal;
view_update(view);

if (options->tab_bar) {
print_tabbar(term, styles, window);
}
Expand Down Expand Up @@ -146,7 +146,7 @@ void update_buffer_windows (
block_iter_goto_offset(&view->cursor, view->saved_cursor_offset);

// This has already been done for the current view
view_update(view, options->scroll_margin);
view_update(view);
}
update_window(term, view->window, styles, options);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void update_screen(EditorState *e, const ScreenState *s)
if (flags & UPDATE_ALL_WINDOWS) {
update_all_windows(term, root_frame, styles);
} else {
view_update(view, options->scroll_margin);
view_update(view);
if (s->id == buffer->id) {
if (s->vx != view->vx || s->vy != view->vy) {
mark_all_lines_changed(buffer);
Expand Down
8 changes: 4 additions & 4 deletions src/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ static void view_update_vx(View *v)
}
}

static void view_update_vy(View *v, unsigned int scroll_margin)
static void view_update_vy(View *v)
{
Window *window = v->window;
int margin = window_get_scroll_margin(window, scroll_margin);
unsigned int margin = window_get_scroll_margin(window);
long max_y = v->vy + window->edit_h - 1 - margin;

if (v->cy < v->vy + margin) {
Expand All @@ -110,15 +110,15 @@ static void view_update_vy(View *v, unsigned int scroll_margin)
}
}

void view_update(View *v, unsigned int scroll_margin)
void view_update(View *v)
{
view_update_cursor_x(v);
view_update_cursor_y(v);
view_update_vx(v);
if (v->force_center || (v->center_on_scroll && view_is_cursor_visible(v))) {
view_center_to_cursor(v);
} else {
view_update_vy(v, scroll_margin);
view_update_vy(v);
}
v->force_center = false;
v->center_on_scroll = false;
Expand Down
2 changes: 1 addition & 1 deletion src/view.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static inline void view_reset_preferred_x(View *view)

void view_update_cursor_y(View *view) NONNULL_ARGS;
void view_update_cursor_x(View *view) NONNULL_ARGS;
void view_update(View *view, unsigned int scroll_margin) NONNULL_ARGS;
void view_update(View *view) NONNULL_ARGS;
long view_get_preferred_x(View *view) NONNULL_ARGS;
bool view_can_close(const View *view) NONNULL_ARGS;
StringView view_get_word_under_cursor(const View *view) NONNULL_ARGS;
Expand Down
11 changes: 8 additions & 3 deletions src/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,15 @@ void window_set_size(Window *window, int w, int h)
window_calculate_line_numbers(window);
}

int window_get_scroll_margin(const Window *window, unsigned int scroll_margin)
unsigned int window_get_scroll_margin(const Window *window)
{
int max = (window->edit_h - 1) / 2;
BUG_ON(max < 0);
int edit_h = window->edit_h;
if (unlikely(edit_h <= 2)) {
return 0;
}

unsigned int max = (edit_h - 1) / 2;
unsigned int scroll_margin = window->editor->options.scroll_margin;
return MIN(max, scroll_margin);
}

Expand Down
2 changes: 1 addition & 1 deletion src/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ View *window_open_files(Window *window, char **filenames, const char *encoding)
void window_calculate_line_numbers(Window *window) NONNULL_ARGS;
void window_set_coordinates(Window *window, int x, int y) NONNULL_ARGS;
void window_set_size(Window *window, int w, int h) NONNULL_ARGS;
int window_get_scroll_margin(const Window *window, unsigned int scroll_margin) NONNULL_ARGS;
unsigned int window_get_scroll_margin(const Window *window) NONNULL_ARGS;
Window *window_prev(Window *window) NONNULL_ARGS_AND_RETURN;
Window *window_next(Window *window) NONNULL_ARGS_AND_RETURN;
void frame_for_each_window(const Frame *frame, void (*func)(Window*, void*), void *data) NONNULL_ARG(1, 2);
Expand Down
2 changes: 1 addition & 1 deletion test/syntax.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ static void test_hl_line(TestContext *ctx)
BlockIter tmp = block_iter(buffer);
hl_fill_start_states(syn, lss, styles, &tmp, buffer->nl);
block_iter_goto_line(&view->cursor, line_nr - 1);
view_update(view, 0);
view_update(view);
ASSERT_EQ(view->cx, 0);
ASSERT_EQ(view->cy, line_nr - 1);

Expand Down

0 comments on commit 33d8677

Please sign in to comment.