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

1.2     ! nicm        1: /* $OpenBSD: cmd-find-window.c,v 1.1 2009/06/01 22:58:49 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.1       nicm       22: #include <string.h>
                     23:
                     24: #include "tmux.h"
                     25:
                     26: /*
                     27:  * Find window containing text.
                     28:  */
                     29:
                     30: int    cmd_find_window_exec(struct cmd *, struct cmd_ctx *);
                     31:
                     32: void   cmd_find_window_callback(void *, int);
                     33:
                     34: const struct cmd_entry cmd_find_window_entry = {
                     35:        "find-window", "findw",
                     36:        CMD_TARGET_WINDOW_USAGE " match-string",
                     37:        CMD_ARG1,
                     38:        cmd_target_init,
                     39:        cmd_target_parse,
                     40:        cmd_find_window_exec,
                     41:        cmd_target_send,
                     42:        cmd_target_recv,
                     43:        cmd_target_free,
                     44:        cmd_target_print
                     45: };
                     46:
                     47: struct cmd_find_window_data {
                     48:        u_int   session;
                     49: };
                     50:
                     51: int
                     52: cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx)
                     53: {
                     54:        struct cmd_target_data          *data = self->data;
                     55:        struct cmd_find_window_data     *cdata;
                     56:        struct session                  *s;
                     57:        struct winlink                  *wl, *wm;
                     58:        struct window                   *w;
                     59:        struct window_pane              *wp;
                     60:        ARRAY_DECL(, u_int)              list_idx;
                     61:        ARRAY_DECL(, char *)             list_ctx;
1.2     ! nicm       62:        char                            *sres, *sctx, *searchstr;
        !            63:        u_int                            i, line;
1.1       nicm       64:
                     65:        if (ctx->curclient == NULL) {
                     66:                ctx->error(ctx, "must be run interactively");
                     67:                return (-1);
                     68:        }
                     69:        s = ctx->curclient->session;
                     70:
                     71:        if ((wl = cmd_find_window(ctx, data->target, NULL)) == NULL)
                     72:                return (-1);
                     73:
                     74:        ARRAY_INIT(&list_idx);
                     75:        ARRAY_INIT(&list_ctx);
                     76:
1.2     ! nicm       77:        xasprintf(&searchstr, "*%s*", data->arg);
1.1       nicm       78:        RB_FOREACH(wm, winlinks, &s->windows) {
                     79:                i = 0;
                     80:                TAILQ_FOREACH(wp, &wm->window->panes, entry) {
                     81:                        i++;
                     82:
1.2     ! nicm       83:                        if (fnmatch(searchstr, wm->window->name, 0) == 0)
1.1       nicm       84:                                sctx = xstrdup("");
                     85:                        else {
1.2     ! nicm       86:                                sres = window_pane_search(wp, data->arg, &line);
1.1       nicm       87:                                if (sres == NULL &&
1.2     ! nicm       88:                                    fnmatch(searchstr, wp->base.title, 0) != 0)
1.1       nicm       89:                                        continue;
                     90:
                     91:                                if (sres == NULL) {
                     92:                                        xasprintf(&sctx,
                     93:                                            "pane %u title: \"%s\"", i - 1,
                     94:                                            wp->base.title);
                     95:                                } else {
1.2     ! nicm       96:                                        xasprintf(&sctx,
        !            97:                                            "pane %u line %u: \"%s\"", i - 1,
        !            98:                                            line + 1, sres);
1.1       nicm       99:                                        xfree(sres);
                    100:                                }
                    101:                        }
                    102:
                    103:                        ARRAY_ADD(&list_idx, wm->idx);
                    104:                        ARRAY_ADD(&list_ctx, sctx);
                    105:                }
                    106:        }
1.2     ! nicm      107:        xfree(searchstr);
1.1       nicm      108:
                    109:        if (ARRAY_LENGTH(&list_idx) == 0) {
                    110:                ctx->error(ctx, "no windows matching: %s", data->arg);
                    111:                ARRAY_FREE(&list_idx);
                    112:                ARRAY_FREE(&list_ctx);
                    113:                return (-1);
                    114:        }
                    115:
                    116:        if (ARRAY_LENGTH(&list_idx) == 1) {
                    117:                if (session_select(s, ARRAY_FIRST(&list_idx)) == 0)
                    118:                        server_redraw_session(s);
                    119:                recalculate_sizes();
                    120:                goto out;
                    121:        }
                    122:
                    123:        if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
                    124:                goto out;
                    125:
                    126:        for (i = 0; i < ARRAY_LENGTH(&list_idx); i++) {
                    127:                wm = winlink_find_by_index(
                    128:                    &s->windows, ARRAY_ITEM(&list_idx, i));
                    129:                w = wm->window;
                    130:
                    131:                sctx = ARRAY_ITEM(&list_ctx, i);
                    132:                window_choose_add(wl->window->active,
                    133:                    wm->idx, "%3d: %s [%ux%u] (%u panes) %s", wm->idx, w->name,
                    134:                    w->sx, w->sy, window_count_panes(w), sctx);
                    135:                xfree(sctx);
                    136:        }
                    137:
                    138:        cdata = xmalloc(sizeof *cdata);
                    139:        if (session_index(s, &cdata->session) != 0)
                    140:                fatalx("session not found");
                    141:
                    142:        window_choose_ready(
                    143:            wl->window->active, 0, cmd_find_window_callback, cdata);
                    144:
                    145: out:
                    146:        ARRAY_FREE(&list_idx);
                    147:        ARRAY_FREE(&list_ctx);
                    148:
                    149:        return (0);
                    150: }
                    151:
                    152: void
                    153: cmd_find_window_callback(void *data, int idx)
                    154: {
                    155:        struct cmd_find_window_data     *cdata = data;
                    156:        struct session                  *s;
                    157:
                    158:        if (idx != -1 && cdata->session <= ARRAY_LENGTH(&sessions) - 1) {
                    159:                s = ARRAY_ITEM(&sessions, cdata->session);
                    160:                if (s != NULL && session_select(s, idx) == 0)
                    161:                        server_redraw_session(s);
                    162:                recalculate_sizes();
                    163:        }
                    164:        xfree(cdata);
                    165: }