input: export input.conf comments ot input-bindings property

This is supposed to turn input.conf comments into inline documentation.
Whether this will be useful depends on whether there'll be a script
using this field.

This changes a small aspect of input.conf parsing fundamentally: this
attempts to strip comments/whitespace from the command string, which
will later be used to generate the command when a key binding is
executed. This should not have any negative effects, but there could be
unknown bugs. (For some reason, every command is parsed when input.conf
is parsed, but it still only stores the string for the command. I guess
that saves some minor amount of memory.)
This commit is contained in:
wm4
2019-11-23 01:09:09 +01:00
parent f379cf0bf8
commit 2dc6b27ee7
4 changed files with 34 additions and 7 deletions

View File

@@ -2652,8 +2652,8 @@ Property list
``cmd``
The command mapped to the key. (Currently, this is exactly the same
string as specified in the source. It's possible that it will be
normalized in the future.)
string as specified in the source, other than stripping whitespace and
comments. It's possible that it will be normalized in the future.)
``is_weak``
If set to true, any existing and active user bindings will take priority.
@@ -2674,6 +2674,11 @@ Property list
in some cases. In addition, this value is dynamic and can change around
at runtime.
``comment``
If available, the comment following the command on the same line. (For
example, the input.conf entry ``f cycle bla # toggle bla`` would
result in an entry with ``comment = "toggle bla", cmd = "cycle bla"``.)
This property is read-only, and change notification is not supported.
Currently, there is no mechanism to change key bindings at runtime, other
than scripts adding or removing their own bindings.

View File

@@ -449,7 +449,6 @@ mp_cmd_t *mp_input_parse_cmd_str(struct mp_log *log, bstr str, const char *loc)
*list = (struct mp_cmd) {
.name = (char *)mp_cmd_list.name,
.def = &mp_cmd_list,
.original = bstrto0(list, original),
};
talloc_steal(list, cmd);
struct mp_cmd_arg arg = {0};
@@ -469,6 +468,16 @@ mp_cmd_t *mp_input_parse_cmd_str(struct mp_log *log, bstr str, const char *loc)
p_prev = &sub->queue_next;
}
cmd->original = bstrto0(cmd, bstr_strip(
bstr_splice(original, 0, str.start - original.start)));
str = bstr_strip(str);
if (bstr_eatstart0(&str, "#") && !bstr_startswith0(str, "#")) {
str = bstr_strip(str);
if (str.len)
cmd->desc = bstrto0(cmd, str);
}
done:
talloc_free(tmp);
return cmd;
@@ -510,6 +519,7 @@ mp_cmd_t *mp_cmd_clone(mp_cmd_t *cmd)
m_option_copy(ret->args[i].type, &ret->args[i].v, &cmd->args[i].v);
}
ret->original = talloc_strdup(ret, cmd->original);
ret->desc = talloc_strdup(ret, cmd->desc);
ret->sender = NULL;
ret->key_name = talloc_strdup(ret, ret->key_name);
ret->key_text = talloc_strdup(ret, ret->key_text);

View File

@@ -100,6 +100,7 @@ typedef struct mp_cmd {
int nargs;
int flags; // mp_cmd_flags bitfield
char *original;
char *desc; // (usually NULL since stripped away later)
char *input_section;
bool is_up_down : 1;
bool is_up : 1;

View File

@@ -64,6 +64,7 @@ struct cmd_bind {
int num_keys;
char *cmd;
char *location; // filename/line number of definition
char *desc; // human readable description
bool is_builtin;
struct cmd_bind_section *owner;
};
@@ -1149,7 +1150,7 @@ static bool bind_matches_key(struct cmd_bind *bind, int num_keys, const int *key
static void bind_keys(struct input_ctx *ictx, bool builtin, bstr section,
const int *keys, int num_keys, bstr command,
const char *loc)
const char *loc, const char *desc)
{
struct cmd_bind_section *bs = get_bind_section(ictx, section);
struct cmd_bind *bind = NULL;
@@ -1175,6 +1176,7 @@ static void bind_keys(struct input_ctx *ictx, bool builtin, bstr section,
*bind = (struct cmd_bind) {
.cmd = bstrdup0(bs->binds, command),
.location = talloc_strdup(bs->binds, loc),
.desc = talloc_strdup(bs->binds, desc),
.owner = bs,
.is_builtin = builtin,
.num_keys = num_keys,
@@ -1250,11 +1252,18 @@ static int parse_config(struct input_ctx *ictx, bool builtin, bstr data,
}
}
bind_keys(ictx, builtin, section, keys, num_keys, command, cur_loc);
// Print warnings if invalid commands are encountered.
struct mp_cmd *cmd = mp_input_parse_cmd(ictx, command, cur_loc);
const char *desc = NULL;
if (cmd) {
desc = cmd->desc;
command = bstr0(cmd->original);
}
bind_keys(ictx, builtin, section, keys, num_keys, command, cur_loc, desc);
n_binds++;
// Print warnings if invalid commands are encountered.
talloc_free(mp_input_parse_cmd(ictx, command, cur_loc));
talloc_free(cmd);
}
talloc_free(cur_loc);
@@ -1524,6 +1533,8 @@ struct mpv_node mp_input_get_bindings(struct input_ctx *ictx)
node_map_add_string(entry, "cmd", b->cmd);
node_map_add_flag(entry, "is_weak", b->is_builtin);
node_map_add_int64(entry, "priority", b_priority);
if (b->desc)
node_map_add_string(entry, "comment", b->desc);
char *key = mp_input_get_key_combo_name(b->keys, b->num_keys);
node_map_add_string(entry, "key", key);