Add a dedicated function to convert a line into a string and use it to simplify the search window function.

This commit is contained in:
Nicholas Marriott
2009-06-24 22:04:18 +00:00
parent f4b8f00255
commit 096cbf2ea5
4 changed files with 75 additions and 36 deletions

View File

@ -590,42 +590,21 @@ window_pane_mouse(
char *
window_pane_search(struct window_pane *wp, const char *searchstr)
{
const struct grid_cell *gc;
const struct grid_utf8 *gu;
char *buf, *s;
size_t off;
u_int i, j, k;
struct screen *s = &wp->base;
char *line, *ptr;
u_int i;
buf = xmalloc(1);
for (j = 0; j < screen_size_y(&wp->base); j++) {
off = 0;
for (i = 0; i < screen_size_x(&wp->base); i++) {
gc = grid_view_peek_cell(wp->base.grid, i, j);
if (gc->flags & GRID_FLAG_UTF8) {
gu = grid_view_peek_utf8(wp->base.grid, i, j);
buf = xrealloc(buf, 1, off + 8);
for (k = 0; k < UTF8_SIZE; k++) {
if (gu->data[k] == 0xff)
break;
buf[off++] = gu->data[k];
}
} else {
buf = xrealloc(buf, 1, off + 1);
buf[off++] = gc->data;
}
}
while (off > 0 && buf[off - 1] == ' ')
off--;
buf[off] = '\0';
if ((s = strstr(buf, searchstr)) != NULL) {
s = section_string(buf, off, s - buf, 40);
xfree(buf);
return (s);
}
ptr = NULL;
for (i = 0; i < screen_size_y(s); i++) {
line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
log_debug("XXX %s", line);
if ((ptr = strstr(line, searchstr)) != NULL)
break;
xfree(line);
}
xfree(buf);
return (NULL);
if (ptr != NULL) {
ptr = section_string(line, strlen(ptr), ptr - line, 40);
xfree(line);
}
return (ptr);
}