Define an abstraction for adding windows and sessions to a choose list, and
use them for the choose-{window,session} commands.

Both these functions return the struct window_choose_data* pointer.
---
 trunk/cmd-choose-session.c |   24 ++++++----------
 trunk/cmd-choose-window.c  |   26 ++++++-----------
 trunk/tmux.h               |   14 ++++++----
 trunk/window-choose.c      |   66 ++++++++++++++++++++++++++++++++++++++------
 4 files changed, 82 insertions(+), 48 deletions(-)

diff --git a/trunk/cmd-choose-session.c b/trunk/cmd-choose-session.c
index 8931141..ea7eea9 100644
--- a/trunk/cmd-choose-session.c
+++ b/trunk/cmd-choose-session.c
@@ -48,7 +48,7 @@ cmd_choose_session_exec(struct cmd *self, struct cmd_ctx *ctx)
        struct window_choose_data       *cdata;
        struct winlink                  *wl;
        struct session                  *s;
-       const char                      *template;
+       const char                      *template, *action;
        u_int                            idx, cur;
 
        if (ctx->curclient == NULL) {
@@ -65,27 +65,19 @@ cmd_choose_session_exec(struct cmd *self, struct cmd_ctx 
*ctx)
        if ((template = args_get(args, 'F')) == NULL)
                template = DEFAULT_SESSION_TEMPLATE;
 
+       if (args->argc != 0)
+               action = args->argv[0];
+       else
+               action = "switch-client -t '%%'";
+
        cur = idx = 0;
        RB_FOREACH(s, sessions, &sessions) {
                if (s == ctx->curclient->session)
                        cur = idx;
                idx++;
 
-               cdata = window_choose_data_create(ctx);
-               if (args->argc != 0)
-                       cdata->action = xstrdup(args->argv[0]);
-               else
-                       cdata->action = xstrdup("switch-client -t '%%'");
-               cdata->idx = s->idx;
-
-               cdata->client->references++;
-               cdata->session->references++;
-
-               cdata->ft_template = xstrdup(template);
-               format_add(cdata->ft, "line", "%u", idx);
-               format_session(cdata->ft, s);
-
-               window_choose_add(wl->window->active, cdata);
+               window_choose_add_session(wl->window->active,
+                       ctx, s, template, action, idx);
        }
 
        window_choose_ready(wl->window->active,
diff --git a/trunk/cmd-choose-window.c b/trunk/cmd-choose-window.c
index 0000c4c..6c54836 100644
--- a/trunk/cmd-choose-window.c
+++ b/trunk/cmd-choose-window.c
@@ -48,7 +48,7 @@ cmd_choose_window_exec(struct cmd *self, struct cmd_ctx *ctx)
        struct window_choose_data       *cdata;
        struct session                  *s;
        struct winlink                  *wl, *wm;
-       const char                      *template;
+       const char                      *template, *action;
        u_int                            idx, cur;
 
        if (ctx->curclient == NULL) {
@@ -66,29 +66,19 @@ cmd_choose_window_exec(struct cmd *self, struct cmd_ctx 
*ctx)
        if ((template = args_get(args, 'F')) == NULL)
                template = DEFAULT_WINDOW_TEMPLATE " \"#{pane_title}\"";
 
+       if (args->argc != 0)
+               action = args->argv[0];
+       else
+               action = "select-window -t '%%'";
+
        cur = idx = 0;
        RB_FOREACH(wm, winlinks, &s->windows) {
                if (wm == s->curw)
                        cur = idx;
                idx++;
 
-               cdata = window_choose_data_create(ctx);
-               if (args->argc != 0)
-                       cdata->action = xstrdup(args->argv[0]);
-               else
-                       cdata->action = xstrdup("select-window -t '%%'");
-
-               cdata->idx = wm->idx;
-               cdata->client->references++;
-               cdata->session->references++;
-
-               cdata->ft_template = xstrdup(template);
-               format_add(cdata->ft, "line", "%u", idx);
-               format_session(cdata->ft, s);
-               format_winlink(cdata->ft, s, wm);
-               format_window_pane(cdata->ft, wm->window->active);
-
-               window_choose_add(wl->window->active, cdata);
+               window_choose_add_window(wl->window->active, ctx, s, wm,
+                                        template, action, idx);
        }
 
        window_choose_ready(wl->window->active,
diff --git a/trunk/tmux.h b/trunk/tmux.h
index f6c0c4c..7fd355f 100644
--- a/trunk/tmux.h
+++ b/trunk/tmux.h
@@ -846,8 +846,7 @@ struct window_choose_data {
        struct session          *session;
        struct format_tree      *ft;
        char                    *ft_template;
-       char                    *raw_format;
-       char                    *action;
+       char                    *command;
        u_int                    idx;
 };
 
@@ -2128,16 +2127,19 @@ void             window_copy_pageup(struct window_pane 
*);
 
 /* window-choose.c */
 extern const struct window_mode window_choose_mode;
-void            window_choose_vadd(
-                    struct window_pane *, int, const char *, va_list);
 void            window_choose_add(struct window_pane *,
                         struct window_choose_data *);
 void            window_choose_ready(struct window_pane *,
                     u_int, void (*)(struct window_choose_data *),
                     void (*)(struct window_choose_data *));
-struct window_choose_data      *window_choose_data_create(
-                       struct cmd_ctx *);
+struct window_choose_data      *window_choose_data_create(struct cmd_ctx *);
 void            window_choose_ctx(struct window_choose_data *);
+struct window_choose_data      *window_choose_add_window(struct window_pane *,
+                       struct cmd_ctx *, struct session *, struct winlink *,
+                       const char *, char *, u_int);
+struct window_choose_data      *window_choose_add_session(struct window_pane *,
+                       struct cmd_ctx *, struct session *, const char *,
+                       char *, u_int);
 
 /* names.c */
 void            queue_window_name(struct window *);
diff --git a/trunk/window-choose.c b/trunk/window-choose.c
index 28c27a9..3c8e8aa 100644
--- a/trunk/window-choose.c
+++ b/trunk/window-choose.c
@@ -134,8 +134,7 @@ window_choose_data_create(struct cmd_ctx *ctx)
        wcd = xmalloc(sizeof *wcd);
        wcd->ft = format_create();
        wcd->ft_template = NULL;
-       wcd->action = NULL;
-       wcd->raw_format = NULL;
+       wcd->command = NULL;
        wcd->client = ctx->curclient;
        wcd->session = ctx->curclient->session;
        wcd->idx = -1;
@@ -483,21 +482,22 @@ window_choose_ctx(struct window_choose_data *cdata)
 {
        struct cmd_ctx           ctx;
        struct cmd_list         *cmdlist;
-       char                    *template, *cause;
+       char                    *cause;
 
-       template = cmd_template_replace(cdata->action,
-                       cdata->raw_format, 1);
+       /* The command template will have already been replaced.  But if it's
+        * NULL, bail here.
+        */
+       if (cdata->command == NULL)
+               return;
 
-       if (cmd_string_parse(template, &cmdlist, &cause) != 0) {
+       if (cmd_string_parse(cdata->command, &cmdlist, &cause) != 0) {
                if (cause != NULL) {
                        *cause = toupper((u_char) *cause);
                        status_message_set(cdata->client, "%s", cause);
                        xfree(cause);
                }
-               xfree(template);
                return;
        }
-       xfree(template);
 
        ctx.msgdata = NULL;
        ctx.curclient = cdata->client;
@@ -511,3 +511,53 @@ window_choose_ctx(struct window_choose_data *cdata)
        cmd_list_exec(cmdlist, &ctx);
        cmd_list_free(cmdlist);
 }
+
+struct window_choose_data *
+window_choose_add_session(struct window_pane *wp, struct cmd_ctx *ctx,
+       struct session *s, const char *template, char *action, u_int idx)
+{
+       struct window_choose_data       *wcd;
+
+       wcd = window_choose_data_create(ctx);
+       wcd->idx = s->idx;
+       wcd->command = cmd_template_replace(action, s->name, 1);
+       wcd->ft_template = xstrdup(template);
+       format_add(wcd->ft, "line", "%u", idx);
+       format_session(wcd->ft, s);
+
+       wcd->client->references++;
+       wcd->session->references++;
+
+       window_choose_add(wp, wcd);
+
+       return (wcd);
+}
+
+struct window_choose_data *
+window_choose_add_window(struct window_pane *wp, struct cmd_ctx *ctx,
+       struct session *s, struct winlink *wl, const char *template,
+       char *action, u_int idx)
+{
+       struct window_choose_data       *wcd;
+       char                            *action_data;
+
+       wcd = window_choose_data_create(ctx);
+
+       xasprintf(&action_data, "%s:%d", s->name, wl->idx);
+       wcd->command = cmd_template_replace(action, action_data, 1);
+       xfree(action_data);
+
+       wcd->idx = wl->idx;
+       wcd->ft_template = xstrdup(template);
+       format_add(wcd->ft, "line", "%u", idx);
+       format_session(wcd->ft, s);
+       format_winlink(wcd->ft, s, wl);
+       format_window_pane(wcd->ft, wl->window->active);
+
+       wcd->client->references++;
+       wcd->session->references++;
+
+       window_choose_add(wp, wcd);
+
+       return (wcd);
+}
-- 
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