1 /* $OpenBSD: cmd-find-window.c,v 1.56 2023/12/27 20:42:01 nicm Exp $ */
2
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
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 <stdlib.h>
22
23 #include "tmux.h"
24
25 /*
26 * Find window containing text.
27 */
28
29 static enum cmd_retval cmd_find_window_exec(struct cmd *, struct cmdq_item *);
30
31 const struct cmd_entry cmd_find_window_entry = {
32 .name = "find-window",
33 .alias = "findw",
34
35 .args = { "CiNrt:TZ", 1, 1, NULL },
36 .usage = "[-CiNrTZ] " CMD_TARGET_PANE_USAGE " match-string",
37
38 .target = { 't', CMD_FIND_PANE, 0 },
39
40 .flags = 0,
41 .exec = cmd_find_window_exec
42 };
43
44 static enum cmd_retval
cmd_find_window_exec(struct cmd * self,struct cmdq_item * item)45 cmd_find_window_exec(struct cmd *self, struct cmdq_item *item)
46 {
47 struct args *args = cmd_get_args(self), *new_args;
48 struct cmd_find_state *target = cmdq_get_target(item);
49 struct window_pane *wp = target->wp;
50 const char *s = args_string(args, 0), *suffix = "";
51 const char *star = "*";
52 struct args_value *filter;
53 int C, N, T;
54
55 C = args_has(args, 'C');
56 N = args_has(args, 'N');
57 T = args_has(args, 'T');
58
59 if (args_has(args, 'r'))
60 star = "";
61 if (args_has(args, 'r') && args_has(args, 'i'))
62 suffix = "/ri";
63 else if (args_has(args, 'r'))
64 suffix = "/r";
65 else if (args_has(args, 'i'))
66 suffix = "/i";
67
68 if (!C && !N && !T)
69 C = N = T = 1;
70
71 filter = xcalloc(1, sizeof *filter);
72 filter->type = ARGS_STRING;
73
74 if (C && N && T) {
75 xasprintf(&filter->string,
76 "#{||:"
77 "#{C%s:%s},#{||:#{m%s:%s%s%s,#{window_name}},"
78 "#{m%s:%s%s%s,#{pane_title}}}}",
79 suffix, s, suffix, star, s, star, suffix, star, s, star);
80 } else if (C && N) {
81 xasprintf(&filter->string,
82 "#{||:#{C%s:%s},#{m%s:%s%s%s,#{window_name}}}",
83 suffix, s, suffix, star, s, star);
84 } else if (C && T) {
85 xasprintf(&filter->string,
86 "#{||:#{C%s:%s},#{m%s:%s%s%s,#{pane_title}}}",
87 suffix, s, suffix, star, s, star);
88 } else if (N && T) {
89 xasprintf(&filter->string,
90 "#{||:#{m%s:%s%s%s,#{window_name}},"
91 "#{m%s:%s%s%s,#{pane_title}}}",
92 suffix, star, s, star, suffix, star, s, star);
93 } else if (C) {
94 xasprintf(&filter->string,
95 "#{C%s:%s}",
96 suffix, s);
97 } else if (N) {
98 xasprintf(&filter->string,
99 "#{m%s:%s%s%s,#{window_name}}",
100 suffix, star, s, star);
101 } else {
102 xasprintf(&filter->string,
103 "#{m%s:%s%s%s,#{pane_title}}",
104 suffix, star, s, star);
105 }
106
107 new_args = args_create();
108 if (args_has(args, 'Z'))
109 args_set(new_args, 'Z', NULL, 0);
110 args_set(new_args, 'f', filter, 0);
111
112 window_pane_set_mode(wp, NULL, &window_tree_mode, target, new_args);
113 args_free(new_args);
114
115 return (CMD_RETURN_NORMAL);
116 }
117