From b992c5e6a1cecafa0cd3133b767bd460f50d0057 Mon Sep 17 00:00:00 2001 From: Nicholas Marriott Date: Mon, 25 Jun 2012 14:08:56 +0000 Subject: Clean up and simplify the choose mode code, from Thomas Adam. --- usr.bin/tmux/cmd-find-window.c | 61 +++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 33 deletions(-) (limited to 'usr.bin/tmux/cmd-find-window.c') diff --git a/usr.bin/tmux/cmd-find-window.c b/usr.bin/tmux/cmd-find-window.c index 0d8f726a905..76e7456d1d6 100644 --- a/usr.bin/tmux/cmd-find-window.c +++ b/usr.bin/tmux/cmd-find-window.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-find-window.c,v 1.14 2012/06/25 13:49:16 nicm Exp $ */ +/* $OpenBSD: cmd-find-window.c,v 1.15 2012/06/25 14:08:55 nicm Exp $ */ /* * Copyright (c) 2009 Nicholas Marriott @@ -30,8 +30,8 @@ int cmd_find_window_exec(struct cmd *, struct cmd_ctx *); u_int cmd_find_window_match_flags(struct args *); -void cmd_find_window_callback(void *, int); -void cmd_find_window_free(void *); +void cmd_find_window_callback(struct window_choose_data *); +void cmd_find_window_free(struct window_choose_data *); /* Flags for determining matching behavior. */ #define CMD_FIND_WINDOW_BY_TITLE 0x1 @@ -53,10 +53,6 @@ const struct cmd_entry cmd_find_window_entry = { cmd_find_window_exec }; -struct cmd_find_window_data { - struct session *session; -}; - u_int cmd_find_window_match_flags(struct args *args) { @@ -81,15 +77,13 @@ int cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx) { struct args *args = self->args; - struct cmd_find_window_data *cdata; + struct window_choose_data *cdata; struct session *s; struct winlink *wl, *wm; struct window_pane *wp; - struct format_tree *ft; ARRAY_DECL(, u_int) list_idx; ARRAY_DECL(, char *) list_ctx; char *str, *sres, *sctx, *searchstr; - char *find_line; const char *template; u_int i, line, match_flags; @@ -172,29 +166,25 @@ cmd_find_window_exec(struct cmd *self, struct cmd_ctx *ctx) wm = winlink_find_by_index( &s->windows, ARRAY_ITEM(&list_idx, i)); - ft = format_create(); - format_add(ft, "line", "%u", i); - format_add(ft, "window_find_matches", "%s", - ARRAY_ITEM(&list_ctx, i)); - format_session(ft, s); - format_winlink(ft, s, wm); + cdata = window_choose_data_create(ctx); + cdata->idx = wm->idx; + cdata->client->references++; - find_line = format_expand(ft, template); - - window_choose_add(wl->window->active, wm->idx, "%s", find_line); + cdata->ft_template = xstrdup(template); + format_add(cdata->ft, "line", "%u", i); + format_add(cdata->ft, "window_find_matches", "%s", + ARRAY_ITEM(&list_ctx, i)); + format_session(cdata->ft, s); + format_winlink(cdata->ft, s, wm); - xfree(find_line); - format_free(ft); + window_choose_add(wl->window->active, cdata); } - cdata = xmalloc(sizeof *cdata); - cdata->session = s; - cdata->session->references++; - window_choose_ready(wl->window->active, - 0, cmd_find_window_callback, cmd_find_window_free, cdata); + 0, cmd_find_window_callback, cmd_find_window_free); out: + ARRAY_FREE(&list_idx); ARRAY_FREE(&list_ctx); @@ -202,27 +192,32 @@ out: } void -cmd_find_window_callback(void *data, int idx) +cmd_find_window_callback(struct window_choose_data *cdata) { - struct cmd_find_window_data *cdata = data; - struct session *s = cdata->session; + struct session *s; - if (idx == -1) + if (cdata == NULL) return; + + s = cdata->session; if (!session_alive(s)) return; - if (session_select(s, idx) == 0) { + if (session_select(s, cdata->idx) == 0) { server_redraw_session(s); recalculate_sizes(); } } void -cmd_find_window_free(void *data) +cmd_find_window_free(struct window_choose_data *cdata) { - struct cmd_find_window_data *cdata = data; + if (cdata == NULL) + return; cdata->session->references--; + + xfree(cdata->ft_template); + format_free(cdata->ft); xfree(cdata); } -- cgit v1.2.3