From 5546b40369b7bc20201a8f3a327be0ff84af7f5a Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 16 Apr 2020 19:38:30 +0200 Subject: grid: grid_reflow() now translates cursor coordinates --- grid.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 7 deletions(-) (limited to 'grid.c') diff --git a/grid.c b/grid.c index 056dd791..b8659b55 100644 --- a/grid.c +++ b/grid.c @@ -53,7 +53,7 @@ grid_row_free(struct row *row) free(row); } -int +void grid_reflow(struct grid *grid, int new_rows, int new_cols, int old_screen_rows, int new_screen_rows) { @@ -79,13 +79,26 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, tll(struct sixel) new_sixels = tll_init(); + /* Turn cursor coordinates into grid absolute coordinates */ + struct coord cursor = grid->cursor.point; + struct coord new_cursor = {}; + cursor.row += grid->offset; + cursor.row &= old_rows - 1; + + struct coord saved_cursor = grid->saved_cursor.point; + struct coord new_saved_cursor = {}; + saved_cursor.row += grid->offset; + saved_cursor.row &= old_rows - 1; + /* * Walk the old grid */ for (int r = 0; r < old_rows; r++) { + const size_t old_row_idx = (offset + r) & (old_rows - 1); + /* Unallocated (empty) rows we can simply skip */ - const struct row *old_row = old_grid[(offset + r) & (old_rows - 1)]; + const struct row *old_row = old_grid[old_row_idx]; if (old_row == NULL) continue; @@ -102,7 +115,7 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, * the "real" sixel list. */ tll_foreach(grid->sixel_images, it) { - if (it->item.pos.row == ((offset + r) & (old_rows - 1))) { + if (it->item.pos.row == old_row_idx) { struct sixel six = it->item; six.pos.row = new_row_idx; @@ -122,7 +135,13 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, /* Walk current line of the old grid */ for (int c = 0; c < old_cols; c++) { - if (old_row->cells[c].wc == 0) { + bool has_cursor = cursor.row == old_row_idx && cursor.col == c; + bool has_saved_cursor + = saved_cursor.row == old_row_idx && saved_cursor.col == c; + + if (old_row->cells[c].wc == 0 && !has_cursor && !has_saved_cursor) { + assert(!has_cursor); + assert(!has_saved_cursor); empty_count++; continue; } @@ -168,6 +187,12 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, new_row->cells[new_col_idx] = *old_cell; new_row->cells[new_col_idx].attrs.clean = 1; + + if (has_cursor) + new_cursor = (struct coord){new_col_idx, new_row_idx}; + if (has_saved_cursor) + new_saved_cursor = (struct coord){new_col_idx, new_row_idx}; + new_col_idx++; } @@ -211,11 +236,32 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, grid_row_free(old_grid[r]); free(grid->rows); - grid->cur_row = new_grid[new_row_idx]; + grid->cur_row = new_grid[new_cursor.row]; grid->rows = new_grid; grid->num_rows = new_rows; grid->num_cols = new_cols; + /* Convert absolute coordinates to screen relative */ + new_cursor.row -= grid->offset; + while (new_cursor.row < 0) + new_cursor.row += grid->num_rows; + + assert(new_cursor.row >= 0); + assert(new_cursor.row < grid->num_rows); + + new_saved_cursor.row -= grid->offset; + while (new_saved_cursor.row < 0) + new_saved_cursor.row += grid->num_rows; + + assert(new_saved_cursor.row >= 0); + assert(new_saved_cursor.row < grid->num_rows); + + grid->cursor.point = new_cursor; + grid->saved_cursor.point = new_saved_cursor; + + grid->cursor.lcf = false; + grid->saved_cursor.lcf = false; + /* Destroy any non-moved sixels */ tll_foreach(grid->sixel_images, it) sixel_destroy(&it->item); @@ -225,6 +271,4 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, tll_foreach(new_sixels, it) tll_push_back(grid->sixel_images, it->item); tll_free(new_sixels); - - return new_row_idx; } -- cgit v1.3 From e37aa3b369b67456f490102c455b6c16367f8e02 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 17 Apr 2020 20:33:08 +0200 Subject: grid: reflow: don't randomly insert hard linebreaks Not sure why I added this code; maybe to workaround issues caused by the fact that the cursor position wasn't properly translated. --- grid.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'grid.c') diff --git a/grid.c b/grid.c index b8659b55..9276eb04 100644 --- a/grid.c +++ b/grid.c @@ -157,6 +157,7 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, /* Out of columns on current row in new grid? */ if (new_col_idx >= new_cols) { +#if 0 /* * If last cell on last row and first cell on new * row are non-empty, wrap the line, otherwise @@ -167,6 +168,7 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, { new_row->linebreak = true; } +#endif new_col_idx = 0; new_row_idx = (new_row_idx + 1) & (new_rows - 1); -- cgit v1.3 From aede474a52460fda2773e703b886172f9db74412 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 17 Apr 2020 20:46:08 +0200 Subject: grid: reflow: turn line-wrapping code into a macro --- grid.c | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) (limited to 'grid.c') diff --git a/grid.c b/grid.c index 9276eb04..eb7f0c25 100644 --- a/grid.c +++ b/grid.c @@ -124,6 +124,21 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, } } +#define line_wrap() \ + do { \ + new_col_idx = 0; \ + new_row_idx = (new_row_idx + 1) & (new_rows - 1); \ + \ + new_row = new_grid[new_row_idx]; \ + if (new_row == NULL) { \ + new_row = grid_row_alloc(new_cols, true); \ + new_grid[new_row_idx] = new_row; \ + } else { \ + memset(new_row->cells, 0, new_cols * sizeof(new_row->cells[0])); \ + new_row->linebreak = false; \ + } \ + } while(0) + /* * Keep track of empty cells. If the old line ends with a * string of empty cells, we don't need to, nor do we want to, @@ -169,18 +184,7 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, new_row->linebreak = true; } #endif - - new_col_idx = 0; - new_row_idx = (new_row_idx + 1) & (new_rows - 1); - - new_row = new_grid[new_row_idx]; - if (new_row == NULL) { - new_row = grid_row_alloc(new_cols, true); - new_grid[new_row_idx] = new_row; - } else { - memset(new_row->cells, 0, new_cols * sizeof(new_row->cells[0])); - new_row->linebreak = false; - } + line_wrap(); } assert(new_row != NULL); @@ -203,19 +207,10 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, if (old_row->linebreak) { new_row->linebreak = true; - - new_col_idx = 0; - new_row_idx = (new_row_idx + 1) & (new_rows - 1); - - new_row = new_grid[new_row_idx]; - if (new_row == NULL) { - new_row = grid_row_alloc(new_cols, true); - new_grid[new_row_idx] = new_row; - } else { - memset(new_row->cells, 0, new_cols * sizeof(new_row->cells[0])); - new_row->linebreak = false; - } + line_wrap(); } + +#undef line_wrap } /* Set offset such that the last reflowed row is at the bottom */ -- cgit v1.3 From 91a71b4147102e031aeb9952dfab920928998198 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 17 Apr 2020 20:48:22 +0200 Subject: grid: reflow: remove commented out code --- grid.c | 2 -- 1 file changed, 2 deletions(-) (limited to 'grid.c') diff --git a/grid.c b/grid.c index eb7f0c25..0c6f9659 100644 --- a/grid.c +++ b/grid.c @@ -61,8 +61,6 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, const int old_rows = grid->num_rows; const int old_cols = grid->num_cols; - //assert(old_rows != new_rows || old_cols != new_cols); - int new_col_idx = 0; int new_row_idx = 0; -- cgit v1.3 From e5521ff79af196cc44d61604bc52c69b997be091 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 17 Apr 2020 21:00:37 +0200 Subject: grid: reflow: generalize cursor coordinate translation Define a list of "tracking points" - coordinates that should be translated while reflowing. Add the cursor coordinates to this list. When a coordinate have been translated, it is removed from the list. This means we don't have to create a copy of the 'cursor' coordinate struct. --- grid.c | 67 ++++++++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 41 insertions(+), 26 deletions(-) (limited to 'grid.c') diff --git a/grid.c b/grid.c index 0c6f9659..3e34c891 100644 --- a/grid.c +++ b/grid.c @@ -79,15 +79,17 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, /* Turn cursor coordinates into grid absolute coordinates */ struct coord cursor = grid->cursor.point; - struct coord new_cursor = {}; cursor.row += grid->offset; cursor.row &= old_rows - 1; struct coord saved_cursor = grid->saved_cursor.point; - struct coord new_saved_cursor = {}; saved_cursor.row += grid->offset; saved_cursor.row &= old_rows - 1; + tll(struct coord *) tracking_points = tll_init(); + tll_push_back(tracking_points, &cursor); + tll_push_back(tracking_points, &saved_cursor); + /* * Walk the old grid */ @@ -148,17 +150,23 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, /* Walk current line of the old grid */ for (int c = 0; c < old_cols; c++) { - bool has_cursor = cursor.row == old_row_idx && cursor.col == c; - bool has_saved_cursor - = saved_cursor.row == old_row_idx && saved_cursor.col == c; - if (old_row->cells[c].wc == 0 && !has_cursor && !has_saved_cursor) { - assert(!has_cursor); - assert(!has_saved_cursor); + /* Check if this cell is one of the tracked cells */ + bool is_tracking_point = false; + tll_foreach(tracking_points, it) { + if (it->item->row == old_row_idx && it->item->col == c) { + is_tracking_point = true; + break; + } + } + + if (old_row->cells[c].wc == 0 && !is_tracking_point) { empty_count++; continue; } + /* Allow left-adjusted and right-adjusted text, with empty + * cells in between, to be "pushed together" */ int old_cols_left = old_cols - c; int cols_needed = empty_count + old_cols_left; int new_cols_left = new_cols - new_col_idx; @@ -192,11 +200,16 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, new_row->cells[new_col_idx] = *old_cell; new_row->cells[new_col_idx].attrs.clean = 1; - if (has_cursor) - new_cursor = (struct coord){new_col_idx, new_row_idx}; - if (has_saved_cursor) - new_saved_cursor = (struct coord){new_col_idx, new_row_idx}; - + /* Translate tracking point(s) */ + if (is_tracking_point && i >= empty_count) { + tll_foreach(tracking_points, it) { + if (it->item->row == old_row_idx && it->item->col == c) { + it->item->row = new_row_idx; + it->item->col = new_col_idx; + tll_remove(tracking_points, it); + } + } + } new_col_idx++; } @@ -231,28 +244,28 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, grid_row_free(old_grid[r]); free(grid->rows); - grid->cur_row = new_grid[new_cursor.row]; + grid->cur_row = new_grid[cursor.row]; grid->rows = new_grid; grid->num_rows = new_rows; grid->num_cols = new_cols; /* Convert absolute coordinates to screen relative */ - new_cursor.row -= grid->offset; - while (new_cursor.row < 0) - new_cursor.row += grid->num_rows; + cursor.row -= grid->offset; + while (cursor.row < 0) + cursor.row += grid->num_rows; - assert(new_cursor.row >= 0); - assert(new_cursor.row < grid->num_rows); + assert(cursor.row >= 0); + assert(cursor.row < grid->num_rows); - new_saved_cursor.row -= grid->offset; - while (new_saved_cursor.row < 0) - new_saved_cursor.row += grid->num_rows; + saved_cursor.row -= grid->offset; + while (saved_cursor.row < 0) + saved_cursor.row += grid->num_rows; - assert(new_saved_cursor.row >= 0); - assert(new_saved_cursor.row < grid->num_rows); + assert(saved_cursor.row >= 0); + assert(saved_cursor.row < grid->num_rows); - grid->cursor.point = new_cursor; - grid->saved_cursor.point = new_saved_cursor; + grid->cursor.point = cursor; + grid->saved_cursor.point = saved_cursor; grid->cursor.lcf = false; grid->saved_cursor.lcf = false; @@ -266,4 +279,6 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, tll_foreach(new_sixels, it) tll_push_back(grid->sixel_images, it->item); tll_free(new_sixels); + + tll_free(tracking_points); } -- cgit v1.3 From ef52ed8a109af7512bd9e9ed62d74e36ae2deb41 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 17 Apr 2020 21:04:32 +0200 Subject: grid: reflow: caller may now pass a list of coordinates that should be translated --- grid.c | 7 ++++++- grid.h | 4 +++- render.c | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) (limited to 'grid.c') diff --git a/grid.c b/grid.c index 3e34c891..b50a095d 100644 --- a/grid.c +++ b/grid.c @@ -55,7 +55,9 @@ grid_row_free(struct row *row) void grid_reflow(struct grid *grid, int new_rows, int new_cols, - int old_screen_rows, int new_screen_rows) + int old_screen_rows, int new_screen_rows, + size_t tracking_points_count, + struct coord *_tracking_points[static tracking_points_count]) { struct row *const *old_grid = grid->rows; const int old_rows = grid->num_rows; @@ -90,6 +92,9 @@ grid_reflow(struct grid *grid, int new_rows, int new_cols, tll_push_back(tracking_points, &cursor); tll_push_back(tracking_points, &saved_cursor); + for (size_t i = 0; i < tracking_points_count; i++) + tll_push_back(tracking_points, _tracking_points[i]); + /* * Walk the old grid */ diff --git a/grid.h b/grid.h index d4045189..a2ff4b99 100644 --- a/grid.h +++ b/grid.h @@ -8,7 +8,9 @@ struct row *grid_row_alloc(int cols, bool initialize); void grid_row_free(struct row *row); void grid_reflow( struct grid *grid, int new_rows, int new_cols, - int old_screen_rows, int new_screen_rows); + int old_screen_rows, int new_screen_rows, + size_t tracking_points_count, + struct coord *tracking_points[static tracking_points_count]); static inline int grid_row_absolute(const struct grid *grid, int row_no) diff --git a/render.c b/render.c index ef0f0608..ca0342f2 100644 --- a/render.c +++ b/render.c @@ -1754,8 +1754,8 @@ maybe_resize(struct terminal *term, int width, int height, bool force) } /* Reflow grids */ - grid_reflow(&term->normal, new_normal_grid_rows, new_cols, old_rows, new_rows); - grid_reflow(&term->alt, new_alt_grid_rows, new_cols, old_rows, new_rows); + grid_reflow(&term->normal, new_normal_grid_rows, new_cols, old_rows, new_rows, 0, NULL); + grid_reflow(&term->alt, new_alt_grid_rows, new_cols, old_rows, new_rows, 0, NULL); /* Reset tab stops */ tll_free(term->tab_stops); -- cgit v1.3