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