Problem: Evaluating "expr" options has more overhead than needed.
Solution: Use call_simple_func() for 'foldtext', 'includeexpr', 'printexpr',
"expr" of 'spellsuggest', 'diffexpr', 'patchexpr', 'balloonexpr',
'formatexpr', 'indentexpr' and 'charconvert'.
'diffopt' option. 'diffexpr' cannot change the value of 'lines' and
'columns'.
+The advantage of using a function call without arguments is that it is faster,
+see |expr-option-function|.
+
Example (this does almost the same as 'diffexpr' being empty): >
set diffexpr=MyDiff()
v:fname_diff patch file
v:fname_out resulting patched file
+The advantage of using a function call without arguments is that it is faster,
+see |expr-option-function|.
+
Example (this does the same as 'patchexpr' being empty): >
set patchexpr=MyPatch()
Note that v:charconvert_from and v:charconvert_to may be different
from 'encoding'. Vim internally uses UTF-8 instead of UCS-2 or UCS-4.
+ The advantage of using a function call without arguments is that it is
+ faster, see |expr-option-function|.
+
Encryption is not done by Vim when using 'charconvert'. If you want
to encrypt the file after conversion, 'charconvert' should take care
of this.
< This will invoke the mylang#Format() function in the
autoload/mylang.vim file in 'runtimepath'. |autoload|
+ The advantage of using a function call without arguments is that it is
+ faster, see |expr-option-function|.
+
The expression is also evaluated when 'textwidth' is set and adding
text beyond that limit. This happens under the same conditions as
when internal formatting is used. Make sure the cursor is kept in the
If the expression starts with s: or |<SID>|, then it is replaced with
the script ID (|local-function|). Example: >
- set includeexpr=s:MyIncludeExpr(v:fname)
- set includeexpr=<SID>SomeIncludeExpr(v:fname)
+ set includeexpr=s:MyIncludeExpr()
+ set includeexpr=<SID>SomeIncludeExpr()
< Otherwise, the expression is evaluated in the context of the script
where the option was set, thus script-local items are available.
+ It is more efficient if the value is just a function call without
+ arguments, see |expr-option-function|.
+
The expression will be evaluated in the |sandbox| when set from a
modeline, see |sandbox-option|.
This option cannot be set in a modeline when 'modelineexpr' is off.
< Otherwise, the expression is evaluated in the context of the script
where the option was set, thus script-local items are available.
+ The advantage of using a function call without arguments is that it is
+ faster, see |expr-option-function|.
+
The expression must return the number of spaces worth of indent. It
can return "-1" to keep the current indent (this means 'autoindent' is
used for the indent).
The file is used for all languages.
expr:{expr} Evaluate expression {expr}. Use a function to avoid
- trouble with spaces. |v:val| holds the badly spelled
- word. The expression must evaluate to a List of
- Lists, each with a suggestion and a score.
+ trouble with spaces. Best is to call a function
+ without arguments, see |expr-option-function|.
+ |v:val| holds the badly spelled word. The expression
+ must evaluate to a List of Lists, each with a
+ suggestion and a score.
Example:
[['the', 33], ['that', 44]] ~
Set 'verbose' and use |z=| to see the scores that the
If you change this option, using a function is an easy way to avoid having to
escape all the spaces. Example: >
- :set printexpr=PrintFile(v:fname_in)
- :function PrintFile(fname)
- : call system("ghostview " .. a:fname)
- : call delete(a:fname)
+ :set printexpr=PrintFile()
+ :function PrintFile()
+ : call system("ghostview " .. v:fname_in)
+ : call delete(v:fname_in)
: return v:shell_error
:endfunc
+It is more efficient if the option is set to just a function call,
+see |expr-option-function|.
+
Be aware that some print programs return control before they have read the
file. If you delete the file too soon it will not be printed. These programs
usually offer an option to have them remove the file when printing is done.
current_sctx = curbuf->b_p_script_ctx[BV_BEXPR];
vim_free(result);
- result = eval_to_string(bexpr, TRUE);
+ result = eval_to_string(bexpr, TRUE, TRUE);
// Remove one trailing newline, it is added when the result was a
// list and it's hardly ever useful. If the user really wants a
tv.vval.v_number = wp->w_id;
set_var((char_u *)"g:statusline_winid", &tv, FALSE);
- usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE);
+ usefmt = eval_to_string_safe(fmt + 2, use_sandbox, FALSE, FALSE);
if (usefmt == NULL)
usefmt = fmt;
if (curwin != save_curwin)
VIsual_active = FALSE;
- str = eval_to_string_safe(p, use_sandbox, FALSE);
+ str = eval_to_string_safe(p, use_sandbox, FALSE, FALSE);
curwin = save_curwin;
curbuf = save_curbuf;
// to be typed. Do generate errors so that try/catch works.
++emsg_silent;
- res = eval_to_string(expr, TRUE);
+ res = eval_to_string(expr, TRUE, FALSE);
debug_break_level = save_dbl;
redir_off = save_ro;
char_u *arg,
int *error,
exarg_T *eap,
- int skip) // only parse, don't execute
+ int skip, // only parse, don't execute
+ int use_simple_function)
{
typval_T tv;
varnumber_T retval = FALSE;
evalarg_T evalarg;
+ int r;
fill_evalarg_from_eap(&evalarg, eap, skip);
if (skip)
++emsg_skip;
- if (eval0(arg, &tv, eap, &evalarg) == FAIL)
+ if (use_simple_function)
+ r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
+ else
+ r = eval0(arg, &tv, eap, &evalarg);
+ if (r == FAIL)
*error = TRUE;
else
{
eval_to_string_eap(
char_u *arg,
int convert,
- exarg_T *eap)
+ exarg_T *eap,
+ int use_simple_function)
{
typval_T tv;
char_u *retval;
evalarg_T evalarg;
+ int r;
fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
- if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
+ if (use_simple_function)
+ r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
+ else
+ r = eval0(arg, &tv, NULL, &evalarg);
+ if (r == FAIL)
retval = NULL;
else
{
char_u *
eval_to_string(
char_u *arg,
- int convert)
+ int convert,
+ int use_simple_function)
{
- return eval_to_string_eap(arg, convert, NULL);
+ return eval_to_string_eap(arg, convert, NULL, use_simple_function);
}
/*
eval_to_string_safe(
char_u *arg,
int use_sandbox,
- int keep_script_version)
+ int keep_script_version,
+ int use_simple_function)
{
char_u *retval;
funccal_entry_T funccal_entry;
++sandbox;
++textlock;
may_garbage_collect = FALSE;
- retval = eval_to_string(arg, FALSE);
+ retval = eval_to_string(arg, FALSE, use_simple_function);
if (use_sandbox)
--sandbox;
--textlock;
* Returns -1 for an error.
*/
varnumber_T
-eval_to_number(char_u *expr)
+eval_to_number(char_u *expr, int use_simple_function)
{
typval_T rettv;
varnumber_T retval;
char_u *p = skipwhite(expr);
+ int r = NOTDONE;
++emsg_off;
- if (eval1(&p, &rettv, &EVALARG_EVALUATE) == FAIL)
+ if (use_simple_function)
+ r = may_call_simple_func(expr, &rettv);
+ if (r == NOTDONE)
+ r = eval1(&p, &rettv, &EVALARG_EVALUATE);
+ if (r == FAIL)
retval = -1;
else
{
*/
typval_T *
eval_expr(char_u *arg, exarg_T *eap)
+{
+ return eval_expr_ext(arg, eap, FALSE);
+}
+
+ typval_T *
+eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
{
typval_T *tv;
evalarg_T evalarg;
fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
tv = ALLOC_ONE(typval_T);
- if (tv != NULL && eval0(arg, tv, eap, &evalarg) == FAIL)
- VIM_CLEAR(tv);
+ if (tv != NULL)
+ {
+ int r = NOTDONE;
+
+ if (use_simple_function)
+ r = eval0_simple_funccal(arg, tv, eap, &evalarg);
+ if (r == NOTDONE)
+ r = eval0(arg, tv, eap, &evalarg);
+
+ if (r == FAIL)
+ VIM_CLEAR(tv);
+ }
clear_evalarg(&evalarg, eap);
return tv;
{
char_u *arg;
typval_T tv;
- int r = NOTDONE;
varnumber_T retval;
char_u *s;
sctx_T saved_sctx = current_sctx;
int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
- OPT_LOCAL);
+ OPT_LOCAL);
arg = skipwhite(wp->w_p_fde);
current_sctx = wp->w_p_script_ctx[WV_FDE];
++textlock;
*cp = NUL;
- // If the expression is "FuncName()" then we can skip a lot of overhead.
- char_u *parens = (char_u *)strstr((char *)arg, "()");
- if (parens != NULL && *skipwhite(parens + 2) == NUL)
- {
- char_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
-
- if (to_name_end(p, TRUE) == parens)
- r = call_simple_func(arg, (int)(parens - arg), &tv);
- }
-
- if (r == NOTDONE)
- r = eval0(arg, &tv, NULL, &EVALARG_EVALUATE);
-
- if (r == FAIL)
+ // Evaluate the expression. If the expression is "FuncName()" call the
+ // function directly.
+ if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
retval = 0;
else
{
}
/*
- * The "evaluate" argument: When FALSE, the argument is only parsed but not
- * executed. The function may return OK, but the rettv will be of type
- * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
+ * The "eval" functions have an "evalarg" argument: When NULL or
+ * "evalarg->eval_flags" does not have EVAL_EVALUATE, then the argument is only
+ * parsed but not executed. The functions may return OK, but the rettv will be
+ * of type VAR_UNKNOWN. The functions still returns FAIL for a syntax error.
*/
/*
return eval0_retarg(arg, rettv, eap, evalarg, NULL);
}
+/*
+ * If "arg" is a simple function call without arguments then call it and return
+ * the result. Otherwise return NOTDONE.
+ */
+ int
+may_call_simple_func(
+ char_u *arg,
+ typval_T *rettv)
+{
+ char_u *parens = (char_u *)strstr((char *)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_u *p = STRNCMP(arg, "<SNR>", 5) == 0 ? skipdigits(arg + 5) : arg;
+
+ if (to_name_end(p, TRUE) == parens)
+ r = call_simple_func(arg, (int)(parens - arg), rettv);
+ }
+ return r;
+}
+
+/*
+ * Handle zero level expression with optimization for a simple function call.
+ * Same arguments and return value as eval0().
+ */
+ int
+eval0_simple_funccal(
+ char_u *arg,
+ typval_T *rettv,
+ exarg_T *eap,
+ evalarg_T *evalarg)
+{
+ int r = may_call_simple_func(arg, rettv);
+
+ if (r == NOTDONE)
+ r = eval0_retarg(arg, rettv, eap, evalarg, NULL);
+ return r;
+}
+
/*
* Like eval0() but when "retarg" is not NULL store the pointer to after the
* expression and don't check what comes after the expression.
c1 = *in_end;
*in_end = NUL;
- temp_result = eval_to_string(expr_start + 1, FALSE);
+ temp_result = eval_to_string(expr_start + 1, FALSE, FALSE);
if (temp_result != NULL)
{
retval = alloc(STRLEN(temp_result) + (expr_start - in_start)
if (ctx != NULL)
current_sctx = *ctx;
- if (eval_to_bool(p_ccv, &err, NULL, FALSE))
+ if (eval_to_bool(p_ccv, &err, NULL, FALSE, TRUE))
err = TRUE;
set_vim_var_string(VV_CC_FROM, NULL, -1);
if (ctx != NULL)
current_sctx = *ctx;
- if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
+ if (eval_to_bool(p_pexpr, &err, NULL, FALSE, TRUE))
err = TRUE;
set_vim_var_string(VV_FNAME_IN, NULL, -1);
current_sctx = *ctx;
// errors are ignored
- tv = eval_expr(p_dex, NULL);
+ tv = eval_expr_ext(p_dex, NULL, TRUE);
free_tv(tv);
set_vim_var_string(VV_FNAME_IN, NULL, -1);
current_sctx = *ctx;
// errors are ignored
- tv = eval_expr(p_pex, NULL);
+ tv = eval_expr_ext(p_pex, NULL, TRUE);
free_tv(tv);
set_vim_var_string(VV_FNAME_IN, NULL, -1);
char_u *p = skipwhite(expr);
sctx_T saved_sctx = current_sctx;
sctx_T *ctx;
+ int r;
// Set "v:val" to the bad word.
prepare_vimvar(VV_VAL, &save_val);
if (ctx != NULL)
current_sctx = *ctx;
- if (eval1(&p, &rettv, &EVALARG_EVALUATE) == OK)
+ r = may_call_simple_func(p, &rettv);
+ if (r == NOTDONE)
+ r = eval1(&p, &rettv, &EVALARG_EVALUATE);
+ if (r == OK)
{
if (rettv.v_type != VAR_LIST)
clear_tv(&rettv);
if (evaluate)
{
*block_end = NUL;
- expr_val = eval_to_string(block_start, TRUE);
+ expr_val = eval_to_string(block_start, TRUE, FALSE);
*block_end = '}';
if (expr_val == NULL)
return NULL;
if (expr != NULL)
{
++emsg_off;
- p = eval_to_string(expr, FALSE);
+ p = eval_to_string(expr, FALSE, FALSE);
--emsg_off;
vim_free(expr);
}
skip = did_emsg || got_int || did_throw || (cstack->cs_idx > 0
&& !(cstack->cs_flags[cstack->cs_idx - 1] & CSF_ACTIVE));
- result = eval_to_bool(eap->arg, &error, eap, skip);
+ result = eval_to_bool(eap->arg, &error, eap, skip, FALSE);
if (!skip && !error)
{
if (skip && ends_excmd(*eap->arg))
semsg(_(e_invalid_expression_str), eap->arg);
else
- result = eval_to_bool(eap->arg, &error, eap, skip);
+ result = eval_to_bool(eap->arg, &error, eap, skip, FALSE);
// When throwing error exceptions, we want to throw always the first
// of several errors in a row. This is what actually happens when
/*
* ":while bool-expr"
*/
- result = eval_to_bool(eap->arg, &error, eap, skip);
+ result = eval_to_bool(eap->arg, &error, eap, skip, FALSE);
}
else
{
#ifdef FEAT_EVAL
if (*cmd == '=') // `={expr}`: Expand expression
- buffer = eval_to_string(cmd + 1, TRUE);
+ buffer = eval_to_string(cmd + 1, TRUE, FALSE);
else
#endif
buffer = get_cmd_output(cmd, NULL,
current_sctx = curbuf->b_p_script_ctx[BV_INEX];
res = eval_to_string_safe(curbuf->b_p_inex,
- was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL), TRUE);
+ was_set_insecurely((char_u *)"includeexpr", OPT_LOCAL),
+ TRUE, TRUE);
set_vim_var_string(VV_FNAME, NULL, 0);
current_sctx = save_sctx;
++emsg_off; // handle exceptions, but don't display errors
text = eval_to_string_safe(wp->w_p_fdt,
- was_set_insecurely((char_u *)"foldtext", OPT_LOCAL), TRUE);
+ was_set_insecurely((char_u *)"foldtext", OPT_LOCAL),
+ TRUE, TRUE);
--emsg_off;
if (text == NULL || did_emsg)
/* Evaluate the expression */
++emsg_skip;
- str = (char *)eval_to_string((char_u *)buffer, TRUE);
+ str = (char *)eval_to_string((char_u *)buffer, TRUE, FALSE);
--emsg_skip;
vim_free(buffer);
if (str == NULL)
char_u *
eval_to_string(
char_u *arg UNUSED,
- int dolist UNUSED)
+ int convert UNUSED,
+ int use_simple_function UNUSED)
{
return NULL;
}
PREINIT:
char_u *value;
PPCODE:
- value = eval_to_string((char_u *)str, TRUE);
+ value = eval_to_string((char_u *)str, TRUE, FALSE);
if (value == NULL)
{
XPUSHs(sv_2mortal(newSViv(0)));
#ifdef FEAT_EVAL
expr = Tcl_GetStringFromObj(objv[objn], NULL);
- str = (char *)eval_to_string((char_u *)expr, TRUE);
+ str = (char *)eval_to_string((char_u *)expr, TRUE, FALSE);
if (str == NULL)
Tcl_SetResult(interp, _("invalid expression"), TCL_STATIC);
else
}
// Note: the evaluation may make "mp" invalid.
- p = eval_to_string(expr, FALSE);
+ p = eval_to_string(expr, FALSE, FALSE);
--textlock;
--ex_normal_lock;
void eval_init(void);
void eval_clear(void);
void fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip);
-int eval_to_bool(char_u *arg, int *error, exarg_T *eap, int skip);
+int eval_to_bool(char_u *arg, int *error, exarg_T *eap, int skip, int use_simple_function);
int eval_expr_valid_arg(typval_T *tv);
funccall_T *eval_expr_get_funccal(typval_T *expr, typval_T *rettv);
int eval_expr_typval(typval_T *expr, typval_T *argv, int argc, funccall_T *fc_arg, typval_T *rettv);
int skip_expr(char_u **pp, evalarg_T *evalarg);
int skip_expr_concatenate(char_u **arg, char_u **start, char_u **end, evalarg_T *evalarg);
char_u *typval2string(typval_T *tv, int convert);
-char_u *eval_to_string_eap(char_u *arg, int convert, exarg_T *eap);
-char_u *eval_to_string(char_u *arg, int convert);
-char_u *eval_to_string_safe(char_u *arg, int use_sandbox, int keep_script_version);
-varnumber_T eval_to_number(char_u *expr);
+char_u *eval_to_string_eap(char_u *arg, int convert, exarg_T *eap, int use_simple_function);
+char_u *eval_to_string(char_u *arg, int convert, int use_simple_function);
+char_u *eval_to_string_safe(char_u *arg, int use_sandbox, int keep_script_version, int use_simple_function);
+varnumber_T eval_to_number(char_u *expr, int use_simple_function);
typval_T *eval_expr(char_u *arg, exarg_T *eap);
+typval_T *eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function);
int call_vim_function(char_u *func, int argc, typval_T *argv, typval_T *rettv);
void *call_func_retstr(char_u *func, int argc, typval_T *argv);
void *call_func_retlist(char_u *func, int argc, typval_T *argv);
char_u *eval_next_line(char_u *arg, evalarg_T *evalarg);
char_u *skipwhite_and_linebreak(char_u *arg, evalarg_T *evalarg);
int eval0(char_u *arg, typval_T *rettv, exarg_T *eap, evalarg_T *evalarg);
+int may_call_simple_func(char_u *arg, typval_T *rettv);
+int eval0_simple_funccal(char_u *arg, typval_T *rettv, exarg_T *eap, evalarg_T *evalarg);
int eval0_retarg(char_u *arg, typval_T *rettv, exarg_T *eap, evalarg_T *evalarg, char_u **retarg);
int eval1(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
void eval_addblob(typval_T *tv1, typval_T *tv2);
// Execute instructions from ISN_SUBSTITUTE.
eval_result[nested] = exe_substitute_instr();
else
- eval_result[nested] = eval_to_string(source + 2, TRUE);
+ eval_result[nested] = eval_to_string(source + 2, TRUE, FALSE);
--nesting;
if (eval_result[nested] != NULL)
return expr_copy;
++nested;
- rv = eval_to_string_eap(expr_copy, TRUE, expr_eap);
+ rv = eval_to_string_eap(expr_copy, TRUE, expr_eap, FALSE);
--nested;
vim_free(expr_copy);
return rv;
curwin = wp;
STRCPY(buf, "b:keymap_name"); // must be writable
++emsg_skip;
- s = p = eval_to_string(buf, FALSE);
+ s = p = eval_to_string(buf, FALSE, FALSE);
--emsg_skip;
curbuf = old_curbuf;
curwin = old_curwin;
* Clear the screen.
* May delay if there is something the user should read.
* Allocated the screen for resizing if needed.
- * Returns TRUE when the screen was actually claared, FALSE if all display
+ * Returns TRUE when the screen was actually cleared, FALSE if all display
* cells were marked for updating.
*/
int
// If runtime/filetype.vim wasn't loaded yet, the scripts will be
// found when it loads.
- if (cmd != NULL && eval_to_number(cmd) > 0)
+ if (cmd != NULL && eval_to_number(cmd, FALSE) > 0)
{
do_cmdline_cmd((char_u *)"augroup filetypedetect");
vim_snprintf((char *)pat, len, ftpat, ffname);
// Evaluate the function.
if (use_sandbox)
++sandbox;
- r = (int)eval_to_number(fex);
+ r = (int)eval_to_number(fex, TRUE);
if (use_sandbox)
--sandbox;
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 634,
/**/
633,
/**/