Add a helper function for the most common format_create/defaults/expand

pattern.
This commit is contained in:
nicm 2017-03-08 13:36:12 +00:00
parent 1e6e606f54
commit 6b2009ad72
11 changed files with 49 additions and 89 deletions

View File

@ -54,8 +54,7 @@ cmd_attach_session(struct cmdq_item *item, int dflag, int rflag,
struct client *c = item->client, *c_loop;
struct winlink *wl = item->state.tflag.wl;
struct window_pane *wp = item->state.tflag.wp;
char *cause, *cwd;
struct format_tree *ft;
char *cause;
if (RB_EMPTY(&sessions)) {
cmdq_error(item, "no sessions");
@ -77,13 +76,8 @@ cmd_attach_session(struct cmdq_item *item, int dflag, int rflag,
}
if (cflag != NULL) {
ft = format_create(item, FORMAT_NONE, 0);
format_defaults(ft, c, s, wl, wp);
cwd = format_expand(ft, cflag);
format_free(ft);
free((void *)s->cwd);
s->cwd = cwd;
s->cwd = format_single(item, cflag, c, s, wl, wp);
}
if (c->session != NULL) {

View File

@ -48,6 +48,7 @@ static enum cmd_retval
cmd_break_pane_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
struct client *c = item->state.c;
struct winlink *wl = item->state.sflag.wl;
struct session *src_s = item->state.sflag.s;
struct session *dst_s = item->state.tflag.s;
@ -55,7 +56,6 @@ cmd_break_pane_exec(struct cmd *self, struct cmdq_item *item)
struct window *w = wl->window;
char *name, *cause;
int idx = item->state.tflag.idx;
struct format_tree *ft;
const char *template;
char *cp;
@ -106,15 +106,9 @@ cmd_break_pane_exec(struct cmd *self, struct cmdq_item *item)
if (args_has(args, 'P')) {
if ((template = args_get(args, 'F')) == NULL)
template = BREAK_PANE_TEMPLATE;
ft = format_create(item, FORMAT_NONE, 0);
format_defaults(ft, item->state.c, dst_s, wl, wp);
cp = format_expand(ft, template);
cp = format_single(item, template, c, dst_s, wl, wp);
cmdq_print(item, "%s", cp);
free(cp);
format_free(ft);
}
return (CMD_RETURN_NORMAL);
}

View File

@ -69,10 +69,10 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
char *shellcmd, *cmd, *cause;
struct cmd_list *cmdlist;
struct cmdq_item *new_item;
struct client *c = item->state.c;
struct session *s = item->state.tflag.s;
struct winlink *wl = item->state.tflag.wl;
struct window_pane *wp = item->state.tflag.wp;
struct format_tree *ft;
const char *cwd;
if (item->client != NULL && item->client->session == NULL)
@ -82,11 +82,7 @@ cmd_if_shell_exec(struct cmd *self, struct cmdq_item *item)
else
cwd = NULL;
ft = format_create(item, FORMAT_NONE, 0);
format_defaults(ft, item->state.c, s, wl, wp);
shellcmd = format_expand(ft, args->argv[0]);
format_free(ft);
shellcmd = format_single(item, args->argv[0], c, s, wl, wp);
if (args_has(args, 'F')) {
cmd = NULL;
if (*shellcmd != '0' && *shellcmd != '\0')

View File

@ -78,7 +78,6 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
char **argv, *cause, *cp;
int detached, already_attached, idx, argc;
u_int sx, sy;
struct format_tree *ft;
struct environ_entry *envent;
struct cmd_find_state fs;
@ -155,10 +154,8 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
/* Get the new session working directory. */
if (args_has(args, 'c')) {
ft = format_create(item, FORMAT_NONE, 0);
format_defaults(ft, c, NULL, NULL, NULL);
to_free = cwd = format_expand(ft, args_get(args, 'c'));
format_free(ft);
cwd = args_get(args, 'c');
to_free = cwd = format_single(item, cwd, c, NULL, NULL, NULL);
} else if (c != NULL && c->session == NULL && c->cwd != NULL)
cwd = c->cwd;
else
@ -322,15 +319,9 @@ cmd_new_session_exec(struct cmd *self, struct cmdq_item *item)
if (args_has(args, 'P')) {
if ((template = args_get(args, 'F')) == NULL)
template = NEW_SESSION_TEMPLATE;
ft = format_create(item, FORMAT_NONE, 0);
format_defaults(ft, c, s, NULL, NULL);
cp = format_expand(ft, template);
cp = format_single(item, template, c, s, NULL, NULL);
cmdq_print(item, "%s", cp);
free(cp);
format_free(ft);
}
if (!detached)

View File

@ -59,7 +59,6 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
const char *cmd, *path, *template, *cwd, *to_free;
char **argv, *cause, *cp;
int argc, detached;
struct format_tree *ft;
struct environ_entry *envent;
struct cmd_find_state fs;
@ -95,10 +94,8 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
to_free = NULL;
if (args_has(args, 'c')) {
ft = format_create(item, FORMAT_NONE, 0);
format_defaults(ft, c, s, NULL, NULL);
cwd = to_free = format_expand(ft, args_get(args, 'c'));
format_free(ft);
cwd = args_get(args, 'c');
to_free = cwd = format_single(item, cwd, c, s, NULL, NULL);
} else if (item->client != NULL && item->client->session == NULL)
cwd = item->client->cwd;
else
@ -142,15 +139,9 @@ cmd_new_window_exec(struct cmd *self, struct cmdq_item *item)
if (args_has(args, 'P')) {
if ((template = args_get(args, 'F')) == NULL)
template = NEW_WINDOW_TEMPLATE;
ft = format_create(item, FORMAT_NONE, 0);
format_defaults(ft, c, s, wl, NULL);
cp = format_expand(ft, template);
cp = format_single(item, template, c, s, wl, NULL);
cmdq_print(item, "%s", cp);
free(cp);
format_free(ft);
}
if (to_free != NULL)

View File

@ -86,11 +86,10 @@ cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
struct cmd_run_shell_data *cdata;
char *shellcmd;
struct client *c = item->state.c;
struct session *s = item->state.tflag.s;
struct winlink *wl = item->state.tflag.wl;
struct window_pane *wp = item->state.tflag.wp;
struct format_tree *ft;
const char *cwd;
if (item->client != NULL && item->client->session == NULL)
@ -100,13 +99,8 @@ cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
else
cwd = NULL;
ft = format_create(item, FORMAT_NONE, 0);
format_defaults(ft, item->state.c, s, wl, wp);
shellcmd = format_expand(ft, args->argv[0]);
format_free(ft);
cdata = xcalloc(1, sizeof *cdata);
cdata->cmd = shellcmd;
cdata->cmd = format_single(item, args->argv[0], c, s, wl, wp);
if (args_has(args, 't') && wp != NULL)
cdata->wp_id = wp->id;
@ -116,7 +110,7 @@ cmd_run_shell_exec(struct cmd *self, struct cmdq_item *item)
if (!args_has(args, 'b'))
cdata->item = item;
job_run(shellcmd, s, cwd, cmd_run_shell_callback, cmd_run_shell_free,
job_run(cdata->cmd, s, cwd, cmd_run_shell_callback, cmd_run_shell_free,
cdata);
if (args_has(args, 'b'))

View File

@ -54,6 +54,7 @@ static enum cmd_retval
cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
{
struct args *args = self->args;
struct client *c = item->state.c;
struct session *s = item->state.tflag.s;
struct winlink *wl = item->state.tflag.wl;
struct window *w = wl->window;
@ -65,7 +66,6 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
int argc, size, percentage;
enum layout_type type;
struct layout_cell *lc;
struct format_tree *ft;
struct environ_entry *envent;
struct cmd_find_state fs;
@ -92,10 +92,8 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
to_free = NULL;
if (args_has(args, 'c')) {
ft = format_create(item, FORMAT_NONE, 0);
format_defaults(ft, item->state.c, s, NULL, NULL);
to_free = cwd = format_expand(ft, args_get(args, 'c'));
format_free(ft);
cwd = args_get(args, 'c');
to_free = cwd = format_single(item, cwd, c, s, NULL, NULL);
} else if (item->client != NULL && item->client->session == NULL)
cwd = item->client->cwd;
else
@ -168,15 +166,9 @@ cmd_split_window_exec(struct cmd *self, struct cmdq_item *item)
if (args_has(args, 'P')) {
if ((template = args_get(args, 'F')) == NULL)
template = SPLIT_WINDOW_TEMPLATE;
ft = format_create(item, FORMAT_NONE, 0);
format_defaults(ft, item->state.c, s, wl, new_wp);
cp = format_expand(ft, template);
cp = format_single(item, template, c, s, wl, new_wp);
cmdq_print(item, "%s", cp);
free(cp);
format_free(ft);
}
notify_window("window-layout-changed", w);

View File

@ -64,10 +64,9 @@ control_notify_window_layout_changed(struct window *w)
{
struct client *c;
struct session *s;
struct format_tree *ft;
struct winlink *wl;
const char *template;
char *expanded;
char *cp;
template = "%layout-change #{window_id} #{window_layout} "
"#{window_visible_layout} #{window_flags}";
@ -88,15 +87,12 @@ control_notify_window_layout_changed(struct window *w)
if (w->layout_root == NULL)
continue;
ft = format_create(NULL, FORMAT_NONE, 0);
wl = winlink_find_by_window(&s->windows, w);
if (wl != NULL) {
format_defaults(ft, c, NULL, wl, NULL);
expanded = format_expand(ft, template);
control_write(c, "%s", expanded);
free(expanded);
cp = format_single(NULL, template, c, NULL, wl, NULL);
control_write(c, "%s", cp);
free(cp);
}
format_free(ft);
}
}

View File

@ -500,7 +500,6 @@ format_merge(struct format_tree *ft, struct format_tree *from)
if (fe->value != NULL)
format_add(ft, fe->key, "%s", fe->value);
}
}
/* Create a new tree. */
@ -1061,6 +1060,22 @@ format_expand(struct format_tree *ft, const char *fmt)
return (buf);
}
/* Expand a single string. */
char *
format_single(struct cmdq_item *item, const char *fmt, struct client *c,
struct session *s, struct winlink *wl, struct window_pane *wp)
{
struct format_tree *ft;
char *expanded;
ft = format_create(item, FORMAT_NONE, 0);
format_defaults(ft, c, s, wl, wp);
expanded = format_expand(ft, fmt);
format_free(ft);
return (expanded);
}
/* Set defaults for any of arguments that are not NULL. */
void
format_defaults(struct format_tree *ft, struct client *c, struct session *s,

3
tmux.h
View File

@ -1508,6 +1508,9 @@ void printflike(3, 4) format_add(struct format_tree *, const char *,
const char *, ...);
char *format_expand_time(struct format_tree *, const char *, time_t);
char *format_expand(struct format_tree *, const char *);
char *format_single(struct cmdq_item *, const char *,
struct client *, struct session *, struct winlink *,
struct window_pane *);
void format_defaults(struct format_tree *, struct client *,
struct session *, struct winlink *, struct window_pane *);
void format_defaults_window(struct format_tree *, struct window *);

View File

@ -1604,29 +1604,23 @@ window_copy_copy_buffer(struct window_pane *wp, const char *bufname, void *buf,
}
static void
window_copy_copy_pipe(struct window_pane *wp, struct session *sess,
window_copy_copy_pipe(struct window_pane *wp, struct session *s,
const char *bufname, const char *arg)
{
void *buf;
size_t len;
struct job *job;
struct format_tree *ft;
char *expanded;
void *buf;
size_t len;
struct job *job;
char *expanded;
buf = window_copy_get_selection(wp, &len);
if (buf == NULL)
return;
expanded = format_single(NULL, arg, NULL, s, NULL, wp);
ft = format_create(NULL, FORMAT_NONE, 0);
format_defaults(ft, NULL, sess, NULL, wp);
expanded = format_expand(ft, arg);
job = job_run(expanded, sess, NULL, NULL, NULL, NULL);
job = job_run(expanded, s, NULL, NULL, NULL, NULL);
bufferevent_write(job->event, buf, len);
free(expanded);
format_free(ft);
window_copy_copy_buffer(wp, bufname, buf, len);
}