vim-patch:8.0.1832: cannot use :unlet for an environment variable

Problem:    Cannot use :unlet for an environment variable.
Solution:   Make it work.  Use unsetenv() if available.
            (Yasuhiro Matsumoto, closes vim/vim#2855)
137374fd65
This commit is contained in:
Jan Edmund Lazo 2018-10-01 22:57:49 -04:00
parent 65206714bc
commit 07fbdf4acc
3 changed files with 44 additions and 0 deletions

View File

@ -9136,6 +9136,14 @@ This does NOT work: >
variables are automatically deleted when the function
ends.
:unl[et] ${env-name} ... *:unlet-environment* *:unlet-$*
Remove environment variable {env-name}.
Can mix {name} and ${env-name} in one :unlet command.
No error message is given for a non-existing
variable, also without !.
If the system does not support deleting an environment
variable, it is made emtpy.
:lockv[ar][!] [depth] {name} ... *:lockvar* *:lockv*
Lock the internal variable {name}. Locking means that
it can no longer be changed (until it is unlocked).

View File

@ -2818,6 +2818,18 @@ static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep)
lval_T lv;
do {
if (*arg == '$') {
const char *name = (char *)++arg;
if (get_env_len((const char_u **)&arg) == 0) {
EMSG2(_(e_invarg2), name - 1);
return;
}
os_unsetenv(name);
arg = skipwhite(arg);
continue;
}
// Parse the name and find the end.
char_u *const name_end = (char_u *)get_lval(arg, NULL, &lv, true,
eap->skip || error,

View File

@ -28,3 +28,27 @@ endfunc
func Test_unlet_fails()
call assert_fails('unlet v:["count"]', 'E46:')
endfunc
func Test_unlet_env()
let envcmd = has('win32') ? 'set' : 'env'
let $FOOBAR = 'test'
let found = 0
for kv in split(system(envcmd), "\r*\n")
if kv == 'FOOBAR=test'
let found = 1
endif
endfor
call assert_equal(1, found)
unlet $FOOBAR
let found = 0
for kv in split(system(envcmd), "\r*\n")
if kv == 'FOOBAR=test'
let found = 1
endif
endfor
call assert_equal(0, found)
unlet $MUST_NOT_BE_AN_ERROR
endfunc