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

1.23    ! nicm        1: /* $OpenBSD: cmd-find-window.c,v 1.22 2013/03/21 16:09:17 nicm Exp $ */
1.1       nicm        2:
                      3: /*
                      4:  * Copyright (c) 2009 Nicholas Marriott <nicm@users.sourceforge.net>
                      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:  */
                     30:
1.17      nicm       31: enum cmd_retval         cmd_find_window_exec(struct cmd *, struct cmd_ctx *);
1.1       nicm       32:
1.15      nicm       33: void   cmd_find_window_callback(struct window_choose_data *);
1.1       nicm       34:
1.9       nicm       35: /* Flags for determining matching behavior. */
                     36: #define CMD_FIND_WINDOW_BY_TITLE   0x1
                     37: #define CMD_FIND_WINDOW_BY_CONTENT 0x2
                     38: #define CMD_FIND_WINDOW_BY_NAME    0x4
                     39:
                     40: #define CMD_FIND_WINDOW_ALL            \
                     41:        (CMD_FIND_WINDOW_BY_TITLE |     \
                     42:         CMD_FIND_WINDOW_BY_CONTENT |   \
                     43:         CMD_FIND_WINDOW_BY_NAME)
                     44:
1.1       nicm       45: const struct cmd_entry cmd_find_window_entry = {
                     46:        "find-window", "findw",
1.13      nicm       47:        "F:CNt:T", 1, 4,
                     48:        "[-CNT] [-F format] " CMD_TARGET_WINDOW_USAGE " match-string",
1.8       nicm       49:        0,
                     50:        NULL,
                     51:        NULL,
                     52:        cmd_find_window_exec
1.1       nicm       53: };
                     54:
1.19      nicm       55: struct cmd_find_window_data {
                     56:        struct winlink  *wl;
                     57:        char            *list_ctx;
                     58:        u_int            pane_id;
                     59: };
                     60: ARRAY_DECL(cmd_find_window_data_list, struct cmd_find_window_data);
                     61:
                     62: u_int  cmd_find_window_match_flags(struct args *);
                     63: void   cmd_find_window_match(struct cmd_find_window_data_list *, int,
                     64:            struct winlink *, const char *, const char *);
                     65:
1.9       nicm       66: u_int
                     67: cmd_find_window_match_flags(struct args *args)
                     68: {
                     69:        u_int   match_flags = 0;
                     70:
                     71:        /* Turn on flags based on the options. */
                     72:        if (args_has(args, 'T'))
                     73:                match_flags |= CMD_FIND_WINDOW_BY_TITLE;
                     74:        if (args_has(args, 'C'))
                     75:                match_flags |= CMD_FIND_WINDOW_BY_CONTENT;
                     76:        if (args_has(args, 'N'))
                     77:                match_flags |= CMD_FIND_WINDOW_BY_NAME;
                     78:
                     79:        /* If none of the flags were set, default to matching anything. */
                     80:        if (match_flags == 0)
                     81:                match_flags = CMD_FIND_WINDOW_ALL;
                     82:
1.12      nicm       83:        return (match_flags);
1.9       nicm       84: }
                     85:
1.19      nicm       86: void
                     87: cmd_find_window_match(struct cmd_find_window_data_list *find_list,
                     88:     int match_flags, struct winlink *wl, const char *str, const char *searchstr)
                     89: {
                     90:        struct cmd_find_window_data      find_data;
                     91:        struct window_pane              *wp;
                     92:        u_int                            i, line;
                     93:        char                            *sres;
                     94:
                     95:        memset(&find_data, 0, sizeof find_data);
                     96:
                     97:        i = 0;
                     98:        TAILQ_FOREACH(wp, &wl->window->panes, entry) {
                     99:                i++;
                    100:
                    101:                if ((match_flags & CMD_FIND_WINDOW_BY_NAME) &&
                    102:                    fnmatch(searchstr, wl->window->name, 0) == 0) {
                    103:                        find_data.list_ctx = xstrdup("");
                    104:                        break;
                    105:                }
                    106:
                    107:                if ((match_flags & CMD_FIND_WINDOW_BY_TITLE) &&
                    108:                    fnmatch(searchstr, wp->base.title, 0) == 0) {
                    109:                        xasprintf(&find_data.list_ctx,
                    110:                            "pane %u title: \"%s\"", i - 1, wp->base.title);
                    111:                        break;
                    112:                }
                    113:
                    114:                if (match_flags & CMD_FIND_WINDOW_BY_CONTENT &&
                    115:                    (sres = window_pane_search(wp, str, &line)) != NULL) {
                    116:                        xasprintf(&find_data.list_ctx,
                    117:                            "pane %u line %u: \"%s\"", i - 1, line + 1, sres);
                    118:                        free(sres);
                    119:                        break;
                    120:                }
                    121:        }
                    122:        if (find_data.list_ctx != NULL) {
                    123:                find_data.wl = wl;
                    124:                find_data.pane_id = i - 1;
                    125:                ARRAY_ADD(find_list, find_data);
                    126:        }
                    127: }
                    128:
1.17      nicm      129: enum cmd_retval
1.1       nicm      130: cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
                    131: {
1.8       nicm      132:        struct args                     *args = self->args;
1.21      nicm      133:        struct client                   *c;
1.15      nicm      134:        struct window_choose_data       *cdata;
1.1       nicm      135:        struct session                  *s;
                    136:        struct winlink                  *wl, *wm;
1.19      nicm      137:        struct cmd_find_window_data_list find_list;
                    138:        char                            *str, *searchstr;
1.13      nicm      139:        const char                      *template;
1.19      nicm      140:        u_int                            i, match_flags;
1.1       nicm      141:
1.23    ! nicm      142:        if ((c = cmd_current_client(ctx)) == NULL) {
        !           143:                ctx->error(ctx, "no client available");
1.17      nicm      144:                return (CMD_RETURN_ERROR);
1.1       nicm      145:        }
1.21      nicm      146:        s = c->session;
1.1       nicm      147:
1.8       nicm      148:        if ((wl = cmd_find_window(ctx, args_get(args, 't'), NULL)) == NULL)
1.17      nicm      149:                return (CMD_RETURN_ERROR);
1.1       nicm      150:
1.13      nicm      151:        if ((template = args_get(args, 'F')) == NULL)
1.18      nicm      152:                template = FIND_WINDOW_TEMPLATE;
1.13      nicm      153:
1.9       nicm      154:        match_flags = cmd_find_window_match_flags(args);
1.8       nicm      155:        str = args->argv[0];
                    156:
1.19      nicm      157:        ARRAY_INIT(&find_list);
1.1       nicm      158:
1.8       nicm      159:        xasprintf(&searchstr, "*%s*", str);
1.19      nicm      160:        RB_FOREACH(wm, winlinks, &s->windows)
                    161:            cmd_find_window_match (&find_list, match_flags, wm, str, searchstr);
1.16      nicm      162:        free(searchstr);
1.1       nicm      163:
1.19      nicm      164:        if (ARRAY_LENGTH(&find_list) == 0) {
1.8       nicm      165:                ctx->error(ctx, "no windows matching: %s", str);
1.19      nicm      166:                ARRAY_FREE(&find_list);
1.17      nicm      167:                return (CMD_RETURN_ERROR);
1.1       nicm      168:        }
                    169:
1.19      nicm      170:        if (ARRAY_LENGTH(&find_list) == 1) {
                    171:                if (session_select(s, ARRAY_FIRST(&find_list).wl->idx) == 0)
1.1       nicm      172:                        server_redraw_session(s);
                    173:                recalculate_sizes();
                    174:                goto out;
                    175:        }
                    176:
                    177:        if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
                    178:                goto out;
                    179:
1.19      nicm      180:        for (i = 0; i < ARRAY_LENGTH(&find_list); i++) {
                    181:                wm = ARRAY_ITEM(&find_list, i).wl;
1.1       nicm      182:
1.21      nicm      183:                cdata = window_choose_data_create(TREE_OTHER, c, c->session);
1.15      nicm      184:                cdata->idx = wm->idx;
1.19      nicm      185:                cdata->wl = wm;
1.15      nicm      186:
                    187:                cdata->ft_template = xstrdup(template);
1.19      nicm      188:                cdata->pane_id = ARRAY_ITEM(&find_list, i).pane_id;
                    189:
1.15      nicm      190:                format_add(cdata->ft, "line", "%u", i);
                    191:                format_add(cdata->ft, "window_find_matches", "%s",
1.19      nicm      192:                    ARRAY_ITEM(&find_list, i).list_ctx);
1.15      nicm      193:                format_session(cdata->ft, s);
                    194:                format_winlink(cdata->ft, s, wm);
1.20      nicm      195:                format_window_pane(cdata->ft, wm->window->active);
1.13      nicm      196:
1.15      nicm      197:                window_choose_add(wl->window->active, cdata);
1.1       nicm      198:        }
                    199:
1.22      nicm      200:        window_choose_ready(wl->window->active, 0, cmd_find_window_callback);
1.1       nicm      201:
                    202: out:
1.19      nicm      203:        ARRAY_FREE(&find_list);
1.17      nicm      204:        return (CMD_RETURN_NORMAL);
1.1       nicm      205: }
                    206:
                    207: void
1.15      nicm      208: cmd_find_window_callback(struct window_choose_data *cdata)
1.1       nicm      209: {
1.19      nicm      210:        struct session          *s;
                    211:        struct window_pane      *wp;
1.7       nicm      212:
1.15      nicm      213:        if (cdata == NULL)
1.7       nicm      214:                return;
1.15      nicm      215:
1.21      nicm      216:        s = cdata->start_session;
1.7       nicm      217:        if (!session_alive(s))
                    218:                return;
1.19      nicm      219:
                    220:        wp = window_pane_at_index(cdata->wl->window, cdata->pane_id);
                    221:        if (wp != NULL && window_pane_visible(wp))
                    222:                window_set_active_pane(cdata->wl->window, wp);
1.1       nicm      223:
1.15      nicm      224:        if (session_select(s, cdata->idx) == 0) {
1.7       nicm      225:                server_redraw_session(s);
1.1       nicm      226:                recalculate_sizes();
                    227:        }
                    228: }