1 |
|
|
/* $OpenBSD: cmd-choose-client.c,v 1.29 2016/01/19 15:59:12 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 |
|
|
* Enter choice mode to choose a client. |
28 |
|
|
*/ |
29 |
|
|
|
30 |
|
|
#define CHOOSE_CLIENT_TEMPLATE \ |
31 |
|
|
"#{client_tty}: #{session_name} " \ |
32 |
|
|
"[#{client_width}x#{client_height} #{client_termname}]" \ |
33 |
|
|
"#{?client_utf8, (utf8),}#{?client_readonly, (ro),} " \ |
34 |
|
|
"(last used #{t:client_activity})" |
35 |
|
|
|
36 |
|
|
enum cmd_retval cmd_choose_client_exec(struct cmd *, struct cmd_q *); |
37 |
|
|
|
38 |
|
|
void cmd_choose_client_callback(struct window_choose_data *); |
39 |
|
|
|
40 |
|
|
const struct cmd_entry cmd_choose_client_entry = { |
41 |
|
|
.name = "choose-client", |
42 |
|
|
.alias = NULL, |
43 |
|
|
|
44 |
|
|
.args = { "F:t:", 0, 1 }, |
45 |
|
|
.usage = CMD_TARGET_WINDOW_USAGE " [-F format] [template]", |
46 |
|
|
|
47 |
|
|
.tflag = CMD_WINDOW, |
48 |
|
|
|
49 |
|
|
.flags = 0, |
50 |
|
|
.exec = cmd_choose_client_exec |
51 |
|
|
}; |
52 |
|
|
|
53 |
|
|
struct cmd_choose_client_data { |
54 |
|
|
struct client *client; |
55 |
|
|
}; |
56 |
|
|
|
57 |
|
|
enum cmd_retval |
58 |
|
|
cmd_choose_client_exec(struct cmd *self, struct cmd_q *cmdq) |
59 |
|
|
{ |
60 |
|
|
struct args *args = self->args; |
61 |
|
|
struct client *c = cmdq->state.c; |
62 |
|
|
struct client *c1; |
63 |
|
|
struct window_choose_data *cdata; |
64 |
|
|
struct winlink *wl = cmdq->state.tflag.wl; |
65 |
|
|
const char *template; |
66 |
|
|
char *action; |
67 |
|
|
u_int idx, cur; |
68 |
|
|
|
69 |
|
|
if (c == NULL) { |
70 |
|
|
cmdq_error(cmdq, "no client available"); |
71 |
|
|
return (CMD_RETURN_ERROR); |
72 |
|
|
} |
73 |
|
|
|
74 |
|
|
if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0) |
75 |
|
|
return (CMD_RETURN_NORMAL); |
76 |
|
|
|
77 |
|
|
if ((template = args_get(args, 'F')) == NULL) |
78 |
|
|
template = CHOOSE_CLIENT_TEMPLATE; |
79 |
|
|
|
80 |
|
|
if (args->argc != 0) |
81 |
|
|
action = xstrdup(args->argv[0]); |
82 |
|
|
else |
83 |
|
|
action = xstrdup("detach-client -t '%%'"); |
84 |
|
|
|
85 |
|
|
cur = idx = 0; |
86 |
|
|
TAILQ_FOREACH(c1, &clients, entry) { |
87 |
|
|
if (c1->session == NULL || c1->tty.path == NULL) |
88 |
|
|
continue; |
89 |
|
|
if (c1 == cmdq->client) |
90 |
|
|
cur = idx; |
91 |
|
|
|
92 |
|
|
cdata = window_choose_data_create(TREE_OTHER, c, c->session); |
93 |
|
|
cdata->idx = idx; |
94 |
|
|
|
95 |
|
|
cdata->ft_template = xstrdup(template); |
96 |
|
|
format_add(cdata->ft, "line", "%u", idx); |
97 |
|
|
format_defaults(cdata->ft, c1, NULL, NULL, NULL); |
98 |
|
|
|
99 |
|
|
cdata->command = cmd_template_replace(action, c1->tty.path, 1); |
100 |
|
|
|
101 |
|
|
window_choose_add(wl->window->active, cdata); |
102 |
|
|
|
103 |
|
|
idx++; |
104 |
|
|
} |
105 |
|
|
free(action); |
106 |
|
|
|
107 |
|
|
window_choose_ready(wl->window->active, cur, |
108 |
|
|
cmd_choose_client_callback); |
109 |
|
|
|
110 |
|
|
return (CMD_RETURN_NORMAL); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
void |
114 |
|
|
cmd_choose_client_callback(struct window_choose_data *cdata) |
115 |
|
|
{ |
116 |
|
|
struct client *c; |
117 |
|
|
u_int idx; |
118 |
|
|
|
119 |
|
|
if (cdata == NULL) |
120 |
|
|
return; |
121 |
|
|
if (cdata->start_client->flags & CLIENT_DEAD) |
122 |
|
|
return; |
123 |
|
|
|
124 |
|
|
idx = 0; |
125 |
|
|
TAILQ_FOREACH(c, &clients, entry) { |
126 |
|
|
if (idx == cdata->idx) |
127 |
|
|
break; |
128 |
|
|
idx++; |
129 |
|
|
} |
130 |
|
|
if (c == NULL || c->session == NULL) |
131 |
|
|
return; |
132 |
|
|
|
133 |
|
|
window_choose_data_run(cdata); |
134 |
|
|
} |