]> granicus.if.org Git - vim/commitdiff
patch 8.0.1832: cannot use :unlet for an environment variable v8.0.1832
authorBram Moolenaar <Bram@vim.org>
Sun, 13 May 2018 13:59:50 +0000 (15:59 +0200)
committerBram Moolenaar <Bram@vim.org>
Sun, 13 May 2018 13:59:50 +0000 (15:59 +0200)
Problem:    Cannot use :unlet for an environment variable.
Solution:   Make it work.  Use unsetenv() if available. (Ken Takata,
            closes #2855)

runtime/doc/eval.txt
src/auto/configure
src/config.h.in
src/configure.ac
src/eval.c
src/misc1.c
src/proto/misc1.pro
src/testdir/test_unlet.vim
src/version.c

index 050b48960a586b2b8647b230621bd901e768d3bc..730a5d11c4f294ebb6a605a310121fed3f5fa8ff 100644 (file)
@@ -9939,6 +9939,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).
index e65884c80ae97e247b4fab0019b366a5882394f4..1628b940e3f36184e87e7e92a4490d7914d1cbe7 100755 (executable)
@@ -12609,7 +12609,7 @@ for ac_func in fchdir fchown fchmod fsync getcwd getpseudotty \
        getpgid setpgid setsid sigaltstack sigstack sigset sigsetjmp sigaction \
        sigprocmask sigvec strcasecmp strerror strftime stricmp strncasecmp \
        strnicmp strpbrk strtol tgetent towlower towupper iswupper \
-       usleep utime utimes mblen ftruncate
+       usleep utime utimes mblen ftruncate unsetenv
 do :
   as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
 ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"
index 7d6122058f8d92fea9b9c3f7c7d2f6bd4d79577d..00117cf8ca13dd9c74323f45b1d76456ef4b0054 100644 (file)
 #undef HAVE_TOWLOWER
 #undef HAVE_TOWUPPER
 #undef HAVE_ISWUPPER
+#undef HAVE_UNSETENV
 #undef HAVE_USLEEP
 #undef HAVE_UTIME
 #undef HAVE_BIND_TEXTDOMAIN_CODESET
index cfc3b1be1b2e90dabf4a5a31f5b7b7fb6ac30f67..107c170ecb470551d07a3e664689ea6733deccbc 100644 (file)
@@ -3709,7 +3709,7 @@ AC_CHECK_FUNCS(fchdir fchown fchmod fsync getcwd getpseudotty \
        getpgid setpgid setsid sigaltstack sigstack sigset sigsetjmp sigaction \
        sigprocmask sigvec strcasecmp strerror strftime stricmp strncasecmp \
        strnicmp strpbrk strtol tgetent towlower towupper iswupper \
-       usleep utime utimes mblen ftruncate)
+       usleep utime utimes mblen ftruncate unsetenv)
 AC_FUNC_FSEEKO
 
 dnl define _LARGE_FILES, _FILE_OFFSET_BITS and _LARGEFILE_SOURCE when
index b787474d108e8bc8ada95dd2b828cf5aac00af27..2ba9e82a80dde7b43f4055adcddc81d1a56de0f5 100644 (file)
@@ -2758,6 +2758,20 @@ ex_unletlock(
 
     do
     {
+       if (*arg == '$')
+       {
+           char_u    *name = ++arg;
+
+           if (get_env_len(&arg) == 0)
+           {
+               EMSG2(_(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);
index 6c77f4b0d2869304579b7f338503bb23b94fc34c..1ab55f0862823f1e517162fa7948d3c09a5a58fd 100644 (file)
@@ -4479,6 +4479,17 @@ remove_tail(char_u *p, char_u *pend, char_u *name)
     return pend;
 }
 
+    void
+vim_unsetenv(char_u *var)
+{
+#ifdef HAVE_UNSETENV
+    unsetenv((char *)var);
+#else
+    mch_setenv((char *)var, "", 0);
+#endif
+}
+
+
 /*
  * Our portable version of setenv.
  */
index 4e299e5f9ac3e548b75a4c064a4562523f5a90e7..4b613e6b1a707a529b6cc897d049e16a5a438179 100644 (file)
@@ -60,6 +60,7 @@ void expand_env(char_u *src, char_u *dst, int dstlen);
 void expand_env_esc(char_u *srcp, char_u *dst, int dstlen, int esc, int one, char_u *startstr);
 char_u *vim_getenv(char_u *name, int *mustfree);
 void vim_setenv(char_u *name, char_u *val);
+void vim_unsetenv(char_u *name);
 char_u *get_env_name(expand_T *xp, int idx);
 char_u *get_users(expand_T *xp, int idx);
 int match_user(char_u *name);
index c20b0beb7fcd43e52e2cf962285f50a618f37dcf..6636f6d66b87ae607cacc70000bf77ef42fd1379 100644 (file)
@@ -21,3 +21,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
index ac41ff30fb10dcd6bb18dc2502d39d5853e09df9..080ba72e38e981cf589374008818d3354e0bcf47 100644 (file)
@@ -761,6 +761,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1832,
 /**/
     1831,
 /**/