mirror of
https://github.com/tmate-io/tmate.git
synced 2024-12-23 23:29:15 +01:00
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:
parent
096cbf2ea5
commit
7e796dea03
2
Makefile
2
Makefile
@ -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
|
||||
|
@ -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
15
grid.c
@ -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);
|
||||
}
|
||||
|
2
server.c
2
server.c
@ -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);
|
||||
|
6
tmux.1
6
tmux.1
@ -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
|
||||
|
7
tmux.h
7
tmux.h
@ -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 *);
|
||||
|
||||
|
52
util.c
52
util.c
@ -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);
|
||||
}
|
26
window.c
26
window.c
@ -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;
|
||||
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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user