Switch show-options over to using a format and add -F.

This commit is contained in:
nicm
2026-07-22 20:12:58 +00:00
committed by tmux update bot
parent 2a5389416a
commit 7277712ca8
4 changed files with 200 additions and 97 deletions

View File

@@ -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 <nicholas.marriott@gmail.com> * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -28,14 +28,23 @@
* Show options. * 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 enum cmd_retval cmd_show_options_exec(struct cmd *, struct cmdq_item *);
static void cmd_show_options_print(struct cmd *, struct cmdq_item *, static void cmd_show_options_print(struct cmd *, struct cmdq_item *,
struct options_entry *, const char *, int); struct options_entry *, const char *, int);
static void cmd_show_hooks_print_monitor(struct cmdq_item *, static void cmd_show_hooks_print_monitor(struct cmd *,
struct options_entry *); struct cmdq_item *, struct options_entry *);
static enum cmd_retval cmd_show_hooks_monitor(struct cmd *, struct cmdq_item *,
int, struct options *);
static enum cmd_retval cmd_show_options_all(struct cmd *, struct cmdq_item *, static enum cmd_retval cmd_show_options_all(struct cmd *, struct cmdq_item *,
int, struct options *); int, struct options *);
@@ -43,8 +52,8 @@ const struct cmd_entry cmd_show_options_entry = {
.name = "show-options", .name = "show-options",
.alias = "show", .alias = "show",
.args = { "AgHpqst:vw", 0, 1, NULL }, .args = { "AgF:Hpqst:vw", 0, 1, NULL },
.usage = "[-AgHpqsvw] " CMD_TARGET_PANE_USAGE " [option]", .usage = "[-AgHpqsvw] [-F format] " CMD_TARGET_PANE_USAGE " [option]",
.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL }, .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", .name = "show-window-options",
.alias = "showw", .alias = "showw",
.args = { "gvt:", 0, 1, NULL }, .args = { "F:gvt:", 0, 1, NULL },
.usage = "[-gv] " CMD_TARGET_WINDOW_USAGE " [option]", .usage = "[-gv] [-F format] " CMD_TARGET_WINDOW_USAGE " [option]",
.target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL }, .target = { 't', CMD_FIND_WINDOW, CMD_FIND_CANFAIL },
@@ -69,8 +78,8 @@ const struct cmd_entry cmd_show_hooks_entry = {
.name = "show-hooks", .name = "show-hooks",
.alias = NULL, .alias = NULL,
.args = { "Bgpt:w", 0, 1, NULL }, .args = { "BF:gpt:w", 0, 1, NULL },
.usage = "[-Bgpw] " CMD_TARGET_PANE_USAGE " [hook]", .usage = "[-Bgpw] [-F format] " CMD_TARGET_PANE_USAGE " [hook]",
.target = { 't', CMD_FIND_PANE, CMD_FIND_CANFAIL }, .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; struct options *oo;
char *argument, *name = NULL, *cause; char *argument, *name = NULL, *cause;
char *array_key = NULL; char *array_key = NULL;
int window, ambiguous, parent, scope; int window, ambiguous, parent, print_parent, scope;
struct options_entry *o; struct options_entry *o;
window = (cmd_get_entry(self) == &cmd_show_window_options_entry); 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); return (CMD_RETURN_ERROR);
} }
if (cmd_get_entry(self) == &cmd_show_hooks_entry && if (cmd_get_entry(self) == &cmd_show_hooks_entry &&
args_has(args, 'B')) args_has(args, 'B')) {
return (cmd_show_hooks_monitor(self, item, scope, oo)); 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)); return (cmd_show_options_all(self, item, scope, oo));
} }
argument = format_single_from_target(item, args_string(args, 0)); 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 (o != NULL) {
if (cmd_get_entry(self) == &cmd_show_hooks_entry && if (cmd_get_entry(self) == &cmd_show_hooks_entry &&
args_has(args, 'B')) args_has(args, 'B'))
cmd_show_hooks_print_monitor(item, o); cmd_show_hooks_print_monitor(self, item, o);
else else {
cmd_show_options_print(self, item, o, array_key, parent); print_parent = parent;
} if (array_key == NULL && options_is_array(o) &&
else if (*name == '@') { 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')) if (args_has(args, 'q'))
goto out; goto out;
cmdq_error(item, "invalid option: %s", argument); cmdq_error(item, "invalid option: %s", argument);
@@ -164,22 +185,21 @@ static void
cmd_show_options_print(struct cmd *self, struct cmdq_item *item, cmd_show_options_print(struct cmd *self, struct cmdq_item *item,
struct options_entry *o, const char *array_key, int parent) struct options_entry *o, const char *array_key, int parent)
{ {
struct args *args = cmd_get_args(self); struct args *args = cmd_get_args(self);
struct options_array_item *a; struct options_array_item *a;
const char *name = options_name(o); struct format_tree *ft;
char *value, *tmp = NULL, *escaped; 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) { if (array_key != NULL)
xasprintf(&tmp, "%s[%s]", name, array_key); value = options_to_string(o, array_key, 0);
name = tmp; else if (options_is_array(o)) {
} else { a = options_array_first(o);
if (options_is_array(o)) { if (a != NULL) {
a = options_array_first(o);
if (a == NULL) {
if (!args_has(args, 'v'))
cmdq_print(item, "%s", name);
return;
}
while (a != NULL) { while (a != NULL) {
array_key = options_array_item_key(a); array_key = options_array_item_key(a);
cmd_show_options_print(self, item, o, array_key, 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; 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 (template == NULL)
if (args_has(args, 'v')) template = SHOW_OPTIONS_TEMPLATE;
cmdq_print(item, "%s", value);
else if (options_is_string(o)) { if (oe != NULL && (oe->flags & OPTIONS_TABLE_IS_HOOK))
escaped = args_escape(value); is_hook = 1;
if (parent) else if (oe == NULL)
cmdq_print(item, "%s* %s", name, escaped); is_user = 1;
else
cmdq_print(item, "%s %s", name, escaped); ft = format_create_from_target(item);
free(escaped); 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 { } else {
if (parent) format_add(ft, "option_array_key", "%s", "");
cmdq_print(item, "%s* %s", name, value); format_add(ft, "option_has_array_key", "0");
else
cmdq_print(item, "%s %s", name, value);
} }
free(value); line = format_expand(ft, template);
format_free(ft);
free(tmp); cmdq_print(item, "%s", line);
free(line);
free(value);
} }
static void 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); value = hooks_monitor_to_string(o);
if (value == NULL) if (value == NULL)
return; return;
cmdq_print(item, "%s", value); if (!hooks_monitor_get(o, &type, &id, &format)) {
free(value); free(value);
} return;
/* 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);
} }
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 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); struct args *args = cmd_get_args(self);
const struct options_table_entry *oe; const struct options_table_entry *oe;
struct options_entry *o; struct options_entry *o;
struct options_array_item *a; const char *name;
const char *name, *array_key; int parent, is_user_hook;
int parent;
o = options_first(oo); o = options_first(oo);
while (o != NULL) { while (o != NULL) {
if (options_table_entry(o) == NULL) { if (options_table_entry(o) == NULL) {
name = options_name(o); name = options_name(o);
if (cmd_get_entry(self) != &cmd_show_hooks_entry) is_user_hook = 0;
cmd_show_options_print(self, item, o, NULL, 0); if (*name == '@') {
else if (*name == '@' && (hooks_is_event(name) || if (hooks_is_event(name) ||
options_get_monitor_data(o) != NULL)) 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); cmd_show_options_print(self, item, o, NULL, 0);
} }
o = options_next(o); o = options_next(o);
@@ -283,24 +363,7 @@ cmd_show_options_all(struct cmd *self, struct cmdq_item *item, int scope,
} else } else
parent = 0; parent = 0;
if (!options_is_array(o)) cmd_show_options_print(self, item, o, NULL, parent);
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);
}
}
} }
return (CMD_RETURN_NORMAL); return (CMD_RETURN_NORMAL);
} }

17
hooks.c
View File

@@ -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 <nicholas.marriott@gmail.com> * Copyright (c) 2026 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -472,3 +472,18 @@ hooks_monitor_to_string(struct options_entry *o)
} }
return (s); 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);
}

25
tmux.1
View File

@@ -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 <nicholas.marriott@gmail.com> .\" Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
.\" .\"
@@ -4689,6 +4689,7 @@ the result would be the default background and a blue foreground.
.Tg show .Tg show
.It Xo Ic show\-options .It Xo Ic show\-options
.Op Fl AgHpqsvw .Op Fl AgHpqsvw
.Op Fl F Ar format
.Op Fl t Ar target\-pane .Op Fl t Ar target\-pane
.Op Ar option .Op Ar option
.Xc .Xc
@@ -4716,6 +4717,10 @@ Global session or window options are listed if
is used. is used.
.Fl v .Fl v
shows only the option value, not the name. shows only the option value, not the name.
With
.Fl F ,
.Ar format
is used for each option.
If If
.Fl q .Fl q
is set, no error will be returned if is set, no error will be returned if
@@ -6593,6 +6598,7 @@ run
immediately. immediately.
.It Xo Ic show\-hooks .It Xo Ic show\-hooks
.Op Fl Bgpw .Op Fl Bgpw
.Op Fl F Ar format
.Op Fl t Ar target\-pane .Op Fl t Ar target\-pane
.Op Ar hook .Op Ar hook
.Xc .Xc
@@ -6605,6 +6611,10 @@ With
shows the subscriptions installed with shows the subscriptions installed with
.Em set\-hook .Em set\-hook
.Fl B . .Fl B .
With
.Fl F ,
.Ar format
is used for each hook.
.El .El
.Sh MOUSE SUPPORT .Sh MOUSE SUPPORT
If the 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" 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_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_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_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" 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" .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_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_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 "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 "origin_flag" Ta "" Ta "Pane origin flag"
.It Li "pane_active" Ta "" Ta "1 if active pane" .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" .It Li "pane_at_bottom" Ta "" Ta "1 if pane is at the bottom of window"

4
tmux.h
View File

@@ -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 <nicholas.marriott@gmail.com> * Copyright (c) 2007 Nicholas Marriott <nicholas.marriott@gmail.com>
@@ -2742,6 +2742,8 @@ void hooks_monitor_add(struct cmdq_item *, struct options *,
void hooks_monitor_remove(struct options *, const char *); void hooks_monitor_remove(struct options *, const char *);
void hooks_monitor_free(void *); void hooks_monitor_free(void *);
char *hooks_monitor_to_string(struct options_entry *); char *hooks_monitor_to_string(struct options_entry *);
int hooks_monitor_get(struct options_entry *, enum monitor_type *, int *,
const char **);
/* options.c */ /* options.c */
struct options *options_create(struct options *); struct options *options_create(struct options *);