xref: /openbsd-src/usr.bin/tmux/cmd-find-window.c (revision e1825d0cf8799e7aad6ea6625d263620cc7969e1)
1*e1825d0cSnicm /* $OpenBSD: cmd-find-window.c,v 1.56 2023/12/27 20:42:01 nicm Exp $ */
2311827fbSnicm 
3311827fbSnicm /*
498ca8272Snicm  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
5311827fbSnicm  *
6311827fbSnicm  * Permission to use, copy, modify, and distribute this software for any
7311827fbSnicm  * purpose with or without fee is hereby granted, provided that the above
8311827fbSnicm  * copyright notice and this permission notice appear in all copies.
9311827fbSnicm  *
10311827fbSnicm  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11311827fbSnicm  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12311827fbSnicm  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13311827fbSnicm  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14311827fbSnicm  * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15311827fbSnicm  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16311827fbSnicm  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17311827fbSnicm  */
18311827fbSnicm 
19311827fbSnicm #include <sys/types.h>
20311827fbSnicm 
217d053cf9Snicm #include <stdlib.h>
22311827fbSnicm 
23311827fbSnicm #include "tmux.h"
24311827fbSnicm 
25311827fbSnicm /*
26311827fbSnicm  * Find window containing text.
27311827fbSnicm  */
28311827fbSnicm 
2968e0a7f2Snicm static enum cmd_retval	cmd_find_window_exec(struct cmd *, struct cmdq_item *);
30311827fbSnicm 
31311827fbSnicm const struct cmd_entry cmd_find_window_entry = {
32c057646bSnicm 	.name = "find-window",
33c057646bSnicm 	.alias = "findw",
34c057646bSnicm 
35a51dead1Snicm 	.args = { "CiNrt:TZ", 1, 1, NULL },
36b07e26c9Snicm 	.usage = "[-CiNrTZ] " CMD_TARGET_PANE_USAGE " match-string",
37c057646bSnicm 
38a42faf7dSnicm 	.target = { 't', CMD_FIND_PANE, 0 },
398d471e80Snicm 
408d471e80Snicm 	.flags = 0,
41c057646bSnicm 	.exec = cmd_find_window_exec
42311827fbSnicm };
43311827fbSnicm 
44dc1f0f5fSnicm static enum cmd_retval
cmd_find_window_exec(struct cmd * self,struct cmdq_item * item)4568e0a7f2Snicm cmd_find_window_exec(struct cmd *self, struct cmdq_item *item)
46311827fbSnicm {
4790d7ba38Snicm 	struct args		*args = cmd_get_args(self), *new_args;
48040343aeSnicm 	struct cmd_find_state	*target = cmdq_get_target(item);
49040343aeSnicm 	struct window_pane	*wp = target->wp;
501693b10bSnicm 	const char		*s = args_string(args, 0), *suffix = "";
51*e1825d0cSnicm 	const char		*star = "*";
52bee784faSnicm 	struct args_value	*filter;
53a42faf7dSnicm 	int			 C, N, T;
54311827fbSnicm 
55a42faf7dSnicm 	C = args_has(args, 'C');
56a42faf7dSnicm 	N = args_has(args, 'N');
57a42faf7dSnicm 	T = args_has(args, 'T');
58311827fbSnicm 
59*e1825d0cSnicm 	if (args_has(args, 'r'))
60*e1825d0cSnicm 		star = "";
61b07e26c9Snicm 	if (args_has(args, 'r') && args_has(args, 'i'))
62b07e26c9Snicm 		suffix = "/ri";
63b07e26c9Snicm 	else if (args_has(args, 'r'))
64b07e26c9Snicm 		suffix = "/r";
65b07e26c9Snicm 	else if (args_has(args, 'i'))
66b07e26c9Snicm 		suffix = "/i";
67b07e26c9Snicm 
68a42faf7dSnicm 	if (!C && !N && !T)
69a42faf7dSnicm 		C = N = T = 1;
70cc9e5b00Snicm 
71bee784faSnicm 	filter = xcalloc(1, sizeof *filter);
72bee784faSnicm 	filter->type = ARGS_STRING;
73bee784faSnicm 
74a42faf7dSnicm 	if (C && N && T) {
75bee784faSnicm 		xasprintf(&filter->string,
76a42faf7dSnicm 		    "#{||:"
77*e1825d0cSnicm 		    "#{C%s:%s},#{||:#{m%s:%s%s%s,#{window_name}},"
78*e1825d0cSnicm 		    "#{m%s:%s%s%s,#{pane_title}}}}",
79*e1825d0cSnicm 		    suffix, s, suffix, star, s, star, suffix, star, s, star);
80a42faf7dSnicm 	} else if (C && N) {
81bee784faSnicm 		xasprintf(&filter->string,
82*e1825d0cSnicm 		    "#{||:#{C%s:%s},#{m%s:%s%s%s,#{window_name}}}",
83*e1825d0cSnicm 		    suffix, s, suffix, star, s, star);
84a42faf7dSnicm 	} else if (C && T) {
85bee784faSnicm 		xasprintf(&filter->string,
86*e1825d0cSnicm 		    "#{||:#{C%s:%s},#{m%s:%s%s%s,#{pane_title}}}",
87*e1825d0cSnicm 		    suffix, s, suffix, star, s, star);
88a42faf7dSnicm 	} else if (N && T) {
89bee784faSnicm 		xasprintf(&filter->string,
90*e1825d0cSnicm 		    "#{||:#{m%s:%s%s%s,#{window_name}},"
91*e1825d0cSnicm 		    "#{m%s:%s%s%s,#{pane_title}}}",
92*e1825d0cSnicm 		    suffix, star, s, star, suffix, star, s, star);
93bee784faSnicm 	} else if (C) {
94bee784faSnicm 		xasprintf(&filter->string,
95bee784faSnicm 		    "#{C%s:%s}",
96bee784faSnicm 		    suffix, s);
97bee784faSnicm 	} else if (N) {
98bee784faSnicm 		xasprintf(&filter->string,
99*e1825d0cSnicm 		    "#{m%s:%s%s%s,#{window_name}}",
100*e1825d0cSnicm 		    suffix, star, s, star);
101bee784faSnicm 	} else {
102bee784faSnicm 		xasprintf(&filter->string,
103*e1825d0cSnicm 		    "#{m%s:%s%s%s,#{pane_title}}",
104*e1825d0cSnicm 		    suffix, star, s, star);
105bee784faSnicm 	}
106ca7befccSnicm 
107eee51546Snicm 	new_args = args_create();
108f456e8d3Snicm 	if (args_has(args, 'Z'))
10983a0a8d9Snicm 		args_set(new_args, 'Z', NULL, 0);
11083a0a8d9Snicm 	args_set(new_args, 'f', filter, 0);
111311827fbSnicm 
112040343aeSnicm 	window_pane_set_mode(wp, NULL, &window_tree_mode, target, new_args);
113a42faf7dSnicm 	args_free(new_args);
114311827fbSnicm 
115a224d0d3Snicm 	return (CMD_RETURN_NORMAL);
116311827fbSnicm }
117