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>
@ -18,6 +18,7 @@
#include <sys/types.h>
#include <fnmatch.h>
#include <string.h>
#include "tmux.h"
@ -58,8 +59,8 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
struct window_pane *wp;
ARRAY_DECL(, u_int) list_idx;
ARRAY_DECL(, char *) list_ctx;
char *sres, *sctx;
u_int i;
char *sres, *sctx, *searchstr;
u_int i, line;
if (ctx->curclient == NULL) {
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_ctx);
xasprintf(&searchstr, "*%s*", data->arg);
RB_FOREACH(wm, winlinks, &s->windows) {
i = 0;
TAILQ_FOREACH(wp, &wm->window->panes, entry) {
i++;
if (strstr(wm->window->name, data->arg) != NULL)
if (fnmatch(searchstr, wm->window->name, 0) == 0)
sctx = xstrdup("");
else {
sres = window_pane_search(wp, data->arg);
sres = window_pane_search(wp, data->arg, &line);
if (sres == NULL &&
strstr(wp->base.title, data->arg) == NULL)
fnmatch(searchstr, wp->base.title, 0) != 0)
continue;
if (sres == NULL) {
@ -91,7 +93,9 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
"pane %u title: \"%s\"", i - 1,
wp->base.title);
} else {
xasprintf(&sctx, "\"%s\"", sres);
xasprintf(&sctx,
"pane %u line %u: \"%s\"", i - 1,
line + 1, sres);
xfree(sres);
}
}
@ -100,6 +104,7 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
ARRAY_ADD(&list_ctx, sctx);
}
}
xfree(searchstr);
if (ARRAY_LENGTH(&list_idx) == 0) {
ctx->error(ctx, "no windows matching: %s", data->arg);

15
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>
@ -502,7 +502,7 @@ grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
const struct grid_utf8 *gu;
char *buf;
size_t len, off;
u_int xx;
u_int xx, i;
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);
memcpy(buf + off, gu->data, UTF8_SIZE);
off += UTF8_SIZE;
while (off > 0 && ((u_char) buf[off]) == 0xff)
off--;
for (i = 0; i < UTF8_SIZE; i++) {
if (gu->data[i] == 0xff)
break;
buf[off++] = gu->data[i];
}
} else {
while (len < off + 2) {
buf = xrealloc(buf, 2, len);
@ -536,6 +537,8 @@ grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx)
}
}
while (off > 0 && buf[off - 1] == ' ')
off--;
buf[off] = '\0';
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>
@ -973,7 +973,7 @@ server_check_window_content(
return (0);
if (session_alert_has_window(s, w, WINDOW_CONTENT))
return (0);
if ((found = window_pane_search(wp, ptr)) == NULL)
if ((found = window_pane_search(wp, ptr, NULL)) == NULL)
return (0);
session_alert_add(s, w, WINDOW_CONTENT);
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>
@ -1577,7 +1577,8 @@ void window_pane_parse(struct window_pane *);
void window_pane_key(struct window_pane *, struct client *, int);
void window_pane_mouse(struct window_pane *,
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 */
const char * layout_name(struct window *);
@ -1647,10 +1648,6 @@ int session_last(struct session *);
void utf8_build(void);
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 */
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>
@ -21,6 +21,7 @@
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <signal.h>
#include <stdint.h>
#include <stdlib.h>
@ -602,23 +603,26 @@ window_pane_mouse(
}
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;
char *line, *ptr;
char *newsearchstr, *line, *msg;
u_int i;
ptr = NULL;
msg = NULL;
xasprintf(&newsearchstr, "*%s*", searchstr);
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)
if (fnmatch(newsearchstr, line, 0) == 0) {
msg = line;
if (lineno != NULL)
*lineno = i;
break;
}
xfree(line);
}
if (ptr != NULL) {
ptr = section_string(line, strlen(ptr), ptr - line, 40);
xfree(line);
}
return (ptr);
xfree(newsearchstr);
return (msg);
}