xref: /openbsd/usr.bin/tmux/cmd-find-window.c (revision 90d7ba38)
1 /* $OpenBSD: cmd-find-window.c,v 1.48 2020/04/13 08:26:27 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 = { "CNrt:TZ", 1, 1 },
36 	.usage = "[-CNrTZ] " 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
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 window_pane	*wp = item->target.wp;
49 	const char		*s = args->argv[0];
50 	char			*filter, *argv = { NULL };
51 	int			 C, N, T;
52 
53 	C = args_has(args, 'C');
54 	N = args_has(args, 'N');
55 	T = args_has(args, 'T');
56 
57 	if (!C && !N && !T)
58 		C = N = T = 1;
59 
60 	if (!args_has(args, 'r')) {
61 		if (C && N && T) {
62 			xasprintf(&filter,
63 			    "#{||:"
64 			    "#{C:%s},#{||:#{m:*%s*,#{window_name}},"
65 			    "#{m:*%s*,#{pane_title}}}}",
66 			    s, s, s);
67 		} else if (C && N) {
68 			xasprintf(&filter,
69 			    "#{||:#{C:%s},#{m:*%s*,#{window_name}}}",
70 			    s, s);
71 		} else if (C && T) {
72 			xasprintf(&filter,
73 			    "#{||:#{C:%s},#{m:*%s*,#{pane_title}}}",
74 			    s, s);
75 		} else if (N && T) {
76 			xasprintf(&filter,
77 			    "#{||:#{m:*%s*,#{window_name}},"
78 			    "#{m:*%s*,#{pane_title}}}",
79 			    s, s);
80 		} else if (C)
81 			xasprintf(&filter, "#{C:%s}", s);
82 		else if (N)
83 			xasprintf(&filter, "#{m:*%s*,#{window_name}}", s);
84 		else
85 			xasprintf(&filter, "#{m:*%s*,#{pane_title}}", s);
86 	} else {
87 		if (C && N && T) {
88 			xasprintf(&filter,
89 			    "#{||:"
90 			    "#{C/r:%s},#{||:#{m/r:%s,#{window_name}},"
91 			    "#{m/r:%s,#{pane_title}}}}",
92 			    s, s, s);
93 		} else if (C && N) {
94 			xasprintf(&filter,
95 			    "#{||:#{C/r:%s},#{m/r:%s,#{window_name}}}",
96 			    s, s);
97 		} else if (C && T) {
98 			xasprintf(&filter,
99 			    "#{||:#{C/r:%s},#{m/r:%s,#{pane_title}}}",
100 			    s, s);
101 		} else if (N && T) {
102 			xasprintf(&filter,
103 			    "#{||:#{m/r:%s,#{window_name}},"
104 			    "#{m/r:%s,#{pane_title}}}",
105 			    s, s);
106 		} else if (C)
107 			xasprintf(&filter, "#{C/r:%s}", s);
108 		else if (N)
109 			xasprintf(&filter, "#{m/r:%s,#{window_name}}", s);
110 		else
111 			xasprintf(&filter, "#{m/r:%s,#{pane_title}}", s);
112 	}
113 
114 	new_args = args_parse("", 1, &argv);
115 	if (args_has(args, 'Z'))
116 		args_set(new_args, 'Z', NULL);
117 	args_set(new_args, 'f', filter);
118 
119 	window_pane_set_mode(wp, NULL, &window_tree_mode, &item->target,
120 	    new_args);
121 
122 	args_free(new_args);
123 	free(filter);
124 
125 	return (CMD_RETURN_NORMAL);
126 }
127