2019-08-27 17:23:28 +02:00
|
|
|
#include "search.h"
|
|
|
|
|
|
2019-12-01 19:22:45 +01:00
|
|
|
#include <string.h>
|
2019-08-27 17:23:28 +02:00
|
|
|
#include <wchar.h>
|
2019-08-30 19:36:37 +02:00
|
|
|
#include <wctype.h>
|
2019-08-29 19:34:41 +02:00
|
|
|
|
|
|
|
|
#include <wayland-client.h>
|
2019-08-27 17:23:28 +02:00
|
|
|
#include <xkbcommon/xkbcommon-compose.h>
|
|
|
|
|
|
|
|
|
|
#define LOG_MODULE "search"
|
2019-08-27 21:11:29 +02:00
|
|
|
#define LOG_ENABLE_DBG 0
|
2019-08-27 17:23:28 +02:00
|
|
|
#include "log.h"
|
2020-10-09 19:44:23 +02:00
|
|
|
#include "config.h"
|
2019-08-27 21:11:40 +02:00
|
|
|
#include "grid.h"
|
2020-03-08 15:28:47 +01:00
|
|
|
#include "input.h"
|
2019-12-03 19:22:47 +01:00
|
|
|
#include "misc.h"
|
2019-08-27 17:23:28 +02:00
|
|
|
#include "render.h"
|
2019-08-27 19:33:19 +02:00
|
|
|
#include "selection.h"
|
2019-08-29 19:34:41 +02:00
|
|
|
#include "shm.h"
|
2020-05-01 11:46:24 +02:00
|
|
|
#include "util.h"
|
2019-08-29 19:34:41 +02:00
|
|
|
|
2020-08-12 18:45:35 +02:00
|
|
|
/*
|
|
|
|
|
* Ensures a "new" viewport doesn't contain any unallocated rows.
|
|
|
|
|
*
|
|
|
|
|
* This is done by first checking if the *first* row is NULL. If so,
|
|
|
|
|
* we move the viewport *forward*, until the first row is non-NULL. At
|
|
|
|
|
* this point, the entire viewport should be allocated rows only.
|
|
|
|
|
*
|
|
|
|
|
* If the first row already was non-NULL, we instead check the *last*
|
|
|
|
|
* row, and if it is NULL, we move the viewport *backward* until the
|
|
|
|
|
* last row is non-NULL.
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
ensure_view_is_allocated(struct terminal *term, int new_view)
|
|
|
|
|
{
|
|
|
|
|
int view_end = (new_view + term->rows - 1) & (term->grid->num_rows - 1);
|
|
|
|
|
|
|
|
|
|
if (term->grid->rows[new_view] == NULL) {
|
|
|
|
|
while (term->grid->rows[new_view] == NULL)
|
|
|
|
|
new_view = (new_view + 1) & (term->grid->num_rows - 1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
else if (term->grid->rows[view_end] == NULL) {
|
|
|
|
|
while (term->grid->rows[view_end] == NULL) {
|
|
|
|
|
new_view--;
|
|
|
|
|
if (new_view < 0)
|
|
|
|
|
new_view += term->grid->num_rows;
|
|
|
|
|
view_end = (new_view + term->rows - 1) & (term->grid->num_rows - 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(_DEBUG)
|
|
|
|
|
for (size_t r = 0; r < term->rows; r++)
|
|
|
|
|
assert(term->grid->rows[(new_view + r) & (term->grid->num_rows - 1)] != NULL);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return new_view;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 19:17:34 +01:00
|
|
|
static bool
|
|
|
|
|
search_ensure_size(struct terminal *term, size_t wanted_size)
|
|
|
|
|
{
|
|
|
|
|
while (wanted_size >= 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 false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
term->search.buf = new_buf;
|
|
|
|
|
term->search.sz = new_sz;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 19:34:41 +02:00
|
|
|
static void
|
|
|
|
|
search_cancel_keep_selection(struct terminal *term)
|
|
|
|
|
{
|
wayland: instantiate sub-surfaces on-demand
While most compositors handle instantiated but not-yet-mapped
sub-surfaces correctly, e.g. kwin does not.
For example, it will incorrectly offset the main surface both
horizontally and vertically with a couple of pixels, leaving two
transparent areas at the top and left, between the SSDs and the main
surface.
Note that a workaround is to position the sub-surfaces inside the main
surface while they're unmapped. However, since the surfaces may be
larger than the main surface (the CSDs, for examples, always are),
this doesn't quite work since kwin, at least, resizes the window to
include the sub-surfaces, even when unmapped.
So, instead we instantiate all sub-surfaces on demand, when we know
where to position them, and when they should be mapped.
2020-02-26 12:22:16 +01:00
|
|
|
struct wl_window *win = term->window;
|
|
|
|
|
if (win->search_sub_surface != NULL)
|
|
|
|
|
wl_subsurface_destroy(win->search_sub_surface);
|
|
|
|
|
if (win->search_surface != NULL)
|
|
|
|
|
wl_surface_destroy(win->search_surface);
|
|
|
|
|
|
|
|
|
|
win->search_surface = NULL;
|
|
|
|
|
win->search_sub_surface = NULL;
|
2019-08-27 17:23:28 +02:00
|
|
|
|
|
|
|
|
free(term->search.buf);
|
|
|
|
|
term->search.buf = NULL;
|
|
|
|
|
term->search.len = 0;
|
|
|
|
|
term->search.sz = 0;
|
2019-08-29 21:03:16 +02:00
|
|
|
term->search.cursor = 0;
|
2019-08-27 19:33:19 +02:00
|
|
|
term->search.match = (struct coord){-1, -1};
|
|
|
|
|
term->search.match_len = 0;
|
2019-08-29 19:34:41 +02:00
|
|
|
term->is_searching = false;
|
2020-01-05 15:25:24 +01:00
|
|
|
term->render.search_glyph_offset = 0;
|
2019-08-29 19:34:41 +02:00
|
|
|
|
2019-11-29 22:11:15 +01:00
|
|
|
term_xcursor_update(term);
|
2019-08-29 19:34:41 +02:00
|
|
|
render_refresh(term);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
search_begin(struct terminal *term)
|
|
|
|
|
{
|
|
|
|
|
LOG_DBG("search: begin");
|
|
|
|
|
|
|
|
|
|
search_cancel_keep_selection(term);
|
|
|
|
|
selection_cancel(term);
|
|
|
|
|
|
wayland: instantiate sub-surfaces on-demand
While most compositors handle instantiated but not-yet-mapped
sub-surfaces correctly, e.g. kwin does not.
For example, it will incorrectly offset the main surface both
horizontally and vertically with a couple of pixels, leaving two
transparent areas at the top and left, between the SSDs and the main
surface.
Note that a workaround is to position the sub-surfaces inside the main
surface while they're unmapped. However, since the surfaces may be
larger than the main surface (the CSDs, for examples, always are),
this doesn't quite work since kwin, at least, resizes the window to
include the sub-surfaces, even when unmapped.
So, instead we instantiate all sub-surfaces on demand, when we know
where to position them, and when they should be mapped.
2020-02-26 12:22:16 +01:00
|
|
|
/* On-demand instantiate wayland surface */
|
|
|
|
|
struct wl_window *win = term->window;
|
|
|
|
|
struct wayland *wayl = term->wl;
|
|
|
|
|
win->search_surface = wl_compositor_create_surface(wayl->compositor);
|
2020-02-29 12:09:28 +01:00
|
|
|
wl_surface_set_user_data(win->search_surface, term->window);
|
|
|
|
|
|
wayland: instantiate sub-surfaces on-demand
While most compositors handle instantiated but not-yet-mapped
sub-surfaces correctly, e.g. kwin does not.
For example, it will incorrectly offset the main surface both
horizontally and vertically with a couple of pixels, leaving two
transparent areas at the top and left, between the SSDs and the main
surface.
Note that a workaround is to position the sub-surfaces inside the main
surface while they're unmapped. However, since the surfaces may be
larger than the main surface (the CSDs, for examples, always are),
this doesn't quite work since kwin, at least, resizes the window to
include the sub-surfaces, even when unmapped.
So, instead we instantiate all sub-surfaces on demand, when we know
where to position them, and when they should be mapped.
2020-02-26 12:22:16 +01:00
|
|
|
win->search_sub_surface = wl_subcompositor_get_subsurface(
|
|
|
|
|
wayl->sub_compositor, win->search_surface, win->surface);
|
2020-03-01 13:06:00 +01:00
|
|
|
wl_subsurface_set_sync(win->search_sub_surface);
|
wayland: instantiate sub-surfaces on-demand
While most compositors handle instantiated but not-yet-mapped
sub-surfaces correctly, e.g. kwin does not.
For example, it will incorrectly offset the main surface both
horizontally and vertically with a couple of pixels, leaving two
transparent areas at the top and left, between the SSDs and the main
surface.
Note that a workaround is to position the sub-surfaces inside the main
surface while they're unmapped. However, since the surfaces may be
larger than the main surface (the CSDs, for examples, always are),
this doesn't quite work since kwin, at least, resizes the window to
include the sub-surfaces, even when unmapped.
So, instead we instantiate all sub-surfaces on demand, when we know
where to position them, and when they should be mapped.
2020-02-26 12:22:16 +01:00
|
|
|
|
2019-08-27 19:33:19 +02:00
|
|
|
term->search.original_view = term->grid->view;
|
|
|
|
|
term->search.view_followed_offset = term->grid->view == term->grid->offset;
|
2019-08-27 17:23:28 +02:00
|
|
|
term->is_searching = true;
|
|
|
|
|
|
2019-11-29 22:11:15 +01:00
|
|
|
term_xcursor_update(term);
|
2020-03-06 19:16:54 +01:00
|
|
|
render_refresh_search(term);
|
2019-08-27 17:23:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
search_cancel(struct terminal *term)
|
|
|
|
|
{
|
2019-08-29 19:34:41 +02:00
|
|
|
if (!term->is_searching)
|
|
|
|
|
return;
|
2019-08-27 17:23:28 +02:00
|
|
|
|
2019-08-29 19:34:41 +02:00
|
|
|
search_cancel_keep_selection(term);
|
|
|
|
|
selection_cancel(term);
|
2019-08-27 17:23:28 +02:00
|
|
|
}
|
|
|
|
|
|
2019-12-03 19:40:22 +01:00
|
|
|
static void
|
|
|
|
|
search_update_selection(struct terminal *term,
|
|
|
|
|
int start_row, int start_col,
|
|
|
|
|
int end_row, int end_col)
|
|
|
|
|
{
|
2020-07-25 11:23:54 +02:00
|
|
|
bool move_viewport = true;
|
|
|
|
|
|
|
|
|
|
int view_end = (term->grid->view + term->rows - 1) & (term->grid->num_rows - 1);
|
|
|
|
|
if (view_end >= term->grid->view) {
|
|
|
|
|
/* Viewport does *not* wrap around */
|
|
|
|
|
if (start_row >= term->grid->view && end_row <= view_end)
|
|
|
|
|
move_viewport = false;
|
|
|
|
|
} else {
|
|
|
|
|
/* Viewport wraps */
|
|
|
|
|
if (start_row >= term->grid->view || end_row <= view_end)
|
|
|
|
|
move_viewport = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (move_viewport) {
|
|
|
|
|
int old_view = term->grid->view;
|
|
|
|
|
int new_view = start_row - term->rows / 2;
|
|
|
|
|
|
|
|
|
|
while (new_view < 0)
|
|
|
|
|
new_view += term->grid->num_rows;
|
|
|
|
|
|
2020-08-12 18:45:35 +02:00
|
|
|
new_view = ensure_view_is_allocated(term, new_view);
|
2020-07-25 11:23:54 +02:00
|
|
|
|
|
|
|
|
/* Don't scroll past scrollback history */
|
2020-08-12 18:45:35 +02:00
|
|
|
int end = (term->grid->offset + term->rows - 1) & (term->grid->num_rows - 1);
|
2020-07-25 11:23:54 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-12 18:45:35 +02:00
|
|
|
#if defined(_DEBUG)
|
|
|
|
|
/* Verify all to-be-visible rows have been allocated */
|
|
|
|
|
for (int r = 0; r < term->rows; r++)
|
|
|
|
|
assert(term->grid->rows[(new_view + r) & (term->grid->num_rows - 1)] != NULL);
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-07-25 11:23:54 +02:00
|
|
|
/* Update view */
|
|
|
|
|
term->grid->view = new_view;
|
|
|
|
|
if (new_view != old_view)
|
|
|
|
|
term_damage_view(term);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-24 17:49:50 +02:00
|
|
|
/* Selection endpoint is inclusive */
|
|
|
|
|
if (--end_col < 0) {
|
|
|
|
|
end_col = term->cols - 1;
|
|
|
|
|
end_row--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Begin a new selection if the start coords changed */
|
|
|
|
|
if (start_row != term->search.match.row ||
|
|
|
|
|
start_col != term->search.match.col)
|
|
|
|
|
{
|
|
|
|
|
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, SELECTION_NORMAL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Update selection endpoint */
|
|
|
|
|
{
|
|
|
|
|
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);
|
|
|
|
|
}
|
2019-12-03 19:40:22 +01:00
|
|
|
}
|
2020-07-08 18:20:34 +02:00
|
|
|
|
2019-08-27 19:33:19 +02:00
|
|
|
static void
|
2019-12-03 19:24:33 +01:00
|
|
|
search_find_next(struct terminal *term)
|
2019-08-27 19:33:19 +02:00
|
|
|
{
|
2019-08-30 20:15:12 +02:00
|
|
|
bool backward = term->search.direction == SEARCH_BACKWARD;
|
|
|
|
|
term->search.direction = SEARCH_BACKWARD;
|
|
|
|
|
|
2019-08-27 19:33:19 +02:00
|
|
|
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;
|
2020-07-24 17:49:50 +02:00
|
|
|
size_t len = term->search.match_len;
|
2019-08-27 19:33:19 +02:00
|
|
|
|
|
|
|
|
assert((len == 0 && start_row == -1 && start_col == -1) ||
|
|
|
|
|
(len > 0 && start_row >= 0 && start_col >= 0));
|
|
|
|
|
|
|
|
|
|
if (len == 0) {
|
2019-08-30 20:15:12 +02:00
|
|
|
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;
|
|
|
|
|
}
|
2019-08-27 19:33:19 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-30 20:15:12 +02:00
|
|
|
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);
|
2019-08-27 19:33:19 +02:00
|
|
|
|
2020-08-12 18:45:35 +02:00
|
|
|
#define ROW_DEC(_r) ((_r) = ((_r) - 1 + term->grid->num_rows) & (term->grid->num_rows - 1))
|
|
|
|
|
#define ROW_INC(_r) ((_r) = ((_r) + 1) & (term->grid->num_rows - 1))
|
2019-08-27 21:09:37 +02:00
|
|
|
|
2019-08-27 19:33:19 +02:00
|
|
|
/* Scan backward from current end-of-output */
|
2019-08-28 17:26:30 +02:00
|
|
|
/* TODO: don't search "scrollback" in alt screen? */
|
2019-08-30 20:15:12 +02:00
|
|
|
for (size_t r = 0;
|
|
|
|
|
r < term->grid->num_rows;
|
|
|
|
|
backward ? ROW_DEC(start_row) : ROW_INC(start_row), r++)
|
|
|
|
|
{
|
|
|
|
|
for (;
|
|
|
|
|
backward ? start_col >= 0 : start_col < term->cols;
|
|
|
|
|
backward ? start_col-- : start_col++)
|
|
|
|
|
{
|
2019-12-03 20:00:38 +01:00
|
|
|
const struct row *row = term->grid->rows[start_row];
|
|
|
|
|
if (row == NULL)
|
|
|
|
|
continue;
|
|
|
|
|
|
2019-08-27 21:16:57 +02:00
|
|
|
if (wcsncasecmp(&row->cells[start_col].wc, term->search.buf, 1) != 0)
|
2019-08-27 19:33:19 +02:00
|
|
|
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++) {
|
2019-12-03 19:17:51 +01:00
|
|
|
if (end_col >= term->cols) {
|
2019-08-27 19:33:19 +02:00
|
|
|
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];
|
|
|
|
|
}
|
2019-12-03 19:17:51 +01:00
|
|
|
|
|
|
|
|
if (wcsncasecmp(&row->cells[end_col].wc, &term->search.buf[i], 1) != 0)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
end_col++;
|
2019-08-27 19:33:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2019-12-03 19:40:22 +01:00
|
|
|
search_update_selection(term, start_row, start_col, end_row, end_col);
|
2019-08-27 19:33:19 +02:00
|
|
|
|
|
|
|
|
/* Update match state */
|
|
|
|
|
term->search.match.row = start_row;
|
|
|
|
|
term->search.match.col = start_col;
|
|
|
|
|
term->search.match_len = match_len;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-30 20:15:12 +02:00
|
|
|
start_col = backward ? term->cols - 1 : 0;
|
2019-08-27 19:33:19 +02:00
|
|
|
}
|
2019-08-27 19:58:44 +02:00
|
|
|
|
|
|
|
|
/* No match */
|
2019-08-27 21:09:37 +02:00
|
|
|
LOG_DBG("no match");
|
2019-08-27 19:58:44 +02:00
|
|
|
term->search.match = (struct coord){-1, -1};
|
|
|
|
|
term->search.match_len = 0;
|
|
|
|
|
selection_cancel(term);
|
2019-08-27 21:09:37 +02:00
|
|
|
#undef ROW_DEC
|
2019-08-27 19:33:19 +02:00
|
|
|
}
|
2020-07-08 18:20:34 +02:00
|
|
|
|
2020-12-05 11:49:32 +01:00
|
|
|
void
|
|
|
|
|
search_add_chars(struct terminal *term, const char *src, size_t count)
|
2020-11-01 12:39:57 +01:00
|
|
|
{
|
|
|
|
|
mbstate_t ps = {0};
|
|
|
|
|
size_t wchars = mbsnrtowcs(NULL, &src, count, 0, &ps);
|
|
|
|
|
|
|
|
|
|
if (wchars == -1) {
|
|
|
|
|
LOG_ERRNO("failed to convert %.*s to wchars", (int)count, src);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!search_ensure_size(term, term->search.len + wchars))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
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.cursor], &src, count,
|
|
|
|
|
wchars, &ps);
|
|
|
|
|
|
|
|
|
|
term->search.len += wchars;
|
|
|
|
|
term->search.cursor += wchars;
|
|
|
|
|
term->search.buf[term->search.len] = L'\0';
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-03 19:22:47 +01:00
|
|
|
static void
|
2019-12-03 19:43:45 +01:00
|
|
|
search_match_to_end_of_word(struct terminal *term, bool spaces_only)
|
2019-12-03 19:22:47 +01:00
|
|
|
{
|
|
|
|
|
if (term->search.match_len == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
assert(term->search.match.row != -1);
|
|
|
|
|
assert(term->search.match.col != -1);
|
|
|
|
|
|
2019-12-03 19:42:43 +01:00
|
|
|
int end_row = term->search.match.row;
|
|
|
|
|
int end_col = term->search.match.col;
|
2019-12-03 19:22:47 +01:00
|
|
|
size_t len = term->search.match_len;
|
|
|
|
|
|
|
|
|
|
/* Calculate end coord - note: assumed to be valid */
|
|
|
|
|
for (size_t i = 0; i < len; i++) {
|
2019-12-03 20:25:22 +01:00
|
|
|
if (++end_col >= term->cols) {
|
2020-08-12 18:45:35 +02:00
|
|
|
end_row = (end_row + 1) & (term->grid->num_rows - 1);
|
2019-12-03 20:25:22 +01:00
|
|
|
end_col = 0;
|
|
|
|
|
}
|
2019-12-03 19:22:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tll(wchar_t) new_chars = tll_init();
|
|
|
|
|
|
|
|
|
|
/* Always append at least one character *if* possible */
|
|
|
|
|
bool first = true;
|
|
|
|
|
|
|
|
|
|
for (size_t r = 0;
|
|
|
|
|
r < term->grid->num_rows;
|
2020-08-12 18:45:35 +02:00
|
|
|
end_row = (end_row + 1) & (term->grid->num_rows - 1), r++)
|
2019-12-03 19:22:47 +01:00
|
|
|
{
|
2019-12-03 19:42:43 +01:00
|
|
|
const struct row *row = term->grid->rows[end_row];
|
2019-12-03 19:22:47 +01:00
|
|
|
if (row == NULL)
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
bool done = false;
|
2019-12-03 19:42:43 +01:00
|
|
|
for (; end_col < term->cols; end_col++) {
|
|
|
|
|
wchar_t wc = row->cells[end_col].wc;
|
2020-10-09 19:44:23 +02:00
|
|
|
if (wc == 0 || (!first && !isword(wc, spaces_only, term->conf->word_delimiters))) {
|
2019-12-03 19:22:47 +01:00
|
|
|
done = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
|
tll_push_back(new_chars, wc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (done)
|
|
|
|
|
break;
|
2019-12-03 20:26:32 +01:00
|
|
|
|
|
|
|
|
end_col = 0;
|
2019-12-03 19:22:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tll_length(new_chars) == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (!search_ensure_size(term, term->search.len + tll_length(new_chars)))
|
|
|
|
|
return;
|
|
|
|
|
|
2019-12-03 19:42:43 +01:00
|
|
|
/* Keep cursor at the end, but don't move it if not */
|
2019-12-03 19:22:47 +01:00
|
|
|
bool move_cursor = term->search.cursor == term->search.len;
|
|
|
|
|
|
2019-12-03 19:42:43 +01:00
|
|
|
/* Append newly found characters to the search buffer */
|
2019-12-03 19:22:47 +01:00
|
|
|
tll_foreach(new_chars, it)
|
|
|
|
|
term->search.buf[term->search.len++] = it->item;
|
|
|
|
|
term->search.buf[term->search.len] = L'\0';
|
|
|
|
|
|
|
|
|
|
if (move_cursor)
|
|
|
|
|
term->search.cursor += tll_length(new_chars);
|
|
|
|
|
|
|
|
|
|
tll_free(new_chars);
|
|
|
|
|
|
2019-12-03 19:40:22 +01:00
|
|
|
search_update_selection(
|
|
|
|
|
term, term->search.match.row, term->search.match.col, end_row, end_col);
|
2019-12-03 19:22:47 +01:00
|
|
|
}
|
2020-07-08 18:20:34 +02:00
|
|
|
|
2019-08-30 19:36:37 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2020-07-08 18:20:34 +02:00
|
|
|
|
2019-08-30 19:36:37 +02:00
|
|
|
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]));
|
2019-08-30 19:42:48 +02:00
|
|
|
if (cursor > 0 && iswspace(term->search.buf[cursor]))
|
2019-08-30 19:36:37 +02:00
|
|
|
cursor++;
|
|
|
|
|
|
|
|
|
|
return term->search.cursor - cursor;
|
|
|
|
|
}
|
2020-07-08 18:20:34 +02:00
|
|
|
|
2020-11-01 12:39:57 +01:00
|
|
|
static void
|
|
|
|
|
from_clipboard_cb(char *text, size_t size, void *user)
|
|
|
|
|
{
|
|
|
|
|
struct terminal *term = user;
|
2020-12-05 11:49:32 +01:00
|
|
|
search_add_chars(term, text, size);
|
2020-11-01 12:39:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
from_clipboard_done(void *user)
|
|
|
|
|
{
|
|
|
|
|
struct terminal *term = user;
|
|
|
|
|
|
|
|
|
|
LOG_DBG("search: buffer: %ls", term->search.buf);
|
|
|
|
|
search_find_next(term);
|
|
|
|
|
render_refresh_search(term);
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
static bool
|
2020-07-22 17:51:27 +02:00
|
|
|
execute_binding(struct seat *seat, struct terminal *term,
|
|
|
|
|
enum bind_action_search action, uint32_t serial)
|
2019-08-27 17:23:28 +02:00
|
|
|
{
|
2020-03-18 15:30:14 +01:00
|
|
|
switch (action) {
|
|
|
|
|
case BIND_ACTION_SEARCH_NONE:
|
|
|
|
|
return false;
|
2020-03-08 15:28:47 +01:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_CANCEL:
|
2019-08-27 19:33:19 +02:00
|
|
|
if (term->search.view_followed_offset)
|
|
|
|
|
term->grid->view = term->grid->offset;
|
2020-08-12 18:45:35 +02:00
|
|
|
else {
|
|
|
|
|
term->grid->view = ensure_view_is_allocated(
|
|
|
|
|
term, term->search.original_view);
|
|
|
|
|
}
|
2019-08-27 19:33:19 +02:00
|
|
|
term_damage_view(term);
|
2019-08-27 17:23:28 +02:00
|
|
|
search_cancel(term);
|
2020-03-18 15:30:14 +01:00
|
|
|
return true;
|
2019-08-27 19:33:19 +02:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_COMMIT:
|
2020-07-22 17:51:27 +02:00
|
|
|
selection_finalize(seat, term, serial);
|
2019-08-29 19:34:41 +02:00
|
|
|
search_cancel_keep_selection(term);
|
2020-03-18 15:30:14 +01:00
|
|
|
return true;
|
2019-08-27 19:33:19 +02:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_FIND_PREV:
|
2019-08-27 19:33:19 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-18 15:30:14 +01:00
|
|
|
return false;
|
2019-08-27 17:23:28 +02:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_FIND_NEXT:
|
2019-08-30 20:15:36 +02:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-03-18 15:30:14 +01:00
|
|
|
return false;
|
2019-08-30 20:15:36 +02:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_EDIT_LEFT:
|
2019-08-29 21:05:18 +02:00
|
|
|
if (term->search.cursor > 0)
|
|
|
|
|
term->search.cursor--;
|
2020-03-18 15:30:14 +01:00
|
|
|
return false;
|
2019-08-29 21:05:18 +02:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_EDIT_LEFT_WORD: {
|
2019-08-30 19:37:18 +02:00
|
|
|
size_t diff = distance_prev_word(term);
|
|
|
|
|
term->search.cursor -= diff;
|
|
|
|
|
assert(term->search.cursor >= 0);
|
|
|
|
|
assert(term->search.cursor <= term->search.len);
|
2020-03-18 15:30:14 +01:00
|
|
|
return false;
|
2019-08-30 19:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_EDIT_RIGHT:
|
2019-08-29 21:05:18 +02:00
|
|
|
if (term->search.cursor < term->search.len)
|
|
|
|
|
term->search.cursor++;
|
2020-03-18 15:30:14 +01:00
|
|
|
return false;
|
2019-08-29 21:05:18 +02:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_EDIT_RIGHT_WORD: {
|
2019-08-30 19:37:18 +02:00
|
|
|
size_t diff = distance_next_word(term);
|
|
|
|
|
term->search.cursor += diff;
|
|
|
|
|
assert(term->search.cursor >= 0);
|
|
|
|
|
assert(term->search.cursor <= term->search.len);
|
2020-03-18 15:30:14 +01:00
|
|
|
return false;
|
2019-08-30 19:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_EDIT_HOME:
|
2019-08-29 21:05:18 +02:00
|
|
|
term->search.cursor = 0;
|
2020-03-18 15:30:14 +01:00
|
|
|
return false;
|
2019-08-29 21:05:18 +02:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_EDIT_END:
|
2019-08-29 21:05:18 +02:00
|
|
|
term->search.cursor = term->search.len;
|
2020-03-18 15:30:14 +01:00
|
|
|
return false;
|
2019-08-29 21:05:18 +02:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_DELETE_PREV:
|
2019-08-29 21:05:18 +02:00
|
|
|
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--;
|
2019-08-27 17:23:28 +02:00
|
|
|
term->search.buf[--term->search.len] = L'\0';
|
2019-08-29 21:05:18 +02:00
|
|
|
}
|
2020-03-18 15:30:14 +01:00
|
|
|
return false;
|
2019-08-29 21:05:18 +02:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_DELETE_PREV_WORD: {
|
2019-08-30 19:37:06 +02:00
|
|
|
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;
|
2020-03-18 15:30:14 +01:00
|
|
|
return false;
|
2019-08-29 21:05:18 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_DELETE_NEXT:
|
|
|
|
|
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';
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
case BIND_ACTION_SEARCH_DELETE_NEXT_WORD: {
|
2019-08-30 19:37:06 +02:00
|
|
|
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;
|
2020-03-18 15:30:14 +01:00
|
|
|
return false;
|
2019-08-29 21:05:18 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_EXTEND_WORD:
|
2019-12-03 19:43:45 +01:00
|
|
|
search_match_to_end_of_word(term, false);
|
2020-03-18 15:30:14 +01:00
|
|
|
return false;
|
2019-12-03 19:43:45 +01:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_EXTEND_WORD_WS:
|
2019-12-03 19:43:45 +01:00
|
|
|
search_match_to_end_of_word(term, true);
|
2020-03-18 15:30:14 +01:00
|
|
|
return false;
|
2019-12-03 19:22:47 +01:00
|
|
|
|
2020-11-01 12:39:57 +01:00
|
|
|
case BIND_ACTION_SEARCH_CLIPBOARD_PASTE:
|
|
|
|
|
text_from_clipboard(
|
|
|
|
|
seat, term, &from_clipboard_cb, &from_clipboard_done, term);
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
case BIND_ACTION_SEARCH_PRIMARY_PASTE:
|
|
|
|
|
text_from_primary(
|
|
|
|
|
seat, term, &from_clipboard_cb, &from_clipboard_done, term);
|
|
|
|
|
return false;
|
|
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
case BIND_ACTION_SEARCH_COUNT:
|
|
|
|
|
assert(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2019-08-27 17:23:28 +02:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
assert(false);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2020-07-08 18:20:34 +02:00
|
|
|
search_input(struct seat *seat, struct terminal *term, uint32_t key,
|
|
|
|
|
xkb_keysym_t sym, xkb_mod_mask_t mods, uint32_t serial)
|
2020-03-18 15:30:14 +01:00
|
|
|
{
|
|
|
|
|
LOG_DBG("search: input: sym=%d/0x%x, mods=0x%08x", sym, sym, mods);
|
|
|
|
|
|
|
|
|
|
enum xkb_compose_status compose_status = xkb_compose_state_get_status(
|
2020-07-08 18:20:34 +02:00
|
|
|
seat->kbd.xkb_compose_state);
|
2019-08-27 17:23:28 +02:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
/* Key bindings */
|
2020-07-08 18:20:34 +02:00
|
|
|
tll_foreach(seat->kbd.bindings.search, it) {
|
2020-03-18 15:30:14 +01:00
|
|
|
if (it->item.bind.mods != mods)
|
|
|
|
|
continue;
|
2019-08-27 17:23:28 +02:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
/* Match symbol */
|
|
|
|
|
if (it->item.bind.sym == sym) {
|
2020-07-22 17:51:27 +02:00
|
|
|
if (!execute_binding(seat, term, it->item.action, serial))
|
2020-03-18 15:30:14 +01:00
|
|
|
goto update_search;
|
2019-08-27 17:23:28 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
/* Match raw key code */
|
|
|
|
|
tll_foreach(it->item.bind.key_codes, code) {
|
|
|
|
|
if (code->item == key) {
|
2020-07-22 17:51:27 +02:00
|
|
|
if (!execute_binding(seat, term, it->item.action, serial))
|
2020-03-18 15:30:14 +01:00
|
|
|
goto update_search;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-27 17:23:28 +02:00
|
|
|
|
2020-08-23 07:42:20 +02:00
|
|
|
uint8_t buf[64] = {0};
|
2020-03-18 15:30:14 +01:00
|
|
|
int count = 0;
|
2019-08-27 17:23:28 +02:00
|
|
|
|
2020-03-18 15:30:14 +01:00
|
|
|
if (compose_status == XKB_COMPOSE_COMPOSED) {
|
|
|
|
|
count = xkb_compose_state_get_utf8(
|
2020-07-08 18:20:34 +02:00
|
|
|
seat->kbd.xkb_compose_state, (char *)buf, sizeof(buf));
|
|
|
|
|
xkb_compose_state_reset(seat->kbd.xkb_compose_state);
|
2020-03-18 15:30:14 +01:00
|
|
|
} else if (compose_status == XKB_COMPOSE_CANCELLED) {
|
|
|
|
|
count = 0;
|
|
|
|
|
} else {
|
|
|
|
|
count = xkb_state_key_get_utf8(
|
2020-07-08 18:20:34 +02:00
|
|
|
seat->kbd.xkb_state, key, (char *)buf, sizeof(buf));
|
2020-03-18 15:30:14 +01:00
|
|
|
}
|
2019-08-29 21:03:46 +02:00
|
|
|
|
2020-08-12 18:47:54 +02:00
|
|
|
if (count == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2020-12-05 11:49:32 +01:00
|
|
|
search_add_chars(term, (const char *)buf, count);
|
2020-03-18 15:30:14 +01:00
|
|
|
|
|
|
|
|
update_search:
|
2020-08-06 23:20:46 +02:00
|
|
|
LOG_DBG("search: buffer: %ls", term->search.buf);
|
2019-12-03 19:24:33 +01:00
|
|
|
search_find_next(term);
|
2020-03-06 19:16:54 +01:00
|
|
|
render_refresh_search(term);
|
2019-08-27 17:23:28 +02:00
|
|
|
}
|