Don't return if in the current window since we may want to report a bell
[tmux-openbsd.git] / cmd-choose-session.c
blob232281051c398416b93155d6b2aa8a6985fe1bb6
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 session.
29 int cmd_choose_session_exec(struct cmd *, struct cmd_ctx *);
31 void cmd_choose_session_callback(void *, int);
32 void cmd_choose_session_free(void *);
34 const struct cmd_entry cmd_choose_session_entry = {
35 "choose-session", NULL,
36 CMD_TARGET_WINDOW_USAGE " [template]",
37 CMD_ARG01, "",
38 cmd_target_init,
39 cmd_target_parse,
40 cmd_choose_session_exec,
41 cmd_target_free,
42 cmd_target_print
45 struct cmd_choose_session_data {
46 struct client *client;
47 char *template;
50 int
51 cmd_choose_session_exec(struct cmd *self, struct cmd_ctx *ctx)
53 struct cmd_target_data *data = self->data;
54 struct cmd_choose_session_data *cdata;
55 struct winlink *wl;
56 struct session *s;
57 struct session_group *sg;
58 u_int i, idx, cur;
59 char tmp[64];
61 if (ctx->curclient == NULL) {
62 ctx->error(ctx, "must be run interactively");
63 return (-1);
66 if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL)
67 return (-1);
69 if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
70 return (0);
72 cur = idx = 0;
73 for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
74 s = ARRAY_ITEM(&sessions, i);
75 if (s == NULL)
76 continue;
77 if (s == ctx->curclient->session)
78 cur = idx;
79 idx++;
81 sg = session_group_find(s);
82 if (sg == NULL)
83 *tmp = '\0';
84 else {
85 idx = session_group_index(sg);
86 xsnprintf(tmp, sizeof tmp, " (group %u)", idx);
89 window_choose_add(wl->window->active, i,
90 "%s: %u windows [%ux%u]%s%s", s->name,
91 winlink_count(&s->windows), s->sx, s->sy,
92 tmp, s->flags & SESSION_UNATTACHED ? "" : " (attached)");
95 cdata = xmalloc(sizeof *cdata);
96 if (data->arg != NULL)
97 cdata->template = xstrdup(data->arg);
98 else
99 cdata->template = xstrdup("switch-client -t '%%'");
100 cdata->client = ctx->curclient;
101 cdata->client->references++;
103 window_choose_ready(wl->window->active,
104 cur, cmd_choose_session_callback, cmd_choose_session_free, cdata);
106 return (0);
109 void
110 cmd_choose_session_callback(void *data, int idx)
112 struct cmd_choose_session_data *cdata = data;
113 struct session *s;
114 struct cmd_list *cmdlist;
115 struct cmd_ctx ctx;
116 char *template, *cause;
118 if (idx == -1)
119 return;
120 if (cdata->client->flags & CLIENT_DEAD)
121 return;
123 if ((u_int) idx > ARRAY_LENGTH(&sessions) - 1)
124 return;
125 s = ARRAY_ITEM(&sessions, idx);
126 if (s == NULL)
127 return;
128 template = cmd_template_replace(cdata->template, s->name, 1);
130 if (cmd_string_parse(template, &cmdlist, &cause) != 0) {
131 if (cause != NULL) {
132 *cause = toupper((u_char) *cause);
133 status_message_set(cdata->client, "%s", cause);
134 xfree(cause);
136 xfree(template);
137 return;
139 xfree(template);
141 ctx.msgdata = NULL;
142 ctx.curclient = cdata->client;
144 ctx.error = key_bindings_error;
145 ctx.print = key_bindings_print;
146 ctx.info = key_bindings_info;
148 ctx.cmdclient = NULL;
150 cmd_list_exec(cmdlist, &ctx);
151 cmd_list_free(cmdlist);
154 void
155 cmd_choose_session_free(void *data)
157 struct cmd_choose_session_data *cdata = data;
159 cdata->client->references--;
160 xfree(cdata->template);
161 xfree(cdata);