Skip to content

Commit

Permalink
Wrap all the individual format_* calls in a single format_defaults
Browse files Browse the repository at this point in the history
functions.
  • Loading branch information
nicm committed Feb 5, 2015
1 parent e5d9cef commit 4946f74
Show file tree
Hide file tree
Showing 23 changed files with 78 additions and 109 deletions.
14 changes: 4 additions & 10 deletions cmd-attach-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,8 @@ cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag,

if (cflag != NULL) {
ft = format_create();
if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
format_client(ft, c);
format_session(ft, s);
format_winlink(ft, s, s->curw);
format_window_pane(ft, s->curw->window->active);
format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s,
NULL, NULL);
cp = format_expand(ft, cflag);
format_free(ft);

Expand Down Expand Up @@ -139,11 +136,8 @@ cmd_attach_session(struct cmd_q *cmdq, const char *tflag, int dflag, int rflag,

if (cflag != NULL) {
ft = format_create();
if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
format_client(ft, c);
format_session(ft, s);
format_winlink(ft, s, s->curw);
format_window_pane(ft, s->curw->window->active);
format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s,
NULL, NULL);
cp = format_expand(ft, cflag);
format_free(ft);

Expand Down
7 changes: 1 addition & 6 deletions cmd-break-pane.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ cmd_break_pane_exec(struct cmd *self, struct cmd_q *cmdq)
char *name;
char *cause;
int base_idx;
struct client *c;
struct format_tree *ft;
const char *template;
char *cp;
Expand Down Expand Up @@ -90,11 +89,7 @@ cmd_break_pane_exec(struct cmd *self, struct cmd_q *cmdq)
template = BREAK_PANE_TEMPLATE;

ft = format_create();
if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
format_client(ft, c);
format_session(ft, s);
format_winlink(ft, s, wl);
format_window_pane(ft, wp);
format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, wl, wp);

cp = format_expand(ft, template);
cmdq_print(cmdq, "%s", cp);
Expand Down
2 changes: 1 addition & 1 deletion cmd-choose-buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ cmd_choose_buffer_exec(struct cmd *self, struct cmd_q *cmdq)
cdata->idx = idx;

cdata->ft_template = xstrdup(template);
format_paste_buffer(cdata->ft, pb, utf8flag);
format_defaults_paste_buffer(cdata->ft, pb, utf8flag);

xasprintf(&action_data, "%s", pb->name);
cdata->command = cmd_template_replace(action, action_data, 1);
Expand Down
3 changes: 1 addition & 2 deletions cmd-choose-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ cmd_choose_client_exec(struct cmd *self, struct cmd_q *cmdq)

cdata->ft_template = xstrdup(template);
format_add(cdata->ft, "line", "%u", i);
format_session(cdata->ft, c1->session);
format_client(cdata->ft, c1);
format_defaults(cdata->ft, c1, NULL, NULL, NULL);

cdata->command = cmd_template_replace(action, c1->tty.path, 1);

Expand Down
6 changes: 1 addition & 5 deletions cmd-display-message.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,7 @@ cmd_display_message_exec(struct cmd *self, struct cmd_q *cmdq)
template = DISPLAY_MESSAGE_TEMPLATE;

ft = format_create();
if (c != NULL)
format_client(ft, c);
format_session(ft, s);
format_winlink(ft, s, wl);
format_window_pane(ft, wp);
format_defaults(ft, c, s, wl, wp);

t = time(NULL);
len = strftime(out, sizeof out, template, localtime(&t));
Expand Down
4 changes: 1 addition & 3 deletions cmd-find-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ cmd_find_window_exec(struct cmd *self, struct cmd_q *cmdq)
format_add(cdata->ft, "line", "%u", i);
format_add(cdata->ft, "window_find_matches", "%s",
ARRAY_ITEM(&find_list, i).list_ctx);
format_session(cdata->ft, s);
format_winlink(cdata->ft, s, wm);
format_window_pane(cdata->ft, wm->window->active);
format_defaults(cdata->ft, NULL, s, wm, NULL);

window_choose_add(wl->window->active, cdata);
}
Expand Down
7 changes: 1 addition & 6 deletions cmd-if-shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,7 @@ cmd_if_shell_exec(struct cmd *self, struct cmd_q *cmdq)
}

ft = format_create();
if (s != NULL)
format_session(ft, s);
if (s != NULL && wl != NULL)
format_winlink(ft, s, wl);
if (wp != NULL)
format_window_pane(ft, wp);
format_defaults(ft, NULL, s, wl, wp);
shellcmd = format_expand(ft, args->argv[0]);
format_free(ft);

Expand Down
2 changes: 1 addition & 1 deletion cmd-list-buffers.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ cmd_list_buffers_exec(unused struct cmd *self, struct cmd_q *cmdq)
pb = NULL;
while ((pb = paste_walk(pb)) != NULL) {
ft = format_create();
format_paste_buffer(ft, pb, 0);
format_defaults_paste_buffer(ft, pb, 0);

line = format_expand(ft, template);
cmdq_print(cmdq, "%s", line);
Expand Down
3 changes: 1 addition & 2 deletions cmd-list-clients.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ cmd_list_clients_exec(struct cmd *self, struct cmd_q *cmdq)

ft = format_create();
format_add(ft, "line", "%u", i);
format_session(ft, c->session);
format_client(ft, c);
format_defaults(ft, c, NULL, NULL, NULL);

line = format_expand(ft, template);
cmdq_print(cmdq, "%s", line);
Expand Down
4 changes: 1 addition & 3 deletions cmd-list-panes.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ cmd_list_panes_window(struct cmd *self,
TAILQ_FOREACH(wp, &wl->window->panes, entry) {
ft = format_create();
format_add(ft, "line", "%u", n);
format_session(ft, s);
format_winlink(ft, s, wl);
format_window_pane(ft, wp);
format_defaults(ft, NULL, s, wl, wp);

line = format_expand(ft, template);
cmdq_print(cmdq, "%s", line);
Expand Down
2 changes: 1 addition & 1 deletion cmd-list-sessions.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ cmd_list_sessions_exec(struct cmd *self, struct cmd_q *cmdq)
RB_FOREACH(s, sessions, &sessions) {
ft = format_create();
format_add(ft, "line", "%u", n);
format_session(ft, s);
format_defaults(ft, NULL, s, NULL, NULL);

line = format_expand(ft, template);
cmdq_print(cmdq, "%s", line);
Expand Down
4 changes: 1 addition & 3 deletions cmd-list-windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ cmd_list_windows_session(
RB_FOREACH(wl, winlinks, &s->windows) {
ft = format_create();
format_add(ft, "line", "%u", n);
format_session(ft, s);
format_winlink(ft, s, wl);
format_window_pane(ft, wl->window->active);
format_defaults(ft, NULL, s, wl, NULL);

line = format_expand(ft, template);
cmdq_print(cmdq, "%s", line);
Expand Down
9 changes: 4 additions & 5 deletions cmd-new-session.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
/* Get the new session working directory. */
if (args_has(args, 'c')) {
ft = format_create();
if ((c0 = cmd_find_client(cmdq, NULL, 1)) != NULL)
format_client(ft, c0);
format_defaults(ft, cmd_find_client(cmdq, NULL, 1), NULL, NULL,
NULL);
cp = format_expand(ft, args_get(args, 'c'));
format_free(ft);

Expand Down Expand Up @@ -287,9 +287,8 @@ cmd_new_session_exec(struct cmd *self, struct cmd_q *cmdq)
template = NEW_SESSION_TEMPLATE;

ft = format_create();
if ((c0 = cmd_find_client(cmdq, NULL, 1)) != NULL)
format_client(ft, c0);
format_session(ft, s);
format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, NULL,
NULL);

cp = format_expand(ft, template);
cmdq_print(cmdq, "%s", cp);
Expand Down
14 changes: 4 additions & 10 deletions cmd-new-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,8 @@ cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq)

if (args_has(args, 'c')) {
ft = format_create();
if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
format_client(ft, c);
format_session(ft, s);
format_winlink(ft, s, s->curw);
format_window_pane(ft, s->curw->window->active);
format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, NULL,
NULL);
cp = format_expand(ft, args_get(args, 'c'));
format_free(ft);

Expand Down Expand Up @@ -173,11 +170,8 @@ cmd_new_window_exec(struct cmd *self, struct cmd_q *cmdq)
template = NEW_WINDOW_TEMPLATE;

ft = format_create();
if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
format_client(ft, c);
format_session(ft, s);
format_winlink(ft, s, wl);
format_window_pane(ft, wl->window->active);
format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, wl,
NULL);

cp = format_expand(ft, template);
cmdq_print(cmdq, "%s", cp);
Expand Down
7 changes: 1 addition & 6 deletions cmd-run-shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,7 @@ cmd_run_shell_exec(struct cmd *self, struct cmd_q *cmdq)
}

ft = format_create();
if (s != NULL)
format_session(ft, s);
if (s != NULL && wl != NULL)
format_winlink(ft, s, wl);
if (wp != NULL)
format_window_pane(ft, wp);
format_defaults(ft, NULL, s, wl, wp);
shellcmd = format_expand(ft, args->argv[0]);
format_free(ft);

Expand Down
14 changes: 4 additions & 10 deletions cmd-split-window.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,8 @@ cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq)

if (args_has(args, 'c')) {
ft = format_create();
if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
format_client(ft, c);
format_session(ft, s);
format_winlink(ft, s, s->curw);
format_window_pane(ft, s->curw->window->active);
format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, NULL,
NULL);
cp = format_expand(ft, args_get(args, 'c'));
format_free(ft);

Expand Down Expand Up @@ -181,11 +178,8 @@ cmd_split_window_exec(struct cmd *self, struct cmd_q *cmdq)
template = SPLIT_WINDOW_TEMPLATE;

ft = format_create();
if ((c = cmd_find_client(cmdq, NULL, 1)) != NULL)
format_client(ft, c);
format_session(ft, s);
format_winlink(ft, s, wl);
format_window_pane(ft, new_wp);
format_defaults(ft, cmd_find_client(cmdq, NULL, 1), s, wl,
new_wp);

cp = format_expand(ft, template);
cmdq_print(cmdq, "%s", cp);
Expand Down
2 changes: 1 addition & 1 deletion control-notify.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ control_notify_window_layout_changed(struct window *w)
ft = format_create();
wl = winlink_find_by_window(&s->windows, w);
if (wl != NULL) {
format_winlink(ft, c->session, wl);
format_defaults(ft, c, NULL, wl, NULL);
control_write(c, "%s", format_expand(ft, template));
}
format_free(ft);
Expand Down
48 changes: 38 additions & 10 deletions format.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@
int format_replace(struct format_tree *, const char *, size_t, char **,
size_t *, size_t *);
char *format_get_command(struct window_pane *);
void format_window_pane_tabs(struct format_tree *, struct window_pane *);

void format_defaults_pane_tabs(struct format_tree *, struct window_pane *);
void format_defaults_session(struct format_tree *, struct session *);
void format_defaults_client(struct format_tree *, struct client *);
void format_defaults_winlink(struct format_tree *, struct session *,
struct winlink *);

/* Entry in format tree. */
struct format_entry {
Expand Down Expand Up @@ -418,9 +423,31 @@ format_get_command(struct window_pane *wp)
return (out);
}

/* Set defaults for any of arguments that are not NULL. */
void
format_defaults(struct format_tree *ft, struct client *c, struct session *s,
struct winlink *wl, struct window_pane *wp)
{
if (s == NULL && c != NULL)
s = c->session;
if (wl == NULL && s != NULL)
wl = s->curw;
if (wp == NULL && wl != NULL)
wp = wl->window->active;

if (c != NULL)
format_defaults_client(ft, c);
if (s != NULL)
format_defaults_session(ft, s);
if (s != NULL && wl != NULL)
format_defaults_winlink(ft, s, wl);
if (wp != NULL)
format_defaults_pane(ft, wp);
}

/* Set default format keys for a session. */
void
format_session(struct format_tree *ft, struct session *s)
format_defaults_session(struct format_tree *ft, struct session *s)
{
struct session_group *sg;
char *tim;
Expand Down Expand Up @@ -451,7 +478,7 @@ format_session(struct format_tree *ft, struct session *s)

/* Set default format keys for a client. */
void
format_client(struct format_tree *ft, struct client *c)
format_defaults_client(struct format_tree *ft, struct client *c)
{
char *tim;
time_t t;
Expand Down Expand Up @@ -501,7 +528,7 @@ format_client(struct format_tree *ft, struct client *c)

/* Set default format keys for a window. */
void
format_window(struct format_tree *ft, struct window *w)
format_defaults_window(struct format_tree *ft, struct window *w)
{
char *layout;

Expand All @@ -523,7 +550,8 @@ format_window(struct format_tree *ft, struct window *w)

/* Set default format keys for a winlink. */
void
format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)
format_defaults_winlink(struct format_tree *ft, struct session *s,
struct winlink *wl)
{
struct window *w = wl->window;
char *flags;
Expand All @@ -533,7 +561,7 @@ format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)

flags = window_printable_flags(s, wl);

format_window(ft, w);
format_defaults_window(ft, w);

format_add(ft, "window_index", "%d", wl->idx);
format_add(ft, "window_flags", "%s", flags);
Expand All @@ -553,7 +581,7 @@ format_winlink(struct format_tree *ft, struct session *s, struct winlink *wl)

/* Add window pane tabs. */
void
format_window_pane_tabs(struct format_tree *ft, struct window_pane *wp)
format_defaults_pane_tabs(struct format_tree *ft, struct window_pane *wp)
{
struct evbuffer *buffer;
u_int i;
Expand All @@ -575,7 +603,7 @@ format_window_pane_tabs(struct format_tree *ft, struct window_pane *wp)

/* Set default format keys for a window pane. */
void
format_window_pane(struct format_tree *ft, struct window_pane *wp)
format_defaults_pane(struct format_tree *ft, struct window_pane *wp)
{
struct grid *gd = wp->base.grid;
struct grid_line *gl;
Expand Down Expand Up @@ -665,12 +693,12 @@ format_window_pane(struct format_tree *ft, struct window_pane *wp)
format_add(ft, "mouse_utf8_flag", "%d",
!!(wp->base.mode & MODE_MOUSE_UTF8));

format_window_pane_tabs(ft, wp);
format_defaults_pane_tabs(ft, wp);
}

/* Set default format keys for paste buffer. */
void
format_paste_buffer(struct format_tree *ft, struct paste_buffer *pb,
format_defaults_paste_buffer(struct format_tree *ft, struct paste_buffer *pb,
int utf8flag)
{
char *s;
Expand Down
4 changes: 2 additions & 2 deletions names.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ format_window_name(struct window *w)
char *fmt, *name;

ft = format_create();
format_window(ft, w);
format_window_pane(ft, w->active);
format_defaults_window(ft, w);
format_defaults_pane(ft, w->active);

fmt = options_get_string(&w->options, "automatic-rename-format");
name = format_expand(ft, fmt);
Expand Down
Loading

0 comments on commit 4946f74

Please sign in to comment.