Problem: Cannot lock a variable in legacy Vim script like in Vim9.
Solution: Make ":lockvar 0" work.
< is equivalent to: >
:let x = 1
:lockvar! x
-< This is useful if you want to make sure the variable
+< NOTE: in Vim9 script `:const` works differently, see
+ |vim9-const|
+ This is useful if you want to make sure the variable
is not modified. If the value is a List or Dictionary
literal then the items also cannot be changed: >
const ll = [1, 2, 3]
[depth] is relevant when locking a |List| or
|Dictionary|. It specifies how deep the locking goes:
+ 0 Lock the variable {name} but not its
+ value.
1 Lock the |List| or |Dictionary| itself,
cannot add or remove items, but can
still change their values.
|Dictionary|, one level deeper.
The default [depth] is 2, thus when {name} is a |List|
or |Dictionary| the values cannot be changed.
- *E743*
+
+ Example with [depth] 0: >
+ let mylist = [1, 2, 3]
+ lockvar 0 mylist
+ let mylist[0] = 77 " OK
+ call add(mylist, 4] " OK
+ let mylist = [7, 8, 9] " Error!
+< *E743*
For unlimited depth use [!] and omit [depth].
However, there is a maximum depth of 100 to catch
loops.
}
else if (*action == 'f' && HI2DI(hi2) != di1)
{
- if (var_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
+ if (value_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
|| var_check_ro(di1->di_flags, arg_errmsg, TRUE))
break;
clear_tv(&di1->di_tv);
if (argvars[2].v_type != VAR_UNKNOWN)
semsg(_(e_toomanyarg), "remove()");
else if ((d = argvars[0].vval.v_dict) != NULL
- && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
+ && !value_check_lock(d->dv_lock, arg_errmsg, TRUE))
{
key = tv_get_string_chk(&argvars[1]);
if (key != NULL)
}
// existing variable, need to check if it can be changed
else if ((flags & GLV_READ_ONLY) == 0
- && var_check_ro(lp->ll_di->di_flags, name, FALSE))
+ && (var_check_ro(lp->ll_di->di_flags, name, FALSE)
+ || var_check_lock(lp->ll_di->di_flags, name, FALSE)))
{
clear_tv(&var1);
return NULL;
semsg(_(e_letwrong), op);
return;
}
- if (var_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
+ if (value_check_lock(lp->ll_blob->bv_lock, lp->ll_name, FALSE))
return;
if (lp->ll_range && rettv->v_type == VAR_BLOB)
}
*endp = cc;
}
- else if (var_check_lock(lp->ll_newkey == NULL
+ else if (value_check_lock(lp->ll_newkey == NULL
? lp->ll_tv->v_lock
: lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
;
*/
for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
{
- if (var_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
+ if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
return;
ri = ri->li_next;
if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
*name_end = cc;
}
else if ((lp->ll_list != NULL
- && var_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
+ && value_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)))
+ && value_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
return FAIL;
else if (lp->ll_range)
{
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))
+ if (value_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
return FAIL;
ll_li = li;
++ll_n1;
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))
+ || value_check_lock(d->dv_lock, name, FALSE))
return FAIL;
delete_var(ht, hi);
int cc;
dictitem_T *di;
- if (deep == 0) // nothing to do
- return OK;
-
if (lp->ll_tv == NULL)
{
cc = *name_end;
di->di_flags |= DI_FLAGS_LOCK;
else
di->di_flags &= ~DI_FLAGS_LOCK;
- item_lock(&di->di_tv, deep, lock, FALSE);
+ if (deep != 0)
+ item_lock(&di->di_tv, deep, lock, FALSE);
}
}
*name_end = cc;
}
+ else if (deep == 0)
+ {
+ // nothing to do
+ }
else if (lp->ll_range)
{
listitem_T *li = lp->ll_li;
goto failed;
}
+ // Check in this order for backwards compatibility:
+ // - Whether the variable is read-only
+ // - Whether the variable value is locked
+ // - Whether the variable is locked
if (var_check_ro(di->di_flags, name, FALSE)
- || var_check_lock(di->di_tv.v_lock, name, FALSE))
+ || value_check_lock(di->di_tv.v_lock, name, FALSE)
+ || var_check_lock(di->di_flags, name, FALSE))
goto failed;
}
else
return FALSE;
}
+/*
+ * Return TRUE if di_flags "flags" indicates variable "name" is locked.
+ * Also give an error message.
+ */
+ int
+var_check_lock(int flags, char_u *name, int use_gettext)
+{
+ if (flags & DI_FLAGS_LOCK)
+ {
+ semsg(_(e_variable_is_locked_str),
+ 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.
}
/*
- * Return TRUE if "flags" indicates variable "name" is locked (immutable).
- * Also give an error message, using "name" or _("name") when use_gettext is
- * TRUE.
+ * Return TRUE if "flags" indicates variable "name" has a locked (immutable)
+ * value. 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)
+value_check_lock(int lock, char_u *name, int use_gettext)
{
if (lock & VAR_LOCKED)
{
}
l = argvars[0].vval.v_list;
- if (l != NULL && !var_check_lock(l->lv_lock,
+ if (l != NULL && !value_check_lock(l->lv_lock,
(char_u *)N_("flatten() argument"), TRUE)
&& list_flatten(l, maxdepth) == OK)
copy_tv(&argvars[0], rettv);
int idx;
if ((l = argvars[0].vval.v_list) == NULL
- || var_check_lock(l->lv_lock, arg_errmsg, TRUE))
+ || value_check_lock(l->lv_lock, arg_errmsg, TRUE))
return;
idx = (long)tv_get_number_chk(&argvars[1], &error);
else
{
l = argvars[0].vval.v_list;
- if (l == NULL || var_check_lock(l->lv_lock,
+ if (l == NULL || value_check_lock(l->lv_lock,
(char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
TRUE))
goto theend;
else if (argvars[0].v_type == VAR_LIST)
{
if ((l = argvars[0].vval.v_list) == NULL
- || (!map && var_check_lock(l->lv_lock, arg_errmsg, TRUE)))
+ || (!map && value_check_lock(l->lv_lock, arg_errmsg, TRUE)))
return;
}
else if (argvars[0].v_type == VAR_DICT)
{
if ((d = argvars[0].vval.v_dict) == NULL
- || (!map && var_check_lock(d->dv_lock, arg_errmsg, TRUE)))
+ || (!map && value_check_lock(d->dv_lock, arg_errmsg, TRUE)))
return;
}
else
--todo;
di = HI2DI(hi);
- if (map && (var_check_lock(di->di_tv.v_lock,
+ if (map && (value_check_lock(di->di_tv.v_lock,
arg_errmsg, TRUE)
|| var_check_ro(di->di_flags,
arg_errmsg, TRUE)))
l->lv_lock = VAR_LOCKED;
for (li = l->lv_first; li != NULL; li = nli)
{
- if (map && var_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
+ if (map && value_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
break;
nli = li->li_next;
set_vim_var_nr(VV_KEY, idx);
if (argvars[0].v_type == VAR_LIST)
{
if ((l = argvars[0].vval.v_list) != NULL
- && !var_check_lock(l->lv_lock,
+ && !value_check_lock(l->lv_lock,
(char_u *)N_("add() argument"), TRUE)
&& list_append_tv(l, &argvars[1]) == OK)
copy_tv(&argvars[0], rettv);
else if (argvars[0].v_type == VAR_BLOB)
{
if ((b = argvars[0].vval.v_blob) != NULL
- && !var_check_lock(b->bv_lock,
+ && !value_check_lock(b->bv_lock,
(char_u *)N_("add() argument"), TRUE))
{
int error = FALSE;
l1 = argvars[0].vval.v_list;
l2 = argvars[1].vval.v_list;
- if (l1 != NULL && !var_check_lock(l1->lv_lock, arg_errmsg, TRUE)
+ if (l1 != NULL && !value_check_lock(l1->lv_lock, arg_errmsg, TRUE)
&& l2 != NULL)
{
if (argvars[2].v_type != VAR_UNKNOWN)
d1 = argvars[0].vval.v_dict;
d2 = argvars[1].vval.v_dict;
- if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE)
+ if (d1 != NULL && !value_check_lock(d1->dv_lock, arg_errmsg, TRUE)
&& d2 != NULL)
{
// Check the third argument.
else if (argvars[0].v_type != VAR_LIST)
semsg(_(e_listblobarg), "insert()");
else if ((l = argvars[0].vval.v_list) != NULL
- && !var_check_lock(l->lv_lock,
+ && !value_check_lock(l->lv_lock,
(char_u *)N_("insert() argument"), TRUE))
{
if (argvars[2].v_type != VAR_UNKNOWN)
if (argvars[0].v_type != VAR_LIST)
semsg(_(e_listblobarg), "reverse()");
else if ((l = argvars[0].vval.v_list) != NULL
- && !var_check_lock(l->lv_lock,
+ && !value_check_lock(l->lv_lock,
(char_u *)N_("reverse() argument"), TRUE))
{
if (l->lv_first == &range_list_item)
void set_var(char_u *name, typval_T *tv, int copy);
void set_var_const(char_u *name, type_T *type, typval_T *tv_arg, int copy, int flags);
int var_check_ro(int flags, char_u *name, int use_gettext);
+int var_check_lock(int flags, char_u *name, int use_gettext);
int var_check_fixed(int flags, char_u *name, int use_gettext);
int var_wrong_func_name(char_u *name, int new_var);
-int var_check_lock(int lock, char_u *name, int use_gettext);
+int value_check_lock(int lock, char_u *name, int use_gettext);
int valid_varname(char_u *varname);
void reset_v_option_vars(void);
void assert_error(garray_T *gap);
if 0 | lockvar x | endif
let x = 'again'
+
+ let val = [1, 2, 3]
+ lockvar 0 val
+ let val[0] = 9
+ call assert_equal([9, 2, 3], val)
+ call add(val, 4)
+ call assert_equal([9, 2, 3, 4], val)
+ call assert_fails('let val = [4, 5, 6]', 'E1122:')
endfunc
" Locked variables
func Test_list_locked_var()
let expected = [
- \ [['0000-000', 'ppppppp'],
+ \ [['1000-000', 'ppppppF'],
\ ['0000-000', 'ppppppp'],
\ ['0000-000', 'ppppppp']],
\ [['1000-000', 'ppppppF'],
exe "unlockvar " . depth . " l"
endif
let ps = islocked("l").islocked("l[1]").islocked("l[1][1]").islocked("l[1][1][0]").'-'.islocked("l[2]").islocked("l[2]['6']").islocked("l[2]['6'][7]")
- call assert_equal(expected[depth][u][0], ps)
+ call assert_equal(expected[depth][u][0], ps, 'depth: ' .. depth)
let ps = ''
try
let l[1][1][0] = 99
catch
let ps .= 'F'
endtry
- call assert_equal(expected[depth][u][1], ps)
+ call assert_equal(expected[depth][u][1], ps, 'depth: ' .. depth)
endfor
endfor
call assert_fails("let x=islocked('a b')", 'E488:')
" Unletting locked variables
func Test_list_locked_var_unlet()
let expected = [
- \ [['0000-000', 'ppppppp'],
+ \ [['1000-000', 'ppppppp'],
\ ['0000-000', 'ppppppp'],
\ ['0000-000', 'ppppppp']],
\ [['1000-000', 'ppFppFp'],
exe "unlockvar " . depth . " l"
endif
let ps = islocked("l").islocked("l[1]").islocked("l[1][1]").islocked("l[1][1][0]").'-'.islocked("l[2]").islocked("l[2]['6']").islocked("l[2]['6'][7]")
- call assert_equal(expected[depth][u][0], ps)
+ call assert_equal(expected[depth][u][0], ps, 'depth: ' .. depth)
let ps = ''
try
unlet l[2]['6'][7]
call s:arg_list_test(1, 2, [3, 4], {5: 6})
endfunc
+func Test_dict_item_locked()
+endfunc
+
" Tests for reverse(), sort(), uniq()
func Test_reverse_sort_uniq()
let l = ['-0', 'A11', 2, 2, 'xaaa', 4, 'foo', 'foo6', 'foo', [0, 1, 2], 'x8', [0, 1, 2], 1.5]
default:
break;
}
- return var_check_lock(tv->v_lock, name, use_gettext)
- || (lock != 0 && var_check_lock(lock, name, use_gettext));
+ return value_check_lock(tv->v_lock, name, use_gettext)
+ || (lock != 0 && value_check_lock(lock, name, use_gettext));
}
/*
if (fudi.fd_di == NULL)
{
// Can't add a function to a locked dictionary
- if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
+ if (value_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
goto erret;
}
// Can't change an existing function if it is locked
- else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
+ else if (value_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
goto erret;
// Give the function a sequential number. Can only be used with a
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 1698,
/**/
1697,
/**/