Rephrase a confusing sentence.
[tmux-openbsd.git] / cmd-choose-window.c
blobb3e4b2b34f236e250cad522af1698ede9f4c22b6
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 window.
29 int cmd_choose_window_exec(struct cmd *, struct cmd_ctx *);
31 void cmd_choose_window_callback(void *, int);
32 void cmd_choose_window_free(void *);
34 const struct cmd_entry cmd_choose_window_entry = {
35 "choose-window", NULL,
36 CMD_TARGET_WINDOW_USAGE " [template]",
37 CMD_ARG01, "",
38 cmd_target_init,
39 cmd_target_parse,
40 cmd_choose_window_exec,
41 cmd_target_free,
42 cmd_target_print
45 struct cmd_choose_window_data {
46 struct client *client;
47 struct session *session;
48 char *template;
51 int
52 cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx)
54 struct cmd_target_data *data = self->data;
55 struct cmd_choose_window_data *cdata;
56 struct session *s;
57 struct winlink *wl, *wm;
58 struct window *w;
59 u_int idx, cur;
60 char flag, *title;
61 const char *left, *right;
63 if (ctx->curclient == NULL) {
64 ctx->error(ctx, "must be run interactively");
65 return (-1);
67 s = ctx->curclient->session;
69 if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL)
70 return (-1);
72 if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
73 return (0);
75 cur = idx = 0;
76 RB_FOREACH(wm, winlinks, &s->windows) {
77 w = wm->window;
79 if (wm == s->curw)
80 cur = idx;
81 idx++;
83 flag = ' ';
84 if (wm->flags & WINLINK_ACTIVITY)
85 flag = '#';
86 else if (wm->flags & WINLINK_BELL)
87 flag = '!';
88 else if (wm->flags & WINLINK_CONTENT)
89 flag = '+';
90 else if (wm->flags & WINLINK_SILENCE)
91 flag = '~';
92 else if (wm == s->curw)
93 flag = '*';
94 else if (wm == TAILQ_FIRST(&s->lastw))
95 flag = '-';
97 title = w->active->screen->title;
98 if (wm == wl)
99 title = w->active->base.title;
100 left = " \"";
101 right = "\"";
102 if (*title == '\0')
103 left = right = "";
105 window_choose_add(wl->window->active,
106 wm->idx, "%3d: %s%c [%ux%u] (%u panes%s)%s%s%s",
107 wm->idx, w->name, flag, w->sx, w->sy, window_count_panes(w),
108 w->active->fd == -1 ? ", dead" : "",
109 left, title, right);
112 cdata = xmalloc(sizeof *cdata);
113 if (data->arg != NULL)
114 cdata->template = xstrdup(data->arg);
115 else
116 cdata->template = xstrdup("select-window -t '%%'");
117 cdata->session = s;
118 cdata->session->references++;
119 cdata->client = ctx->curclient;
120 cdata->client->references++;
122 window_choose_ready(wl->window->active,
123 cur, cmd_choose_window_callback, cmd_choose_window_free, cdata);
125 return (0);
128 void
129 cmd_choose_window_callback(void *data, int idx)
131 struct cmd_choose_window_data *cdata = data;
132 struct cmd_list *cmdlist;
133 struct cmd_ctx ctx;
134 char *target, *template, *cause;
136 if (idx == -1)
137 return;
138 if (cdata->client->flags & CLIENT_DEAD)
139 return;
140 if (cdata->session->flags & SESSION_DEAD)
141 return;
142 if (cdata->client->session != cdata->session)
143 return;
145 xasprintf(&target, "%s:%d", cdata->session->name, idx);
146 template = cmd_template_replace(cdata->template, target, 1);
147 xfree(target);
149 if (cmd_string_parse(template, &cmdlist, &cause) != 0) {
150 if (cause != NULL) {
151 *cause = toupper((u_char) *cause);
152 status_message_set(cdata->client, "%s", cause);
153 xfree(cause);
155 xfree(template);
156 return;
158 xfree(template);
160 ctx.msgdata = NULL;
161 ctx.curclient = cdata->client;
163 ctx.error = key_bindings_error;
164 ctx.print = key_bindings_print;
165 ctx.info = key_bindings_info;
167 ctx.cmdclient = NULL;
169 cmd_list_exec(cmdlist, &ctx);
170 cmd_list_free(cmdlist);
173 void
174 cmd_choose_window_free(void *data)
176 struct cmd_choose_window_data *cdata = data;
178 cdata->session->references--;
179 cdata->client->references--;
180 xfree(cdata->template);
181 xfree(cdata);