summaryrefslogtreecommitdiffstats
path: root/window-choose.c
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@openbsd.org>2012-06-25 14:27:25 +0000
committerNicholas Marriott <nicm@openbsd.org>2012-06-25 14:27:25 +0000
commit67b926cf3c77737e3b40c9d70c38314ac19ba105 (patch)
tree70e11d99991264920f3a063dc4cdccb37522202f /window-choose.c
parent5b6f78186c63a19e213eb8597efe69277556e038 (diff)
Provide common helper function for adding windows and sessions to choose
lists and expand %% in command before using it rather than at callback time. From Thomas Adam.
Diffstat (limited to 'window-choose.c')
-rw-r--r--window-choose.c66
1 files changed, 58 insertions, 8 deletions
diff --git a/window-choose.c b/window-choose.c
index a21db699..ed951661 100644
--- a/window-choose.c
+++ b/window-choose.c
@@ -133,8 +133,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;
@@ -482,21 +481,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;
@@ -510,3 +510,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);
+}