[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.6

1.6     ! nicm        1: /* $OpenBSD: cmd-choose-window.c,v 1.5 2009/07/26 12:58:44 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>
                     20:
                     21: #include "tmux.h"
                     22:
                     23: /*
                     24:  * Enter choice mode to choose a window.
                     25:  */
                     26:
                     27: int    cmd_choose_window_exec(struct cmd *, struct cmd_ctx *);
                     28:
                     29: void   cmd_choose_window_callback(void *, int);
                     30:
                     31: const struct cmd_entry cmd_choose_window_entry = {
                     32:        "choose-window", NULL,
                     33:        CMD_TARGET_WINDOW_USAGE,
1.2       nicm       34:        0, 0,
1.1       nicm       35:        cmd_target_init,
                     36:        cmd_target_parse,
                     37:        cmd_choose_window_exec,
                     38:        cmd_target_free,
                     39:        cmd_target_print
                     40: };
                     41:
                     42: struct cmd_choose_window_data {
                     43:        u_int   session;
                     44: };
                     45:
                     46: int
                     47: cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx)
                     48: {
                     49:        struct cmd_target_data          *data = self->data;
                     50:        struct cmd_choose_window_data   *cdata;
                     51:        struct session                  *s;
                     52:        struct winlink                  *wl, *wm;
                     53:        struct window                   *w;
                     54:        u_int                            idx, cur;
1.6     ! nicm       55:        char                             flag, *title;
        !            56:        const char                      *left, *right;
1.1       nicm       57:
                     58:        if (ctx->curclient == NULL) {
                     59:                ctx->error(ctx, "must be run interactively");
                     60:                return (-1);
                     61:        }
                     62:        s = ctx->curclient->session;
                     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:        RB_FOREACH(wm, winlinks, &s->windows) {
                     72:                w = wm->window;
                     73:
                     74:                if (wm == s->curw)
                     75:                        cur = idx;
                     76:                idx++;
                     77:
1.6     ! nicm       78:                flag = ' ';
        !            79:                if (session_alert_has(s, wm, WINDOW_ACTIVITY))
        !            80:                        flag = '#';
        !            81:                else if (session_alert_has(s, wm, WINDOW_BELL))
        !            82:                        flag = '!';
        !            83:                else if (session_alert_has(s, wm, WINDOW_CONTENT))
        !            84:                        flag = '+';
        !            85:                else if (wm == s->curw)
        !            86:                        flag = '*';
        !            87:                else if (wm == SLIST_FIRST(&s->lastw))
        !            88:                        flag = '-';
        !            89:
        !            90:                title = w->active->screen->title;
        !            91:                if (wm == wl)
        !            92:                        title = w->active->base.title;
        !            93:                left = " \"";
        !            94:                right = "\"";
        !            95:                if (*title == '\0')
        !            96:                        left = right = "";
        !            97:
1.1       nicm       98:                window_choose_add(wl->window->active,
1.6     ! nicm       99:                    wm->idx, "%3d: %s%c [%ux%u] (%u panes)%s%s%s",
        !           100:                    wm->idx, w->name, flag, w->sx, w->sy, window_count_panes(w),
        !           101:                    left, title, right);
1.1       nicm      102:        }
                    103:
                    104:        cdata = xmalloc(sizeof *cdata);
                    105:        if (session_index(s, &cdata->session) != 0)
                    106:                fatalx("session not found");
                    107:
                    108:        window_choose_ready(
1.3       nicm      109:            wl->window->active, cur, cmd_choose_window_callback, xfree, cdata);
1.1       nicm      110:
                    111:        return (0);
                    112: }
                    113:
                    114: void
                    115: cmd_choose_window_callback(void *data, int idx)
                    116: {
                    117:        struct cmd_choose_window_data   *cdata = data;
                    118:        struct session                  *s;
                    119:
                    120:        if (idx != -1 && cdata->session <= ARRAY_LENGTH(&sessions) - 1) {
                    121:                s = ARRAY_ITEM(&sessions, cdata->session);
                    122:                if (s != NULL && session_select(s, idx) == 0)
                    123:                        server_redraw_session(s);
                    124:                recalculate_sizes();
                    125:        }
                    126: }