aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/eval
diff options
context:
space:
mode:
authorMatthieu Coudron <mattator@gmail.com>2020-07-20 22:04:30 +0200
committerGitHub <noreply@github.com>2020-07-20 22:04:30 +0200
commit459800db43146680ef75f3cd251dd4d57d72bf48 (patch)
tree9359464f875da5f994ebc47c94a7b9c10235d023 /src/nvim/eval
parent409a1dcdc622368bd69559513df9c6a2f0c2f439 (diff)
parent9464399c8c8af44eea9c723f090e782af0346a1a (diff)
downloadrneovim-459800db43146680ef75f3cd251dd4d57d72bf48.tar.gz
rneovim-459800db43146680ef75f3cd251dd4d57d72bf48.tar.bz2
rneovim-459800db43146680ef75f3cd251dd4d57d72bf48.zip
Merge pull request #12575 from cbarrete/vim-8.2.0935
[RFC] vim-patch:8.2.{0935,0937}
Diffstat (limited to 'src/nvim/eval')
-rw-r--r--src/nvim/eval/funcs.c36
-rw-r--r--src/nvim/eval/typval.c51
2 files changed, 87 insertions, 0 deletions
diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c
index 831167a489..e350d09935 100644
--- a/src/nvim/eval/funcs.c
+++ b/src/nvim/eval/funcs.c
@@ -2160,6 +2160,42 @@ static void f_expandcmd(typval_T *argvars, typval_T *rettv, FunPtr fptr)
rettv->vval.v_string = cmdstr;
}
+
+/// "flatten(list[, {maxdepth}])" function
+static void f_flatten(typval_T *argvars, typval_T *rettv, FunPtr fptr)
+{
+ list_T *list;
+ long maxdepth;
+ bool error = false;
+
+ if (argvars[0].v_type != VAR_LIST) {
+ EMSG2(_(e_listarg), "flatten()");
+ return;
+ }
+
+ if (argvars[1].v_type == VAR_UNKNOWN) {
+ maxdepth = 999999;
+ } else {
+ maxdepth = (long)tv_get_number_chk(&argvars[1], &error);
+ if (error) {
+ return;
+ }
+ if (maxdepth < 0) {
+ EMSG(_("E900: maxdepth must be non-negative number"));
+ return;
+ }
+ }
+
+ list = argvars[0].vval.v_list;
+ if (list != NULL
+ && !tv_check_lock(tv_list_locked(list),
+ N_("flatten() argument"),
+ TV_TRANSLATE)
+ && tv_list_flatten(list, maxdepth) == OK) {
+ tv_copy(&argvars[0], rettv);
+ }
+}
+
/*
* "extend(list, list [, idx])" function
* "extend(dict, dict [, action])" function
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c
index 2394eb8099..ef8e66a992 100644
--- a/src/nvim/eval/typval.c
+++ b/src/nvim/eval/typval.c
@@ -640,6 +640,57 @@ tv_list_copy_error:
return NULL;
}
+/// Flatten "list" in place to depth "maxdepth".
+/// Does nothing if "maxdepth" is 0.
+///
+/// @param[in,out] list List to flatten
+/// @param[in] maxdepth Maximum depth that will be flattened
+///
+/// @return OK or FAIL
+int tv_list_flatten(list_T *list, long maxdepth)
+ FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_WARN_UNUSED_RESULT
+{
+ listitem_T *item;
+ listitem_T *to_free;
+ int n;
+ if (maxdepth == 0) {
+ return OK;
+ }
+
+ n = 0;
+ item = list->lv_first;
+ while (item != NULL) {
+ fast_breakcheck();
+ if (got_int) {
+ return FAIL;
+ }
+ if (item->li_tv.v_type == VAR_LIST) {
+ listitem_T *next = item->li_next;
+
+ tv_list_drop_items(list, item, item);
+ tv_list_extend(list, item->li_tv.vval.v_list, next);
+ tv_clear(&item->li_tv);
+ to_free = item;
+
+ if (item->li_prev == NULL) {
+ item = list->lv_first;
+ } else {
+ item = item->li_prev->li_next;
+ }
+ xfree(to_free);
+
+ if (++n >= maxdepth) {
+ n = 0;
+ item = next;
+ }
+ } else {
+ n = 0;
+ item = item->li_next;
+ }
+ }
+ return OK;
+}
+
/// Extend first list with the second
///
/// @param[out] l1 List to extend.