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.

Also display the line number and the entire line in the results, and lose the
nasty section_string function and the now empty util.c file.

Initially from Tiago Cunha.
This commit is contained in:
Nicholas Marriott 2009-06-24 22:49:56 +00:00
parent 096cbf2ea5
commit 7e796dea03
8 changed files with 44 additions and 83 deletions

View File

@ -32,7 +32,7 @@ SRCS= arg.c attributes.c buffer-poll.c buffer.c cfg.c client-fn.c \
mode-key.c names.c options-cmd.c options.c paste.c procname.c \ mode-key.c names.c options-cmd.c options.c paste.c procname.c \
resize.c screen-redraw.c screen-write.c screen.c server-fn.c \ resize.c screen-redraw.c screen-write.c screen.c server-fn.c \
server-msg.c server.c session.c status.c tmux.c tty-keys.c tty-term.c \ server-msg.c server.c session.c status.c tmux.c tty-keys.c tty-term.c \
tty-write.c tty.c utf8.c util.c window-choose.c window-clock.c \ tty-write.c tty.c utf8.c window-choose.c window-clock.c \
window-copy.c window-more.c window-scroll.c window.c xmalloc.c window-copy.c window-more.c window-scroll.c window.c xmalloc.c
CFLAGS+= -Wno-long-long -Wall -W -Wnested-externs -Wformat=2 CFLAGS+= -Wno-long-long -Wall -W -Wnested-externs -Wformat=2

View File

@ -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);

15
grid.c
View File

@ -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

@ -959,7 +959,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);

6
tmux.1
View File

@ -664,7 +664,9 @@ Move down a pane.
.Ar match-string .Ar match-string
.Xc .Xc
.D1 (alias: Ic findw ) .D1 (alias: Ic findw )
Search for Search for the
.Xr fnmatch 3
pattern
.Ar match-string .Ar match-string
in window names, titles, and visible content (but not history). in window names, titles, and visible content (but not history).
If only one window is matched, it'll be automatically selected, otherwise a If only one window is matched, it'll be automatically selected, otherwise a
@ -1316,6 +1318,8 @@ Windows with activity are highlighted in the status line.
.Xc .Xc
Monitor content in the window. Monitor content in the window.
When When
.Xr fnmatch 3
pattern
.Ar match-string .Ar match-string
appears in the window, it is highlighted in the status line. appears in the window, it is highlighted in the status line.
.It Xo Ic remain-on-exit .It Xo Ic remain-on-exit

7
tmux.h
View File

@ -1456,7 +1456,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 *);
@ -1526,10 +1527,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);
/* procname.c */ /* procname.c */
char *get_proc_name(int, char *); char *get_proc_name(int, char *);

52
util.c
View File

@ -1,52 +0,0 @@
/* $OpenBSD$ */
/*
* 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

@ -21,6 +21,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <fnmatch.h>
#include <paths.h> #include <paths.h>
#include <signal.h> #include <signal.h>
#include <stdint.h> #include <stdint.h>
@ -588,23 +589,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);
} }