summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2016-07-07 17:33:02 +0200
committerBram Moolenaar <Bram@vim.org>2016-07-07 17:33:02 +0200
commit1e5e1231ac9e1ba9678812c96f9d554a078eeec4 (patch)
tree21ea01576e37017525dd70dc9f640ad04c5f9456
parent9d5d3c9c4468ad76f16b50eabd3d9e7eab2ed44d (diff)
patch 7.4.1996v7.4.1996
Problem: Capturing the output of a command takes a few commands. Solution: Add evalcmd().
-rw-r--r--runtime/doc/eval.txt10
-rw-r--r--src/Makefile1
-rw-r--r--src/eval.c39
-rw-r--r--src/testdir/test_alot.vim3
-rw-r--r--src/testdir/test_evalcmd.vim8
-rw-r--r--src/version.c2
6 files changed, 62 insertions, 1 deletions
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index 2e6e08a360..4b55f911d0 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -1961,6 +1961,7 @@ diff_hlID({lnum}, {col}) Number diff highlighting at {lnum}/{col}
empty({expr}) Number |TRUE| if {expr} is empty
escape({string}, {chars}) String escape {chars} in {string} with '\'
eval({string}) any evaluate {string} into its value
+evalcmd({command}) String execute {command} and get the output
eventhandler() Number |TRUE| if inside an event handler
executable({expr}) Number 1 if executable {expr} exists
exepath({expr}) String full path of the command {expr}
@@ -3231,6 +3232,15 @@ eval({string}) Evaluate {string} and return the result. Especially useful to
them. Also works for |Funcref|s that refer to existing
functions.
+evalcmd({command}) *evalcmd()*
+ Execute Ex {command} and return the output as a string. This
+ is equivalent to: >
+ redir => var
+ {command}
+ redir END
+< To get a list of lines use: >
+ split(evalcmd(cmd), "\n")
+
eventhandler() *eventhandler()*
Returns 1 when inside an event handler. That is that Vim got
interrupted while waiting for the user to type a character,
diff --git a/src/Makefile b/src/Makefile
index 6add935913..13bf776914 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -2023,6 +2023,7 @@ test_arglist \
test_cmdline \
test_cursor_func \
test_delete \
+ test_evalcmd \
test_ex_undo \
test_expand \
test_expand_dllpath \
diff --git a/src/eval.c b/src/eval.c
index 69238c1813..74ed263d51 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -555,6 +555,7 @@ static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
static void f_empty(typval_T *argvars, typval_T *rettv);
static void f_escape(typval_T *argvars, typval_T *rettv);
static void f_eval(typval_T *argvars, typval_T *rettv);
+static void f_evalcmd(typval_T *argvars, typval_T *rettv);
static void f_eventhandler(typval_T *argvars, typval_T *rettv);
static void f_executable(typval_T *argvars, typval_T *rettv);
static void f_exepath(typval_T *argvars, typval_T *rettv);
@@ -1133,6 +1134,7 @@ set_internal_string_var(char_u *name, char_u *value)
}
static lval_T *redir_lval = NULL;
+#define EVALCMD_BUSY (redir_lval == (lval_T *)&redir_lval)
static garray_T redir_ga; /* only valid when redir_lval is not NULL */
static char_u *redir_endp = NULL;
static char_u *redir_varname = NULL;
@@ -1250,6 +1252,12 @@ var_redir_stop(void)
{
typval_T tv;
+ if (EVALCMD_BUSY)
+ {
+ redir_lval = NULL;
+ return;
+ }
+
if (redir_lval != NULL)
{
/* If there was no error: assign the text to the variable. */
@@ -8556,6 +8564,7 @@ static struct fst
{"empty", 1, 1, f_empty},
{"escape", 2, 2, f_escape},
{"eval", 1, 1, f_eval},
+ {"evalcmd", 1, 1, f_evalcmd},
{"eventhandler", 0, 0, f_eventhandler},
{"executable", 1, 1, f_executable},
{"exepath", 1, 1, f_exepath},
@@ -11337,6 +11346,36 @@ f_eval(typval_T *argvars, typval_T *rettv)
}
/*
+ * "evalcmd()" function
+ */
+ static void
+f_evalcmd(typval_T *argvars, typval_T *rettv)
+{
+ char_u *s;
+
+ rettv->vval.v_string = NULL;
+ rettv->v_type = VAR_STRING;
+
+ s = get_tv_string_chk(&argvars[0]);
+ if (s != NULL)
+ {
+ redir_vname = TRUE;
+ redir_lval = (lval_T *)&redir_lval;
+ ga_init2(&redir_ga, (int)sizeof(char), 500);
+
+ if (do_cmdline_cmd(s) == OK)
+ rettv->vval.v_string = redir_ga.ga_data;
+ else
+ vim_free(redir_ga.ga_data);
+
+ redir_ga.ga_data = NULL;
+ redir_vname = FALSE;
+ redir_lval = NULL;
+ }
+
+}
+
+/*
* "eventhandler()" function
*/
static void
diff --git a/src/testdir/test_alot.vim b/src/testdir/test_alot.vim
index e99e137640..074c2dec91 100644
--- a/src/testdir/test_alot.vim
+++ b/src/testdir/test_alot.vim
@@ -5,9 +5,10 @@ source test_assign.vim
source test_autocmd.vim
source test_cursor_func.vim
source test_delete.vim
+source test_evalcmd.vim
source test_ex_undo.vim
-source test_expr.vim
source test_expand.vim
+source test_expr.vim
source test_expand_dllpath.vim
source test_feedkeys.vim
source test_fnamemodify.vim
diff --git a/src/testdir/test_evalcmd.vim b/src/testdir/test_evalcmd.vim
new file mode 100644
index 0000000000..e7f48bcfd3
--- /dev/null
+++ b/src/testdir/test_evalcmd.vim
@@ -0,0 +1,8 @@
+" test evalcmd()
+
+func Test_evalcmd()
+ call assert_equal("\nnocompatible", evalcmd('set compatible?'))
+ call assert_equal("\nsomething\nnice", evalcmd('echo "something\nnice"'))
+ call assert_fails('call evalcmd("doesnotexist")', 'E492:')
+endfunc
+
diff --git a/src/version.c b/src/version.c
index f34f7a0a79..b6b9e59eb2 100644
--- a/src/version.c
+++ b/src/version.c
@@ -759,6 +759,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 1996,
+/**/
1995,
/**/
1994,