aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/nvim/eval.c50
-rw-r--r--src/nvim/eval/typval.c2
2 files changed, 29 insertions, 23 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index 6307dd8abc..2a7314d969 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -9190,13 +9190,34 @@ static void get_buffer_lines(buf_T *buf, linenr_T start, linenr_T end, int retli
}
}
+/// Get the line number from VimL object
+///
+/// @note Unlike tv_get_lnum(), this one supports only "$" special string.
+///
+/// @param[in] tv Object to get value from. Is expected to be a number or
+/// a special string "$".
+/// @param[in] buf Buffer to take last line number from in case tv is "$". May
+/// be NULL, in this case "$" results in zero return.
+///
+/// @return Line number or 0 in case of error.
+static linenr_T tv_get_lnum_buf(const typval_T *const tv,
+ const buf_T *const buf)
+ FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_WARN_UNUSED_RESULT
+{
+ if (tv->v_type == VAR_STRING
+ && tv->vval.v_string != NULL
+ && tv->vval.v_string[0] == '$'
+ && buf != NULL) {
+ return buf->b_ml.ml_line_count;
+ }
+ return get_tv_number_chk(tv, NULL);
+}
+
/*
* "getbufline()" function
*/
static void f_getbufline(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
- linenr_T lnum;
- linenr_T end;
buf_T *buf = NULL;
if (tv_check_str_or_nr(&argvars[0])) {
@@ -9205,12 +9226,10 @@ static void f_getbufline(typval_T *argvars, typval_T *rettv, FunPtr fptr)
emsg_off--;
}
- lnum = get_tv_lnum_buf(&argvars[1], buf);
- if (argvars[2].v_type == VAR_UNKNOWN) {
- end = lnum;
- } else {
- end = get_tv_lnum_buf(&argvars[2], buf);
- }
+ const linenr_T lnum = tv_get_lnum_buf(&argvars[1], buf);
+ const linenr_T end = (argvars[2].v_type == VAR_UNKNOWN
+ ? lnum
+ : tv_get_lnum_buf(&argvars[2], buf));
get_buffer_lines(buf, lnum, end, true, rettv);
}
@@ -18464,21 +18483,6 @@ varnumber_T get_tv_number_chk(const typval_T *const varp, bool *const denote)
return n;
}
-/*
- * Get the lnum from the first argument.
- * Also accepts "$", then "buf" is used.
- * Returns 0 on error.
- */
-static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
-{
- if (argvars[0].v_type == VAR_STRING
- && argvars[0].vval.v_string != NULL
- && argvars[0].vval.v_string[0] == '$'
- && buf != NULL)
- return buf->b_ml.ml_line_count;
- return get_tv_number_chk(&argvars[0], NULL);
-}
-
// TODO(ZyX-I): move to eval/typval
/// Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c
index 6a16b3869a..38032762dc 100644
--- a/src/nvim/eval/typval.c
+++ b/src/nvim/eval/typval.c
@@ -2026,6 +2026,7 @@ bool tv_check_str_or_nr(const typval_T *const tv)
///
/// @return Line number or -1 or 0.
linenr_T tv_get_lnum(const typval_T *const tv)
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
linenr_T lnum = get_tv_number_chk(tv, NULL);
if (lnum == 0) { // No valid number, try using same function as line() does.
@@ -2046,6 +2047,7 @@ linenr_T tv_get_lnum(const typval_T *const tv)
///
/// @return Floating-point value of the variable or zero.
float_T tv_get_float(const typval_T *const tv)
+ FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT
{
switch (tv->v_type) {
case VAR_NUMBER: {