aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Hahler <git@thequod.de>2019-07-30 08:00:36 +0200
committerJustin M. Keyes <justinkz@gmail.com>2019-08-06 01:17:32 +0200
commitfd66ad226221a22f223dcfca337e893d0a9cca46 (patch)
tree7638ea511f479ee980e1cf8c43b41d7afc98f9e9
parentb09e03c64d0f38a43b5ef068141bc86e365cd7fa (diff)
downloadrneovim-fd66ad226221a22f223dcfca337e893d0a9cca46.tar.gz
rneovim-fd66ad226221a22f223dcfca337e893d0a9cca46.tar.bz2
rneovim-fd66ad226221a22f223dcfca337e893d0a9cca46.zip
vim-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 vim/vim#2875) https://github.com/vim/vim/commit/691ddeefb545d8488e5a495af61caba2e57b3de9
-rw-r--r--runtime/doc/eval.txt44
-rw-r--r--runtime/doc/usr_41.txt4
-rw-r--r--src/nvim/eval.c77
-rw-r--r--src/nvim/eval.lua3
-rw-r--r--src/nvim/testdir/test_environ.vim44
5 files changed, 164 insertions, 8 deletions
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index 697e456817..5e8c5151f2 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -1180,6 +1180,13 @@ $VAR environment variable
The String value of any environment variable. When it is not defined, the
result is an empty string.
+
+The functions `getenv()` and `setenv()` can also be used and work for
+environment variables with non-alphanumeric names.
+The function `environ()` can be used to get a Dict with all environment
+variables.
+
+
*expr-env-expand*
Note that there is a difference between using $VAR directly and using
expand("$VAR"). Using it directly will only expand environment variables that
@@ -2061,6 +2068,7 @@ did_filetype() Number |TRUE| if FileType autocommand event used
diff_filler({lnum}) Number diff filler lines about {lnum}
diff_hlID({lnum}, {col}) Number diff highlighting at {lnum}/{col}
empty({expr}) Number |TRUE| if {expr} is empty
+environ() Dict return environment variables
escape({string}, {chars}) String escape {chars} in {string} with '\'
eval({string}) any evaluate {string} into its value
eventhandler() Number |TRUE| if inside an event handler
@@ -2118,6 +2126,7 @@ getcompletion({pat}, {type} [, {filtered}])
List list of cmdline completion matches
getcurpos() List position of the cursor
getcwd([{winnr} [, {tabnr}]]) String get the current working directory
+getenv({name}) String return environment variable
getfontname([{name}]) String name of font being used
getfperm({fname}) String file permissions of file {fname}
getfsize({fname}) Number size in bytes of file {fname}
@@ -2298,6 +2307,7 @@ setbufline( {expr}, {lnum}, {line})
setbufvar({expr}, {varname}, {val}) set {varname} in buffer {expr} to {val}
setcharsearch({dict}) Dict set character search from {dict}
setcmdpos({pos}) Number set cursor position in command-line
+setenv({name}, {val}) none set environment variable
setfperm({fname}, {mode} Number set {fname} file permissions to {mode}
setline({lnum}, {line}) Number set line {lnum} to {line}
setloclist({nr}, {list}[, {action}[, {what}]])
@@ -3344,6 +3354,14 @@ diff_hlID({lnum}, {col}) *diff_hlID()*
The highlight ID can be used with |synIDattr()| to obtain
syntax information about the highlighting.
+environ() *environ()*
+ Return all of environment variables as dictionary. You can
+ check if an environment variable exists like this: >
+ :echo has_key(environ(), 'HOME')
+< Note that the variable name may be CamelCase; to ignore case
+ use this: >
+ :echo index(keys(environ()), 'HOME', 0, 1) != -1
+
empty({expr}) *empty()*
Return the Number 1 if {expr} is empty, zero otherwise.
A |List| or |Dictionary| is empty when it does not have any
@@ -4333,14 +4351,11 @@ getcwd([{winnr}[, {tabnr}]]) *getcwd()*
< If {winnr} is -1 it is ignored, only the tab is resolved.
{winnr} can be the window number or the |window-ID|.
-
-getfsize({fname}) *getfsize()*
- The result is a Number, which is the size in bytes of the
- given file {fname}.
- If {fname} is a directory, 0 is returned.
- If the file {fname} can't be found, -1 is returned.
- If the size of {fname} is too big to fit in a Number then -2
- is returned.
+getenv({name}) *getenv()*
+ Return the value of environment variable {name}.
+ When the variable does not exist |v:null| is returned. That
+ is different from a variable set to an empty string.
+ See also |expr-env|.
getfontname([{name}]) *getfontname()*
Without an argument returns the name of the normal font being
@@ -4371,6 +4386,14 @@ getfperm({fname}) *getfperm()*
For setting permissions use |setfperm()|.
+getfsize({fname}) *getfsize()*
+ The result is a Number, which is the size in bytes of the
+ given file {fname}.
+ If {fname} is a directory, 0 is returned.
+ If the file {fname} can't be found, -1 is returned.
+ If the size of {fname} is too big to fit in a Number then -2
+ is returned.
+
getftime({fname}) *getftime()*
The result is a Number, which is the last modification time of
the given file {fname}. The value is measured as seconds
@@ -7079,6 +7102,11 @@ setcmdpos({pos}) *setcmdpos()*
Returns 0 when successful, 1 when not editing the command
line.
+setenv({name}, {val}) *setenv()*
+ Set environment variable {name} to {val}.
+ When {val} is |v:null| the environment variable is deleted.
+ See also |expr-env|.
+
setfperm({fname}, {mode}) *setfperm()* *chmod*
Set the file permissions for {fname} to {mode}.
{mode} must be a string with 9 characters. It is of the form
diff --git a/runtime/doc/usr_41.txt b/runtime/doc/usr_41.txt
index c806507c3e..b26b7cb646 100644
--- a/runtime/doc/usr_41.txt
+++ b/runtime/doc/usr_41.txt
@@ -770,6 +770,9 @@ System functions and manipulation of files:
rename() rename a file
system() get the result of a shell command as a string
systemlist() get the result of a shell command as a list
+ environ() get all environment variables
+ getenv() get one environment variable
+ setenv() set an environment variable
hostname() name of the system
readfile() read a file into a List of lines
readdir() get a List of file names in a directory
@@ -899,6 +902,7 @@ GUI: *gui-functions*
getwinposy() Y position of the Vim window
balloon_show() set the balloon content
balloon_split() split a message for a balloon
+ balloon_gettext() get the text in the balloon
Vim server: *server-functions*
serverlist() return the list of server names
diff --git a/src/nvim/eval.c b/src/nvim/eval.c
index d82a081c27..91c82a393e 100644
--- a/src/nvim/eval.c
+++ b/src/nvim/eval.c
@@ -8495,6 +8495,54 @@ static void f_empty(typval_T *argvars, typval_T *rettv, FunPtr fptr)
rettv->vval.v_number = n;
}
+/// "environ()" function
+static void f_environ(typval_T *argvars, typval_T *rettv, FunPtr fptr)
+{
+ int i = 0;
+ char_u *entry, *value;
+# ifdef WIN32
+ extern wchar_t **_wenviron;
+# else
+ extern char **environ;
+# endif
+
+ tv_dict_alloc_ret(rettv);
+
+# ifdef WIN32
+ if (*_wenviron == NULL) {
+ return;
+ }
+# else
+ if (*environ == NULL) {
+ return;
+ }
+# endif
+
+ for (i = 0; ; i++) {
+# ifdef WIN32
+ uint16_t *p;
+
+ if ((p = (uint16_t *)_wenviron[i]) == NULL) {
+ return;
+ }
+ entry = utf16_to_enc(p, NULL);
+# else
+ if ((entry = (char_u *)environ[i]) == NULL) {
+ return;
+ }
+ entry = vim_strsave(entry);
+# endif
+ if ((value = vim_strchr(entry, '=')) == NULL) {
+ xfree(entry);
+ continue;
+ }
+ *value++ = NUL;
+ tv_dict_add_str(rettv->vval.v_dict, (char *)entry, STRLEN((char *)entry),
+ (const char *)value);
+ xfree(entry);
+ }
+}
+
/*
* "escape({string}, {chars})" function
*/
@@ -8508,6 +8556,21 @@ static void f_escape(typval_T *argvars, typval_T *rettv, FunPtr fptr)
rettv->v_type = VAR_STRING;
}
+/// "getenv()" function
+static void f_getenv(typval_T *argvars, typval_T *rettv, FunPtr fptr)
+{
+ char_u *p = (char_u *)vim_getenv(tv_get_string(&argvars[0]));
+
+ if (p == NULL || *p == NUL) {
+ rettv->v_type = VAR_SPECIAL;
+ rettv->vval.v_number = kSpecialVarNull;
+ return;
+ }
+ p = vim_strsave(p);
+ rettv->vval.v_string = p;
+ rettv->v_type = VAR_STRING;
+}
+
/*
* "eval()" function
*/
@@ -15319,6 +15382,20 @@ static void f_setcmdpos(typval_T *argvars, typval_T *rettv, FunPtr fptr)
}
}
+/// "setenv()" function
+static void f_setenv(typval_T *argvars, typval_T *rettv, FunPtr fptr)
+{
+ char namebuf[NUMBUFLEN];
+ char valbuf[NUMBUFLEN];
+ const char *name = tv_get_string_buf(&argvars[0], namebuf);
+
+ if (argvars[1].v_type == VAR_SPECIAL
+ && argvars[1].vval.v_number == kSpecialVarNull) {
+ os_unsetenv(name);
+ } else {
+ vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf));
+ }
+}
/// "setfperm({fname}, {mode})" function
static void f_setfperm(typval_T *argvars, typval_T *rettv, FunPtr fptr)
diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua
index 0b77a24f7a..db45409e77 100644
--- a/src/nvim/eval.lua
+++ b/src/nvim/eval.lua
@@ -89,6 +89,7 @@ return {
diff_filler={args=1},
diff_hlID={args=2},
empty={args=1},
+ environ={},
escape={args=2},
eval={args=1},
eventhandler={},
@@ -135,6 +136,7 @@ return {
getcompletion={args={2, 3}},
getcurpos={},
getcwd={args={0,2}},
+ getenv={args={1}},
getfontname={args={0, 1}},
getfperm={args=1},
getfsize={args=1},
@@ -274,6 +276,7 @@ return {
setbufvar={args=3},
setcharsearch={args=1},
setcmdpos={args=1},
+ setenv={args=2},
setfperm={args=2},
setline={args=2},
setloclist={args={2, 4}},
diff --git a/src/nvim/testdir/test_environ.vim b/src/nvim/testdir/test_environ.vim
new file mode 100644
index 0000000000..094c4ce36f
--- /dev/null
+++ b/src/nvim/testdir/test_environ.vim
@@ -0,0 +1,44 @@
+scriptencoding utf-8
+
+func Test_environ()
+ unlet! $TESTENV
+ call assert_equal(0, has_key(environ(), 'TESTENV'))
+ let $TESTENV = 'foo'
+ call assert_equal(1, has_key(environ(), 'TESTENV'))
+ let $TESTENV = 'こんにちわ'
+ call assert_equal('こんにちわ', environ()['TESTENV'])
+endfunc
+
+func Test_getenv()
+ unlet! $TESTENV
+ call assert_equal(v:null, getenv('TESTENV'))
+ let $TESTENV = 'foo'
+ call assert_equal('foo', getenv('TESTENV'))
+endfunc
+
+func Test_setenv()
+ unlet! $TESTENV
+ call setenv('TEST ENV', 'foo')
+ call assert_equal('foo', getenv('TEST ENV'))
+ call setenv('TEST ENV', v:null)
+ call assert_equal(v:null, getenv('TEST ENV'))
+endfunc
+
+func Test_external_env()
+ call setenv('FOO', 'HelloWorld')
+ if has('win32')
+ let result = system('echo %FOO%')
+ else
+ let result = system('echo $FOO')
+ endif
+ let result = substitute(result, '[ \r\n]', '', 'g')
+ call assert_equal('HelloWorld', result)
+
+ call setenv('FOO', v:null)
+ if has('win32')
+ let result = system('set | grep ^FOO=')
+ else
+ let result = system('env | grep ^FOO=')
+ endif
+ call assert_equal('', result)
+endfunc