aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/eval.c57
-rw-r--r--src/nvim/eval/typval.c52
-rw-r--r--src/nvim/ops.c3
-rw-r--r--src/nvim/undo.c3
4 files changed, 67 insertions, 48 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index afbb93ef92..e7c2d95cdd 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -5570,42 +5570,6 @@ int dict_add_nr_str(dict_T *d, char *key, long nr, char_u *str)
}
/*
- * Add a list entry to dictionary "d".
- * Returns FAIL when key already exists.
- */
-int dict_add_list(dict_T *d, char *key, list_T *list)
-{
- dictitem_T *item = tv_dict_item_alloc(key);
-
- item->di_tv.v_lock = 0;
- item->di_tv.v_type = VAR_LIST;
- item->di_tv.vval.v_list = list;
- if (tv_dict_add(d, item) == FAIL) {
- tv_dict_item_free(item);
- return FAIL;
- }
- ++list->lv_refcount;
- return OK;
-}
-
-/// Add a dict entry to dictionary "d".
-/// Returns FAIL when out of memory and when key already exists.
-int dict_add_dict(dict_T *d, char *key, dict_T *dict)
-{
- dictitem_T *const item = tv_dict_item_alloc(key);
-
- item->di_tv.v_lock = 0;
- item->di_tv.v_type = VAR_DICT;
- item->di_tv.vval.v_dict = dict;
- if (tv_dict_add(d, item) == FAIL) {
- tv_dict_item_free(item);
- return FAIL;
- }
- dict->dv_refcount++;
- return OK;
-}
-
-/*
* Allocate a variable for a Dictionary and fill it from "*arg".
* Return OK or FAIL. Returns NOTDONE for {expr}.
*/
@@ -9128,7 +9092,7 @@ static dict_T *get_buffer_info(buf_T *buf)
NULL);
// Get a reference to buffer variables
- dict_add_dict(dict, "variables", buf->b_vars);
+ tv_dict_add_dict(dict, S_LEN("variables"), buf->b_vars);
// List of windows displaying this buffer
list_T *const windows = tv_list_alloc();
@@ -9137,13 +9101,13 @@ static dict_T *get_buffer_info(buf_T *buf)
tv_list_append_number(windows, (varnumber_T)wp->handle);
}
}
- dict_add_list(dict, "windows", windows);
+ tv_dict_add_list(dict, S_LEN("windows"), windows);
if (buf->b_signlist != NULL) {
// List of signs placed in this buffer
list_T *const signs = tv_list_alloc();
get_buffer_signs(buf, signs);
- dict_add_list(dict, "signs", signs);
+ tv_dict_add_list(dict, S_LEN("signs"), signs);
}
return dict;
@@ -9900,7 +9864,7 @@ static void f_getmatches(typval_T *argvars, typval_T *rettv, FunPtr fptr)
// match added with matchaddpos()
for (i = 0; i < MAXPOSMATCH; ++i) {
llpos_T *llpos;
- char buf[6];
+ char buf[6];
llpos = &cur->pos.pos[i];
if (llpos->lnum == 0) {
@@ -9912,8 +9876,9 @@ static void f_getmatches(typval_T *argvars, typval_T *rettv, FunPtr fptr)
tv_list_append_number(l, (varnumber_T)llpos->col);
tv_list_append_number(l, (varnumber_T)llpos->len);
}
- sprintf(buf, "pos%d", i + 1);
- dict_add_list(dict, buf, l);
+ int len = snprintf(buf, sizeof(buf), "pos%d", i + 1);
+ assert((size_t)len < sizeof(buf));
+ tv_dict_add_list(dict, buf, (size_t)len, l);
}
} else {
dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
@@ -10082,10 +10047,10 @@ static dict_T *get_tabpage_info(tabpage_T *tp, int tp_idx)
FOR_ALL_WINDOWS_IN_TAB(wp, tp) {
tv_list_append_number(l, (varnumber_T)wp->handle);
}
- dict_add_list(dict, "windows", l);
+ tv_dict_add_list(dict, S_LEN("windows"), l);
// Make a reference to tabpage variables
- dict_add_dict(dict, "variables", tp->tp_vars);
+ tv_dict_add_dict(dict, S_LEN("variables"), tp->tp_vars);
return dict;
}
@@ -10187,7 +10152,7 @@ static dict_T *get_win_info(win_T *wp, int16_t tpnr, int16_t winnr)
NULL);
// Add a reference to window variables
- dict_add_dict(dict, "variables", wp->w_vars);
+ tv_dict_add_dict(dict, S_LEN("variables"), wp->w_vars);
return dict;
}
@@ -17029,7 +16994,7 @@ static void f_undotree(typval_T *argvars, typval_T *rettv, FunPtr fptr)
list = tv_list_alloc();
u_eval_tree(curbuf->b_u_oldhead, list);
- dict_add_list(dict, "entries", list);
+ tv_dict_add_list(dict, S_LEN("entries"), list);
}
/*
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c
index 0df818040b..f9885926c7 100644
--- a/src/nvim/eval/typval.c
+++ b/src/nvim/eval/typval.c
@@ -1166,6 +1166,58 @@ bool tv_dict_get_callback(dict_T *const d,
return res;
}
+//{{{2 dict_add*
+
+/// Add a list entry to dictionary
+///
+/// @param[out] d Dictionary to add entry to.
+/// @param[in] key Key to add.
+/// @param[in] key_len Key length.
+/// @param list List to add. Will have reference count incremented.
+///
+/// @return OK in case of success, FAIL when key already exists.
+int tv_dict_add_list(dict_T *const d, const char *const key,
+ const size_t key_len, list_T *const list)
+ FUNC_ATTR_NONNULL_ALL
+{
+ dictitem_T *item = tv_dict_item_alloc_len(key, key_len);
+
+ item->di_tv.v_lock = VAR_UNLOCKED;
+ item->di_tv.v_type = VAR_LIST;
+ item->di_tv.vval.v_list = list;
+ if (tv_dict_add(d, item) == FAIL) {
+ tv_dict_item_free(item);
+ return FAIL;
+ }
+ list->lv_refcount++;
+ return OK;
+}
+
+/// Add a dictionary entry to dictionary
+///
+/// @param[out] d Dictionary to add entry to.
+/// @param[in] key Key to add.
+/// @param[in] key_len Key length.
+/// @param dict Dictionary to add. Will have reference count incremented.
+///
+/// @return OK in case of success, FAIL when key already exists.
+int tv_dict_add_dict(dict_T *const d, const char *const key,
+ const size_t key_len, dict_T *const dict)
+ FUNC_ATTR_NONNULL_ALL
+{
+ dictitem_T *const item = tv_dict_item_alloc(key);
+
+ item->di_tv.v_lock = VAR_UNLOCKED;
+ item->di_tv.v_type = VAR_DICT;
+ item->di_tv.vval.v_dict = dict;
+ if (tv_dict_add(d, item) == FAIL) {
+ tv_dict_item_free(item);
+ return FAIL;
+ }
+ dict->dv_refcount++;
+ return OK;
+}
+
//{{{2 Operations on the whole dict
/// Clear all the keys of a Dictionary. "d" remains a valid empty Dictionary.
diff --git a/src/nvim/ops.c b/src/nvim/ops.c
index 4bca2f0889..28ac5aad4e 100644
--- a/src/nvim/ops.c
+++ b/src/nvim/ops.c
@@ -42,6 +42,7 @@
#include "nvim/terminal.h"
#include "nvim/ui.h"
#include "nvim/undo.h"
+#include "nvim/macros.h"
#include "nvim/window.h"
#include "nvim/os/input.h"
#include "nvim/os/time.h"
@@ -2561,7 +2562,7 @@ static void do_autocmd_textyankpost(oparg_T *oap, yankreg_T *reg)
tv_list_append_string(list, (const char *)reg->y_array[i], -1);
}
list->lv_lock = VAR_FIXED;
- dict_add_list(dict, "regcontents", list);
+ tv_dict_add_list(dict, S_LEN("regcontents"), list);
// the register type
char buf[NUMBUFLEN+2];
diff --git a/src/nvim/undo.c b/src/nvim/undo.c
index 6e21dccebb..8c75d28f2a 100644
--- a/src/nvim/undo.c
+++ b/src/nvim/undo.c
@@ -82,6 +82,7 @@
#include "nvim/vim.h"
#include "nvim/ascii.h"
#include "nvim/undo.h"
+#include "nvim/macros.h"
#include "nvim/cursor.h"
#include "nvim/edit.h"
#include "nvim/eval.h"
@@ -2956,7 +2957,7 @@ void u_eval_tree(u_header_T *first_uhp, list_T *list)
/* Recursive call to add alternate undo tree. */
u_eval_tree(uhp->uh_alt_next.ptr, alt_list);
- dict_add_list(dict, "alt", alt_list);
+ tv_dict_add_list(dict, S_LEN("alt"), alt_list);
}
tv_list_append_dict(list, dict);