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

1.30    ! nicm        1: /* $OpenBSD: cmd-choose-tree.c,v 1.29 2015/12/13 14:32:38 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:  */
1.27      nicm       34:
                     35: #define CHOOSE_TREE_SESSION_TEMPLATE                           \
                     36:        "#{session_name}: #{session_windows} windows"           \
                     37:        "#{?session_grouped, (group ,}"                         \
                     38:        "#{session_group}#{?session_grouped,),}"                \
                     39:        "#{?session_attached, (attached),}"
                     40: #define CHOOSE_TREE_WINDOW_TEMPLATE                            \
                     41:        "#{window_index}: #{window_name}#{window_flags} "       \
                     42:        "\"#{pane_title}\""
1.1       nicm       43:
1.21      nicm       44: enum cmd_retval        cmd_choose_tree_exec(struct cmd *, struct cmd_q *);
1.1       nicm       45:
                     46: const struct cmd_entry cmd_choose_tree_entry = {
1.30    ! nicm       47:        .name = "choose-tree",
        !            48:        .alias = NULL,
        !            49:
        !            50:        .args = { "S:W:swub:c:t:", 0, 1 },
        !            51:        .usage = "[-suw] [-b session-template] [-c window template] "
        !            52:                 "[-S format] [-W format] " CMD_TARGET_WINDOW_USAGE,
        !            53:
        !            54:        .flags = CMD_WINDOW_T,
        !            55:        .exec = cmd_choose_tree_exec
1.1       nicm       56: };
                     57:
                     58: const struct cmd_entry cmd_choose_session_entry = {
1.30    ! nicm       59:        .name = "choose-session",
        !            60:        .alias = NULL,
        !            61:
        !            62:        .args = { "F:t:", 0, 1 },
        !            63:
        !            64:        .usage = CMD_TARGET_WINDOW_USAGE " [-F format] [template]",
        !            65:        .flags = CMD_WINDOW_T,
        !            66:
        !            67:        .exec = cmd_choose_tree_exec
1.1       nicm       68: };
                     69:
                     70: const struct cmd_entry cmd_choose_window_entry = {
1.30    ! nicm       71:        .name = "choose-window",
        !            72:        .alias = NULL,
        !            73:
        !            74:        .args = { "F:t:", 0, 1 },
        !            75:        .usage = CMD_TARGET_WINDOW_USAGE "[-F format] [template]",
        !            76:
        !            77:        .flags = CMD_WINDOW_T,
        !            78:        .exec = cmd_choose_tree_exec
1.1       nicm       79: };
                     80:
1.4       nicm       81: enum cmd_retval
1.21      nicm       82: cmd_choose_tree_exec(struct cmd *self, struct cmd_q *cmdq)
1.1       nicm       83: {
                     84:        struct args                     *args = self->args;
1.29      nicm       85:        struct client                   *c = cmdq->state.c;
                     86:        struct winlink                  *wl = cmdq->state.tflag.wl, *wm;
                     87:        struct session                  *s = cmdq->state.tflag.s, *s2;
1.1       nicm       88:        struct window_choose_data       *wcd = NULL;
                     89:        const char                      *ses_template, *win_template;
1.8       nicm       90:        char                            *final_win_action, *cur_win_template;
                     91:        char                            *final_win_template_middle;
                     92:        char                            *final_win_template_last;
1.1       nicm       93:        const char                      *ses_action, *win_action;
1.8       nicm       94:        u_int                            cur_win, idx_ses, win_ses, win_max;
1.1       nicm       95:        u_int                            wflag, sflag;
                     96:
                     97:        ses_template = win_template = NULL;
                     98:        ses_action = win_action = NULL;
                     99:
1.29      nicm      100:        if (c == NULL) {
1.21      nicm      101:                cmdq_error(cmdq, "no client available");
1.4       nicm      102:                return (CMD_RETURN_ERROR);
1.1       nicm      103:        }
                    104:
                    105:        if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
1.4       nicm      106:                return (CMD_RETURN_NORMAL);
1.1       nicm      107:
                    108:        /* Sort out which command this is. */
                    109:        wflag = sflag = 0;
                    110:        if (self->entry == &cmd_choose_session_entry) {
                    111:                sflag = 1;
                    112:                if ((ses_template = args_get(args, 'F')) == NULL)
1.7       nicm      113:                        ses_template = CHOOSE_TREE_SESSION_TEMPLATE;
1.1       nicm      114:
                    115:                if (args->argc != 0)
                    116:                        ses_action = args->argv[0];
                    117:                else
                    118:                        ses_action = CMD_CHOOSE_TREE_SESSION_ACTION;
                    119:        } else if (self->entry == &cmd_choose_window_entry) {
                    120:                wflag = 1;
                    121:                if ((win_template = args_get(args, 'F')) == NULL)
1.7       nicm      122:                        win_template = CHOOSE_TREE_WINDOW_TEMPLATE;
1.1       nicm      123:
                    124:                if (args->argc != 0)
                    125:                        win_action = args->argv[0];
                    126:                else
                    127:                        win_action = CMD_CHOOSE_TREE_WINDOW_ACTION;
                    128:        } else {
                    129:                wflag = args_has(args, 'w');
                    130:                sflag = args_has(args, 's');
                    131:
                    132:                if ((ses_action = args_get(args, 'b')) == NULL)
                    133:                        ses_action = CMD_CHOOSE_TREE_SESSION_ACTION;
                    134:
                    135:                if ((win_action = args_get(args, 'c')) == NULL)
                    136:                        win_action = CMD_CHOOSE_TREE_WINDOW_ACTION;
                    137:
                    138:                if ((ses_template = args_get(args, 'S')) == NULL)
1.7       nicm      139:                        ses_template = CHOOSE_TREE_SESSION_TEMPLATE;
1.1       nicm      140:
                    141:                if ((win_template = args_get(args, 'W')) == NULL)
1.7       nicm      142:                        win_template = CHOOSE_TREE_WINDOW_TEMPLATE;
1.1       nicm      143:        }
                    144:
                    145:        /*
                    146:         * If not asking for windows and sessions, assume no "-ws" given and
                    147:         * hence display the entire tree outright.
                    148:         */
                    149:        if (!wflag && !sflag)
                    150:                wflag = sflag = 1;
                    151:
                    152:        /*
                    153:         * If we're drawing in tree mode, including sessions, then pad the
                    154:         * window template, otherwise just render the windows as a flat list
                    155:         * without any padding.
                    156:         */
1.8       nicm      157:        if (wflag && sflag) {
1.11      nicm      158:                xasprintf(&final_win_template_middle,
                    159:                    " \001tq\001> %s", win_template);
                    160:                xasprintf(&final_win_template_last,
                    161:                    " \001mq\001> %s", win_template);
1.8       nicm      162:        } else if (wflag) {
                    163:                final_win_template_middle = xstrdup(win_template);
                    164:                final_win_template_last = xstrdup(win_template);
                    165:        } else
                    166:                final_win_template_middle = final_win_template_last = NULL;
1.1       nicm      167:
                    168:        idx_ses = cur_win = -1;
                    169:        RB_FOREACH(s2, sessions, &sessions) {
                    170:                idx_ses++;
                    171:
                    172:                /*
                    173:                 * If we're just choosing windows, jump straight there. Note
                    174:                 * that this implies the current session, so only choose
                    175:                 * windows when the session matches this one.
                    176:                 */
                    177:                if (wflag && !sflag) {
                    178:                        if (s != s2)
                    179:                                continue;
                    180:                        goto windows_only;
                    181:                }
                    182:
                    183:                wcd = window_choose_add_session(wl->window->active,
1.18      nicm      184:                    c, s2, ses_template, ses_action, idx_ses);
1.1       nicm      185:
                    186:                /* If we're just choosing sessions, skip choosing windows. */
                    187:                if (sflag && !wflag) {
                    188:                        if (s == s2)
                    189:                                cur_win = idx_ses;
                    190:                        continue;
                    191:                }
                    192: windows_only:
1.8       nicm      193:                win_ses = win_max = -1;
                    194:                RB_FOREACH(wm, winlinks, &s2->windows)
                    195:                        win_max++;
1.1       nicm      196:                RB_FOREACH(wm, winlinks, &s2->windows) {
                    197:                        win_ses++;
                    198:                        if (sflag && wflag)
                    199:                                idx_ses++;
                    200:
                    201:                        if (wm == s2->curw && s == s2) {
                    202:                                if (wflag && !sflag) {
                    203:                                        /*
                    204:                                         * Then we're only counting windows.
                    205:                                         * So remember which is the current
                    206:                                         * window in the list.
                    207:                                         */
                    208:                                        cur_win = win_ses;
                    209:                                } else
                    210:                                        cur_win = idx_ses;
                    211:                        }
                    212:
1.20      nicm      213:                        xasprintf(&final_win_action, "%s %s %s",
                    214:                            wcd != NULL ? wcd->command : "",
                    215:                            wcd != NULL ? ";" : "", win_action);
1.8       nicm      216:
                    217:                        if (win_ses != win_max)
                    218:                                cur_win_template = final_win_template_middle;
                    219:                        else
                    220:                                cur_win_template = final_win_template_last;
1.1       nicm      221:
                    222:                        window_choose_add_window(wl->window->active,
1.16      nicm      223:                            c, s2, wm, cur_win_template,
1.10      nicm      224:                            final_win_action,
                    225:                            (wflag && !sflag) ? win_ses : idx_ses);
1.1       nicm      226:
1.3       nicm      227:                        free(final_win_action);
1.1       nicm      228:                }
1.13      nicm      229:
1.1       nicm      230:                /*
                    231:                 * If we're just drawing windows, don't consider moving on to
                    232:                 * other sessions as we only list windows in this session.
                    233:                 */
                    234:                if (wflag && !sflag)
                    235:                        break;
                    236:        }
1.8       nicm      237:        free(final_win_template_middle);
                    238:        free(final_win_template_last);
1.1       nicm      239:
1.15      nicm      240:        window_choose_ready(wl->window->active, cur_win, NULL);
1.12      nicm      241:
1.23      nicm      242:        if (args_has(args, 'u')) {
1.12      nicm      243:                window_choose_expand_all(wl->window->active);
1.23      nicm      244:                window_choose_set_current(wl->window->active, cur_win);
                    245:        }
1.1       nicm      246:
1.4       nicm      247:        return (CMD_RETURN_NORMAL);
1.1       nicm      248: }