]> granicus.if.org Git - vim/commitdiff
patch 8.1.1933: the eval.c file is too big v8.1.1933
authorBram Moolenaar <Bram@vim.org>
Tue, 27 Aug 2019 20:48:30 +0000 (22:48 +0200)
committerBram Moolenaar <Bram@vim.org>
Tue, 27 Aug 2019 20:48:30 +0000 (22:48 +0200)
Problem:    The eval.c file is too big.
Solution:   Move code related to variables to evalvars.c. (Yegappan
            Lakshmanan, closes #4868)

16 files changed:
Filelist
src/Make_cyg_ming.mak
src/Make_morph.mak
src/Make_mvc.mak
src/Make_vms.mms
src/Makefile
src/README.md
src/eval.c
src/evalfunc.c
src/evalvars.c [new file with mode: 0644]
src/globals.h
src/proto.h
src/proto/eval.pro
src/proto/evalvars.pro [new file with mode: 0644]
src/version.c
src/vim.h

index 3ab547aa249bc7965a3ae05c61c4e5b9e4577910..3ec1acf1d52207e457c5da2768dbea734e9405f0 100644 (file)
--- a/Filelist
+++ b/Filelist
@@ -35,6 +35,7 @@ SRC_ALL =     \
                src/edit.c \
                src/eval.c \
                src/evalfunc.c \
+               src/evalvars.c \
                src/ex_cmdidxs.h \
                src/ex_cmds.c \
                src/ex_cmds.h \
@@ -188,6 +189,7 @@ SRC_ALL =   \
                src/proto/edit.pro \
                src/proto/eval.pro \
                src/proto/evalfunc.pro \
+               src/proto/evalvars.pro \
                src/proto/ex_cmds.pro \
                src/proto/ex_cmds2.pro \
                src/proto/ex_docmd.pro \
index bb4c7395f5c7ddfcc7e9a6216cbf68855c334542..e362815ce863eff2a0f7f230206fe9951f917359 100644 (file)
@@ -722,6 +722,7 @@ OBJ = \
        $(OUTDIR)/edit.o \
        $(OUTDIR)/eval.o \
        $(OUTDIR)/evalfunc.o \
+       $(OUTDIR)/evalvars.o \
        $(OUTDIR)/ex_cmds.o \
        $(OUTDIR)/ex_cmds2.o \
        $(OUTDIR)/ex_docmd.o \
index 08111aa25a01ffc9bd639d05d2ccca5e4fd94474..432efa2ab2cc90008732b69a0d97c67fb11d8df5 100644 (file)
@@ -42,6 +42,7 @@ SRC = arabic.c                                                \
        edit.c                                                  \
        eval.c                                                  \
        evalfunc.c                                              \
+       evalvars.c                                              \
        ex_cmds.c                                               \
        ex_cmds2.c                                              \
        ex_docmd.c                                              \
index facc15ec58d3b9cbd38c2f37ab7d15031946de54..8050f328716c3ae02ffaaf96e4f5376719ccb5f1 100644 (file)
@@ -731,6 +731,7 @@ OBJ = \
        $(OUTDIR)\edit.obj \
        $(OUTDIR)\eval.obj \
        $(OUTDIR)\evalfunc.obj \
+       $(OUTDIR)\evalvars.obj \
        $(OUTDIR)\ex_cmds.obj \
        $(OUTDIR)\ex_cmds2.obj \
        $(OUTDIR)\ex_docmd.obj \
@@ -1486,6 +1487,8 @@ $(OUTDIR)/eval.obj:       $(OUTDIR) eval.c  $(INCL)
 
 $(OUTDIR)/evalfunc.obj:        $(OUTDIR) evalfunc.c  $(INCL)
 
+$(OUTDIR)/evalvars.obj:        $(OUTDIR) evalvars.c  $(INCL)
+
 $(OUTDIR)/ex_cmds.obj: $(OUTDIR) ex_cmds.c  $(INCL)
 
 $(OUTDIR)/ex_cmds2.obj:        $(OUTDIR) ex_cmds2.c  $(INCL)
@@ -1771,6 +1774,7 @@ proto.h: \
        proto/edit.pro \
        proto/eval.pro \
        proto/evalfunc.pro \
+       proto/evalvars.pro \
        proto/ex_cmds.pro \
        proto/ex_cmds2.pro \
        proto/ex_docmd.pro \
index d1ceb6b1b655e07567786a1282f1e1cf5d7131ae..73a9862ee0e5afd4d02ba4dfc5537020c830cd0c 100644 (file)
@@ -309,7 +309,8 @@ ALL_LIBS = $(LIBS) $(GUI_LIB_DIR) $(GUI_LIB) \
 
 SRC =  arabic.c arglist.c autocmd.c beval.c blob.c blowfish.c buffer.c \
        change.c charset.c cmdexpand.c cmdhist.c crypt.c crypt_zip.c \
-       debugger.c dict.c diff.c digraph.c edit.c eval.c evalfunc.c ex_cmds.c \
+       debugger.c dict.c diff.c digraph.c edit.c eval.c evalfunc.c \
+       evalvars.c ex_cmds.c \
        ex_cmds2.c ex_docmd.c ex_eval.c ex_getln.c if_cscope.c if_xcmdsrv.c \
        fileio.c findfile.c fold.c getchar.c hardcopy.c hashtab.c highlight.c \
        indent.c insexpand.c json.c list.c main.c map.c mark.c menu.c mbyte.c \
@@ -326,7 +327,8 @@ SRC =       arabic.c arglist.c autocmd.c beval.c blob.c blowfish.c buffer.c \
 OBJ =  arabic.obj arglist.obj autocmd.obj beval.obj blob.obj blowfish.obj \
        buffer.obj change.obj charset.obj cmdexpand.obj cmdhist.obj \
        crypt.obj crypt_zip.obj debugger.obj dict.obj diff.obj digraph.obj \
-       edit.obj eval.obj evalfunc.obj ex_cmds.obj ex_cmds2.obj ex_docmd.obj \
+       edit.obj eval.obj evalfunc.obj evalvars.obj ex_cmds.obj ex_cmds2.obj \
+       ex_docmd.obj \
        ex_eval.obj ex_getln.obj if_cscope.obj if_xcmdsrv.obj fileio.obj \
        findfile.obj fold.obj getchar.obj hardcopy.obj hashtab.obj \
        highlight.obj indent.obj insexpand.obj json.obj list.obj main.obj \
@@ -569,6 +571,10 @@ evalfunc.obj : evalfunc.c vim.h [.auto]config.h feature.h os_unix.h \
  ascii.h keymap.h term.h macros.h option.h structs.h \
  regexp.h gui.h beval.h [.proto]gui_beval.pro alloc.h ex_cmds.h spell.h \
  proto.h globals.h version.h
+evalvars.obj : evalvars.c vim.h [.auto]config.h feature.h os_unix.h \
+ ascii.h keymap.h term.h macros.h option.h structs.h \
+ regexp.h gui.h beval.h [.proto]gui_beval.pro alloc.h ex_cmds.h spell.h \
+ proto.h globals.h version.h
 ex_cmds.obj : ex_cmds.c vim.h [.auto]config.h feature.h os_unix.h \
  ascii.h keymap.h term.h macros.h structs.h regexp.h \
  gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \
index 46b865ee078847d5f66d66017996f9edb28aa5d1..0f724621a7e1bc52ead65e38c7e061df7b1fdf73 100644 (file)
@@ -1596,6 +1596,7 @@ BASIC_SRC = \
        edit.c \
        eval.c \
        evalfunc.c \
+       evalvars.c \
        ex_cmds.c \
        ex_cmds2.c \
        ex_docmd.c \
@@ -1725,6 +1726,7 @@ OBJ_COMMON = \
        objects/edit.o \
        objects/eval.o \
        objects/evalfunc.o \
+       objects/evalvars.o \
        objects/ex_cmds.o \
        objects/ex_cmds2.o \
        objects/ex_docmd.o \
@@ -1867,6 +1869,7 @@ PRO_AUTO = \
        edit.pro \
        eval.pro \
        evalfunc.pro \
+       evalvars.pro \
        ex_cmds.pro \
        ex_cmds2.pro \
        ex_docmd.pro \
@@ -3052,6 +3055,9 @@ objects/eval.o: eval.c
 objects/evalfunc.o: evalfunc.c
        $(CCC) -o $@ evalfunc.c
 
+objects/evalvars.o: evalvars.c
+       $(CCC) -o $@ evalvars.c
+
 objects/ex_cmds.o: ex_cmds.c
        $(CCC) -o $@ ex_cmds.c
 
@@ -3570,6 +3576,10 @@ objects/evalfunc.o: evalfunc.c vim.h protodef.h auto/config.h feature.h os_unix.
  auto/osdef.h ascii.h keymap.h term.h macros.h option.h beval.h \
  proto/gui_beval.pro structs.h regexp.h gui.h alloc.h ex_cmds.h spell.h \
  proto.h globals.h version.h
+objects/evalvars.o: evalvars.c vim.h protodef.h auto/config.h feature.h \
+ os_unix.h auto/osdef.h ascii.h keymap.h term.h macros.h option.h beval.h \
+ proto/gui_beval.pro structs.h regexp.h gui.h alloc.h ex_cmds.h spell.h \
+ proto.h globals.h
 objects/ex_cmds.o: ex_cmds.c vim.h protodef.h auto/config.h feature.h os_unix.h \
  auto/osdef.h ascii.h keymap.h term.h macros.h option.h beval.h \
  proto/gui_beval.pro structs.h regexp.h gui.h alloc.h ex_cmds.h spell.h \
index 979cdea31583dcd3b834e65a61a655eba89e3575..0b6a311bcd796bf1dbf8ec557f674de9a51ea01f 100644 (file)
@@ -34,6 +34,7 @@ debugger.c    | vim script debugger
 diff.c         | diff mode (vimdiff)
 eval.c         | expression evaluation
 evalfunc.c     | built-in functions
+evalvars.c     | vim variables
 fileio.c       | reading and writing files
 findfile.c     | search for files in 'path'
 fold.c         | folding
index 9a2642cbe81b812e0beb671d486f082df4cf56ec..7795f41dbf044a6439c2eb148f11fb6c5fbf722e 100644 (file)
 # 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?
@@ -977,6 +944,38 @@ eval_to_number(char_u *expr)
     return retval;
 }
 
+/*
+ * 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".
@@ -1237,1082 +1236,239 @@ 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}
+ * 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.
+ * Indexing only works if "name" is an existing List or Dictionary.
+ * "name" points to the start of the name.
+ * If "rettv" is not NULL it points to the value to be assigned.
+ * "unlet" is TRUE for ":unlet": slightly different behavior when something is
+ * wrong; must end in space or cmd separator.
+ *
+ * flags:
+ *  GLV_QUIET:       do not give error messages
+ *  GLV_READ_ONLY:   will not change the variable
+ *  GLV_NO_AUTOLOAD: do not use script autoloading
  *
- * 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.
+ * Returns a pointer to just after the name, including indexes.
+ * When an evaluation error occurs "lp->ll_name" is NULL;
+ * Returns NULL for a parsing error.  Still need to free items in "lp"!
  */
-    static list_T *
-heredoc_get(exarg_T *eap, char_u *cmd)
+    char_u *
+get_lval(
+    char_u     *name,
+    typval_T   *rettv,
+    lval_T     *lp,
+    int                unlet,
+    int                skip,
+    int                flags,      /* GLV_ values */
+    int                fne_flags)  /* flags for find_name_end() */
 {
-    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;
+    char_u     *expr_start, *expr_end;
+    int                cc;
+    dictitem_T *v;
+    typval_T   var1;
+    typval_T   var2;
+    int                empty1 = FALSE;
+    listitem_T *ni;
+    char_u     *key = NULL;
+    int                len;
+    hashtab_T  *ht;
+    int                quiet = flags & GLV_QUIET;
 
-    if (eap->getline == NULL)
-    {
-       emsg(_("E991: cannot use =<< here"));
-       return NULL;
-    }
+    /* Clear everything in "lp". */
+    vim_memset(lp, 0, sizeof(lval_T));
 
-    // 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])))
+    if (skip)
     {
-       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;
+       /* When skipping just find the end of the name. */
+       lp->ll_name = name;
+       return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
     }
 
-    // The marker is the next word.
-    if (*cmd != NUL && *cmd != '"')
+    /* Find the end of the name. */
+    p = find_name_end(name, &expr_start, &expr_end, fne_flags);
+    if (expr_start != NULL)
     {
-       marker = skipwhite(cmd);
-       p = skiptowhite(marker);
-       if (*skipwhite(p) != NUL && *skipwhite(p) != '"')
+       /* Don't expand the name when we already know there is an error. */
+       if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
+                                                   && *p != '[' && *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)
+       lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
+       if (lp->ll_exp_name == NULL)
        {
-           // set the text indent from the first line.
-           p = theline;
-           text_indent_len = 0;
-           while (VIM_ISWHITE(*p))
+           /* Report an invalid expression in braces, unless the
+            * expression evaluation has been cancelled due to an
+            * aborting error, an interrupt, or an exception. */
+           if (!aborting() && !quiet)
            {
-               p++;
-               text_indent_len++;
+               emsg_severe = TRUE;
+               semsg(_(e_invarg2), name);
+               return NULL;
            }
-           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);
        }
+       lp->ll_name = lp->ll_exp_name;
     }
     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);
-       }
-    }
-}
+       lp->ll_name = name;
 
-/*
- * 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;
+    /* Without [idx] or .key we are done. */
+    if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
+       return p;
 
-    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;
-    }
+    cc = *p;
+    *p = NUL;
+    /* Only pass &ht when we would write to the variable, it prevents autoload
+     * as well. */
+    v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
+                                                     flags & GLV_NO_AUTOLOAD);
+    if (v == NULL && !quiet)
+       semsg(_(e_undefvar), lp->ll_name);
+    *p = cc;
+    if (v == NULL)
+       return NULL;
 
     /*
-     * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
+     * Loop until no more [idx] or .key is following.
      */
-    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 != ']')
+    lp->ll_tv = &v->di_tv;
+    var1.v_type = VAR_UNKNOWN;
+    var2.v_type = VAR_UNKNOWN;
+    while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
     {
-       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 == ';')
+       if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
+               && !(lp->ll_tv->v_type == VAR_DICT
+                                          && lp->ll_tv->vval.v_dict != NULL)
+               && !(lp->ll_tv->v_type == VAR_BLOB
+                                          && lp->ll_tv->vval.v_blob != NULL))
        {
-           /* 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), &ltv, FALSE, is_const,
-                                                           (char_u *)"]", op);
-           clear_tv(&ltv);
-           if (arg == NULL)
-               return FAIL;
-           break;
+           if (!quiet)
+               emsg(_("E689: Can only index a List, Dictionary or Blob"));
+           return NULL;
        }
-       else if (*arg != ',' && *arg != ']')
+       if (lp->ll_range)
        {
-           internal_error("ex_let_vars()");
-           return FAIL;
+           if (!quiet)
+               emsg(_("E708: [:] must come last"));
+           return NULL;
        }
-    }
-
-    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 (;;)
+       len = -1;
+       if (*p == '.')
        {
-           p = skipwhite(p + 1);       /* skip whites after '[', ';' or ',' */
-           s = skip_var_one(p);
-           if (s == p)
+           key = p + 1;
+           for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
+               ;
+           if (len == 0)
            {
-               semsg(_(e_invarg2), p);
+               if (!quiet)
+                   emsg(_(e_emptykey));
                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;
-           }
+           p = key + len;
        }
        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;
-           }
+           /* Get the index [expr] or the first index [expr: ]. */
+           p = skipwhite(p + 1);
+           if (*p == ':')
+               empty1 = TRUE;
            else
            {
-               if (tofree != NULL)
-                   name = tofree;
-               if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
-                   error = TRUE;
-               else
+               empty1 = FALSE;
+               if (eval1(&p, &var1, TRUE) == FAIL)     /* recursive! */
+                   return NULL;
+               if (tv_get_string_chk(&var1) == NULL)
                {
-                   /* 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);
-                   }
+                   /* not a number or string */
+                   clear_tv(&var1);
+                   return NULL;
                }
            }
 
-           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())
+           /* Optionally get the second index [ :expr]. */
+           if (*p == ':')
            {
-               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)
+               if (lp->ll_tv->v_type == VAR_DICT)
                {
-                   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;
+                   if (!quiet)
+                       emsg(_(e_dictrange));
+                   clear_tv(&var1);
+                   return NULL;
                }
-               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 != '.'))
+               if (rettv != NULL
+                       && !(rettv->v_type == VAR_LIST
+                                                && rettv->vval.v_list != NULL)
+                       && !(rettv->v_type == VAR_BLOB
+                                               && rettv->vval.v_blob != NULL))
                {
-                   semsg(_(e_letwrong), op);
-                   s = NULL;  // don't set the value
+                   if (!quiet)
+                       emsg(_("E709: [:] requires a List or Blob value"));
+                   clear_tv(&var1);
+                   return NULL;
                }
+               p = skipwhite(p + 1);
+               if (*p == ']')
+                   lp->ll_empty2 = TRUE;
                else
                {
-                   if (opt_type == 1)  // number
+                   lp->ll_empty2 = FALSE;
+                   if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
                    {
-                       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;
-                       }
+                       clear_tv(&var1);
+                       return NULL;
                    }
-                   else if (opt_type == 0 && stringval != NULL) // string
+                   if (tv_get_string_chk(&var2) == NULL)
                    {
-                       s = concat_str(stringval, s);
-                       vim_free(stringval);
-                       stringval = s;
+                       /* not a number or string */
+                       clear_tv(&var1);
+                       clear_tv(&var2);
+                       return NULL;
                    }
                }
+               lp->ll_range = TRUE;
            }
-           if (s != NULL)
+           else
+               lp->ll_range = FALSE;
+
+           if (*p != ']')
            {
-               set_option_value(arg, n, s, opt_flags);
-               arg_end = p;
+               if (!quiet)
+                   emsg(_(e_missbrac));
+               clear_tv(&var1);
+               clear_tv(&var2);
+               return NULL;
            }
-           *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;
+           /* Skip to past ']'. */
+           ++p;
        }
-       ++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 == '.')
+       if (lp->ll_tv->v_type == VAR_DICT)
+       {
+           if (len == -1)
            {
-               s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
-               if (s != NULL)
+               /* "[key]": get key from "var1" */
+               key = tv_get_string_chk(&var1); /* is number or string */
+               if (key == NULL)
                {
-                   p = ptofree = concat_str(s, p);
-                   vim_free(s);
+                   clear_tv(&var1);
+                   return NULL;
                }
            }
-           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;
+           lp->ll_list = NULL;
+           lp->ll_dict = lp->ll_tv->vval.v_dict;
+           lp->ll_di = dict_find(lp->ll_dict, key, len);
 
-       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.
- * Indexing only works if "name" is an existing List or Dictionary.
- * "name" points to the start of the name.
- * If "rettv" is not NULL it points to the value to be assigned.
- * "unlet" is TRUE for ":unlet": slightly different behavior when something is
- * wrong; must end in space or cmd separator.
- *
- * flags:
- *  GLV_QUIET:       do not give error messages
- *  GLV_READ_ONLY:   will not change the variable
- *  GLV_NO_AUTOLOAD: do not use script autoloading
- *
- * Returns a pointer to just after the name, including indexes.
- * When an evaluation error occurs "lp->ll_name" is NULL;
- * Returns NULL for a parsing error.  Still need to free items in "lp"!
- */
-    char_u *
-get_lval(
-    char_u     *name,
-    typval_T   *rettv,
-    lval_T     *lp,
-    int                unlet,
-    int                skip,
-    int                flags,      /* GLV_ values */
-    int                fne_flags)  /* flags for find_name_end() */
-{
-    char_u     *p;
-    char_u     *expr_start, *expr_end;
-    int                cc;
-    dictitem_T *v;
-    typval_T   var1;
-    typval_T   var2;
-    int                empty1 = FALSE;
-    listitem_T *ni;
-    char_u     *key = NULL;
-    int                len;
-    hashtab_T  *ht;
-    int                quiet = flags & GLV_QUIET;
-
-    /* Clear everything in "lp". */
-    vim_memset(lp, 0, sizeof(lval_T));
-
-    if (skip)
-    {
-       /* When skipping just find the end of the name. */
-       lp->ll_name = name;
-       return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
-    }
-
-    /* Find the end of the name. */
-    p = find_name_end(name, &expr_start, &expr_end, fne_flags);
-    if (expr_start != NULL)
-    {
-       /* Don't expand the name when we already know there is an error. */
-       if (unlet && !VIM_ISWHITE(*p) && !ends_excmd(*p)
-                                                   && *p != '[' && *p != '.')
-       {
-           emsg(_(e_trailing));
-           return NULL;
-       }
-
-       lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
-       if (lp->ll_exp_name == NULL)
-       {
-           /* Report an invalid expression in braces, unless the
-            * expression evaluation has been cancelled due to an
-            * aborting error, an interrupt, or an exception. */
-           if (!aborting() && !quiet)
-           {
-               emsg_severe = TRUE;
-               semsg(_(e_invarg2), name);
-               return NULL;
-           }
-       }
-       lp->ll_name = lp->ll_exp_name;
-    }
-    else
-       lp->ll_name = name;
-
-    /* Without [idx] or .key we are done. */
-    if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
-       return p;
-
-    cc = *p;
-    *p = NUL;
-    /* Only pass &ht when we would write to the variable, it prevents autoload
-     * as well. */
-    v = find_var(lp->ll_name, (flags & GLV_READ_ONLY) ? NULL : &ht,
-                                                     flags & GLV_NO_AUTOLOAD);
-    if (v == NULL && !quiet)
-       semsg(_(e_undefvar), lp->ll_name);
-    *p = cc;
-    if (v == NULL)
-       return NULL;
-
-    /*
-     * Loop until no more [idx] or .key is following.
-     */
-    lp->ll_tv = &v->di_tv;
-    var1.v_type = VAR_UNKNOWN;
-    var2.v_type = VAR_UNKNOWN;
-    while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
-    {
-       if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
-               && !(lp->ll_tv->v_type == VAR_DICT
-                                          && lp->ll_tv->vval.v_dict != NULL)
-               && !(lp->ll_tv->v_type == VAR_BLOB
-                                          && lp->ll_tv->vval.v_blob != NULL))
-       {
-           if (!quiet)
-               emsg(_("E689: Can only index a List, Dictionary or Blob"));
-           return NULL;
-       }
-       if (lp->ll_range)
-       {
-           if (!quiet)
-               emsg(_("E708: [:] must come last"));
-           return NULL;
-       }
-
-       len = -1;
-       if (*p == '.')
-       {
-           key = p + 1;
-           for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
-               ;
-           if (len == 0)
-           {
-               if (!quiet)
-                   emsg(_(e_emptykey));
-               return NULL;
-           }
-           p = key + len;
-       }
-       else
-       {
-           /* Get the index [expr] or the first index [expr: ]. */
-           p = skipwhite(p + 1);
-           if (*p == ':')
-               empty1 = TRUE;
-           else
-           {
-               empty1 = FALSE;
-               if (eval1(&p, &var1, TRUE) == FAIL)     /* recursive! */
-                   return NULL;
-               if (tv_get_string_chk(&var1) == NULL)
-               {
-                   /* not a number or string */
-                   clear_tv(&var1);
-                   return NULL;
-               }
-           }
-
-           /* Optionally get the second index [ :expr]. */
-           if (*p == ':')
-           {
-               if (lp->ll_tv->v_type == VAR_DICT)
-               {
-                   if (!quiet)
-                       emsg(_(e_dictrange));
-                   clear_tv(&var1);
-                   return NULL;
-               }
-               if (rettv != NULL
-                       && !(rettv->v_type == VAR_LIST
-                                                && rettv->vval.v_list != NULL)
-                       && !(rettv->v_type == VAR_BLOB
-                                               && rettv->vval.v_blob != NULL))
-               {
-                   if (!quiet)
-                       emsg(_("E709: [:] requires a List or Blob value"));
-                   clear_tv(&var1);
-                   return NULL;
-               }
-               p = skipwhite(p + 1);
-               if (*p == ']')
-                   lp->ll_empty2 = TRUE;
-               else
-               {
-                   lp->ll_empty2 = FALSE;
-                   if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
-                   {
-                       clear_tv(&var1);
-                       return NULL;
-                   }
-                   if (tv_get_string_chk(&var2) == NULL)
-                   {
-                       /* not a number or string */
-                       clear_tv(&var1);
-                       clear_tv(&var2);
-                       return NULL;
-                   }
-               }
-               lp->ll_range = TRUE;
-           }
-           else
-               lp->ll_range = FALSE;
-
-           if (*p != ']')
-           {
-               if (!quiet)
-                   emsg(_(e_missbrac));
-               clear_tv(&var1);
-               clear_tv(&var2);
-               return NULL;
-           }
-
-           /* Skip to past ']'. */
-           ++p;
-       }
-
-       if (lp->ll_tv->v_type == VAR_DICT)
-       {
-           if (len == -1)
-           {
-               /* "[key]": get key from "var1" */
-               key = tv_get_string_chk(&var1); /* is number or string */
-               if (key == NULL)
-               {
-                   clear_tv(&var1);
-                   return NULL;
-               }
-           }
-           lp->ll_list = NULL;
-           lp->ll_dict = lp->ll_tv->vval.v_dict;
-           lp->ll_di = dict_find(lp->ll_dict, key, len);
-
-           /* When assigning to a scope dictionary check that a function and
-            * variable name is valid (only variable name unless it is l: or
-            * g: dictionary). Disallow overwriting a builtin function. */
-           if (rettv != NULL && lp->ll_dict->dv_scope != 0)
+           /* When assigning to a scope dictionary check that a function and
+            * variable name is valid (only variable name unless it is l: or
+            * g: dictionary). Disallow overwriting a builtin function. */
+           if (rettv != NULL && lp->ll_dict->dv_scope != 0)
            {
                int prevval;
                int wrong;
@@ -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,387 +2261,15 @@ set_context_for_expression(
     xp->xp_pattern = arg;
 }
 
+#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
 /*
- * ":unlet[!] var1 ... " command.
- */
-    void
-ex_unlet(exarg_T *eap)
-{
-    ex_unletlock(eap, eap->arg, 0);
-}
-
-/*
- * ":lockvar" and ":unlockvar" commands
+ * Delete all "menutrans_" variables.
  */
     void
-ex_lockvar(exarg_T *eap)
+del_menutrans_vars(void)
 {
-    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. */
-       while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
-       {
-           item_lock(&li->li_tv, deep, lock);
-           li = li->li_next;
-           ++lp->ll_n1;
-       }
-    }
-    else if (lp->ll_list != NULL)
-       /* (un)lock a List item. */
-       item_lock(&lp->ll_li->li_tv, deep, lock);
-    else
-       /* (un)lock a Dictionary item. */
-       item_lock(&lp->ll_di->di_tv, deep, lock);
-
-    return ret;
-}
-
-/*
- * Lock or unlock an item.  "deep" is nr of levels to go.
- */
-    static void
-item_lock(typval_T *tv, int deep, int lock)
-{
-    static int recurse = 0;
-    list_T     *l;
-    listitem_T *li;
-    dict_T     *d;
-    blob_T     *b;
-    hashitem_T *hi;
-    int                todo;
-
-    if (recurse >= DICT_MAXNEST)
-    {
-       emsg(_("E743: variable nested too deep for (un)lock"));
-       return;
-    }
-    if (deep == 0)
-       return;
-    ++recurse;
-
-    /* lock/unlock the item itself */
-    if (lock)
-       tv->v_lock |= VAR_LOCKED;
-    else
-       tv->v_lock &= ~VAR_LOCKED;
-
-    switch (tv->v_type)
-    {
-       case VAR_UNKNOWN:
-       case VAR_NUMBER:
-       case VAR_STRING:
-       case VAR_FUNC:
-       case VAR_PARTIAL:
-       case VAR_FLOAT:
-       case VAR_SPECIAL:
-       case VAR_JOB:
-       case VAR_CHANNEL:
-           break;
-
-       case VAR_BLOB:
-           if ((b = tv->vval.v_blob) != NULL)
-           {
-               if (lock)
-                   b->bv_lock |= VAR_LOCKED;
-               else
-                   b->bv_lock &= ~VAR_LOCKED;
-           }
-           break;
-       case VAR_LIST:
-           if ((l = tv->vval.v_list) != NULL)
-           {
-               if (lock)
-                   l->lv_lock |= VAR_LOCKED;
-               else
-                   l->lv_lock &= ~VAR_LOCKED;
-               if (deep < 0 || deep > 1)
-                   /* recursive: lock/unlock the items the List contains */
-                   for (li = l->lv_first; li != NULL; li = li->li_next)
-                       item_lock(&li->li_tv, deep - 1, lock);
-           }
-           break;
-       case VAR_DICT:
-           if ((d = tv->vval.v_dict) != NULL)
-           {
-               if (lock)
-                   d->dv_lock |= VAR_LOCKED;
-               else
-                   d->dv_lock &= ~VAR_LOCKED;
-               if (deep < 0 || deep > 1)
-               {
-                   /* recursive: lock/unlock the items the List contains */
-                   todo = (int)d->dv_hashtab.ht_used;
-                   for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
-                   {
-                       if (!HASHITEM_EMPTY(hi))
-                       {
-                           --todo;
-                           item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
-                       }
-                   }
-               }
-           }
-    }
-    --recurse;
-}
-
-#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
-/*
- * Delete all "menutrans_" variables.
- */
-    void
-del_menutrans_vars(void)
-{
-    hashitem_T *hi;
-    int                todo;
+    hashitem_T *hi;
+    int                todo;
 
     hash_lock(&globvarht);
     todo = (int)globvarht.ht_used;
@@ -6871,7 +5655,7 @@ list2fpos(
  * Advance "arg" to the first character after the name.
  * Return 0 for error.
  */
-    static int
+    int
 get_env_len(char_u **arg)
 {
     char_u     *p;
@@ -6929,7 +5713,7 @@ get_id_len(char_u **arg)
  * If the name contains 'magic' {}'s, expand them and return the
  * expanded name in an allocated string via 'alias' - caller must free.
  */
-    static int
+    int
 get_name_len(
     char_u     **arg,
     char_u     **alias,
@@ -7454,53 +6238,6 @@ set_cmdarg(exarg_T *eap, char_u *oldarg)
     return oldval;
 }
 
-/*
- * Get the value of internal variable "name".
- * Return OK or FAIL.  If OK is returned "rettv" must be cleared.
- */
-    static int
-get_var_tv(
-    char_u     *name,
-    int                len,            /* length of "name" */
-    typval_T   *rettv,         /* NULL when only checking existence */
-    dictitem_T **dip,          /* non-NULL when typval's dict item is needed */
-    int                verbose,        /* may give error message */
-    int                no_autoload)    /* do not use script autoloading */
-{
-    int                ret = OK;
-    typval_T   *tv = NULL;
-    dictitem_T *v;
-    int                cc;
-
-    /* truncate the name, so that we can use strcmp() */
-    cc = name[len];
-    name[len] = NUL;
-
-    /*
-     * Check for user-defined variables.
-     */
-    v = find_var(name, NULL, no_autoload);
-    if (v != NULL)
-    {
-       tv = &v->di_tv;
-       if (dip != NULL)
-           *dip = v;
-    }
-
-    if (tv == NULL)
-    {
-       if (rettv != NULL && verbose)
-           semsg(_(e_undefvar), name);
-       ret = FAIL;
-    }
-    else if (rettv != NULL)
-       copy_tv(tv, rettv);
-
-    name[len] = cc;
-
-    return ret;
-}
-
 /*
  * Check if variable "name[len]" is a local variable or an argument.
  * If so, "*eval_lavars_used" is set to TRUE.
@@ -7875,746 +6612,371 @@ tv_get_float(typval_T *varp)
            emsg(_("E907: Using a special value as a Float"));
            break;
        case VAR_JOB:
-# ifdef FEAT_JOB_CHANNEL
-           emsg(_("E911: Using a Job as a Float"));
-           break;
-# endif
-       case VAR_CHANNEL:
-# ifdef FEAT_JOB_CHANNEL
-           emsg(_("E914: Using a Channel as a Float"));
-           break;
-# endif
-       case VAR_BLOB:
-           emsg(_("E975: Using a Blob as a Float"));
-           break;
-       case VAR_UNKNOWN:
-           internal_error("tv_get_float(UNKNOWN)");
-           break;
-    }
-    return 0;
-}
-#endif
-
-/*
- * Get the string value of a variable.
- * If it is a Number variable, the number is converted into a string.
- * tv_get_string() uses a single, static buffer.  YOU CAN ONLY USE IT ONCE!
- * tv_get_string_buf() uses a given buffer.
- * If the String variable has never been set, return an empty string.
- * Never returns NULL;
- * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
- * NULL on error.
- */
-    char_u *
-tv_get_string(typval_T *varp)
-{
-    static char_u   mybuf[NUMBUFLEN];
-
-    return tv_get_string_buf(varp, mybuf);
-}
-
-    char_u *
-tv_get_string_buf(typval_T *varp, char_u *buf)
-{
-    char_u     *res =  tv_get_string_buf_chk(varp, buf);
-
-    return res != NULL ? res : (char_u *)"";
-}
-
-/*
- * Careful: This uses a single, static buffer.  YOU CAN ONLY USE IT ONCE!
- */
-    char_u *
-tv_get_string_chk(typval_T *varp)
-{
-    static char_u   mybuf[NUMBUFLEN];
-
-    return tv_get_string_buf_chk(varp, mybuf);
-}
-
-    char_u *
-tv_get_string_buf_chk(typval_T *varp, char_u *buf)
-{
-    switch (varp->v_type)
-    {
-       case VAR_NUMBER:
-           vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
-                                           (long_long_T)varp->vval.v_number);
-           return buf;
-       case VAR_FUNC:
-       case VAR_PARTIAL:
-           emsg(_("E729: using Funcref as a String"));
-           break;
-       case VAR_LIST:
-           emsg(_("E730: using List as a String"));
-           break;
-       case VAR_DICT:
-           emsg(_("E731: using Dictionary as a String"));
-           break;
-       case VAR_FLOAT:
-#ifdef FEAT_FLOAT
-           emsg(_(e_float_as_string));
-           break;
-#endif
-       case VAR_STRING:
-           if (varp->vval.v_string != NULL)
-               return varp->vval.v_string;
-           return (char_u *)"";
-       case VAR_SPECIAL:
-           STRCPY(buf, get_var_special_name(varp->vval.v_number));
-           return buf;
-        case VAR_BLOB:
-           emsg(_("E976: using Blob as a String"));
-           break;
-       case VAR_JOB:
-#ifdef FEAT_JOB_CHANNEL
-           {
-               job_T *job = varp->vval.v_job;
-               char  *status;
-
-               if (job == NULL)
-                   return (char_u *)"no process";
-               status = job->jv_status == JOB_FAILED ? "fail"
-                               : job->jv_status >= JOB_ENDED ? "dead"
-                               : "run";
-# ifdef UNIX
-               vim_snprintf((char *)buf, NUMBUFLEN,
-                           "process %ld %s", (long)job->jv_pid, status);
-# elif defined(MSWIN)
-               vim_snprintf((char *)buf, NUMBUFLEN,
-                           "process %ld %s",
-                           (long)job->jv_proc_info.dwProcessId,
-                           status);
-# else
-               /* fall-back */
-               vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
-# endif
-               return buf;
-           }
-#endif
-           break;
-       case VAR_CHANNEL:
-#ifdef FEAT_JOB_CHANNEL
-           {
-               channel_T *channel = varp->vval.v_channel;
-               char      *status = channel_status(channel, -1);
-
-               if (channel == NULL)
-                   vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
-               else
-                   vim_snprintf((char *)buf, NUMBUFLEN,
-                                    "channel %d %s", channel->ch_id, status);
-               return buf;
-           }
-#endif
-           break;
-       case VAR_UNKNOWN:
-           emsg(_("E908: using an invalid value as a String"));
-           break;
-    }
-    return NULL;
-}
-
-/*
- * Turn a typeval into a string.  Similar to tv_get_string_buf() but uses
- * string() on Dict, List, etc.
- */
-    static char_u *
-tv_stringify(typval_T *varp, char_u *buf)
-{
-    if (varp->v_type == VAR_LIST
-           || varp->v_type == VAR_DICT
-           || varp->v_type == VAR_FUNC
-           || varp->v_type == VAR_PARTIAL
-           || varp->v_type == VAR_FLOAT)
-    {
-       typval_T tmp;
-
-       f_string(varp, &tmp);
-       tv_get_string_buf(&tmp, buf);
-       clear_tv(varp);
-       *varp = tmp;
-       return tmp.vval.v_string;
-    }
-    return tv_get_string_buf(varp, buf);
-}
-
-/*
- * Find variable "name" in the list of variables.
- * Return a pointer to it if found, NULL if not found.
- * Careful: "a:0" variables don't have a name.
- * When "htp" is not NULL we are writing to the variable, set "htp" to the
- * hashtab_T used.
- */
-    dictitem_T *
-find_var(char_u *name, hashtab_T **htp, int no_autoload)
-{
-    char_u     *varname;
-    hashtab_T  *ht;
-    dictitem_T *ret = NULL;
-
-    ht = find_var_ht(name, &varname);
-    if (htp != NULL)
-       *htp = ht;
-    if (ht == NULL)
-       return NULL;
-    ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
-    if (ret != NULL)
-       return ret;
-
-    /* Search in parent scope for lambda */
-    return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
-}
-
-/*
- * Find variable "varname" in hashtab "ht" with name "htname".
- * Returns NULL if not found.
- */
-    dictitem_T *
-find_var_in_ht(
-    hashtab_T  *ht,
-    int                htname,
-    char_u     *varname,
-    int                no_autoload)
-{
-    hashitem_T *hi;
-
-    if (*varname == NUL)
-    {
-       /* Must be something like "s:", otherwise "ht" would be NULL. */
-       switch (htname)
-       {
-           case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
-           case 'g': return &globvars_var;
-           case 'v': return &vimvars_var;
-           case 'b': return &curbuf->b_bufvar;
-           case 'w': return &curwin->w_winvar;
-           case 't': return &curtab->tp_winvar;
-           case 'l': return get_funccal_local_var();
-           case 'a': return get_funccal_args_var();
-       }
-       return NULL;
-    }
-
-    hi = hash_find(ht, varname);
-    if (HASHITEM_EMPTY(hi))
-    {
-       /* For global variables we may try auto-loading the script.  If it
-        * worked find the variable again.  Don't auto-load a script if it was
-        * loaded already, otherwise it would be loaded every time when
-        * checking if a function name is a Funcref variable. */
-       if (ht == &globvarht && !no_autoload)
-       {
-           /* Note: script_autoload() may make "hi" invalid. It must either
-            * be obtained again or not used. */
-           if (!script_autoload(varname, FALSE) || aborting())
-               return NULL;
-           hi = hash_find(ht, varname);
-       }
-       if (HASHITEM_EMPTY(hi))
-           return NULL;
-    }
-    return HI2DI(hi);
-}
-
-/*
- * Find the hashtab used for a variable name.
- * Return NULL if the name is not valid.
- * Set "varname" to the start of name without ':'.
- */
-    hashtab_T *
-find_var_ht(char_u *name, char_u **varname)
-{
-    hashitem_T *hi;
-    hashtab_T  *ht;
-
-    if (name[0] == NUL)
-       return NULL;
-    if (name[1] != ':')
-    {
-       /* The name must not start with a colon or #. */
-       if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
-           return NULL;
-       *varname = name;
-
-       // "version" is "v:version" in all scopes if scriptversion < 3.
-       // Same for a few other variables marked with VV_COMPAT.
-       if (current_sctx.sc_version < 3)
-       {
-           hi = hash_find(&compat_hashtab, name);
-           if (!HASHITEM_EMPTY(hi))
-               return &compat_hashtab;
-       }
-
-       ht = get_funccal_local_ht();
-       if (ht == NULL)
-           return &globvarht;                  /* global variable */
-       return ht;                              /* local variable */
-    }
-    *varname = name + 2;
-    if (*name == 'g')                          /* global variable */
-       return &globvarht;
-    // There must be no ':' or '#' in the rest of the name, unless g: is used
-    if (vim_strchr(name + 2, ':') != NULL
-                              || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
-       return NULL;
-    if (*name == 'b')                          /* buffer variable */
-       return &curbuf->b_vars->dv_hashtab;
-    if (*name == 'w')                          /* window variable */
-       return &curwin->w_vars->dv_hashtab;
-    if (*name == 't')                          /* tab page variable */
-       return &curtab->tp_vars->dv_hashtab;
-    if (*name == 'v')                          /* v: variable */
-       return &vimvarht;
-    if (*name == 'a')                          /* a: function argument */
-       return get_funccal_args_ht();
-    if (*name == 'l')                          /* l: local function variable */
-       return get_funccal_local_ht();
-    if (*name == 's'                           /* script variable */
-           && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
-       return &SCRIPT_VARS(current_sctx.sc_sid);
-    return NULL;
-}
-
-/*
- * Get the string value of a (global/local) variable.
- * Note: see tv_get_string() for how long the pointer remains valid.
- * Returns NULL when it doesn't exist.
- */
-    char_u *
-get_var_value(char_u *name)
-{
-    dictitem_T *v;
-
-    v = find_var(name, NULL, FALSE);
-    if (v == NULL)
-       return NULL;
-    return tv_get_string(&v->di_tv);
-}
-
-/*
- * Allocate a new hashtab for a sourced script.  It will be used while
- * sourcing this script and when executing functions defined in the script.
- */
-    void
-new_script_vars(scid_T id)
-{
-    int                i;
-    hashtab_T  *ht;
-    scriptvar_T *sv;
-
-    if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
-    {
-       /* Re-allocating ga_data means that an ht_array pointing to
-        * ht_smallarray becomes invalid.  We can recognize this: ht_mask is
-        * at its init value.  Also reset "v_dict", it's always the same. */
-       for (i = 1; i <= ga_scripts.ga_len; ++i)
-       {
-           ht = &SCRIPT_VARS(i);
-           if (ht->ht_mask == HT_INIT_SIZE - 1)
-               ht->ht_array = ht->ht_smallarray;
-           sv = SCRIPT_SV(i);
-           sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
-       }
-
-       while (ga_scripts.ga_len < id)
-       {
-           sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
-               ALLOC_CLEAR_ONE(scriptvar_T);
-           init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
-           ++ga_scripts.ga_len;
-       }
-    }
-}
-
-/*
- * Initialize dictionary "dict" as a scope and set variable "dict_var" to
- * point to it.
- */
-    void
-init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
-{
-    hash_init(&dict->dv_hashtab);
-    dict->dv_lock = 0;
-    dict->dv_scope = scope;
-    dict->dv_refcount = DO_NOT_FREE_CNT;
-    dict->dv_copyID = 0;
-    dict_var->di_tv.vval.v_dict = dict;
-    dict_var->di_tv.v_type = VAR_DICT;
-    dict_var->di_tv.v_lock = VAR_FIXED;
-    dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
-    dict_var->di_key[0] = NUL;
+# ifdef FEAT_JOB_CHANNEL
+           emsg(_("E911: Using a Job as a Float"));
+           break;
+# endif
+       case VAR_CHANNEL:
+# ifdef FEAT_JOB_CHANNEL
+           emsg(_("E914: Using a Channel as a Float"));
+           break;
+# endif
+       case VAR_BLOB:
+           emsg(_("E975: Using a Blob as a Float"));
+           break;
+       case VAR_UNKNOWN:
+           internal_error("tv_get_float(UNKNOWN)");
+           break;
+    }
+    return 0;
 }
+#endif
 
 /*
- * Unreference a dictionary initialized by init_var_dict().
+ * Get the string value of a variable.
+ * If it is a Number variable, the number is converted into a string.
+ * tv_get_string() uses a single, static buffer.  YOU CAN ONLY USE IT ONCE!
+ * tv_get_string_buf() uses a given buffer.
+ * If the String variable has never been set, return an empty string.
+ * Never returns NULL;
+ * tv_get_string_chk() and tv_get_string_buf_chk() are similar, but return
+ * NULL on error.
  */
-    void
-unref_var_dict(dict_T *dict)
+    char_u *
+tv_get_string(typval_T *varp)
 {
-    /* Now the dict needs to be freed if no one else is using it, go back to
-     * normal reference counting. */
-    dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
-    dict_unref(dict);
-}
+    static char_u   mybuf[NUMBUFLEN];
 
-/*
- * Clean up a list of internal variables.
- * Frees all allocated variables and the value they contain.
- * Clears hashtab "ht", does not free it.
- */
-    void
-vars_clear(hashtab_T *ht)
-{
-    vars_clear_ext(ht, TRUE);
+    return tv_get_string_buf(varp, mybuf);
 }
 
-/*
- * Like vars_clear(), but only free the value if "free_val" is TRUE.
- */
-    void
-vars_clear_ext(hashtab_T *ht, int free_val)
+    char_u *
+tv_get_string_buf(typval_T *varp, char_u *buf)
 {
-    int                todo;
-    hashitem_T *hi;
-    dictitem_T *v;
-
-    hash_lock(ht);
-    todo = (int)ht->ht_used;
-    for (hi = ht->ht_array; todo > 0; ++hi)
-    {
-       if (!HASHITEM_EMPTY(hi))
-       {
-           --todo;
+    char_u     *res =  tv_get_string_buf_chk(varp, buf);
 
-           /* Free the variable.  Don't remove it from the hashtab,
-            * ht_array might change then.  hash_clear() takes care of it
-            * later. */
-           v = HI2DI(hi);
-           if (free_val)
-               clear_tv(&v->di_tv);
-           if (v->di_flags & DI_FLAGS_ALLOC)
-               vim_free(v);
-       }
-    }
-    hash_clear(ht);
-    ht->ht_used = 0;
+    return res != NULL ? res : (char_u *)"";
 }
 
 /*
- * Delete a variable from hashtab "ht" at item "hi".
- * Clear the variable value and free the dictitem.
+ * Careful: This uses a single, static buffer.  YOU CAN ONLY USE IT ONCE!
  */
-    static void
-delete_var(hashtab_T *ht, hashitem_T *hi)
+    char_u *
+tv_get_string_chk(typval_T *varp)
 {
-    dictitem_T *di = HI2DI(hi);
+    static char_u   mybuf[NUMBUFLEN];
 
-    hash_remove(ht, hi);
-    clear_tv(&di->di_tv);
-    vim_free(di);
+    return tv_get_string_buf_chk(varp, mybuf);
 }
 
-/*
- * List the value of one internal variable.
- */
-    static void
-list_one_var(dictitem_T *v, char *prefix, int *first)
+    char_u *
+tv_get_string_buf_chk(typval_T *varp, char_u *buf)
 {
-    char_u     *tofree;
-    char_u     *s;
-    char_u     numbuf[NUMBUFLEN];
-
-    s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
-    list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
-                                        s == NULL ? (char_u *)"" : s, first);
-    vim_free(tofree);
-}
-
-    static void
-list_one_var_a(
-    char       *prefix,
-    char_u     *name,
-    int                type,
-    char_u     *string,
-    int                *first)  /* when TRUE clear rest of screen and set to FALSE */
-{
-    /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
-    msg_start();
-    msg_puts(prefix);
-    if (name != NULL)  /* "a:" vars don't have a name stored */
-       msg_puts((char *)name);
-    msg_putchar(' ');
-    msg_advance(22);
-    if (type == VAR_NUMBER)
-       msg_putchar('#');
-    else if (type == VAR_FUNC || type == VAR_PARTIAL)
-       msg_putchar('*');
-    else if (type == VAR_LIST)
-    {
-       msg_putchar('[');
-       if (*string == '[')
-           ++string;
-    }
-    else if (type == VAR_DICT)
-    {
-       msg_putchar('{');
-       if (*string == '{')
-           ++string;
-    }
-    else
-       msg_putchar(' ');
+    switch (varp->v_type)
+    {
+       case VAR_NUMBER:
+           vim_snprintf((char *)buf, NUMBUFLEN, "%lld",
+                                           (long_long_T)varp->vval.v_number);
+           return buf;
+       case VAR_FUNC:
+       case VAR_PARTIAL:
+           emsg(_("E729: using Funcref as a String"));
+           break;
+       case VAR_LIST:
+           emsg(_("E730: using List as a String"));
+           break;
+       case VAR_DICT:
+           emsg(_("E731: using Dictionary as a String"));
+           break;
+       case VAR_FLOAT:
+#ifdef FEAT_FLOAT
+           emsg(_(e_float_as_string));
+           break;
+#endif
+       case VAR_STRING:
+           if (varp->vval.v_string != NULL)
+               return varp->vval.v_string;
+           return (char_u *)"";
+       case VAR_SPECIAL:
+           STRCPY(buf, get_var_special_name(varp->vval.v_number));
+           return buf;
+        case VAR_BLOB:
+           emsg(_("E976: using Blob as a String"));
+           break;
+       case VAR_JOB:
+#ifdef FEAT_JOB_CHANNEL
+           {
+               job_T *job = varp->vval.v_job;
+               char  *status;
 
-    msg_outtrans(string);
+               if (job == NULL)
+                   return (char_u *)"no process";
+               status = job->jv_status == JOB_FAILED ? "fail"
+                               : job->jv_status >= JOB_ENDED ? "dead"
+                               : "run";
+# ifdef UNIX
+               vim_snprintf((char *)buf, NUMBUFLEN,
+                           "process %ld %s", (long)job->jv_pid, status);
+# elif defined(MSWIN)
+               vim_snprintf((char *)buf, NUMBUFLEN,
+                           "process %ld %s",
+                           (long)job->jv_proc_info.dwProcessId,
+                           status);
+# else
+               /* fall-back */
+               vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
+# endif
+               return buf;
+           }
+#endif
+           break;
+       case VAR_CHANNEL:
+#ifdef FEAT_JOB_CHANNEL
+           {
+               channel_T *channel = varp->vval.v_channel;
+               char      *status = channel_status(channel, -1);
 
-    if (type == VAR_FUNC || type == VAR_PARTIAL)
-       msg_puts("()");
-    if (*first)
-    {
-       msg_clr_eos();
-       *first = FALSE;
+               if (channel == NULL)
+                   vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
+               else
+                   vim_snprintf((char *)buf, NUMBUFLEN,
+                                    "channel %d %s", channel->ch_id, status);
+               return buf;
+           }
+#endif
+           break;
+       case VAR_UNKNOWN:
+           emsg(_("E908: using an invalid value as a String"));
+           break;
     }
+    return NULL;
 }
 
 /*
- * Set variable "name" to value in "tv".
- * If the variable already exists, the value is updated.
- * Otherwise the variable is created.
+ * Turn a typeval into a string.  Similar to tv_get_string_buf() but uses
+ * string() on Dict, List, etc.
  */
-    void
-set_var(
-    char_u     *name,
-    typval_T   *tv,
-    int                copy)       // make copy of value in "tv"
+    static char_u *
+tv_stringify(typval_T *varp, char_u *buf)
 {
-    set_var_const(name, tv, copy, FALSE);
+    if (varp->v_type == VAR_LIST
+           || varp->v_type == VAR_DICT
+           || varp->v_type == VAR_FUNC
+           || varp->v_type == VAR_PARTIAL
+           || varp->v_type == VAR_FLOAT)
+    {
+       typval_T tmp;
+
+       f_string(varp, &tmp);
+       tv_get_string_buf(&tmp, buf);
+       clear_tv(varp);
+       *varp = tmp;
+       return tmp.vval.v_string;
+    }
+    return tv_get_string_buf(varp, buf);
 }
 
 /*
- * Set variable "name" to value in "tv".
- * If the variable already exists and "is_const" is FALSE the value is updated.
- * Otherwise the variable is created.
+ * Find variable "name" in the list of variables.
+ * Return a pointer to it if found, NULL if not found.
+ * Careful: "a:0" variables don't have a name.
+ * When "htp" is not NULL we are writing to the variable, set "htp" to the
+ * hashtab_T used.
  */
-    static void
-set_var_const(
-    char_u     *name,
-    typval_T   *tv,
-    int                copy,       // make copy of value in "tv"
-    int                is_const)   // disallow to modify existing variable
+    dictitem_T *
+find_var(char_u *name, hashtab_T **htp, int no_autoload)
 {
-    dictitem_T *v;
     char_u     *varname;
     hashtab_T  *ht;
+    dictitem_T *ret = NULL;
 
     ht = find_var_ht(name, &varname);
-    if (ht == NULL || *varname == NUL)
-    {
-       semsg(_(e_illvar), name);
-       return;
-    }
-    v = find_var_in_ht(ht, 0, varname, TRUE);
-
-    /* Search in parent scope which is possible to reference from lambda */
-    if (v == NULL)
-       v = find_var_in_scoped_ht(name, TRUE);
-
-    if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
-                                     && var_check_func_name(name, v == NULL))
-       return;
-
-    if (v != NULL)
-    {
-       if (is_const)
-       {
-           emsg(_(e_cannot_mod));
-           return;
-       }
-
-       /* existing variable, need to clear the value */
-       if (var_check_ro(v->di_flags, name, FALSE)
-                             || var_check_lock(v->di_tv.v_lock, name, FALSE))
-           return;
-
-       /*
-        * Handle setting internal v: variables separately where needed to
-        * prevent changing the type.
-        */
-       if (ht == &vimvarht)
-       {
-           if (v->di_tv.v_type == VAR_STRING)
-           {
-               VIM_CLEAR(v->di_tv.vval.v_string);
-               if (copy || tv->v_type != VAR_STRING)
-               {
-                   char_u *val = tv_get_string(tv);
-
-                   // Careful: when assigning to v:errmsg and tv_get_string()
-                   // causes an error message the variable will alrady be set.
-                   if (v->di_tv.vval.v_string == NULL)
-                       v->di_tv.vval.v_string = vim_strsave(val);
-               }
-               else
-               {
-                   /* Take over the string to avoid an extra alloc/free. */
-                   v->di_tv.vval.v_string = tv->vval.v_string;
-                   tv->vval.v_string = NULL;
-               }
-               return;
-           }
-           else if (v->di_tv.v_type == VAR_NUMBER)
-           {
-               v->di_tv.vval.v_number = tv_get_number(tv);
-               if (STRCMP(varname, "searchforward") == 0)
-                   set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
-#ifdef FEAT_SEARCH_EXTRA
-               else if (STRCMP(varname, "hlsearch") == 0)
-               {
-                   no_hlsearch = !v->di_tv.vval.v_number;
-                   redraw_all_later(SOME_VALID);
-               }
-#endif
-               return;
-           }
-           else if (v->di_tv.v_type != tv->v_type)
-           {
-               semsg(_("E963: setting %s to value with wrong type"), name);
-               return;
-           }
-       }
+    if (htp != NULL)
+       *htp = ht;
+    if (ht == NULL)
+       return NULL;
+    ret = find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
+    if (ret != NULL)
+       return ret;
 
-       clear_tv(&v->di_tv);
-    }
-    else                   /* add a new variable */
-    {
-       // Can't add "v:" or "a:" variable.
-       if (ht == &vimvarht || ht == get_funccal_args_ht())
-       {
-           semsg(_(e_illvar), name);
-           return;
-       }
+    /* Search in parent scope for lambda */
+    return find_var_in_scoped_ht(name, no_autoload || htp != NULL);
+}
 
-       // Make sure the variable name is valid.
-       if (!valid_varname(varname))
-           return;
+/*
+ * Find variable "varname" in hashtab "ht" with name "htname".
+ * Returns NULL if not found.
+ */
+    dictitem_T *
+find_var_in_ht(
+    hashtab_T  *ht,
+    int                htname,
+    char_u     *varname,
+    int                no_autoload)
+{
+    hashitem_T *hi;
 
-       v = alloc(sizeof(dictitem_T) + STRLEN(varname));
-       if (v == NULL)
-           return;
-       STRCPY(v->di_key, varname);
-       if (hash_add(ht, DI2HIKEY(v)) == FAIL)
+    if (*varname == NUL)
+    {
+       /* Must be something like "s:", otherwise "ht" would be NULL. */
+       switch (htname)
        {
-           vim_free(v);
-           return;
+           case 's': return &SCRIPT_SV(current_sctx.sc_sid)->sv_var;
+           case 'g': return &globvars_var;
+           case 'v': return &vimvars_var;
+           case 'b': return &curbuf->b_bufvar;
+           case 'w': return &curwin->w_winvar;
+           case 't': return &curtab->tp_winvar;
+           case 'l': return get_funccal_local_var();
+           case 'a': return get_funccal_args_var();
        }
-       v->di_flags = DI_FLAGS_ALLOC;
-       if (is_const)
-           v->di_flags |= DI_FLAGS_LOCK;
+       return NULL;
     }
 
-    if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
-       copy_tv(tv, &v->di_tv);
-    else
+    hi = hash_find(ht, varname);
+    if (HASHITEM_EMPTY(hi))
     {
-       v->di_tv = *tv;
-       v->di_tv.v_lock = 0;
-       init_tv(tv);
+       /* For global variables we may try auto-loading the script.  If it
+        * worked find the variable again.  Don't auto-load a script if it was
+        * loaded already, otherwise it would be loaded every time when
+        * checking if a function name is a Funcref variable. */
+       if (ht == &globvarht && !no_autoload)
+       {
+           /* Note: script_autoload() may make "hi" invalid. It must either
+            * be obtained again or not used. */
+           if (!script_autoload(varname, FALSE) || aborting())
+               return NULL;
+           hi = hash_find(ht, varname);
+       }
+       if (HASHITEM_EMPTY(hi))
+           return NULL;
     }
-
-    if (is_const)
-       v->di_tv.v_lock |= VAR_LOCKED;
+    return HI2DI(hi);
 }
 
 /*
- * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
- * Also give an error message.
+ * Find the hashtab used for a variable name.
+ * Return NULL if the name is not valid.
+ * Set "varname" to the start of name without ':'.
  */
-    int
-var_check_ro(int flags, char_u *name, int use_gettext)
+    hashtab_T *
+find_var_ht(char_u *name, char_u **varname)
 {
-    if (flags & DI_FLAGS_RO)
-    {
-       semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
-       return TRUE;
-    }
-    if ((flags & DI_FLAGS_RO_SBX) && sandbox)
+    hashitem_T *hi;
+    hashtab_T  *ht;
+
+    if (name[0] == NUL)
+       return NULL;
+    if (name[1] != ':')
     {
-       semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
-       return TRUE;
+       /* The name must not start with a colon or #. */
+       if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
+           return NULL;
+       *varname = name;
+
+       // "version" is "v:version" in all scopes if scriptversion < 3.
+       // Same for a few other variables marked with VV_COMPAT.
+       if (current_sctx.sc_version < 3)
+       {
+           hi = hash_find(&compat_hashtab, name);
+           if (!HASHITEM_EMPTY(hi))
+               return &compat_hashtab;
+       }
+
+       ht = get_funccal_local_ht();
+       if (ht == NULL)
+           return &globvarht;                  /* global variable */
+       return ht;                              /* local variable */
     }
-    return FALSE;
+    *varname = name + 2;
+    if (*name == 'g')                          /* global variable */
+       return &globvarht;
+    // There must be no ':' or '#' in the rest of the name, unless g: is used
+    if (vim_strchr(name + 2, ':') != NULL
+                              || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
+       return NULL;
+    if (*name == 'b')                          /* buffer variable */
+       return &curbuf->b_vars->dv_hashtab;
+    if (*name == 'w')                          /* window variable */
+       return &curwin->w_vars->dv_hashtab;
+    if (*name == 't')                          /* tab page variable */
+       return &curtab->tp_vars->dv_hashtab;
+    if (*name == 'v')                          /* v: variable */
+       return &vimvarht;
+    if (*name == 'a')                          /* a: function argument */
+       return get_funccal_args_ht();
+    if (*name == 'l')                          /* l: local function variable */
+       return get_funccal_local_ht();
+    if (*name == 's'                           /* script variable */
+           && current_sctx.sc_sid > 0 && current_sctx.sc_sid <= ga_scripts.ga_len)
+       return &SCRIPT_VARS(current_sctx.sc_sid);
+    return NULL;
 }
 
 /*
- * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
- * Also give an error message.
+ * Allocate a new hashtab for a sourced script.  It will be used while
+ * sourcing this script and when executing functions defined in the script.
  */
-    int
-var_check_fixed(int flags, char_u *name, int use_gettext)
+    void
+new_script_vars(scid_T id)
 {
-    if (flags & DI_FLAGS_FIX)
+    int                i;
+    hashtab_T  *ht;
+    scriptvar_T *sv;
+
+    if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
     {
-       semsg(_("E795: Cannot delete variable %s"),
-                                     use_gettext ? (char_u *)_(name) : name);
-       return TRUE;
+       /* Re-allocating ga_data means that an ht_array pointing to
+        * ht_smallarray becomes invalid.  We can recognize this: ht_mask is
+        * at its init value.  Also reset "v_dict", it's always the same. */
+       for (i = 1; i <= ga_scripts.ga_len; ++i)
+       {
+           ht = &SCRIPT_VARS(i);
+           if (ht->ht_mask == HT_INIT_SIZE - 1)
+               ht->ht_array = ht->ht_smallarray;
+           sv = SCRIPT_SV(i);
+           sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
+       }
+
+       while (ga_scripts.ga_len < id)
+       {
+           sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
+               ALLOC_CLEAR_ONE(scriptvar_T);
+           init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
+           ++ga_scripts.ga_len;
+       }
     }
-    return FALSE;
 }
 
 /*
- * Check if a funcref is assigned to a valid variable name.
- * Return TRUE and give an error if not.
+ * Initialize dictionary "dict" as a scope and set variable "dict_var" to
+ * point to it.
  */
-    int
-var_check_func_name(
-    char_u *name,    /* points to start of variable name */
-    int    new_var)  /* TRUE when creating the variable */
+    void
+init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
 {
-    /* Allow for w: b: s: and t:. */
-    if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
-           && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
-                                                    ? name[2] : name[0]))
-    {
-       semsg(_("E704: Funcref variable name must start with a capital: %s"),
-                                                                       name);
-       return TRUE;
-    }
-    /* Don't allow hiding a function.  When "v" is not NULL we might be
-     * assigning another function to the same var, the type is checked
-     * below. */
-    if (new_var && function_exists(name, FALSE))
-    {
-       semsg(_("E705: Variable name conflicts with existing function: %s"),
-                                                                   name);
-       return TRUE;
-    }
-    return FALSE;
+    hash_init(&dict->dv_hashtab);
+    dict->dv_lock = 0;
+    dict->dv_scope = scope;
+    dict->dv_refcount = DO_NOT_FREE_CNT;
+    dict->dv_copyID = 0;
+    dict_var->di_tv.vval.v_dict = dict;
+    dict_var->di_tv.v_type = VAR_DICT;
+    dict_var->di_tv.v_lock = VAR_FIXED;
+    dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
+    dict_var->di_key[0] = NUL;
 }
 
 /*
- * Return TRUE if "flags" indicates variable "name" is locked (immutable).
- * Also give an error message, using "name" or _("name") when use_gettext is
- * TRUE.
+ * Unreference a dictionary initialized by init_var_dict().
  */
-    int
-var_check_lock(int lock, char_u *name, int use_gettext)
+    void
+unref_var_dict(dict_T *dict)
 {
-    if (lock & VAR_LOCKED)
-    {
-       semsg(_("E741: Value is locked: %s"),
-                               name == NULL ? (char_u *)_("Unknown")
-                                            : use_gettext ? (char_u *)_(name)
-                                            : name);
-       return TRUE;
-    }
-    if (lock & VAR_FIXED)
-    {
-       semsg(_("E742: Cannot change value of %s"),
-                               name == NULL ? (char_u *)_("Unknown")
-                                            : use_gettext ? (char_u *)_(name)
-                                            : name);
-       return TRUE;
-    }
-    return FALSE;
+    /* Now the dict needs to be freed if no one else is using it, go back to
+     * normal reference counting. */
+    dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
+    dict_unref(dict);
 }
 
 /*
@@ -8648,25 +7010,6 @@ tv_check_lock(typval_T *tv, char_u *name, int use_gettext)
                    || (lock != 0 && var_check_lock(lock, name, use_gettext));
 }
 
-/*
- * Check if a variable name is valid.
- * Return FALSE and give an error if not.
- */
-    int
-valid_varname(char_u *varname)
-{
-    char_u *p;
-
-    for (p = varname; *p != NUL; ++p)
-       if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
-                                                  && *p != AUTOLOAD_CHAR)
-       {
-           semsg(_(e_illvar), varname);
-           return FALSE;
-       }
-    return TRUE;
-}
-
 /*
  * Copy the values from typval_T "from" to typval_T "to".
  * When needed allocates string or increases reference count.
@@ -9249,155 +7592,13 @@ find_tabwin(
     return wp;
 }
 
-/*
- * getwinvar() and gettabwinvar()
- */
-    void
-getwinvar(
-    typval_T   *argvars,
-    typval_T   *rettv,
-    int                off)        /* 1 for gettabwinvar() */
-{
-    win_T      *win;
-    char_u     *varname;
-    dictitem_T *v;
-    tabpage_T  *tp = NULL;
-    int                done = FALSE;
-    win_T      *oldcurwin;
-    tabpage_T  *oldtabpage;
-    int                need_switch_win;
-
-    if (off == 1)
-       tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
-    else
-       tp = curtab;
-    win = find_win_by_nr(&argvars[off], tp);
-    varname = tv_get_string_chk(&argvars[off + 1]);
-    ++emsg_off;
-
-    rettv->v_type = VAR_STRING;
-    rettv->vval.v_string = NULL;
-
-    if (win != NULL && varname != NULL)
-    {
-       /* Set curwin to be our win, temporarily.  Also set the tabpage,
-        * otherwise the window is not valid. Only do this when needed,
-        * autocommands get blocked. */
-       need_switch_win = !(tp == curtab && win == curwin);
-       if (!need_switch_win
-                 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
-       {
-           if (*varname == '&')
-           {
-               if (varname[1] == NUL)
-               {
-                   /* get all window-local options in a dict */
-                   dict_T      *opts = get_winbuf_options(FALSE);
-
-                   if (opts != NULL)
-                   {
-                       rettv_dict_set(rettv, opts);
-                       done = TRUE;
-                   }
-               }
-               else if (get_option_tv(&varname, rettv, 1) == OK)
-                   /* window-local-option */
-                   done = TRUE;
-           }
-           else
-           {
-               /* Look up the variable. */
-               /* Let getwinvar({nr}, "") return the "w:" dictionary. */
-               v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
-                                                             varname, FALSE);
-               if (v != NULL)
-               {
-                   copy_tv(&v->di_tv, rettv);
-                   done = TRUE;
-               }
-           }
-       }
-
-       if (need_switch_win)
-           /* restore previous notion of curwin */
-           restore_win(oldcurwin, oldtabpage, TRUE);
-    }
-
-    if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
-       /* use the default return value */
-       copy_tv(&argvars[off + 2], rettv);
-
-    --emsg_off;
-}
-
-/*
- * "setwinvar()" and "settabwinvar()" functions
- */
-    void
-setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
-{
-    win_T      *win;
-    win_T      *save_curwin;
-    tabpage_T  *save_curtab;
-    int                need_switch_win;
-    char_u     *varname, *winvarname;
-    typval_T   *varp;
-    char_u     nbuf[NUMBUFLEN];
-    tabpage_T  *tp = NULL;
-
-    if (check_secure())
-       return;
-
-    if (off == 1)
-       tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
-    else
-       tp = curtab;
-    win = find_win_by_nr(&argvars[off], tp);
-    varname = tv_get_string_chk(&argvars[off + 1]);
-    varp = &argvars[off + 2];
-
-    if (win != NULL && varname != NULL && varp != NULL)
-    {
-       need_switch_win = !(tp == curtab && win == curwin);
-       if (!need_switch_win
-              || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
-       {
-           if (*varname == '&')
-           {
-               long    numval;
-               char_u  *strval;
-               int             error = FALSE;
-
-               ++varname;
-               numval = (long)tv_get_number_chk(varp, &error);
-               strval = tv_get_string_buf_chk(varp, nbuf);
-               if (!error && strval != NULL)
-                   set_option_value(varname, numval, strval, OPT_LOCAL);
-           }
-           else
-           {
-               winvarname = alloc(STRLEN(varname) + 3);
-               if (winvarname != NULL)
-               {
-                   STRCPY(winvarname, "w:");
-                   STRCPY(winvarname + 2, varname);
-                   set_var(winvarname, varp, TRUE);
-                   vim_free(winvarname);
-               }
-           }
-       }
-       if (need_switch_win)
-           restore_win(save_curwin, save_curtab, TRUE);
-    }
-}
-
 /*
  * Skip over the name of an option: "&option", "&g:option" or "&l:option".
  * "arg" points to the "&" or '+' when called, to "option" when returning.
  * Returns NULL when no option name found.  Otherwise pointer to the char
  * after the option name.
  */
-    static char_u *
+    char_u *
 find_option_end(char_u **arg, int *opt_flags)
 {
     char_u     *p = *arg;
@@ -9797,38 +7998,6 @@ typval_tostring(typval_T *arg)
     return ret;
 }
 
-    int
-var_exists(char_u *var)
-{
-    char_u     *name;
-    char_u     *tofree;
-    typval_T    tv;
-    int                len = 0;
-    int                n = FALSE;
-
-    /* get_name_len() takes care of expanding curly braces */
-    name = var;
-    len = get_name_len(&var, &tofree, TRUE, FALSE);
-    if (len > 0)
-    {
-       if (tofree != NULL)
-           name = tofree;
-       n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
-       if (n)
-       {
-           /* handle d.key, l[idx], f(expr) */
-           n = (handle_subscript(&var, &tv, TRUE, FALSE, name, &name) == OK);
-           if (n)
-               clear_tv(&tv);
-       }
-    }
-    if (*var != NUL)
-       n = FALSE;
-
-    vim_free(tofree);
-    return n;
-}
-
 #endif /* FEAT_EVAL */
 
 
index 882bf092562b195b5f7cde613d636694f16026e8..8f80dc4f35befe270cc2894b2d6afe3dd622a297 100644 (file)
@@ -155,14 +155,11 @@ static void f_getqflist(typval_T *argvars, typval_T *rettv);
 static void f_getreg(typval_T *argvars, typval_T *rettv);
 static void f_getregtype(typval_T *argvars, typval_T *rettv);
 static void f_gettabinfo(typval_T *argvars, typval_T *rettv);
-static void f_gettabvar(typval_T *argvars, typval_T *rettv);
-static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
 static void f_gettagstack(typval_T *argvars, typval_T *rettv);
 static void f_getwininfo(typval_T *argvars, typval_T *rettv);
 static void f_getwinpos(typval_T *argvars, typval_T *rettv);
 static void f_getwinposx(typval_T *argvars, typval_T *rettv);
 static void f_getwinposy(typval_T *argvars, typval_T *rettv);
-static void f_getwinvar(typval_T *argvars, typval_T *rettv);
 static void f_glob(typval_T *argvars, typval_T *rettv);
 static void f_globpath(typval_T *argvars, typval_T *rettv);
 static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
@@ -293,10 +290,7 @@ static void f_setloclist(typval_T *argvars, typval_T *rettv);
 static void f_setpos(typval_T *argvars, typval_T *rettv);
 static void f_setqflist(typval_T *argvars, typval_T *rettv);
 static void f_setreg(typval_T *argvars, typval_T *rettv);
-static void f_settabvar(typval_T *argvars, typval_T *rettv);
-static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
 static void f_settagstack(typval_T *argvars, typval_T *rettv);
-static void f_setwinvar(typval_T *argvars, typval_T *rettv);
 #ifdef FEAT_CRYPT
 static void f_sha256(typval_T *argvars, typval_T *rettv);
 #endif /* FEAT_CRYPT */
@@ -5314,58 +5308,6 @@ f_gettabinfo(typval_T *argvars, typval_T *rettv)
     }
 }
 
-/*
- * "gettabvar()" function
- */
-    static void
-f_gettabvar(typval_T *argvars, typval_T *rettv)
-{
-    win_T      *oldcurwin;
-    tabpage_T  *tp, *oldtabpage;
-    dictitem_T *v;
-    char_u     *varname;
-    int                done = FALSE;
-
-    rettv->v_type = VAR_STRING;
-    rettv->vval.v_string = NULL;
-
-    varname = tv_get_string_chk(&argvars[1]);
-    tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
-    if (tp != NULL && varname != NULL)
-    {
-       /* Set tp to be our tabpage, temporarily.  Also set the window to the
-        * first window in the tabpage, otherwise the window is not valid. */
-       if (switch_win(&oldcurwin, &oldtabpage,
-               tp == curtab || tp->tp_firstwin == NULL ? firstwin
-                                           : tp->tp_firstwin, tp, TRUE) == OK)
-       {
-           /* look up the variable */
-           /* Let gettabvar({nr}, "") return the "t:" dictionary. */
-           v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
-           if (v != NULL)
-           {
-               copy_tv(&v->di_tv, rettv);
-               done = TRUE;
-           }
-       }
-
-       /* restore previous notion of curwin */
-       restore_win(oldcurwin, oldtabpage, TRUE);
-    }
-
-    if (!done && argvars[2].v_type != VAR_UNKNOWN)
-       copy_tv(&argvars[2], rettv);
-}
-
-/*
- * "gettabwinvar()" function
- */
-    static void
-f_gettabwinvar(typval_T *argvars, typval_T *rettv)
-{
-    getwinvar(argvars, rettv, 1);
-}
-
 /*
  * "gettagstack()" function
  */
@@ -5624,15 +5566,6 @@ f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
 #endif
 }
 
-/*
- * "getwinvar()" function
- */
-    static void
-f_getwinvar(typval_T *argvars, typval_T *rettv)
-{
-    getwinvar(argvars, rettv, 0);
-}
-
 /*
  * "glob()" function
  */
@@ -10242,55 +10175,6 @@ free_lstval:
     rettv->vval.v_number = 0;
 }
 
-/*
- * "settabvar()" function
- */
-    static void
-f_settabvar(typval_T *argvars, typval_T *rettv)
-{
-    tabpage_T  *save_curtab;
-    tabpage_T  *tp;
-    char_u     *varname, *tabvarname;
-    typval_T   *varp;
-
-    rettv->vval.v_number = 0;
-
-    if (check_secure())
-       return;
-
-    tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
-    varname = tv_get_string_chk(&argvars[1]);
-    varp = &argvars[2];
-
-    if (varname != NULL && varp != NULL && tp != NULL)
-    {
-       save_curtab = curtab;
-       goto_tabpage_tp(tp, FALSE, FALSE);
-
-       tabvarname = alloc(STRLEN(varname) + 3);
-       if (tabvarname != NULL)
-       {
-           STRCPY(tabvarname, "t:");
-           STRCPY(tabvarname + 2, varname);
-           set_var(tabvarname, varp, TRUE);
-           vim_free(tabvarname);
-       }
-
-       /* Restore current tabpage */
-       if (valid_tabpage(save_curtab))
-           goto_tabpage_tp(save_curtab, FALSE, FALSE);
-    }
-}
-
-/*
- * "settabwinvar()" function
- */
-    static void
-f_settabwinvar(typval_T *argvars, typval_T *rettv)
-{
-    setwinvar(argvars, rettv, 1);
-}
-
 /*
  * "settagstack()" function
  */
@@ -10347,15 +10231,6 @@ f_settagstack(typval_T *argvars, typval_T *rettv)
        rettv->vval.v_number = 0;
 }
 
-/*
- * "setwinvar()" function
- */
-    static void
-f_setwinvar(typval_T *argvars, typval_T *rettv)
-{
-    setwinvar(argvars, rettv, 0);
-}
-
 #ifdef FEAT_CRYPT
 /*
  * "sha256({string})" function
diff --git a/src/evalvars.c b/src/evalvars.c
new file mode 100644 (file)
index 0000000..ae4d954
--- /dev/null
@@ -0,0 +1,1946 @@
+/* vi:set ts=8 sts=4 sw=4 noet:
+ *
+ * VIM - Vi IMproved   by Bram Moolenaar
+ *
+ * Do ":help uganda"  in Vim to read copying and usage conditions.
+ * Do ":help credits" in Vim to see a list of people who contributed.
+ * See README.txt for an overview of the Vim source code.
+ */
+
+/*
+ * evalvars.c: functions for dealing with variables
+ */
+
+#include "vim.h"
+
+#if defined(FEAT_EVAL) || defined(PROTO)
+
+static char *e_letunexp        = N_("E18: Unexpected characters in :let");
+
+static void ex_let_const(exarg_T *eap, int is_const);
+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 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 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 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);
+
+/*
+ * 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;
+ */
+    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), &ltv, FALSE, is_const,
+                                                           (char_u *)"]", op);
+           clear_tv(&ltv);
+           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.
+ */
+    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 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;
+}
+
+/*
+ * ":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 (is_compatht(ht))
+               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.
+       while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
+       {
+           item_lock(&li->li_tv, deep, lock);
+           li = li->li_next;
+           ++lp->ll_n1;
+       }
+    }
+    else if (lp->ll_list != NULL)
+       // (un)lock a List item.
+       item_lock(&lp->ll_li->li_tv, deep, lock);
+    else
+       // (un)lock a Dictionary item.
+       item_lock(&lp->ll_di->di_tv, deep, lock);
+
+    return ret;
+}
+
+/*
+ * Lock or unlock an item.  "deep" is nr of levels to go.
+ */
+    static void
+item_lock(typval_T *tv, int deep, int lock)
+{
+    static int recurse = 0;
+    list_T     *l;
+    listitem_T *li;
+    dict_T     *d;
+    blob_T     *b;
+    hashitem_T *hi;
+    int                todo;
+
+    if (recurse >= DICT_MAXNEST)
+    {
+       emsg(_("E743: variable nested too deep for (un)lock"));
+       return;
+    }
+    if (deep == 0)
+       return;
+    ++recurse;
+
+    // lock/unlock the item itself
+    if (lock)
+       tv->v_lock |= VAR_LOCKED;
+    else
+       tv->v_lock &= ~VAR_LOCKED;
+
+    switch (tv->v_type)
+    {
+       case VAR_UNKNOWN:
+       case VAR_NUMBER:
+       case VAR_STRING:
+       case VAR_FUNC:
+       case VAR_PARTIAL:
+       case VAR_FLOAT:
+       case VAR_SPECIAL:
+       case VAR_JOB:
+       case VAR_CHANNEL:
+           break;
+
+       case VAR_BLOB:
+           if ((b = tv->vval.v_blob) != NULL)
+           {
+               if (lock)
+                   b->bv_lock |= VAR_LOCKED;
+               else
+                   b->bv_lock &= ~VAR_LOCKED;
+           }
+           break;
+       case VAR_LIST:
+           if ((l = tv->vval.v_list) != NULL)
+           {
+               if (lock)
+                   l->lv_lock |= VAR_LOCKED;
+               else
+                   l->lv_lock &= ~VAR_LOCKED;
+               if (deep < 0 || deep > 1)
+                   // recursive: lock/unlock the items the List contains
+                   for (li = l->lv_first; li != NULL; li = li->li_next)
+                       item_lock(&li->li_tv, deep - 1, lock);
+           }
+           break;
+       case VAR_DICT:
+           if ((d = tv->vval.v_dict) != NULL)
+           {
+               if (lock)
+                   d->dv_lock |= VAR_LOCKED;
+               else
+                   d->dv_lock &= ~VAR_LOCKED;
+               if (deep < 0 || deep > 1)
+               {
+                   // recursive: lock/unlock the items the List contains
+                   todo = (int)d->dv_hashtab.ht_used;
+                   for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
+                   {
+                       if (!HASHITEM_EMPTY(hi))
+                       {
+                           --todo;
+                           item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
+                       }
+                   }
+               }
+           }
+    }
+    --recurse;
+}
+
+/*
+ * Get the value of internal variable "name".
+ * Return OK or FAIL.  If OK is returned "rettv" must be cleared.
+ */
+    int
+get_var_tv(
+    char_u     *name,
+    int                len,            // length of "name"
+    typval_T   *rettv,         // NULL when only checking existence
+    dictitem_T **dip,          // non-NULL when typval's dict item is needed
+    int                verbose,        // may give error message
+    int                no_autoload)    // do not use script autoloading
+{
+    int                ret = OK;
+    typval_T   *tv = NULL;
+    dictitem_T *v;
+    int                cc;
+
+    // truncate the name, so that we can use strcmp()
+    cc = name[len];
+    name[len] = NUL;
+
+    // Check for user-defined variables.
+    v = find_var(name, NULL, no_autoload);
+    if (v != NULL)
+    {
+       tv = &v->di_tv;
+       if (dip != NULL)
+           *dip = v;
+    }
+
+    if (tv == NULL)
+    {
+       if (rettv != NULL && verbose)
+           semsg(_(e_undefvar), name);
+       ret = FAIL;
+    }
+    else if (rettv != NULL)
+       copy_tv(tv, rettv);
+
+    name[len] = cc;
+
+    return ret;
+}
+
+/*
+ * Get the string value of a (global/local) variable.
+ * Note: see tv_get_string() for how long the pointer remains valid.
+ * Returns NULL when it doesn't exist.
+ */
+    char_u *
+get_var_value(char_u *name)
+{
+    dictitem_T *v;
+
+    v = find_var(name, NULL, FALSE);
+    if (v == NULL)
+       return NULL;
+    return tv_get_string(&v->di_tv);
+}
+
+/*
+ * Clean up a list of internal variables.
+ * Frees all allocated variables and the value they contain.
+ * Clears hashtab "ht", does not free it.
+ */
+    void
+vars_clear(hashtab_T *ht)
+{
+    vars_clear_ext(ht, TRUE);
+}
+
+/*
+ * Like vars_clear(), but only free the value if "free_val" is TRUE.
+ */
+    void
+vars_clear_ext(hashtab_T *ht, int free_val)
+{
+    int                todo;
+    hashitem_T *hi;
+    dictitem_T *v;
+
+    hash_lock(ht);
+    todo = (int)ht->ht_used;
+    for (hi = ht->ht_array; todo > 0; ++hi)
+    {
+       if (!HASHITEM_EMPTY(hi))
+       {
+           --todo;
+
+           // Free the variable.  Don't remove it from the hashtab,
+           // ht_array might change then.  hash_clear() takes care of it
+           // later.
+           v = HI2DI(hi);
+           if (free_val)
+               clear_tv(&v->di_tv);
+           if (v->di_flags & DI_FLAGS_ALLOC)
+               vim_free(v);
+       }
+    }
+    hash_clear(ht);
+    ht->ht_used = 0;
+}
+
+/*
+ * Delete a variable from hashtab "ht" at item "hi".
+ * Clear the variable value and free the dictitem.
+ */
+    void
+delete_var(hashtab_T *ht, hashitem_T *hi)
+{
+    dictitem_T *di = HI2DI(hi);
+
+    hash_remove(ht, hi);
+    clear_tv(&di->di_tv);
+    vim_free(di);
+}
+
+/*
+ * List the value of one internal variable.
+ */
+    static void
+list_one_var(dictitem_T *v, char *prefix, int *first)
+{
+    char_u     *tofree;
+    char_u     *s;
+    char_u     numbuf[NUMBUFLEN];
+
+    s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
+    list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
+                                        s == NULL ? (char_u *)"" : s, first);
+    vim_free(tofree);
+}
+
+    static void
+list_one_var_a(
+    char       *prefix,
+    char_u     *name,
+    int                type,
+    char_u     *string,
+    int                *first)  // when TRUE clear rest of screen and set to FALSE
+{
+    // don't use msg() or msg_attr() to avoid overwriting "v:statusmsg"
+    msg_start();
+    msg_puts(prefix);
+    if (name != NULL)  // "a:" vars don't have a name stored
+       msg_puts((char *)name);
+    msg_putchar(' ');
+    msg_advance(22);
+    if (type == VAR_NUMBER)
+       msg_putchar('#');
+    else if (type == VAR_FUNC || type == VAR_PARTIAL)
+       msg_putchar('*');
+    else if (type == VAR_LIST)
+    {
+       msg_putchar('[');
+       if (*string == '[')
+           ++string;
+    }
+    else if (type == VAR_DICT)
+    {
+       msg_putchar('{');
+       if (*string == '{')
+           ++string;
+    }
+    else
+       msg_putchar(' ');
+
+    msg_outtrans(string);
+
+    if (type == VAR_FUNC || type == VAR_PARTIAL)
+       msg_puts("()");
+    if (*first)
+    {
+       msg_clr_eos();
+       *first = FALSE;
+    }
+}
+
+/*
+ * Set variable "name" to value in "tv".
+ * If the variable already exists, the value is updated.
+ * Otherwise the variable is created.
+ */
+    void
+set_var(
+    char_u     *name,
+    typval_T   *tv,
+    int                copy)       // make copy of value in "tv"
+{
+    set_var_const(name, tv, copy, FALSE);
+}
+
+/*
+ * Set variable "name" to value in "tv".
+ * If the variable already exists and "is_const" is FALSE the value is updated.
+ * Otherwise the variable is created.
+ */
+    void
+set_var_const(
+    char_u     *name,
+    typval_T   *tv,
+    int                copy,       // make copy of value in "tv"
+    int                is_const)   // disallow to modify existing variable
+{
+    dictitem_T *v;
+    char_u     *varname;
+    hashtab_T  *ht;
+
+    ht = find_var_ht(name, &varname);
+    if (ht == NULL || *varname == NUL)
+    {
+       semsg(_(e_illvar), name);
+       return;
+    }
+    v = find_var_in_ht(ht, 0, varname, TRUE);
+
+    // Search in parent scope which is possible to reference from lambda
+    if (v == NULL)
+       v = find_var_in_scoped_ht(name, TRUE);
+
+    if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
+                                     && var_check_func_name(name, v == NULL))
+       return;
+
+    if (v != NULL)
+    {
+       if (is_const)
+       {
+           emsg(_(e_cannot_mod));
+           return;
+       }
+
+       // existing variable, need to clear the value
+       if (var_check_ro(v->di_flags, name, FALSE)
+                             || var_check_lock(v->di_tv.v_lock, name, FALSE))
+           return;
+
+       // Handle setting internal v: variables separately where needed to
+       // prevent changing the type.
+       if (is_vimvarht(ht))
+       {
+           if (v->di_tv.v_type == VAR_STRING)
+           {
+               VIM_CLEAR(v->di_tv.vval.v_string);
+               if (copy || tv->v_type != VAR_STRING)
+               {
+                   char_u *val = tv_get_string(tv);
+
+                   // Careful: when assigning to v:errmsg and tv_get_string()
+                   // causes an error message the variable will alrady be set.
+                   if (v->di_tv.vval.v_string == NULL)
+                       v->di_tv.vval.v_string = vim_strsave(val);
+               }
+               else
+               {
+                   // Take over the string to avoid an extra alloc/free.
+                   v->di_tv.vval.v_string = tv->vval.v_string;
+                   tv->vval.v_string = NULL;
+               }
+               return;
+           }
+           else if (v->di_tv.v_type == VAR_NUMBER)
+           {
+               v->di_tv.vval.v_number = tv_get_number(tv);
+               if (STRCMP(varname, "searchforward") == 0)
+                   set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
+#ifdef FEAT_SEARCH_EXTRA
+               else if (STRCMP(varname, "hlsearch") == 0)
+               {
+                   no_hlsearch = !v->di_tv.vval.v_number;
+                   redraw_all_later(SOME_VALID);
+               }
+#endif
+               return;
+           }
+           else if (v->di_tv.v_type != tv->v_type)
+           {
+               semsg(_("E963: setting %s to value with wrong type"), name);
+               return;
+           }
+       }
+
+       clear_tv(&v->di_tv);
+    }
+    else                   // add a new variable
+    {
+       // Can't add "v:" or "a:" variable.
+       if (is_vimvarht(ht) || ht == get_funccal_args_ht())
+       {
+           semsg(_(e_illvar), name);
+           return;
+       }
+
+       // Make sure the variable name is valid.
+       if (!valid_varname(varname))
+           return;
+
+       v = alloc(sizeof(dictitem_T) + STRLEN(varname));
+       if (v == NULL)
+           return;
+       STRCPY(v->di_key, varname);
+       if (hash_add(ht, DI2HIKEY(v)) == FAIL)
+       {
+           vim_free(v);
+           return;
+       }
+       v->di_flags = DI_FLAGS_ALLOC;
+       if (is_const)
+           v->di_flags |= DI_FLAGS_LOCK;
+    }
+
+    if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
+       copy_tv(tv, &v->di_tv);
+    else
+    {
+       v->di_tv = *tv;
+       v->di_tv.v_lock = 0;
+       init_tv(tv);
+    }
+
+    if (is_const)
+       v->di_tv.v_lock |= VAR_LOCKED;
+}
+
+/*
+ * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
+ * Also give an error message.
+ */
+    int
+var_check_ro(int flags, char_u *name, int use_gettext)
+{
+    if (flags & DI_FLAGS_RO)
+    {
+       semsg(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
+       return TRUE;
+    }
+    if ((flags & DI_FLAGS_RO_SBX) && sandbox)
+    {
+       semsg(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
+       return TRUE;
+    }
+    return FALSE;
+}
+
+/*
+ * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
+ * Also give an error message.
+ */
+    int
+var_check_fixed(int flags, char_u *name, int use_gettext)
+{
+    if (flags & DI_FLAGS_FIX)
+    {
+       semsg(_("E795: Cannot delete variable %s"),
+                                     use_gettext ? (char_u *)_(name) : name);
+       return TRUE;
+    }
+    return FALSE;
+}
+
+/*
+ * Check if a funcref is assigned to a valid variable name.
+ * Return TRUE and give an error if not.
+ */
+    int
+var_check_func_name(
+    char_u *name,    // points to start of variable name
+    int    new_var)  // TRUE when creating the variable
+{
+    // Allow for w: b: s: and t:.
+    if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
+           && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
+                                                    ? name[2] : name[0]))
+    {
+       semsg(_("E704: Funcref variable name must start with a capital: %s"),
+                                                                       name);
+       return TRUE;
+    }
+    // Don't allow hiding a function.  When "v" is not NULL we might be
+    // assigning another function to the same var, the type is checked
+    // below.
+    if (new_var && function_exists(name, FALSE))
+    {
+       semsg(_("E705: Variable name conflicts with existing function: %s"),
+                                                                   name);
+       return TRUE;
+    }
+    return FALSE;
+}
+
+/*
+ * Return TRUE if "flags" indicates variable "name" is locked (immutable).
+ * Also give an error message, using "name" or _("name") when use_gettext is
+ * TRUE.
+ */
+    int
+var_check_lock(int lock, char_u *name, int use_gettext)
+{
+    if (lock & VAR_LOCKED)
+    {
+       semsg(_("E741: Value is locked: %s"),
+                               name == NULL ? (char_u *)_("Unknown")
+                                            : use_gettext ? (char_u *)_(name)
+                                            : name);
+       return TRUE;
+    }
+    if (lock & VAR_FIXED)
+    {
+       semsg(_("E742: Cannot change value of %s"),
+                               name == NULL ? (char_u *)_("Unknown")
+                                            : use_gettext ? (char_u *)_(name)
+                                            : name);
+       return TRUE;
+    }
+    return FALSE;
+}
+
+/*
+ * Check if a variable name is valid.
+ * Return FALSE and give an error if not.
+ */
+    int
+valid_varname(char_u *varname)
+{
+    char_u *p;
+
+    for (p = varname; *p != NUL; ++p)
+       if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
+                                                  && *p != AUTOLOAD_CHAR)
+       {
+           semsg(_(e_illvar), varname);
+           return FALSE;
+       }
+    return TRUE;
+}
+
+/*
+ * getwinvar() and gettabwinvar()
+ */
+    static void
+getwinvar(
+    typval_T   *argvars,
+    typval_T   *rettv,
+    int                off)        // 1 for gettabwinvar()
+{
+    win_T      *win;
+    char_u     *varname;
+    dictitem_T *v;
+    tabpage_T  *tp = NULL;
+    int                done = FALSE;
+    win_T      *oldcurwin;
+    tabpage_T  *oldtabpage;
+    int                need_switch_win;
+
+    if (off == 1)
+       tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
+    else
+       tp = curtab;
+    win = find_win_by_nr(&argvars[off], tp);
+    varname = tv_get_string_chk(&argvars[off + 1]);
+    ++emsg_off;
+
+    rettv->v_type = VAR_STRING;
+    rettv->vval.v_string = NULL;
+
+    if (win != NULL && varname != NULL)
+    {
+       // Set curwin to be our win, temporarily.  Also set the tabpage,
+       // otherwise the window is not valid. Only do this when needed,
+       // autocommands get blocked.
+       need_switch_win = !(tp == curtab && win == curwin);
+       if (!need_switch_win
+                 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
+       {
+           if (*varname == '&')
+           {
+               if (varname[1] == NUL)
+               {
+                   // get all window-local options in a dict
+                   dict_T      *opts = get_winbuf_options(FALSE);
+
+                   if (opts != NULL)
+                   {
+                       rettv_dict_set(rettv, opts);
+                       done = TRUE;
+                   }
+               }
+               else if (get_option_tv(&varname, rettv, 1) == OK)
+                   // window-local-option
+                   done = TRUE;
+           }
+           else
+           {
+               // Look up the variable.
+               // Let getwinvar({nr}, "") return the "w:" dictionary.
+               v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
+                                                             varname, FALSE);
+               if (v != NULL)
+               {
+                   copy_tv(&v->di_tv, rettv);
+                   done = TRUE;
+               }
+           }
+       }
+
+       if (need_switch_win)
+           // restore previous notion of curwin
+           restore_win(oldcurwin, oldtabpage, TRUE);
+    }
+
+    if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
+       // use the default return value
+       copy_tv(&argvars[off + 2], rettv);
+
+    --emsg_off;
+}
+
+/*
+ * "setwinvar()" and "settabwinvar()" functions
+ */
+    static void
+setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
+{
+    win_T      *win;
+    win_T      *save_curwin;
+    tabpage_T  *save_curtab;
+    int                need_switch_win;
+    char_u     *varname, *winvarname;
+    typval_T   *varp;
+    char_u     nbuf[NUMBUFLEN];
+    tabpage_T  *tp = NULL;
+
+    if (check_secure())
+       return;
+
+    if (off == 1)
+       tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
+    else
+       tp = curtab;
+    win = find_win_by_nr(&argvars[off], tp);
+    varname = tv_get_string_chk(&argvars[off + 1]);
+    varp = &argvars[off + 2];
+
+    if (win != NULL && varname != NULL && varp != NULL)
+    {
+       need_switch_win = !(tp == curtab && win == curwin);
+       if (!need_switch_win
+              || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
+       {
+           if (*varname == '&')
+           {
+               long    numval;
+               char_u  *strval;
+               int             error = FALSE;
+
+               ++varname;
+               numval = (long)tv_get_number_chk(varp, &error);
+               strval = tv_get_string_buf_chk(varp, nbuf);
+               if (!error && strval != NULL)
+                   set_option_value(varname, numval, strval, OPT_LOCAL);
+           }
+           else
+           {
+               winvarname = alloc(STRLEN(varname) + 3);
+               if (winvarname != NULL)
+               {
+                   STRCPY(winvarname, "w:");
+                   STRCPY(winvarname + 2, varname);
+                   set_var(winvarname, varp, TRUE);
+                   vim_free(winvarname);
+               }
+           }
+       }
+       if (need_switch_win)
+           restore_win(save_curwin, save_curtab, TRUE);
+    }
+}
+
+    int
+var_exists(char_u *var)
+{
+    char_u     *name;
+    char_u     *tofree;
+    typval_T    tv;
+    int                len = 0;
+    int                n = FALSE;
+
+    // get_name_len() takes care of expanding curly braces
+    name = var;
+    len = get_name_len(&var, &tofree, TRUE, FALSE);
+    if (len > 0)
+    {
+       if (tofree != NULL)
+           name = tofree;
+       n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
+       if (n)
+       {
+           // handle d.key, l[idx], f(expr)
+           n = (handle_subscript(&var, &tv, TRUE, FALSE, name, &name) == OK);
+           if (n)
+               clear_tv(&tv);
+       }
+    }
+    if (*var != NUL)
+       n = FALSE;
+
+    vim_free(tofree);
+    return n;
+}
+
+/*
+ * "gettabvar()" function
+ */
+    void
+f_gettabvar(typval_T *argvars, typval_T *rettv)
+{
+    win_T      *oldcurwin;
+    tabpage_T  *tp, *oldtabpage;
+    dictitem_T *v;
+    char_u     *varname;
+    int                done = FALSE;
+
+    rettv->v_type = VAR_STRING;
+    rettv->vval.v_string = NULL;
+
+    varname = tv_get_string_chk(&argvars[1]);
+    tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
+    if (tp != NULL && varname != NULL)
+    {
+       // Set tp to be our tabpage, temporarily.  Also set the window to the
+       // first window in the tabpage, otherwise the window is not valid.
+       if (switch_win(&oldcurwin, &oldtabpage,
+               tp == curtab || tp->tp_firstwin == NULL ? firstwin
+                                           : tp->tp_firstwin, tp, TRUE) == OK)
+       {
+           // look up the variable
+           // Let gettabvar({nr}, "") return the "t:" dictionary.
+           v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
+           if (v != NULL)
+           {
+               copy_tv(&v->di_tv, rettv);
+               done = TRUE;
+           }
+       }
+
+       // restore previous notion of curwin
+       restore_win(oldcurwin, oldtabpage, TRUE);
+    }
+
+    if (!done && argvars[2].v_type != VAR_UNKNOWN)
+       copy_tv(&argvars[2], rettv);
+}
+
+/*
+ * "gettabwinvar()" function
+ */
+    void
+f_gettabwinvar(typval_T *argvars, typval_T *rettv)
+{
+    getwinvar(argvars, rettv, 1);
+}
+
+/*
+ * "getwinvar()" function
+ */
+    void
+f_getwinvar(typval_T *argvars, typval_T *rettv)
+{
+    getwinvar(argvars, rettv, 0);
+}
+
+/*
+ * "settabvar()" function
+ */
+    void
+f_settabvar(typval_T *argvars, typval_T *rettv)
+{
+    tabpage_T  *save_curtab;
+    tabpage_T  *tp;
+    char_u     *varname, *tabvarname;
+    typval_T   *varp;
+
+    rettv->vval.v_number = 0;
+
+    if (check_secure())
+       return;
+
+    tp = find_tabpage((int)tv_get_number_chk(&argvars[0], NULL));
+    varname = tv_get_string_chk(&argvars[1]);
+    varp = &argvars[2];
+
+    if (varname != NULL && varp != NULL && tp != NULL)
+    {
+       save_curtab = curtab;
+       goto_tabpage_tp(tp, FALSE, FALSE);
+
+       tabvarname = alloc(STRLEN(varname) + 3);
+       if (tabvarname != NULL)
+       {
+           STRCPY(tabvarname, "t:");
+           STRCPY(tabvarname + 2, varname);
+           set_var(tabvarname, varp, TRUE);
+           vim_free(tabvarname);
+       }
+
+       // Restore current tabpage
+       if (valid_tabpage(save_curtab))
+           goto_tabpage_tp(save_curtab, FALSE, FALSE);
+    }
+}
+
+/*
+ * "settabwinvar()" function
+ */
+    void
+f_settabwinvar(typval_T *argvars, typval_T *rettv)
+{
+    setwinvar(argvars, rettv, 1);
+}
+
+/*
+ * "setwinvar()" function
+ */
+    void
+f_setwinvar(typval_T *argvars, typval_T *rettv)
+{
+    setwinvar(argvars, rettv, 0);
+}
+
+#endif // FEAT_EVAL
index 712a739e4fdb6f90712a4a59bb63da375c87ea7d..f94ab582adf3a9004862d2ff5c11d3e84191a2de 100644 (file)
@@ -1547,6 +1547,10 @@ EXTERN char e_re_damg[]  INIT(= N_("E43: Damaged match string"));
 EXTERN char e_re_corr[]        INIT(= N_("E44: Corrupted regexp program"));
 EXTERN char e_readonly[]       INIT(= N_("E45: 'readonly' option is set (add ! to override)"));
 #ifdef FEAT_EVAL
+EXTERN char e_undefvar[]       INIT(= N_("E121: Undefined variable: %s"));
+EXTERN char e_letwrong[]       INIT(= N_("E734: Wrong variable type for %s="));
+EXTERN char e_illvar[]         INIT(= N_("E461: Illegal variable name: %s"));
+EXTERN char e_cannot_mod[]     INIT(= N_("E995: Cannot modify existing variable"));
 EXTERN char e_readonlyvar[]    INIT(= N_("E46: Cannot change read-only variable \"%s\""));
 EXTERN char e_readonlysbx[]    INIT(= N_("E794: Cannot set variable in the sandbox: \"%s\""));
 EXTERN char e_emptykey[]       INIT(= N_("E713: Cannot use empty key for Dictionary"));
index 10c8ff5735d0e02bc81dae265e3208a1eabeb21f..05a7cf811a1126103359820b139f6177fac16dcf 100644 (file)
@@ -112,6 +112,7 @@ extern int _stricoll(char *a, char *b);
 # ifdef FEAT_VIMINFO
 # include "viminfo.pro"
 # endif
+# include "evalvars.pro"
 
 /* These prototypes cannot be produced automatically. */
 int smsg(const char *, ...)
index 55bb718c0a05b24813a04bdee04c404cd4fee8b2..1724c0ef09eab98d4ad9ffdacf5ad9a1a4201ec9 100644 (file)
@@ -1,4 +1,6 @@
 /* eval.c */
+varnumber_T num_divide(varnumber_T n1, varnumber_T n2);
+varnumber_T num_modulus(varnumber_T n1, varnumber_T n2);
 void eval_init(void);
 void eval_clear(void);
 void set_internal_string_var(char_u *name, char_u *value);
@@ -17,6 +19,10 @@ int skip_expr(char_u **pp);
 char_u *eval_to_string(char_u *arg, char_u **nextcmd, int convert);
 char_u *eval_to_string_safe(char_u *arg, char_u **nextcmd, int use_sandbox);
 varnumber_T eval_to_number(char_u *expr);
+void list_vim_vars(int *first);
+void list_script_vars(int *first);
+int is_vimvarht(hashtab_T *ht);
+int is_compatht(hashtab_T *ht);
 void prepare_vimvar(int idx, typval_T *save_tv);
 void restore_vimvar(int idx, typval_T *save_tv);
 list_T *eval_spell_expr(char_u *badword, char_u *expr);
@@ -27,18 +33,13 @@ varnumber_T call_func_retnr(char_u *func, int argc, typval_T *argv);
 void *call_func_retstr(char_u *func, int argc, typval_T *argv);
 void *call_func_retlist(char_u *func, int argc, typval_T *argv);
 int eval_foldexpr(char_u *arg, int *cp);
-void ex_let(exarg_T *eap);
-void ex_const(exarg_T *eap);
-void list_hashtable_vars(hashtab_T *ht, char *prefix, int empty, int *first);
 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 set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, int is_const, char_u *op);
 void *eval_for_line(char_u *arg, int *errp, char_u **nextcmdp, int skip);
 int next_for_item(void *fi_void, char_u *arg);
 void free_for_info(void *fi_void);
 void set_context_for_expression(expand_T *xp, char_u *arg, cmdidx_T cmdidx);
-void ex_unlet(exarg_T *eap);
-void ex_lockvar(exarg_T *eap);
-int do_unlet(char_u *name, int forceit);
 void del_menutrans_vars(void);
 char_u *get_user_var_name(expand_T *xp, int idx);
 int pattern_match(char_u *pat, char_u *text, int ic);
@@ -62,7 +63,9 @@ char_u *string_quote(char_u *str, int function);
 int string2float(char_u *text, float_T *value);
 pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
 int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
+int get_env_len(char_u **arg);
 int get_id_len(char_u **arg);
+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);
 int eval_isnamec(int c);
 int eval_isnamec1(int c);
@@ -98,18 +101,9 @@ char_u *tv_get_string_buf_chk(typval_T *varp, char_u *buf);
 dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
 dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
 hashtab_T *find_var_ht(char_u *name, char_u **varname);
-char_u *get_var_value(char_u *name);
 void new_script_vars(scid_T id);
 void init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope);
 void unref_var_dict(dict_T *dict);
-void vars_clear(hashtab_T *ht);
-void vars_clear_ext(hashtab_T *ht, int free_val);
-void set_var(char_u *name, typval_T *tv, int copy);
-int var_check_ro(int flags, char_u *name, int use_gettext);
-int var_check_fixed(int flags, char_u *name, int use_gettext);
-int var_check_func_name(char_u *name, int new_var);
-int var_check_lock(int lock, char_u *name, int use_gettext);
-int valid_varname(char_u *varname);
 void copy_tv(typval_T *from, typval_T *to);
 int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
 void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog, int secret);
@@ -119,8 +113,7 @@ void ex_execute(exarg_T *eap);
 win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
 win_T *find_win_by_nr_or_id(typval_T *vp);
 win_T *find_tabwin(typval_T *wvp, typval_T *tvp, tabpage_T **ptp);
-void getwinvar(typval_T *argvars, typval_T *rettv, int off);
-void setwinvar(typval_T *argvars, typval_T *rettv, int off);
+char_u *find_option_end(char_u **arg, int *opt_flags);
 char_u *autoload_name(char_u *name);
 int script_autoload(char_u *name, int reload);
 void last_set_msg(sctx_T script_ctx);
@@ -128,7 +121,6 @@ void reset_v_option_vars(void);
 void assert_error(garray_T *gap);
 int typval_compare(typval_T *typ1, typval_T *typ2, exptype_T type, int type_is, int ic);
 char_u *typval_tostring(typval_T *arg);
-int var_exists(char_u *var);
 int modify_fname(char_u *src, int tilde_file, int *usedlen, char_u **fnamep, char_u **bufp, int *fnamelen);
 char_u *do_string_sub(char_u *str, char_u *pat, char_u *sub, typval_T *expr, char_u *flags);
 void filter_map(typval_T *argvars, typval_T *rettv, int map);
diff --git a/src/proto/evalvars.pro b/src/proto/evalvars.pro
new file mode 100644 (file)
index 0000000..dc4a791
--- /dev/null
@@ -0,0 +1,29 @@
+/* evalvars.c */
+void ex_let(exarg_T *eap);
+void ex_const(exarg_T *eap);
+int ex_let_vars(char_u *arg_start, typval_T *tv, int copy, int semicolon, int var_count, int is_const, char_u *op);
+char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
+void list_hashtable_vars(hashtab_T *ht, char *prefix, int empty, int *first);
+void ex_unlet(exarg_T *eap);
+void ex_lockvar(exarg_T *eap);
+int do_unlet(char_u *name, int forceit);
+int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
+char_u *get_var_value(char_u *name);
+void vars_clear(hashtab_T *ht);
+void vars_clear_ext(hashtab_T *ht, int free_val);
+void delete_var(hashtab_T *ht, hashitem_T *hi);
+void set_var(char_u *name, typval_T *tv, int copy);
+void set_var_const(char_u *name, typval_T *tv, int copy, int is_const);
+int var_check_ro(int flags, char_u *name, int use_gettext);
+int var_check_fixed(int flags, char_u *name, int use_gettext);
+int var_check_func_name(char_u *name, int new_var);
+int var_check_lock(int lock, char_u *name, int use_gettext);
+int valid_varname(char_u *varname);
+int var_exists(char_u *var);
+void f_gettabvar(typval_T *argvars, typval_T *rettv);
+void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
+void f_getwinvar(typval_T *argvars, typval_T *rettv);
+void f_settabvar(typval_T *argvars, typval_T *rettv);
+void f_settabwinvar(typval_T *argvars, typval_T *rettv);
+void f_setwinvar(typval_T *argvars, typval_T *rettv);
+/* vim: set ft=c : */
index 412ae926f1358a03693995d853d5ce6d543411c6..33bf502d3e8208ee353983633bfefcfd7b02f21f 100644 (file)
@@ -761,6 +761,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1933,
 /**/
     1932,
 /**/
index dca8748179089cac60a5bd21d45a207a2cff48ba..1875e3ba286bfdb91ab08db2f4da58a7255b9d72 100644 (file)
--- a/src/vim.h
+++ b/src/vim.h
@@ -2003,6 +2003,8 @@ typedef int sock_T;
 #define VAR_TYPE_CHANNEL    9
 #define VAR_TYPE_BLOB      10
 
+#define DICT_MAXNEST 100       /* maximum nesting of lists and dicts */
+
 #ifdef FEAT_CLIPBOARD
 
 /* VIM_ATOM_NAME is the older Vim-specific selection type for X11.  Still