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

Annotation of src/usr.bin/tmux/cmd-find-window.c, Revision 1.41

1.41    ! nicm        1: /* $OpenBSD: cmd-find-window.c,v 1.40 2017/04/22 08:56:24 nicm Exp $ */
1.1       nicm        2:
                      3: /*
1.37      nicm        4:  * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
1.1       nicm        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:
1.2       nicm       21: #include <fnmatch.h>
1.16      nicm       22: #include <stdlib.h>
1.1       nicm       23: #include <string.h>
                     24:
                     25: #include "tmux.h"
                     26:
                     27: /*
                     28:  * Find window containing text.
                     29:  */
1.29      nicm       30:
                     31: #define FIND_WINDOW_TEMPLATE                                   \
                     32:        "#{window_index}: #{window_name} "                      \
                     33:        "[#{window_width}x#{window_height}] "                   \
                     34:        "(#{window_panes} panes) #{window_find_matches}"
1.1       nicm       35:
1.39      nicm       36: static enum cmd_retval cmd_find_window_exec(struct cmd *, struct cmdq_item *);
1.1       nicm       37:
1.39      nicm       38: static void            cmd_find_window_callback(struct window_choose_data *);
1.1       nicm       39:
1.9       nicm       40: /* Flags for determining matching behavior. */
                     41: #define CMD_FIND_WINDOW_BY_TITLE   0x1
                     42: #define CMD_FIND_WINDOW_BY_CONTENT 0x2
                     43: #define CMD_FIND_WINDOW_BY_NAME    0x4
                     44:
                     45: #define CMD_FIND_WINDOW_ALL            \
                     46:        (CMD_FIND_WINDOW_BY_TITLE |     \
                     47:         CMD_FIND_WINDOW_BY_CONTENT |   \
                     48:         CMD_FIND_WINDOW_BY_NAME)
                     49:
1.1       nicm       50: const struct cmd_entry cmd_find_window_entry = {
1.35      nicm       51:        .name = "find-window",
                     52:        .alias = "findw",
                     53:
                     54:        .args = { "F:CNt:T", 1, 4 },
                     55:        .usage = "[-CNT] [-F format] " CMD_TARGET_WINDOW_USAGE " match-string",
                     56:
1.41    ! nicm       57:        .target = { 't', CMD_FIND_WINDOW, 0 },
1.36      nicm       58:
                     59:        .flags = 0,
1.35      nicm       60:        .exec = cmd_find_window_exec
1.1       nicm       61: };
                     62:
1.19      nicm       63: struct cmd_find_window_data {
                     64:        struct winlink  *wl;
                     65:        char            *list_ctx;
                     66:        u_int            pane_id;
1.33      nicm       67:        TAILQ_ENTRY(cmd_find_window_data) entry;
1.19      nicm       68: };
1.33      nicm       69: TAILQ_HEAD(cmd_find_window_list, cmd_find_window_data);
1.19      nicm       70:
1.38      nicm       71: static u_int   cmd_find_window_match_flags(struct args *);
                     72: static void    cmd_find_window_match(struct cmd_find_window_list *, int,
                     73:                    struct winlink *, const char *, const char *);
1.19      nicm       74:
1.38      nicm       75: static u_int
1.9       nicm       76: cmd_find_window_match_flags(struct args *args)
                     77: {
                     78:        u_int   match_flags = 0;
                     79:
                     80:        /* Turn on flags based on the options. */
                     81:        if (args_has(args, 'T'))
                     82:                match_flags |= CMD_FIND_WINDOW_BY_TITLE;
                     83:        if (args_has(args, 'C'))
                     84:                match_flags |= CMD_FIND_WINDOW_BY_CONTENT;
                     85:        if (args_has(args, 'N'))
                     86:                match_flags |= CMD_FIND_WINDOW_BY_NAME;
                     87:
                     88:        /* If none of the flags were set, default to matching anything. */
                     89:        if (match_flags == 0)
                     90:                match_flags = CMD_FIND_WINDOW_ALL;
                     91:
1.12      nicm       92:        return (match_flags);
1.9       nicm       93: }
                     94:
1.38      nicm       95: static void
1.33      nicm       96: cmd_find_window_match(struct cmd_find_window_list *find_list,
1.26      nicm       97:     int match_flags, struct winlink *wl, const char *str,
                     98:     const char *searchstr)
1.19      nicm       99: {
1.33      nicm      100:        struct cmd_find_window_data     *find_data;
1.19      nicm      101:        struct window_pane              *wp;
                    102:        u_int                            i, line;
                    103:        char                            *sres;
                    104:
1.33      nicm      105:        find_data = xcalloc(1, sizeof *find_data);
1.19      nicm      106:
                    107:        i = 0;
                    108:        TAILQ_FOREACH(wp, &wl->window->panes, entry) {
                    109:                i++;
                    110:
                    111:                if ((match_flags & CMD_FIND_WINDOW_BY_NAME) &&
                    112:                    fnmatch(searchstr, wl->window->name, 0) == 0) {
1.33      nicm      113:                        find_data->list_ctx = xstrdup("");
1.19      nicm      114:                        break;
                    115:                }
                    116:
                    117:                if ((match_flags & CMD_FIND_WINDOW_BY_TITLE) &&
                    118:                    fnmatch(searchstr, wp->base.title, 0) == 0) {
1.33      nicm      119:                        xasprintf(&find_data->list_ctx,
1.19      nicm      120:                            "pane %u title: \"%s\"", i - 1, wp->base.title);
                    121:                        break;
                    122:                }
                    123:
                    124:                if (match_flags & CMD_FIND_WINDOW_BY_CONTENT &&
                    125:                    (sres = window_pane_search(wp, str, &line)) != NULL) {
1.33      nicm      126:                        xasprintf(&find_data->list_ctx,
1.19      nicm      127:                            "pane %u line %u: \"%s\"", i - 1, line + 1, sres);
                    128:                        free(sres);
                    129:                        break;
                    130:                }
                    131:        }
1.33      nicm      132:
                    133:        if (find_data->list_ctx != NULL) {
                    134:                find_data->wl = wl;
                    135:                find_data->pane_id = i - 1;
                    136:                TAILQ_INSERT_TAIL(find_list, find_data, entry);
                    137:        } else
                    138:                free(find_data);
1.19      nicm      139: }
                    140:
1.38      nicm      141: static enum cmd_retval
1.39      nicm      142: cmd_find_window_exec(struct cmd *self, struct cmdq_item *item)
1.1       nicm      143: {
1.8       nicm      144:        struct args                     *args = self->args;
1.40      nicm      145:        struct cmd_find_state           *current = &item->shared->current;
1.41    ! nicm      146:        struct client                   *c = cmd_find_client(item, NULL, 1);
1.15      nicm      147:        struct window_choose_data       *cdata;
1.41    ! nicm      148:        struct session                  *s = item->target.s;
        !           149:        struct winlink                  *wl = item->target.wl, *wm;
1.33      nicm      150:        struct cmd_find_window_list      find_list;
                    151:        struct cmd_find_window_data     *find_data;
                    152:        struct cmd_find_window_data     *find_data1;
1.19      nicm      153:        char                            *str, *searchstr;
1.13      nicm      154:        const char                      *template;
1.19      nicm      155:        u_int                            i, match_flags;
1.1       nicm      156:
1.34      nicm      157:        if (c == NULL) {
1.39      nicm      158:                cmdq_error(item, "no client available");
1.17      nicm      159:                return (CMD_RETURN_ERROR);
1.1       nicm      160:        }
                    161:
1.13      nicm      162:        if ((template = args_get(args, 'F')) == NULL)
1.18      nicm      163:                template = FIND_WINDOW_TEMPLATE;
1.13      nicm      164:
1.9       nicm      165:        match_flags = cmd_find_window_match_flags(args);
1.8       nicm      166:        str = args->argv[0];
                    167:
1.33      nicm      168:        TAILQ_INIT(&find_list);
1.1       nicm      169:
1.8       nicm      170:        xasprintf(&searchstr, "*%s*", str);
1.19      nicm      171:        RB_FOREACH(wm, winlinks, &s->windows)
1.30      nicm      172:            cmd_find_window_match(&find_list, match_flags, wm, str, searchstr);
1.16      nicm      173:        free(searchstr);
1.1       nicm      174:
1.33      nicm      175:        if (TAILQ_EMPTY(&find_list)) {
1.39      nicm      176:                cmdq_error(item, "no windows matching: %s", str);
1.17      nicm      177:                return (CMD_RETURN_ERROR);
1.1       nicm      178:        }
                    179:
1.33      nicm      180:        if (TAILQ_NEXT(TAILQ_FIRST(&find_list), entry) == NULL) {
1.40      nicm      181:                if (session_select(s, TAILQ_FIRST(&find_list)->wl->idx) == 0) {
                    182:                        cmd_find_from_session(current, s);
1.1       nicm      183:                        server_redraw_session(s);
1.40      nicm      184:                }
1.1       nicm      185:                recalculate_sizes();
                    186:                goto out;
                    187:        }
                    188:
                    189:        if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
                    190:                goto out;
                    191:
1.33      nicm      192:        i = 0;
                    193:        TAILQ_FOREACH(find_data, &find_list, entry) {
1.21      nicm      194:                cdata = window_choose_data_create(TREE_OTHER, c, c->session);
1.33      nicm      195:                cdata->idx = find_data->wl->idx;
                    196:                cdata->wl = find_data->wl;
1.15      nicm      197:
                    198:                cdata->ft_template = xstrdup(template);
1.33      nicm      199:                cdata->pane_id = find_data->pane_id;
1.19      nicm      200:
1.15      nicm      201:                format_add(cdata->ft, "line", "%u", i);
                    202:                format_add(cdata->ft, "window_find_matches", "%s",
1.33      nicm      203:                    find_data->list_ctx);
                    204:                format_defaults(cdata->ft, NULL, s, find_data->wl, NULL);
1.13      nicm      205:
1.15      nicm      206:                window_choose_add(wl->window->active, cdata);
1.33      nicm      207:
                    208:                i++;
1.1       nicm      209:        }
                    210:
1.22      nicm      211:        window_choose_ready(wl->window->active, 0, cmd_find_window_callback);
1.1       nicm      212:
                    213: out:
1.33      nicm      214:        TAILQ_FOREACH_SAFE(find_data, &find_list, entry, find_data1) {
                    215:                free(find_data->list_ctx);
                    216:                TAILQ_REMOVE(&find_list, find_data, entry);
                    217:                free(find_data);
                    218:        }
1.17      nicm      219:        return (CMD_RETURN_NORMAL);
1.1       nicm      220: }
                    221:
1.38      nicm      222: static void
1.15      nicm      223: cmd_find_window_callback(struct window_choose_data *cdata)
1.1       nicm      224: {
1.19      nicm      225:        struct session          *s;
                    226:        struct window_pane      *wp;
1.7       nicm      227:
1.15      nicm      228:        if (cdata == NULL)
1.7       nicm      229:                return;
1.15      nicm      230:
1.21      nicm      231:        s = cdata->start_session;
1.7       nicm      232:        if (!session_alive(s))
                    233:                return;
1.19      nicm      234:
                    235:        wp = window_pane_at_index(cdata->wl->window, cdata->pane_id);
                    236:        if (wp != NULL && window_pane_visible(wp))
                    237:                window_set_active_pane(cdata->wl->window, wp);
1.1       nicm      238:
1.15      nicm      239:        if (session_select(s, cdata->idx) == 0) {
1.7       nicm      240:                server_redraw_session(s);
1.1       nicm      241:                recalculate_sizes();
                    242:        }
                    243: }