Annotation of embedaddon/tmux/cmd-find-window.c, revision 1.1
1.1 ! misho 1: /* $OpenBSD$ */
! 2:
! 3: /*
! 4: * Copyright (c) 2009 Nicholas Marriott <nicholas.marriott@gmail.com>
! 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 <fnmatch.h>
! 22: #include <stdlib.h>
! 23: #include <string.h>
! 24:
! 25: #include "tmux.h"
! 26:
! 27: /*
! 28: * Find window containing text.
! 29: */
! 30:
! 31: #define FIND_WINDOW_TEMPLATE \
! 32: "#{window_index}: #{window_name} " \
! 33: "[#{window_width}x#{window_height}] " \
! 34: "(#{window_panes} panes) #{window_find_matches}"
! 35:
! 36: static enum cmd_retval cmd_find_window_exec(struct cmd *, struct cmdq_item *);
! 37:
! 38: static void cmd_find_window_callback(struct window_choose_data *);
! 39:
! 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:
! 50: const struct cmd_entry cmd_find_window_entry = {
! 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:
! 57: .tflag = CMD_WINDOW,
! 58:
! 59: .flags = 0,
! 60: .exec = cmd_find_window_exec
! 61: };
! 62:
! 63: struct cmd_find_window_data {
! 64: struct winlink *wl;
! 65: char *list_ctx;
! 66: u_int pane_id;
! 67: TAILQ_ENTRY(cmd_find_window_data) entry;
! 68: };
! 69: TAILQ_HEAD(cmd_find_window_list, cmd_find_window_data);
! 70:
! 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 *);
! 74:
! 75: static u_int
! 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:
! 92: return (match_flags);
! 93: }
! 94:
! 95: static void
! 96: cmd_find_window_match(struct cmd_find_window_list *find_list,
! 97: int match_flags, struct winlink *wl, const char *str,
! 98: const char *searchstr)
! 99: {
! 100: struct cmd_find_window_data *find_data;
! 101: struct window_pane *wp;
! 102: u_int i, line;
! 103: char *sres;
! 104:
! 105: find_data = xcalloc(1, sizeof *find_data);
! 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) {
! 113: find_data->list_ctx = xstrdup("");
! 114: break;
! 115: }
! 116:
! 117: if ((match_flags & CMD_FIND_WINDOW_BY_TITLE) &&
! 118: fnmatch(searchstr, wp->base.title, 0) == 0) {
! 119: xasprintf(&find_data->list_ctx,
! 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) {
! 126: xasprintf(&find_data->list_ctx,
! 127: "pane %u line %u: \"%s\"", i - 1, line + 1, sres);
! 128: free(sres);
! 129: break;
! 130: }
! 131: }
! 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);
! 139: }
! 140:
! 141: static enum cmd_retval
! 142: cmd_find_window_exec(struct cmd *self, struct cmdq_item *item)
! 143: {
! 144: struct args *args = self->args;
! 145: struct client *c = item->state.c;
! 146: struct window_choose_data *cdata;
! 147: struct session *s = item->state.tflag.s;
! 148: struct winlink *wl = item->state.tflag.wl, *wm;
! 149: struct cmd_find_window_list find_list;
! 150: struct cmd_find_window_data *find_data;
! 151: struct cmd_find_window_data *find_data1;
! 152: char *str, *searchstr;
! 153: const char *template;
! 154: u_int i, match_flags;
! 155:
! 156: if (c == NULL) {
! 157: cmdq_error(item, "no client available");
! 158: return (CMD_RETURN_ERROR);
! 159: }
! 160:
! 161: if ((template = args_get(args, 'F')) == NULL)
! 162: template = FIND_WINDOW_TEMPLATE;
! 163:
! 164: match_flags = cmd_find_window_match_flags(args);
! 165: str = args->argv[0];
! 166:
! 167: TAILQ_INIT(&find_list);
! 168:
! 169: xasprintf(&searchstr, "*%s*", str);
! 170: RB_FOREACH(wm, winlinks, &s->windows)
! 171: cmd_find_window_match(&find_list, match_flags, wm, str, searchstr);
! 172: free(searchstr);
! 173:
! 174: if (TAILQ_EMPTY(&find_list)) {
! 175: cmdq_error(item, "no windows matching: %s", str);
! 176: return (CMD_RETURN_ERROR);
! 177: }
! 178:
! 179: if (TAILQ_NEXT(TAILQ_FIRST(&find_list), entry) == NULL) {
! 180: if (session_select(s, TAILQ_FIRST(&find_list)->wl->idx) == 0)
! 181: server_redraw_session(s);
! 182: recalculate_sizes();
! 183: goto out;
! 184: }
! 185:
! 186: if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
! 187: goto out;
! 188:
! 189: i = 0;
! 190: TAILQ_FOREACH(find_data, &find_list, entry) {
! 191: cdata = window_choose_data_create(TREE_OTHER, c, c->session);
! 192: cdata->idx = find_data->wl->idx;
! 193: cdata->wl = find_data->wl;
! 194:
! 195: cdata->ft_template = xstrdup(template);
! 196: cdata->pane_id = find_data->pane_id;
! 197:
! 198: format_add(cdata->ft, "line", "%u", i);
! 199: format_add(cdata->ft, "window_find_matches", "%s",
! 200: find_data->list_ctx);
! 201: format_defaults(cdata->ft, NULL, s, find_data->wl, NULL);
! 202:
! 203: window_choose_add(wl->window->active, cdata);
! 204:
! 205: i++;
! 206: }
! 207:
! 208: window_choose_ready(wl->window->active, 0, cmd_find_window_callback);
! 209:
! 210: out:
! 211: TAILQ_FOREACH_SAFE(find_data, &find_list, entry, find_data1) {
! 212: free(find_data->list_ctx);
! 213: TAILQ_REMOVE(&find_list, find_data, entry);
! 214: free(find_data);
! 215: }
! 216: return (CMD_RETURN_NORMAL);
! 217: }
! 218:
! 219: static void
! 220: cmd_find_window_callback(struct window_choose_data *cdata)
! 221: {
! 222: struct session *s;
! 223: struct window_pane *wp;
! 224:
! 225: if (cdata == NULL)
! 226: return;
! 227:
! 228: s = cdata->start_session;
! 229: if (!session_alive(s))
! 230: return;
! 231:
! 232: wp = window_pane_at_index(cdata->wl->window, cdata->pane_id);
! 233: if (wp != NULL && window_pane_visible(wp))
! 234: window_set_active_pane(cdata->wl->window, wp);
! 235:
! 236: if (session_select(s, cdata->idx) == 0) {
! 237: server_redraw_session(s);
! 238: recalculate_sizes();
! 239: }
! 240: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>