From 5b1f1602bccfb7c37e50a8509f44b24e9aa3b5d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sat, 9 Apr 2022 15:09:02 +0200 Subject: [PATCH] =?UTF-8?q?refactor:=20add=20a=20=E2=80=98range=E2=80=99?= =?UTF-8?q?=20struct,=20grouping=20a=20start=20and=20end=20coord=20togethe?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- input.c | 5 ++- render.c | 8 ++-- search.c | 4 +- selection.c | 104 ++++++++++++++++++++++++++-------------------------- terminal.c | 40 +++++++++++--------- terminal.h | 16 ++++---- url-mode.c | 24 +++++++----- 7 files changed, 105 insertions(+), 96 deletions(-) diff --git a/input.c b/input.c index 6dbf7fe3..1bf1e14b 100644 --- a/input.c +++ b/input.c @@ -2121,8 +2121,9 @@ wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, auto_scroll_direction, selection_col); } - if (term->selection.ongoing && (cursor_is_on_new_cell || - term->selection.end.row < 0)) + if (term->selection.ongoing && ( + cursor_is_on_new_cell || + term->selection.coords.end.row < 0)) { selection_update(term, selection_col, selection_row); } diff --git a/render.c b/render.c index e9edc012..e408a9dd 100644 --- a/render.c +++ b/render.c @@ -3231,7 +3231,7 @@ render_urls(struct terminal *term) continue; bool hide = false; - const struct coord *pos = &url->start; + const struct coord *pos = &url->range.start; const int _row = (pos->row - scrollback_end @@ -3701,14 +3701,14 @@ maybe_resize(struct terminal *term, int width, int height, bool force) * tracking points list. */ struct coord *const tracking_points[] = { - &term->selection.start, - &term->selection.end, + &term->selection.coords.start, + &term->selection.coords.end, }; /* Resize grids */ grid_resize_and_reflow( &term->normal, new_normal_grid_rows, new_cols, old_rows, new_rows, - term->selection.end.row >= 0 ? ALEN(tracking_points) : 0, + term->selection.coords.end.row >= 0 ? ALEN(tracking_points) : 0, tracking_points); grid_resize_without_reflow( diff --git a/search.c b/search.c index b422d957..f24d49cc 100644 --- a/search.c +++ b/search.c @@ -450,11 +450,11 @@ search_match_to_end_of_word(struct terminal *term, bool spaces_only) if (term->search.match_len == 0) return; - xassert(term->selection.end.row != -1); + xassert(term->selection.coords.end.row != -1); const bool move_cursor = term->search.cursor == term->search.len; - const struct coord old_end = term->selection.end; + const struct coord old_end = term->selection.coords.end; struct coord new_end = old_end; struct row *row = NULL; diff --git a/selection.c b/selection.c index fbd0f484..a4bc5f3b 100644 --- a/selection.c +++ b/selection.c @@ -45,16 +45,16 @@ selection_on_rows(const struct terminal *term, int row_start, int row_end) term->selection.start.row, term->selection.end.row, row_start, row_end, term->grid->offset); - if (term->selection.end.row < 0) + if (term->selection.coords.end.row < 0) return false; - xassert(term->selection.start.row != -1); + xassert(term->selection.coords.start.row != -1); row_start += term->grid->offset; row_end += term->grid->offset; - const struct coord *start = &term->selection.start; - const struct coord *end = &term->selection.end; + const struct coord *start = &term->selection.coords.start; + const struct coord *end = &term->selection.coords.end; if ((row_start <= start->row && row_end >= start->row) || (row_start <= end->row && row_end >= end->row)) @@ -79,29 +79,29 @@ selection_on_rows(const struct terminal *term, int row_start, int row_end) void selection_view_up(struct terminal *term, int new_view) { - if (likely(term->selection.start.row < 0)) + if (likely(term->selection.coords.start.row < 0)) return; if (likely(new_view < term->grid->view)) return; - term->selection.start.row += term->grid->num_rows; - if (term->selection.end.row >= 0) - term->selection.end.row += term->grid->num_rows; + term->selection.coords.start.row += term->grid->num_rows; + if (term->selection.coords.end.row >= 0) + term->selection.coords.end.row += term->grid->num_rows; } void selection_view_down(struct terminal *term, int new_view) { - if (likely(term->selection.start.row < 0)) + if (likely(term->selection.coords.start.row < 0)) return; if (likely(new_view > term->grid->view)) return; - term->selection.start.row &= term->grid->num_rows - 1; - if (term->selection.end.row >= 0) - term->selection.end.row &= term->grid->num_rows - 1; + term->selection.coords.start.row &= term->grid->num_rows - 1; + if (term->selection.coords.end.row >= 0) + term->selection.coords.end.row &= term->grid->num_rows - 1; } static void @@ -251,7 +251,7 @@ extract_one_const_wrapper(struct terminal *term, char * selection_to_text(const struct terminal *term) { - if (term->selection.end.row == -1) + if (term->selection.coords.end.row == -1) return NULL; struct extraction_context *ctx = extract_begin(term->selection.kind, true); @@ -259,7 +259,7 @@ selection_to_text(const struct terminal *term) return NULL; foreach_selected( - (struct terminal *)term, term->selection.start, term->selection.end, + (struct terminal *)term, term->selection.coords.start, term->selection.coords.end, &extract_one_const_wrapper, ctx); char *text; @@ -479,11 +479,11 @@ selection_start(struct terminal *term, int col, int row, switch (kind) { case SELECTION_CHAR_WISE: case SELECTION_BLOCK: - term->selection.start = (struct coord){col, term->grid->view + row}; - term->selection.end = (struct coord){-1, -1}; + term->selection.coords.start = (struct coord){col, term->grid->view + row}; + term->selection.coords.end = (struct coord){-1, -1}; - term->selection.pivot.start = term->selection.start; - term->selection.pivot.end = term->selection.end; + term->selection.pivot.start = term->selection.coords.start; + term->selection.pivot.end = term->selection.coords.end; break; case SELECTION_WORD_WISE: { @@ -491,10 +491,10 @@ selection_start(struct terminal *term, int col, int row, selection_find_word_boundary_left(term, &start, spaces_only); selection_find_word_boundary_right(term, &end, spaces_only); - term->selection.start = (struct coord){ + term->selection.coords.start = (struct coord){ start.col, term->grid->view + start.row}; - term->selection.pivot.start = term->selection.start; + term->selection.pivot.start = term->selection.coords.start; term->selection.pivot.end = (struct coord){end.col, term->grid->view + end.row}; selection_update(term, end.col, end.row); @@ -506,9 +506,9 @@ selection_start(struct terminal *term, int col, int row, selection_find_line_boundary_left(term, &start, spaces_only); selection_find_line_boundary_right(term, &end, spaces_only); - term->selection.start = (struct coord){ + term->selection.coords.start = (struct coord){ start.col, term->grid->view + start.row}; - term->selection.pivot.start = term->selection.start; + term->selection.pivot.start = term->selection.coords.start; term->selection.pivot.end = (struct coord){end.col, term->grid->view + end.row}; selection_update(term, end.col, end.row); @@ -632,7 +632,7 @@ reset_modify_context(struct mark_context *ctx) static void selection_modify(struct terminal *term, struct coord start, struct coord end) { - xassert(term->selection.start.row != -1); + xassert(term->selection.coords.start.row != -1); xassert(start.row != -1 && start.col != -1); xassert(end.row != -1 && end.col != -1); @@ -645,16 +645,16 @@ selection_modify(struct terminal *term, struct coord start, struct coord end) foreach_selected(term, start, end, &premark_selected, &ctx); reset_modify_context(&ctx); - if (term->selection.end.row >= 0) { + if (term->selection.coords.end.row >= 0) { /* Unmark previous selection, ignoring cells that are part of * the new selection */ - foreach_selected(term, term->selection.start, term->selection.end, + foreach_selected(term, term->selection.coords.start, term->selection.coords.end, &unmark_selected, &ctx); reset_modify_context(&ctx); } - term->selection.start = start; - term->selection.end = end; + term->selection.coords.start = start; + term->selection.coords.end = end; /* Mark new selection */ foreach_selected(term, start, end, &mark_selected, &ctx); @@ -744,20 +744,20 @@ set_pivot_point_for_block_and_char_wise(struct terminal *term, void selection_update(struct terminal *term, int col, int row) { - if (term->selection.start.row < 0) + if (term->selection.coords.start.row < 0) return; if (!term->selection.ongoing) return; LOG_DBG("selection updated: start = %d,%d, end = %d,%d -> %d, %d", - term->selection.start.row, term->selection.start.col, - term->selection.end.row, term->selection.end.col, + term->selection.coords.start.row, term->selection.coords.start.col, + term->selection.coords.end.row, term->selection.coords.end.col, row, col); xassert(term->grid->view + row != -1); - struct coord new_start = term->selection.start; + struct coord new_start = term->selection.coords.start; struct coord new_end = {col, term->grid->view + row}; /* Adjust start point if the selection has changed 'direction' */ @@ -900,11 +900,11 @@ selection_update(struct terminal *term, int col, int row) void selection_dirty_cells(struct terminal *term) { - if (term->selection.start.row < 0 || term->selection.end.row < 0) + if (term->selection.coords.start.row < 0 || term->selection.coords.end.row < 0) return; foreach_selected( - term, term->selection.start, term->selection.end, &mark_selected, + term, term->selection.coords.start, term->selection.coords.end, &mark_selected, &(struct mark_context){0}); } @@ -912,8 +912,8 @@ static void selection_extend_normal(struct terminal *term, int col, int row, enum selection_kind new_kind) { - const struct coord *start = &term->selection.start; - const struct coord *end = &term->selection.end; + const struct coord *start = &term->selection.coords.start; + const struct coord *end = &term->selection.coords.end; if (start->row > end->row || (start->row == end->row && start->col > end->col)) @@ -1020,8 +1020,8 @@ selection_extend_normal(struct terminal *term, int col, int row, static void selection_extend_block(struct terminal *term, int col, int row) { - const struct coord *start = &term->selection.start; - const struct coord *end = &term->selection.end; + const struct coord *start = &term->selection.coords.start; + const struct coord *end = &term->selection.coords.end; struct coord top_left = { .row = min(start->row, end->row), @@ -1089,7 +1089,7 @@ void selection_extend(struct seat *seat, struct terminal *term, int col, int row, enum selection_kind new_kind) { - if (term->selection.start.row < 0 || term->selection.end.row < 0) { + if (term->selection.coords.start.row < 0 || term->selection.coords.end.row < 0) { /* No existing selection */ return; } @@ -1101,8 +1101,8 @@ selection_extend(struct seat *seat, struct terminal *term, row += term->grid->view; - if ((row == term->selection.start.row && col == term->selection.start.col) || - (row == term->selection.end.row && col == term->selection.end.col)) + if ((row == term->selection.coords.start.row && col == term->selection.coords.start.col) || + (row == term->selection.coords.end.row && col == term->selection.coords.end.col)) { /* Extension point *is* one of the current end points */ return; @@ -1138,14 +1138,14 @@ selection_finalize(struct seat *seat, struct terminal *term, uint32_t serial) selection_stop_scroll_timer(term); term->selection.ongoing = false; - if (term->selection.start.row < 0 || term->selection.end.row < 0) + if (term->selection.coords.start.row < 0 || term->selection.coords.end.row < 0) return; - xassert(term->selection.start.row != -1); - xassert(term->selection.end.row != -1); + xassert(term->selection.coords.start.row != -1); + xassert(term->selection.coords.end.row != -1); - term->selection.start.row &= (term->grid->num_rows - 1); - term->selection.end.row &= (term->grid->num_rows - 1); + term->selection.coords.start.row &= (term->grid->num_rows - 1); + term->selection.coords.end.row &= (term->grid->num_rows - 1); switch (term->conf->selection_target) { case SELECTION_TARGET_NONE: @@ -1169,21 +1169,21 @@ void selection_cancel(struct terminal *term) { LOG_DBG("selection cancelled: start = %d,%d end = %d,%d", - term->selection.start.row, term->selection.start.col, - term->selection.end.row, term->selection.end.col); + term->selection.coords.start.row, term->selection.coords.start.col, + term->selection.coords.end.row, term->selection.coords.end.col); selection_stop_scroll_timer(term); - if (term->selection.start.row >= 0 && term->selection.end.row >= 0) { + if (term->selection.coords.start.row >= 0 && term->selection.coords.end.row >= 0) { foreach_selected( - term, term->selection.start, term->selection.end, + term, term->selection.coords.start, term->selection.coords.end, &unmark_selected, &(struct mark_context){0}); render_refresh(term); } term->selection.kind = SELECTION_NONE; - term->selection.start = (struct coord){-1, -1}; - term->selection.end = (struct coord){-1, -1}; + term->selection.coords.start = (struct coord){-1, -1}; + term->selection.coords.end = (struct coord){-1, -1}; term->selection.pivot.start = (struct coord){-1, -1}; term->selection.pivot.end = (struct coord){-1, -1}; term->selection.direction = SELECTION_UNDIR; @@ -1566,7 +1566,7 @@ text_to_clipboard(struct seat *seat, struct terminal *term, char *text, uint32_t void selection_to_clipboard(struct seat *seat, struct terminal *term, uint32_t serial) { - if (term->selection.start.row < 0 || term->selection.end.row < 0) + if (term->selection.coords.start.row < 0 || term->selection.coords.end.row < 0) return; /* Get selection as a string */ diff --git a/terminal.c b/terminal.c index f6d8f814..84edec65 100644 --- a/terminal.c +++ b/terminal.c @@ -1157,8 +1157,10 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, .cursor = conf->cursor.color.cursor, }, .selection = { - .start = {-1, -1}, - .end = {-1, -1}, + .coords = { + .start = {-1, -1}, + .end = {-1, -1}, + }, .pivot = { .start = {-1, -1}, .end = {-1, -1}, @@ -2155,8 +2157,8 @@ term_erase_scrollback(struct terminal *term) const int rel_start = (start - scrollback_start + num_rows) & mask; const int rel_end = (end - scrollback_start + num_rows) & mask; - const int sel_start = term->selection.start.row; - const int sel_end = term->selection.end.row; + const int sel_start = term->selection.coords.start.row; + const int sel_end = term->selection.coords.end.row; if (sel_end >= 0) { /* @@ -2237,8 +2239,10 @@ UNITTEST }, .grid = &term.normal, .selection = { - .start = {-1, -1}, - .end = {-1, -1}, + .coords = { + .start = {-1, -1}, + .end = {-1, -1}, + }, .kind = SELECTION_NONE, .auto_scroll = { .fd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK), @@ -2281,14 +2285,14 @@ UNITTEST term.normal.offset = 14; /* Screen covers rows 14,15,0,1,2 */ /* Selection covers rows 15,0,1,2,3 */ - term.selection.start = (struct coord){.row = 15}; - term.selection.end = (struct coord){.row = 19}; + term.selection.coords.start = (struct coord){.row = 15}; + term.selection.coords.end = (struct coord){.row = 19}; term.selection.kind = SELECTION_CHAR_WISE; populate_scrollback(); term_erase_scrollback(&term); - xassert(term.selection.start.row < 0); - xassert(term.selection.end.row < 0); + xassert(term.selection.coords.start.row < 0); + xassert(term.selection.coords.end.row < 0); xassert(term.selection.kind == SELECTION_NONE); /* @@ -2297,18 +2301,18 @@ UNITTEST */ /* Selection covers rows 15,0 */ - term.selection.start = (struct coord){.row = 15}; - term.selection.end = (struct coord){.row = 16}; + term.selection.coords.start = (struct coord){.row = 15}; + term.selection.coords.end = (struct coord){.row = 16}; term.selection.kind = SELECTION_CHAR_WISE; populate_scrollback(); term_erase_scrollback(&term); - xassert(term.selection.start.row == 15); - xassert(term.selection.end.row == 16); + xassert(term.selection.coords.start.row == 15); + xassert(term.selection.coords.end.row == 16); xassert(term.selection.kind == SELECTION_CHAR_WISE); - term.selection.start = (struct coord){-1, -1}; - term.selection.end = (struct coord){-1, -1}; + term.selection.coords.start = (struct coord){-1, -1}; + term.selection.coords.end = (struct coord){-1, -1}; term.selection.kind = SELECTION_NONE; /* @@ -2502,7 +2506,7 @@ term_scroll_partial(struct terminal *term, struct scroll_region region, int rows xassert(rows <= region.end - region.start); /* Cancel selections that cannot be scrolled */ - if (unlikely(term->selection.end.row >= 0)) { + if (unlikely(term->selection.coords.end.row >= 0)) { /* * Selection is (partly) inside either the top or bottom * scrolling regions, or on (at least one) of the lines @@ -2567,7 +2571,7 @@ term_scroll_reverse_partial(struct terminal *term, xassert(rows <= region.end - region.start); /* Cancel selections that cannot be scrolled */ - if (unlikely(term->selection.end.row >= 0)) { + if (unlikely(term->selection.coords.end.row >= 0)) { /* * Selection is (partly) inside either the top or bottom * scrolling regions, or on (at least one) of the lines diff --git a/terminal.h b/terminal.h index 76f6bf22..03297678 100644 --- a/terminal.h +++ b/terminal.h @@ -79,6 +79,11 @@ struct coord { int row; }; +struct range { + struct coord start; + struct coord end; +}; + struct cursor { struct coord point; bool lcf; @@ -288,8 +293,7 @@ struct url { uint64_t id; char *url; char32_t *key; - struct coord start; - struct coord end; + struct range range; enum url_action action; bool url_mode_dont_change_url_attr; /* Entering/exiting URL mode doesn’t touch the cells’ attr.url */ bool osc8; @@ -476,15 +480,11 @@ struct terminal { struct { enum selection_kind kind; enum selection_direction direction; - struct coord start; - struct coord end; + struct range coords; bool ongoing; bool spaces_only; /* SELECTION_SEMANTIC_WORD */ - struct { - struct coord start; - struct coord end; - } pivot; + struct range pivot; struct { int fd; diff --git a/url-mode.c b/url-mode.c index 6625e99a..365d1e1b 100644 --- a/url-mode.c +++ b/url-mode.c @@ -411,8 +411,10 @@ auto_detected(const struct terminal *term, enum url_action action, ((struct url){ .id = (uint64_t)rand() << 32 | rand(), .url = url_utf8, - .start = start, - .end = end, + .range = { + .start = start, + .end = end, + }, .action = action, .osc8 = false})); } @@ -466,8 +468,10 @@ osc8_uris(const struct terminal *term, enum url_action action, url_list_t *urls) ((struct url){ .id = range->id, .url = xstrdup(range->uri), - .start = start, - .end = end, + .range = { + .start = start, + .end = end, + }, .action = action, .url_mode_dont_change_url_attr = dont_touch_url_attr, .osc8 = true})); @@ -486,11 +490,11 @@ remove_overlapping(url_list_t *urls, int cols) const struct url *out = &outer->item; const struct url *in = &inner->item; - uint64_t in_start = in->start.row * cols + in->start.col; - uint64_t in_end = in->end.row * cols + in->end.col; + uint64_t in_start = in->range.start.row * cols + in->range.start.col; + uint64_t in_end = in->range.end.row * cols + in->range.end.col; - uint64_t out_start = out->start.row * cols + out->start.col; - uint64_t out_end = out->end.row * cols + out->end.col; + uint64_t out_start = out->range.start.row * cols + out->range.start.col; + uint64_t out_end = out->range.end.row * cols + out->range.end.col; if ((in_start <= out_start && in_end >= out_start) || (in_start <= out_end && in_end >= out_end) || @@ -669,8 +673,8 @@ tag_cells_for_url(struct terminal *term, const struct url *url, bool value) if (url->url_mode_dont_change_url_attr) return; - const struct coord *start = &url->start; - const struct coord *end = &url->end; + const struct coord *start = &url->range.start; + const struct coord *end = &url->range.end; size_t end_r = end->row & (term->grid->num_rows - 1);