diff options
-rw-r--r-- | runtime/doc/options.txt | 9 | ||||
-rw-r--r-- | src/nvim/eval.c | 21 | ||||
-rw-r--r-- | src/nvim/eval/userfunc.c | 9 |
3 files changed, 31 insertions, 8 deletions
diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt index b4eb3b6f28..bc2a8ae263 100644 --- a/runtime/doc/options.txt +++ b/runtime/doc/options.txt @@ -406,7 +406,14 @@ The value of a few options, such as 'foldexpr', is an expression that is evaluated to get a value. The evaluation can have quite a bit of overhead. One way to minimize the overhead, and also to keep the option value very simple, is to define a function and set the option to call it without -arguments. +arguments. A |v:lua-call| can also be used. Example: >vim + lua << EOF + function _G.MyFoldFunc() + -- ... compute fold level for line v:lnum + return level + end + EOF + set foldexpr=v:lua.MyFoldFunc() Setting the filetype diff --git a/src/nvim/eval.c b/src/nvim/eval.c index c70c36993b..b0f7aefc7b 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -1466,14 +1466,14 @@ Object eval_foldtext(win_T *wp) /// When "use_namespace" is true recognize "b:", "s:", etc. /// Return a pointer to just after the name. Equal to "arg" if there is no /// valid name. -static char *to_name_end(char *arg, bool use_namespace) +static const char *to_name_end(const char *arg, bool use_namespace) { // Quick check for valid starting character. if (!eval_isnamec1(*arg)) { return arg; } - char *p; + const char *p; for (p = arg + 1; *p != NUL && eval_isnamec(*p); MB_PTR_ADV(p)) { // Include a namespace such as "s:var" and "v:var". But "n:" is not // and can be used in slice "[n:]". @@ -2621,16 +2621,23 @@ int eval0(char *arg, typval_T *rettv, exarg_T *eap, evalarg_T *const evalarg) /// If "arg" is a simple function call without arguments then call it and return /// the result. Otherwise return NOTDONE. -static int may_call_simple_func(char *arg, typval_T *rettv) +static int may_call_simple_func(const char *arg, typval_T *rettv) { - char *parens = strstr(arg, "()"); + const char *parens = strstr(arg, "()"); int r = NOTDONE; // If the expression is "FuncName()" then we can skip a lot of overhead. if (parens != NULL && *skipwhite(parens + 2) == NUL) { - char *p = strncmp(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg; - if (to_name_end(p, true) == parens) { - r = call_simple_func(arg, (size_t)(parens - arg), rettv); + if (strnequal(arg, "v:lua.", 6)) { + const char *p = arg + 6; + if (skip_luafunc_name(p) == parens) { + r = call_simple_luafunc(p, (size_t)(parens - p), rettv); + } + } else { + const char *p = strncmp(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg; + if (to_name_end(p, true) == parens) { + r = call_simple_func(arg, (size_t)(parens - arg), rettv); + } } } return r; diff --git a/src/nvim/eval/userfunc.c b/src/nvim/eval/userfunc.c index 3690ab5d7b..f7ce5334f0 100644 --- a/src/nvim/eval/userfunc.c +++ b/src/nvim/eval/userfunc.c @@ -1776,6 +1776,15 @@ theend: return ret; } +int call_simple_luafunc(const char *funcname, size_t len, typval_T *rettv) + FUNC_ATTR_NONNULL_ALL +{ + typval_T argvars[1]; + argvars[0].v_type = VAR_UNKNOWN; + nlua_typval_call(funcname, len, argvars, 0, rettv); + return OK; +} + /// Call a function without arguments, partial or dict. /// This is like call_func() when the call is only "FuncName()". /// To be used by "expr" options. |