[BACK]Return to cmd-choose-session.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tmux

Annotation of src/usr.bin/tmux/cmd-choose-session.c, Revision 1.6

1.6     ! nicm        1: /* $OpenBSD: cmd-choose-session.c,v 1.5 2009/08/25 12:18:51 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.5       nicm       36:        CMD_TARGET_WINDOW_USAGE " [template]",
                     37:        CMD_ARG01, 0,
1.1       nicm       38:        cmd_target_init,
                     39:        cmd_target_parse,
                     40:        cmd_choose_session_exec,
                     41:        cmd_target_free,
                     42:        cmd_target_print
                     43: };
                     44:
                     45: struct cmd_choose_session_data {
1.5       nicm       46:        u_int            client;
                     47:        char            *template;
1.1       nicm       48: };
                     49:
                     50: int
                     51: cmd_choose_session_exec(struct cmd *self, struct cmd_ctx *ctx)
                     52: {
                     53:        struct cmd_target_data          *data = self->data;
                     54:        struct cmd_choose_session_data  *cdata;
                     55:        struct winlink                  *wl;
                     56:        struct session                  *s;
                     57:        u_int                            i, idx, cur;
                     58:
                     59:        if (ctx->curclient == NULL) {
                     60:                ctx->error(ctx, "must be run interactively");
                     61:                return (-1);
                     62:        }
                     63:
                     64:        if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL)
                     65:                return (-1);
                     66:
                     67:        if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
                     68:                return (0);
                     69:
                     70:        cur = idx = 0;
                     71:        for (i = 0; i < ARRAY_LENGTH(&sessions); i++) {
                     72:                s = ARRAY_ITEM(&sessions, i);
                     73:                if (s == NULL)
                     74:                        continue;
                     75:                if (s == ctx->curclient->session)
                     76:                        cur = idx;
                     77:                idx++;
                     78:
                     79:                window_choose_add(wl->window->active, i,
                     80:                    "%s: %u windows [%ux%u]%s", s->name,
                     81:                    winlink_count(&s->windows), s->sx, s->sy,
                     82:                    s->flags & SESSION_UNATTACHED ? "" : " (attached)");
                     83:        }
                     84:
                     85:        cdata = xmalloc(sizeof *cdata);
1.5       nicm       86:        if (data->arg != NULL)
                     87:                cdata->template = xstrdup(data->arg);
                     88:        else
                     89:                cdata->template = xstrdup("switch-client -t '%%'");
1.1       nicm       90:        cdata->client = server_client_index(ctx->curclient);
                     91:
1.5       nicm       92:        window_choose_ready(wl->window->active,
                     93:            cur, cmd_choose_session_callback, cmd_choose_session_free, cdata);
1.1       nicm       94:
                     95:        return (0);
                     96: }
                     97:
                     98: void
                     99: cmd_choose_session_callback(void *data, int idx)
                    100: {
                    101:        struct cmd_choose_session_data  *cdata = data;
                    102:        struct client                   *c;
1.5       nicm      103:        struct session                  *s;
                    104:        struct cmd_list                 *cmdlist;
                    105:        struct cmd_ctx                   ctx;
                    106:        char                            *template, *cause;
                    107:
                    108:        if (idx == -1)
                    109:                return;
                    110:        if (cdata->client > ARRAY_LENGTH(&clients) - 1)
                    111:                return;
                    112:        c = ARRAY_ITEM(&clients, cdata->client);
                    113:
                    114:        if ((u_int) idx > ARRAY_LENGTH(&sessions) - 1)
                    115:                return;
                    116:        s = ARRAY_ITEM(&sessions, idx);
                    117:        if (s == NULL)
                    118:                return;
                    119:        template = cmd_template_replace(cdata->template, s->name, 1);
                    120:
                    121:        if (cmd_string_parse(template, &cmdlist, &cause) != 0) {
                    122:                if (cause != NULL) {
                    123:                        *cause = toupper((u_char) *cause);
                    124:                        status_message_set(c, "%s", cause);
                    125:                        xfree(cause);
1.1       nicm      126:                }
1.5       nicm      127:                xfree(template);
                    128:                return;
1.1       nicm      129:        }
1.5       nicm      130:        xfree(template);
                    131:
                    132:        ctx.msgdata = NULL;
                    133:        ctx.curclient = c;
                    134:
                    135:        ctx.error = key_bindings_error;
                    136:        ctx.print = key_bindings_print;
                    137:        ctx.info = key_bindings_info;
                    138:
                    139:        ctx.cmdclient = NULL;
                    140:
                    141:        cmd_list_exec(cmdlist, &ctx);
                    142:        cmd_list_free(cmdlist);
                    143: }
                    144:
                    145: void
                    146: cmd_choose_session_free(void *data)
                    147: {
                    148:        struct cmd_choose_session_data  *cdata = data;
                    149:
                    150:        xfree(cdata->template);
                    151:        xfree(cdata);
1.1       nicm      152: }