diff options
| author | Josh Rahm <joshuarahm@gmail.com> | 2022-08-03 00:08:17 -0600 |
|---|---|---|
| committer | Josh Rahm <joshuarahm@gmail.com> | 2022-08-03 00:08:17 -0600 |
| commit | 9449e1b8d273ff78eb894c588110ffa0c17d6ee3 (patch) | |
| tree | 9e4470c33bd4187d9f42f0b2c4aaa995310c5be8 /src/nvim/eval.c | |
| parent | 308e1940dcd64aa6c344c403d4f9e0dda58d9c5c (diff) | |
| parent | b8dcbcc732baf84fc48d6b272c3ade0bcb129b3b (diff) | |
| download | rneovim-9449e1b8d273ff78eb894c588110ffa0c17d6ee3.tar.gz rneovim-9449e1b8d273ff78eb894c588110ffa0c17d6ee3.tar.bz2 rneovim-9449e1b8d273ff78eb894c588110ffa0c17d6ee3.zip | |
Merge remote-tracking branch 'upstream/master' into rahm
Diffstat (limited to 'src/nvim/eval.c')
| -rw-r--r-- | src/nvim/eval.c | 1738 |
1 files changed, 42 insertions, 1696 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 16c3e72c5b..9cc92cb62e 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -27,6 +27,7 @@ #include "nvim/eval/gc.h" #include "nvim/eval/typval.h" #include "nvim/eval/userfunc.h" +#include "nvim/eval/vars.h" #include "nvim/ex_cmds2.h" #include "nvim/ex_getln.h" #include "nvim/ex_session.h" @@ -58,20 +59,13 @@ #define DICT_MAXNEST 100 // maximum nesting of lists and dicts -static char *e_letunexp = N_("E18: Unexpected characters in :let"); static char *e_missbrac = N_("E111: Missing ']'"); static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary"); -static char *e_illvar = N_("E461: Illegal variable name: %s"); -static char *e_cannot_mod = N_("E995: Cannot modify existing variable"); static char *e_nowhitespace = N_("E274: No white space allowed before parenthesis"); -static char *e_lock_unlock = N_("E940: Cannot lock or unlock variable %s"); static char *e_write2 = N_("E80: Error while writing: %s"); static char *e_string_list_or_blob_required = N_("E1098: String, List or Blob required"); -// TODO(ZyX-I): move to eval/executor -static char *e_letwrong = N_("E734: Wrong variable type for %s="); - static char * const namespace_char = "abglstvw"; /// Variable used for g: @@ -212,7 +206,7 @@ static struct vimvar { VV(VV_OLDFILES, "oldfiles", VAR_LIST, 0), VV(VV_WINDOWID, "windowid", VAR_NUMBER, VV_RO_SBX), VV(VV_PROGPATH, "progpath", VAR_STRING, VV_RO), - VV(VV_COMPLETED_ITEM, "completed_item", VAR_DICT, VV_RO), + VV(VV_COMPLETED_ITEM, "completed_item", VAR_DICT, 0), VV(VV_OPTION_NEW, "option_new", VAR_STRING, VV_RO), VV(VV_OPTION_OLD, "option_old", VAR_STRING, VV_RO), VV(VV_OPTION_OLDLOCAL, "option_oldlocal", VAR_STRING, VV_RO), @@ -967,6 +961,30 @@ typval_T *eval_expr(char *arg) return tv; } +/// 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); + } +} + +bool is_vimvarht(const hashtab_T *ht) +{ + return ht == &vimvarht; +} + +bool is_compatht(const 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. @@ -1251,723 +1269,6 @@ int eval_foldexpr(char *arg, int *cp) return (int)retval; } -/// ":cons[t] var = expr1" define constant -/// ":cons[t] [name1, name2, ...] = expr1" define constants unpacking list -/// ":cons[t] [name, ..., ; lastname] = expr" define constants unpacking list -void ex_const(exarg_T *eap) -{ - ex_let_const(eap, true); -} - -/// 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. -/// -/// @return a List with {lines} or NULL. -static list_T *heredoc_get(exarg_T *eap, char *cmd) -{ - char *marker; - char *p; - int marker_indent_len = 0; - int text_indent_len = 0; - char *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 || ascii_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 (ascii_iswhite(*p)) { - p++; - marker_indent_len++; - } - text_indent_len = -1; - } - - // The marker is the next word. - if (*cmd != NUL && *cmd != '"') { - marker = skipwhite(cmd); - p = (char *)skiptowhite((char_u *)marker); - if (*skipwhite(p) != NUL && *skipwhite(p) != '"') { - emsg(_(e_trailing)); - return NULL; - } - *p = NUL; - if (islower(*marker)) { - emsg(_("E221: Marker cannot start with lower case letter")); - return NULL; - } - } else { - emsg(_("E172: Missing marker")); - return NULL; - } - - list_T *l = tv_list_alloc(0); - for (;;) { - int mi = 0; - int ti = 0; - - char *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) { - xfree(theline); - break; - } - if (text_indent_len == -1 && *theline != NUL) { - // set the text indent from the first line. - p = theline; - text_indent_len = 0; - while (ascii_iswhite(*p)) { - p++; - text_indent_len++; - } - text_indent = xstrnsave(theline, (size_t)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; - } - } - } - - tv_list_append_string(l, theline + ti, -1); - xfree(theline); - } - xfree(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. -/// ":let [name, ..., ; lastname] = expr" unpack list. -void ex_let(exarg_T *eap) -{ - ex_let_const(eap, false); -} - -static void ex_let_const(exarg_T *eap, const bool is_const) -{ - char *arg = eap->arg; - char *expr = NULL; - typval_T rettv; - int i; - int var_count = 0; - int semicolon = 0; - char op[2]; - char *argend; - int first = true; - - argend = (char *)skip_var_list(arg, &var_count, &semicolon); - if (argend == NULL) { - return; - } - if (argend > arg && argend[-1] == '.') { // For var.='str'. - argend--; - } - expr = skipwhite(argend); - if (*expr != '=' && !((vim_strchr("+-*/%.", *expr) != NULL - && expr[1] == '=') || STRNCMP(expr, "..=", 3) == 0)) { - // ":let" without "=": list variables - if (*arg == '[') { - emsg(_(e_invarg)); - } else if (!ends_excmd(*arg)) { - // ":let var1 var2" - arg = (char *)list_arg_vars(eap, (const char *)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 = (char *)check_nextcmd((char_u *)arg); - } else if (expr[0] == '=' && expr[1] == '<' && expr[2] == '<') { - // HERE document - list_T *l = heredoc_get(eap, expr + 3); - if (l != NULL) { - tv_list_set_ret(&rettv, l); - if (!eap->skip) { - op[0] = '='; - op[1] = NUL; - (void)ex_let_vars(eap->arg, &rettv, false, semicolon, var_count, - is_const, (char *)op); - } - tv_clear(&rettv); - } - } else { - op[0] = '='; - op[1] = NUL; - if (*expr != '=') { - if (vim_strchr("+-*/%.", *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) { - tv_clear(&rettv); - } - emsg_skip--; - } else if (i != FAIL) { - (void)ex_let_vars(eap->arg, &rettv, false, semicolon, var_count, - is_const, (char *)op); - tv_clear(&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. -/// -/// @param copy copy values from "tv", don't move -/// @param semicolon from skip_var_list() -/// @param var_count from skip_var_list() -/// @param is_const lock variables for :const -/// -/// @return OK or FAIL; -static int ex_let_vars(char *arg_start, typval_T *tv, int copy, int semicolon, int var_count, - int is_const, char *op) -{ - char *arg = arg_start; - 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) { - emsg(_(e_listreq)); - return FAIL; - } - list_T *const l = tv->vval.v_list; - - const int len = tv_list_len(l); - if (semicolon == 0 && var_count < len) { - emsg(_("E687: Less targets than List items")); - return FAIL; - } - if (var_count - semicolon > len) { - emsg(_("E688: More targets than List items")); - return FAIL; - } - // List l may actually be NULL, but it should fail with E688 or even earlier - // if you try to do ":let [] = v:_null_list". - assert(l != NULL); - - listitem_T *item = tv_list_first(l); - size_t rest_len = (size_t)tv_list_len(l); - while (*arg != ']') { - arg = skipwhite(arg + 1); - arg = ex_let_one(arg, TV_LIST_ITEM_TV(item), true, is_const, ",;]", op); - if (arg == NULL) { - return FAIL; - } - rest_len--; - - item = TV_LIST_ITEM_NEXT(l, item); - arg = skipwhite(arg); - if (*arg == ';') { - /* Put the rest of the list (may be empty) in the var after ';'. - * Create a new list for this. */ - list_T *const rest_list = tv_list_alloc((ptrdiff_t)rest_len); - while (item != NULL) { - tv_list_append_tv(rest_list, TV_LIST_ITEM_TV(item)); - item = TV_LIST_ITEM_NEXT(l, item); - } - - ltv.v_type = VAR_LIST; - ltv.v_lock = VAR_UNLOCKED; - ltv.vval.v_list = rest_list; - tv_list_ref(rest_list); - - arg = ex_let_one(skipwhite(arg + 1), <v, false, is_const, "]", op); - tv_clear(<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 const char *skip_var_list(const char *arg, int *var_count, int *semicolon) -{ - const char *p; - const char *s; - - if (*arg == '[') { - // "[var, var]": find the matching ']'. - p = arg; - for (;;) { - p = skipwhite(p + 1); // skip whites after '[', ';' or ',' - s = skip_var_one((char *)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(_("E452: 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((char *)arg); - } -} - -/// Skip one (assignable) variable name, including @r, $VAR, &option, d.key, -/// l[idx]. -static const char *skip_var_one(const char *arg) -{ - if (*arg == '@' && arg[1] != NUL) { - return arg + 1 + utfc_ptr2len(arg + 1); - } - return (char *)find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg, - NULL, NULL, FNE_INCL_BR | FNE_CHECK_START); -} - -/// List variables for hashtab "ht" with prefix "prefix". -/// -/// @param empty if TRUE also list NULL strings as empty strings. -void list_hashtable_vars(hashtab_T *ht, const char *prefix, int empty, int *first) -{ - hashitem_T *hi; - dictitem_T *di; - int todo; - - todo = (int)ht->ht_used; - for (hi = ht->ht_array; todo > 0 && !got_int; ++hi) { - if (!HASHITEM_EMPTY(hi)) { - todo--; - di = TV_DICT_HI2DI(hi); - char buf[IOSIZE]; - - // apply :filter /pat/ to variable name - xstrlcpy(buf, prefix, IOSIZE); - xstrlcat(buf, (char *)di->di_key, IOSIZE); - if (message_filtered((char_u *)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 const char *list_arg_vars(exarg_T *eap, const char *arg, int *first) -{ - int error = FALSE; - int len; - const char *name; - const char *name_start; - 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 (!ascii_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; - char *tofree; - 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) - const char *const 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 *const s = encode_tv2echo(&tv, NULL); - const char *const used_name = (arg == arg_subsc - ? name - : name_start); - const ptrdiff_t name_size = (used_name == tofree - ? (ptrdiff_t)strlen(used_name) - : (arg - used_name)); - list_one_var_a("", used_name, name_size, - tv.v_type, s == NULL ? "" : s, first); - xfree(s); - } - tv_clear(&tv); - } - } - } - - xfree(tofree); - } - - arg = (const char *)skipwhite(arg); - } - - return arg; -} - -// TODO(ZyX-I): move to eval/ex_cmds - -/// Set one item of `:let var = expr` or `:let [v1, v2] = list` to its value -/// -/// @param[in] arg Start of the variable name. -/// @param[in] tv Value to assign to the variable. -/// @param[in] copy If true, copy value from `tv`. -/// @param[in] endchars Valid characters after variable name or NULL. -/// @param[in] op Operation performed: *op is `+`, `-`, `.` for `+=`, etc. -/// NULL for `=`. -/// -/// @return a pointer to the char just after the var name or NULL in case of -/// error. -static char *ex_let_one(char *arg, typval_T *const tv, const bool copy, const bool is_const, - const char *const endchars, const char *const op) - FUNC_ATTR_NONNULL_ARG(1, 2) FUNC_ATTR_WARN_UNUSED_RESULT -{ - char *arg_end = NULL; - int len; - int opt_flags; - char *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++; - char *name = arg; - len = get_env_len((const char **)&arg); - if (len == 0) { - semsg(_(e_invarg2), name - 1); - } else { - if (op != NULL && vim_strchr("+-*/%", *op) != NULL) { - semsg(_(e_letwrong), op); - } else if (endchars != NULL - && vim_strchr(endchars, *skipwhite(arg)) == NULL) { - emsg(_(e_letunexp)); - } else if (!check_secure()) { - const char c1 = name[len]; - name[len] = NUL; - const char *p = tv_get_string_chk(tv); - if (p != NULL && op != NULL && *op == '.') { - char *s = vim_getenv(name); - - if (s != NULL) { - tofree = (char *)concat_str((const char_u *)s, (const char_u *)p); - p = (const char *)tofree; - xfree(s); - } - } - if (p != NULL) { - os_setenv(name, p, 1); - 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; - xfree(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. - char *const p = (char *)find_option_end((const char **)&arg, &opt_flags); - if (p == NULL - || (endchars != NULL - && vim_strchr(endchars, *skipwhite(p)) == NULL)) { - emsg(_(e_letunexp)); - } else { - int opt_type; - long numval; - char *stringval = NULL; - const char *s = NULL; - - const char c1 = *p; - *p = NUL; - - varnumber_T n = tv_get_number(tv); - if (tv->v_type != VAR_BOOL && tv->v_type != VAR_SPECIAL) { - 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 = num_divide(numval, n); break; - case '%': - n = num_modulus(numval, n); break; - } - } else if (opt_type == 0 && stringval != NULL) { // string - char *const oldstringval = stringval; - stringval = (char *)concat_str((const char_u *)stringval, - (const char_u *)s); - xfree(oldstringval); - s = stringval; - } - } - } - if (s != NULL || tv->v_type == VAR_BOOL - || tv->v_type == VAR_SPECIAL) { - set_option_value((const char *)arg, n, s, opt_flags); - arg_end = p; - } - *p = c1; - xfree(stringval); - } - // ":let @r = expr": Set register contents. - } else if (*arg == '@') { - if (is_const) { - emsg(_("E996: Cannot lock a register")); - return NULL; - } - arg++; - - int regname = utf_ptr2char(arg); - int mblen = utf_ptr2len(arg); - - if (op != NULL && vim_strchr("+-*/%", *op) != NULL) { - semsg(_(e_letwrong), op); - } else if (endchars != NULL - && vim_strchr(endchars, *skipwhite(arg + mblen)) == NULL) { - emsg(_(e_letunexp)); - } else { - char *s; - - char *ptofree = NULL; - const char *p = tv_get_string_chk(tv); - if (p != NULL && op != NULL && *op == '.') { - s = get_reg_contents(regname == '@' ? '"' : regname, kGRegExprSrc); - if (s != NULL) { - ptofree = (char *)concat_str((char_u *)s, (const char_u *)p); - p = (const char *)ptofree; - xfree(s); - } - } - if (p != NULL) { - - write_reg_contents(regname == '@' ? '"' : regname, - (const char_u *)p, (ssize_t)STRLEN(p), false); - arg_end = arg + mblen; - } - xfree(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; - - char *const 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; -} - // TODO(ZyX-I): move to eval/executor /// Get an lvalue @@ -2357,8 +1658,8 @@ void clear_lval(lval_T *lp) /// @param endp points to just after the parsed name. /// @param op NULL, "+" for "+=", "-" for "-=", "*" for "*=", "/" for "/=", /// "%" for "%=", "." for ".=" or "=" for "=". -static void set_var_lval(lval_T *lp, char *endp, typval_T *rettv, int copy, const bool is_const, - const char *op) +void set_var_lval(lval_T *lp, char *endp, typval_T *rettv, int copy, const bool is_const, + const char *op) { int cc; listitem_T *ri; @@ -2810,327 +2111,6 @@ void set_context_for_expression(expand_T *xp, char *arg, cmdidx_T cmdidx) xp->xp_pattern = arg; } -/// ":unlet[!] var1 ... " command. -void ex_unlet(exarg_T *eap) -{ - ex_unletlock(eap, eap->arg, 0, do_unlet_var); -} - -// TODO(ZyX-I): move to eval/ex_cmds - -/// ":lockvar" and ":unlockvar" commands -void ex_lockvar(exarg_T *eap) -{ - char *arg = eap->arg; - int deep = 2; - - if (eap->forceit) { - deep = -1; - } else if (ascii_isdigit(*arg)) { - deep = getdigits_int(&arg, false, -1); - arg = skipwhite(arg); - } - - ex_unletlock(eap, arg, deep, do_lock_var); -} - -// TODO(ZyX-I): move to eval/ex_cmds - -/// Common parsing logic for :unlet, :lockvar and :unlockvar. -/// -/// Invokes `callback` afterwards if successful and `eap->skip == false`. -/// -/// @param[in] eap Ex command arguments for the command. -/// @param[in] argstart Start of the string argument for the command. -/// @param[in] deep Levels to (un)lock for :(un)lockvar, -1 to (un)lock -/// everything. -/// @param[in] callback Appropriate handler for the command. -static void ex_unletlock(exarg_T *eap, char *argstart, int deep, ex_unletlock_callback callback) - FUNC_ATTR_NONNULL_ALL -{ - char *arg = argstart; - char *name_end; - bool error = false; - lval_T lv; - - do { - if (*arg == '$') { - lv.ll_name = (const char *)arg; - lv.ll_tv = NULL; - arg++; - if (get_env_len((const char **)&arg) == 0) { - semsg(_(e_invarg2), arg - 1); - return; - } - if (!error && !eap->skip && callback(&lv, arg, eap, deep) == FAIL) { - error = true; - } - name_end = arg; - } else { - // 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 - || (!ascii_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 && callback(&lv, name_end, eap, deep) == FAIL) { - error = true; - } - - if (!eap->skip) { - clear_lval(&lv); - } - } - arg = skipwhite(name_end); - } while (!ends_excmd(*arg)); - - eap->nextcmd = (char *)check_nextcmd((char_u *)arg); -} - -// TODO(ZyX-I): move to eval/ex_cmds - -/// Unlet a variable indicated by `lp`. -/// -/// @param[in] lp The lvalue. -/// @param[in] name_end End of the string argument for the command. -/// @param[in] eap Ex command arguments for :unlet. -/// @param[in] deep Unused. -/// -/// @return OK on success, or FAIL on failure. -static int do_unlet_var(lval_T *lp, char *name_end, exarg_T *eap, int deep FUNC_ATTR_UNUSED) - FUNC_ATTR_NONNULL_ALL -{ - int forceit = eap->forceit; - int ret = OK; - int cc; - - if (lp->ll_tv == NULL) { - cc = (char_u)(*name_end); - *name_end = NUL; - - // Environment variable, normal name or expanded name. - if (*lp->ll_name == '$') { - os_unsetenv(lp->ll_name + 1); - } else if (do_unlet(lp->ll_name, lp->ll_name_len, forceit) == FAIL) { - ret = FAIL; - } - *name_end = (char)cc; - } else if ((lp->ll_list != NULL - // ll_list is not NULL when lvalue is not in a list, NULL lists - // yield E689. - && var_check_lock(tv_list_locked(lp->ll_list), - lp->ll_name, - lp->ll_name_len)) - || (lp->ll_dict != NULL - && var_check_lock(lp->ll_dict->dv_lock, - lp->ll_name, - lp->ll_name_len))) { - return FAIL; - } else if (lp->ll_range) { - assert(lp->ll_list != NULL); - // Delete a range of List items. - listitem_T *const first_li = lp->ll_li; - listitem_T *last_li = first_li; - for (;;) { - listitem_T *const li = TV_LIST_ITEM_NEXT(lp->ll_list, lp->ll_li); - if (var_check_lock(TV_LIST_ITEM_TV(lp->ll_li)->v_lock, - lp->ll_name, - lp->ll_name_len)) { - return false; - } - lp->ll_li = li; - lp->ll_n1++; - if (lp->ll_li == NULL || (!lp->ll_empty2 && lp->ll_n2 < lp->ll_n1)) { - break; - } else { - last_li = lp->ll_li; - } - } - tv_list_remove_items(lp->ll_list, first_li, last_li); - } else { - if (lp->ll_list != NULL) { - // unlet a List item. - tv_list_item_remove(lp->ll_list, lp->ll_li); - } else { - // unlet a Dictionary item. - dict_T *d = lp->ll_dict; - assert(d != NULL); - dictitem_T *di = lp->ll_di; - bool watched = tv_dict_is_watched(d); - char *key = NULL; - typval_T oldtv; - - if (watched) { - tv_copy(&di->di_tv, &oldtv); - // need to save key because dictitem_remove will free it - key = xstrdup((char *)di->di_key); - } - - tv_dict_item_remove(d, di); - - if (watched) { - tv_dict_watcher_notify(d, key, NULL, &oldtv); - tv_clear(&oldtv); - xfree(key); - } - } - } - - return ret; -} - -// TODO(ZyX-I): move to eval/ex_cmds - -/// unlet a variable -/// -/// @param[in] name Variable name to unlet. -/// @param[in] name_len Variable name length. -/// @param[in] forceit If true, do not complain if variable doesn’t exist. -/// -/// @return OK if it existed, FAIL otherwise. -int do_unlet(const char *const name, const size_t name_len, const bool forceit) - FUNC_ATTR_NONNULL_ALL -{ - const char *varname; - dict_T *dict; - hashtab_T *ht = find_var_ht_dict(name, name_len, &varname, &dict); - - if (ht != NULL && *varname != NUL) { - dict_T *d = get_current_funccal_dict(ht); - if (d == NULL) { - if (ht == &globvarht) { - d = &globvardict; - } else if (ht == &compat_hashtab) { - d = &vimvardict; - } else { - dictitem_T *const di = find_var_in_ht(ht, *name, "", 0, false); - d = di->di_tv.vval.v_dict; - } - if (d == NULL) { - internal_error("do_unlet()"); - return FAIL; - } - } - - hashitem_T *hi = hash_find(ht, varname); - if (HASHITEM_EMPTY(hi)) { - hi = find_hi_in_scoped_ht(name, &ht); - } - if (hi != NULL && !HASHITEM_EMPTY(hi)) { - dictitem_T *const di = TV_DICT_HI2DI(hi); - if (var_check_fixed(di->di_flags, name, TV_CSTRING) - || var_check_ro(di->di_flags, name, TV_CSTRING) - || var_check_lock(d->dv_lock, name, TV_CSTRING)) { - return FAIL; - } - - if (var_check_lock(d->dv_lock, name, TV_CSTRING)) { - return FAIL; - } - - typval_T oldtv; - bool watched = tv_dict_is_watched(dict); - - if (watched) { - tv_copy(&di->di_tv, &oldtv); - } - - delete_var(ht, hi); - - if (watched) { - tv_dict_watcher_notify(dict, varname, NULL, &oldtv); - tv_clear(&oldtv); - } - return OK; - } - } - if (forceit) { - return OK; - } - semsg(_("E108: No such variable: \"%s\""), name); - return FAIL; -} - -// TODO(ZyX-I): move to eval/ex_cmds - -/// Lock or unlock variable indicated by `lp`. |