2009-06-01 22:58:49 +00:00
|
|
|
/* $OpenBSD$ */
|
|
|
|
|
|
|
|
/*
|
2016-01-19 15:59:12 +00:00
|
|
|
* Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
|
2009-06-01 22:58:49 +00:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2009-06-24 22:49:56 +00:00
|
|
|
#include <fnmatch.h>
|
2012-07-10 11:53:01 +00:00
|
|
|
#include <stdlib.h>
|
2009-06-01 22:58:49 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "tmux.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find window containing text.
|
|
|
|
*/
|
|
|
|
|
2014-10-20 23:35:28 +00:00
|
|
|
#define FIND_WINDOW_TEMPLATE \
|
|
|
|
"#{window_index}: #{window_name} " \
|
|
|
|
"[#{window_width}x#{window_height}] " \
|
|
|
|
"(#{window_panes} panes) #{window_find_matches}"
|
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static enum cmd_retval cmd_find_window_exec(struct cmd *, struct cmdq_item *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2016-10-16 19:04:05 +00:00
|
|
|
static void cmd_find_window_callback(struct window_choose_data *);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2012-03-20 17:09:48 +00:00
|
|
|
/* Flags for determining matching behavior. */
|
|
|
|
#define CMD_FIND_WINDOW_BY_TITLE 0x1
|
|
|
|
#define CMD_FIND_WINDOW_BY_CONTENT 0x2
|
|
|
|
#define CMD_FIND_WINDOW_BY_NAME 0x4
|
|
|
|
|
|
|
|
#define CMD_FIND_WINDOW_ALL \
|
|
|
|
(CMD_FIND_WINDOW_BY_TITLE | \
|
|
|
|
CMD_FIND_WINDOW_BY_CONTENT | \
|
|
|
|
CMD_FIND_WINDOW_BY_NAME)
|
|
|
|
|
2009-06-01 22:58:49 +00:00
|
|
|
const struct cmd_entry cmd_find_window_entry = {
|
2015-12-13 21:53:57 +00:00
|
|
|
.name = "find-window",
|
|
|
|
.alias = "findw",
|
|
|
|
|
|
|
|
.args = { "F:CNt:T", 1, 4 },
|
|
|
|
.usage = "[-CNT] [-F format] " CMD_TARGET_WINDOW_USAGE " match-string",
|
|
|
|
|
2015-12-14 00:31:54 +00:00
|
|
|
.tflag = CMD_WINDOW,
|
|
|
|
|
|
|
|
.flags = 0,
|
2015-12-13 21:53:57 +00:00
|
|
|
.exec = cmd_find_window_exec
|
2009-06-01 22:58:49 +00:00
|
|
|
};
|
|
|
|
|
2012-09-03 12:20:17 +00:00
|
|
|
struct cmd_find_window_data {
|
|
|
|
struct winlink *wl;
|
|
|
|
char *list_ctx;
|
|
|
|
u_int pane_id;
|
2015-05-07 07:59:52 +00:00
|
|
|
TAILQ_ENTRY(cmd_find_window_data) entry;
|
2012-09-03 12:20:17 +00:00
|
|
|
};
|
2015-05-07 07:59:52 +00:00
|
|
|
TAILQ_HEAD(cmd_find_window_list, cmd_find_window_data);
|
2012-09-03 12:20:17 +00:00
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static u_int cmd_find_window_match_flags(struct args *);
|
|
|
|
static void cmd_find_window_match(struct cmd_find_window_list *, int,
|
|
|
|
struct winlink *, const char *, const char *);
|
2012-09-03 12:20:17 +00:00
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static u_int
|
2012-03-20 17:09:48 +00:00
|
|
|
cmd_find_window_match_flags(struct args *args)
|
|
|
|
{
|
|
|
|
u_int match_flags = 0;
|
|
|
|
|
|
|
|
/* Turn on flags based on the options. */
|
|
|
|
if (args_has(args, 'T'))
|
|
|
|
match_flags |= CMD_FIND_WINDOW_BY_TITLE;
|
|
|
|
if (args_has(args, 'C'))
|
|
|
|
match_flags |= CMD_FIND_WINDOW_BY_CONTENT;
|
|
|
|
if (args_has(args, 'N'))
|
|
|
|
match_flags |= CMD_FIND_WINDOW_BY_NAME;
|
|
|
|
|
|
|
|
/* If none of the flags were set, default to matching anything. */
|
|
|
|
if (match_flags == 0)
|
|
|
|
match_flags = CMD_FIND_WINDOW_ALL;
|
|
|
|
|
2012-04-01 20:53:47 +00:00
|
|
|
return (match_flags);
|
2012-03-20 17:09:48 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static void
|
2015-05-07 07:59:52 +00:00
|
|
|
cmd_find_window_match(struct cmd_find_window_list *find_list,
|
2014-04-17 14:45:49 +00:00
|
|
|
int match_flags, struct winlink *wl, const char *str,
|
|
|
|
const char *searchstr)
|
2012-09-03 12:20:17 +00:00
|
|
|
{
|
2015-05-07 07:59:52 +00:00
|
|
|
struct cmd_find_window_data *find_data;
|
2012-09-03 12:20:17 +00:00
|
|
|
struct window_pane *wp;
|
|
|
|
u_int i, line;
|
|
|
|
char *sres;
|
|
|
|
|
2015-05-07 07:59:52 +00:00
|
|
|
find_data = xcalloc(1, sizeof *find_data);
|
2012-09-03 12:20:17 +00:00
|
|
|
|
|
|
|
i = 0;
|
|
|
|
TAILQ_FOREACH(wp, &wl->window->panes, entry) {
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if ((match_flags & CMD_FIND_WINDOW_BY_NAME) &&
|
|
|
|
fnmatch(searchstr, wl->window->name, 0) == 0) {
|
2015-05-07 07:59:52 +00:00
|
|
|
find_data->list_ctx = xstrdup("");
|
2012-09-03 12:20:17 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((match_flags & CMD_FIND_WINDOW_BY_TITLE) &&
|
|
|
|
fnmatch(searchstr, wp->base.title, 0) == 0) {
|
2015-05-07 07:59:52 +00:00
|
|
|
xasprintf(&find_data->list_ctx,
|
2012-09-03 12:20:17 +00:00
|
|
|
"pane %u title: \"%s\"", i - 1, wp->base.title);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (match_flags & CMD_FIND_WINDOW_BY_CONTENT &&
|
|
|
|
(sres = window_pane_search(wp, str, &line)) != NULL) {
|
2015-05-07 07:59:52 +00:00
|
|
|
xasprintf(&find_data->list_ctx,
|
2012-09-03 12:20:17 +00:00
|
|
|
"pane %u line %u: \"%s\"", i - 1, line + 1, sres);
|
|
|
|
free(sres);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-05-07 07:59:52 +00:00
|
|
|
|
|
|
|
if (find_data->list_ctx != NULL) {
|
|
|
|
find_data->wl = wl;
|
|
|
|
find_data->pane_id = i - 1;
|
|
|
|
TAILQ_INSERT_TAIL(find_list, find_data, entry);
|
|
|
|
} else
|
|
|
|
free(find_data);
|
2012-09-03 12:20:17 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static enum cmd_retval
|
2016-10-16 19:04:05 +00:00
|
|
|
cmd_find_window_exec(struct cmd *self, struct cmdq_item *item)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2011-01-04 00:42:46 +00:00
|
|
|
struct args *args = self->args;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct client *c = item->state.c;
|
2012-06-25 14:08:55 +00:00
|
|
|
struct window_choose_data *cdata;
|
2016-10-16 19:04:05 +00:00
|
|
|
struct session *s = item->state.tflag.s;
|
|
|
|
struct winlink *wl = item->state.tflag.wl, *wm;
|
2015-05-07 07:59:52 +00:00
|
|
|
struct cmd_find_window_list find_list;
|
|
|
|
struct cmd_find_window_data *find_data;
|
|
|
|
struct cmd_find_window_data *find_data1;
|
2012-09-03 12:20:17 +00:00
|
|
|
char *str, *searchstr;
|
2012-05-22 11:35:37 +00:00
|
|
|
const char *template;
|
2012-09-03 12:20:17 +00:00
|
|
|
u_int i, match_flags;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2015-12-13 14:32:38 +00:00
|
|
|
if (c == NULL) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "no client available");
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2012-05-22 11:35:37 +00:00
|
|
|
if ((template = args_get(args, 'F')) == NULL)
|
2012-08-14 08:51:53 +00:00
|
|
|
template = FIND_WINDOW_TEMPLATE;
|
2012-05-22 11:35:37 +00:00
|
|
|
|
2012-03-20 17:09:48 +00:00
|
|
|
match_flags = cmd_find_window_match_flags(args);
|
2011-01-04 00:42:46 +00:00
|
|
|
str = args->argv[0];
|
|
|
|
|
2015-05-07 07:59:52 +00:00
|
|
|
TAILQ_INIT(&find_list);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2011-01-04 00:42:46 +00:00
|
|
|
xasprintf(&searchstr, "*%s*", str);
|
2012-09-03 12:20:17 +00:00
|
|
|
RB_FOREACH(wm, winlinks, &s->windows)
|
2014-10-22 23:18:53 +00:00
|
|
|
cmd_find_window_match(&find_list, match_flags, wm, str, searchstr);
|
2012-07-10 11:53:01 +00:00
|
|
|
free(searchstr);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2015-05-07 07:59:52 +00:00
|
|
|
if (TAILQ_EMPTY(&find_list)) {
|
2016-10-16 19:04:05 +00:00
|
|
|
cmdq_error(item, "no windows matching: %s", str);
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_ERROR);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2015-05-07 07:59:52 +00:00
|
|
|
if (TAILQ_NEXT(TAILQ_FIRST(&find_list), entry) == NULL) {
|
|
|
|
if (session_select(s, TAILQ_FIRST(&find_list)->wl->idx) == 0)
|
2009-06-01 22:58:49 +00:00
|
|
|
server_redraw_session(s);
|
|
|
|
recalculate_sizes();
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
|
|
|
|
goto out;
|
|
|
|
|
2015-05-07 07:59:52 +00:00
|
|
|
i = 0;
|
|
|
|
TAILQ_FOREACH(find_data, &find_list, entry) {
|
2013-03-21 16:09:59 +00:00
|
|
|
cdata = window_choose_data_create(TREE_OTHER, c, c->session);
|
2015-05-07 07:59:52 +00:00
|
|
|
cdata->idx = find_data->wl->idx;
|
|
|
|
cdata->wl = find_data->wl;
|
2012-05-22 11:35:37 +00:00
|
|
|
|
2012-06-25 14:08:55 +00:00
|
|
|
cdata->ft_template = xstrdup(template);
|
2015-05-07 07:59:52 +00:00
|
|
|
cdata->pane_id = find_data->pane_id;
|
2012-09-03 12:20:17 +00:00
|
|
|
|
2012-06-25 14:08:55 +00:00
|
|
|
format_add(cdata->ft, "line", "%u", i);
|
|
|
|
format_add(cdata->ft, "window_find_matches", "%s",
|
2015-05-07 07:59:52 +00:00
|
|
|
find_data->list_ctx);
|
|
|
|
format_defaults(cdata->ft, NULL, s, find_data->wl, NULL);
|
2012-05-22 11:35:37 +00:00
|
|
|
|
2012-06-25 14:08:55 +00:00
|
|
|
window_choose_add(wl->window->active, cdata);
|
2015-05-07 07:59:52 +00:00
|
|
|
|
|
|
|
i++;
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 16:09:59 +00:00
|
|
|
window_choose_ready(wl->window->active, 0, cmd_find_window_callback);
|
2009-06-01 22:58:49 +00:00
|
|
|
|
|
|
|
out:
|
2015-05-07 07:59:52 +00:00
|
|
|
TAILQ_FOREACH_SAFE(find_data, &find_list, entry, find_data1) {
|
|
|
|
free(find_data->list_ctx);
|
|
|
|
TAILQ_REMOVE(&find_list, find_data, entry);
|
|
|
|
free(find_data);
|
|
|
|
}
|
2012-07-11 07:10:15 +00:00
|
|
|
return (CMD_RETURN_NORMAL);
|
2009-06-01 22:58:49 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 21:51:39 +00:00
|
|
|
static void
|
2012-06-25 14:08:55 +00:00
|
|
|
cmd_find_window_callback(struct window_choose_data *cdata)
|
2009-06-01 22:58:49 +00:00
|
|
|
{
|
2012-09-03 12:20:17 +00:00
|
|
|
struct session *s;
|
|
|
|
struct window_pane *wp;
|
2009-06-01 22:58:49 +00:00
|
|
|
|
2012-06-25 14:08:55 +00:00
|
|
|
if (cdata == NULL)
|
2010-12-20 00:03:55 +00:00
|
|
|
return;
|
2012-06-25 14:08:55 +00:00
|
|
|
|
2013-03-21 16:09:59 +00:00
|
|
|
s = cdata->start_session;
|
2010-12-20 00:03:55 +00:00
|
|
|
if (!session_alive(s))
|
|
|
|
return;
|
|
|
|
|
2012-09-03 12:20:17 +00:00
|
|
|
wp = window_pane_at_index(cdata->wl->window, cdata->pane_id);
|
|
|
|
if (wp != NULL && window_pane_visible(wp))
|
|
|
|
window_set_active_pane(cdata->wl->window, wp);
|
|
|
|
|
2012-06-25 14:08:55 +00:00
|
|
|
if (session_select(s, cdata->idx) == 0) {
|
2010-12-20 00:03:55 +00:00
|
|
|
server_redraw_session(s);
|
2009-06-01 22:58:49 +00:00
|
|
|
recalculate_sizes();
|
|
|
|
}
|
|
|
|
}
|