aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/eval
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/eval')
-rw-r--r--src/nvim/eval/executor.c6
-rw-r--r--src/nvim/eval/typval.c28
-rw-r--r--src/nvim/eval/typval.h2
3 files changed, 31 insertions, 5 deletions
diff --git a/src/nvim/eval/executor.c b/src/nvim/eval/executor.c
index ab48ace400..41b55e4a57 100644
--- a/src/nvim/eval/executor.c
+++ b/src/nvim/eval/executor.c
@@ -77,10 +77,10 @@ int eexe_mod_op(typval_T *const tv1, const typval_T *const tv2,
if (tv2->v_type == VAR_FLOAT) {
break;
}
- char *s = (char *)get_tv_string(tv1);
+ const char *tvs = tv_get_string(tv1);
char numbuf[NUMBUFLEN];
- s = (char *)concat_str((char_u *)s,
- get_tv_string_buf(tv2, (char_u *)numbuf));
+ char *const s = (char *)concat_str(
+ (const char_u *)tvs, get_tv_string_buf(tv2, (char_u *)numbuf));
tv_clear(tv1);
tv1->v_type = VAR_STRING;
tv1->vval.v_string = (char_u *)s;
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c
index 62460bcc3a..cbeb2e059c 100644
--- a/src/nvim/eval/typval.c
+++ b/src/nvim/eval/typval.c
@@ -733,7 +733,7 @@ varnumber_T tv_list_find_nr(list_T *const l, const int n, bool *ret_error)
/// @param[in] n Index in a list.
///
/// @return [allocated] Copy of the list item string value.
-char *tv_list_find_str(list_T *l, int n)
+const char *tv_list_find_str(list_T *l, int n)
FUNC_ATTR_MALLOC
{
const listitem_T *const li = tv_list_find(l, n - 1);
@@ -741,7 +741,7 @@ char *tv_list_find_str(list_T *l, int n)
EMSGN(_(e_listidx), n);
return NULL;
}
- return (char *)get_tv_string(&li->li_tv);
+ return tv_get_string(&li->li_tv);
}
/// Locate item in a list and return its index
@@ -2014,3 +2014,27 @@ bool tv_check_str_or_nr(const typval_T *const tv)
assert(false);
return false;
}
+
+//{{{2 Get
+
+/// Get the string value of a variable
+///
+/// @warning For number and special values it uses a single, static buffer. It
+/// may be used only once, next call to get_tv_string may reuse it. Use
+/// get_tv_string_buf() if you need to use tv_get_string() output after
+/// calling it again.
+///
+/// @note get_tv_string_chk() and get_tv_string_buf_chk() are similar, but
+/// return NULL on error.
+///
+/// @param[in] varp Varible to get value of.
+///
+/// @return Variable value if it is VAR_STRING variable, number converted to
+/// a string for VAR_NUMBER, v: variable name for VAR_SPECIAL or empty
+/// string.
+const char *tv_get_string(const typval_T *const varp)
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET FUNC_ATTR_WARN_UNUSED_RESULT
+{
+ static char_u mybuf[NUMBUFLEN];
+ return (const char *)get_tv_string_buf((typval_T *)varp, mybuf);
+}
diff --git a/src/nvim/eval/typval.h b/src/nvim/eval/typval.h
index 0eef2ddaac..5645772124 100644
--- a/src/nvim/eval/typval.h
+++ b/src/nvim/eval/typval.h
@@ -205,6 +205,8 @@ struct dictvar_S {
/// Type used for script ID
typedef int scid_T;
+/// Format argument for scid_T
+#define PRIdSCID "d"
// Structure to hold info for a function that is currently being executed.
typedef struct funccall_S funccall_T;