Previously, each entry's action to be performed from the choose-list,
happened at callback time.  Instead, interpolate the command much earlier
before it's added to the choose-list.  This makes it easier later on to
chain commands together, such as in choose-tree.
---
 trunk/cmd-choose-buffer.c  |   24 +++++++++++++-----------
 trunk/cmd-choose-client.c  |   17 ++++++++++-------
 trunk/cmd-choose-session.c |   22 +++++++---------------
 trunk/cmd-choose-window.c  |   15 ++++++---------
 4 files changed, 36 insertions(+), 42 deletions(-)

diff --git a/trunk/cmd-choose-buffer.c b/trunk/cmd-choose-buffer.c
index b37071d..0a9a4d2 100644
--- a/trunk/cmd-choose-buffer.c
+++ b/trunk/cmd-choose-buffer.c
@@ -48,8 +48,9 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
        struct window_choose_data       *cdata;
        struct winlink                  *wl;
        struct paste_buffer             *pb;
-       u_int                            idx;
+       char                            *action, *action_data;
        const char                      *template;
+       u_int                            idx;
 
        if (ctx->curclient == NULL) {
                ctx->error(ctx, "must be run interactively");
@@ -68,14 +69,14 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx 
*ctx)
        if (window_pane_set_mode(wl->window->active, &window_choose_mode) != 0)
                return (0);
 
+       if (args->argc != 0)
+               action = xstrdup(args->argv[0]);
+       else
+               action = xstrdup("paste-buffer -b '%%'");
+
        idx = 0;
        while ((pb = paste_walk_stack(&global_buffers, &idx)) != NULL) {
                cdata = window_choose_data_create(ctx);
-               if (args->argc != 0)
-                       cdata->action = xstrdup(args->argv[0]);
-               else
-                       cdata->action = xstrdup("paste-buffer -b '%%'");
-
                cdata->idx = idx - 1;
                cdata->client->references++;
 
@@ -83,8 +84,13 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx *ctx)
                format_add(cdata->ft, "line", "%u", idx - 1);
                format_paste_buffer(cdata->ft, pb);
 
+               xasprintf(&action_data, "%u", idx - 1);
+               cdata->command = cmd_template_replace(action, action_data, 1);
+               xfree(action_data);
+
                window_choose_add(wl->window->active, cdata);
        }
+       xfree(action);
 
        window_choose_ready(wl->window->active,
            0, cmd_choose_buffer_callback, cmd_choose_buffer_free);
@@ -95,14 +101,11 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_ctx 
*ctx)
 void
 cmd_choose_buffer_callback(struct window_choose_data *cdata)
 {
-       u_int                            idx = cdata->idx;
-
        if (cdata == NULL)
                return;
        if (cdata->client->flags & CLIENT_DEAD)
                return;
 
-       xasprintf(&cdata->raw_format, "%u", idx);
        window_choose_ctx(cdata);
 }
 
@@ -116,9 +119,8 @@ cmd_choose_buffer_free(struct window_choose_data *data)
 
        cdata->client->references--;
 
+       xfree(cdata->command);
        xfree(cdata->ft_template);
-       xfree(cdata->action);
-       xfree(cdata->raw_format);
        format_free(cdata->ft);
        xfree(cdata);
 }
diff --git a/trunk/cmd-choose-client.c b/trunk/cmd-choose-client.c
index 7ca4346..f0812fc 100644
--- a/trunk/cmd-choose-client.c
+++ b/trunk/cmd-choose-client.c
@@ -54,6 +54,7 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
        struct winlink                  *wl;
        struct client                   *c;
        const char                      *template;
+       char                            *action;
        u_int                            i, idx, cur;
 
        if (ctx->curclient == NULL) {
@@ -70,6 +71,11 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
        if ((template = args_get(args, 'F')) == NULL)
                template = DEFAULT_CLIENT_TEMPLATE;
 
+       if (args->argc != 0)
+               action = xstrdup(args->argv[0]);
+       else
+               action = xstrdup("detach-client -t '%%'");
+
        cur = idx = 0;
        for (i = 0; i < ARRAY_LENGTH(&clients); i++) {
                c = ARRAY_ITEM(&clients, i);
@@ -80,11 +86,6 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
                idx++;
 
                cdata = window_choose_data_create(ctx);
-               if (args->argc != 0)
-                       cdata->action = xstrdup(args->argv[0]);
-               else
-                       cdata->action = xstrdup("detach-client -t '%%'");
-
                cdata->idx = i;
                cdata->client->references++;
 
@@ -93,8 +94,11 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_ctx *ctx)
                format_session(cdata->ft, c->session);
                format_client(cdata->ft, c);
 
+               cdata->command = cmd_template_replace(action, c->tty.path, 1);
+
                window_choose_add(wl->window->active, cdata);
        }
+       xfree(action);
 
        window_choose_ready(wl->window->active,
            cur, cmd_choose_client_callback, cmd_choose_client_free);
@@ -121,7 +125,6 @@ cmd_choose_client_callback(struct window_choose_data *cdata)
        if (c == NULL || c->session == NULL)
                return;
 
-       xasprintf(&cdata->raw_format, "%s", c->tty.path);
        window_choose_ctx(cdata);
 }
 
@@ -134,7 +137,7 @@ cmd_choose_client_free(struct window_choose_data *cdata)
        cdata->client->references--;
 
        xfree(cdata->ft_template);
-       xfree(cdata->action);
+       xfree(cdata->command);
        format_free(cdata->ft);
        xfree(cdata);
 }
diff --git a/trunk/cmd-choose-session.c b/trunk/cmd-choose-session.c
index ea7eea9..b1aed06 100644
--- a/trunk/cmd-choose-session.c
+++ b/trunk/cmd-choose-session.c
@@ -45,10 +45,10 @@ int
 cmd_choose_session_exec(struct cmd *self, struct cmd_ctx *ctx)
 {
        struct args                     *args = self->args;
-       struct window_choose_data       *cdata;
        struct winlink                  *wl;
        struct session                  *s;
-       const char                      *template, *action;
+       char                            *action;
+       const char                      *template;
        u_int                            idx, cur;
 
        if (ctx->curclient == NULL) {
@@ -66,9 +66,9 @@ cmd_choose_session_exec(struct cmd *self, struct cmd_ctx *ctx)
                template = DEFAULT_SESSION_TEMPLATE;
 
        if (args->argc != 0)
-               action = args->argv[0];
+               action = xstrdup(args->argv[0]);
        else
-               action = "switch-client -t '%%'";
+               action = xstrdup("switch-client -t '%%'");
 
        cur = idx = 0;
        RB_FOREACH(s, sessions, &sessions) {
@@ -79,6 +79,7 @@ cmd_choose_session_exec(struct cmd *self, struct cmd_ctx *ctx)
                window_choose_add_session(wl->window->active,
                        ctx, s, template, action, idx);
        }
+       xfree(action);
 
        window_choose_ready(wl->window->active,
            cur, cmd_choose_session_callback, cmd_choose_session_free);
@@ -89,21 +90,12 @@ cmd_choose_session_exec(struct cmd *self, struct cmd_ctx 
*ctx)
 void
 cmd_choose_session_callback(struct window_choose_data *cdata)
 {
-       struct session                  *s;
-       u_int                            idx;
-
        if (cdata == NULL)
                return;
-       if (cdata->client->flags & CLIENT_DEAD)
-               return;
-
-       idx = cdata->idx;
 
-       s = session_find_by_index(idx);
-       if (s == NULL)
+       if (cdata->client->flags & CLIENT_DEAD)
                return;
 
-       cdata->raw_format = xstrdup(s->name);
        window_choose_ctx(cdata);
 }
 
@@ -116,8 +108,8 @@ cmd_choose_session_free(struct window_choose_data *cdata)
        cdata->client->references--;
        cdata->session->references--;
 
+       xfree(cdata->command);
        xfree(cdata->ft_template);
-       xfree(cdata->action);
        format_free(cdata->ft);
        xfree(cdata);
 }
diff --git a/trunk/cmd-choose-window.c b/trunk/cmd-choose-window.c
index 6c54836..903dd0c 100644
--- a/trunk/cmd-choose-window.c
+++ b/trunk/cmd-choose-window.c
@@ -45,10 +45,10 @@ int
 cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx)
 {
        struct args                     *args = self->args;
-       struct window_choose_data       *cdata;
        struct session                  *s;
        struct winlink                  *wl, *wm;
-       const char                      *template, *action;
+       const char                      *template;
+       char                            *action;
        u_int                            idx, cur;
 
        if (ctx->curclient == NULL) {
@@ -67,9 +67,9 @@ cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx)
                template = DEFAULT_WINDOW_TEMPLATE " \"#{pane_title}\"";
 
        if (args->argc != 0)
-               action = args->argv[0];
+               action = xstrdup(args->argv[0]);
        else
-               action = "select-window -t '%%'";
+               action = xstrdup("select-window -t '%%'");
 
        cur = idx = 0;
        RB_FOREACH(wm, winlinks, &s->windows) {
@@ -80,6 +80,7 @@ cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx)
                window_choose_add_window(wl->window->active, ctx, s, wm,
                                         template, action, idx);
        }
+       xfree(action);
 
        window_choose_ready(wl->window->active,
            cur, cmd_choose_window_callback, cmd_choose_window_free);
@@ -91,7 +92,6 @@ void
 cmd_choose_window_callback(struct window_choose_data *cdata)
 {
        struct session                  *s = cdata->session;
-       u_int                            idx;
 
        if (cdata == NULL)
                return;
@@ -102,9 +102,6 @@ cmd_choose_window_callback(struct window_choose_data *cdata)
        if (cdata->client->flags & CLIENT_DEAD)
                return;
 
-       idx = cdata->idx;
-
-       xasprintf(&cdata->raw_format, "%s:%d", s->name, idx);
        window_choose_ctx(cdata);
 }
 
@@ -118,7 +115,7 @@ cmd_choose_window_free(struct window_choose_data *cdata)
        cdata->client->references--;
 
        xfree(cdata->ft_template);
-       xfree(cdata->action);
+       xfree(cdata->command);
        format_free(cdata->ft);
        xfree(cdata);
 }
-- 
1.7.10


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
tmux-users mailing list
tmux-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/tmux-users

Reply via email to