From 7277712ca8b603d3b11edbb470de50eb1c64bbf1 Mon Sep 17 00:00:00 2001 From: nicm Date: Wed, 22 Jul 2026 20:12:58 +0000 Subject: [PATCH] Switch show-options over to using a format and add -F. --- cmd-show-options.c | 251 ++++++++++++++++++++++++++++----------------- hooks.c | 17 ++- tmux.1 | 25 ++++- tmux.h | 4 +- 4 files changed, 200 insertions(+), 97 deletions(-) diff --git a/cmd-show-options.c b/cmd-show-options.c index edf2b1354..0b3329f7c 100644 --- a/cmd-show-options.c +++ b/cmd-show-options.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cmd-show-options.c,v 1.74 2026/07/22 18:58:48 nicm Exp $ */ +/* $OpenBSD: cmd-show-options.c,v 1.75 2026/07/22 20:12:58 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -28,14 +28,23 @@ * Show options. */ +#define SHOW_OPTIONS_TEMPLATE \ + "#{?option_value_only," \ + "#{option_value}," \ + "#{option_name}#{?option_has_array_key," \ + "[#{option_array_key}],}" \ + "#{?option_is_parent,*,}" \ + "#{?option_has_value, " \ + "#{?option_is_string,#{q/a:option_value},#{option_value}},}}" +#define SHOW_HOOKS_MONITOR_TEMPLATE \ + "#{option_name}:#{hook_monitor_target}:#{hook_monitor_format}" + static enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmdq_item *); static void cmd_show_options_print(struct cmd *, struct cmdq_item *, struct options_entry *, const char *, int); -static void cmd_show_hooks_print_monitor(struct cmdq_item *, - struct options_entry *); -static enum cmd_retval cmd_show_hooks_monitor(struct cmd *, struct cmdq_item *, - int, struct options *); +static void cmd_show_hooks_print_monitor(struct cmd *, + struct cmdq_item *, struct options_entry *); static enum cmd_retval cmd_show_options_all(struct cmd *, struct cmdq_item *, int, struct options *); @@ -43,8 +52,8 @@ const struct cmd_entry cmd_show_options_entry = { .name = "show-options", .alias = "show", - .args = { "AgHpqst:vw", 0, 1, NULL }, - .usage = "[-AgHpqsvw] " CMD_TARGET_PANE_USAGE " [option]", + .args = { "AgF:Hpqst:vw", 0, 1, NULL }, + .usage = "[-AgHpqsvw] [-F format] " CMD_TARGET_PANE_USAGE " [option]", .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL }, @@ -56,8 +65,8 @@ const struct cmd_entry cmd_show_window_options_entry = { .name = "show-window-options", .alias = "showw", - .args = { "gvt:", 0, 1, NULL }, - .usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]", + .args = { "F:gvt:", 0, 1, NULL }, + .usage = "[-gv] [-F format] " CMD_TARGET_WINDOW_USAGE " [option]", .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL }, @@ -69,8 +78,8 @@ const struct cmd_entry cmd_show_hooks_entry = { .name = "show-hooks", .alias = NULL, - .args = { "Bgpt:w", 0, 1, NULL }, - .usage = "[-Bgpw] " CMD_TARGET_PANE_USAGE " [hook]", + .args = { "BF:gpt:w", 0, 1, NULL }, + .usage = "[-Bgpw] [-F format] " CMD_TARGET_PANE_USAGE " [hook]", .target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL }, @@ -86,7 +95,7 @@ cmd_show_options_exec(struct cmd *self, struct cmdq_item *item) struct options *oo; char *argument, *name = NULL, *cause; char *array_key = NULL; - int window, ambiguous, parent, scope; + int window, ambiguous, parent, print_parent, scope; struct options_entry *o; window = (cmd_get_entry(self) == &cmd_show_window_options_entry); @@ -102,8 +111,14 @@ cmd_show_options_exec(struct cmd *self, struct cmdq_item *item) return (CMD_RETURN_ERROR); } if (cmd_get_entry(self) == &cmd_show_hooks_entry && - args_has(args, 'B')) - return (cmd_show_hooks_monitor(self, item, scope, oo)); + args_has(args, 'B')) { + o = options_first(oo); + while (o != NULL) { + cmd_show_hooks_print_monitor(self, item, o); + o = options_next(o); + } + return (CMD_RETURN_NORMAL); + } return (cmd_show_options_all(self, item, scope, oo)); } argument = format_single_from_target(item, args_string(args, 0)); @@ -136,11 +151,17 @@ cmd_show_options_exec(struct cmd *self, struct cmdq_item *item) if (o != NULL) { if (cmd_get_entry(self) == &cmd_show_hooks_entry && args_has(args, 'B')) - cmd_show_hooks_print_monitor(item, o); - else - cmd_show_options_print(self, item, o, array_key, parent); - } - else if (*name == '@') { + cmd_show_hooks_print_monitor(self, item, o); + else { + print_parent = parent; + if (array_key == NULL && options_is_array(o) && + options_array_first(o) == NULL) { + print_parent = 0; + } + cmd_show_options_print(self, item, o, array_key, + print_parent); + } + } else if (*name == '@') { if (args_has(args, 'q')) goto out; cmdq_error(item, "invalid option: %s", argument); @@ -164,22 +185,21 @@ static void cmd_show_options_print(struct cmd *self, struct cmdq_item *item, struct options_entry *o, const char *array_key, int parent) { - struct args *args = cmd_get_args(self); - struct options_array_item *a; - const char *name = options_name(o); - char *value, *tmp = NULL, *escaped; + struct args *args = cmd_get_args(self); + struct options_array_item *a; + struct format_tree *ft; + const char *name = options_name(o); + const char *template = args_get(args, 'F'); + char *value, *line; + int is_hook = 0, is_user = 0; + int has_value = 1; + const struct options_table_entry *oe = options_table_entry(o); - if (array_key != NULL) { - xasprintf(&tmp, "%s[%s]", name, array_key); - name = tmp; - } else { - if (options_is_array(o)) { - a = options_array_first(o); - if (a == NULL) { - if (!args_has(args, 'v')) - cmdq_print(item, "%s", name); - return; - } + if (array_key != NULL) + value = options_to_string(o, array_key, 0); + else if (options_is_array(o)) { + a = options_array_first(o); + if (a != NULL) { while (a != NULL) { array_key = options_array_item_key(a); cmd_show_options_print(self, item, o, array_key, @@ -188,54 +208,106 @@ cmd_show_options_print(struct cmd *self, struct cmdq_item *item, } return; } - } + if (template == NULL && args_has(args, 'v')) + return; + value = xstrdup(""); + has_value = 0; + } else + value = options_to_string(o, NULL, 0); - value = options_to_string(o, array_key, 0); - if (args_has(args, 'v')) - cmdq_print(item, "%s", value); - else if (options_is_string(o)) { - escaped = args_escape(value); - if (parent) - cmdq_print(item, "%s* %s", name, escaped); - else - cmdq_print(item, "%s %s", name, escaped); - free(escaped); + if (template == NULL) + template = SHOW_OPTIONS_TEMPLATE; + + if (oe != NULL && (oe->flags & OPTIONS_TABLE_IS_HOOK)) + is_hook = 1; + else if (oe == NULL) + is_user = 1; + + ft = format_create_from_target(item); + format_add(ft, "option_name", "%s", name); + format_add(ft, "option_value", "%s", value); + format_add(ft, "option_value_only", "%d", args_has(args, 'v')); + format_add(ft, "option_is_parent", "%d", parent); + format_add(ft, "option_is_array", "%d", options_is_array(o)); + format_add(ft, "option_is_string", "%d", options_is_string(o)); + format_add(ft, "option_is_hook", "%d", is_hook); + format_add(ft, "option_is_user", "%d", is_user); + format_add(ft, "option_has_value", "%d", has_value); + if (array_key != NULL) { + format_add(ft, "option_array_key", "%s", array_key); + format_add(ft, "option_has_array_key", "1"); } else { - if (parent) - cmdq_print(item, "%s* %s", name, value); - else - cmdq_print(item, "%s %s", name, value); + format_add(ft, "option_array_key", "%s", ""); + format_add(ft, "option_has_array_key", "0"); } - free(value); + line = format_expand(ft, template); + format_free(ft); - free(tmp); + cmdq_print(item, "%s", line); + free(line); + free(value); } static void -cmd_show_hooks_print_monitor(struct cmdq_item *item, struct options_entry *o) +cmd_show_hooks_print_monitor(struct cmd *self, struct cmdq_item *item, + struct options_entry *o) { - char *value; + struct args *args = cmd_get_args(self); + struct format_tree *ft; + enum monitor_type type; + const char *template = args_get(args, 'F'), *format; + char *value, *target, *line; + int id; value = hooks_monitor_to_string(o); if (value == NULL) return; - cmdq_print(item, "%s", value); - free(value); -} - -/* Show all hook monitors. */ -static enum cmd_retval -cmd_show_hooks_monitor(__unused struct cmd *self, struct cmdq_item *item, - __unused int scope, struct options *oo) -{ - struct options_entry *o; - - o = options_first(oo); - while (o != NULL) { - cmd_show_hooks_print_monitor(item, o); - o = options_next(o); + if (!hooks_monitor_get(o, &type, &id, &format)) { + free(value); + return; } - return (CMD_RETURN_NORMAL); + if (template == NULL) + template = SHOW_HOOKS_MONITOR_TEMPLATE; + + switch (type) { + case MONITOR_SESSION: + target = xstrdup(""); + break; + case MONITOR_PANE: + xasprintf(&target, "%%%d", id); + break; + case MONITOR_ALL_PANES: + target = xstrdup("%*"); + break; + case MONITOR_WINDOW: + xasprintf(&target, "@%d", id); + break; + case MONITOR_ALL_WINDOWS: + target = xstrdup("@*"); + break; + } + + ft = format_create_from_target(item); + format_add(ft, "option_name", "%s", options_name(o)); + format_add(ft, "option_value", "%s", value); + format_add(ft, "option_value_only", "%d", 0); + format_add(ft, "option_is_parent", "%d", 0); + format_add(ft, "option_is_array", "%d", 0); + format_add(ft, "option_is_string", "%d", 1); + format_add(ft, "option_is_hook", "%d", 1); + format_add(ft, "option_is_user", "%d", 1); + format_add(ft, "option_has_value", "%d", 1); + format_add(ft, "option_array_key", "%s", ""); + format_add(ft, "option_has_array_key", "0"); + format_add(ft, "hook_monitor_target", "%s", target); + format_add(ft, "hook_monitor_format", "%s", format); + line = format_expand(ft, template); + format_free(ft); + + cmdq_print(item, "%s", line); + free(line); + free(target); + free(value); } static enum cmd_retval @@ -245,18 +317,26 @@ cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope, struct args *args = cmd_get_args(self); const struct options_table_entry *oe; struct options_entry *o; - struct options_array_item *a; - const char *name, *array_key; - int parent; + const char *name; + int parent, is_user_hook; o = options_first(oo); while (o != NULL) { if (options_table_entry(o) == NULL) { name = options_name(o); - if (cmd_get_entry(self) != &cmd_show_hooks_entry) - cmd_show_options_print(self, item, o, NULL, 0); - else if (*name == '@' && (hooks_is_event(name) || - options_get_monitor_data(o) != NULL)) + is_user_hook = 0; + if (*name == '@') { + if (hooks_is_event(name) || + options_get_monitor_data(o) != NULL) { + is_user_hook = 1; + } + } + if (cmd_get_entry(self) != &cmd_show_hooks_entry) { + if (!is_user_hook || args_has(args, 'H')) { + cmd_show_options_print(self, item, o, + NULL, 0); + } + } else if (is_user_hook) cmd_show_options_print(self, item, o, NULL, 0); } o = options_next(o); @@ -283,24 +363,7 @@ cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope, } else parent = 0; - if (!options_is_array(o)) - cmd_show_options_print(self, item, o, NULL, parent); - else if ((a = options_array_first(o)) == NULL) { - if (!args_has(args, 'v')) { - name = options_name(o); - if (parent) - cmdq_print(item, "%s*", name); - else - cmdq_print(item, "%s", name); - } - } else { - while (a != NULL) { - array_key = options_array_item_key(a); - cmd_show_options_print(self, item, o, array_key, - parent); - a = options_array_next(a); - } - } + cmd_show_options_print(self, item, o, NULL, parent); } return (CMD_RETURN_NORMAL); } diff --git a/hooks.c b/hooks.c index ae7d90de4..bd0d1d732 100644 --- a/hooks.c +++ b/hooks.c @@ -1,4 +1,4 @@ -/* $OpenBSD: hooks.c,v 1.14 2026/07/13 22:03:08 nicm Exp $ */ +/* $OpenBSD: hooks.c,v 1.15 2026/07/22 20:12:58 nicm Exp $ */ /* * Copyright (c) 2026 Nicholas Marriott @@ -472,3 +472,18 @@ hooks_monitor_to_string(struct options_entry *o) } return (s); } + +/* Get the parts of a hook monitor. */ +int +hooks_monitor_get(struct options_entry *o, enum monitor_type *type, int *id, + const char **format) +{ + struct hook_monitor *hm = options_get_monitor_data(o); + + if (hm == NULL) + return (0); + *type = hm->type; + *id = hm->id; + *format = hm->format; + return (1); +} diff --git a/tmux.1 b/tmux.1 index c3d66e73f..1a1f9fef5 100644 --- a/tmux.1 +++ b/tmux.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: tmux.1,v 1.1147 2026/07/22 08:19:14 nicm Exp $ +.\" $OpenBSD: tmux.1,v 1.1148 2026/07/22 20:12:58 nicm Exp $ .\" .\" Copyright (c) 2007 Nicholas Marriott .\" @@ -4689,6 +4689,7 @@ the result would be the default background and a blue foreground. .Tg show .It Xo Ic show\-options .Op Fl AgHpqsvw +.Op Fl F Ar format .Op Fl t Ar target\-pane .Op Ar option .Xc @@ -4716,6 +4717,10 @@ Global session or window options are listed if is used. .Fl v shows only the option value, not the name. +With +.Fl F , +.Ar format +is used for each option. If .Fl q is set, no error will be returned if @@ -6593,6 +6598,7 @@ run immediately. .It Xo Ic show\-hooks .Op Fl Bgpw +.Op Fl F Ar format .Op Fl t Ar target\-pane .Op Ar hook .Xc @@ -6605,6 +6611,10 @@ With shows the subscriptions installed with .Em set\-hook .Fl B . +With +.Fl F , +.Ar format +is used for each hook. .El .Sh MOUSE SUPPORT If the @@ -7269,6 +7279,8 @@ The following variables are available, where appropriate: .It Li "hook" Ta "" Ta "Name of running hook, if any" .It Li "hook_client" Ta "" Ta "Name of client where hook was run, if any" .It Li "hook_last" Ta "" Ta "Previous value for a monitor hook" +.It Li "hook_monitor_format" Ta "" Ta "Format for a monitor hook" +.It Li "hook_monitor_target" Ta "" Ta "Target for a monitor hook" .It Li "hook_pane" Ta "" Ta "ID of pane where hook was run, if any" .It Li "hook_session" Ta "" Ta "ID of session where hook was run, if any" .It Li "hook_session_name" Ta "" Ta "Name of session where hook was run, if any" @@ -7310,6 +7322,17 @@ The following variables are available, where appropriate: .It Li "next_session_id" Ta "" Ta "Unique session ID for next new session" .It Li "next_window_active" Ta "" Ta "1 if next window in W: loop is active" .It Li "next_window_index" Ta "" Ta "Index of next window in W: loop" +.It Li "option_array_key" Ta "" Ta "Array key, if option is an array item" +.It Li "option_has_array_key" Ta "" Ta "1 if option array key is present" +.It Li "option_has_value" Ta "" Ta "1 if option value is present" +.It Li "option_is_array" Ta "" Ta "1 if option is an array" +.It Li "option_is_hook" Ta "" Ta "1 if option is a hook" +.It Li "option_is_parent" Ta "" Ta "1 if option is inherited" +.It Li "option_is_string" Ta "" Ta "1 if option is a string" +.It Li "option_is_user" Ta "" Ta "1 if option is a user option" +.It Li "option_name" Ta "" Ta "Name of option" +.It Li "option_value" Ta "" Ta "Value of option" +.It Li "option_value_only" Ta "" Ta "1 if only option values will be shown" .It Li "origin_flag" Ta "" Ta "Pane origin flag" .It Li "pane_active" Ta "" Ta "1 if active pane" .It Li "pane_at_bottom" Ta "" Ta "1 if pane is at the bottom of window" diff --git a/tmux.h b/tmux.h index 04ad25b82..0e40a5ea2 100644 --- a/tmux.h +++ b/tmux.h @@ -1,4 +1,4 @@ -/* $OpenBSD: tmux.h,v 1.1413 2026/07/22 08:19:14 nicm Exp $ */ +/* $OpenBSD: tmux.h,v 1.1414 2026/07/22 20:12:58 nicm Exp $ */ /* * Copyright (c) 2007 Nicholas Marriott @@ -2742,6 +2742,8 @@ void hooks_monitor_add(struct cmdq_item *, struct options *, void hooks_monitor_remove(struct options *, const char *); void hooks_monitor_free(void *); char *hooks_monitor_to_string(struct options_entry *); +int hooks_monitor_get(struct options_entry *, enum monitor_type *, int *, + const char **); /* options.c */ struct options *options_create(struct options *);