Annotation of src/usr.bin/tmux/cmd-choose-window.c, Revision 1.17
1.17 ! nicm 1: /* $OpenBSD: cmd-choose-window.c,v 1.16 2010/12/20 00:03:55 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
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>
1.8 nicm 20:
21: #include <ctype.h>
1.1 nicm 22:
23: #include "tmux.h"
24:
25: /*
26: * Enter choice mode to choose a window.
27: */
28:
29: int cmd_choose_window_exec(struct cmd *, struct cmd_ctx *);
30:
31: void cmd_choose_window_callback(void *, int);
1.7 nicm 32: void cmd_choose_window_free(void *);
1.1 nicm 33:
34: const struct cmd_entry cmd_choose_window_entry = {
35: "choose-window", NULL,
1.7 nicm 36: CMD_TARGET_WINDOW_USAGE " [template]",
1.11 nicm 37: CMD_ARG01, "",
1.1 nicm 38: cmd_target_init,
39: cmd_target_parse,
40: cmd_choose_window_exec,
41: cmd_target_free,
42: cmd_target_print
43: };
44:
45: struct cmd_choose_window_data {
1.9 nicm 46: struct client *client;
47: struct session *session;
1.7 nicm 48: char *template;
1.1 nicm 49: };
50:
51: int
52: cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx)
53: {
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;
1.17 ! nicm 60: char *flags, *title;
1.6 nicm 61: const char *left, *right;
1.1 nicm 62:
63: if (ctx->curclient == NULL) {
64: ctx->error(ctx, "must be run interactively");
65: return (-1);
66: }
67: s = ctx->curclient->session;
68:
69: if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL)
70: return (-1);
71:
72: if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
73: return (0);
74:
75: cur = idx = 0;
76: RB_FOREACH(wm, winlinks, &s->windows) {
77: w = wm->window;
78:
79: if (wm == s->curw)
80: cur = idx;
81: idx++;
82:
1.17 ! nicm 83: flags = window_printable_flags(s, wm);
1.6 nicm 84: title = w->active->screen->title;
85: if (wm == wl)
86: title = w->active->base.title;
87: left = " \"";
88: right = "\"";
89: if (*title == '\0')
90: left = right = "";
91:
1.1 nicm 92: window_choose_add(wl->window->active,
1.17 ! nicm 93: wm->idx, "%3d: %s%s [%ux%u] (%u panes%s)%s%s%s",
! 94: wm->idx, w->name, flags, w->sx, w->sy, window_count_panes(w),
1.13 nicm 95: w->active->fd == -1 ? ", dead" : "",
1.6 nicm 96: left, title, right);
1.17 ! nicm 97:
! 98: xfree(flags);
1.1 nicm 99: }
100:
101: cdata = xmalloc(sizeof *cdata);
1.7 nicm 102: if (data->arg != NULL)
103: cdata->template = xstrdup(data->arg);
104: else
105: cdata->template = xstrdup("select-window -t '%%'");
1.9 nicm 106: cdata->session = s;
107: cdata->session->references++;
108: cdata->client = ctx->curclient;
109: cdata->client->references++;
1.1 nicm 110:
1.12 nicm 111: window_choose_ready(wl->window->active,
1.7 nicm 112: cur, cmd_choose_window_callback, cmd_choose_window_free, cdata);
1.1 nicm 113:
1.12 nicm 114: return (0);
1.1 nicm 115: }
116:
117: void
118: cmd_choose_window_callback(void *data, int idx)
119: {
120: struct cmd_choose_window_data *cdata = data;
1.16 nicm 121: struct session *s = cdata->session;
1.7 nicm 122: struct cmd_list *cmdlist;
123: struct cmd_ctx ctx;
124: char *target, *template, *cause;
125:
126: if (idx == -1)
127: return;
1.16 nicm 128: if (!session_alive(s))
129: return;
1.9 nicm 130: if (cdata->client->flags & CLIENT_DEAD)
1.12 nicm 131: return;
1.7 nicm 132:
1.16 nicm 133: xasprintf(&target, "%s:%d", s->name, idx);
1.7 nicm 134: template = cmd_template_replace(cdata->template, target, 1);
135: xfree(target);
136:
137: if (cmd_string_parse(template, &cmdlist, &cause) != 0) {
138: if (cause != NULL) {
139: *cause = toupper((u_char) *cause);
1.9 nicm 140: status_message_set(cdata->client, "%s", cause);
1.7 nicm 141: xfree(cause);
142: }
143: xfree(template);
144: return;
145: }
146: xfree(template);
147:
148: ctx.msgdata = NULL;
1.9 nicm 149: ctx.curclient = cdata->client;
1.7 nicm 150:
151: ctx.error = key_bindings_error;
152: ctx.print = key_bindings_print;
153: ctx.info = key_bindings_info;
154:
155: ctx.cmdclient = NULL;
156:
157: cmd_list_exec(cmdlist, &ctx);
158: cmd_list_free(cmdlist);
159: }
1.1 nicm 160:
1.7 nicm 161: void
162: cmd_choose_window_free(void *data)
163: {
164: struct cmd_choose_window_data *cdata = data;
165:
1.9 nicm 166: cdata->session->references--;
167: cdata->client->references--;
1.7 nicm 168: xfree(cdata->template);
169: xfree(cdata);
1.1 nicm 170: }