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

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