diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2009-06-24 22:49:57 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2009-06-24 22:49:57 +0000 |
commit | d2c48d8397137f3a735b3c5dc72576d5acc1222e (patch) | |
tree | 74796f3af1ffed75cceadd38b265f2b7400566dc /usr.bin | |
parent | cf4d2a9f2dbc1c2c23122b99c17349f01e64f168 (diff) |
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.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/tmux/Makefile | 4 | ||||
-rw-r--r-- | usr.bin/tmux/cmd-find-window.c | 19 | ||||
-rw-r--r-- | usr.bin/tmux/grid.c | 17 | ||||
-rw-r--r-- | usr.bin/tmux/server.c | 4 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.1 | 8 | ||||
-rw-r--r-- | usr.bin/tmux/tmux.h | 9 | ||||
-rw-r--r-- | usr.bin/tmux/util.c | 52 | ||||
-rw-r--r-- | usr.bin/tmux/window.c | 28 |
8 files changed, 51 insertions, 90 deletions
diff --git a/usr.bin/tmux/Makefile b/usr.bin/tmux/Makefile index 9200a8cbfaa..456b83cd4c6 100644 --- a/usr.bin/tmux/Makefile +++ b/usr.bin/tmux/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.1 2009/06/01 22:58:49 nicm Exp $ +# $OpenBSD: Makefile,v 1.2 2009/06/24 22:49:56 nicm Exp $ .PATH: ${.CURDIR}/.. @@ -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 \ 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 \ - 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 CFLAGS+= -Wno-long-long -Wall -W -Wnested-externs -Wformat=2 diff --git a/usr.bin/tmux/cmd-find-window.c b/usr.bin/tmux/cmd-find-window.c index 772892c114d..213935ddb01 100644 --- a/usr.bin/tmux/cmd-find-window.c +++ b/usr.bin/tmux/cmd-find-window.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-find-window.c,v 1.1 2009/06/01 22:58:49 nicm 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); diff --git a/usr.bin/tmux/grid.c b/usr.bin/tmux/grid.c index b16766a92d3..fed00e693a4 100644 --- a/usr.bin/tmux/grid.c +++ b/usr.bin/tmux/grid.c @@ -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); @@ -535,7 +536,9 @@ grid_string_cells(struct grid *gd, u_int px, u_int py, u_int nx) buf[off++] = gc->data; } } - + + while (off > 0 && buf[off - 1] == ' ') + off--; buf[off] = '\0'; return (buf); } diff --git a/usr.bin/tmux/server.c b/usr.bin/tmux/server.c index d60b5f33e33..ab2a4cd8907 100644 --- a/usr.bin/tmux/server.c +++ b/usr.bin/tmux/server.c @@ -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> @@ -959,7 +959,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); diff --git a/usr.bin/tmux/tmux.1 b/usr.bin/tmux/tmux.1 index a3c6a3c6239..17e03f4a737 100644 --- a/usr.bin/tmux/tmux.1 +++ b/usr.bin/tmux/tmux.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tmux.1,v 1.15 2009/06/24 08:13:22 jmc Exp $ +.\" $OpenBSD: tmux.1,v 1.16 2009/06/24 22:49:56 nicm Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net> .\" @@ -664,7 +664,9 @@ Move down a pane. .Ar match-string .Xc .D1 (alias: Ic findw ) -Search for +Search for the +.Xr fnmatch 3 +pattern .Ar match-string in window names, titles, and visible content (but not history). 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 Monitor content in the window. When +.Xr fnmatch 3 +pattern .Ar match-string appears in the window, it is highlighted in the status line. .It Xo Ic remain-on-exit diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h index 5b7d8daa26b..969031de6a2 100644 --- a/usr.bin/tmux/tmux.h +++ b/usr.bin/tmux/tmux.h @@ -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> @@ -1456,7 +1456,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 *); @@ -1526,10 +1527,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); - /* procname.c */ char *get_proc_name(int, char *); diff --git a/usr.bin/tmux/util.c b/usr.bin/tmux/util.c deleted file mode 100644 index 65f709a128f..00000000000 --- a/usr.bin/tmux/util.c +++ /dev/null @@ -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); -} diff --git a/usr.bin/tmux/window.c b/usr.bin/tmux/window.c index 1027daa0385..33d44da61be 100644 --- a/usr.bin/tmux/window.c +++ b/usr.bin/tmux/window.c @@ -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 <paths.h> #include <signal.h> #include <stdint.h> @@ -588,23 +589,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) - break; - xfree(line); - } - if (ptr != NULL) { - ptr = section_string(line, strlen(ptr), ptr - line, 40); + if (fnmatch(newsearchstr, line, 0) == 0) { + msg = line; + if (lineno != NULL) + *lineno = i; + break; + } xfree(line); } - return (ptr); + + xfree(newsearchstr); + return (msg); } |