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