From 61cabdac13986e6cb2ba69e81d5934f55d92a1a9 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 27 Aug 2019 17:23:28 +0200 Subject: search: wip: re-direct input while searching, and build a search buffer This adds a new state, 'is_searching'. While active, input is re-directed, and stored in a search buffer. In the future, we'll use this buffer and search for its content in the scrollback buffer, and move the view and create a selection on matches. When rendering in 'is_searching', everything is dimmed. In the future, we'll render the current search buffer on-top of the dimmed "regular" terminal output. --- search.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 search.c (limited to 'search.c') diff --git a/search.c b/search.c new file mode 100644 index 00000000..55c67bb0 --- /dev/null +++ b/search.c @@ -0,0 +1,107 @@ +#include "search.h" + +#include +#include + +#define LOG_MODULE "search" +#define LOG_ENABLE_DBG 1 +#include "log.h" +#include "render.h" + +void +search_begin(struct terminal *term) +{ + LOG_DBG("search: begin"); + + free(term->search.buf); + term->search.buf = NULL; + term->search.len = 0; + term->search.sz = 0; + term->is_searching = true; + + render_refresh(term); +} + +void +search_cancel(struct terminal *term) +{ + LOG_DBG("search: cancel"); + + free(term->search.buf); + term->search.buf = NULL; + term->search.len = 0; + term->search.sz = 0; + term->is_searching = false; + + render_refresh(term); +} + +void +search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask_t mods) +{ + LOG_DBG("search: input: sym=%d/0x%x, mods=0x%08x", sym, sym, mods); + + const xkb_mod_mask_t ctrl = 1 << term->kbd.mod_ctrl; + //const xkb_mod_mask_t alt = 1 << term->kbd.mod_alt; + //const xkb_mod_mask_t shift = 1 << term->kbd.mod_shift; + //const xkb_mod_mask_t meta = 1 << term->kbd.mod_meta; + + enum xkb_compose_status compose_status = xkb_compose_state_get_status( + term->kbd.xkb_compose_state); + + if ((mods == 0 && sym == XKB_KEY_Escape) || (mods == ctrl && sym == XKB_KEY_g)) + search_cancel(term); + + else if (mods == 0 && sym == XKB_KEY_BackSpace) { + if (term->search.len > 0) + term->search.buf[--term->search.len] = L'\0'; + } + + else { + uint8_t buf[64] = {0}; + int count = 0; + + if (compose_status == XKB_COMPOSE_COMPOSED) { + count = xkb_compose_state_get_utf8( + term->kbd.xkb_compose_state, (char *)buf, sizeof(buf)); + xkb_compose_state_reset(term->kbd.xkb_compose_state); + } else { + count = xkb_state_key_get_utf8( + term->kbd.xkb_state, key, (char *)buf, sizeof(buf)); + } + + const char *src = (const char *)buf; + mbstate_t ps = {0}; + size_t wchars = mbsnrtowcs(NULL, &src, count, 0, &ps); + + if (wchars == -1) { + LOG_ERRNO("failed to convert %.*s to wchars", count, buf); + return; + } + + while (term->search.len + wchars >= term->search.sz) { + size_t new_sz = term->search.sz == 0 ? 64 : term->search.sz * 2; + wchar_t *new_buf = realloc(term->search.buf, new_sz * sizeof(term->search.buf[0])); + + if (new_buf == NULL) { + LOG_ERRNO("failed to resize search buffer"); + return; + } + + term->search.buf = new_buf; + term->search.sz = new_sz; + } + + assert(term->search.len + wchars < term->search.sz); + + memset(&ps, 0, sizeof(ps)); + mbsnrtowcs(&term->search.buf[term->search.len], &src, count, + term->search.sz - term->search.len - 1, &ps); + + term->search.len += wchars; + term->search.buf[term->search.len] = L'\0'; + } + + LOG_DBG("search: buffer: %S", term->search.buf); + render_refresh(term); +} -- cgit v1.3 From aee5045395ca2393b60d962e6e9440a59edbfd6e Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 27 Aug 2019 19:33:19 +0200 Subject: search: wip: initial search matching * match search buffer against scrollback content * adjust view to ensure matched content is visible * create selection on a successful match * finalize selection when user presses enter (to "commit" the search) * ctrl+r searches for the next match. Needs more work though. --- grid.h | 16 +++++- search.c | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- terminal.h | 5 ++ 3 files changed, 187 insertions(+), 3 deletions(-) (limited to 'search.c') diff --git a/grid.h b/grid.h index a421a5c0..96ef4181 100644 --- a/grid.h +++ b/grid.h @@ -7,12 +7,24 @@ void grid_swap_row(struct grid *grid, int row_a, int row_b, bool initialize); struct row *grid_row_alloc(int cols, bool initialize); void grid_row_free(struct row *row); +static inline int +grid_row_absolute(const struct grid *grid, int row_no) +{ + return (grid->offset + row_no) & (grid->num_rows - 1); +} + +static inline int +grid_row_absolute_in_view(const struct grid *grid, int row_no) +{ + return (grid->view + row_no) & (grid->num_rows - 1); +} + static inline struct row * _grid_row_maybe_alloc(struct grid *grid, int row_no, bool alloc_if_null) { assert(grid->offset >= 0); - int real_row = (grid->offset + row_no) & (grid->num_rows - 1); + int real_row = grid_row_absolute(grid, row_no); struct row *row = grid->rows[real_row]; if (row == NULL && alloc_if_null) { @@ -41,7 +53,7 @@ grid_row_in_view(struct grid *grid, int row_no) { assert(grid->view >= 0); - int real_row = (grid->view + row_no) & (grid->num_rows - 1); + int real_row = grid_row_absolute_in_view(grid, row_no); struct row *row = grid->rows[real_row]; assert(row != NULL); diff --git a/search.c b/search.c index 55c67bb0..cdc7ccf5 100644 --- a/search.c +++ b/search.c @@ -7,6 +7,8 @@ #define LOG_ENABLE_DBG 1 #include "log.h" #include "render.h" +#include "selection.h" +#include "grid.h" void search_begin(struct terminal *term) @@ -17,6 +19,10 @@ search_begin(struct terminal *term) term->search.buf = NULL; term->search.len = 0; term->search.sz = 0; + term->search.match = (struct coord){-1, -1}; + term->search.match_len = 0; + term->search.original_view = term->grid->view; + term->search.view_followed_offset = term->grid->view == term->grid->offset; term->is_searching = true; render_refresh(term); @@ -36,6 +42,133 @@ search_cancel(struct terminal *term) render_refresh(term); } +static void +search_update(struct terminal *term) +{ + if (term->search.len == 0) { + term->search.match = (struct coord){-1, -1}; + term->search.match_len = 0; + selection_cancel(term); + return; + } + + int start_row = term->search.match.row; + int start_col = term->search.match.col; + size_t len = term->search.match_len; + + assert((len == 0 && start_row == -1 && start_col == -1) || + (len > 0 && start_row >= 0 && start_col >= 0)); + + if (len == 0) { + start_row = grid_row_absolute(term->grid, term->rows - 1); + start_col = term->cols - 1; + } + + LOG_DBG("search: update: starting at row=%d col=%d", start_row, start_col); + + /* Scan backward from current end-of-output */ + for (; start_row >= 0; start_row--) { + const struct row *row = term->grid->rows[start_row]; + if (row == NULL) + break; + + for (; start_col >= 0; start_col--) { + if (row->cells[start_col].wc != term->search.buf[0]) + continue; + + /* + * Got a match on the first letter. Now we'll see if the + * rest of the search buffer matches. + */ + + LOG_DBG("search: initial match at row=%d, col=%d", start_row, start_col); + + int end_row = start_row; + int end_col = start_col; + size_t match_len = 0; + + for (size_t i = 0; i < term->search.len; i++, match_len++) { + if (row->cells[end_col].wc != term->search.buf[i]) + break; + + if (++end_col >= term->cols) { + if (end_row + 1 > grid_row_absolute(term->grid, term->grid->offset + term->rows - 1)) { + /* Don't continue past end of the world */ + break; + } + + end_row++; + end_col = 0; + row = term->grid->rows[end_row]; + } + } + + if (match_len != term->search.len) { + /* Didn't match (completely) */ + continue; + } + + /* + * We matched the entire buffer. Move view to ensure the + * match is visible, create a selection and return. + */ + + int old_view = term->grid->view; + int new_view = start_row; + + /* Prevent scrolling in uninitialized rows */ + bool all_initialized = false; + do { + all_initialized = true; + + for (int i = 0; i < term->rows; i++) { + int row_no = (new_view + i) % term->grid->num_rows; + if (term->grid->rows[row_no] == NULL) { + all_initialized = false; + new_view--; + break; + } + } + } while (!all_initialized); + + /* Don't scroll past scrollback history */ + int end = (term->grid->offset + term->rows - 1) % term->grid->num_rows; + if (end >= term->grid->offset) { + /* Not wrapped */ + if (new_view >= term->grid->offset && new_view <= end) + new_view = term->grid->offset; + } else { + if (new_view >= term->grid->offset || new_view <= end) + new_view = term->grid->offset; + } + + /* Update view */ + term->grid->view = new_view; + if (new_view != old_view) + term_damage_view(term); + + /* Selection endpoint is inclusive */ + if (--end_col < 0) { + end_col = term->cols - 1; + start_row--; + } + + /* Begin a selection (it's finalized when "committing" the search) */ + selection_start(term, start_col, start_row - term->grid->view); + selection_update(term, end_col, end_row - term->grid->view); + + /* Update match state */ + term->search.match.row = start_row; + term->search.match.col = start_col; + term->search.match_len = match_len; + + return; + } + + start_col = term->cols - 1; + } +} + void search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask_t mods) { @@ -49,8 +182,41 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask enum xkb_compose_status compose_status = xkb_compose_state_get_status( term->kbd.xkb_compose_state); - if ((mods == 0 && sym == XKB_KEY_Escape) || (mods == ctrl && sym == XKB_KEY_g)) + /* Cancel search */ + if ((mods == 0 && sym == XKB_KEY_Escape) || + (mods == ctrl && sym == XKB_KEY_g)) + { + if (term->search.view_followed_offset) + term->grid->view = term->grid->offset; + else + term->grid->view = term->search.original_view; + term_damage_view(term); search_cancel(term); + } + + /* "Commit" search - copy selection to primary and cancel search */ + else if (mods == 0 && sym == XKB_KEY_Return) { + selection_finalize(term, term->input_serial); + search_cancel(term); + } + + else if (mods == ctrl && sym == XKB_KEY_r) { + if (term->search.match_len > 0) { + int new_col = term->search.match.col - 1; + int new_row = term->search.match.row; + + if (new_col < 0) { + new_col = term->cols - 1; + new_row--; + } + + if (new_row >= 0) { + term->search.match.col = new_col; + term->search.match.row = new_row; + search_update(term); + } + } + } else if (mods == 0 && sym == XKB_KEY_BackSpace) { if (term->search.len > 0) @@ -103,5 +269,6 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask } LOG_DBG("search: buffer: %S", term->search.buf); + search_update(term); render_refresh(term); } diff --git a/terminal.h b/terminal.h index fbd734bc..5989788a 100644 --- a/terminal.h +++ b/terminal.h @@ -336,6 +336,11 @@ struct terminal { wchar_t *buf; size_t len; size_t sz; + + int original_view; + bool view_followed_offset; + struct coord match; + size_t match_len; } search; struct grid normal; -- cgit v1.3 From d518d9dafb218c308bd8e2be22b6c6bad6a4ac44 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 27 Aug 2019 19:55:41 +0200 Subject: search: don't create a new selection when match start coords haven't changed --- search.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index cdc7ccf5..03e114c0 100644 --- a/search.c +++ b/search.c @@ -153,8 +153,14 @@ search_update(struct terminal *term) start_row--; } - /* Begin a selection (it's finalized when "committing" the search) */ - selection_start(term, start_col, start_row - term->grid->view); + /* Begin a new selection if the start coords changed */ + if (start_row != term->search.match.row || + start_col != term->search.match.col) + { + selection_start(term, start_col, start_row - term->grid->view); + } + + /* Update selection endpoint */ selection_update(term, end_col, end_row - term->grid->view); /* Update match state */ -- cgit v1.3 From d1974913f782ba4a5993313646b3a3fdb2eb40c6 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 27 Aug 2019 19:56:02 +0200 Subject: search: search_update() is always called at end of search_input() --- search.c | 1 - 1 file changed, 1 deletion(-) (limited to 'search.c') diff --git a/search.c b/search.c index 03e114c0..30edad2e 100644 --- a/search.c +++ b/search.c @@ -219,7 +219,6 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask if (new_row >= 0) { term->search.match.col = new_col; term->search.match.row = new_row; - search_update(term); } } } -- cgit v1.3 From c1bbb64a4d87f8fb4d5884cccb49e47af462b45a Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 27 Aug 2019 19:56:16 +0200 Subject: search: return early after committing a search This fixes an issue where the selection was finalized, but then cleared. --- search.c | 1 + 1 file changed, 1 insertion(+) (limited to 'search.c') diff --git a/search.c b/search.c index 30edad2e..48b45b9a 100644 --- a/search.c +++ b/search.c @@ -204,6 +204,7 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask else if (mods == 0 && sym == XKB_KEY_Return) { selection_finalize(term, term->input_serial); search_cancel(term); + return; } else if (mods == ctrl && sym == XKB_KEY_r) { -- cgit v1.3 From c3b5fa82bec9e84db9028df65e47793b21e11851 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 27 Aug 2019 19:58:31 +0200 Subject: search: 'len' is unused in release builds (it's only used in an assert) --- search.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'search.c') diff --git a/search.c b/search.c index 48b45b9a..37ab5684 100644 --- a/search.c +++ b/search.c @@ -54,7 +54,7 @@ search_update(struct terminal *term) int start_row = term->search.match.row; int start_col = term->search.match.col; - size_t len = term->search.match_len; + size_t len __attribute__((unused)) = term->search.match_len; assert((len == 0 && start_row == -1 && start_col == -1) || (len > 0 && start_row >= 0 && start_col >= 0)); -- cgit v1.3 From 2895f8fa107e9034f4d9b0731b1eddfd0cc9347b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 27 Aug 2019 19:58:44 +0200 Subject: search: cancel selection and clear match stats when we don't match --- search.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'search.c') diff --git a/search.c b/search.c index 37ab5684..992de851 100644 --- a/search.c +++ b/search.c @@ -173,6 +173,11 @@ search_update(struct terminal *term) start_col = term->cols - 1; } + + /* No match */ + term->search.match = (struct coord){-1, -1}; + term->search.match_len = 0; + selection_cancel(term); } void -- cgit v1.3 From 275915228584e665f71266da30084c9ef5d67b14 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 27 Aug 2019 21:09:37 +0200 Subject: search: loop through the *entire* scrollback buffer --- search.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index 992de851..a44ca7ec 100644 --- a/search.c +++ b/search.c @@ -66,11 +66,13 @@ search_update(struct terminal *term) LOG_DBG("search: update: starting at row=%d col=%d", start_row, start_col); +#define ROW_DEC(_r) ((_r) = ((_r) - 1 + term->grid->num_rows) % term->grid->num_rows) + /* Scan backward from current end-of-output */ - for (; start_row >= 0; start_row--) { + for (size_t r = 0; r < term->grid->num_rows; ROW_DEC(start_row), r++) { const struct row *row = term->grid->rows[start_row]; if (row == NULL) - break; + continue; for (; start_col >= 0; start_col--) { if (row->cells[start_col].wc != term->search.buf[0]) @@ -175,9 +177,12 @@ search_update(struct terminal *term) } /* No match */ + LOG_DBG("no match"); term->search.match = (struct coord){-1, -1}; term->search.match_len = 0; selection_cancel(term); + +#undef ROW_DEC } void -- cgit v1.3 From 36350b01a5a51bd105b3852047b6889cc2a91e2a Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 27 Aug 2019 21:11:29 +0200 Subject: search: disable log output --- search.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'search.c') diff --git a/search.c b/search.c index a44ca7ec..849a5c8a 100644 --- a/search.c +++ b/search.c @@ -4,7 +4,7 @@ #include #define LOG_MODULE "search" -#define LOG_ENABLE_DBG 1 +#define LOG_ENABLE_DBG 0 #include "log.h" #include "render.h" #include "selection.h" -- cgit v1.3 From bd5e5798674ebfdbe7fe198fee493505b85c0b2a Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 27 Aug 2019 21:11:40 +0200 Subject: search: sort includes --- search.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'search.c') diff --git a/search.c b/search.c index 849a5c8a..17ac0cb8 100644 --- a/search.c +++ b/search.c @@ -6,9 +6,9 @@ #define LOG_MODULE "search" #define LOG_ENABLE_DBG 0 #include "log.h" +#include "grid.h" #include "render.h" #include "selection.h" -#include "grid.h" void search_begin(struct terminal *term) -- cgit v1.3 From 064063fa4918fca4853d4c267f6316960bdb8e19 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Tue, 27 Aug 2019 21:16:57 +0200 Subject: search: match case insensitive --- search.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index 17ac0cb8..d0740b02 100644 --- a/search.c +++ b/search.c @@ -75,7 +75,7 @@ search_update(struct terminal *term) continue; for (; start_col >= 0; start_col--) { - if (row->cells[start_col].wc != term->search.buf[0]) + if (wcsncasecmp(&row->cells[start_col].wc, term->search.buf, 1) != 0) continue; /* @@ -90,7 +90,7 @@ search_update(struct terminal *term) size_t match_len = 0; for (size_t i = 0; i < term->search.len; i++, match_len++) { - if (row->cells[end_col].wc != term->search.buf[i]) + if (wcsncasecmp(&row->cells[end_col].wc, &term->search.buf[i], 1) != 0) break; if (++end_col >= term->cols) { -- cgit v1.3 From 64179bce4669bb1da753bd9de1f4bf3aeb1b6141 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 28 Aug 2019 17:26:30 +0200 Subject: search: start search in current view, rather than from end-of-output --- search.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index d0740b02..903645e5 100644 --- a/search.c +++ b/search.c @@ -60,15 +60,17 @@ search_update(struct terminal *term) (len > 0 && start_row >= 0 && start_col >= 0)); if (len == 0) { - start_row = grid_row_absolute(term->grid, term->rows - 1); + start_row = grid_row_absolute_in_view(term->grid, term->rows - 1); start_col = term->cols - 1; } - LOG_DBG("search: update: starting at row=%d col=%d", start_row, start_col); + LOG_DBG("search: update: starting at row=%d col=%d (offset = %d, view = %d)", + start_row, start_col, term->grid->offset, term->grid->view); #define ROW_DEC(_r) ((_r) = ((_r) - 1 + term->grid->num_rows) % term->grid->num_rows) /* Scan backward from current end-of-output */ + /* TODO: don't search "scrollback" in alt screen? */ for (size_t r = 0; r < term->grid->num_rows; ROW_DEC(start_row), r++) { const struct row *row = term->grid->rows[start_row]; if (row == NULL) -- cgit v1.3 From b8b43e2eabfd8647a9fc41d28cf7ea0003c03b2c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 28 Aug 2019 17:26:44 +0200 Subject: search: fix start/end row in selection --- search.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index 903645e5..82902e6c 100644 --- a/search.c +++ b/search.c @@ -161,11 +161,25 @@ search_update(struct terminal *term) if (start_row != term->search.match.row || start_col != term->search.match.col) { - selection_start(term, start_col, start_row - term->grid->view); + int selection_row = start_row - term->grid->view; + while (selection_row < 0) + selection_row += term->grid->num_rows; + + assert(selection_row >= 0 && + selection_row < term->grid->num_rows); + selection_start(term, start_col, selection_row); } /* Update selection endpoint */ - selection_update(term, end_col, end_row - term->grid->view); + { + int selection_row = end_row - term->grid->view; + while (selection_row < 0) + selection_row += term->grid->num_rows; + + assert(selection_row >= 0 && + selection_row < term->grid->num_rows); + selection_update(term, end_col, selection_row); + } /* Update match state */ term->search.match.row = start_row; -- cgit v1.3 From 0d0ee8f378e43bf761ea52911c5afe6fdf7c9aa8 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 28 Aug 2019 21:03:30 +0200 Subject: search: log search buffer at INFO level ... until we actually render the search buffer somehow --- search.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'search.c') diff --git a/search.c b/search.c index 82902e6c..46bbb153 100644 --- a/search.c +++ b/search.c @@ -300,7 +300,7 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask term->search.buf[term->search.len] = L'\0'; } - LOG_DBG("search: buffer: %S", term->search.buf); + LOG_INFO("search: buffer: %S", term->search.buf); search_update(term); render_refresh(term); } -- cgit v1.3 From 94b4c916eebf71d399f8a2cbe820eee5d79f5514 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Wed, 28 Aug 2019 21:54:02 +0200 Subject: search: cancel existing selection when starting a new search --- search.c | 1 + 1 file changed, 1 insertion(+) (limited to 'search.c') diff --git a/search.c b/search.c index 46bbb153..6aa6be5c 100644 --- a/search.c +++ b/search.c @@ -25,6 +25,7 @@ search_begin(struct terminal *term) term->search.view_followed_offset = term->grid->view == term->grid->offset; term->is_searching = true; + selection_cancel(term); render_refresh(term); } -- cgit v1.3 From 66912cbfb5e44d44670582fad932932f9de6794b Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 19:34:41 +0200 Subject: search: use a sub-surface to render the search buffer --- main.c | 5 +++ search.c | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ terminal.h | 1 + 3 files changed, 141 insertions(+), 14 deletions(-) (limited to 'search.c') diff --git a/main.c b/main.c index 60d041f1..b78305f7 100644 --- a/main.c +++ b/main.c @@ -197,6 +197,11 @@ handle_global(void *data, struct wl_registry *registry, term->wl.registry, name, &wl_compositor_interface, 4); } + else if (strcmp(interface, wl_subcompositor_interface.name) == 0) { + term->wl.sub_compositor = wl_registry_bind( + term->wl.registry, name, &wl_subcompositor_interface, 1); + } + else if (strcmp(interface, wl_shm_interface.name) == 0) { term->wl.shm = wl_registry_bind( term->wl.registry, name, &wl_shm_interface, 1); diff --git a/search.c b/search.c index 6aa6be5c..558f98c7 100644 --- a/search.c +++ b/search.c @@ -1,6 +1,8 @@ #include "search.h" #include + +#include #include #define LOG_MODULE "search" @@ -9,11 +11,98 @@ #include "grid.h" #include "render.h" #include "selection.h" +#include "shm.h" -void -search_begin(struct terminal *term) +#define max(x, y) ((x) > (y) ? (x) : (y)) + +static struct wl_surface *wl_surf; +static struct wl_subsurface *sub_surf; + + +static inline pixman_color_t +color_hex_to_pixman_with_alpha(uint32_t color, uint16_t alpha) { - LOG_DBG("search: begin"); + int alpha_div = 0xffff / alpha; + return (pixman_color_t){ + .red = ((color >> 16 & 0xff) | (color >> 8 & 0xff00)) / alpha_div, + .green = ((color >> 8 & 0xff) | (color >> 0 & 0xff00)) / alpha_div, + .blue = ((color >> 0 & 0xff) | (color << 8 & 0xff00)) / alpha_div, + .alpha = alpha, + }; +} + +static inline pixman_color_t +color_hex_to_pixman(uint32_t color) +{ + /* Count on the compiler optimizing this */ + return color_hex_to_pixman_with_alpha(color, 0xffff); +} + +/* TODO: move to render.c? */ +static void +render(struct terminal *term) +{ + assert(sub_surf != NULL); + + /* TODO: at least sway allows the subsurface to extend outside the + * main window. Do we want that? */ + const int scale = term->scale >= 1 ? term->scale : 1; + const int margin = scale * 3; + const int width = 2 * margin + max(20, term->search.len) * term->cell_width; + const int height = 2 * margin + 1 * term->cell_height; + + struct buffer *buf = shm_get_buffer(term->wl.shm, width, height, 1); + + /* Background - yellow on empty/match, red on mismatch */ + pixman_color_t color = color_hex_to_pixman( + term->search.match_len == term->search.len ? 0xffff00 : 0xff0000); + + pixman_image_fill_rectangles( + PIXMAN_OP_SRC, buf->pix, &color, + 1, &(pixman_rectangle16_t){0, 0, width, height}); + + int x = margin; + int y = margin; + pixman_color_t fg = color_hex_to_pixman(0x000000); + + /* Text (what the user entered - *not* match(es)) */ + for (size_t i = 0; i < term->search.len; i++) { + const struct glyph *glyph = font_glyph_for_wc(&term->fonts[0], term->search.buf[i]); + if (glyph == NULL) + continue; + + pixman_image_t *src = pixman_image_create_solid_fill(&fg); + pixman_image_composite32( + PIXMAN_OP_OVER, src, glyph->pix, buf->pix, 0, 0, 0, 0, + x + glyph->x, y + term->fextents.ascent - glyph->y, + glyph->width, glyph->height); + pixman_image_unref(src); + + x += glyph->width; + } + + LOG_INFO("match length: %zu", term->search.match_len); + + wl_subsurface_set_position( + sub_surf, term->width - width - margin, term->height - height - margin); + + wl_surface_damage_buffer(wl_surf, 0, 0, width, height); + wl_surface_attach(wl_surf, buf->wl_buf, 0, 0); + wl_surface_set_buffer_scale(wl_surf, scale); + wl_surface_commit(wl_surf); +} + +static void +search_cancel_keep_selection(struct terminal *term) +{ + if (sub_surf != NULL) { + wl_subsurface_destroy(sub_surf); + sub_surf = NULL; + } + if (wl_surf != NULL) { + wl_surface_destroy(wl_surf); + wl_surf = NULL; + } free(term->search.buf); term->search.buf = NULL; @@ -21,35 +110,65 @@ search_begin(struct terminal *term) term->search.sz = 0; term->search.match = (struct coord){-1, -1}; term->search.match_len = 0; + term->is_searching = false; + + render_refresh(term); +} + +void +search_begin(struct terminal *term) +{ + LOG_DBG("search: begin"); + + search_cancel_keep_selection(term); + selection_cancel(term); + term->search.original_view = term->grid->view; term->search.view_followed_offset = term->grid->view == term->grid->offset; term->is_searching = true; - selection_cancel(term); + wl_surf = wl_compositor_create_surface(term->wl.compositor); + if (wl_surf != NULL) { + sub_surf = wl_subcompositor_get_subsurface( + term->wl.sub_compositor, wl_surf, term->wl.surface); + + if (sub_surf != NULL) { + /* Sub-surface updates may occur without updating the main + * window */ + wl_subsurface_set_desync(sub_surf); + } + } + + if (wl_surf == NULL || sub_surf == NULL) { + LOG_ERR("failed to create sub-surface for search box"); + if (wl_surf != NULL) + wl_surface_destroy(wl_surf); + assert(sub_surf == NULL); + } + + render(term); render_refresh(term); } void search_cancel(struct terminal *term) { - LOG_DBG("search: cancel"); - - free(term->search.buf); - term->search.buf = NULL; - term->search.len = 0; - term->search.sz = 0; - term->is_searching = false; + if (!term->is_searching) + return; - render_refresh(term); + search_cancel_keep_selection(term); + selection_cancel(term); } static void search_update(struct terminal *term) { if (term->search.len == 0) { + LOG_INFO("len == 0"); term->search.match = (struct coord){-1, -1}; term->search.match_len = 0; selection_cancel(term); + render(term); return; } @@ -187,6 +306,7 @@ search_update(struct terminal *term) term->search.match.col = start_col; term->search.match_len = match_len; + render(term); return; } @@ -198,7 +318,7 @@ search_update(struct terminal *term) term->search.match = (struct coord){-1, -1}; term->search.match_len = 0; selection_cancel(term); - + render(term); #undef ROW_DEC } @@ -225,12 +345,13 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask term->grid->view = term->search.original_view; term_damage_view(term); search_cancel(term); + return; } /* "Commit" search - copy selection to primary and cancel search */ else if (mods == 0 && sym == XKB_KEY_Return) { selection_finalize(term, term->input_serial); - search_cancel(term); + search_cancel_keep_selection(term); return; } diff --git a/terminal.h b/terminal.h index 18130392..755c7b4a 100644 --- a/terminal.h +++ b/terminal.h @@ -41,6 +41,7 @@ struct wayland { struct wl_display *display; struct wl_registry *registry; struct wl_compositor *compositor; + struct wl_subcompositor *sub_compositor; struct wl_surface *surface; struct wl_shm *shm; struct wl_seat *seat; -- cgit v1.3 From 44a353a7f74b562cb057c23760013a12becbecf3 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 20:15:33 +0200 Subject: search: move wayland surface variables into the term struct --- main.c | 20 ++++++++++++-------- search.c | 47 +++++++++++++++++++++++------------------------ terminal.h | 16 ++++++++++++++-- 3 files changed, 49 insertions(+), 34 deletions(-) (limited to 'search.c') diff --git a/main.c b/main.c index b78305f7..e92996ef 100644 --- a/main.c +++ b/main.c @@ -1019,12 +1019,6 @@ out: if (term.wl.xdg_output_manager != NULL) zxdg_output_manager_v1_destroy(term.wl.xdg_output_manager); - if (term.render.frame_callback != NULL) - wl_callback_destroy(term.render.frame_callback); - if (term.wl.xdg_toplevel != NULL) - xdg_toplevel_destroy(term.wl.xdg_toplevel); - if (term.wl.xdg_surface != NULL) - xdg_surface_destroy(term.wl.xdg_surface); free(term.wl.pointer.theme_name); if (term.wl.pointer.theme != NULL) wl_cursor_theme_destroy(term.wl.pointer.theme); @@ -1054,10 +1048,20 @@ out: zwp_primary_selection_device_manager_v1_destroy(term.wl.primary_selection_device_manager); if (term.wl.seat != NULL) wl_seat_destroy(term.wl.seat); - if (term.wl.surface != NULL) - wl_surface_destroy(term.wl.surface); + if (term.wl.search_sub_surface != NULL) + wl_subsurface_destroy(term.wl.search_sub_surface); + if (term.wl.search_surface != NULL) + wl_surface_destroy(term.wl.search_surface); + if (term.render.frame_callback != NULL) + wl_callback_destroy(term.render.frame_callback); + if (term.wl.xdg_toplevel != NULL) + xdg_toplevel_destroy(term.wl.xdg_toplevel); + if (term.wl.xdg_surface != NULL) + xdg_surface_destroy(term.wl.xdg_surface); if (term.wl.shell != NULL) xdg_wm_base_destroy(term.wl.shell); + if (term.wl.surface != NULL) + wl_surface_destroy(term.wl.surface); if (term.wl.shm != NULL) wl_shm_destroy(term.wl.shm); if (term.wl.compositor != NULL) diff --git a/search.c b/search.c index 558f98c7..150bd050 100644 --- a/search.c +++ b/search.c @@ -15,8 +15,6 @@ #define max(x, y) ((x) > (y) ? (x) : (y)) -static struct wl_surface *wl_surf; -static struct wl_subsurface *sub_surf; static inline pixman_color_t @@ -42,7 +40,7 @@ color_hex_to_pixman(uint32_t color) static void render(struct terminal *term) { - assert(sub_surf != NULL); + assert(term->wl.search_sub_surface != NULL); /* TODO: at least sway allows the subsurface to extend outside the * main window. Do we want that? */ @@ -84,24 +82,25 @@ render(struct terminal *term) LOG_INFO("match length: %zu", term->search.match_len); wl_subsurface_set_position( - sub_surf, term->width - width - margin, term->height - height - margin); + term->wl.search_sub_surface, + term->width - width - margin, term->height - height - margin); - wl_surface_damage_buffer(wl_surf, 0, 0, width, height); - wl_surface_attach(wl_surf, buf->wl_buf, 0, 0); - wl_surface_set_buffer_scale(wl_surf, scale); - wl_surface_commit(wl_surf); + wl_surface_damage_buffer(term->wl.search_surface, 0, 0, width, height); + wl_surface_attach(term->wl.search_surface, buf->wl_buf, 0, 0); + wl_surface_set_buffer_scale(term->wl.search_surface, scale); + wl_surface_commit(term->wl.search_surface); } static void search_cancel_keep_selection(struct terminal *term) { - if (sub_surf != NULL) { - wl_subsurface_destroy(sub_surf); - sub_surf = NULL; + if (term->wl.search_sub_surface != NULL) { + wl_subsurface_destroy(term->wl.search_sub_surface); + term->wl.search_sub_surface = NULL; } - if (wl_surf != NULL) { - wl_surface_destroy(wl_surf); - wl_surf = NULL; + if (term->wl.search_surface != NULL) { + wl_surface_destroy(term->wl.search_surface); + term->wl.search_surface = NULL; } free(term->search.buf); @@ -127,23 +126,23 @@ search_begin(struct terminal *term) term->search.view_followed_offset = term->grid->view == term->grid->offset; term->is_searching = true; - wl_surf = wl_compositor_create_surface(term->wl.compositor); - if (wl_surf != NULL) { - sub_surf = wl_subcompositor_get_subsurface( - term->wl.sub_compositor, wl_surf, term->wl.surface); + term->wl.search_surface = wl_compositor_create_surface(term->wl.compositor); + if (term->wl.search_surface != NULL) { + term->wl.search_sub_surface = wl_subcompositor_get_subsurface( + term->wl.sub_compositor, term->wl.search_surface, term->wl.surface); - if (sub_surf != NULL) { + if (term->wl.search_sub_surface != NULL) { /* Sub-surface updates may occur without updating the main * window */ - wl_subsurface_set_desync(sub_surf); + wl_subsurface_set_desync(term->wl.search_sub_surface); } } - if (wl_surf == NULL || sub_surf == NULL) { + if (term->wl.search_surface == NULL || term->wl.search_sub_surface == NULL) { LOG_ERR("failed to create sub-surface for search box"); - if (wl_surf != NULL) - wl_surface_destroy(wl_surf); - assert(sub_surf == NULL); + if (term->wl.search_surface != NULL) + wl_surface_destroy(term->wl.search_surface); + assert(term->wl.search_sub_surface == NULL); } render(term); diff --git a/terminal.h b/terminal.h index 755c7b4a..948a8f11 100644 --- a/terminal.h +++ b/terminal.h @@ -42,15 +42,19 @@ struct wayland { struct wl_registry *registry; struct wl_compositor *compositor; struct wl_subcompositor *sub_compositor; - struct wl_surface *surface; struct wl_shm *shm; + struct wl_seat *seat; + struct wl_keyboard *keyboard; struct zxdg_output_manager_v1 *xdg_output_manager; + + /* Clipboard */ struct wl_data_device_manager *data_device_manager; struct wl_data_device *data_device; struct zwp_primary_selection_device_manager_v1 *primary_selection_device_manager; struct zwp_primary_selection_device_v1 *primary_selection_device; - struct wl_keyboard *keyboard; + + /* Cursor */ struct { struct wl_pointer *pointer; uint32_t serial; @@ -61,9 +65,17 @@ struct wayland { int size; char *theme_name; } pointer; + + /* Main window */ + struct wl_surface *surface; struct xdg_wm_base *shell; struct xdg_surface *xdg_surface; struct xdg_toplevel *xdg_toplevel; + + /* Scrollback search */ + struct wl_surface *search_surface; + struct wl_subsurface *search_sub_surface; + bool have_argb8888; tll(struct monitor) monitors; /* All available outputs */ tll(const struct monitor *) on_outputs; /* Outputs we're mapped on */ -- cgit v1.3 From 2c3ab701e7fc95f30b573b20873dc4dfb5c2fa66 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 20:18:06 +0200 Subject: search: move render() function to the 'render' module --- render.c | 54 +++++++++++++++++++++++++++++++++++++++++ render.h | 1 + search.c | 84 ++++------------------------------------------------------------ 3 files changed, 59 insertions(+), 80 deletions(-) (limited to 'search.c') diff --git a/render.c b/render.c index 8bdaeb0f..9c01b15f 100644 --- a/render.c +++ b/render.c @@ -691,6 +691,60 @@ frame_callback(void *data, struct wl_callback *wl_callback, uint32_t callback_da grid_render(term); } +void +render_search_box(struct terminal *term) +{ + assert(term->wl.search_sub_surface != NULL); + + /* TODO: at least sway allows the subsurface to extend outside the + * main window. Do we want that? */ + const int scale = term->scale >= 1 ? term->scale : 1; + const int margin = scale * 3; + const int width = 2 * margin + max(20, term->search.len) * term->cell_width; + const int height = 2 * margin + 1 * term->cell_height; + + struct buffer *buf = shm_get_buffer(term->wl.shm, width, height, 1); + + /* Background - yellow on empty/match, red on mismatch */ + pixman_color_t color = color_hex_to_pixman( + term->search.match_len == term->search.len ? 0xffff00 : 0xff0000); + + pixman_image_fill_rectangles( + PIXMAN_OP_SRC, buf->pix, &color, + 1, &(pixman_rectangle16_t){0, 0, width, height}); + + int x = margin; + int y = margin; + pixman_color_t fg = color_hex_to_pixman(0x000000); + + /* Text (what the user entered - *not* match(es)) */ + for (size_t i = 0; i < term->search.len; i++) { + const struct glyph *glyph = font_glyph_for_wc(&term->fonts[0], term->search.buf[i]); + if (glyph == NULL) + continue; + + pixman_image_t *src = pixman_image_create_solid_fill(&fg); + pixman_image_composite32( + PIXMAN_OP_OVER, src, glyph->pix, buf->pix, 0, 0, 0, 0, + x + glyph->x, y + term->fextents.ascent - glyph->y, + glyph->width, glyph->height); + pixman_image_unref(src); + + x += glyph->width; + } + + LOG_INFO("match length: %zu", term->search.match_len); + + wl_subsurface_set_position( + term->wl.search_sub_surface, + term->width - width - margin, term->height - height - margin); + + wl_surface_damage_buffer(term->wl.search_surface, 0, 0, width, height); + wl_surface_attach(term->wl.search_surface, buf->wl_buf, 0, 0); + wl_surface_set_buffer_scale(term->wl.search_surface, scale); + wl_surface_commit(term->wl.search_surface); +} + static void reflow(struct row **new_grid, int new_cols, int new_rows, struct row *const *old_grid, int old_cols, int old_rows) diff --git a/render.h b/render.h index a3620665..c2fa489e 100644 --- a/render.h +++ b/render.h @@ -10,6 +10,7 @@ void render_resize(struct terminal *term, int width, int height); void render_set_title(struct terminal *term, const char *title); void render_refresh(struct terminal *term); +void render_search_box(struct terminal *term); bool render_reload_cursor_theme(struct terminal *term); void render_update_cursor_surface(struct terminal *term); diff --git a/search.c b/search.c index 150bd050..81bdf8f3 100644 --- a/search.c +++ b/search.c @@ -15,82 +15,6 @@ #define max(x, y) ((x) > (y) ? (x) : (y)) - - -static inline pixman_color_t -color_hex_to_pixman_with_alpha(uint32_t color, uint16_t alpha) -{ - int alpha_div = 0xffff / alpha; - return (pixman_color_t){ - .red = ((color >> 16 & 0xff) | (color >> 8 & 0xff00)) / alpha_div, - .green = ((color >> 8 & 0xff) | (color >> 0 & 0xff00)) / alpha_div, - .blue = ((color >> 0 & 0xff) | (color << 8 & 0xff00)) / alpha_div, - .alpha = alpha, - }; -} - -static inline pixman_color_t -color_hex_to_pixman(uint32_t color) -{ - /* Count on the compiler optimizing this */ - return color_hex_to_pixman_with_alpha(color, 0xffff); -} - -/* TODO: move to render.c? */ -static void -render(struct terminal *term) -{ - assert(term->wl.search_sub_surface != NULL); - - /* TODO: at least sway allows the subsurface to extend outside the - * main window. Do we want that? */ - const int scale = term->scale >= 1 ? term->scale : 1; - const int margin = scale * 3; - const int width = 2 * margin + max(20, term->search.len) * term->cell_width; - const int height = 2 * margin + 1 * term->cell_height; - - struct buffer *buf = shm_get_buffer(term->wl.shm, width, height, 1); - - /* Background - yellow on empty/match, red on mismatch */ - pixman_color_t color = color_hex_to_pixman( - term->search.match_len == term->search.len ? 0xffff00 : 0xff0000); - - pixman_image_fill_rectangles( - PIXMAN_OP_SRC, buf->pix, &color, - 1, &(pixman_rectangle16_t){0, 0, width, height}); - - int x = margin; - int y = margin; - pixman_color_t fg = color_hex_to_pixman(0x000000); - - /* Text (what the user entered - *not* match(es)) */ - for (size_t i = 0; i < term->search.len; i++) { - const struct glyph *glyph = font_glyph_for_wc(&term->fonts[0], term->search.buf[i]); - if (glyph == NULL) - continue; - - pixman_image_t *src = pixman_image_create_solid_fill(&fg); - pixman_image_composite32( - PIXMAN_OP_OVER, src, glyph->pix, buf->pix, 0, 0, 0, 0, - x + glyph->x, y + term->fextents.ascent - glyph->y, - glyph->width, glyph->height); - pixman_image_unref(src); - - x += glyph->width; - } - - LOG_INFO("match length: %zu", term->search.match_len); - - wl_subsurface_set_position( - term->wl.search_sub_surface, - term->width - width - margin, term->height - height - margin); - - wl_surface_damage_buffer(term->wl.search_surface, 0, 0, width, height); - wl_surface_attach(term->wl.search_surface, buf->wl_buf, 0, 0); - wl_surface_set_buffer_scale(term->wl.search_surface, scale); - wl_surface_commit(term->wl.search_surface); -} - static void search_cancel_keep_selection(struct terminal *term) { @@ -145,7 +69,7 @@ search_begin(struct terminal *term) assert(term->wl.search_sub_surface == NULL); } - render(term); + render_search_box(term); render_refresh(term); } @@ -167,7 +91,7 @@ search_update(struct terminal *term) term->search.match = (struct coord){-1, -1}; term->search.match_len = 0; selection_cancel(term); - render(term); + render_search_box(term); return; } @@ -305,7 +229,7 @@ search_update(struct terminal *term) term->search.match.col = start_col; term->search.match_len = match_len; - render(term); + render_search_box(term); return; } @@ -317,7 +241,7 @@ search_update(struct terminal *term) term->search.match = (struct coord){-1, -1}; term->search.match_len = 0; selection_cancel(term); - render(term); + render_search_box(term); #undef ROW_DEC } -- cgit v1.3 From 1e4b11b0fa73d4fa9e4f124fa04173f92f645d1f Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 20:22:07 +0200 Subject: search: don't destroy/recreate search (sub)surface Create the sub-surface once, at startup. Then, instead of destroying it when committing/cancelling a search, unmap it (by attaching a NULL buffer to it). --- main.c | 7 +++++++ search.c | 29 ++--------------------------- 2 files changed, 9 insertions(+), 27 deletions(-) (limited to 'search.c') diff --git a/main.c b/main.c index 18e2a19a..a076fb24 100644 --- a/main.c +++ b/main.c @@ -723,6 +723,7 @@ main(int argc, char *const *argv) goto out; } + /* Main window */ term.wl.surface = wl_compositor_create_surface(term.wl.compositor); if (term.wl.surface == NULL) { LOG_ERR("failed to create wayland surface"); @@ -740,6 +741,12 @@ main(int argc, char *const *argv) xdg_toplevel_set_app_id(term.wl.xdg_toplevel, "foot"); term_set_window_title(&term, "foot"); + /* Scrollback search box */ + term.wl.search_surface = wl_compositor_create_surface(term.wl.compositor); + term.wl.search_sub_surface = wl_subcompositor_get_subsurface( + term.wl.sub_compositor, term.wl.search_surface, term.wl.surface); + wl_subsurface_set_desync(term.wl.search_sub_surface); + wl_surface_commit(term.wl.surface); wl_display_roundtrip(term.wl.display); diff --git a/search.c b/search.c index 81bdf8f3..051a32db 100644 --- a/search.c +++ b/search.c @@ -18,14 +18,8 @@ static void search_cancel_keep_selection(struct terminal *term) { - if (term->wl.search_sub_surface != NULL) { - wl_subsurface_destroy(term->wl.search_sub_surface); - term->wl.search_sub_surface = NULL; - } - if (term->wl.search_surface != NULL) { - wl_surface_destroy(term->wl.search_surface); - term->wl.search_surface = NULL; - } + wl_surface_attach(term->wl.search_surface, NULL, 0, 0); + wl_surface_commit(term->wl.search_surface); free(term->search.buf); term->search.buf = NULL; @@ -50,25 +44,6 @@ search_begin(struct terminal *term) term->search.view_followed_offset = term->grid->view == term->grid->offset; term->is_searching = true; - term->wl.search_surface = wl_compositor_create_surface(term->wl.compositor); - if (term->wl.search_surface != NULL) { - term->wl.search_sub_surface = wl_subcompositor_get_subsurface( - term->wl.sub_compositor, term->wl.search_surface, term->wl.surface); - - if (term->wl.search_sub_surface != NULL) { - /* Sub-surface updates may occur without updating the main - * window */ - wl_subsurface_set_desync(term->wl.search_sub_surface); - } - } - - if (term->wl.search_surface == NULL || term->wl.search_sub_surface == NULL) { - LOG_ERR("failed to create sub-surface for search box"); - if (term->wl.search_surface != NULL) - wl_surface_destroy(term->wl.search_surface); - assert(term->wl.search_sub_surface == NULL); - } - render_search_box(term); render_refresh(term); } -- cgit v1.3 From 7d920a6d1b968a72f4be9fb89ce4c101e6ba1b64 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 20:23:55 +0200 Subject: search: remove INFO log (that really was a debug log) --- search.c | 1 - 1 file changed, 1 deletion(-) (limited to 'search.c') diff --git a/search.c b/search.c index 051a32db..f43581f7 100644 --- a/search.c +++ b/search.c @@ -62,7 +62,6 @@ static void search_update(struct terminal *term) { if (term->search.len == 0) { - LOG_INFO("len == 0"); term->search.match = (struct coord){-1, -1}; term->search.match_len = 0; selection_cancel(term); -- cgit v1.3 From 6d5f2004294a25307390adaf8182a04a3aa8025a Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 20:23:59 +0200 Subject: search: turn info log to a debug log --- search.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'search.c') diff --git a/search.c b/search.c index f43581f7..3d7f8b0a 100644 --- a/search.c +++ b/search.c @@ -319,7 +319,7 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask term->search.buf[term->search.len] = L'\0'; } - LOG_INFO("search: buffer: %S", term->search.buf); + LOG_DBG("search: buffer: %S", term->search.buf); search_update(term); render_refresh(term); } -- cgit v1.3 From 6003b878138315013458afbc4f3675e7f542eb55 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 21:03:16 +0200 Subject: search: search_cancel: reset cursor position --- search.c | 1 + 1 file changed, 1 insertion(+) (limited to 'search.c') diff --git a/search.c b/search.c index 3d7f8b0a..ffa50c0e 100644 --- a/search.c +++ b/search.c @@ -25,6 +25,7 @@ search_cancel_keep_selection(struct terminal *term) term->search.buf = NULL; term->search.len = 0; term->search.sz = 0; + term->search.cursor = 0; term->search.match = (struct coord){-1, -1}; term->search.match_len = 0; term->is_searching = false; -- cgit v1.3 From 10649178f8f42d38e4e9d9760eeda4d11dfdadda Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 21:03:46 +0200 Subject: search: insert new characters at cursor, not at the end of the buffer --- search.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index ffa50c0e..6326aded 100644 --- a/search.c +++ b/search.c @@ -312,11 +312,16 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask assert(term->search.len + wchars < term->search.sz); + memmove(&term->search.buf[term->search.cursor + wchars], + &term->search.buf[term->search.cursor], + (term->search.len - term->search.cursor) * sizeof(wchar_t)); + memset(&ps, 0, sizeof(ps)); - mbsnrtowcs(&term->search.buf[term->search.len], &src, count, - term->search.sz - term->search.len - 1, &ps); + mbsnrtowcs(&term->search.buf[term->search.cursor], &src, count, + wchars, &ps); term->search.len += wchars; + term->search.cursor += wchars; term->search.buf[term->search.len] = L'\0'; } -- cgit v1.3 From 4d0ea114542312a71e6f47027b65618711198182 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 21:05:18 +0200 Subject: search: add basic cursor navigation support --- search.c | 47 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index 6326aded..e453079c 100644 --- a/search.c +++ b/search.c @@ -226,7 +226,7 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask LOG_DBG("search: input: sym=%d/0x%x, mods=0x%08x", sym, sym, mods); const xkb_mod_mask_t ctrl = 1 << term->kbd.mod_ctrl; - //const xkb_mod_mask_t alt = 1 << term->kbd.mod_alt; + const xkb_mod_mask_t alt = 1 << term->kbd.mod_alt; //const xkb_mod_mask_t shift = 1 << term->kbd.mod_shift; //const xkb_mod_mask_t meta = 1 << term->kbd.mod_meta; @@ -270,9 +270,52 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask } } + else if (mods == 0 && sym == XKB_KEY_Left) { + if (term->search.cursor > 0) + term->search.cursor--; + } + + else if (mods == 0 && sym == XKB_KEY_Right) { + if (term->search.cursor < term->search.len) + term->search.cursor++; + } + + else if ((mods == 0 && sym == XKB_KEY_Home) || + (mods == ctrl && sym == XKB_KEY_a)) + term->search.cursor = 0; + + else if ((mods == 0 && sym == XKB_KEY_End) || + (mods == ctrl && sym == XKB_KEY_e)) + term->search.cursor = term->search.len; + else if (mods == 0 && sym == XKB_KEY_BackSpace) { - if (term->search.len > 0) + if (term->search.cursor > 0) { + memmove( + &term->search.buf[term->search.cursor - 1], + &term->search.buf[term->search.cursor], + (term->search.len - term->search.cursor) * sizeof(wchar_t)); + term->search.cursor--; + term->search.buf[--term->search.len] = L'\0'; + } + } + + else if ((mods == alt || mods == ctrl) && sym == XKB_KEY_BackSpace) { + /* TODO: delete word backward */ + } + + else if ((mods == alt && sym == XKB_KEY_d) || + (mods == ctrl && sym == XKB_KEY_Delete)) { + /* TODO: delete word forward */ + } + + else if (mods == 0 && sym == XKB_KEY_Delete) { + if (term->search.cursor < term->search.len) { + memmove( + &term->search.buf[term->search.cursor], + &term->search.buf[term->search.cursor + 1], + (term->search.len - term->search.cursor - 1) * sizeof(wchar_t)); term->search.buf[--term->search.len] = L'\0'; + } } else { -- cgit v1.3 From 4c9e7d0c19b4b07842cab7cf79e136bd4f11d059 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 21:09:49 +0200 Subject: search: call render_search_box() from search_inptu() --- search.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index e453079c..4812b73f 100644 --- a/search.c +++ b/search.c @@ -66,7 +66,6 @@ search_update(struct terminal *term) term->search.match = (struct coord){-1, -1}; term->search.match_len = 0; selection_cancel(term); - render_search_box(term); return; } @@ -204,7 +203,6 @@ search_update(struct terminal *term) term->search.match.col = start_col; term->search.match_len = match_len; - render_search_box(term); return; } @@ -216,7 +214,6 @@ search_update(struct terminal *term) term->search.match = (struct coord){-1, -1}; term->search.match_len = 0; selection_cancel(term); - render_search_box(term); #undef ROW_DEC } @@ -371,4 +368,5 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask LOG_DBG("search: buffer: %S", term->search.buf); search_update(term); render_refresh(term); + render_search_box(term); } -- cgit v1.3 From 9c6ece747ed328b0e922597ec52646c00bb68c8c Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 21:10:13 +0200 Subject: search: for now, reset search match state whenever the buffer changes --- search.c | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'search.c') diff --git a/search.c b/search.c index 4812b73f..7804eaa0 100644 --- a/search.c +++ b/search.c @@ -293,6 +293,9 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask (term->search.len - term->search.cursor) * sizeof(wchar_t)); term->search.cursor--; term->search.buf[--term->search.len] = L'\0'; + + term->search.match = (struct coord){-1, -1}; + term->search.match_len = 0; } } @@ -312,6 +315,9 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask &term->search.buf[term->search.cursor + 1], (term->search.len - term->search.cursor - 1) * sizeof(wchar_t)); term->search.buf[--term->search.len] = L'\0'; + + term->search.match = (struct coord){-1, -1}; + term->search.match_len = 0; } } @@ -363,6 +369,9 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask term->search.len += wchars; term->search.cursor += wchars; term->search.buf[term->search.len] = L'\0'; + + term->search.match = (struct coord){-1, -1}; + term->search.match_len = 0; } LOG_DBG("search: buffer: %S", term->search.buf); -- cgit v1.3 From db291573b78aaef20e87f42b217bdddae8f0f732 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 29 Aug 2019 21:16:25 +0200 Subject: Revert "search: for now, reset search match state whenever the buffer changes" This reverts commit 9c6ece747ed328b0e922597ec52646c00bb68c8c. --- search.c | 9 --------- 1 file changed, 9 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index 7804eaa0..4812b73f 100644 --- a/search.c +++ b/search.c @@ -293,9 +293,6 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask (term->search.len - term->search.cursor) * sizeof(wchar_t)); term->search.cursor--; term->search.buf[--term->search.len] = L'\0'; - - term->search.match = (struct coord){-1, -1}; - term->search.match_len = 0; } } @@ -315,9 +312,6 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask &term->search.buf[term->search.cursor + 1], (term->search.len - term->search.cursor - 1) * sizeof(wchar_t)); term->search.buf[--term->search.len] = L'\0'; - - term->search.match = (struct coord){-1, -1}; - term->search.match_len = 0; } } @@ -369,9 +363,6 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask term->search.len += wchars; term->search.cursor += wchars; term->search.buf[term->search.len] = L'\0'; - - term->search.match = (struct coord){-1, -1}; - term->search.match_len = 0; } LOG_DBG("search: buffer: %S", term->search.buf); -- cgit v1.3 From 61318d068e486eab77b833ed2bd2e4036ee54b25 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 30 Aug 2019 19:36:37 +0200 Subject: search: utility functions distance_{next,prev}_word() These functions calculate the distance from the current cursor to the next/previous word. --- search.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'search.c') diff --git a/search.c b/search.c index 4812b73f..5c9c16e5 100644 --- a/search.c +++ b/search.c @@ -1,6 +1,7 @@ #include "search.h" #include +#include #include #include @@ -217,6 +218,61 @@ search_update(struct terminal *term) #undef ROW_DEC } +static size_t +distance_next_word(const struct terminal *term) +{ + size_t cursor = term->search.cursor; + + /* First eat non-whitespace. This is the word we're skipping past */ + while (cursor < term->search.len) { + if (iswspace(term->search.buf[cursor++])) + break; + } + + assert(cursor == term->search.len || iswspace(term->search.buf[cursor - 1])); + + /* Now skip past whitespace, so that we end up at the beginning of + * the next word */ + while (cursor < term->search.len) { + if (!iswspace(term->search.buf[cursor++])) + break; + } + + LOG_INFO("cursor = %zu, iswspace() = %d", cursor, iswspace(term->search.buf[cursor - 1])); + assert(cursor == term->search.len || !iswspace(term->search.buf[cursor - 1])); + + if (cursor < term->search.len && !iswspace(term->search.buf[cursor])) + cursor--; + + return cursor - term->search.cursor; +} + +static size_t +distance_prev_word(const struct terminal *term) +{ + int cursor = term->search.cursor; + + /* First, eat whitespace prefix */ + while (cursor > 0) { + if (!iswspace(term->search.buf[--cursor])) + break; + } + + assert(cursor == 0 || !iswspace(term->search.buf[cursor])); + + /* Now eat non-whitespace. This is the word we're skipping past */ + while (cursor > 0) { + if (iswspace(term->search.buf[--cursor])) + break; + } + + assert(cursor == 0 || iswspace(term->search.buf[cursor])); + if (iswspace(term->search.buf[cursor])) + cursor++; + + return term->search.cursor - cursor; +} + void search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask_t mods) { -- cgit v1.3 From 64460c5abe5db44a344893687e1f5cf5663452c7 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 30 Aug 2019 19:37:06 +0200 Subject: search: implement delete word backward/forward --- search.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index 5c9c16e5..89204350 100644 --- a/search.c +++ b/search.c @@ -353,12 +353,28 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask } else if ((mods == alt || mods == ctrl) && sym == XKB_KEY_BackSpace) { - /* TODO: delete word backward */ + size_t diff = distance_prev_word(term); + size_t old_cursor = term->search.cursor; + size_t new_cursor = old_cursor - diff; + + memmove(&term->search.buf[new_cursor], + &term->search.buf[old_cursor], + (term->search.len - old_cursor) * sizeof(wchar_t)); + + term->search.len -= diff; + term->search.cursor = new_cursor; } else if ((mods == alt && sym == XKB_KEY_d) || (mods == ctrl && sym == XKB_KEY_Delete)) { - /* TODO: delete word forward */ + size_t diff = distance_next_word(term); + size_t cursor = term->search.cursor; + + memmove(&term->search.buf[cursor], + &term->search.buf[cursor + diff], + (term->search.len - (cursor + diff)) * sizeof(wchar_t)); + + term->search.len -= diff; } else if (mods == 0 && sym == XKB_KEY_Delete) { -- cgit v1.3 From 0fceed6f000126dce3914ad5fcd9aad774458597 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 30 Aug 2019 19:37:18 +0200 Subject: search: implement move cursor word backward/forward --- search.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'search.c') diff --git a/search.c b/search.c index 89204350..1cd32a2a 100644 --- a/search.c +++ b/search.c @@ -328,11 +328,29 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask term->search.cursor--; } + else if ((mods == ctrl && sym == XKB_KEY_Left) || + (mods == alt && sym == XKB_KEY_b)) + { + size_t diff = distance_prev_word(term); + term->search.cursor -= diff; + assert(term->search.cursor >= 0); + assert(term->search.cursor <= term->search.len); + } + else if (mods == 0 && sym == XKB_KEY_Right) { if (term->search.cursor < term->search.len) term->search.cursor++; } + else if ((mods == ctrl && sym == XKB_KEY_Right) || + (mods == alt && sym == XKB_KEY_f)) + { + size_t diff = distance_next_word(term); + term->search.cursor += diff; + assert(term->search.cursor >= 0); + assert(term->search.cursor <= term->search.len); + } + else if ((mods == 0 && sym == XKB_KEY_Home) || (mods == ctrl && sym == XKB_KEY_a)) term->search.cursor = 0; -- cgit v1.3 From 42714c51a9840e434ecd9e9f34e49e721dfd58ea Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 30 Aug 2019 19:42:48 +0200 Subject: search: fix distance to prev word calculation when final cursor was 0 --- search.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'search.c') diff --git a/search.c b/search.c index 1cd32a2a..4bb9223d 100644 --- a/search.c +++ b/search.c @@ -267,7 +267,7 @@ distance_prev_word(const struct terminal *term) } assert(cursor == 0 || iswspace(term->search.buf[cursor])); - if (iswspace(term->search.buf[cursor])) + if (cursor > 0 && iswspace(term->search.buf[cursor])) cursor++; return term->search.cursor - cursor; -- cgit v1.3 From 556bf95b2802fba5bc4e204a27ae9283d0cfcba5 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 30 Aug 2019 20:15:12 +0200 Subject: search: make search-direction configurable --- search.c | 30 +++++++++++++++++++++++------- terminal.h | 1 + 2 files changed, 24 insertions(+), 7 deletions(-) (limited to 'search.c') diff --git a/search.c b/search.c index 4bb9223d..21059f41 100644 --- a/search.c +++ b/search.c @@ -63,6 +63,9 @@ search_cancel(struct terminal *term) static void search_update(struct terminal *term) { + bool backward = term->search.direction == SEARCH_BACKWARD; + term->search.direction = SEARCH_BACKWARD; + if (term->search.len == 0) { term->search.match = (struct coord){-1, -1}; term->search.match_len = 0; @@ -78,23 +81,36 @@ search_update(struct terminal *term) (len > 0 && start_row >= 0 && start_col >= 0)); if (len == 0) { - start_row = grid_row_absolute_in_view(term->grid, term->rows - 1); - start_col = term->cols - 1; + if (backward) { + start_row = grid_row_absolute_in_view(term->grid, term->rows - 1); + start_col = term->cols - 1; + } else { + start_row = grid_row_absolute_in_view(term->grid, 0); + start_col = 0; + } } - LOG_DBG("search: update: starting at row=%d col=%d (offset = %d, view = %d)", - start_row, start_col, term->grid->offset, term->grid->view); + LOG_DBG("search: update: %s: starting at row=%d col=%d (offset = %d, view = %d)", + backward ? "backward" : "forward", start_row, start_col, + term->grid->offset, term->grid->view); #define ROW_DEC(_r) ((_r) = ((_r) - 1 + term->grid->num_rows) % term->grid->num_rows) +#define ROW_INC(_r) ((_r) = ((_r) + 1) % term->grid->num_rows) /* Scan backward from current end-of-output */ /* TODO: don't search "scrollback" in alt screen? */ - for (size_t r = 0; r < term->grid->num_rows; ROW_DEC(start_row), r++) { + for (size_t r = 0; + r < term->grid->num_rows; + backward ? ROW_DEC(start_row) : ROW_INC(start_row), r++) + { const struct row *row = term->grid->rows[start_row]; if (row == NULL) continue; - for (; start_col >= 0; start_col--) { + for (; + backward ? start_col >= 0 : start_col < term->cols; + backward ? start_col-- : start_col++) + { if (wcsncasecmp(&row->cells[start_col].wc, term->search.buf, 1) != 0) continue; @@ -207,7 +223,7 @@ search_update(struct terminal *term) return; } - start_col = term->cols - 1; + start_col = backward ? term->cols - 1 : 0; } /* No match */ diff --git a/terminal.h b/terminal.h index f5eabad1..f435ab3b 100644 --- a/terminal.h +++ b/terminal.h @@ -353,6 +353,7 @@ struct terminal { size_t len; size_t sz; size_t cursor; + enum { SEARCH_BACKWARD, SEARCH_FORWARD} direction; int original_view; bool view_followed_offset; -- cgit v1.3 From f24a40a3912713199473eec9452c047c28847f56 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Fri, 30 Aug 2019 20:15:36 +0200 Subject: search: ctrl+s searches forward --- search.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'search.c') diff --git a/search.c b/search.c index 21059f41..28a24367 100644 --- a/search.c +++ b/search.c @@ -339,6 +339,24 @@ search_input(struct terminal *term, uint32_t key, xkb_keysym_t sym, xkb_mod_mask } } + else if (mods == ctrl && sym == XKB_KEY_s) { + if (term->search.match_len > 0) { + int new_col = term->search.match.col + 1; + int new_row = term->search.match.row; + + if (new_col >= term->cols) { + new_col = 0; + new_row++; + } + + if (new_row < term->grid->num_rows) { + term->search.match.col = new_col; + term->search.match.row = new_row; + term->search.direction = SEARCH_FORWARD; + } + } + } + else if (mods == 0 && sym == XKB_KEY_Left) { if (term->search.cursor > 0) term->search.cursor--; -- cgit v1.3