From 4ddf0821a9a24f6e6a6ca60ab6d38e095a88591b Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Mon, 14 Dec 2020 17:59:46 -0500 Subject: scroll to cursor --- buffer.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++---- util.c | 8 ++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/buffer.c b/buffer.c index c0213ea..42c5ee3 100644 --- a/buffer.c +++ b/buffer.c @@ -442,18 +442,62 @@ bool buffer_pos_move_down(TextBuffer *buffer, BufferPos *pos) { return true; } +// if the cursor is offscreen, this will scroll to make it onscreen. +static void buffer_scroll_to_cursor(TextBuffer *buffer) { + i64 cursor_line = buffer->cursor_pos.line; + i64 cursor_col = buffer_index_to_column(buffer, (u32)cursor_line, buffer->cursor_pos.index); + i64 display_lines = buffer_display_rows(buffer); + i64 display_cols = buffer_display_cols(buffer); + double scroll_x = buffer->scroll_x, scroll_y = buffer->scroll_y; + i64 scroll_padding = 5; + + // scroll left if cursor is off screen in that direction + double max_scroll_x = (double)(cursor_col - scroll_padding); + scroll_x = util_mind(scroll_x, max_scroll_x); + // scroll right + double min_scroll_x = (double)(cursor_col - display_cols + scroll_padding); + scroll_x = util_maxd(scroll_x, min_scroll_x); + // scroll up + double max_scroll_y = (double)(cursor_line - scroll_padding); + scroll_y = util_mind(scroll_y, max_scroll_y); + // scroll down + double min_scroll_y = (double)(cursor_line - display_lines + scroll_padding); + scroll_y = util_maxd(scroll_y, min_scroll_y); + + buffer->scroll_x = scroll_x; + buffer->scroll_y = scroll_y; + buffer_correct_scroll(buffer); // it's possible that min/max_scroll_x/y go too far +} + bool buffer_cursor_move_left(TextBuffer *buffer) { - return buffer_pos_move_left(buffer, &buffer->cursor_pos); + if (buffer_pos_move_left(buffer, &buffer->cursor_pos)) { + buffer_scroll_to_cursor(buffer); + return true; + } + return false; } bool buffer_cursor_move_right(TextBuffer *buffer) { - return buffer_pos_move_right(buffer, &buffer->cursor_pos); + if (buffer_pos_move_right(buffer, &buffer->cursor_pos)) { + buffer_scroll_to_cursor(buffer); + return true; + } + return false; } bool buffer_cursor_move_up(TextBuffer *buffer) { - return buffer_pos_move_up(buffer, &buffer->cursor_pos); + if (buffer_pos_move_up(buffer, &buffer->cursor_pos)) { + buffer_scroll_to_cursor(buffer); + return true; + } + return false; } bool buffer_cursor_move_down(TextBuffer *buffer) { - return buffer_pos_move_down(buffer, &buffer->cursor_pos); + if (buffer_pos_move_down(buffer, &buffer->cursor_pos)) { + buffer_scroll_to_cursor(buffer); + return true; + } + return false; } + diff --git a/util.c b/util.c index c1446a8..e183461 100644 --- a/util.c +++ b/util.c @@ -25,4 +25,12 @@ static void util_zero_memory(void *mem, size_t size) { memset(mem, 0, size); } +static double util_maxd(double a, double b) { + return a > b ? a : b; +} + +static double util_mind(double a, double b) { + return a < b ? a : b; +} + #endif -- cgit v1.2.3