diff options
author | Bram Moolenaar <Bram@vim.org> | 2019-08-27 22:48:30 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2019-08-27 22:48:30 +0200 |
commit | 0522ba0359c96a8c2a4fc8fca0d3b58e49dda759 (patch) | |
tree | be800b3f0d6f992a9fc8332f72eb6b3361c93a4c /src/eval.c | |
parent | d20070274c47668560e02db184e1f8e456c3c326 (diff) |
patch 8.1.1933: the eval.c file is too bigv8.1.1933
Problem: The eval.c file is too big.
Solution: Move code related to variables to evalvars.c. (Yegappan
Lakshmanan, closes #4868)
Diffstat (limited to 'src/eval.c')
-rw-r--r-- | src/eval.c | 1907 |
1 files changed, 38 insertions, 1869 deletions
diff --git a/src/eval.c b/src/eval.c index 9a2642cbe8..7795f41dbf 100644 --- a/src/eval.c +++ b/src/eval.c @@ -20,15 +20,8 @@ # include <float.h> #endif -#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */ - -static char *e_letunexp = N_("E18: Unexpected characters in :let"); -static char *e_undefvar = N_("E121: Undefined variable: %s"); static char *e_missbrac = N_("E111: Missing ']'"); static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary"); -static char *e_letwrong = N_("E734: Wrong variable type for %s="); -static char *e_illvar = N_("E461: Illegal variable name: %s"); -static char *e_cannot_mod = N_("E995: Cannot modify existing variable"); #ifdef FEAT_FLOAT static char *e_float_as_string = N_("E806: using Float as a String"); #endif @@ -217,25 +210,7 @@ static struct vimvar static dictitem_T vimvars_var; /* variable used for v: */ #define vimvarht vimvardict.dv_hashtab -static void ex_let_const(exarg_T *eap, int is_const); -static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, int is_const, char_u *nextchars); -static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon); -static char_u *skip_var_one(char_u *arg); -static void list_glob_vars(int *first); -static void list_buf_vars(int *first); -static void list_win_vars(int *first); -static void list_tab_vars(int *first); -static void list_vim_vars(int *first); -static void list_script_vars(int *first); -static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first); -static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, int is_const, char_u *endchars, char_u *op); -static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, int is_const, char_u *op); static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op); -static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep); -static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit); -static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock); -static void item_lock(typval_T *tv, int deep, int lock); - static int eval2(char_u **arg, typval_T *rettv, int evaluate); static int eval3(char_u **arg, typval_T *rettv, int evaluate); static int eval4(char_u **arg, typval_T *rettv, int evaluate); @@ -248,18 +223,10 @@ static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate); static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate); static int free_unref_items(int copyID); static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate); -static int get_env_len(char_u **arg); -static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose); static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end); -static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload); static void check_vars(char_u *name, int len); static typval_T *alloc_string_tv(char_u *string); -static void delete_var(hashtab_T *ht, hashitem_T *hi); -static void list_one_var(dictitem_T *v, char *prefix, int *first); -static void list_one_var_a(char *prefix, char_u *name, int type, char_u *string, int *first); -static void set_var_const(char_u *name, typval_T *tv, int copy, int is_const); static int tv_check_lock(typval_T *tv, char_u *name, int use_gettext); -static char_u *find_option_end(char_u **arg, int *opt_flags); /* for VIM_VERSION_ defines */ #include "version.h" @@ -267,7 +234,7 @@ static char_u *find_option_end(char_u **arg, int *opt_flags); /* * Return "n1" divided by "n2", taking care of dividing by zero. */ - static varnumber_T + varnumber_T num_divide(varnumber_T n1, varnumber_T n2) { varnumber_T result; @@ -290,7 +257,7 @@ num_divide(varnumber_T n1, varnumber_T n2) /* * Return "n1" modulus "n2", taking care of dividing by zero. */ - static varnumber_T + varnumber_T num_modulus(varnumber_T n1, varnumber_T n2) { // Give an error when n2 is 0? @@ -978,6 +945,38 @@ eval_to_number(char_u *expr) } /* + * List Vim variables. + */ + void +list_vim_vars(int *first) +{ + list_hashtable_vars(&vimvarht, "v:", FALSE, first); +} + +/* + * List script-local variables, if there is a script. + */ + void +list_script_vars(int *first) +{ + if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len) + list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid), + "s:", FALSE, first); +} + + int +is_vimvarht(hashtab_T *ht) +{ + return ht == &vimvarht; +} + + int +is_compatht(hashtab_T *ht) +{ + return ht == &compat_hashtab; +} + +/* * Prepare v: variable "idx" to be used. * Save the current typeval in "save_tv". * When not used yet add the variable to the v: hashtable. @@ -1237,849 +1236,6 @@ eval_foldexpr(char_u *arg, int *cp) #endif /* - * Get a list of lines from a HERE document. The here document is a list of - * lines surrounded by a marker. - * cmd << {marker} - * {line1} - * {line2} - * .... - * {marker} - * - * The {marker} is a string. If the optional 'trim' word is supplied before the - * marker, then the leading indentation before the lines (matching the - * indentation in the 'cmd' line) is stripped. - * Returns a List with {lines} or NULL. - */ - static list_T * -heredoc_get(exarg_T *eap, char_u *cmd) -{ - char_u *theline; - char_u *marker; - list_T *l; - char_u *p; - int marker_indent_len = 0; - int text_indent_len = 0; - char_u *text_indent = NULL; - - if (eap->getline == NULL) - { - emsg(_("E991: cannot use =<< here")); - return NULL; - } - - // Check for the optional 'trim' word before the marker - cmd = skipwhite(cmd); - if (STRNCMP(cmd, "trim", 4) == 0 && (cmd[4] == NUL || VIM_ISWHITE(cmd[4]))) - { - cmd = skipwhite(cmd + 4); - - // Trim the indentation from all the lines in the here document. - // The amount of indentation trimmed is the same as the indentation of - // the first line after the :let command line. To find the end marker - // the indent of the :let command line is trimmed. - p = *eap->cmdlinep; - while (VIM_ISWHITE(*p)) - { - p++; - marker_indent_len++; - } - text_indent_len = -1; - } - - // The marker is the next word. - if (*cmd != NUL && *cmd != '"') - { - marker = skipwhite(cmd); - p = skiptowhite(marker); - if (*skipwhite(p) != NUL && *skipwhite(p) != '"') - { - emsg(_(e_trailing)); - return NULL; - } - *p = NUL; - if (vim_islower(*marker)) - { - emsg(_("E221: Marker cannot start with lower case letter")); - return NULL; - } - } - else - { - emsg(_("E172: Missing marker")); - return NULL; - } - - l = list_alloc(); - if (l == NULL) - return NULL; - - for (;;) - { - int mi = 0; - int ti = 0; - - theline = eap->getline(NUL, eap->cookie, 0, FALSE); - if (theline == NULL) - { - semsg(_("E990: Missing end marker '%s'"), marker); - break; - } - - // with "trim": skip the indent matching the :let line to find the - // marker - if (marker_indent_len > 0 - && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0) - mi = marker_indent_len; - if (STRCMP(marker, theline + mi) == 0) - { - vim_free(theline); - break; - } - - if (text_indent_len == -1 && *theline != NUL) - { - // set the text indent from the first line. - p = theline; - text_indent_len = 0; - while (VIM_ISWHITE(*p)) - { - p++; - text_indent_len++; - } - text_indent = vim_strnsave(theline, text_indent_len); - } - // with "trim": skip the indent matching the first line - if (text_indent != NULL) - for (ti = 0; ti < text_indent_len; ++ti) - if (theline[ti] != text_indent[ti]) - break; - - if (list_append_string(l, theline + ti, -1) == FAIL) - break; - vim_free(theline); - } - vim_free(text_indent); - - return l; -} - -/* - * ":let" list all variable values - * ":let var1 var2" list variable values - * ":let var = expr" assignment command. - * ":let var += expr" assignment command. - * ":let var -= expr" assignment command. - * ":let var *= expr" assignment command. - * ":let var /= expr" assignment command. - * ":let var %= expr" assignment command. - * ":let var .= expr" assignment command. - * ":let var ..= expr" assignment command. - * ":let [var1, var2] = expr" unpack list. - */ - void -ex_let(exarg_T *eap) -{ - ex_let_const(eap, FALSE); -} - -/* - * ":const" list all variable values - * ":const var1 var2" list variable values - * ":const var = expr" assignment command. - * ":const [var1, var2] = expr" unpack list. - */ - void -ex_const(exarg_T *eap) -{ - ex_let_const(eap, TRUE); -} - - static void -ex_let_const(exarg_T *eap, int is_const) -{ - char_u *arg = eap->arg; - char_u *expr = NULL; - typval_T rettv; - int i; - int var_count = 0; - int semicolon = 0; - char_u op[2]; - char_u *argend; - int first = TRUE; - int concat; - - argend = skip_var_list(arg, &var_count, &semicolon); - if (argend == NULL) - return; - if (argend > arg && argend[-1] == '.') // for var.='str' - --argend; - expr = skipwhite(argend); - concat = expr[0] == '.' - && ((expr[1] == '=' && current_sctx.sc_version < 2) - || (expr[1] == '.' && expr[2] == '=')); - if (*expr != '=' && !((vim_strchr((char_u *)"+-*/%", *expr) != NULL - && expr[1] == '=') || concat)) - { - /* - * ":let" without "=": list variables - */ - if (*arg == '[') - emsg(_(e_invarg)); - else if (expr[0] == '.') - emsg(_("E985: .= is not supported with script version 2")); - else if (!ends_excmd(*arg)) - /* ":let var1 var2" */ - arg = list_arg_vars(eap, arg, &first); - else if (!eap->skip) - { - /* ":let" */ - list_glob_vars(&first); - list_buf_vars(&first); - list_win_vars(&first); - list_tab_vars(&first); - list_script_vars(&first); - list_func_vars(&first); - list_vim_vars(&first); - } - eap->nextcmd = check_nextcmd(arg); - } - else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<') - { - list_T *l; - - // HERE document - l = heredoc_get(eap, expr + 3); - if (l != NULL) - { - rettv_list_set(&rettv, l); - op[0] = '='; - op[1] = NUL; - (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count, - is_const, op); - clear_tv(&rettv); - } - } - else - { - op[0] = '='; - op[1] = NUL; - if (*expr != '=') - { - if (vim_strchr((char_u *)"+-*/%.", *expr) != NULL) - { - op[0] = *expr; // +=, -=, *=, /=, %= or .= - if (expr[0] == '.' && expr[1] == '.') // ..= - ++expr; - } - expr = skipwhite(expr + 2); - } - else - expr = skipwhite(expr + 1); - - if (eap->skip) - ++emsg_skip; - i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip); - if (eap->skip) - { - if (i != FAIL) - clear_tv(&rettv); - --emsg_skip; - } - else if (i != FAIL) - { - (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count, - is_const, op); - clear_tv(&rettv); - } - } -} - -/* - * Assign the typevalue "tv" to the variable or variables at "arg_start". - * Handles both "var" with any type and "[var, var; var]" with a list type. - * When "op" is not NULL it points to a string with characters that - * must appear after the variable(s). Use "+", "-" or "." for add, subtract - * or concatenate. - * Returns OK or FAIL; - */ - static int -ex_let_vars( - char_u *arg_start, - typval_T *tv, - int copy, // copy values from "tv", don't move - int semicolon, // from skip_var_list() - int var_count, // from skip_var_list() - int is_const, // lock variables for const - char_u *op) -{ - char_u *arg = arg_start; - list_T *l; - int i; - listitem_T *item; - typval_T ltv; - - if (*arg != '[') - { - /* - * ":let var = expr" or ":for var in list" - */ - if (ex_let_one(arg, tv, copy, is_const, op, op) == NULL) - return FAIL; - return OK; - } - - /* - * ":let [v1, v2] = list" or ":for [v1, v2] in listlist" - */ - if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL) - { - emsg(_(e_listreq)); - return FAIL; - } - - i = list_len(l); - if (semicolon == 0 && var_count < i) - { - emsg(_("E687: Less targets than List items")); - return FAIL; - } - if (var_count - semicolon > i) - { - emsg(_("E688: More targets than List items")); - return FAIL; - } - - item = l->lv_first; - while (*arg != ']') - { - arg = skipwhite(arg + 1); - arg = ex_let_one(arg, &item->li_tv, TRUE, is_const, - (char_u *)",;]", op); - item = item->li_next; - if (arg == NULL) - return FAIL; - - arg = skipwhite(arg); - if (*arg == ';') - { - /* Put the rest of the list (may be empty) in the var after ';'. - * Create a new list for this. */ - l = list_alloc(); - if (l == NULL) - return FAIL; - while (item != NULL) - { - list_append_tv(l, &item->li_tv); - item = item->li_next; - } - - ltv.v_type = VAR_LIST; - ltv.v_lock = 0; - ltv.vval.v_list = l; - l->lv_refcount = 1; - - arg = ex_let_one(skipwhite(arg + 1), <v, FALSE, is_const, - (char_u *)"]", op); - clear_tv(<v); - if (arg == NULL) - return FAIL; - break; - } - else if (*arg != ',' && *arg != ']') - { - internal_error("ex_let_vars()"); - return FAIL; - } - } - - return OK; -} - -/* - * Skip over assignable variable "var" or list of variables "[var, var]". - * Used for ":let varvar = expr" and ":for varvar in expr". - * For "[var, var]" increment "*var_count" for each variable. - * for "[var, var; var]" set "semicolon". - * Return NULL for an error. - */ - static char_u * -skip_var_list( - char_u *arg, - int *var_count, - int *semicolon) -{ - char_u *p, *s; - - if (*arg == '[') - { - /* "[var, var]": find the matching ']'. */ - p = arg; - for (;;) - { - p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */ - s = skip_var_one(p); - if (s == p) - { - semsg(_(e_invarg2), p); - return NULL; - } - ++*var_count; - - p = skipwhite(s); - if (*p == ']') - break; - else if (*p == ';') - { - if (*semicolon == 1) - { - emsg(_("Double ; in list of variables")); - return NULL; - } - *semicolon = 1; - } - else if (*p != ',') - { - semsg(_(e_invarg2), p); - return NULL; - } - } - return p + 1; - } - else - return skip_var_one(arg); -} - -/* - * Skip one (assignable) variable name, including @r, $VAR, &option, d.key, - * l[idx]. - */ - static char_u * -skip_var_one(char_u *arg) -{ - if (*arg == '@' && arg[1] != NUL) - return arg + 2; - return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg, - NULL, NULL, FNE_INCL_BR | FNE_CHECK_START); -} - -/* - * List variables for hashtab "ht" with prefix "prefix". - * If "empty" is TRUE also list NULL strings as empty strings. - */ - void -list_hashtable_vars( - hashtab_T *ht, - char *prefix, - int empty, - int *first) -{ - hashitem_T *hi; - dictitem_T *di; - int todo; - char_u buf[IOSIZE]; - - todo = (int)ht->ht_used; - for (hi = ht->ht_array; todo > 0 && !got_int; ++hi) - { - if (!HASHITEM_EMPTY(hi)) - { - --todo; - di = HI2DI(hi); - - // apply :filter /pat/ to variable name - vim_strncpy((char_u *)buf, (char_u *)prefix, IOSIZE - 1); - vim_strcat((char_u *)buf, di->di_key, IOSIZE); - if (message_filtered(buf)) - continue; - - if (empty || di->di_tv.v_type != VAR_STRING - || di->di_tv.vval.v_string != NULL) - list_one_var(di, prefix, first); - } - } -} - -/* - * List global variables. - */ - static void -list_glob_vars(int *first) -{ - list_hashtable_vars(&globvarht, "", TRUE, first); -} - -/* - * List buffer variables. - */ - static void -list_buf_vars(int *first) -{ - list_hashtable_vars(&curbuf->b_vars->dv_hashtab, "b:", TRUE, first); -} - -/* - * List window variables. - */ - static void -list_win_vars(int *first) -{ - list_hashtable_vars(&curwin->w_vars->dv_hashtab, "w:", TRUE, first); -} - -/* - * List tab page variables. - */ - static void -list_tab_vars(int *first) -{ - list_hashtable_vars(&curtab->tp_vars->dv_hashtab, "t:", TRUE, first); -} - -/* - * List Vim variables. - */ - static void -list_vim_vars(int *first) -{ - list_hashtable_vars(&vimvarht, "v:", FALSE, first); -} - -/* - * List script-local variables, if there is a script. - */ - static void -list_script_vars(int *first) -{ - if (current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len) - list_hashtable_vars(&SCRIPT_VARS(current_sctx.sc_sid), - "s:", FALSE, first); -} - -/* - * List variables in "arg". - */ - static char_u * -list_arg_vars(exarg_T *eap, char_u *arg, int *first) -{ - int error = FALSE; - int len; - char_u *name; - char_u *name_start; - char_u *arg_subsc; - char_u *tofree; - typval_T tv; - - while (!ends_excmd(*arg) && !got_int) - { - if (error || eap->skip) - { - arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START); - if (!VIM_ISWHITE(*arg) && !ends_excmd(*arg)) - { - emsg_severe = TRUE; - emsg(_(e_trailing)); - break; - } - } - else - { - /* get_name_len() takes care of expanding curly braces */ - name_start = name = arg; - len = get_name_len(&arg, &tofree, TRUE, TRUE); - if (len <= 0) - { - /* This is mainly to keep test 49 working: when expanding - * curly braces fails overrule the exception error message. */ - if (len < 0 && !aborting()) - { - emsg_severe = TRUE; - semsg(_(e_invarg2), arg); - break; - } - error = TRUE; - } - else - { - if (tofree != NULL) - name = tofree; - if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL) - error = TRUE; - else - { - /* handle d.key, l[idx], f(expr) */ - arg_subsc = arg; - if (handle_subscript(&arg, &tv, TRUE, TRUE, - name, &name) == FAIL) - error = TRUE; - else - { - if (arg == arg_subsc && len == 2 && name[1] == ':') - { - switch (*name) - { - case 'g': list_glob_vars(first); break; - case 'b': list_buf_vars(first); break; - case 'w': list_win_vars(first); break; - case 't': list_tab_vars(first); break; - case 'v': list_vim_vars(first); break; - case 's': list_script_vars(first); break; - case 'l': list_func_vars(first); break; - default: - semsg(_("E738: Can't list variables for %s"), name); - } - } - else - { - char_u numbuf[NUMBUFLEN]; - char_u *tf; - int c; - char_u *s; - - s = echo_string(&tv, &tf, numbuf, 0); - c = *arg; - *arg = NUL; - list_one_var_a("", - arg == arg_subsc ? name : name_start, - tv.v_type, - s == NULL ? (char_u *)"" : s, - first); - *arg = c; - vim_free(tf); - } - clear_tv(&tv); - } - } - } - - vim_free(tofree); - } - - arg = skipwhite(arg); - } - - return arg; -} - -/* - * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value. - * Returns a pointer to the char just after the var name. - * Returns NULL if there is an error. - */ - static char_u * -ex_let_one( - char_u *arg, // points to variable name - typval_T *tv, // value to assign to variable - int copy, // copy value from "tv" - int is_const, // lock variable for const - char_u *endchars, // valid chars after variable name or NULL - char_u *op) // "+", "-", "." or NULL -{ - int c1; - char_u *name; - char_u *p; - char_u *arg_end = NULL; - int len; - int opt_flags; - char_u *tofree = NULL; - - /* - * ":let $VAR = expr": Set environment variable. - */ - if (*arg == '$') - { - if (is_const) - { - emsg(_("E996: Cannot lock an environment variable")); - return NULL; - } - /* Find the end of the name. */ - ++arg; - name = arg; - len = get_env_len(&arg); - if (len == 0) - semsg(_(e_invarg2), name - 1); - else - { - if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL) - semsg(_(e_letwrong), op); - else if (endchars != NULL - && vim_strchr(endchars, *skipwhite(arg)) == NULL) - emsg(_(e_letunexp)); - else if (!check_secure()) - { - c1 = name[len]; - name[len] = NUL; - p = tv_get_string_chk(tv); - if (p != NULL && op != NULL && *op == '.') - { - int mustfree = FALSE; - char_u *s = vim_getenv(name, &mustfree); - - if (s != NULL) - { - p = tofree = concat_str(s, p); - if (mustfree) - vim_free(s); - } - } - if (p != NULL) - { - vim_setenv(name, p); - if (STRICMP(name, "HOME") == 0) - init_homedir(); - else if (didset_vim && STRICMP(name, "VIM") == 0) - didset_vim = FALSE; - else if (didset_vimruntime - && STRICMP(name, "VIMRUNTIME") == 0) - didset_vimruntime = FALSE; - arg_end = arg; - } - name[len] = c1; - vim_free(tofree); - } - } - } - - /* - * ":let &option = expr": Set option value. - * ":let &l:option = expr": Set local option value. - * ":let &g:option = expr": Set global option value. - */ - else if (*arg == '&') - { - if (is_const) - { - emsg(_("E996: Cannot lock an option")); - return NULL; - } - /* Find the end of the name. */ - p = find_option_end(&arg, &opt_flags); - if (p == NULL || (endchars != NULL - && vim_strchr(endchars, *skipwhite(p)) == NULL)) - emsg(_(e_letunexp)); - else - { - long n; - int opt_type; - long numval; - char_u *stringval = NULL; - char_u *s; - - c1 = *p; - *p = NUL; - - n = (long)tv_get_number(tv); - s = tv_get_string_chk(tv); /* != NULL if number or string */ - if (s != NULL && op != NULL && *op != '=') - { - opt_type = get_option_value(arg, &numval, - &stringval, opt_flags); - if ((opt_type == 1 && *op == '.') - || (opt_type == 0 && *op != '.')) - { - semsg(_(e_letwrong), op); - s = NULL; // don't set the value - } - else - { - if (opt_type == 1) // number - { - switch (*op) - { - case '+': n = numval + n; break; - case '-': n = numval - n; break; - case '*': n = numval * n; break; - case '/': n = (long)num_divide(numval, n); break; - case '%': n = (long)num_modulus(numval, n); break; - } - } - else if (opt_type == 0 && stringval != NULL) // string - { - s = concat_str(stringval, s); - vim_free(stringval); - stringval = s; - } - } - } - if (s != NULL) - { - set_option_value(arg, n, s, opt_flags); - arg_end = p; - } - *p = c1; - vim_free(stringval); - } - } - - /* - * ":let @r = expr": Set register contents. - */ - else if (*arg == '@') - { - if (is_const) - { - emsg(_("E996: Cannot lock a register")); - return NULL; - } - ++arg; - if (op != NULL && vim_strchr((char_u *)"+-*/%", *op) != NULL) - semsg(_(e_letwrong), op); - else if (endchars != NULL - && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL) - emsg(_(e_letunexp)); - else - { - char_u *ptofree = NULL; - char_u *s; - - p = tv_get_string_chk(tv); - if (p != NULL && op != NULL && *op == '.') - { - s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC); - if (s != NULL) - { - p = ptofree = concat_str(s, p); - vim_free(s); - } - } - if (p != NULL) - { - write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE); - arg_end = arg + 1; - } - vim_free(ptofree); - } - } - - /* - * ":let var = expr": Set internal variable. - * ":let {expr} = expr": Idem, name made with curly braces - */ - else if (eval_isnamec1(*arg) || *arg == '{') - { - lval_T lv; - - p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START); - if (p != NULL && lv.ll_name != NULL) - { - if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL) - emsg(_(e_letunexp)); - else - { - set_var_lval(&lv, p, tv, copy, is_const, op); - arg_end = p; - } - } - clear_lval(&lv); - } - - else - semsg(_(e_invarg2), arg); - - return arg_end; -} - -/* * Get an lval: variable, Dict item or List item that can be assigned a value * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]", * "name.key", "name.key[expr]" etc. @@ -2502,7 +1658,7 @@ clear_lval(lval_T *lp) * "op" is NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=", * "%" for "%=", "." for ".=" or "=" for "=". */ - static void + void set_var_lval( lval_T *lp, char_u *endp, @@ -3105,378 +2261,6 @@ set_context_for_expression( xp->xp_pattern = arg; } -/* - * ":unlet[!] var1 ... " command. - */ - void -ex_unlet(exarg_T *eap) -{ - ex_unletlock(eap, eap->arg, 0); -} - -/* - * ":lockvar" and ":unlockvar" commands - */ - void -ex_lockvar(exarg_T *eap) -{ - char_u *arg = eap->arg; - int deep = 2; - - if (eap->forceit) - deep = -1; - else if (vim_isdigit(*arg)) - { - deep = getdigits(&arg); - arg = skipwhite(arg); - } - - ex_unletlock(eap, arg, deep); -} - -/* - * ":unlet", ":lockvar" and ":unlockvar" are quite similar. - */ - static void -ex_unletlock( - exarg_T *eap, - char_u *argstart, - int deep) -{ - char_u *arg = argstart; - char_u *name_end; - int error = FALSE; - lval_T lv; - - do - { - if (*arg == '$') - { - char_u *name = ++arg; - - if (get_env_len(&arg) == 0) - { - semsg(_(e_invarg2), name - 1); - return; - } - vim_unsetenv(name); - arg = skipwhite(arg); - continue; - } - - /* Parse the name and find the end. */ - name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0, - FNE_CHECK_START); - if (lv.ll_name == NULL) - error = TRUE; /* error but continue parsing */ - if (name_end == NULL || (!VIM_ISWHITE(*name_end) - && !ends_excmd(*name_end))) - { - if (name_end != NULL) - { - emsg_severe = TRUE; - emsg(_(e_trailing)); - } - if (!(eap->skip || error)) - clear_lval(&lv); - break; - } - - if (!error && !eap->skip) - { - if (eap->cmdidx == CMD_unlet) - { - if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL) - error = TRUE; - } - else - { - if (do_lock_var(&lv, name_end, deep, - eap->cmdidx == CMD_lockvar) == FAIL) - error = TRUE; - } - } - - if (!eap->skip) - clear_lval(&lv); - - arg = skipwhite(name_end); - } while (!ends_excmd(*arg)); - - eap->nextcmd = check_nextcmd(arg); -} - - static int -do_unlet_var( - lval_T *lp, - char_u *name_end, - int forceit) -{ - int ret = OK; - int cc; - - if (lp->ll_tv == NULL) - { - cc = *name_end; - *name_end = NUL; - - /* Normal name or expanded name. */ - if (do_unlet(lp->ll_name, forceit) == FAIL) - ret = FAIL; - *name_end = cc; - } - else if ((lp->ll_list != NULL - && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE)) - || (lp->ll_dict != NULL - && var_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE))) - return FAIL; - else if (lp->ll_range) - { - listitem_T *li; - listitem_T *ll_li = lp->ll_li; - int ll_n1 = lp->ll_n1; - - while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1)) - { - li = ll_li->li_next; - if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE)) - return FAIL; - ll_li = li; - ++ll_n1; - } - - /* Delete a range of List items. */ - while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1)) - { - li = lp->ll_li->li_next; - listitem_remove(lp->ll_list, lp->ll_li); - lp->ll_li = li; - ++lp->ll_n1; - } - } - else - { - if (lp->ll_list != NULL) - /* unlet a List item. */ - listitem_remove(lp->ll_list, lp->ll_li); - else - /* unlet a Dictionary item. */ - dictitem_remove(lp->ll_dict, lp->ll_di); - } - - return ret; -} - -/* - * "unlet" a variable. Return OK if it existed, FAIL if not. - * When "forceit" is TRUE don't complain if the variable doesn't exist. - */ - int -do_unlet(char_u *name, int forceit) -{ - hashtab_T *ht; - hashitem_T *hi; - char_u *varname; - dict_T *d; - dictitem_T *di; - - ht = find_var_ht(name, &varname); - if (ht != NULL && *varname != NUL) - { - d = get_current_funccal_dict(ht); - if (d == NULL) - { - if (ht == &globvarht) - d = &globvardict; - else if (ht == &compat_hashtab) - d = &vimvardict; - else - { - di = find_var_in_ht(ht, *name, (char_u *)"", FALSE); - d = di == NULL ? NULL : di->di_tv.vval.v_dict; - } - if (d == NULL) - { - internal_error("do_unlet()"); - return FAIL; - } - } - hi = hash_find(ht, varname); - if (HASHITEM_EMPTY(hi)) - hi = find_hi_in_scoped_ht(name, &ht); - if (hi != NULL && !HASHITEM_EMPTY(hi)) - { - di = HI2DI(hi); - if (var_check_fixed(di->di_flags, name, FALSE) - || var_check_ro(di->di_flags, name, FALSE) - || var_check_lock(d->dv_lock, name, FALSE)) - return FAIL; - - delete_var(ht, hi); - return OK; - } - } - if (forceit) - return OK; - semsg(_("E108: No such variable: \"%s\""), name); - return FAIL; -} - -/* - * Lock or unlock variable indicated by "lp". - * "deep" is the levels to go (-1 for unlimited); - * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar". - */ - static int -do_lock_var( - lval_T *lp, - char_u *name_end, - int deep, - int lock) -{ - int ret = OK; - int cc; - dictitem_T *di; - - if (deep == 0) /* nothing to do */ - return OK; - - if (lp->ll_tv == NULL) - { - cc = *name_end; - *name_end = NUL; - - /* Normal name or expanded name. */ - di = find_var(lp->ll_name, NULL, TRUE); - if (di == NULL) - ret = FAIL; - else if ((di->di_flags & DI_FLAGS_FIX) - && di->di_tv.v_type != VAR_DICT - && di->di_tv.v_type != VAR_LIST) - /* For historic reasons this error is not given for a list or dict. - * E.g., the b: dict could be locked/unlocked. */ - semsg(_("E940: Cannot lock or unlock variable %s"), lp->ll_name); - else - { - if (lock) - di->di_flags |= DI_FLAGS_LOCK; - else - di->di_flags &= ~DI_FLAGS_LOCK; - item_lock(&di->di_tv, deep, lock); - } - *name_end = cc; - } - else if (lp->ll_range) - { - listitem_T *li = lp->ll_li; - - /* (un)lock a range of List items. */ |