From 88f7f5f8675a15d567837bd076e39e09bfe1aa74 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Tue, 23 Feb 2021 14:30:49 -0500 Subject: goto line preview --- buffer.c | 10 +++++++++- main.c | 2 -- math.c | 10 ++++++++++ menu.c | 14 +++++++++----- ted.h | 3 +++ 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/buffer.c b/buffer.c index 57325b8..0c226ea 100644 --- a/buffer.c +++ b/buffer.c @@ -864,7 +864,15 @@ void buffer_scroll_to_pos(TextBuffer *buffer, BufferPos pos) { } // scroll in such a way that this position is in the center of the screen -void buffer_scroll_center_pos(TextBuffer *buffer) { +void buffer_scroll_center_pos(TextBuffer *buffer, BufferPos pos) { + double line = pos.line; + double col = buffer_index_to_column(buffer, pos.line, pos.index); + + double display_lines = buffer_display_lines(buffer); + double display_cols = buffer_display_cols(buffer); + buffer->scroll_x = col - display_cols * 0.5; + buffer->scroll_y = line - display_lines * 0.5; + buffer_correct_scroll(buffer); } // if the cursor is offscreen, this will scroll to make it onscreen. diff --git a/main.c b/main.c index 72e3ff1..3063ffe 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,4 @@ // @TODO: -// - goto line - // - :run -- if .html file, open in browser, otherwise figure out a way of sending a command to shell // running in terminal emulator?? (don't want to implement stdin/ANSI codes/etc.) diff --git a/math.c b/math.c index b76c049..7d16a52 100644 --- a/math.c +++ b/math.c @@ -456,6 +456,16 @@ static void v4_print(v4 v) { printf("(%f, %f, %f, %f)\n", v.x, v.y, v.z, v.w); } +typedef struct { + double x, y; +} v2d; + +static v2d V2D(double x, double y) { + v2d v; + v.x = x; + v.y = y; + return v; +} // matrices are column-major, because that's what they are in OpenGL typedef struct { diff --git a/menu.c b/menu.c index fb9eb56..497fbba 100644 --- a/menu.c +++ b/menu.c @@ -1,7 +1,9 @@ static void menu_open(Ted *ted, Menu menu) { if (ted->find) find_close(ted); ted->menu = menu; - ted->prev_active_buffer = ted->active_buffer; + TextBuffer *prev_buf = ted->prev_active_buffer = ted->active_buffer; + ted->prev_active_buffer_scroll = V2D(prev_buf->scroll_x, prev_buf->scroll_y); + ted->active_buffer = NULL; *ted->warn_overwrite = 0; // clear warn_overwrite buffer_clear(&ted->line_buffer); @@ -31,8 +33,10 @@ static void menu_open(Ted *ted, Menu menu) { } static void menu_close(Ted *ted) { - ted->active_buffer = ted->prev_active_buffer; + TextBuffer *buffer = ted->active_buffer = ted->prev_active_buffer; ted->prev_active_buffer = NULL; + buffer->scroll_x = ted->prev_active_buffer_scroll.x; + buffer->scroll_y = ted->prev_active_buffer_scroll.y; switch (ted->menu) { case MENU_NONE: assert(0); break; case MENU_OPEN: @@ -207,16 +211,16 @@ static void menu_update(Ted *ted) { long line_number = strtol(contents, &end, 0); TextBuffer *buffer = ted->prev_active_buffer; if (line_number > 0 && *end == '\0' && line_number <= (long)buffer->nlines) { + BufferPos pos = {line_number - 1, 0}; + if (line_buffer->line_buffer_submitted) { // let's go there! - BufferPos pos = {line_number - 1, 0}; - menu_close(ted); buffer_cursor_move_to_pos(buffer, pos); buffer_center_cursor(buffer); } else { // scroll to the line - + buffer_scroll_center_pos(buffer, pos); } } line_buffer->line_buffer_submitted = false; diff --git a/ted.h b/ted.h index 2ca308f..d629dd5 100644 --- a/ted.h +++ b/ted.h @@ -272,6 +272,9 @@ typedef struct Ted { BuildError *build_errors; // dynamic array of build errors u32 build_error; // build error we are currently "on" + + // used by menus to keep track of the scroll position so we can return to it. + v2d prev_active_buffer_scroll; char **tag_selector_entries; // an array of all tags (see tag_selector_open) -- cgit v1.2.3