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

Annotation of src/usr.bin/tmux/cmd-choose-tree.c, Revision 1.15

1.15    ! nicm        1: /* $OpenBSD: cmd-choose-tree.c,v 1.14 2013/03/21 16:08:24 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2012 Thomas Adam <thomas@xteddy.org>
                      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 <ctype.h>
1.3       nicm       22: #include <stdlib.h>
1.1       nicm       23:
                     24: #include <string.h>
                     25:
                     26: #include "tmux.h"
                     27:
                     28: #define CMD_CHOOSE_TREE_WINDOW_ACTION "select-window -t '%%'"
                     29: #define CMD_CHOOSE_TREE_SESSION_ACTION "switch-client -t '%%'"
                     30:
                     31: /*
                     32:  * Enter choice mode to choose a session and/or window.
                     33:  */
                     34:
1.6       nicm       35: enum cmd_retval        cmd_choose_tree_exec(struct cmd *, struct cmd_ctx *);
1.1       nicm       36:
                     37: const struct cmd_entry cmd_choose_tree_entry = {
                     38:        "choose-tree", NULL,
1.12      nicm       39:        "S:W:swub:c:t:", 0, 1,
                     40:        "[-swu] [-b session-template] [-c window template] [-S format] " \
1.2       nicm       41:        "[-W format] " CMD_TARGET_WINDOW_USAGE,
1.1       nicm       42:        0,
                     43:        NULL,
                     44:        NULL,
                     45:        cmd_choose_tree_exec
                     46: };
                     47:
                     48: const struct cmd_entry cmd_choose_session_entry = {
                     49:        "choose-session", NULL,
                     50:        "F:t:", 0, 1,
                     51:        CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
                     52:        0,
                     53:        NULL,
                     54:        NULL,
                     55:        cmd_choose_tree_exec
                     56: };
                     57:
                     58: const struct cmd_entry cmd_choose_window_entry = {
                     59:        "choose-window", NULL,
                     60:        "F:t:", 0, 1,
                     61:        CMD_TARGET_WINDOW_USAGE "[-F format] [template]",
                     62:        0,
                     63:        NULL,
                     64:        NULL,
                     65:        cmd_choose_tree_exec
                     66: };
                     67:
1.4       nicm       68: enum cmd_retval
1.1       nicm       69: cmd_choose_tree_exec(struct cmd *self, struct cmd_ctx *ctx)
                     70: {
                     71:        struct args                     *args = self->args;
                     72:        struct winlink                  *wl, *wm;
                     73:        struct session                  *s, *s2;
                     74:        struct window_choose_data       *wcd = NULL;
                     75:        const char                      *ses_template, *win_template;
1.8       nicm       76:        char                            *final_win_action, *cur_win_template;
                     77:        char                            *final_win_template_middle;
                     78:        char                            *final_win_template_last;
1.1       nicm       79:        const char                      *ses_action, *win_action;
1.8       nicm       80:        u_int                            cur_win, idx_ses, win_ses, win_max;
1.1       nicm       81:        u_int                            wflag, sflag;
                     82:
                     83:        ses_template = win_template = NULL;
                     84:        ses_action = win_action = NULL;
                     85:
                     86:        if (ctx->curclient == NULL) {
                     87:                ctx->error(ctx, "must be run interactively");
1.4       nicm       88:                return (CMD_RETURN_ERROR);
1.1       nicm       89:        }
                     90:
                     91:        s = ctx->curclient->session;
                     92:
                     93:        if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
1.4       nicm       94:                return (CMD_RETURN_ERROR);
1.1       nicm       95:
                     96:        if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
1.4       nicm       97:                return (CMD_RETURN_NORMAL);
1.1       nicm       98:
                     99:        /* Sort out which command this is. */
                    100:        wflag = sflag = 0;
                    101:        if (self->entry == &cmd_choose_session_entry) {
                    102:                sflag = 1;
                    103:                if ((ses_template = args_get(args, 'F')) == NULL)
1.7       nicm      104:                        ses_template = CHOOSE_TREE_SESSION_TEMPLATE;
1.1       nicm      105:
                    106:                if (args->argc != 0)
                    107:                        ses_action = args->argv[0];
                    108:                else
                    109:                        ses_action = CMD_CHOOSE_TREE_SESSION_ACTION;
                    110:        } else if (self->entry == &cmd_choose_window_entry) {
                    111:                wflag = 1;
                    112:                if ((win_template = args_get(args, 'F')) == NULL)
1.7       nicm      113:                        win_template = CHOOSE_TREE_WINDOW_TEMPLATE;
1.1       nicm      114:
                    115:                if (args->argc != 0)
                    116:                        win_action = args->argv[0];
                    117:                else
                    118:                        win_action = CMD_CHOOSE_TREE_WINDOW_ACTION;
                    119:        } else {
                    120:                wflag = args_has(args, 'w');
                    121:                sflag = args_has(args, 's');
                    122:
                    123:                if ((ses_action = args_get(args, 'b')) == NULL)
                    124:                        ses_action = CMD_CHOOSE_TREE_SESSION_ACTION;
                    125:
                    126:                if ((win_action = args_get(args, 'c')) == NULL)
                    127:                        win_action = CMD_CHOOSE_TREE_WINDOW_ACTION;
                    128:
                    129:                if ((ses_template = args_get(args, 'S')) == NULL)
1.7       nicm      130:                        ses_template = CHOOSE_TREE_SESSION_TEMPLATE;
1.1       nicm      131:
                    132:                if ((win_template = args_get(args, 'W')) == NULL)
1.7       nicm      133:                        win_template = CHOOSE_TREE_WINDOW_TEMPLATE;
1.1       nicm      134:        }
                    135:
                    136:        /*
                    137:         * If not asking for windows and sessions, assume no "-ws" given and
                    138:         * hence display the entire tree outright.
                    139:         */
                    140:        if (!wflag && !sflag)
                    141:                wflag = sflag = 1;
                    142:
                    143:        /*
                    144:         * If we're drawing in tree mode, including sessions, then pad the
                    145:         * window template, otherwise just render the windows as a flat list
                    146:         * without any padding.
                    147:         */
1.8       nicm      148:        if (wflag && sflag) {
1.11      nicm      149:                xasprintf(&final_win_template_middle,
                    150:                    " \001tq\001> %s", win_template);
                    151:                xasprintf(&final_win_template_last,
                    152:                    " \001mq\001> %s", win_template);
1.8       nicm      153:        } else if (wflag) {
                    154:                final_win_template_middle = xstrdup(win_template);
                    155:                final_win_template_last = xstrdup(win_template);
                    156:        } else
                    157:                final_win_template_middle = final_win_template_last = NULL;
1.1       nicm      158:
                    159:        idx_ses = cur_win = -1;
                    160:        RB_FOREACH(s2, sessions, &sessions) {
                    161:                idx_ses++;
                    162:
                    163:                /*
                    164:                 * If we're just choosing windows, jump straight there. Note
                    165:                 * that this implies the current session, so only choose
                    166:                 * windows when the session matches this one.
                    167:                 */
                    168:                if (wflag && !sflag) {
                    169:                        if (s != s2)
                    170:                                continue;
                    171:                        goto windows_only;
                    172:                }
                    173:
                    174:                wcd = window_choose_add_session(wl->window->active,
1.8       nicm      175:                    ctx, s2, ses_template, (char *)ses_action, idx_ses);
1.1       nicm      176:
                    177:                /* If we're just choosing sessions, skip choosing windows. */
                    178:                if (sflag && !wflag) {
                    179:                        if (s == s2)
                    180:                                cur_win = idx_ses;
                    181:                        continue;
                    182:                }
                    183: windows_only:
1.8       nicm      184:                win_ses = win_max = -1;
                    185:                RB_FOREACH(wm, winlinks, &s2->windows)
                    186:                        win_max++;
1.1       nicm      187:                RB_FOREACH(wm, winlinks, &s2->windows) {
                    188:                        win_ses++;
                    189:                        if (sflag && wflag)
                    190:                                idx_ses++;
                    191:
                    192:                        if (wm == s2->curw && s == s2) {
                    193:                                if (wflag && !sflag) {
                    194:                                        /*
                    195:                                         * Then we're only counting windows.
                    196:                                         * So remember which is the current
                    197:                                         * window in the list.
                    198:                                         */
                    199:                                        cur_win = win_ses;
                    200:                                } else
                    201:                                        cur_win = idx_ses;
                    202:                        }
                    203:
                    204:                        xasprintf(&final_win_action, "%s ; %s", win_action,
1.8       nicm      205:                            wcd ? wcd->command : "");
                    206:
                    207:                        if (win_ses != win_max)
                    208:                                cur_win_template = final_win_template_middle;
                    209:                        else
                    210:                                cur_win_template = final_win_template_last;
1.1       nicm      211:
                    212:                        window_choose_add_window(wl->window->active,
1.8       nicm      213:                            ctx, s2, wm, cur_win_template,
1.10      nicm      214:                            final_win_action,
                    215:                            (wflag && !sflag) ? win_ses : idx_ses);
1.1       nicm      216:
1.3       nicm      217:                        free(final_win_action);
1.1       nicm      218:                }
1.13      nicm      219:
1.1       nicm      220:                /*
                    221:                 * If we're just drawing windows, don't consider moving on to
                    222:                 * other sessions as we only list windows in this session.
                    223:                 */
                    224:                if (wflag && !sflag)
                    225:                        break;
                    226:        }
1.8       nicm      227:        free(final_win_template_middle);
                    228:        free(final_win_template_last);
1.1       nicm      229:
1.15    ! nicm      230:        window_choose_ready(wl->window->active, cur_win, NULL);
1.12      nicm      231:
                    232:        if (args_has(args, 'u'))
                    233:                window_choose_expand_all(wl->window->active);
1.1       nicm      234:
1.4       nicm      235:        return (CMD_RETURN_NORMAL);
1.1       nicm      236: }