From b68d5da71b5014d7570c6d8c097ab4a9905ed287 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 24 Apr 2022 12:03:31 +0200 Subject: search: fix debug log This has been broken since the forward/backward search logic was refactored. --- search.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'search.c') diff --git a/search.c b/search.c index a1ef8eb4..77492e3f 100644 --- a/search.c +++ b/search.c @@ -467,7 +467,8 @@ search_find_next(struct terminal *term, enum search_direction direction) LOG_DBG( "update: %s: starting at row=%d col=%d " "(offset = %d, view = %d)", - backward ? "backward" : "forward", start.row, start.col, + direction != SEARCH_FORWARD ? "backward" : "forward", + start.row, start.col, grid->offset, grid->view); struct coord end = start; -- cgit v1.3 From f7c29ee39497a23b95e4ef97fc3ed87360014d7b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 23 Apr 2022 12:24:28 +0200 Subject: search: maches_next: assert match coordinates are valid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * They are within range (i.e. ‘row’ does not exceed term->rows-1) * ‘end’ comes after ‘start’ --- search.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'search.c') diff --git a/search.c b/search.c index 77492e3f..330ea791 100644 --- a/search.c +++ b/search.c @@ -561,6 +561,15 @@ search_matches_next(struct search_match_iterator *iter) match.end.row = match.end.row - grid->view + grid->num_rows; match.end.row &= grid->num_rows - 1; + xassert(match.start.row >= 0); + xassert(match.start.row < term->rows); + xassert(match.end.row >= 0); + xassert(match.end.row < term->rows); + + xassert(match.end.row > match.start.row || + (match.end.row == match.start.row && + match.end.col >= match.start.col)); + if (return_primary_match) { iter->start.row = 0; iter->start.col = 0; -- cgit v1.3 From d068e821d60dc9d9ae606969010b087040b98b59 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 23 Apr 2022 12:25:21 +0200 Subject: search: matches_next: don’t wrap around grid->num_rows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When bumping the iter’s start.row, we’re working with view-local coordinates. That is, 0 >= row < term->rows. This means it is wrong to and with grid->num_rows - 1, because a), ‘row’ should **never** be that big. And b), if we do, we’ll just end up in an infinite loop, where the next call to matches_next() just starts over from the beginning again (and eventually hitting the exact same place that got us started). --- search.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index 330ea791..8f99fae7 100644 --- a/search.c +++ b/search.c @@ -580,10 +580,14 @@ search_matches_next(struct search_match_iterator *iter) if (iter->start.col >= term->cols) { iter->start.col = 0; - iter->start.row++; - iter->start.row &= grid->num_rows - 1; + iter->start.row++; /* Overflow is caught in next iteration */ } + xassert(iter->start.row >= 0); + xassert(iter->start.row <= term->rows); + xassert(iter->start.col >= 0); + xassert(iter->start.col < term->cols); + if (match.start.row == term->search.match.row && match.start.col == term->search.match.col) { -- cgit v1.3 From 082e242ce5c954c59f52a6878a6d3794694a1182 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 23 Apr 2022 12:28:12 +0200 Subject: search: matches_next: stop searching when start.row >= term->rows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As this means the last call to sarch_matches_next() found a match at the bottom of the view, and then set the iter’s *next* start position to a row outside the view. This is fine, but we need to handle it, by immediately stopping the iter. --- search.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'search.c') diff --git a/search.c b/search.c index 8f99fae7..337db697 100644 --- a/search.c +++ b/search.c @@ -539,7 +539,16 @@ search_matches_next(struct search_match_iterator *iter) /* First, return the primary match */ match = term->selection.coords; found = true; - } else { + } + + else if (iter->start.row >= term->rows) { + goto no_match; + } + + else { + xassert(iter->start.row >= 0); + xassert(iter->start.row < term->rows); + struct coord abs_start = iter->start; abs_start.row = grid_row_absolute_in_view(grid, abs_start.row); -- cgit v1.3 From 1d48b7b77c69ca518d0bbdc079ca3736311b9c98 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sat, 23 Apr 2022 12:35:07 +0200 Subject: search: matches_next: assert start’s ‘col’ is valid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- search.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'search.c') diff --git a/search.c b/search.c index 337db697..73f831eb 100644 --- a/search.c +++ b/search.c @@ -548,6 +548,8 @@ search_matches_next(struct search_match_iterator *iter) else { xassert(iter->start.row >= 0); xassert(iter->start.row < term->rows); + xassert(iter->start.col >= 0); + xassert(iter->start.col < term->cols); struct coord abs_start = iter->start; abs_start.row = grid_row_absolute_in_view(grid, abs_start.row); -- cgit v1.3 From 5c4ddebc3c57724bd89b959a33f5ad0bd97f482b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 25 Apr 2022 20:00:47 +0200 Subject: search: fix multiple crashes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * When extending the selection to the next word boundary, ensure the row numbers are valid: - use selection_get_end() when retrieving the current end coordinate. This alone fixes a crash where we previously would crash in an out-of-bounds array access in grid->rows[], due to term->selection.coords.end being unbounded. - ensure the new end coordinate is bounded before and after calling selection_find_word_boundary_right(). * When moving the viewport (to ensure a new match is visible), make sure we don’t end up with the match outside the new viewport. Under certain, unusual, circumstances, the moved viewport _still_ did not contain the match. This resulted in assertions triggering later, that assumed the match(es) are *always* visible. It’s fairly easy to trigger this one by running foot with e.g. foot -o scrollback.lines=0 --window-size-chars 25x3 and then hitting enter a couple of times, to fill the scrollback history (which should consist of a single row in the example above), and the do a scrollback search for (part of) the prompt, and keep searching backward until it crashes. This would happen if calculated (new) viewport had to be adjusted (for example, to ensure it didn’t go past the scrollback end). This patch changes the logic used when calculating the new viewport. Instead of calculating the wanted viewport (match is vertically centered) and then trying to adjust it to ensure the new viewport is valid, start with a “safe” new viewport value, and then determine how much we can move it, if at all, to center the match. This is done by using scrollback relative coordinates. In this coordinate system, the new viewport must be >= 0, and < grid->num_lines - term->rows This makes it very easy to limit the amount by which the viewport is adjusted. As a side-effect, we can remove all the old re-adjustment logic. * The match iterator no longer special cases the primary match. This was needed before, when the search iterator did not handle overlapping matches correctly. Now that we do, the iterator is guaranteed to find the primary match, and thus we no longer need to special case it. This fixes a bug where the primary match was returned twice, due to the logic checking if a secondary match is the same as the primary match was flawed... Closes #1036 --- search.c | 180 ++++++++++++++++++++++++--------------------------------------- 1 file changed, 69 insertions(+), 111 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index 73f831eb..09b59d50 100644 --- a/search.c +++ b/search.c @@ -82,11 +82,7 @@ search_ensure_size(struct terminal *term, size_t wanted_size) static bool has_wrapped_around(const struct terminal *term, int abs_row_no) { - const struct grid *grid = term->grid; - int scrollback_start = grid->offset + term->rows; - int rebased_row = abs_row_no - scrollback_start + grid->num_rows; - rebased_row &= grid->num_rows - 1; - + int rebased_row = grid_row_abs_to_sb(term->grid, term->rows, abs_row_no); return rebased_row == 0; } @@ -182,6 +178,9 @@ search_update_selection(struct terminal *term, const struct range *match) int end_row = match->end.row; int end_col = match->end.col; + xassert(start_row >= 0); + xassert(start_row < grid->num_rows); + bool move_viewport = true; int view_end = (grid->view + term->rows - 1) & (grid->num_rows - 1); @@ -196,24 +195,14 @@ search_update_selection(struct terminal *term, const struct range *match) } if (move_viewport) { - int old_view = grid->view; - int new_view = start_row - term->rows / 2; - - while (new_view < 0) - new_view += grid->num_rows; - - new_view = ensure_view_is_allocated(term, new_view); - - /* Don't scroll past scrollback history */ - int end = (grid->offset + term->rows - 1) & (grid->num_rows - 1); - if (end >= grid->offset) { - /* Not wrapped */ - if (new_view >= grid->offset && new_view <= end) - new_view = grid->offset; - } else { - if (new_view >= grid->offset || new_view <= end) - new_view = grid->offset; - } + int rebased_new_view = grid_row_abs_to_sb(grid, term->rows, start_row); + + rebased_new_view -= term->rows / 2; + rebased_new_view = + min(max(rebased_new_view, 0), grid->num_rows - term->rows); + + const int old_view = grid->view; + int new_view = grid_row_sb_to_abs(grid, term->rows, rebased_new_view); #if defined(_DEBUG) /* Verify all to-be-visible rows have been allocated */ @@ -227,23 +216,8 @@ search_update_selection(struct terminal *term, const struct range *match) term_damage_view(term); } -#if 0 - /* Selection endpoint is inclusive */ - if (--end_col < 0) { - end_col = term->cols - 1; - end_row--; - } -#endif - - /* - * Begin a new selection if the start coords changed - * - * Note: check column against selection.coords, since our “old” - * start column isn’t reliable - we modify it when searching - * “next” or “prev”. - */ if (start_row != term->search.match.row || - start_col != term->selection.coords.start.col) + start_col != term->search.match.col) { int selection_row = start_row - grid->view + grid->num_rows; selection_row &= grid->num_rows - 1; @@ -516,7 +490,7 @@ search_matches_new_iter(struct terminal *term) { return (struct search_match_iterator){ .term = term, - .start = {-2, -2}, + .start = {0, 0}, }; } @@ -529,87 +503,64 @@ search_matches_next(struct search_match_iterator *iter) if (term->search.match_len == 0) goto no_match; - struct range match; - bool found; + if (iter->start.row >= term->rows) + goto no_match; - const bool return_primary_match = - iter->start.row == -2 && term->selection.coords.end.row >= 0; + xassert(iter->start.row >= 0); + xassert(iter->start.row < term->rows); + xassert(iter->start.col >= 0); + xassert(iter->start.col < term->cols); - if (return_primary_match) { - /* First, return the primary match */ - match = term->selection.coords; - found = true; - } + struct coord abs_start = iter->start; + abs_start.row = grid_row_absolute_in_view(grid, abs_start.row); - else if (iter->start.row >= term->rows) { + struct coord abs_end = { + term->cols - 1, + grid_row_absolute_in_view(grid, term->rows - 1)}; + + struct range match; + bool found = find_next(term, SEARCH_FORWARD, abs_start, abs_end, &match); + if (!found) goto no_match; - } - else { - xassert(iter->start.row >= 0); - xassert(iter->start.row < term->rows); - xassert(iter->start.col >= 0); - xassert(iter->start.col < term->cols); + LOG_DBG("match at (absolute coordinates) %dx%d-%dx%d", + match.start.row, match.start.col, + match.end.row, match.end.col); - struct coord abs_start = iter->start; - abs_start.row = grid_row_absolute_in_view(grid, abs_start.row); + /* Convert absolute row numbers back to view relative */ + match.start.row = match.start.row - grid->view + grid->num_rows; + match.start.row &= grid->num_rows - 1; + match.end.row = match.end.row - grid->view + grid->num_rows; + match.end.row &= grid->num_rows - 1; - struct coord abs_end = { - term->cols - 1, - grid_row_absolute_in_view(grid, term->rows - 1)}; + LOG_DBG("match at (view-local coordinates) %dx%d-%dx%d, view=%d", + match.start.row, match.start.col, + match.end.row, match.end.col, grid->view); - found = find_next(term, SEARCH_FORWARD, abs_start, abs_end, &match); - } + xassert(match.start.row >= 0); + xassert(match.start.row < term->rows); + xassert(match.end.row >= 0); + xassert(match.end.row < term->rows); - if (found) { - LOG_DBG("match at %dx%d-%dx%d", - match.start.row, match.start.col, - match.end.row, match.end.col); - - /* Convert absolute row numbers back to view relative */ - match.start.row = match.start.row - grid->view + grid->num_rows; - match.start.row &= grid->num_rows - 1; - match.end.row = match.end.row - grid->view + grid->num_rows; - match.end.row &= grid->num_rows - 1; - - xassert(match.start.row >= 0); - xassert(match.start.row < term->rows); - xassert(match.end.row >= 0); - xassert(match.end.row < term->rows); - - xassert(match.end.row > match.start.row || - (match.end.row == match.start.row && - match.end.col >= match.start.col)); - - if (return_primary_match) { - iter->start.row = 0; - iter->start.col = 0; - } else { - /* Continue at next column, next time */ - iter->start.row = match.start.row; - iter->start.col = match.start.col + 1; - - if (iter->start.col >= term->cols) { - iter->start.col = 0; - iter->start.row++; /* Overflow is caught in next iteration */ - } + xassert(match.end.row > match.start.row || + (match.end.row == match.start.row && + match.end.col >= match.start.col)); - xassert(iter->start.row >= 0); - xassert(iter->start.row <= term->rows); - xassert(iter->start.col >= 0); - xassert(iter->start.col < term->cols); + /* Continue at next column, next time */ + iter->start.row = match.start.row; + iter->start.col = match.start.col + 1; - if (match.start.row == term->search.match.row && - match.start.col == term->search.match.col) - { - /* Primary match is handled explicitly */ - LOG_DBG("primary match: skipping"); - return search_matches_next(iter); - } - } - return match; + if (iter->start.col >= term->cols) { + iter->start.col = 0; + iter->start.row++; /* Overflow is caught in next iteration */ } + xassert(iter->start.row >= 0); + xassert(iter->start.row <= term->rows); + xassert(iter->start.col >= 0); + xassert(iter->start.col < term->cols); + return match; + no_match: iter->start.row = -1; iter->start.col = -1; @@ -663,15 +614,18 @@ search_match_to_end_of_word(struct terminal *term, bool spaces_only) if (term->search.match_len == 0) return; - xassert(term->selection.coords.end.row != -1); + xassert(term->selection.coords.end.row >= 0); struct grid *grid = term->grid; const bool move_cursor = term->search.cursor == term->search.len; - const struct coord old_end = term->selection.coords.end; + struct coord old_end = selection_get_end(term); struct coord new_end = old_end; struct row *row = NULL; + xassert(new_end.row >= 0); + xassert(new_end.row < grid->num_rows); + /* Advances a coordinate by one column, to the right. Returns * false if we’ve reached the scrollback wrap-around */ #define advance_pos(coord) __extension__ \ @@ -691,12 +645,16 @@ search_match_to_end_of_word(struct terminal *term, bool spaces_only) if (!advance_pos(new_end)) return; + xassert(new_end.row >= 0); + xassert(new_end.row < grid->num_rows); xassert(grid->rows[new_end.row] != NULL); /* Find next word boundary */ - new_end.row -= grid->view; + 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); new_end.row += grid->view; + new_end.row &= grid->num_rows - 1; struct coord pos = old_end; row = grid->rows[pos.row]; -- cgit v1.3 From 1e87dbc4dc87c9567a7851135d91728796b0696f Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Mon, 25 Apr 2022 19:55:00 +0200 Subject: search: work around Sway sub-surface unmap bug Unmapping a sub-surface in Sway does not damage the underlying surface. As a result, "committing" a scrollback search will typically leave most of the foot window dimmed. It can be seen when "cancelling" a search as well, but there it's less obvious - only the margins are left dimmed. This is because cancelling a search damaged the current viewport (something that shouldn't be needed). Out of sway, river, weston and mutter, only Sway needs this workaround. This is a workaround for https://github.com/swaywm/sway/issues/6960 --- search.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'search.c') diff --git a/search.c b/search.c index 09b59d50..72c589f2 100644 --- a/search.c +++ b/search.c @@ -116,6 +116,11 @@ search_cancel_keep_selection(struct terminal *term) term_xcursor_update(term); render_refresh(term); + + /* Work around Sway bug - unmapping a sub-surface does not damage + * the underlying surface */ + term_damage_margins(term); + term_damage_view(term); } void @@ -795,7 +800,6 @@ execute_binding(struct seat *seat, struct terminal *term, grid->view = ensure_view_is_allocated( term, term->search.original_view); } - term_damage_view(term); search_cancel(term); return true; -- cgit v1.3 From c82c6116ede2706a9d5b28d43e8271ec13be2794 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 26 Apr 2022 19:32:08 +0200 Subject: search: regression: crash when moving viewport MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 5c4ddebc3c57724bd89b959a33f5ad0bd97f482b refactored search_update_selection(), specifically, the logic that moves the viewport. It did so by converting the absolute row number (of the match) to scrollback relative coordinates. This way we could ensure the viewport wasn’t moved “too much” (e.g. beyond the scrollback start). However, grid_row_abs_to_sb() and grid_row_sb_to_abs() doesn’t take a partially filled scrollback into account. This means the row (numbers) it returns may refer to *uninitialized* rows. Since: * The match row itself is valid (we *know* it has text on it) * We *subtract* from it, when setting the new viewport (to center the match on the screen). it’s only the *upper* part of the new viewport that may be uninitialized. I.e. we may have adjusted it too much. So, what we need to do is move the viewport forward until its *first* row is initialized. Then we know the rest will be too. --- search.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'search.c') diff --git a/search.c b/search.c index 72c589f2..8f2315a0 100644 --- a/search.c +++ b/search.c @@ -209,6 +209,13 @@ search_update_selection(struct terminal *term, const struct range *match) const int old_view = grid->view; int new_view = grid_row_sb_to_abs(grid, term->rows, rebased_new_view); + /* Scrollback may not be completely filled yet */ + { + const int mask = grid->num_rows - 1; + while (grid->rows[new_view] == NULL) + new_view = (new_view + 1) & mask; + } + #if defined(_DEBUG) /* Verify all to-be-visible rows have been allocated */ for (int r = 0; r < term->rows; r++) -- cgit v1.3 From 694938b85bed679c95ac506711475bf06cd80b0e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 26 Apr 2022 19:47:02 +0200 Subject: search: assert that the match is *inside* the new viewport --- search.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'search.c') diff --git a/search.c b/search.c index 8f2315a0..d4460e5b 100644 --- a/search.c +++ b/search.c @@ -222,6 +222,15 @@ search_update_selection(struct terminal *term, const struct range *match) xassert(grid->rows[(new_view + r) & (grid->num_rows - 1)] != NULL); #endif +#if defined(_DEBUG) + { + int rel_start_row = grid_row_abs_to_sb(grid, term->rows, start_row); + int rel_view = grid_row_abs_to_sb(grid, term->rows, new_view); + xassert(rel_view <= rel_start_row); + xassert(rel_start_row < rel_view + term->rows); + } +#endif + /* Update view */ grid->view = new_view; if (new_view != old_view) -- cgit v1.3 From 0e9ebf433b589c15e17bd37f73991ecc71e9b3d6 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 26 Apr 2022 18:24:22 +0200 Subject: search: fix infinite loop when highlighting all matches MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit find_next() did not always terminate correctly, causing search_matches_next() to never terminate, which finally leads to an infinite loop when rendering the search overlay surface, while finding all matches to highlight. The problem is that find_next(), after having found the initial matching characters, enters a nested while loop that tries to match the rest of the search criteria. This inner while loop did not check if we’ve reached the last cell, and happily continued past it (eventually wrappping around the scrollback buffer). Closes #1047 --- CHANGELOG.md | 4 +++- search.c | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'search.c') diff --git a/CHANGELOG.md b/CHANGELOG.md index bb094d93..65dd2542 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -58,7 +58,8 @@ * UI not refreshing when pasting something into the scrollback search box, that does not result in a grid update (for example, when the search criteria did not result in any matches) ([#1040][1040]). -* foot freezing in scrollback search mode, using 100% CPU ([#1036][1036]). +* foot freezing in scrollback search mode, using 100% CPU + ([#1036][1036], [#1047][1047]). * Crash when extending a selection to the next word boundary in scrollback search mode ([#1036][1036]). * Scrollback search mode not always highlighting all matches @@ -66,6 +67,7 @@ [1040]: https://codeberg.org/dnkl/foot/issues/1040 [1036]: https://codeberg.org/dnkl/foot/issues/1036 +[1047]: https://codeberg.org/dnkl/foot/issues/1036 ### Security diff --git a/search.c b/search.c index d4460e5b..b4d06c6c 100644 --- a/search.c +++ b/search.c @@ -375,6 +375,13 @@ find_next(struct terminal *term, enum search_direction direction, if (match_len != term->search.len) { /* Didn't match (completely) */ + + if (match_start_row == abs_end.row && + match_start_col == abs_end.col) + { + break; + } + continue; } @@ -563,10 +570,16 @@ search_matches_next(struct search_match_iterator *iter) xassert(match.end.row >= 0); xassert(match.end.row < term->rows); + /* Assert match end comes *after* the match start */ xassert(match.end.row > match.start.row || (match.end.row == match.start.row && match.end.col >= match.start.col)); + /* Assert the match starts at, or after, the iterator position */ + xassert(match.start.row > iter->start.row || + (match.start.row == iter->start.row && + match.start.col >= iter->start.col)); + /* Continue at next column, next time */ iter->start.row = match.start.row; iter->start.col = match.start.col + 1; -- 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 'search.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