xref: /openbsd-src/usr.bin/tmux/cmd-display-panes.c (revision fb8aa7497fded39583f40e800732f9c046411717)
1 /* $OpenBSD: cmd-display-panes.c,v 1.13 2016/06/16 10:55:47 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 <ctype.h>
22 #include <stdlib.h>
23 
24 #include "tmux.h"
25 
26 /*
27  * Display panes on a client.
28  */
29 
30 static enum cmd_retval	 cmd_display_panes_exec(struct cmd *, struct cmd_q *);
31 
32 static void		 cmd_display_panes_callback(struct client *,
33 			     struct window_pane *);
34 
35 const struct cmd_entry cmd_display_panes_entry = {
36 	.name = "display-panes",
37 	.alias = "displayp",
38 
39 	.args = { "t:", 0, 1 },
40 	.usage = CMD_TARGET_CLIENT_USAGE,
41 
42 	.tflag = CMD_CLIENT,
43 
44 	.flags = 0,
45 	.exec = cmd_display_panes_exec
46 };
47 
48 static enum cmd_retval
49 cmd_display_panes_exec(struct cmd *self, struct cmd_q *cmdq)
50 {
51 	struct args	*args = self->args;
52 	struct client	*c = cmdq->state.c;
53 
54 	if (c->identify_callback != NULL)
55 		return (CMD_RETURN_NORMAL);
56 
57 	c->identify_callback = cmd_display_panes_callback;
58 	if (args->argc != 0)
59 		c->identify_callback_data = xstrdup(args->argv[0]);
60 	else
61 		c->identify_callback_data = xstrdup("select-pane -t '%%'");
62 
63 	server_set_identify(c);
64 
65 	return (CMD_RETURN_NORMAL);
66 }
67 
68 static void
69 cmd_display_panes_callback(struct client *c, struct window_pane *wp)
70 {
71 	struct cmd_list	*cmdlist;
72 	char		*template, *cmd, *expanded, *cause;
73 
74 	template = c->identify_callback_data;
75 	if (wp != NULL) {
76 		xasprintf(&expanded, "%%%u", wp->id);
77 		cmd = cmd_template_replace(template, expanded, 1);
78 
79 		if (cmd_string_parse(cmd, &cmdlist, NULL, 0, &cause) != 0) {
80 			if (cause != NULL) {
81 				*cause = toupper((u_char) *cause);
82 				status_message_set(c, "%s", cause);
83 				free(cause);
84 			}
85 		} else {
86 			cmdq_run(c->cmdq, cmdlist, NULL);
87 			cmd_list_free(cmdlist);
88 		}
89 
90 		free(cmd);
91 		free(expanded);
92 	}
93 
94 	free(c->identify_callback_data);
95 	c->identify_callback_data = NULL;
96 	c->identify_callback = NULL;
97 }
98