Change find-window and monitor-content to use fnmatch(3). For convenience and

compatibility, *s are implicitly added at the start and end of the pattern.
This commit is contained in:
Nicholas Marriott 2009-06-25 16:04:24 +00:00
parent 853ad68162
commit f7a9eb46fc
6 changed files with 43 additions and 86 deletions

View File

@ -1,4 +1,4 @@
/* $Id: cmd-find-window.c,v 1.8 2009-05-19 13:32:55 tcunha Exp $ */ /* $OpenBSD: cmd-find-window.c,v 1.2 2009/06/24 22:49:56 nicm Exp $ */
/* /*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
@ -18,6 +18,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <fnmatch.h>
#include <string.h> #include <string.h>
#include "tmux.h" #include "tmux.h"
@ -58,8 +59,8 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
struct window_pane *wp; struct window_pane *wp;
ARRAY_DECL(, u_int) list_idx; ARRAY_DECL(, u_int) list_idx;
ARRAY_DECL(, char *) list_ctx; ARRAY_DECL(, char *) list_ctx;
char *sres, *sctx; char *sres, *sctx, *searchstr;
u_int i; u_int i, line;
if (ctx->curclient == NULL) { if (ctx->curclient == NULL) {
ctx->error(ctx, "must be run interactively"); ctx->error(ctx, "must be run interactively");
@ -73,17 +74,18 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
ARRAY_INIT(&list_idx); ARRAY_INIT(&list_idx);
ARRAY_INIT(&list_ctx); ARRAY_INIT(&list_ctx);
xasprintf(&searchstr, "*%s*", data->arg);
RB_FOREACH(wm, winlinks, &s->windows) { RB_FOREACH(wm, winlinks, &s->windows) {
i = 0; i = 0;
TAILQ_FOREACH(wp, &wm->window->panes, entry) { TAILQ_FOREACH(wp, &wm->window->panes, entry) {
i++; i++;
if (strstr(wm->window->name, data->arg) != NULL) if (fnmatch(searchstr, wm->window->name, 0) == 0)
sctx = xstrdup(""); sctx = xstrdup("");
else { else {
sres = window_pane_search(wp, data->arg); sres = window_pane_search(wp, data->arg, &line);
if (sres == NULL && if (sres == NULL &&
strstr(wp->base.title, data->arg) == NULL) fnmatch(searchstr, wp->base.title, 0) != 0)
continue; continue;
if (sres == NULL) { if (sres == NULL) {
@ -91,7 +93,9 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
"pane %u title: \"%s\"", i - 1, "pane %u title: \"%s\"", i - 1,
wp->base.title); wp->base.title);
} else { } else {
xasprintf(&sctx, "\"%s\"", sres); xasprintf(&sctx,
"pane %u line %u: \"%s\"", i - 1,
line + 1, sres);
xfree(sres); xfree(sres);
} }
} }
@ -100,6 +104,7 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
ARRAY_ADD(&list_ctx, sctx); ARRAY_ADD(&list_ctx, sctx);
} }
} }
xfree(searchstr);
if (ARRAY_LENGTH(&list_idx) == 0) { if (ARRAY_LENGTH(&list_idx) == 0) {
ctx->error(ctx, "no windows matching: %s", data->arg); ctx->error(ctx, "no windows matching: %s", data->arg);

17
grid.c
View File

@ -1,4 +1,4 @@
/* $OpenBSD: grid.c,v 1.3 2009/06/24 22:04:18 nicm Exp $ */ /* $OpenBSD: grid.c,v 1.4 2009/06/24 22:49:56 nicm Exp $ */
/* /*
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
@ -502,7 +502,7 @@ grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
const struct grid_utf8 *gu; const struct grid_utf8 *gu;
char *buf; char *buf;
size_t len, off; size_t len, off;
u_int xx; u_int xx, i;
GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx); GRID_DEBUG(gd, "px=%u, py=%u, nx=%u", px, py, nx);
@ -522,10 +522,11 @@ grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
} }
gu = grid_peek_utf8(gd, xx, py); gu = grid_peek_utf8(gd, xx, py);
memcpy(buf + off, gu->data, UTF8_SIZE); for (i = 0; i < UTF8_SIZE; i++) {
off += UTF8_SIZE; if (gu->data[i] == 0xff)
while (off > 0 && ((u_char) buf[off]) == 0xff) break;
off--; buf[off++] = gu->data[i];
}
} else { } else {
while (len < off + 2) { while (len < off + 2) {
buf = xrealloc(buf, 2, len); buf = xrealloc(buf, 2, len);
@ -535,7 +536,9 @@ grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
buf[off++] = gc->data; buf[off++] = gc->data;
} }
} }
while (off > 0 && buf[off - 1] == ' ')
off--;
buf[off] = '\0'; buf[off] = '\0';
return (buf); return (buf);
} }

View File

@ -1,4 +1,4 @@
/* $OpenBSD: server.c,v 1.2 2009/06/24 17:36:15 nicm Exp $ */ /* $OpenBSD: server.c,v 1.3 2009/06/24 22:49:56 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -973,7 +973,7 @@ server_check_window_content(
return (0); return (0);
if (session_alert_has_window(s, w, WINDOW_CONTENT)) if (session_alert_has_window(s, w, WINDOW_CONTENT))
return (0); return (0);
if ((found = window_pane_search(wp, ptr)) == NULL) if ((found = window_pane_search(wp, ptr, NULL)) == NULL)
return (0); return (0);
session_alert_add(s, w, WINDOW_CONTENT); session_alert_add(s, w, WINDOW_CONTENT);
xfree(found); xfree(found);

9
tmux.h
View File

@ -1,4 +1,4 @@
/* $OpenBSD: tmux.h,v 1.9 2009/06/24 22:04:18 nicm Exp $ */ /* $OpenBSD: tmux.h,v 1.10 2009/06/24 22:49:56 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -1577,7 +1577,8 @@ void window_pane_parse(struct window_pane *);
void window_pane_key(struct window_pane *, struct client *, int); void window_pane_key(struct window_pane *, struct client *, int);
void window_pane_mouse(struct window_pane *, void window_pane_mouse(struct window_pane *,
struct client *, u_char, u_char, u_char); struct client *, u_char, u_char, u_char);
char *window_pane_search(struct window_pane *, const char *); char *window_pane_search(
struct window_pane *, const char *, u_int *);
/* layout.c */ /* layout.c */
const char * layout_name(struct window *); const char * layout_name(struct window *);
@ -1647,10 +1648,6 @@ int session_last(struct session *);
void utf8_build(void); void utf8_build(void);
int utf8_width(const u_char *); int utf8_width(const u_char *);
/* util.c */
char *section_string(char *, size_t, size_t, size_t);
void clean_string(const char *, char *, size_t);
/* osdep-*.c */ /* osdep-*.c */
char *osdep_get_name(int, char *); char *osdep_get_name(int, char *);

52
util.c
View File

@ -1,52 +0,0 @@
/* $OpenBSD: util.c,v 1.2 2009/06/03 19:37:27 nicm Exp $ */
/*
* Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include "tmux.h"
/* Return a section of a string around a point. */
char *
section_string(char *buf, size_t len, size_t sectoff, size_t sectlen)
{
char *s;
size_t first, last;
if (len <= sectlen) {
first = 0;
last = len;
} else if (sectoff < sectlen / 2) {
first = 0;
last = sectlen;
} else if (sectoff + sectlen / 2 > len) {
last = len;
first = last - sectlen;
} else {
first = sectoff - sectlen / 2;
last = first + sectlen;
}
if (last - first > 3 && first != 0)
first += 3;
if (last - first > 3 && last != len)
last -= 3;
xasprintf(&s, "%s%.*s%s", first == 0 ? "" : "...",
(int) (last - first), buf + first, last == len ? "" : "...");
return (s);
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: window.c,v 1.4 2009/06/24 22:04:18 nicm Exp $ */ /* $OpenBSD: window.c,v 1.5 2009/06/24 22:49:56 nicm Exp $ */
/* /*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> * Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@ -21,6 +21,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <fnmatch.h>
#include <signal.h> #include <signal.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
@ -602,23 +603,26 @@ window_pane_mouse(
} }
char * char *
window_pane_search(struct window_pane *wp, const char *searchstr) window_pane_search(struct window_pane *wp, const char *searchstr, u_int *lineno)
{ {
struct screen *s = &wp->base; struct screen *s = &wp->base;
char *line, *ptr; char *newsearchstr, *line, *msg;
u_int i; u_int i;
ptr = NULL; msg = NULL;
xasprintf(&newsearchstr, "*%s*", searchstr);
for (i = 0; i < screen_size_y(s); i++) { for (i = 0; i < screen_size_y(s); i++) {
line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s)); line = grid_view_string_cells(s->grid, 0, i, screen_size_x(s));
log_debug("XXX %s", line); if (fnmatch(newsearchstr, line, 0) == 0) {
if ((ptr = strstr(line, searchstr)) != NULL) msg = line;
break; if (lineno != NULL)
*lineno = i;
break;
}
xfree(line); xfree(line);
} }
if (ptr != NULL) {
ptr = section_string(line, strlen(ptr), ptr - line, 40); xfree(newsearchstr);
xfree(line); return (msg);
}
return (ptr);
} }