From 2628af573d98f7bdd4db02c7a80c860b867a45fb Mon Sep 17 00:00:00 2001 From: nicm Date: Mon, 18 Mar 2019 14:10:25 +0000 Subject: [PATCH] Add format variables for the default formats for the various modes (tree_mode_format and so on) and add a -a flag to display-message to list variables with values. --- cmd-display-message.c | 18 ++++++++++++++++-- format.c | 36 +++++++++++++++++++++++++++++++++++- tmux.1 | 9 +++++---- tmux.h | 4 ++++ window-buffer.c | 1 + window-client.c | 1 + window-tree.c | 1 + window.c | 11 +++++++++++ 8 files changed, 74 insertions(+), 7 deletions(-) diff --git a/cmd-display-message.c b/cmd-display-message.c index 07cd12a8..2e935908 100644 --- a/cmd-display-message.c +++ b/cmd-display-message.c @@ -39,8 +39,8 @@ const struct cmd_entry cmd_display_message_entry = { .name = "display-message", .alias = "display", - .args = { "c:pt:F:v", 0, 1 }, - .usage = "[-pv] [-c target-client] [-F format] " + .args = { "ac:pt:F:v", 0, 1 }, + .usage = "[-apv] [-c target-client] [-F format] " CMD_TARGET_PANE_USAGE " [message]", .target = { 't', CMD_FIND_PANE, 0 }, @@ -49,6 +49,14 @@ const struct cmd_entry cmd_display_message_entry = { .exec = cmd_display_message_exec }; +static void +cmd_display_message_each(const char *key, const char *value, void *arg) +{ + struct cmdq_item *item = arg; + + cmdq_print(item, "%s=%s", key, value); +} + static enum cmd_retval cmd_display_message_exec(struct cmd *self, struct cmdq_item *item) { @@ -91,6 +99,12 @@ cmd_display_message_exec(struct cmd *self, struct cmdq_item *item) ft = format_create(item->client, item, FORMAT_NONE, flags); format_defaults(ft, target_c, s, wl, wp); + if (args_has(args, 'a')) { + if (item != NULL) + format_each(ft, cmd_display_message_each, item); + return (CMD_RETURN_NORMAL); + } + msg = format_expand_time(ft, template); if (args_has(self->args, 'p')) cmdq_print(item, "%s", msg); diff --git a/format.c b/format.c index aa0a8aa2..501b0a34 100644 --- a/format.c +++ b/format.c @@ -701,7 +701,9 @@ format_merge(struct format_tree *ft, struct format_tree *from) struct format_tree * format_create(struct client *c, struct cmdq_item *item, int tag, int flags) { - struct format_tree *ft; + struct format_tree *ft; + const struct window_mode **wm; + char tmp[64]; if (!event_initialized(&format_job_event)) { evtimer_set(&format_job_event, format_job_timer, NULL); @@ -727,6 +729,14 @@ format_create(struct client *c, struct cmdq_item *item, int tag, int flags) format_add(ft, "socket_path", "%s", socket_path); format_add_tv(ft, "start_time", &start_time); + for (wm = all_window_modes; *wm != NULL; wm++) { + if ((*wm)->default_format != NULL) { + xsnprintf(tmp, sizeof tmp, "%s_format", (*wm)->name); + tmp[strcspn(tmp, "-")] = '_'; + format_add(ft, tmp, "%s", (*wm)->default_format); + } + } + if (item != NULL) { if (item->cmd != NULL) format_add(ft, "command", "%s", item->cmd->entry->name); @@ -755,6 +765,30 @@ format_free(struct format_tree *ft) free(ft); } +/* Walk each format. */ +void +format_each(struct format_tree *ft, void (*cb)(const char *, const char *, + void *), void *arg) +{ + struct format_entry *fe; + static char s[64]; + + RB_FOREACH(fe, format_entry_tree, &ft->tree) { + if (fe->t != 0) { + xsnprintf(s, sizeof s, "%lld", (long long)fe->t); + cb(fe->key, fe->value, s); + } else { + if (fe->value == NULL && fe->cb != NULL) { + fe->cb(ft, fe); + if (fe->value == NULL) + fe->value = xstrdup(""); + } + cb(fe->key, fe->value, arg); + } + } +} + + /* Add a key-value pair. */ void format_add(struct format_tree *ft, const char *key, const char *fmt, ...) diff --git a/tmux.1 b/tmux.1 index ce036018..c7b7d812 100644 --- a/tmux.1 +++ b/tmux.1 @@ -4177,7 +4177,7 @@ option. This command works only from inside .Nm . .It Xo Ic display-message -.Op Fl pv +.Op Fl apv .Op Fl c Ar target-client .Op Fl t Ar target-pane .Op Ar message @@ -4200,9 +4200,10 @@ if is given, otherwise the active pane for the session attached to .Ar target-client . .Pp -With -.Fl v , -verbose logging is printed as the format is parsed. +.Fl v +prints verbose logging as the format is parsed and +.Fl a +lists the format variables and their values. .El .Sh BUFFERS .Nm diff --git a/tmux.h b/tmux.h index a1ed1b46..9adef27f 100644 --- a/tmux.h +++ b/tmux.h @@ -704,6 +704,7 @@ struct screen_write_ctx { struct window_mode_entry; struct window_mode { const char *name; + const char *default_format; struct screen *(*init)(struct window_mode_entry *, struct cmd_find_state *, struct args *); @@ -1587,6 +1588,8 @@ struct format_tree *format_create(struct client *, struct cmdq_item *, int, void format_free(struct format_tree *); void printflike(3, 4) format_add(struct format_tree *, const char *, const char *, ...); +void format_each(struct format_tree *, void (*)(const char *, + const char *, void *), void *); char *format_expand_time(struct format_tree *, const char *); char *format_expand(struct format_tree *, const char *); char *format_single(struct cmdq_item *, const char *, @@ -2156,6 +2159,7 @@ void screen_select_cell(struct screen *, struct grid_cell *, /* window.c */ extern struct windows windows; extern struct window_pane_tree all_window_panes; +extern const struct window_mode *all_window_modes[]; int window_cmp(struct window *, struct window *); RB_PROTOTYPE(windows, window, entry, window_cmp); int winlink_cmp(struct winlink *, struct winlink *); diff --git a/window-buffer.c b/window-buffer.c index 7c918c70..06d346e3 100644 --- a/window-buffer.c +++ b/window-buffer.c @@ -41,6 +41,7 @@ static void window_buffer_key(struct window_mode_entry *, const struct window_mode window_buffer_mode = { .name = "buffer-mode", + .default_format = WINDOW_BUFFER_DEFAULT_FORMAT, .init = window_buffer_init, .free = window_buffer_free, diff --git a/window-client.c b/window-client.c index db1edbb3..ec98984d 100644 --- a/window-client.c +++ b/window-client.c @@ -42,6 +42,7 @@ static void window_client_key(struct window_mode_entry *, const struct window_mode window_client_mode = { .name = "client-mode", + .default_format = WINDOW_CLIENT_DEFAULT_FORMAT, .init = window_client_init, .free = window_client_free, diff --git a/window-tree.c b/window-tree.c index 46b28eae..120d22c8 100644 --- a/window-tree.c +++ b/window-tree.c @@ -55,6 +55,7 @@ static void window_tree_key(struct window_mode_entry *, const struct window_mode window_tree_mode = { .name = "tree-mode", + .default_format = WINDOW_TREE_DEFAULT_FORMAT, .init = window_tree_init, .free = window_tree_free, diff --git a/window.c b/window.c index a253e8bf..a5c68ece 100644 --- a/window.c +++ b/window.c @@ -62,6 +62,17 @@ static u_int next_window_pane_id; static u_int next_window_id; static u_int next_active_point; +/* List of window modes. */ +const struct window_mode *all_window_modes[] = { + &window_buffer_mode, + &window_client_mode, + &window_clock_mode, + &window_copy_mode, + &window_tree_mode, + &window_view_mode, + NULL +}; + static void window_destroy(struct window *); static struct window_pane *window_pane_create(struct window *, u_int, u_int,