Fix bind-key -t.
[tmux-openbsd.git] / cmd-choose-client.c
blobac766ab5c532c5e488932a9ec453f91635a6b2ad
1 /* $OpenBSD$ */
3 /*
4 * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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.
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.
19 #include <sys/types.h>
21 #include <ctype.h>
23 #include "tmux.h"
26 * Enter choice mode to choose a client.
29 int cmd_choose_client_exec(struct cmd *, struct cmd_ctx *);
31 void cmd_choose_client_callback(void *, int);
32 void cmd_choose_client_free(void *);
34 const struct cmd_entry cmd_choose_client_entry = {
35 "choose-client", NULL,
36 "t:", 0, 1,
37 CMD_TARGET_WINDOW_USAGE " [template]",
39 NULL,
40 NULL,
41 cmd_choose_client_exec
44 struct cmd_choose_client_data {
45 struct client *client;
46 char *template;
49 int
50 cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
52 struct args *args = self->args;
53 struct cmd_choose_client_data *cdata;
54 struct winlink *wl;
55 struct client *c;
56 u_int i, idx, cur;
58 if (ctx->curclient == NULL) {
59 ctx->error(ctx, "must be run interactively");
60 return (-1);
63 if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
64 return (-1);
66 if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
67 return (0);
69 cur = idx = 0;
70 for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
71 c = ARRAY_ITEM(&clients, i);
72 if (c == NULL || c->session == NULL)
73 continue;
74 if (c == ctx->curclient)
75 cur = idx;
76 idx++;
78 window_choose_add(wl->window->active, i,
79 "%s: %s [%ux%u %s]%s", c->tty.path,
80 c->session->name, c->tty.sx, c->tty.sy,
81 c->tty.termname, c->tty.flags & TTY_UTF8 ? " (utf8)" : "");
84 cdata = xmalloc(sizeof *cdata);
85 if (args->argc != 0)
86 cdata->template = xstrdup(args->argv[0]);
87 else
88 cdata->template = xstrdup("detach-client -t '%%'");
89 cdata->client = ctx->curclient;
90 cdata->client->references++;
92 window_choose_ready(wl->window->active,
93 cur, cmd_choose_client_callback, cmd_choose_client_free, cdata);
95 return (0);
98 void
99 cmd_choose_client_callback(void *data, int idx)
101 struct cmd_choose_client_data *cdata = data;
102 struct client *c;
103 struct cmd_list *cmdlist;
104 struct cmd_ctx ctx;
105 char *template, *cause;
107 if (idx == -1)
108 return;
109 if (cdata->client->flags & CLIENT_DEAD)
110 return;
112 if ((u_int) idx > ARRAY_LENGTH(&clients) - 1)
113 return;
114 c = ARRAY_ITEM(&clients, idx);
115 if (c == NULL || c->session == NULL)
116 return;
117 template = cmd_template_replace(cdata->template, c->tty.path, 1);
119 if (cmd_string_parse(template, &cmdlist, &cause) != 0) {
120 if (cause != NULL) {
121 *cause = toupper((u_char) *cause);
122 status_message_set(c, "%s", cause);
123 xfree(cause);
125 xfree(template);
126 return;
128 xfree(template);
130 ctx.msgdata = NULL;
131 ctx.curclient = cdata->client;
133 ctx.error = key_bindings_error;
134 ctx.print = key_bindings_print;
135 ctx.info = key_bindings_info;
137 ctx.cmdclient = NULL;
139 cmd_list_exec(cmdlist, &ctx);
140 cmd_list_free(cmdlist);
143 void
144 cmd_choose_client_free(void *data)
146 struct cmd_choose_client_data *cdata = data;
148 cdata->client->references--;
149 xfree(cdata->template);
150 xfree(cdata);