summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2018-01-24 05:12:05 +0100
committerKevin Mitchell <kevmitch@gmail.com>2018-01-25 20:18:32 -0800
commit3deef308c8e57ca1fa6bdd60abd6a3213d7eb3c6 (patch)
tree8e274c34373fbd70785123c1eb8194cd1abfc656
parent415fc6e3271827fa9195a10c71bb5025e8733a5e (diff)
downloadmpv-3deef308c8e57ca1fa6bdd60abd6a3213d7eb3c6.tar.bz2
mpv-3deef308c8e57ca1fa6bdd60abd6a3213d7eb3c6.tar.xz
options: add string list -toggle action
-rw-r--r--DOCS/man/mpv.rst1
-rw-r--r--options/m_option.c24
2 files changed, 25 insertions, 0 deletions
diff --git a/DOCS/man/mpv.rst b/DOCS/man/mpv.rst
index 1919485069..7f4561c6a9 100644
--- a/DOCS/man/mpv.rst
+++ b/DOCS/man/mpv.rst
@@ -435,6 +435,7 @@ Suffix Meaning
-del Delete an existing item by integer index
-pre Prepend 1 or more items
-set Set a list of items
+-toggle Append an item, or remove if if it already exists
============= ===============================================
Although some operations allow specifying multiple ``,``-separated items, using
diff --git a/options/m_option.c b/options/m_option.c
index d011c2a187..522e771356 100644
--- a/options/m_option.c
+++ b/options/m_option.c
@@ -1318,6 +1318,15 @@ static struct bstr get_nextsep(struct bstr *ptr, char sep, bool modify)
return bstr_splice(orig, 0, str.start - orig.start);
}
+static int find_list_bstr(char **list, bstr item)
+{
+ for (int n = 0; list && list[n]; n++) {
+ if (bstr_equals0(item, list[n]))
+ return n;
+ }
+ return -1;
+}
+
static int parse_str_list_impl(struct mp_log *log, const m_option_t *opt,
struct bstr name, struct bstr param, void *dst,
int default_op)
@@ -1339,6 +1348,20 @@ static int parse_str_list_impl(struct mp_log *log, const m_option_t *opt,
op = OP_CLR;
} else if (bstr_endswith0(name, "-set")) {
op = OP_NONE;
+ } else if (bstr_endswith0(name, "-toggle")) {
+ if (dst) {
+ char **list = VAL(dst);
+ int index = find_list_bstr(list, param);
+ if (index >= 0) {
+ char *old = list[index];
+ for (int n = index; list[n]; n++)
+ list[n] = list[n + 1];
+ talloc_free(old);
+ return 1;
+ }
+ }
+ op = OP_ADD;
+ multi = false;
}
// Clear the list ??
@@ -1510,6 +1533,7 @@ const m_option_type_t m_option_type_string_list = {
{"del"},
{"pre"},
{"set"},
+ {"toggle"},
{0}
},
};