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

Annotation of src/usr.bin/tmux/cmd-choose-client.c, Revision 1.8

1.8     ! nicm        1: /* $OpenBSD: cmd-choose-client.c,v 1.7 2012/05/22 11:35:37 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.2       nicm       20:
                     21: #include <ctype.h>
1.1       nicm       22:
                     23: #include "tmux.h"
                     24:
                     25: /*
                     26:  * Enter choice mode to choose a client.
                     27:  */
                     28:
                     29: int    cmd_choose_client_exec(struct cmd *, struct cmd_ctx *);
                     30:
1.8     ! nicm       31: void   cmd_choose_client_callback(struct window_choose_data *);
        !            32: void   cmd_choose_client_free(struct window_choose_data *);
1.1       nicm       33:
                     34: const struct cmd_entry cmd_choose_client_entry = {
                     35:        "choose-client", NULL,
1.7       nicm       36:        "F:t:", 0, 1,
                     37:        CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
1.5       nicm       38:        0,
                     39:        NULL,
                     40:        NULL,
                     41:        cmd_choose_client_exec
1.1       nicm       42: };
                     43:
                     44: struct cmd_choose_client_data {
1.3       nicm       45:        struct client   *client;
1.1       nicm       46:        char            *template;
                     47: };
                     48:
                     49: int
                     50: cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
                     51: {
1.5       nicm       52:        struct args                     *args = self->args;
1.8     ! nicm       53:        struct window_choose_data       *cdata;
1.1       nicm       54:        struct winlink                  *wl;
                     55:        struct client                   *c;
1.7       nicm       56:        const char                      *template;
1.1       nicm       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:
1.5       nicm       64:        if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
1.1       nicm       65:                return (-1);
                     66:
                     67:        if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
                     68:                return (0);
                     69:
1.7       nicm       70:        if ((template = args_get(args, 'F')) == NULL)
                     71:                template = DEFAULT_CLIENT_TEMPLATE;
                     72:
1.1       nicm       73:        cur = idx = 0;
                     74:        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                     75:                c = ARRAY_ITEM(&clients, i);
                     76:                if (c == NULL || c->session == NULL)
                     77:                        continue;
                     78:                if (c == ctx->curclient)
                     79:                        cur = idx;
                     80:                idx++;
                     81:
1.8     ! nicm       82:                cdata = window_choose_data_create(ctx);
        !            83:                if (args->argc != 0)
        !            84:                        cdata->action = xstrdup(args->argv[0]);
        !            85:                else
        !            86:                        cdata->action = xstrdup("detach-client -t '%%'");
        !            87:
        !            88:                cdata->idx = i;
        !            89:                cdata->client->references++;
        !            90:
        !            91:                cdata->ft_template = xstrdup(template);
        !            92:                format_add(cdata->ft, "line", "%u", i);
        !            93:                format_session(cdata->ft, c->session);
        !            94:                format_client(cdata->ft, c);
1.7       nicm       95:
1.8     ! nicm       96:                window_choose_add(wl->window->active, cdata);
1.1       nicm       97:        }
                     98:
                     99:        window_choose_ready(wl->window->active,
1.8     ! nicm      100:            cur, cmd_choose_client_callback, cmd_choose_client_free);
1.1       nicm      101:
                    102:        return (0);
                    103: }
                    104:
                    105: void
1.8     ! nicm      106: cmd_choose_client_callback(struct window_choose_data *cdata)
1.1       nicm      107: {
1.8     ! nicm      108:        struct client   *c;
1.1       nicm      109:
1.8     ! nicm      110:        if (cdata == NULL)
1.1       nicm      111:                return;
1.3       nicm      112:        if (cdata->client->flags & CLIENT_DEAD)
1.1       nicm      113:                return;
                    114:
1.8     ! nicm      115:        if (cdata->idx > ARRAY_LENGTH(&clients) - 1)
1.1       nicm      116:                return;
1.8     ! nicm      117:        c = ARRAY_ITEM(&clients, cdata->idx);
1.3       nicm      118:        if (c == NULL || c->session == NULL)
1.1       nicm      119:                return;
                    120:
1.8     ! nicm      121:        xasprintf(&cdata->raw_format, "%s", c->tty.path);
        !           122:        window_choose_ctx(cdata);
1.1       nicm      123: }
                    124:
                    125: void
1.8     ! nicm      126: cmd_choose_client_free(struct window_choose_data *cdata)
1.1       nicm      127: {
1.8     ! nicm      128:        if (cdata == NULL)
        !           129:                return;
1.1       nicm      130:
1.3       nicm      131:        cdata->client->references--;
1.8     ! nicm      132:
        !           133:        xfree(cdata->ft_template);
        !           134:        xfree(cdata->action);
        !           135:        format_free(cdata->ft);
1.1       nicm      136:        xfree(cdata);
                    137: }