Add a key to change an array key in customize mode.

This commit is contained in:
nicm
2026-07-06 14:40:57 +00:00
parent 1117caaf09
commit 20ef19b42f
4 changed files with 338 additions and 122 deletions

View File

@@ -178,10 +178,10 @@ cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
struct window_pane *loop;
struct options *oo;
struct options_entry *parent, *o, *po;
char *name, *argument, *expanded = NULL;
char *cause;
char *name, *argument, *cause;
char *expanded = NULL, *array_key = NULL;
const char *value;
int window, idx, already, error, ambiguous;
int window, already, error, ambiguous;
int scope;
window = (cmd_get_entry(self) == &cmd_set_window_option_entry);
@@ -202,8 +202,8 @@ cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
return (CMD_RETURN_NORMAL);
}
/* Parse option name and index. */
name = options_match(argument, &idx, &ambiguous);
/* Parse option name and array key. */
name = options_match(argument, &array_key, &ambiguous);
if (name == NULL) {
if (args_has(args, 'q'))
goto out;
@@ -235,21 +235,23 @@ cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
o = options_get_only(oo, name);
parent = options_get(oo, name);
/* Check that array options and indexes match up. */
if (idx != -1 && (*name == '@' || !options_is_array(parent))) {
/* Check that array options and keys match up. */
if (array_key != NULL && (*name == '@' || !options_is_array(parent))) {
cmdq_error(item, "not an array: %s", argument);
goto fail;
}
/* With -o, check this option is not already set. */
if (!args_has(args, 'u') && args_has(args, 'o')) {
if (idx == -1)
if (array_key == NULL)
already = (o != NULL);
else {
if (o == NULL)
already = 0;
else if (options_array_get(o, array_key) != NULL)
already = 1;
else
already = (options_array_get(o, idx) != NULL);
already = 0;
}
if (already) {
if (args_has(args, 'q'))
@@ -265,7 +267,8 @@ cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
po = options_get_only(loop->options, name);
if (po == NULL)
continue;
if (options_remove_or_default(po, idx, &cause) != 0) {
if (options_remove_or_default(po, array_key,
&cause) != 0) {
cmdq_error(item, "%s", cause);
free(cause);
goto fail;
@@ -275,7 +278,7 @@ cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
if (args_has(args, 'u') || args_has(args, 'U')) {
if (o == NULL)
goto out;
if (options_remove_or_default(o, idx, &cause) != 0) {
if (options_remove_or_default(o, array_key, &cause) != 0) {
cmdq_error(item, "%s", cause);
free(cause);
goto fail;
@@ -286,7 +289,7 @@ cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
goto fail;
}
options_set_string(oo, name, append, "%s", value);
} else if (idx == -1 && !options_is_array(parent)) {
} else if (array_key == NULL && !options_is_array(parent)) {
error = options_from_string(oo, options_table_entry(parent),
options_table_entry(parent)->name, value,
args_has(args, 'a'), &cause);
@@ -302,7 +305,7 @@ cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
}
if (o == NULL)
o = options_empty(oo, options_table_entry(parent));
if (idx == -1) {
if (array_key == NULL) {
if (!append)
options_array_clear(o);
if (options_array_assign(o, value, &cause) != 0) {
@@ -310,7 +313,7 @@ cmd_set_option_exec(struct cmd *self, struct cmdq_item *item)
free(cause);
goto fail;
}
} else if (options_array_set(o, idx, value, append,
} else if (options_array_set(o, array_key, value, append,
&cause) != 0) {
cmdq_error(item, "%s", cause);
free(cause);
@@ -324,11 +327,13 @@ out:
free(argument);
free(expanded);
free(name);
free(array_key);
return (CMD_RETURN_NORMAL);
fail:
free(argument);
free(expanded);
free(name);
free(array_key);
return (CMD_RETURN_ERROR);
}