cmd-display-panes.c revision 1.1.1.7
1/* $OpenBSD$ */
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
30static enum cmd_retval	cmd_display_panes_exec(struct cmd *,
31			    struct cmdq_item *);
32
33static void		cmd_display_panes_callback(struct client *,
34			    struct window_pane *);
35
36const struct cmd_entry cmd_display_panes_entry = {
37	.name = "display-panes",
38	.alias = "displayp",
39
40	.args = { "t:", 0, 1 },
41	.usage = CMD_TARGET_CLIENT_USAGE,
42
43	.tflag = CMD_CLIENT,
44
45	.flags = CMD_AFTERHOOK,
46	.exec = cmd_display_panes_exec
47};
48
49static enum cmd_retval
50cmd_display_panes_exec(struct cmd *self, struct cmdq_item *item)
51{
52	struct args	*args = self->args;
53	struct client	*c = item->state.c;
54
55	if (c->identify_callback != NULL)
56		return (CMD_RETURN_NORMAL);
57
58	c->identify_callback = cmd_display_panes_callback;
59	if (args->argc != 0)
60		c->identify_callback_data = xstrdup(args->argv[0]);
61	else
62		c->identify_callback_data = xstrdup("select-pane -t '%%'");
63
64	server_client_set_identify(c);
65
66	return (CMD_RETURN_NORMAL);
67}
68
69static enum cmd_retval
70cmd_display_panes_error(struct cmdq_item *item, void *data)
71{
72	char	*error = data;
73
74	cmdq_error(item, "%s", error);
75	free(error);
76
77	return (CMD_RETURN_NORMAL);
78}
79
80static void
81cmd_display_panes_callback(struct client *c, struct window_pane *wp)
82{
83	struct cmd_list		*cmdlist;
84	struct cmdq_item	*new_item;
85	char			*template, *cmd, *expanded, *cause;
86
87	template = c->identify_callback_data;
88	if (wp == NULL)
89		goto out;
90	xasprintf(&expanded, "%%%u", wp->id);
91	cmd = cmd_template_replace(template, expanded, 1);
92
93	cmdlist = cmd_string_parse(cmd, NULL, 0, &cause);
94	if (cmdlist == NULL) {
95		if (cause != NULL) {
96			new_item = cmdq_get_callback(cmd_display_panes_error,
97			    cause);
98		} else
99			new_item = NULL;
100	} else {
101		new_item = cmdq_get_command(cmdlist, NULL, NULL, 0);
102		cmd_list_free(cmdlist);
103	}
104
105	if (new_item != NULL)
106		cmdq_append(c, new_item);
107
108	free(cmd);
109	free(expanded);
110
111out:
112	free(c->identify_callback_data);
113	c->identify_callback_data = NULL;
114	c->identify_callback = NULL;
115}
116