Annotation of src/usr.bin/tmux/cmd-choose-session.c, Revision 1.13
1.13 ! nicm 1: /* $OpenBSD: cmd-choose-session.c,v 1.12 2011/01/04 00:42:46 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.6 nicm 20:
21: #include <ctype.h>
1.1 nicm 22:
23: #include "tmux.h"
24:
25: /*
26: * Enter choice mode to choose a session.
27: */
28:
29: int cmd_choose_session_exec(struct cmd *, struct cmd_ctx *);
30:
31: void cmd_choose_session_callback(void *, int);
1.5 nicm 32: void cmd_choose_session_free(void *);
1.1 nicm 33:
34: const struct cmd_entry cmd_choose_session_entry = {
35: "choose-session", NULL,
1.13 ! nicm 36: "F:t:", 0, 1,
! 37: CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
1.12 nicm 38: 0,
39: NULL,
40: NULL,
41: cmd_choose_session_exec
1.1 nicm 42: };
43:
44: struct cmd_choose_session_data {
1.7 nicm 45: struct client *client;
1.5 nicm 46: char *template;
1.1 nicm 47: };
48:
49: int
50: cmd_choose_session_exec(struct cmd *self, struct cmd_ctx *ctx)
51: {
1.12 nicm 52: struct args *args = self->args;
1.1 nicm 53: struct cmd_choose_session_data *cdata;
54: struct winlink *wl;
55: struct session *s;
1.13 ! nicm 56: struct format_tree *ft;
! 57: const char *template;
! 58: char *line;
! 59: u_int idx, cur;
1.1 nicm 60:
61: if (ctx->curclient == NULL) {
62: ctx->error(ctx, "must be run interactively");
63: return (-1);
64: }
65:
1.12 nicm 66: if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
1.1 nicm 67: return (-1);
68:
69: if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
70: return (0);
71:
1.13 ! nicm 72: if ((template = args_get(args, 'F')) == NULL)
! 73: template = DEFAULT_SESSION_TEMPLATE;
! 74:
1.1 nicm 75: cur = idx = 0;
1.11 nicm 76: RB_FOREACH(s, sessions, &sessions) {
1.1 nicm 77: if (s == ctx->curclient->session)
78: cur = idx;
79: idx++;
80:
1.13 ! nicm 81: ft = format_create();
! 82: format_add(ft, "line", "%u", idx);
! 83: format_session(ft, s);
! 84:
! 85: line = format_expand(ft, template);
! 86: window_choose_add(wl->window->active, s->idx, "%s", line);
! 87: xfree(line);
! 88:
! 89: format_free(ft);
1.1 nicm 90: }
91:
92: cdata = xmalloc(sizeof *cdata);
1.12 nicm 93: if (args->argc != 0)
94: cdata->template = xstrdup(args->argv[0]);
1.5 nicm 95: else
96: cdata->template = xstrdup("switch-client -t '%%'");
1.7 nicm 97: cdata->client = ctx->curclient;
98: cdata->client->references++;
1.1 nicm 99:
1.5 nicm 100: window_choose_ready(wl->window->active,
101: cur, cmd_choose_session_callback, cmd_choose_session_free, cdata);
1.1 nicm 102:
103: return (0);
104: }
105:
106: void
107: cmd_choose_session_callback(void *data, int idx)
108: {
109: struct cmd_choose_session_data *cdata = data;
1.5 nicm 110: struct session *s;
111: struct cmd_list *cmdlist;
112: struct cmd_ctx ctx;
113: char *template, *cause;
114:
115: if (idx == -1)
116: return;
1.7 nicm 117: if (cdata->client->flags & CLIENT_DEAD)
1.5 nicm 118: return;
119:
1.11 nicm 120: s = session_find_by_index(idx);
1.5 nicm 121: if (s == NULL)
122: return;
123: template = cmd_template_replace(cdata->template, s->name, 1);
124:
125: if (cmd_string_parse(template, &cmdlist, &cause) != 0) {
126: if (cause != NULL) {
127: *cause = toupper((u_char) *cause);
1.7 nicm 128: status_message_set(cdata->client, "%s", cause);
1.5 nicm 129: xfree(cause);
1.1 nicm 130: }
1.5 nicm 131: xfree(template);
132: return;
1.1 nicm 133: }
1.5 nicm 134: xfree(template);
135:
136: ctx.msgdata = NULL;
1.7 nicm 137: ctx.curclient = cdata->client;
1.5 nicm 138:
139: ctx.error = key_bindings_error;
140: ctx.print = key_bindings_print;
141: ctx.info = key_bindings_info;
142:
143: ctx.cmdclient = NULL;
144:
145: cmd_list_exec(cmdlist, &ctx);
146: cmd_list_free(cmdlist);
147: }
148:
149: void
150: cmd_choose_session_free(void *data)
151: {
152: struct cmd_choose_session_data *cdata = data;
153:
1.7 nicm 154: cdata->client->references--;
1.5 nicm 155: xfree(cdata->template);
156: xfree(cdata);
1.1 nicm 157: }