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

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