From 8c0fca30db83576f7df5d48c94ecfa9896f8a075 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 23 Apr 2022 12:23:27 +0200 Subject: selection: find_word_boundary: assert ‘pos’ is valid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- selection.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'selection.c') diff --git a/selection.c b/selection.c index f279a834..da62d354 100644 --- a/selection.c +++ b/selection.c @@ -270,6 +270,11 @@ void selection_find_word_boundary_left(struct terminal *term, struct coord *pos, bool spaces_only) { + xassert(pos->row >= 0); + xassert(pos->row < term->rows); + xassert(pos->col >= 0); + xassert(pos->col < term->cols); + const struct row *r = grid_row_in_view(term->grid, pos->row); char32_t c = r->cells[pos->col].wc; @@ -343,6 +348,11 @@ void selection_find_word_boundary_right(struct terminal *term, struct coord *pos, bool spaces_only) { + xassert(pos->row >= 0); + xassert(pos->row < term->rows); + xassert(pos->col >= 0); + xassert(pos->col < term->cols); + const struct row *r = grid_row_in_view(term->grid, pos->row); char32_t c = r->cells[pos->col].wc; -- cgit v1.3 From 9c0f1a671cb015b650f7ed80e8fcf66d07c9e87a Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 23 Apr 2022 15:49:25 +0200 Subject: selection: assert serial is non-zero before copying data to the clipboard --- selection.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'selection.c') diff --git a/selection.c b/selection.c index da62d354..8a18c6bb 100644 --- a/selection.c +++ b/selection.c @@ -1533,6 +1533,8 @@ static const struct zwp_primary_selection_source_v1_listener primary_selection_s bool text_to_clipboard(struct seat *seat, struct terminal *term, char *text, uint32_t serial) { + xassert(serial != 0); + struct wl_clipboard *clipboard = &seat->clipboard; if (clipboard->data_source != NULL) { @@ -1568,7 +1570,6 @@ text_to_clipboard(struct seat *seat, struct terminal *term, char *text, uint32_t wl_data_device_set_selection(seat->data_device, clipboard->data_source, serial); /* Needed when sending the selection to other client */ - xassert(serial != 0); clipboard->serial = serial; return true; } @@ -1981,6 +1982,8 @@ text_to_primary(struct seat *seat, struct terminal *term, char *text, uint32_t s if (term->wl->primary_selection_device_manager == NULL) return false; + xassert(serial != 0); + struct wl_primary *primary = &seat->primary; /* TODO: somehow share code with the clipboard equivalent */ -- cgit v1.3 From 6316a5eb0cf2b1fe25ca0e9d60c4e11fce9c22d1 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 25 Apr 2022 19:59:23 +0200 Subject: selection: add start/end coordinate getters MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Internally, selection coordinates are *unbounded* (that is, the row numbers may be larger than grid->num_rows) while a selection is ongoing. Only after it has been finalized are the coordinates bounded. This means it isn’t safe to use term->selection.coords.* directly. --- selection.c | 24 ++++++++++++++++++++++++ selection.h | 3 +++ 2 files changed, 27 insertions(+) (limited to 'selection.c') diff --git a/selection.c b/selection.c index 8a18c6bb..091d3ed0 100644 --- a/selection.c +++ b/selection.c @@ -38,6 +38,29 @@ static const char *const mime_type_map[] = { [DATA_OFFER_MIME_TEXT_UTF8_STRING] = "UTF8_STRING", }; +static inline struct coord +bounded(const struct grid *grid, struct coord coord) +{ + coord.row &= grid->num_rows - 1; + return coord; +} + +struct coord +selection_get_start(const struct terminal *term) +{ + if (term->selection.coords.start.row < 0) + return term->selection.coords.start; + return bounded(term->grid, term->selection.coords.start); +} + +struct coord +selection_get_end(const struct terminal *term) +{ + if (term->selection.coords.end.row < 0) + return term->selection.coords.end; + return bounded(term->grid, term->selection.coords.end); +} + bool selection_on_rows(const struct terminal *term, int row_start, int row_end) { @@ -2461,3 +2484,4 @@ const struct zwp_primary_selection_device_v1_listener primary_selection_device_l .data_offer = &primary_data_offer, .selection = &primary_selection, }; + diff --git a/selection.h b/selection.h index 295d8d1c..0a6ece91 100644 --- a/selection.h +++ b/selection.h @@ -79,3 +79,6 @@ void selection_find_word_boundary_left( struct terminal *term, struct coord *pos, bool spaces_only); void selection_find_word_boundary_right( struct terminal *term, struct coord *pos, bool spaces_only); + +struct coord selection_get_start(const struct terminal *term); +struct coord selection_get_end(const struct terminal *term); -- cgit v1.3 From 76305104486a90c2320dd102c32fe26831ebb658 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 27 Apr 2022 18:44:57 +0200 Subject: selection: find_word_boundary_right: add “stop-on-space-to-word-boundary” MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When true, selection_find_word_boundary_right() behaves as before - it stops as soon as it encounters a character that isn’t of the same *type* as the “initial” character (the last character in the selection). Take this, for example: The Quick Brown Fox The selection will first stop at the end of “the”, then just *before* “quick”, then at the end of “quick”. Then just *before* “brown”, and then at the end of “brown”, and so on. This suits mouse selections pretty good. But when selection_find_word_boundary_right() is used to extend a search match, it’s better to ignore space-to-word character transitions. That is, we want The Quick Brown Fox to first extend to the end of “the”, then immediately to the end of “quick”, then to the end of “brown”, and so on. Setting the ‘stop_to_space_to_word_boundary’ argument to false results in latter behavior. This is now done by search, when executing the “extend-to-word-boundary” and “extend-to-next-whitespace” key bindings. --- search.c | 2 +- selection.c | 27 +++++++++++++++++++-------- selection.h | 3 ++- 3 files changed, 22 insertions(+), 10 deletions(-) (limited to 'selection.c') diff --git a/search.c b/search.c index b4d06c6c..f6d377ea 100644 --- a/search.c +++ b/search.c @@ -686,7 +686,7 @@ search_match_to_end_of_word(struct terminal *term, bool spaces_only) /* Find next word boundary */ new_end.row -= grid->view + grid->num_rows; new_end.row &= grid->num_rows - 1; - selection_find_word_boundary_right(term, &new_end, spaces_only); + selection_find_word_boundary_right(term, &new_end, spaces_only, false); new_end.row += grid->view; new_end.row &= grid->num_rows - 1; diff --git a/selection.c b/selection.c index 091d3ed0..6b57f48c 100644 --- a/selection.c +++ b/selection.c @@ -369,7 +369,8 @@ selection_find_word_boundary_left(struct terminal *term, struct coord *pos, void selection_find_word_boundary_right(struct terminal *term, struct coord *pos, - bool spaces_only) + bool spaces_only, + bool stop_on_space_to_word_boundary) { xassert(pos->row >= 0); xassert(pos->row < term->rows); @@ -395,6 +396,7 @@ selection_find_word_boundary_right(struct terminal *term, struct coord *pos, !initial_is_space && !isword(c, spaces_only, term->conf->word_delimiters); bool initial_is_word = c != 0 && isword(c, spaces_only, term->conf->word_delimiters); + bool have_seen_word = initial_is_word; while (true) { int next_col = pos->col + 1; @@ -435,13 +437,22 @@ selection_find_word_boundary_right(struct terminal *term, struct coord *pos, bool is_word = c != 0 && isword(c, spaces_only, term->conf->word_delimiters); - if (initial_is_space && !is_space) - break; - if (initial_is_delim && !is_delim) - break; + if (stop_on_space_to_word_boundary) { + if (initial_is_space && !is_space) + break; + if (initial_is_delim && !is_delim) + break; + } else { + if (initial_is_space && ((have_seen_word && is_space) || is_delim)) + break; + if (initial_is_delim && ((have_seen_word && is_delim) || is_space)) + break; + } if (initial_is_word && !is_word) break; + have_seen_word = is_word; + pos->col = next_col; pos->row = next_row; } @@ -522,7 +533,7 @@ selection_start(struct terminal *term, int col, int row, case SELECTION_WORD_WISE: { struct coord start = {col, row}, end = {col, row}; selection_find_word_boundary_left(term, &start, spaces_only); - selection_find_word_boundary_right(term, &end, spaces_only); + selection_find_word_boundary_right(term, &end, spaces_only, true); term->selection.coords.start = (struct coord){ start.col, term->grid->view + start.row}; @@ -863,7 +874,7 @@ selection_update(struct terminal *term, int col, int row) case SELECTION_RIGHT: { struct coord end = {col, row}; selection_find_word_boundary_right( - term, &end, term->selection.spaces_only); + term, &end, term->selection.spaces_only, true); new_end = (struct coord){end.col, term->grid->view + end.row}; break; } @@ -1013,7 +1024,7 @@ selection_extend_normal(struct terminal *term, int col, int row, struct coord pivot_end = pivot_start; selection_find_word_boundary_left(term, &pivot_start, spaces_only); - selection_find_word_boundary_right(term, &pivot_end, spaces_only); + selection_find_word_boundary_right(term, &pivot_end, spaces_only, true); term->selection.pivot.start = (struct coord){pivot_start.col, term->grid->view + pivot_start.row}; diff --git a/selection.h b/selection.h index 0a6ece91..3d0c224e 100644 --- a/selection.h +++ b/selection.h @@ -78,7 +78,8 @@ void selection_stop_scroll_timer(struct terminal *term); void selection_find_word_boundary_left( struct terminal *term, struct coord *pos, bool spaces_only); void selection_find_word_boundary_right( - struct terminal *term, struct coord *pos, bool spaces_only); + struct terminal *term, struct coord *pos, bool spaces_only, + bool stop_on_space_to_word_boundary); struct coord selection_get_start(const struct terminal *term); struct coord selection_get_end(const struct terminal *term); -- cgit v1.3