cmd-find-window.c revision 1.1.1.8 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.1.3 christos #include <stdlib.h>
22 1.1 jmmv
23 1.1 jmmv #include "tmux.h"
24 1.1 jmmv
25 1.1 jmmv /*
26 1.1 jmmv * Find window containing text.
27 1.1 jmmv */
28 1.1 jmmv
29 1.1.1.7 christos static enum cmd_retval cmd_find_window_exec(struct cmd *, struct cmdq_item *);
30 1.1 jmmv
31 1.1 jmmv const struct cmd_entry cmd_find_window_entry = {
32 1.1.1.6 christos .name = "find-window",
33 1.1.1.6 christos .alias = "findw",
34 1.1.1.6 christos
35 1.1.1.8 christos .args = { "CNt:T", 1, 1 },
36 1.1.1.8 christos .usage = "[-CNT] " CMD_TARGET_PANE_USAGE " match-string",
37 1.1.1.6 christos
38 1.1.1.8 christos .target = { 't', CMD_FIND_PANE, 0 },
39 1.1.1.6 christos
40 1.1.1.6 christos .flags = 0,
41 1.1.1.6 christos .exec = cmd_find_window_exec
42 1.1 jmmv };
43 1.1 jmmv
44 1.1.1.7 christos static enum cmd_retval
45 1.1.1.7 christos cmd_find_window_exec(struct cmd *self, struct cmdq_item *item)
46 1.1 jmmv {
47 1.1.1.8 christos struct args *args = self->args, *new_args;
48 1.1.1.8 christos struct window_pane *wp = item->target.wp;
49 1.1.1.8 christos const char *s = args->argv[0];
50 1.1.1.8 christos char *filter, *argv = { NULL };
51 1.1.1.8 christos int C, N, T;
52 1.1.1.8 christos
53 1.1.1.8 christos C = args_has(args, 'C');
54 1.1.1.8 christos N = args_has(args, 'N');
55 1.1.1.8 christos T = args_has(args, 'T');
56 1.1.1.8 christos
57 1.1.1.8 christos if (!C && !N && !T)
58 1.1.1.8 christos C = N = T = 1;
59 1.1.1.8 christos
60 1.1.1.8 christos if (C && N && T) {
61 1.1.1.8 christos xasprintf(&filter,
62 1.1.1.8 christos "#{||:"
63 1.1.1.8 christos "#{C:%s},#{||:#{m:*%s*,#{window_name}},"
64 1.1.1.8 christos "#{m:*%s*,#{pane_title}}}}",
65 1.1.1.8 christos s, s, s);
66 1.1.1.8 christos } else if (C && N) {
67 1.1.1.8 christos xasprintf(&filter,
68 1.1.1.8 christos "#{||:#{C:%s},#{m:*%s*,#{window_name}}}",
69 1.1.1.8 christos s, s);
70 1.1.1.8 christos } else if (C && T) {
71 1.1.1.8 christos xasprintf(&filter,
72 1.1.1.8 christos "#{||:#{C:%s},#{m:*%s*,#{pane_title}}}",
73 1.1.1.8 christos s, s);
74 1.1.1.8 christos } else if (N && T) {
75 1.1.1.8 christos xasprintf(&filter,
76 1.1.1.8 christos "#{||:#{m:*%s*,#{window_name}},#{m:*%s*,#{pane_title}}}",
77 1.1.1.8 christos s, s);
78 1.1.1.8 christos } else if (C)
79 1.1.1.8 christos xasprintf(&filter, "#{C:%s}", s);
80 1.1.1.8 christos else if (N)
81 1.1.1.8 christos xasprintf(&filter, "#{m:*%s*,#{window_name}}", s);
82 1.1.1.8 christos else
83 1.1.1.8 christos xasprintf(&filter, "#{m:*%s*,#{pane_title}}", s);
84 1.1 jmmv
85 1.1.1.8 christos new_args = args_parse("", 1, &argv);
86 1.1.1.8 christos args_set(new_args, 'f', filter);
87 1.1 jmmv
88 1.1.1.8 christos window_pane_set_mode(wp, &window_tree_mode, &item->target, new_args);
89 1.1.1.3 christos
90 1.1.1.8 christos args_free(new_args);
91 1.1.1.8 christos free(filter);
92 1.1.1.8 christos
93 1.1.1.8 christos return (CMD_RETURN_NORMAL);
94 1.1 jmmv }
95