From 78665a7e809afd470e1236867761d51de9737eaa Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 26 Sep 2023 17:54:03 +0200 Subject: search: add more key bindings to extend the current match This patch adds the following new search key bindings: * extend-char (shift+right) * extend-line-down (shift+down) * extend-backward-char (shift+left) * extend-backward-to-word-boundary (ctrl+shift+left) * extend-backward-to-next-whitespace (ctrl+shift+alt+left) * extend-line-up (shift+up) They can be used to extend the search match (i.e. the selection). This patch also adds an initial set of key bindings to scroll in the scrollback history: * scrollback-up-page * scrollback-down-page These work just like the key bindings for the normal mode. Also note that it was already possible to scroll using the mouse. This patch also fixes a couple of search mode bugs: * crashing when a search match ends in the last column * grapheme clusters not being matched correctly * Search match not being "extendable" after a pointer leave event * A few others, related to either large matches, or extending matches after moving the viewport. There are still a couple of (known) issues: * A search match isn't correctly highlighted if its *starting* point is outside the viewport. * Extending the match to end of the scrollback (i.e. the most recent output) is simply buggy. Related to #419 --- search.c | 415 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 357 insertions(+), 58 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index 6c2a2a7e..1fb6bb3e 100644 --- a/search.c +++ b/search.c @@ -9,6 +9,7 @@ #define LOG_ENABLE_DBG 0 #include "log.h" #include "char32.h" +#include "commands.h" #include "config.h" #include "extract.h" #include "grid.h" @@ -82,7 +83,14 @@ search_ensure_size(struct terminal *term, size_t wanted_size) } static bool -has_wrapped_around(const struct terminal *term, int abs_row_no) +has_wrapped_around_left(const struct terminal *term, int abs_row_no) +{ + int rebased_row = grid_row_abs_to_sb(term->grid, term->rows, abs_row_no); + return rebased_row == term->grid->num_rows - 1 || term->grid->rows[abs_row_no] == NULL; +} + +static bool +has_wrapped_around_right(const struct terminal *term, int abs_row_no) { int rebased_row = grid_row_abs_to_sb(term->grid, term->rows, abs_row_no); return rebased_row == 0; @@ -235,13 +243,19 @@ search_update_selection(struct terminal *term, const struct range *match) } if (start_row != term->search.match.row || - start_col != term->search.match.col) + start_col != term->search.match.col || + + /* Pointer leave events trigger selection_finalize() :/ */ + !term->selection.ongoing) { int selection_row = start_row - grid->view + grid->num_rows; selection_row &= grid->num_rows - 1; selection_start( term, start_col, selection_row, SELECTION_CHAR_WISE, false); + + term->search.match.row = start_row; + term->search.match.col = start_col; } /* Update selection endpoint */ @@ -273,16 +287,16 @@ matches_cell(const struct terminal *term, const struct cell *cell, size_t search return -1; if (composed != NULL) { - if (search_ofs + 1 + composed->count > term->search.len) + if (search_ofs + composed->count > term->search.len) return -1; for (size_t j = 1; j < composed->count; j++) { - if (composed->chars[j] != term->search.buf[search_ofs + 1 + j]) + if (composed->chars[j] != term->search.buf[search_ofs + j]) return -1; } } - return composed != NULL ? 1 + composed->count : 1; + return composed != NULL ? composed->count : 1; } static bool @@ -369,8 +383,11 @@ find_next(struct terminal *term, enum search_direction direction, match_len += additional_chars; match_end_col++; - while (match_row->cells[match_end_col].wc > CELL_SPACER) + while (match_end_col < term->cols && + match_row->cells[match_end_col].wc > CELL_SPACER) + { match_end_col++; + } } if (match_len != term->search.len) { @@ -546,6 +563,7 @@ search_matches_next(struct search_match_iterator *iter) term->cols - 1, grid_row_absolute_in_view(grid, term->rows - 1)}; + /* BUG: matches *starting* outside the view, but ending *inside*, aren't matched */ struct range match; bool found = find_next(term, SEARCH_FORWARD, abs_start, abs_end, &match); if (!found) @@ -565,11 +583,6 @@ search_matches_next(struct search_match_iterator *iter) match.start.row, match.start.col, match.end.row, match.end.col, grid->view); - xassert(match.start.row >= 0); - xassert(match.start.row < term->rows); - 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 && @@ -642,67 +655,282 @@ search_add_chars(struct terminal *term, const char *src, size_t count) add_wchars(term, c32s, chars); } +enum extend_direction {SEARCH_EXTEND_LEFT, SEARCH_EXTEND_RIGHT}; + +static bool +coord_advance_left(const struct terminal *term, struct coord *pos, + const struct row **row) +{ + const struct grid *grid = term->grid; + struct coord new_pos = *pos; + + if (--new_pos.col < 0) { + new_pos.row = (new_pos.row - 1 + grid->num_rows) & (grid->num_rows - 1); + new_pos.col = term->cols - 1; + + if (has_wrapped_around_left(term, new_pos.row)) + return false; + + if (row != NULL) + *row = grid->rows[new_pos.row]; + } + + *pos = new_pos; + return true; +} + +static bool +coord_advance_right(const struct terminal *term, struct coord *pos, + const struct row **row) +{ + const struct grid *grid = term->grid; + struct coord new_pos = *pos; + + if (++new_pos.col >= term->cols) { + new_pos.row = (new_pos.row + 1) & (grid->num_rows - 1); + new_pos.col = 0; + + if (has_wrapped_around_right(term, new_pos.row)) + return false; + + if (row != NULL) + *row = grid->rows[new_pos.row]; + } + + *pos = new_pos; + return true; +} + static void -search_match_to_end_of_word(struct terminal *term, bool spaces_only) +search_extend_find_char(const struct terminal *term, struct coord *target, + enum extend_direction direction) { - if (term->search.match_len == 0) + struct coord pos = direction == SEARCH_EXTEND_LEFT + ? selection_get_start(term) : selection_get_end(term); + xassert(pos.row >= 0); + xassert(pos.row < term->grid->num_rows); + + *target = pos; + + const struct row *row = term->grid->rows[pos.row]; + + while (true) { + switch (direction) { + case SEARCH_EXTEND_LEFT: + if (!coord_advance_left(term, &pos, &row)) + return; + break; + + case SEARCH_EXTEND_RIGHT: + if (!coord_advance_right(term, &pos, &row)) + return; + break; + } + + const char32_t wc = row->cells[pos.col].wc; + + if (wc >= CELL_SPACER || wc == U'\0') + continue; + + *target = pos; return; + } +} - xassert(term->selection.coords.end.row >= 0); +static void +search_extend_find_char_left(const struct terminal *term, struct coord *target) +{ + search_extend_find_char(term, target, SEARCH_EXTEND_LEFT); +} +static void +search_extend_find_char_right(const struct terminal *term, struct coord *target) +{ + search_extend_find_char(term, target, SEARCH_EXTEND_RIGHT); +} + +static void +search_extend_find_word(const struct terminal *term, bool spaces_only, + struct coord *target, enum extend_direction direction) +{ struct grid *grid = term->grid; - const bool move_cursor = term->search.cursor == term->search.len; + struct coord pos = direction == SEARCH_EXTEND_LEFT + ? selection_get_start(term) + : selection_get_end(term); + + xassert(pos.row >= 0); + xassert(pos.row < grid->num_rows); - 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__ \ - ({ \ - bool wrapped_around = false; \ - if (++(coord).col >= term->cols) { \ - (coord).row = ((coord).row + 1) & (grid->num_rows - 1); \ - (coord).col = 0; \ - row = grid->rows[(coord).row]; \ - if (has_wrapped_around(term, (coord.row))) \ - wrapped_around = true; \ - } \ - !wrapped_around; \ - }) + *target = pos; /* First character to consider is the *next* character */ - if (!advance_pos(new_end)) - return; + switch (direction) { + case SEARCH_EXTEND_LEFT: + if (!coord_advance_left(term, &pos, NULL)) + return; + break; + + case SEARCH_EXTEND_RIGHT: + if (!coord_advance_right(term, &pos, NULL)) + return; + break; + } - xassert(new_end.row >= 0); - xassert(new_end.row < grid->num_rows); - xassert(grid->rows[new_end.row] != NULL); + xassert(pos.row >= 0); + xassert(pos.row < grid->num_rows); + xassert(grid->rows[pos.row] != NULL); /* 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, false); - new_end.row += grid->view; - new_end.row &= grid->num_rows - 1; + switch (direction) { + case SEARCH_EXTEND_LEFT: + selection_find_word_boundary_left(term, &pos, spaces_only); + break; + + case SEARCH_EXTEND_RIGHT: + selection_find_word_boundary_right(term, &pos, spaces_only, false); + break; + } - struct coord pos = old_end; - row = grid->rows[pos.row]; + *target = pos; +} + +static void +search_extend_find_word_left(const struct terminal *term, bool spaces_only, + struct coord *target) +{ + search_extend_find_word(term, spaces_only, target, SEARCH_EXTEND_LEFT); +} + +static void +search_extend_find_word_right(const struct terminal *term, bool spaces_only, + struct coord *target) +{ + search_extend_find_word(term, spaces_only, target, SEARCH_EXTEND_RIGHT); +} + +static void +search_extend_find_line(const struct terminal *term, struct coord *target, + enum extend_direction direction) +{ + struct coord pos = direction == SEARCH_EXTEND_LEFT + ? selection_get_start(term) : selection_get_end(term); + + xassert(pos.row >= 0); + xassert(pos.row < term->grid->num_rows); + + *target = pos; + + const struct grid *grid = term->grid; + + switch (direction) { + case SEARCH_EXTEND_LEFT: + pos.row = (pos.row - 1 + grid->num_rows) & (grid->num_rows - 1); + if (has_wrapped_around_right(term, pos.row)) + return; + break; + + case SEARCH_EXTEND_RIGHT: + pos.row = (pos.row + 1) & (grid->num_rows - 1); + if (has_wrapped_around_left(term, pos.row)) + return; + break; + } + + *target = pos; +} + +static void +search_extend_find_line_up(const struct terminal *term, struct coord *target) +{ + search_extend_find_line(term, target, SEARCH_EXTEND_LEFT); +} + +static void +search_extend_find_line_down(const struct terminal *term, struct coord *target) +{ + search_extend_find_line(term, target, SEARCH_EXTEND_RIGHT); +} + +static void +search_extend_left(struct terminal *term, const struct coord *target) +{ + const struct coord last_coord = selection_get_start(term); + struct coord pos = *target; + const struct row *row = term->grid->rows[pos.row]; + + const bool move_cursor = term->search.cursor != 0; + + struct extraction_context *ctx = extract_begin(SELECTION_NONE, false); + if (ctx == NULL) + return; + + while (pos.col != last_coord.col || pos.row != last_coord.row) { + if (!extract_one(term, row, &row->cells[pos.col], pos.col, ctx)) + break; + if (!coord_advance_right(term, &pos, &row)) + break; + } + + char32_t *new_text; + size_t new_len; + + if (!extract_finish_wide(ctx, &new_text, &new_len)) + return; + + if (!search_ensure_size(term, term->search.len + new_len)) + return; + + memmove(&term->search.buf[new_len], &term->search.buf[0], + term->search.len * sizeof(term->search.buf[0])); + + size_t actually_copied = 0; + for (size_t i = 0; i < new_len; i++) { + if (new_text[i] == U'\n') { + /* extract() adds newlines, which we never match against */ + continue; + } + + term->search.buf[actually_copied++] = new_text[i]; + term->search.len++; + } + + xassert(actually_copied <= new_len); + if (actually_copied < new_len) { + memmove( + &term->search.buf[actually_copied], &term->search.buf[new_len], + (term->search.len - actually_copied) * sizeof(term->search.buf[0])); + } + + term->search.buf[term->search.len] = U'\0'; + free(new_text); + + if (move_cursor) + term->search.cursor += actually_copied; + + struct range match = {.start = *target, .end = selection_get_end(term)}; + search_update_selection(term, &match); + + term->search.match_len = term->search.len; +} + +static void +search_extend_right(struct terminal *term, const struct coord *target) +{ + struct coord pos = selection_get_end(term); + const struct row *row = term->grid->rows[pos.row]; + + const bool move_cursor = term->search.cursor == term->search.len; struct extraction_context *ctx = extract_begin(SELECTION_NONE, false); if (ctx == NULL) return; do { - if (!advance_pos(pos)) + if (!coord_advance_right(term, &pos, &row)) break; if (!extract_one(term, row, &row->cells[pos.col], pos.col, ctx)) break; - } while (pos.col != new_end.col || pos.row != new_end.row); + } while (pos.col != target->col || pos.row != target->row); char32_t *new_text; size_t new_len; @@ -728,12 +956,9 @@ search_match_to_end_of_word(struct terminal *term, bool spaces_only) if (move_cursor) term->search.cursor = term->search.len; - struct range match = {.start = term->search.match, .end = new_end}; + struct range match = {.start = term->search.match, .end = *target}; search_update_selection(term, &match); - term->search.match_len = term->search.len; - -#undef advance_pos } static size_t @@ -822,6 +1047,20 @@ execute_binding(struct seat *seat, struct terminal *term, case BIND_ACTION_SEARCH_NONE: return false; + case BIND_ACTION_SEARCH_SCROLLBACK_UP_PAGE: + if (term->grid == &term->normal) { + cmd_scrollback_up(term, term->rows); + return true; + } + return false; + + case BIND_ACTION_SEARCH_SCROLLBACK_DOWN_PAGE: + if (term->grid == &term->normal) { + cmd_scrollback_down(term, term->rows); + return true; + } + return false; + case BIND_ACTION_SEARCH_CANCEL: if (term->search.view_followed_offset) grid->view = grid->offset; @@ -967,17 +1206,77 @@ execute_binding(struct seat *seat, struct terminal *term, return true; } - case BIND_ACTION_SEARCH_EXTEND_WORD: - search_match_to_end_of_word(term, false); + case BIND_ACTION_SEARCH_EXTEND_CHAR: { + struct coord target; + search_extend_find_char_right(term, &target); + search_extend_right(term, &target); + *update_search_result = false; + *redraw = true; + return true; + } + + case BIND_ACTION_SEARCH_EXTEND_WORD: { + struct coord target; + search_extend_find_word_right(term, false, &target); + search_extend_right(term, &target); *update_search_result = false; *redraw = true; return true; + } - case BIND_ACTION_SEARCH_EXTEND_WORD_WS: - search_match_to_end_of_word(term, true); + case BIND_ACTION_SEARCH_EXTEND_WORD_WS: { + struct coord target; + search_extend_find_word_right(term, true, &target); + search_extend_right(term, &target); *update_search_result = false; *redraw = true; return true; + } + + case BIND_ACTION_SEARCH_EXTEND_LINE_DOWN: { + struct coord target; + search_extend_find_line_down(term, &target); + search_extend_right(term, &target); + *update_search_result = false; + *redraw = true; + return true; + } + + case BIND_ACTION_SEARCH_EXTEND_BACKWARD_CHAR: { + struct coord target; + search_extend_find_char_left(term, &target); + search_extend_left(term, &target); + *update_search_result = false; + *redraw = true; + return true; + } + + case BIND_ACTION_SEARCH_EXTEND_BACKWARD_WORD: { + struct coord target; + search_extend_find_word_left(term, false, &target); + search_extend_left(term, &target); + *update_search_result = false; + *redraw = true; + return true; + } + + case BIND_ACTION_SEARCH_EXTEND_BACKWARD_WORD_WS: { + struct coord target; + search_extend_find_word_left(term, true, &target); + search_extend_left(term, &target); + *update_search_result = false; + *redraw = true; + return true; + } + + case BIND_ACTION_SEARCH_EXTEND_LINE_UP: { + struct coord target; + search_extend_find_line_up(term, &target); + search_extend_left(term, &target); + *update_search_result = false; + *redraw = true; + return true; + } case BIND_ACTION_SEARCH_CLIPBOARD_PASTE: text_from_clipboard( -- cgit v1.3 From ddf4eb3b78ebe590d0dd6675536f9a7e4043ee71 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 27 Sep 2023 18:36:52 +0200 Subject: search: don't try to extend a search match when there is none --- search.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'search.c') diff --git a/search.c b/search.c index 1fb6bb3e..185a83ca 100644 --- a/search.c +++ b/search.c @@ -705,6 +705,9 @@ static void search_extend_find_char(const struct terminal *term, struct coord *target, enum extend_direction direction) { + if (term->search.match_len == 0) + return; + struct coord pos = direction == SEARCH_EXTEND_LEFT ? selection_get_start(term) : selection_get_end(term); xassert(pos.row >= 0); @@ -753,11 +756,14 @@ static void search_extend_find_word(const struct terminal *term, bool spaces_only, struct coord *target, enum extend_direction direction) { + if (term->search.match_len == 0) + return; + struct grid *grid = term->grid; struct coord pos = direction == SEARCH_EXTEND_LEFT ? selection_get_start(term) : selection_get_end(term); - + xassert(pos.row >= 0); xassert(pos.row < grid->num_rows); @@ -812,6 +818,9 @@ static void search_extend_find_line(const struct terminal *term, struct coord *target, enum extend_direction direction) { + if (term->search.match_len == 0) + return; + struct coord pos = direction == SEARCH_EXTEND_LEFT ? selection_get_start(term) : selection_get_end(term); @@ -854,6 +863,9 @@ search_extend_find_line_down(const struct terminal *term, struct coord *target) static void search_extend_left(struct terminal *term, const struct coord *target) { + if (term->search.match_len == 0) + return; + const struct coord last_coord = selection_get_start(term); struct coord pos = *target; const struct row *row = term->grid->rows[pos.row]; @@ -916,6 +928,9 @@ search_extend_left(struct terminal *term, const struct coord *target) static void search_extend_right(struct terminal *term, const struct coord *target) { + if (term->search.match_len == 0) + return; + struct coord pos = selection_get_end(term); const struct row *row = term->grid->rows[pos.row]; -- cgit v1.3 From a772179b6c850aad32f52d51c384ad933746c0cd Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 8 Oct 2023 10:28:17 +0200 Subject: search: fix mixup in search_extend_find_line() The has_wrapped_around_{right,left} functions were mixed up, causing false positives and false negatives, resulting in bad search matches. Also make all search_extend_find* functions return a boolean; false means no change in the selection. In this case, we can skip trying to extend the selection, and updating the UI. --- search.c | 128 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 69 insertions(+), 59 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index 185a83ca..90dd337c 100644 --- a/search.c +++ b/search.c @@ -701,12 +701,12 @@ coord_advance_right(const struct terminal *term, struct coord *pos, return true; } -static void +static bool search_extend_find_char(const struct terminal *term, struct coord *target, enum extend_direction direction) { if (term->search.match_len == 0) - return; + return false; struct coord pos = direction == SEARCH_EXTEND_LEFT ? selection_get_start(term) : selection_get_end(term); @@ -721,12 +721,12 @@ search_extend_find_char(const struct terminal *term, struct coord *target, switch (direction) { case SEARCH_EXTEND_LEFT: if (!coord_advance_left(term, &pos, &row)) - return; + return false; break; case SEARCH_EXTEND_RIGHT: if (!coord_advance_right(term, &pos, &row)) - return; + return false; break; } @@ -736,28 +736,28 @@ search_extend_find_char(const struct terminal *term, struct coord *target, continue; *target = pos; - return; + return true; } } -static void +static bool search_extend_find_char_left(const struct terminal *term, struct coord *target) { - search_extend_find_char(term, target, SEARCH_EXTEND_LEFT); + return search_extend_find_char(term, target, SEARCH_EXTEND_LEFT); } -static void +static bool search_extend_find_char_right(const struct terminal *term, struct coord *target) { - search_extend_find_char(term, target, SEARCH_EXTEND_RIGHT); + return search_extend_find_char(term, target, SEARCH_EXTEND_RIGHT); } -static void +static bool search_extend_find_word(const struct terminal *term, bool spaces_only, struct coord *target, enum extend_direction direction) { if (term->search.match_len == 0) - return; + return false; struct grid *grid = term->grid; struct coord pos = direction == SEARCH_EXTEND_LEFT @@ -773,12 +773,12 @@ search_extend_find_word(const struct terminal *term, bool spaces_only, switch (direction) { case SEARCH_EXTEND_LEFT: if (!coord_advance_left(term, &pos, NULL)) - return; + return false; break; case SEARCH_EXTEND_RIGHT: if (!coord_advance_right(term, &pos, NULL)) - return; + return false; break; } @@ -798,28 +798,29 @@ search_extend_find_word(const struct terminal *term, bool spaces_only, } *target = pos; + return true; } -static void +static bool search_extend_find_word_left(const struct terminal *term, bool spaces_only, struct coord *target) { - search_extend_find_word(term, spaces_only, target, SEARCH_EXTEND_LEFT); + return search_extend_find_word(term, spaces_only, target, SEARCH_EXTEND_LEFT); } -static void +static bool search_extend_find_word_right(const struct terminal *term, bool spaces_only, struct coord *target) { - search_extend_find_word(term, spaces_only, target, SEARCH_EXTEND_RIGHT); + return search_extend_find_word(term, spaces_only, target, SEARCH_EXTEND_RIGHT); } -static void +static bool search_extend_find_line(const struct terminal *term, struct coord *target, enum extend_direction direction) { if (term->search.match_len == 0) - return; + return false; struct coord pos = direction == SEARCH_EXTEND_LEFT ? selection_get_start(term) : selection_get_end(term); @@ -834,30 +835,31 @@ search_extend_find_line(const struct terminal *term, struct coord *target, switch (direction) { case SEARCH_EXTEND_LEFT: pos.row = (pos.row - 1 + grid->num_rows) & (grid->num_rows - 1); - if (has_wrapped_around_right(term, pos.row)) - return; + if (has_wrapped_around_left(term, pos.row)) + return false; break; case SEARCH_EXTEND_RIGHT: pos.row = (pos.row + 1) & (grid->num_rows - 1); - if (has_wrapped_around_left(term, pos.row)) - return; + if (has_wrapped_around_right(term, pos.row)) + return false; break; } *target = pos; + return true; } -static void +static bool search_extend_find_line_up(const struct terminal *term, struct coord *target) { - search_extend_find_line(term, target, SEARCH_EXTEND_LEFT); + return search_extend_find_line(term, target, SEARCH_EXTEND_LEFT); } -static void +static bool search_extend_find_line_down(const struct terminal *term, struct coord *target) { - search_extend_find_line(term, target, SEARCH_EXTEND_RIGHT); + return search_extend_find_line(term, target, SEARCH_EXTEND_RIGHT); } static void @@ -1223,73 +1225,81 @@ execute_binding(struct seat *seat, struct terminal *term, case BIND_ACTION_SEARCH_EXTEND_CHAR: { struct coord target; - search_extend_find_char_right(term, &target); - search_extend_right(term, &target); - *update_search_result = false; - *redraw = true; + if (search_extend_find_char_right(term, &target)) { + search_extend_right(term, &target); + *update_search_result = false; + *redraw = true; + } return true; } case BIND_ACTION_SEARCH_EXTEND_WORD: { struct coord target; - search_extend_find_word_right(term, false, &target); - search_extend_right(term, &target); - *update_search_result = false; - *redraw = true; + if (search_extend_find_word_right(term, false, &target)) { + search_extend_right(term, &target); + *update_search_result = false; + *redraw = true; + } return true; } case BIND_ACTION_SEARCH_EXTEND_WORD_WS: { struct coord target; - search_extend_find_word_right(term, true, &target); - search_extend_right(term, &target); - *update_search_result = false; - *redraw = true; + if (search_extend_find_word_right(term, true, &target)) { + search_extend_right(term, &target); + *update_search_result = false; + *redraw = true; + } return true; } case BIND_ACTION_SEARCH_EXTEND_LINE_DOWN: { struct coord target; - search_extend_find_line_down(term, &target); - search_extend_right(term, &target); - *update_search_result = false; - *redraw = true; + if (search_extend_find_line_down(term, &target)) { + search_extend_right(term, &target); + *update_search_result = false; + *redraw = true; + } return true; } case BIND_ACTION_SEARCH_EXTEND_BACKWARD_CHAR: { struct coord target; - search_extend_find_char_left(term, &target); - search_extend_left(term, &target); - *update_search_result = false; - *redraw = true; + if (search_extend_find_char_left(term, &target)) { + search_extend_left(term, &target); + *update_search_result = false; + *redraw = true; + } return true; } case BIND_ACTION_SEARCH_EXTEND_BACKWARD_WORD: { struct coord target; - search_extend_find_word_left(term, false, &target); - search_extend_left(term, &target); - *update_search_result = false; - *redraw = true; + if (search_extend_find_word_left(term, false, &target)) { + search_extend_left(term, &target); + *update_search_result = false; + *redraw = true; + } return true; } case BIND_ACTION_SEARCH_EXTEND_BACKWARD_WORD_WS: { struct coord target; - search_extend_find_word_left(term, true, &target); - search_extend_left(term, &target); - *update_search_result = false; - *redraw = true; + if (search_extend_find_word_left(term, true, &target)) { + search_extend_left(term, &target); + *update_search_result = false; + *redraw = true; + } return true; } case BIND_ACTION_SEARCH_EXTEND_LINE_UP: { struct coord target; - search_extend_find_line_up(term, &target); - search_extend_left(term, &target); - *update_search_result = false; - *redraw = true; + if (search_extend_find_line_up(term, &target)) { + search_extend_left(term, &target); + *update_search_result = false; + *redraw = true; + } return true; } -- cgit v1.3 From 3e67415e3e7b09c3e10c4ace3008bbaaae54110a Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 8 Oct 2023 10:37:16 +0200 Subject: config: add remaining search.scrollback key bindings All scrollback up/down key bindings are now available in search mode. --- config.c | 10 ++++++++-- doc/foot.ini.5.scd | 25 +++++++++++++++++++++++++ foot.ini | 10 ++++++++++ key-binding.h | 7 ++++++- search.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 3 deletions(-) (limited to 'search.c') diff --git a/config.c b/config.c index 8bbe27cb..e5ebb558 100644 --- a/config.c +++ b/config.c @@ -134,6 +134,14 @@ static const char *const binding_action_map[] = { static const char *const search_binding_action_map[] = { [BIND_ACTION_SEARCH_NONE] = NULL, + [BIND_ACTION_SEARCH_SCROLLBACK_UP_PAGE] = "scrollback-up-page", + [BIND_ACTION_SEARCH_SCROLLBACK_UP_HALF_PAGE] = "scrollback-up-half-page", + [BIND_ACTION_SEARCH_SCROLLBACK_UP_LINE] = "scrollback-up-line", + [BIND_ACTION_SEARCH_SCROLLBACK_DOWN_PAGE] = "scrollback-down-page", + [BIND_ACTION_SEARCH_SCROLLBACK_DOWN_HALF_PAGE] = "scrollback-down-half-page", + [BIND_ACTION_SEARCH_SCROLLBACK_DOWN_LINE] = "scrollback-down-line", + [BIND_ACTION_SEARCH_SCROLLBACK_HOME] = "scrollback-home", + [BIND_ACTION_SEARCH_SCROLLBACK_END] = "scrollback-end", [BIND_ACTION_SEARCH_CANCEL] = "cancel", [BIND_ACTION_SEARCH_COMMIT] = "commit", [BIND_ACTION_SEARCH_FIND_PREV] = "find-prev", @@ -159,8 +167,6 @@ static const char *const search_binding_action_map[] = { [BIND_ACTION_SEARCH_CLIPBOARD_PASTE] = "clipboard-paste", [BIND_ACTION_SEARCH_PRIMARY_PASTE] = "primary-paste", [BIND_ACTION_SEARCH_UNICODE_INPUT] = "unicode-input", - [BIND_ACTION_SEARCH_SCROLLBACK_UP_PAGE] = "scrollback-up-page", - [BIND_ACTION_SEARCH_SCROLLBACK_DOWN_PAGE] = "scrollback-down-page", }; static const char *const url_binding_action_map[] = { diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index 2e0127b1..5ef4dcb4 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -987,6 +987,31 @@ scrollback search mode. The syntax is exactly the same as the regular Unicode input mode. See _key-bindings.unicode-input_ for details. Default: _none_. +*scrollback-up-page* + Scrolls up/back one page in history. Default: _Shift+Page\_Up_. + +*scrollback-up-half-page* + Scrolls up/back half of a page in history. Default: _none_. + +*scrollback-up-line* + Scrolls up/back a single line in history. Default: _none_. + +*scrollback-down-page* + Scroll down/forward one page in history. Default: + _Shift+Page\_Down_. + +*scrollback-down-half-page* + Scroll down/forward half of a page in history. Default: _none_. + +*scrollback-down-line* + Scroll down/forward a single line in history. Default: _none_. + +*scrollback-home* + Scroll to the beginning of the scrollback. Default: _none_. + +*scrollback-end* + Scroll to the end (bottom) of the scrollback. Default: _none_. + # SECTION: url-bindings This section lets you override the default key bindings used in URL diff --git a/foot.ini b/foot.ini index dbd2baab..cdbd8259 100644 --- a/foot.ini +++ b/foot.ini @@ -139,6 +139,8 @@ # scrollback-down-page=Shift+Page_Down # scrollback-down-half-page=none # scrollback-down-line=none +# scrollback-home=none +# scrollback-end=none # clipboard-copy=Control+Shift+c XF86Copy # clipboard-paste=Control+Shift+v XF86Paste # primary-paste=Shift+Insert @@ -187,6 +189,14 @@ # clipboard-paste=Control+v Control+Shift+v Control+y XF86Paste # primary-paste=Shift+Insert # unicode-input=none +# scrollback-up-page=Shift+Page_Up +# scrollback-up-half-page=none +# scrollback-up-line=none +# scrollback-down-page=Shift+Page_Down +# scrollback-down-half-page=none +# scrollback-down-line=none +# scrollback-home=none +# scrollback-end=none [url-bindings] # cancel=Control+g Control+c Control+d Escape diff --git a/key-binding.h b/key-binding.h index b85fc072..050c80a6 100644 --- a/key-binding.h +++ b/key-binding.h @@ -59,8 +59,13 @@ enum bind_action_normal { enum bind_action_search { BIND_ACTION_SEARCH_NONE, BIND_ACTION_SEARCH_SCROLLBACK_UP_PAGE, + BIND_ACTION_SEARCH_SCROLLBACK_UP_HALF_PAGE, + BIND_ACTION_SEARCH_SCROLLBACK_UP_LINE, BIND_ACTION_SEARCH_SCROLLBACK_DOWN_PAGE, - // TODO: copy the remaining scrollback key-bindings from normal mode + BIND_ACTION_SEARCH_SCROLLBACK_DOWN_HALF_PAGE, + BIND_ACTION_SEARCH_SCROLLBACK_DOWN_LINE, + BIND_ACTION_SEARCH_SCROLLBACK_HOME, + BIND_ACTION_SEARCH_SCROLLBACK_END, BIND_ACTION_SEARCH_CANCEL, BIND_ACTION_SEARCH_COMMIT, BIND_ACTION_SEARCH_FIND_PREV, diff --git a/search.c b/search.c index 90dd337c..55388577 100644 --- a/search.c +++ b/search.c @@ -1071,6 +1071,20 @@ execute_binding(struct seat *seat, struct terminal *term, } return false; + case BIND_ACTION_SEARCH_SCROLLBACK_UP_HALF_PAGE: + if (term->grid == &term->normal) { + cmd_scrollback_up(term, max(term->rows / 2, 1)); + return true; + } + break; + + case BIND_ACTION_SEARCH_SCROLLBACK_UP_LINE: + if (term->grid == &term->normal) { + cmd_scrollback_up(term, 1); + return true; + } + break; + case BIND_ACTION_SEARCH_SCROLLBACK_DOWN_PAGE: if (term->grid == &term->normal) { cmd_scrollback_down(term, term->rows); @@ -1078,6 +1092,34 @@ execute_binding(struct seat *seat, struct terminal *term, } return false; + case BIND_ACTION_SEARCH_SCROLLBACK_DOWN_HALF_PAGE: + if (term->grid == &term->normal) { + cmd_scrollback_down(term, max(term->rows / 2, 1)); + return true; + } + break; + + case BIND_ACTION_SEARCH_SCROLLBACK_DOWN_LINE: + if (term->grid == &term->normal) { + cmd_scrollback_down(term, 1); + return true; + } + break; + + case BIND_ACTION_SEARCH_SCROLLBACK_HOME: + if (term->grid == &term->normal) { + cmd_scrollback_up(term, term->grid->num_rows); + return true; + } + break; + + case BIND_ACTION_SEARCH_SCROLLBACK_END: + if (term->grid == &term->normal) { + cmd_scrollback_down(term, term->grid->num_rows); + return true; + } + break; + case BIND_ACTION_SEARCH_CANCEL: if (term->search.view_followed_offset) grid->view = grid->offset; -- cgit v1.3