From 38c050746dba0b309780862bf90db43d143b2f0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 16 May 2020 16:28:32 +0200 Subject: [PATCH] selection: on_row_in_view() -> on_rows_in_view(), and now takes a range This allows us to check the selection once when scrolling, instead of once for each scrolled in line. --- selection.c | 28 ++++++++++++++++++++++++---- selection.h | 2 +- terminal.c | 22 ++++++++++++---------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/selection.c b/selection.c index af52e4b5..26c17b87 100644 --- a/selection.c +++ b/selection.c @@ -30,17 +30,37 @@ selection_enabled(const struct terminal *term) } bool -selection_on_row_in_view(const struct terminal *term, int row_no) +selection_on_rows_in_view(const struct terminal *term, int row_start, int row_end) { + LOG_DBG("selection: %d-%d, range: %d-%d (view=%d)", + term->selection.start.row, term->selection.end.row, + row_start, row_end, term->grid->view); + if (term->selection.start.row == -1 || term->selection.end.row == -1) return false; const struct coord *start = &term->selection.start; const struct coord *end = &term->selection.end; - assert(start->row <= end->row); - row_no += term->grid->view; - return row_no >= start->row && row_no <= end->row; + if (start->row > end->row) { + const struct coord *tmp = start; + start = end; + end = tmp; + } + + row_start += term->grid->view; + row_end += term->grid->view; + + if (row_start <= start->row && row_end >= end->row) { + /* Row range completely encompases the selection */ + return true; + } + + else if (row_start >= start->row || row_end <= end->row) + return true; + + else + return false; } static void diff --git a/selection.h b/selection.h index 5bc216e1..adb66821 100644 --- a/selection.h +++ b/selection.h @@ -16,7 +16,7 @@ void selection_finalize(struct terminal *term, uint32_t serial); void selection_cancel(struct terminal *term); void selection_extend(struct terminal *term, int col, int row, uint32_t serial); -bool selection_on_row_in_view(const struct terminal *term, int row_no); +bool selection_on_rows_in_view(const struct terminal *term, int start, int end); void selection_mark_word(struct terminal *term, int col, int row, bool spaces_only, uint32_t serial); diff --git a/terminal.c b/terminal.c index 44046f1c..537df250 100644 --- a/terminal.c +++ b/terminal.c @@ -1750,13 +1750,14 @@ term_scroll_partial(struct terminal *term, struct scroll_region region, int rows for (int i = term->rows - 1; i >= region.end; i--) grid_swap_row(term->grid, i - rows, i, false); + const int begin_scrolled_in = max(region.end - rows, region.start); + /* Erase scrolled in lines */ - for (int r = max(region.end - rows, region.start); r < region.end; r++) { + for (int r = begin_scrolled_in; r < region.end; r++) erase_line(term, grid_row_and_alloc(term->grid, r)); - //sixel_delete_at_row(term, r); - if (selection_on_row_in_view(term, r)) - selection_cancel(term); - } + + if (selection_on_rows_in_view(term, begin_scrolled_in, region.end - 1)) + selection_cancel(term); sixel_delete_in_range(term, max(region.end - rows, region.start), region.end - 1); term_damage_scroll(term, DAMAGE_SCROLL, region, rows); @@ -1803,13 +1804,14 @@ term_scroll_reverse_partial(struct terminal *term, for (int i = 0 + rows; i < region.start + rows; i++) grid_swap_row(term->grid, i, i - rows, false); + const int end_scrolled_in = min(region.start + rows, region.end); + /* Erase scrolled in lines */ - for (int r = region.start; r < min(region.start + rows, region.end); r++) { + for (int r = region.start; r < end_scrolled_in; r++) erase_line(term, grid_row_and_alloc(term->grid, r)); - //sixel_delete_at_row(term, r); - if (selection_on_row_in_view(term, r)) - selection_cancel(term); - } + + if (selection_on_rows_in_view(term, region.start, end_scrolled_in - 1)) + selection_cancel(term); sixel_delete_in_range(term, region.start, min(region.start + rows, region.end) - 1); term_damage_scroll(term, DAMAGE_SCROLL_REVERSE, region, rows);