summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2009-06-24 22:04:19 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2009-06-24 22:04:19 +0000
commitcf4d2a9f2dbc1c2c23122b99c17349f01e64f168 (patch)
treee777fc698f74f6d8cf470f9cf934916762072f43 /usr.bin
parent43ed6af81c7fd9a856ae611e2e5eedb7c5045f62 (diff)
Add a dedicated function to convert a line into a string and use it to simplify the search window function.
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/tmux/grid-view.c14
-rw-r--r--usr.bin/tmux/grid.c48
-rw-r--r--usr.bin/tmux/tmux.h4
-rw-r--r--usr.bin/tmux/window.c55
4 files changed, 80 insertions, 41 deletions
diff --git a/usr.bin/tmux/grid-view.c b/usr.bin/tmux/grid-view.c
index 7e4920ac7f8..8154495b544 100644
--- a/usr.bin/tmux/grid-view.c
+++ b/usr.bin/tmux/grid-view.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: grid-view.c,v 1.1 2009/06/01 22:58:49 nicm Exp $ */
+/* $OpenBSD: grid-view.c,v 1.2 2009/06/24 22:04:18 nicm Exp $ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -209,3 +209,15 @@ grid_view_delete_cells(struct grid *gd, u_int px, u_int py, u_int nx)
grid_move_cells(gd, px, px + nx, py, (sx - 1) - (px + nx));
}
+
+/* Convert cells into a string. */
+char *
+grid_view_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
+{
+ GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx);
+
+ px = grid_view_x(gd, px);
+ py = grid_view_y(gd, py);
+
+ return (grid_string_cells(gd, px, py, nx));
+}
diff --git a/usr.bin/tmux/grid.c b/usr.bin/tmux/grid.c
index 9a5753d2f6e..b16766a92d3 100644
--- a/usr.bin/tmux/grid.c
+++ b/usr.bin/tmux/grid.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: grid.c,v 1.2 2009/06/05 03:13:16 ray Exp $ */
+/* $OpenBSD: grid.c,v 1.3 2009/06/24 22:04:18 nicm Exp $ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -493,3 +493,49 @@ grid_move_cells(struct grid *gd, u_int dx, u_int px, u_int py, u_int nx)
grid_put_cell(gd, xx, py, &grid_default_cell);
}
}
+
+/* Convert cells into a string. */
+char *
+grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
+{
+ const struct grid_cell *gc;
+ const struct grid_utf8 *gu;
+ char *buf;
+ size_t len, off;
+ u_int xx;
+
+ GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx);
+
+ len = 128;
+ buf = xmalloc(len);
+ off = 0;
+
+ for (xx = px; xx < px + nx; xx++) {
+ gc = grid_peek_cell(gd, xx, py);
+ if (gc->flags & GRID_FLAG_PADDING)
+ continue;
+
+ if (gc->flags & GRID_FLAG_UTF8) {
+ while (len < off + UTF8_SIZE + 1) {
+ buf = xrealloc(buf, 2, len);
+ len *= 2;
+ }
+
+ gu = grid_peek_utf8(gd, xx, py);
+ memcpy(buf + off, gu->data, UTF8_SIZE);
+ off += UTF8_SIZE;
+ while (off > 0 && ((u_char) buf[off]) == 0xff)
+ off--;
+ } else {
+ while (len < off + 2) {
+ buf = xrealloc(buf, 2, len);
+ len *= 2;
+ }
+
+ buf[off++] = gc->data;
+ }
+ }
+
+ buf[off] = '\0';
+ return (buf);
+}
diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h
index e7f51dcb240..5b7d8daa26b 100644
--- a/usr.bin/tmux/tmux.h
+++ b/usr.bin/tmux/tmux.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: tmux.h,v 1.8 2009/06/24 16:01:02 nicm Exp $ */
+/* $OpenBSD: tmux.h,v 1.9 2009/06/24 22:04:18 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -1327,6 +1327,7 @@ void grid_clear_lines(struct grid *, u_int, u_int);
void grid_move_lines(struct grid *, u_int, u_int, u_int);
void grid_clear_cells(struct grid *, u_int, u_int, u_int);
void grid_move_cells(struct grid *, u_int, u_int, u_int, u_int);
+char *grid_string_cells(struct grid *, u_int, u_int, u_int);
/* grid-view.c */
const struct grid_cell *grid_view_peek_cell(struct grid *, u_int, u_int);
@@ -1348,6 +1349,7 @@ void grid_view_delete_lines_region(
struct grid *, u_int, u_int, u_int, u_int);
void grid_view_insert_cells(struct grid *, u_int, u_int, u_int);
void grid_view_delete_cells(struct grid *, u_int, u_int, u_int);
+char *grid_view_string_cells(struct grid *, u_int, u_int, u_int);
/* screen-write.c */
void screen_write_start(
diff --git a/usr.bin/tmux/window.c b/usr.bin/tmux/window.c
index 70bb1812f2c..1027daa0385 100644
--- a/usr.bin/tmux/window.c
+++ b/usr.bin/tmux/window.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: window.c,v 1.3 2009/06/23 20:17:30 nicm Exp $ */
+/* $OpenBSD: window.c,v 1.4 2009/06/24 22:04:18 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -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;
-
- 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);
- }
+ struct screen *s = &wp->base;
+ char *line, *ptr;
+ u_int i;
+
+ 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);
}