xref: /openbsd-src/usr.bin/tmux/cmd-find-window.c (revision ac9b4aacc1da35008afea06a5d23c2f2dea9b93e)
1 /* $OpenBSD: cmd-find-window.c,v 1.19 2012/09/03 12:20:17 nicm Exp $ */
2 
3 /*
4  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 
21 #include <fnmatch.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include "tmux.h"
26 
27 /*
28  * Find window containing text.
29  */
30 
31 enum cmd_retval	 cmd_find_window_exec(struct cmd *, struct cmd_ctx *);
32 
33 void	cmd_find_window_callback(struct window_choose_data *);
34 void	cmd_find_window_free(struct window_choose_data *);
35 
36 /* Flags for determining matching behavior. */
37 #define CMD_FIND_WINDOW_BY_TITLE   0x1
38 #define CMD_FIND_WINDOW_BY_CONTENT 0x2
39 #define CMD_FIND_WINDOW_BY_NAME    0x4
40 
41 #define CMD_FIND_WINDOW_ALL		\
42 	(CMD_FIND_WINDOW_BY_TITLE |	\
43 	 CMD_FIND_WINDOW_BY_CONTENT |	\
44 	 CMD_FIND_WINDOW_BY_NAME)
45 
46 const struct cmd_entry cmd_find_window_entry = {
47 	"find-window", "findw",
48 	"F:CNt:T", 1, 4,
49 	"[-CNT] [-F format] " CMD_TARGET_WINDOW_USAGE " match-string",
50 	0,
51 	NULL,
52 	NULL,
53 	cmd_find_window_exec
54 };
55 
56 struct cmd_find_window_data {
57 	struct winlink	*wl;
58 	char		*list_ctx;
59 	u_int		 pane_id;
60 };
61 ARRAY_DECL(cmd_find_window_data_list, struct cmd_find_window_data);
62 
63 u_int	cmd_find_window_match_flags(struct args *);
64 void	cmd_find_window_match(struct cmd_find_window_data_list *, int,
65 	    struct winlink *, const char *, const char *);
66 
67 u_int
68 cmd_find_window_match_flags(struct args *args)
69 {
70 	u_int	match_flags = 0;
71 
72 	/* Turn on flags based on the options. */
73 	if (args_has(args, 'T'))
74 		match_flags |= CMD_FIND_WINDOW_BY_TITLE;
75 	if (args_has(args, 'C'))
76 		match_flags |= CMD_FIND_WINDOW_BY_CONTENT;
77 	if (args_has(args, 'N'))
78 		match_flags |= CMD_FIND_WINDOW_BY_NAME;
79 
80 	/* If none of the flags were set, default to matching anything. */
81 	if (match_flags == 0)
82 		match_flags = CMD_FIND_WINDOW_ALL;
83 
84 	return (match_flags);
85 }
86 
87 void
88 cmd_find_window_match(struct cmd_find_window_data_list *find_list,
89     int match_flags, struct winlink *wl, const char *str, const char *searchstr)
90 {
91 	struct cmd_find_window_data	 find_data;
92 	struct window_pane		*wp;
93 	u_int				 i, line;
94 	char				*sres;
95 
96 	memset(&find_data, 0, sizeof find_data);
97 
98 	i = 0;
99 	TAILQ_FOREACH(wp, &wl->window->panes, entry) {
100 		i++;
101 
102 		if ((match_flags & CMD_FIND_WINDOW_BY_NAME) &&
103 		    fnmatch(searchstr, wl->window->name, 0) == 0) {
104 			find_data.list_ctx = xstrdup("");
105 			break;
106 		}
107 
108 		if ((match_flags & CMD_FIND_WINDOW_BY_TITLE) &&
109 		    fnmatch(searchstr, wp->base.title, 0) == 0) {
110 			xasprintf(&find_data.list_ctx,
111 			    "pane %u title: \"%s\"", i - 1, wp->base.title);
112 			break;
113 		}
114 
115 		if (match_flags & CMD_FIND_WINDOW_BY_CONTENT &&
116 		    (sres = window_pane_search(wp, str, &line)) != NULL) {
117 			xasprintf(&find_data.list_ctx,
118 			    "pane %u line %u: \"%s\"", i - 1, line + 1, sres);
119 			free(sres);
120 			break;
121 		}
122 	}
123 	if (find_data.list_ctx != NULL) {
124 		find_data.wl = wl;
125 		find_data.pane_id = i - 1;
126 		ARRAY_ADD(find_list, find_data);
127 	}
128 }
129 
130 enum cmd_retval
131 cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
132 {
133 	struct args			*args = self->args;
134 	struct window_choose_data	*cdata;
135 	struct session			*s;
136 	struct winlink			*wl, *wm;
137 	struct cmd_find_window_data_list find_list;
138 	char				*str, *searchstr;
139 	const char			*template;
140 	u_int				 i, match_flags;
141 
142 	if (ctx->curclient == NULL) {
143 		ctx->error(ctx, "must be run interactively");
144 		return (CMD_RETURN_ERROR);
145 	}
146 	s = ctx->curclient->session;
147 
148 	if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
149 		return (CMD_RETURN_ERROR);
150 
151 	if ((template = args_get(args, 'F')) == NULL)
152 		template = FIND_WINDOW_TEMPLATE;
153 
154 	match_flags = cmd_find_window_match_flags(args);
155 	str = args->argv[0];
156 
157 	ARRAY_INIT(&find_list);
158 
159 	xasprintf(&searchstr, "*%s*", str);
160 	RB_FOREACH(wm, winlinks, &s->windows)
161 	    cmd_find_window_match (&find_list, match_flags, wm, str, searchstr);
162 	free(searchstr);
163 
164 	if (ARRAY_LENGTH(&find_list) == 0) {
165 		ctx->error(ctx, "no windows matching: %s", str);
166 		ARRAY_FREE(&find_list);
167 		return (CMD_RETURN_ERROR);
168 	}
169 
170 	if (ARRAY_LENGTH(&find_list) == 1) {
171 		if (session_select(s, ARRAY_FIRST(&find_list).wl->idx) == 0)
172 			server_redraw_session(s);
173 		recalculate_sizes();
174 		goto out;
175 	}
176 
177 	if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
178 		goto out;
179 
180 	for (i = 0; i < ARRAY_LENGTH(&find_list); i++) {
181 		wm = ARRAY_ITEM(&find_list, i).wl;
182 
183 		cdata = window_choose_data_create(ctx);
184 		cdata->idx = wm->idx;
185 		cdata->client->references++;
186 		cdata->wl = wm;
187 
188 		cdata->ft_template = xstrdup(template);
189 		cdata->pane_id = ARRAY_ITEM(&find_list, i).pane_id;
190 
191 		format_add(cdata->ft, "line", "%u", i);
192 		format_add(cdata->ft, "window_find_matches", "%s",
193 		    ARRAY_ITEM(&find_list, i).list_ctx);
194 		format_session(cdata->ft, s);
195 		format_winlink(cdata->ft, s, wm);
196 
197 		window_choose_add(wl->window->active, cdata);
198 	}
199 
200 	window_choose_ready(wl->window->active,
201 	    0, cmd_find_window_callback, cmd_find_window_free);
202 
203 out:
204 	ARRAY_FREE(&find_list);
205 	return (CMD_RETURN_NORMAL);
206 }
207 
208 void
209 cmd_find_window_callback(struct window_choose_data *cdata)
210 {
211 	struct session		*s;
212 	struct window_pane	*wp;
213 
214 	if (cdata == NULL)
215 		return;
216 
217 	s = cdata->session;
218 	if (!session_alive(s))
219 		return;
220 
221 	wp = window_pane_at_index(cdata->wl->window, cdata->pane_id);
222 	if (wp != NULL && window_pane_visible(wp))
223 		window_set_active_pane(cdata->wl->window, wp);
224 
225 	if (session_select(s, cdata->idx) == 0) {
226 		server_redraw_session(s);
227 		recalculate_sizes();
228 	}
229 }
230 
231 void
232 cmd_find_window_free(struct window_choose_data *cdata)
233 {
234 	if (cdata == NULL)
235 		return;
236 
237 	cdata->session->references--;
238 
239 	free(cdata->ft_template);
240 	format_free(cdata->ft);
241 	free(cdata);
242 }
243