1
0
forked from aniani/vim

patch 8.2.0918: duplicate code for evaluating expression argument

Problem:    Duplicate code for evaluating expression argument.
Solution:   Merge the code and make the use more flexible.
This commit is contained in:
Bram Moolenaar
2020-06-07 14:50:50 +02:00
parent e928366de5
commit a9c0104947
7 changed files with 28 additions and 121 deletions

View File

@@ -211,6 +211,18 @@ eval1_emsg(char_u **arg, typval_T *rettv, int evaluate)
return ret; return ret;
} }
/*
* Return whether a typval is a valid expression to pass to eval_expr_typval()
* or eval_expr_to_bool(). An empty string returns FALSE;
*/
int
eval_expr_valid_arg(typval_T *tv)
{
return tv->v_type != VAR_UNKNOWN
&& (tv->v_type != VAR_STRING
|| (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
}
/* /*
* Evaluate an expression, which can be a function, partial or string. * Evaluate an expression, which can be a function, partial or string.
* Pass arguments "argv[argc]". * Pass arguments "argv[argc]".

View File

@@ -6399,11 +6399,9 @@ search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
int options = SEARCH_KEEP; int options = SEARCH_KEEP;
int subpatnum; int subpatnum;
searchit_arg_T sia; searchit_arg_T sia;
evalarg_T skip; int use_skip = FALSE;
pos_T firstpos; pos_T firstpos;
CLEAR_FIELD(skip);
pat = tv_get_string(&argvars[0]); pat = tv_get_string(&argvars[0]);
dir = get_search_arg(&argvars[1], flagsp); // may set p_ws dir = get_search_arg(&argvars[1], flagsp); // may set p_ws
if (dir == 0) if (dir == 0)
@@ -6429,9 +6427,7 @@ search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
if (time_limit < 0) if (time_limit < 0)
goto theend; goto theend;
#endif #endif
if (argvars[4].v_type != VAR_UNKNOWN use_skip = eval_expr_valid_arg(&argvars[4]);
&& evalarg_get(&argvars[4], &skip) == FAIL)
goto theend;
} }
} }
@@ -6471,19 +6467,20 @@ search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos)) if (firstpos.lnum != 0 && EQUAL_POS(pos, firstpos))
subpatnum = FAIL; subpatnum = FAIL;
if (subpatnum == FAIL || !evalarg_valid(&skip)) if (subpatnum == FAIL || !use_skip)
// didn't find it or no skip argument // didn't find it or no skip argument
break; break;
firstpos = pos; firstpos = pos;
// If the skip pattern matches, ignore this match. // If the skip expression matches, ignore this match.
{ {
int do_skip; int do_skip;
int err; int err;
pos_T save_pos = curwin->w_cursor; pos_T save_pos = curwin->w_cursor;
curwin->w_cursor = pos; curwin->w_cursor = pos;
do_skip = evalarg_call_bool(&skip, &err); err = FALSE;
do_skip = eval_expr_to_bool(&argvars[4], &err);
curwin->w_cursor = save_pos; curwin->w_cursor = save_pos;
if (err) if (err)
{ {
@@ -6523,7 +6520,6 @@ search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
curwin->w_set_curswant = TRUE; curwin->w_set_curswant = TRUE;
theend: theend:
p_ws = save_p_ws; p_ws = save_p_ws;
evalarg_clean(&skip);
return retval; return retval;
} }
@@ -6791,14 +6787,9 @@ searchpair_cmn(typval_T *argvars, pos_T *match_pos)
skip = NULL; skip = NULL;
else else
{ {
// Type is checked later.
skip = &argvars[4]; skip = &argvars[4];
if (skip->v_type != VAR_FUNC && skip->v_type != VAR_PARTIAL
&& skip->v_type != VAR_STRING)
{
// Type error
semsg(_(e_invarg2), tv_get_string(&argvars[4]));
goto theend;
}
if (argvars[5].v_type != VAR_UNKNOWN) if (argvars[5].v_type != VAR_UNKNOWN)
{ {
lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL); lnum_stop = (long)tv_get_number_chk(&argvars[5], NULL);
@@ -6922,12 +6913,7 @@ do_searchpair(
options |= SEARCH_START; options |= SEARCH_START;
if (skip != NULL) if (skip != NULL)
{ use_skip = eval_expr_valid_arg(skip);
// Empty string means to not use the skip expression.
if (skip->v_type == VAR_STRING || skip->v_type == VAR_FUNC)
use_skip = skip->vval.v_string != NULL
&& *skip->vval.v_string != NUL;
}
save_cursor = curwin->w_cursor; save_cursor = curwin->w_cursor;
pos = curwin->w_cursor; pos = curwin->w_cursor;

View File

@@ -3811,79 +3811,4 @@ free_callback(callback_T *callback)
callback->cb_name = NULL; callback->cb_name = NULL;
} }
/*
* Process a function argument that can be a string expression or a function
* reference.
* "tv" must remain valid until calling evalarg_clean()!
* Returns FAIL when the argument is invalid.
*/
int
evalarg_get(typval_T *tv, evalarg_T *eva)
{
if (tv->v_type == VAR_STRING
|| tv->v_type == VAR_NUMBER
|| tv->v_type == VAR_BOOL
|| tv->v_type == VAR_SPECIAL)
{
eva->eva_string = tv_get_string_buf(tv, eva->eva_buf);
return OK;
}
eva->eva_callback = get_callback(tv);
return eva->eva_callback.cb_name == NULL ? FAIL : OK;
}
/*
* Return whether "eva" has a valid expression or callback.
*/
int
evalarg_valid(evalarg_T *eva)
{
return eva->eva_string != NULL || eva->eva_callback.cb_name != NULL;
}
/*
* Invoke the expression or callback "eva" and return the result in "tv".
* Returns FAIL if something failed
*/
int
evalarg_call(evalarg_T *eva, typval_T *tv)
{
typval_T argv[1];
if (eva->eva_string != NULL)
return eval0(eva->eva_string, tv, NULL, EVAL_EVALUATE);
argv[0].v_type = VAR_UNKNOWN;
return call_callback(&eva->eva_callback, -1, tv, 0, argv);
}
/*
* Like evalarg_call(), but just return TRUE of FALSE.
* Sets "error" to TRUE if evaluation failed.
*/
int
evalarg_call_bool(evalarg_T *eva, int *error)
{
typval_T tv;
int r;
if (evalarg_call(eva, &tv) == FAIL)
{
*error = TRUE;
return FALSE;
}
r = tv_get_number(&tv);
clear_tv(&tv);
*error = FALSE;
return r;
}
void
evalarg_clean(evalarg_T *eva)
{
if (eva->eva_string == NULL)
free_callback(&eva->eva_callback);
}
#endif // FEAT_EVAL #endif // FEAT_EVAL

View File

@@ -4,6 +4,7 @@ varnumber_T num_modulus(varnumber_T n1, varnumber_T n2);
void eval_init(void); void eval_init(void);
void eval_clear(void); void eval_clear(void);
int eval_to_bool(char_u *arg, int *error, char_u **nextcmd, int skip); int eval_to_bool(char_u *arg, int *error, char_u **nextcmd, int skip);
int eval_expr_valid_arg(typval_T *tv);
int eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv); int eval_expr_typval(typval_T *expr, typval_T *argv, int argc, typval_T *rettv);
int eval_expr_to_bool(typval_T *expr, int *error); int eval_expr_to_bool(typval_T *expr, int *error);
char_u *eval_to_string_skip(char_u *arg, char_u **nextcmd, int skip); char_u *eval_to_string_skip(char_u *arg, char_u **nextcmd, int skip);
@@ -19,14 +20,14 @@ void *call_func_retlist(char_u *func, int argc, typval_T *argv);
int eval_foldexpr(char_u *arg, int *cp); int eval_foldexpr(char_u *arg, int *cp);
char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags); char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
void clear_lval(lval_T *lp); void clear_lval(lval_T *lp);
void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, int is_const, char_u *op); void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, int flags, char_u *op);
void *eval_for_line(char_u *arg, int *errp, char_u **nextcmdp, int skip); void *eval_for_line(char_u *arg, int *errp, char_u **nextcmdp, int skip);
int next_for_item(void *fi_void, char_u *arg); int next_for_item(void *fi_void, char_u *arg);
void free_for_info(void *fi_void); void free_for_info(void *fi_void);
void set_context_for_expression(expand_T *xp, char_u *arg, cmdidx_T cmdidx); void set_context_for_expression(expand_T *xp, char_u *arg, cmdidx_T cmdidx);
int pattern_match(char_u *pat, char_u *text, int ic); int pattern_match(char_u *pat, char_u *text, int ic);
int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate); int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int flags);
int eval1(char_u **arg, typval_T *rettv, int evaluate); int eval1(char_u **arg, typval_T *rettv, int flags);
void eval_addblob(typval_T *tv1, typval_T *tv2); void eval_addblob(typval_T *tv1, typval_T *tv2);
int eval_addlist(typval_T *tv1, typval_T *tv2); int eval_addlist(typval_T *tv1, typval_T *tv2);
char_u *partial_name(partial_T *pt); char_u *partial_name(partial_T *pt);
@@ -50,7 +51,7 @@ int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags); char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
int eval_isnamec(int c); int eval_isnamec(int c);
int eval_isnamec1(int c); int eval_isnamec1(int c);
int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose, char_u *start_leader, char_u **end_leaderp); int handle_subscript(char_u **arg, typval_T *rettv, int flags, int verbose, char_u *start_leader, char_u **end_leaderp);
int item_copy(typval_T *from, typval_T *to, int deep, int copyID); int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
void echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr); void echo_one(typval_T *rettv, int with_space, int *atstart, int *needclr);
void ex_echo(exarg_T *eap); void ex_echo(exarg_T *eap);

View File

@@ -88,9 +88,4 @@ callback_T get_callback(typval_T *arg);
void put_callback(callback_T *cb, typval_T *tv); void put_callback(callback_T *cb, typval_T *tv);
void set_callback(callback_T *dest, callback_T *src); void set_callback(callback_T *dest, callback_T *src);
void free_callback(callback_T *callback); void free_callback(callback_T *callback);
int evalarg_get(typval_T *tv, evalarg_T *eva);
int evalarg_valid(evalarg_T *eva);
int evalarg_call(evalarg_T *eva, typval_T *tv);
int evalarg_call_bool(evalarg_T *eva, int *error);
void evalarg_clean(evalarg_T *eva);
/* vim: set ft=c : */ /* vim: set ft=c : */

View File

@@ -4130,20 +4130,6 @@ typedef struct
int sa_wrapped; // search wrapped around int sa_wrapped; // search wrapped around
} searchit_arg_T; } searchit_arg_T;
/*
* Function argument that can be a string, funcref or partial.
* - declare: evalarg_T name;
* - init: CLEAR_FIELD(name);
* - set: evalarg_get(&argvars[3], &name);
* - use: if (evalarg_valid(&name)) res = evalarg_call(&name);
* - cleanup: evalarg_clean(&name);
*/
typedef struct
{
char_u eva_buf[NUMBUFLEN]; // buffer for get_tv_string_buf()
char_u *eva_string;
callback_T eva_callback;
} evalarg_T;
#define WRITEBUFSIZE 8192 // size of normal write buffer #define WRITEBUFSIZE 8192 // size of normal write buffer

View File

@@ -754,6 +754,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
918,
/**/ /**/
917, 917,
/**/ /**/