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

Annotation of src/usr.bin/tmux/cmd-choose-window.c, Revision 1.21

1.21    ! nicm        1: /* $OpenBSD: cmd-choose-window.c,v 1.20 2012/05/29 08:15:45 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:
1.21    ! nicm       31: void   cmd_choose_window_callback(struct window_choose_data *);
        !            32: void   cmd_choose_window_free(struct window_choose_data *);
1.1       nicm       33:
                     34: const struct cmd_entry cmd_choose_window_entry = {
                     35:        "choose-window", NULL,
1.19      nicm       36:        "F:t:", 0, 1,
                     37:        CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
1.18      nicm       38:        0,
                     39:        NULL,
                     40:        NULL,
                     41:        cmd_choose_window_exec
1.1       nicm       42: };
                     43:
                     44: int
                     45: cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx)
                     46: {
1.18      nicm       47:        struct args                     *args = self->args;
1.21    ! nicm       48:        struct window_choose_data       *cdata;
1.1       nicm       49:        struct session                  *s;
                     50:        struct winlink                  *wl, *wm;
1.19      nicm       51:        const char                      *template;
1.1       nicm       52:        u_int                            idx, cur;
                     53:
                     54:        if (ctx->curclient == NULL) {
                     55:                ctx->error(ctx, "must be run interactively");
                     56:                return (-1);
                     57:        }
                     58:        s = ctx->curclient->session;
                     59:
1.18      nicm       60:        if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
1.1       nicm       61:                return (-1);
                     62:
                     63:        if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
                     64:                return (0);
                     65:
1.19      nicm       66:        if ((template = args_get(args, 'F')) == NULL)
1.20      nicm       67:                template = DEFAULT_WINDOW_TEMPLATE " \"#{pane_title}\"";
1.19      nicm       68:
1.1       nicm       69:        cur = idx = 0;
                     70:        RB_FOREACH(wm, winlinks, &s->windows) {
                     71:                if (wm == s->curw)
                     72:                        cur = idx;
                     73:                idx++;
                     74:
1.21    ! nicm       75:                cdata = window_choose_data_create(ctx);
        !            76:                if (args->argc != 0)
        !            77:                        cdata->action = xstrdup(args->argv[0]);
        !            78:                else
        !            79:                        cdata->action = xstrdup("select-window -t '%%'");
        !            80:
        !            81:                cdata->idx = wm->idx;
        !            82:                cdata->client->references++;
        !            83:                cdata->session->references++;
        !            84:
        !            85:                cdata->ft_template = xstrdup(template);
        !            86:                format_add(cdata->ft, "line", "%u", idx);
        !            87:                format_session(cdata->ft, s);
        !            88:                format_winlink(cdata->ft, s, wm);
        !            89:                format_window_pane(cdata->ft, wm->window->active);
1.19      nicm       90:
1.21    ! nicm       91:                window_choose_add(wl->window->active, cdata);
1.1       nicm       92:        }
1.12      nicm       93:        window_choose_ready(wl->window->active,
1.21    ! nicm       94:            cur, cmd_choose_window_callback, cmd_choose_window_free);
1.1       nicm       95:
1.12      nicm       96:        return (0);
1.1       nicm       97: }
                     98:
                     99: void
1.21    ! nicm      100: cmd_choose_window_callback(struct window_choose_data *cdata)
1.1       nicm      101: {
1.21    ! nicm      102:        struct session  *s;
1.7       nicm      103:
1.21    ! nicm      104:        if (cdata == NULL)
1.16      nicm      105:                return;
1.9       nicm      106:        if (cdata->client->flags & CLIENT_DEAD)
1.12      nicm      107:                return;
1.7       nicm      108:
1.21    ! nicm      109:        s = cdata->session;
        !           110:        if (!session_alive(s))
1.7       nicm      111:                return;
                    112:
1.21    ! nicm      113:        xasprintf(&cdata->raw_format, "%s:%u", s->name, cdata->idx);
        !           114:        window_choose_ctx(cdata);
1.7       nicm      115: }
1.1       nicm      116:
1.7       nicm      117: void
1.21    ! nicm      118: cmd_choose_window_free(struct window_choose_data *cdata)
1.7       nicm      119: {
1.21    ! nicm      120:        if (cdata == NULL)
        !           121:                return;
1.7       nicm      122:
1.9       nicm      123:        cdata->session->references--;
                    124:        cdata->client->references--;
1.21    ! nicm      125:
        !           126:        xfree(cdata->ft_template);
        !           127:        xfree(cdata->action);
        !           128:        format_free(cdata->ft);
1.7       nicm      129:        xfree(cdata);
1.1       nicm      130: }