xref: /openbsd/usr.bin/tmux/cmd-find-window.c (revision d89ec533)
1 /* $OpenBSD: cmd-find-window.c,v 1.54 2021/08/21 20:46:43 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
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 	struct args_value	*filter;
52 	int			 C, N, T;
53 
54 	C = args_has(args, 'C');
55 	N = args_has(args, 'N');
56 	T = args_has(args, 'T');
57 
58 	if (args_has(args, 'r') && args_has(args, 'i'))
59 		suffix = "/ri";
60 	else if (args_has(args, 'r'))
61 		suffix = "/r";
62 	else if (args_has(args, 'i'))
63 		suffix = "/i";
64 
65 	if (!C && !N && !T)
66 		C = N = T = 1;
67 
68 	filter = xcalloc(1, sizeof *filter);
69 	filter->type = ARGS_STRING;
70 
71 	if (C && N && T) {
72 		xasprintf(&filter->string,
73 		    "#{||:"
74 		    "#{C%s:%s},#{||:#{m%s:*%s*,#{window_name}},"
75 		    "#{m%s:*%s*,#{pane_title}}}}",
76 		    suffix, s, suffix, s, suffix, s);
77 	} else if (C && N) {
78 		xasprintf(&filter->string,
79 		    "#{||:#{C%s:%s},#{m%s:*%s*,#{window_name}}}",
80 		    suffix, s, suffix, s);
81 	} else if (C && T) {
82 		xasprintf(&filter->string,
83 		    "#{||:#{C%s:%s},#{m%s:*%s*,#{pane_title}}}",
84 		    suffix, s, suffix, s);
85 	} else if (N && T) {
86 		xasprintf(&filter->string,
87 		    "#{||:#{m%s:*%s*,#{window_name}},"
88 		    "#{m%s:*%s*,#{pane_title}}}",
89 		    suffix, s, suffix, s);
90 	} else if (C) {
91 		xasprintf(&filter->string,
92 		    "#{C%s:%s}",
93 		    suffix, s);
94 	} else if (N) {
95 		xasprintf(&filter->string,
96 		    "#{m%s:*%s*,#{window_name}}",
97 		    suffix, s);
98 	} else {
99 		xasprintf(&filter->string,
100 		    "#{m%s:*%s*,#{pane_title}}",
101 		    suffix, s);
102 	}
103 
104 	new_args = args_create();
105 	if (args_has(args, 'Z'))
106 		args_set(new_args, 'Z', NULL);
107 	args_set(new_args, 'f', filter);
108 
109 	window_pane_set_mode(wp, NULL, &window_tree_mode, target, new_args);
110 	args_free(new_args);
111 
112 	return (CMD_RETURN_NORMAL);
113 }
114