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