diff options
author | Scott Prager <splinterofchaos@gmail.com> | 2014-10-28 13:27:47 -0400 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-11-07 13:34:56 -0300 |
commit | fd36dc208e2ded0173209163424cbadc15329558 (patch) | |
tree | 5b75c3c30fd0064b430810e0b701e7855887e8fa /src | |
parent | e90973e0359b782a6e3ad4997258d9ca42495c2f (diff) | |
download | rneovim-fd36dc208e2ded0173209163424cbadc15329558.tar.gz rneovim-fd36dc208e2ded0173209163424cbadc15329558.tar.bz2 rneovim-fd36dc208e2ded0173209163424cbadc15329558.zip |
job: Let vimL jobsend() accept a list.
Use save_tv_as_string(), same as vimL system(). This also makes
jobsend() more liberal in what it can accept. For example,
`jobsend(j, 123)` is now valid.
Closes #1176
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/eval.c | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index ae40a10937..59363a3608 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -10593,9 +10593,9 @@ static void f_jobsend(typval_T *argvars, typval_T *rettv) return; } - if (argvars[0].v_type != VAR_NUMBER || argvars[1].v_type != VAR_STRING) { - // First argument is the job id and second is the string to write to - // the job's stdin + if (argvars[0].v_type != VAR_NUMBER || argvars[1].v_type == VAR_UNKNOWN) { + // First argument is the job id and second is the string or list to write + // to the job's stdin EMSG(_(e_invarg)); return; } @@ -10608,10 +10608,15 @@ static void f_jobsend(typval_T *argvars, typval_T *rettv) return; } - WBuffer *buf = wstream_new_buffer(xstrdup((char *)argvars[1].vval.v_string), - strlen((char *)argvars[1].vval.v_string), - 1, - free); + ssize_t input_len; + char *input = (char *) save_tv_as_string(&argvars[1], &input_len, true); + if (input_len < 0) { + return; // Error handled by save_tv_as_string(). + } else if (input_len == 0) { + return; // Not an error, but nothing to do. + } + + WBuffer *buf = wstream_new_buffer(input, input_len, 1, free); rettv->vval.v_number = job_write(job, buf); } @@ -14469,7 +14474,7 @@ static void get_system_output_as_rettv(typval_T *argvars, typval_T *rettv, // get input to the shell command (if any), and its length ssize_t input_len; - char *input = (char *) save_tv_as_string(&argvars[1], &input_len); + char *input = (char *) save_tv_as_string(&argvars[1], &input_len, false); if (input_len == -1) { return; } @@ -15158,9 +15163,10 @@ static bool write_list(FILE *fd, list_T *list, bool binary) /// /// @param[in] tv A value to store as a string. /// @param[out] len The length of the resulting string or -1 on error. +/// @param[in] endnl If true, the output will end in a newline (if a list). /// @returns an allocated string if `tv` represents a VimL string, list, or /// number; NULL otherwise. -static char_u *save_tv_as_string(typval_T *tv, ssize_t *len) +static char_u *save_tv_as_string(typval_T *tv, ssize_t *len, bool endnl) FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL { if (tv->v_type == VAR_UNKNOWN) { @@ -15192,13 +15198,13 @@ static char_u *save_tv_as_string(typval_T *tv, ssize_t *len) return NULL; } - char_u *ret = xmalloc(*len); + char_u *ret = xmalloc(*len + endnl); char_u *end = ret; for (listitem_T *li = list->lv_first; li != NULL; li = li->li_next) { for (char_u *s = get_tv_string(&li->li_tv); *s != NUL; s++) { *end++ = (*s == '\n') ? NUL : *s; } - if (li->li_next != NULL) { + if (endnl || li->li_next != NULL) { *end++ = '\n'; } } |