Skip to content

Commit 691ddee

Browse files
committed
patch 8.1.1305: there is no easy way to manipulate environment variables
Problem: There is no easy way to manipulate environment variables. Solution: Add environ(), getenv() and setenv(). (Yasuhiro Matsumoto, closes #2875)
1 parent 68cbb14 commit 691ddee

File tree

6 files changed

+185
-7
lines changed

6 files changed

+185
-7
lines changed

runtime/doc/eval.txt

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,6 +1369,13 @@ $VAR environment variable
13691369

13701370
The String value of any environment variable. When it is not defined, the
13711371
result is an empty string.
1372+
1373+
The functions `getenv()` and `setenv()` can also be used and work for
1374+
environment variables with non-alphanumeric names.
1375+
The function `environ()` can be used to get a Dict with all environment
1376+
variables.
1377+
1378+
13721379
*expr-env-expand*
13731380
Note that there is a difference between using $VAR directly and using
13741381
expand("$VAR"). Using it directly will only expand environment variables that
@@ -2303,6 +2310,7 @@ did_filetype() Number |TRUE| if FileType autocmd event used
23032310
diff_filler({lnum}) Number diff filler lines about {lnum}
23042311
diff_hlID({lnum}, {col}) Number diff highlighting at {lnum}/{col}
23052312
empty({expr}) Number |TRUE| if {expr} is empty
2313+
environ() Dict return environment variables
23062314
escape({string}, {chars}) String escape {chars} in {string} with '\'
23072315
eval({string}) any evaluate {string} into its value
23082316
eventhandler() Number |TRUE| if inside an event handler
@@ -2360,6 +2368,7 @@ getcompletion({pat}, {type} [, {filtered}])
23602368
List list of cmdline completion matches
23612369
getcurpos() List position of the cursor
23622370
getcwd([{winnr} [, {tabnr}]]) String get the current working directory
2371+
getenv({name}) String return environment variable
23632372
getfontname([{name}]) String name of font being used
23642373
getfperm({fname}) String file permissions of file {fname}
23652374
getfsize({fname}) Number size in bytes of file {fname}
@@ -2568,6 +2577,7 @@ setbufvar({expr}, {varname}, {val})
25682577
none set {varname} in buffer {expr} to {val}
25692578
setcharsearch({dict}) Dict set character search from {dict}
25702579
setcmdpos({pos}) Number set cursor position in command-line
2580+
setenv({name}, {val}) none set environment variable
25712581
setfperm({fname}, {mode}) Number set {fname} file permissions to {mode}
25722582
setline({lnum}, {line}) Number set line {lnum} to {line}
25732583
setloclist({nr}, {list} [, {action} [, {what}]])
@@ -3905,6 +3915,14 @@ diff_hlID({lnum}, {col}) *diff_hlID()*
39053915
The highlight ID can be used with |synIDattr()| to obtain
39063916
syntax information about the highlighting.
39073917

3918+
environ() *environ()*
3919+
Return all of environment variables as dictionary. You can
3920+
check if an environment variable exists like this: >
3921+
:echo has_key(environ(), 'HOME')
3922+
< Note that the variable name may be CamelCase; to ignore case
3923+
use this: >
3924+
:echo index(keys(environ()), 'HOME', 0, 1) != -1
3925+
39083926
empty({expr}) *empty()*
39093927
Return the Number 1 if {expr} is empty, zero otherwise.
39103928
- A |List| or |Dictionary| is empty when it does not have any
@@ -4970,13 +4988,11 @@ getcwd([{winnr} [, {tabnr}]])
49704988
" Get the working directory of current tabpage
49714989
:echo getcwd(-1, 0)
49724990
<
4973-
getfsize({fname}) *getfsize()*
4974-
The result is a Number, which is the size in bytes of the
4975-
given file {fname}.
4976-
If {fname} is a directory, 0 is returned.
4977-
If the file {fname} can't be found, -1 is returned.
4978-
If the size of {fname} is too big to fit in a Number then -2
4979-
is returned.
4991+
getenv({name}) *getenv()*
4992+
Return the value of environment variable {name}.
4993+
When the variable does not exist |v:null| is returned. That
4994+
is different from a variable set to an empty string.
4995+
See also |expr-env|.
49804996

49814997
getfontname([{name}]) *getfontname()*
49824998
Without an argument returns the name of the normal font being
@@ -5009,6 +5025,14 @@ getfperm({fname}) *getfperm()*
50095025

50105026
For setting permissions use |setfperm()|.
50115027

5028+
getfsize({fname}) *getfsize()*
5029+
The result is a Number, which is the size in bytes of the
5030+
given file {fname}.
5031+
If {fname} is a directory, 0 is returned.
5032+
If the file {fname} can't be found, -1 is returned.
5033+
If the size of {fname} is too big to fit in a Number then -2
5034+
is returned.
5035+
50125036
getftime({fname}) *getftime()*
50135037
The result is a Number, which is the last modification time of
50145038
the given file {fname}. The value is measured as seconds
@@ -8012,6 +8036,11 @@ setcmdpos({pos}) *setcmdpos()*
80128036
Returns 0 when successful, 1 when not editing the command
80138037
line.
80148038

8039+
setenv({name}, {val}) *setenv()*
8040+
Set environment variable {name} to {val}.
8041+
When {val} is |v:null| the environment variable is deleted.
8042+
See also |expr-env|.
8043+
80158044
setfperm({fname}, {mode}) *setfperm()* *chmod*
80168045
Set the file permissions for {fname} to {mode}.
80178046
{mode} must be a string with 9 characters. It is of the form

runtime/doc/usr_41.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,9 @@ System functions and manipulation of files:
774774
rename() rename a file
775775
system() get the result of a shell command as a string
776776
systemlist() get the result of a shell command as a list
777+
environ() get all environment variables
778+
getenv() get one environment variable
779+
setenv() set an environment variable
777780
hostname() name of the system
778781
readfile() read a file into a List of lines
779782
readdir() get a List of file names in a directory
@@ -903,6 +906,7 @@ GUI: *gui-functions*
903906
getwinposy() Y position of the Vim window
904907
balloon_show() set the balloon content
905908
balloon_split() split a message for a balloon
909+
balloon_gettext() get the text in the balloon
906910

907911
Vim server: *server-functions*
908912
serverlist() return the list of server names

src/evalfunc.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ static void f_did_filetype(typval_T *argvars, typval_T *rettv);
137137
static void f_diff_filler(typval_T *argvars, typval_T *rettv);
138138
static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
139139
static void f_empty(typval_T *argvars, typval_T *rettv);
140+
static void f_environ(typval_T *argvars, typval_T *rettv);
140141
static void f_escape(typval_T *argvars, typval_T *rettv);
141142
static void f_eval(typval_T *argvars, typval_T *rettv);
142143
static void f_eventhandler(typval_T *argvars, typval_T *rettv);
@@ -187,6 +188,7 @@ static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
187188
static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
188189
static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
189190
static void f_getcwd(typval_T *argvars, typval_T *rettv);
191+
static void f_getenv(typval_T *argvars, typval_T *rettv);
190192
static void f_getfontname(typval_T *argvars, typval_T *rettv);
191193
static void f_getfperm(typval_T *argvars, typval_T *rettv);
192194
static void f_getfsize(typval_T *argvars, typval_T *rettv);
@@ -365,6 +367,7 @@ static void f_setbufline(typval_T *argvars, typval_T *rettv);
365367
static void f_setbufvar(typval_T *argvars, typval_T *rettv);
366368
static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
367369
static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
370+
static void f_setenv(typval_T *argvars, typval_T *rettv);
368371
static void f_setfperm(typval_T *argvars, typval_T *rettv);
369372
static void f_setline(typval_T *argvars, typval_T *rettv);
370373
static void f_setloclist(typval_T *argvars, typval_T *rettv);
@@ -629,6 +632,7 @@ static struct fst
629632
{"diff_filler", 1, 1, f_diff_filler},
630633
{"diff_hlID", 2, 2, f_diff_hlID},
631634
{"empty", 1, 1, f_empty},
635+
{"environ", 0, 0, f_environ},
632636
{"escape", 2, 2, f_escape},
633637
{"eval", 1, 1, f_eval},
634638
{"eventhandler", 0, 0, f_eventhandler},
@@ -681,6 +685,7 @@ static struct fst
681685
#endif
682686
{"getcurpos", 0, 0, f_getcurpos},
683687
{"getcwd", 0, 2, f_getcwd},
688+
{"getenv", 1, 1, f_getenv},
684689
{"getfontname", 0, 1, f_getfontname},
685690
{"getfperm", 1, 1, f_getfperm},
686691
{"getfsize", 1, 1, f_getfsize},
@@ -873,6 +878,7 @@ static struct fst
873878
{"setbufvar", 3, 3, f_setbufvar},
874879
{"setcharsearch", 1, 1, f_setcharsearch},
875880
{"setcmdpos", 1, 1, f_setcmdpos},
881+
{"setenv", 2, 2, f_setenv},
876882
{"setfperm", 2, 2, f_setfperm},
877883
{"setline", 2, 2, f_setline},
878884
{"setloclist", 2, 4, f_setloclist},
@@ -3339,6 +3345,59 @@ f_empty(typval_T *argvars, typval_T *rettv)
33393345
rettv->vval.v_number = n;
33403346
}
33413347

3348+
/*
3349+
* "environ()" function
3350+
*/
3351+
static void
3352+
f_environ(typval_T *argvars UNUSED, typval_T *rettv)
3353+
{
3354+
#if !defined(AMIGA)
3355+
int i = 0;
3356+
char_u *entry, *value;
3357+
# ifdef MSWIN
3358+
extern wchar_t **_wenviron;
3359+
# else
3360+
extern char **environ;
3361+
# endif
3362+
3363+
if (rettv_dict_alloc(rettv) != OK)
3364+
return;
3365+
3366+
# ifdef MSWIN
3367+
if (*_wenviron == NULL)
3368+
return;
3369+
# else
3370+
if (*environ == NULL)
3371+
return;
3372+
# endif
3373+
3374+
for (i = 0; ; ++i)
3375+
{
3376+
# ifdef MSWIN
3377+
short_u *p;
3378+
3379+
if ((p = (short_u *)_wenviron[i]) == NULL)
3380+
return;
3381+
entry = utf16_to_enc(p, NULL);
3382+
# else
3383+
if ((entry = (char_u *)environ[i]) == NULL)
3384+
return;
3385+
entry = vim_strsave(entry);
3386+
# endif
3387+
if (entry == NULL) // out of memory
3388+
return;
3389+
if ((value = vim_strchr(entry, '=')) == NULL)
3390+
{
3391+
vim_free(entry);
3392+
continue;
3393+
}
3394+
*value++ = NUL;
3395+
dict_add_string(rettv->vval.v_dict, (char *)entry, value);
3396+
vim_free(entry);
3397+
}
3398+
#endif
3399+
}
3400+
33423401
/*
33433402
* "escape({string}, {chars})" function
33443403
*/
@@ -5260,6 +5319,27 @@ f_getcwd(typval_T *argvars, typval_T *rettv)
52605319
#endif
52615320
}
52625321

5322+
/*
5323+
* "getenv()" function
5324+
*/
5325+
static void
5326+
f_getenv(typval_T *argvars, typval_T *rettv)
5327+
{
5328+
int mustfree = FALSE;
5329+
char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree);
5330+
5331+
if (p == NULL)
5332+
{
5333+
rettv->v_type = VAR_SPECIAL;
5334+
rettv->vval.v_number = VVAL_NULL;
5335+
return;
5336+
}
5337+
if (!mustfree)
5338+
p = vim_strsave(p);
5339+
rettv->vval.v_string = p;
5340+
rettv->v_type = VAR_STRING;
5341+
}
5342+
52635343
/*
52645344
* "getfontname()" function
52655345
*/
@@ -11424,6 +11504,23 @@ f_setcmdpos(typval_T *argvars, typval_T *rettv)
1142411504
rettv->vval.v_number = set_cmdline_pos(pos);
1142511505
}
1142611506

11507+
/*
11508+
* "setenv()" function
11509+
*/
11510+
static void
11511+
f_setenv(typval_T *argvars, typval_T *rettv UNUSED)
11512+
{
11513+
char_u namebuf[NUMBUFLEN];
11514+
char_u valbuf[NUMBUFLEN];
11515+
char_u *name = tv_get_string_buf(&argvars[0], namebuf);
11516+
11517+
if (argvars[1].v_type == VAR_SPECIAL
11518+
&& argvars[1].vval.v_number == VVAL_NULL)
11519+
vim_unsetenv(name);
11520+
else
11521+
vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
11522+
}
11523+
1142711524
/*
1142811525
* "setfperm({fname}, {mode})" function
1142911526
*/

src/testdir/Make_all.mak

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ NEW_TESTS = \
104104
test_erasebackword \
105105
test_escaped_glob \
106106
test_eval_stuff \
107+
test_environ \
107108
test_ex_equal \
108109
test_ex_undo \
109110
test_ex_z \
@@ -320,6 +321,7 @@ NEW_TESTS_RES = \
320321
test_digraph.res \
321322
test_display.res \
322323
test_edit.res \
324+
test_environ.res \
323325
test_erasebackword.res \
324326
test_escaped_glob.res \
325327
test_eval_stuff.res \

src/testdir/test_environ.vim

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
scriptencoding utf-8
2+
3+
func Test_environ()
4+
unlet! $TESTENV
5+
call assert_equal(0, has_key(environ(), 'TESTENV'))
6+
let $TESTENV = 'foo'
7+
call assert_equal(1, has_key(environ(), 'TESTENV'))
8+
let $TESTENV = 'こんにちわ'
9+
call assert_equal('こんにちわ', environ()['TESTENV'])
10+
endfunc
11+
12+
func Test_getenv()
13+
unlet! $TESTENV
14+
call assert_equal(v:null, getenv('TESTENV'))
15+
let $TESTENV = 'foo'
16+
call assert_equal('foo', getenv('TESTENV'))
17+
endfunc
18+
19+
func Test_setenv()
20+
unlet! $TESTENV
21+
call setenv('TEST ENV', 'foo')
22+
call assert_equal('foo', getenv('TEST ENV'))
23+
call setenv('TEST ENV', v:null)
24+
call assert_equal(v:null, getenv('TEST ENV'))
25+
endfunc
26+
27+
func Test_external_env()
28+
call setenv('FOO', 'HelloWorld')
29+
if has('win32')
30+
let result = system('echo %FOO%')
31+
else
32+
let result = system('echo $FOO')
33+
endif
34+
let result = substitute(result, '[ \r\n]', '', 'g')
35+
call assert_equal('HelloWorld', result)
36+
37+
call setenv('FOO', v:null)
38+
if has('win32')
39+
let result = system('set | grep ^FOO=')
40+
else
41+
let result = system('env | grep ^FOO=')
42+
endif
43+
call assert_equal('', result)
44+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,8 @@ static char *(features[]) =
767767

768768
static int included_patches[] =
769769
{ /* Add new patch number below this line */
770+
/**/
771+
1305,
770772
/**/
771773
1304,
772774
/**/

0 commit comments

Comments
 (0)