Skip to content

Instantly share code, notes, and snippets.

@mattn
Last active December 16, 2015 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattn/5449425 to your computer and use it in GitHub Desktop.
Save mattn/5449425 to your computer and use it in GitHub Desktop.
diff -r ef341d8811b2 runtime/doc/eval.txt
--- a/runtime/doc/eval.txt Mon Apr 15 22:22:58 2013 +0200
+++ b/runtime/doc/eval.txt Wed May 01 19:53:32 2013 +0900
@@ -2278,6 +2278,20 @@
{dict} is for functions with the "dict" attribute. It will be
used to set the local variable "self". |Dictionary-function|
+capture({command}) *capture()*
+ Capture output of {command}.
+ {command} is a |String|, it is passed as {command}.
+ {command} is a |List|, return all results of {command} of each
+ items.
+ Examples: >
+ let a = capture('echo "foo"')
+ echo a
+< foo >
+ let a = capture(['echo "foo"', 'echo "bar"'])
+ echo a
+< foobar >
+< This function is not available in the |sandbox|.
+
ceil({expr}) *ceil()*
Return the smallest integral value greater than or equal to
{expr} as a |Float| (round up).
diff -r ef341d8811b2 src/eval.c
--- a/src/eval.c Mon Apr 15 22:22:58 2013 +0200
+++ b/src/eval.c Wed May 01 19:53:32 2013 +0900
@@ -487,6 +487,7 @@
static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
+static void f_capture __ARGS((typval_T *argvars, typval_T *rettv));
#ifdef FEAT_FLOAT
static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
#endif
@@ -7868,6 +7869,7 @@
{"byte2line", 1, 1, f_byte2line},
{"byteidx", 2, 2, f_byteidx},
{"call", 2, 3, f_call},
+ {"capture", 1, 1, f_capture},
#ifdef FEAT_FLOAT
{"ceil", 1, 1, f_ceil},
#endif
@@ -9286,6 +9288,43 @@
(void)func_call(func, &argvars[1], selfdict, rettv);
}
+/*
+ * "capture(command)" function
+ */
+ static void
+f_capture(argvars, rettv)
+ typval_T *argvars;
+ typval_T *rettv;
+{
+ int save_msg_silent = msg_silent;
+ garray_T *save_capture_ga = capture_ga;
+
+ if (check_secure())
+ return;
+
+ capture_ga = alloc(sizeof(garray_T));
+ ga_init2(capture_ga, (int)sizeof(char), 80);
+
+ ++msg_silent;
+ if (argvars[0].v_type == VAR_LIST)
+ {
+ listitem_T *li;
+ for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
+ do_cmdline_cmd(get_tv_string(&li->li_tv));
+ }
+ else
+ do_cmdline_cmd(get_tv_string(&argvars[0]));
+ msg_silent = save_msg_silent;
+
+ ga_append(capture_ga, NUL);
+ rettv->v_type = VAR_STRING;
+ rettv->vval.v_string = vim_strsave((char_u*)capture_ga->ga_data);
+
+ ga_clear(capture_ga);
+ vim_free(capture_ga);
+ capture_ga = save_capture_ga;
+}
+
#ifdef FEAT_FLOAT
/*
* "ceil({float})" function
diff -r ef341d8811b2 src/globals.h
--- a/src/globals.h Mon Apr 15 22:22:58 2013 +0200
+++ b/src/globals.h Wed May 01 19:53:32 2013 +0900
@@ -1095,6 +1095,7 @@
EXTERN int redir_reg INIT(= 0); /* message redirection register */
EXTERN int redir_vname INIT(= 0); /* message redirection variable */
#endif
+extern garray_T *capture_ga INIT(= NULL); /* capture() buffer */
#ifdef FEAT_LANGMAP
EXTERN char_u langmap_mapchar[256]; /* mapping for language keys */
diff -r ef341d8811b2 src/message.c
--- a/src/message.c Mon Apr 15 22:22:58 2013 +0200
+++ b/src/message.c Wed May 01 19:53:32 2013 +0900
@@ -3101,6 +3101,18 @@
char_u *s = str;
static int cur_col = 0;
+ /* Append output to capture(). */
+ if (capture_ga)
+ {
+ int len = maxlen > 0 ? maxlen : STRLEN(str);
+ if (ga_grow(capture_ga, len) == OK)
+ {
+ mch_memmove((char *)capture_ga->ga_data
+ + capture_ga->ga_len, str, (size_t)len);
+ capture_ga->ga_len += len;
+ }
+ }
+
/* Don't do anything for displaying prompts and the like. */
if (redir_off)
return;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment