systemlist: add keepempty option to preserve final newline

This commit is contained in:
Björn Linse
2014-11-27 21:30:17 +01:00
parent ecf1e672e1
commit 1464b0eda2
3 changed files with 28 additions and 6 deletions

View File

@@ -6188,11 +6188,12 @@ system({expr} [, {input}]) *system()* *E677*
Use |:checktime| to force a check. Use |:checktime| to force a check.
systemlist({expr} [, {input}]) *systemlist()* systemlist({expr} [, {input} [, {keepempty}]]) *systemlist()*
Same as |system()|, but returns a |List| with lines (parts of Same as |system()|, but returns a |List| with lines (parts of
output separated by NL) with NULs transformed into NLs. Output output separated by NL) with NULs transformed into NLs. Output
is the same as |readfile()| will output with {binary} argument is the same as |readfile()| will output with {binary} argument
set to "b". set to "b", except that a final newline is not preserved,
unless {keepempty} is present and it's non-zero.
Returns an empty string on error, so be careful not to run Returns an empty string on error, so be careful not to run
into |E706|. into |E706|.

View File

@@ -6593,7 +6593,7 @@ static struct fst {
{"synconcealed", 2, 2, f_synconcealed}, {"synconcealed", 2, 2, f_synconcealed},
{"synstack", 2, 2, f_synstack}, {"synstack", 2, 2, f_synstack},
{"system", 1, 2, f_system}, {"system", 1, 2, f_system},
{"systemlist", 1, 2, f_systemlist}, {"systemlist", 1, 3, f_systemlist},
{"tabpagebuflist", 0, 1, f_tabpagebuflist}, {"tabpagebuflist", 0, 1, f_tabpagebuflist},
{"tabpagenr", 0, 1, f_tabpagenr}, {"tabpagenr", 0, 1, f_tabpagenr},
{"tabpagewinnr", 1, 2, f_tabpagewinnr}, {"tabpagewinnr", 1, 2, f_tabpagewinnr},
@@ -14523,7 +14523,7 @@ static void f_synstack(typval_T *argvars, typval_T *rettv)
} }
} }
static list_T* string_to_list(char_u *str, size_t len) static list_T* string_to_list(char_u *str, size_t len, bool keepempty)
{ {
list_T *list = list_alloc(); list_T *list = list_alloc();
@@ -14543,6 +14543,11 @@ static list_T* string_to_list(char_u *str, size_t len)
list_append(list, li); list_append(list, li);
} }
// Optionally retain final newline, if present
if (keepempty && str[len-1] == NL) {
list_append_string(list, (char_u*)"", 0);
}
return list; return list;
} }
@@ -14585,7 +14590,11 @@ static void get_system_output_as_rettv(typval_T *argvars, typval_T *rettv,
} }
if (retlist) { if (retlist) {
rettv->vval.v_list = string_to_list((char_u *) res, nread); int keepempty = 0;
if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN) {
keepempty = get_tv_number(&argvars[2]);
}
rettv->vval.v_list = string_to_list((char_u *) res, nread, keepempty != 0);
rettv->vval.v_list->lv_refcount++; rettv->vval.v_list->lv_refcount++;
rettv->v_type = VAR_LIST; rettv->v_type = VAR_LIST;
@@ -19723,7 +19732,7 @@ static void apply_job_autocmds(int id, char *name, char *type,
str_slot->li_tv.v_type = VAR_LIST; str_slot->li_tv.v_type = VAR_LIST;
str_slot->li_tv.v_lock = 0; str_slot->li_tv.v_lock = 0;
str_slot->li_tv.vval.v_list = str_slot->li_tv.vval.v_list =
string_to_list((char_u *) received, received_len); string_to_list((char_u *) received, received_len, false);
str_slot->li_tv.vval.v_list->lv_refcount++; str_slot->li_tv.vval.v_list->lv_refcount++;
list_append(list, str_slot); list_append(list, str_slot);

View File

@@ -188,6 +188,18 @@ describe('systemlist()', function()
end) end)
end) end)
describe('when keepempty option is', function()
it('0, ignores trailing newline', function()
eq({'aa','bb'}, eval("systemlist('cat',['aa','bb'],0)"))
eq({'aa','bb'}, eval("systemlist('cat',['aa','bb',''],0)"))
end)
it('1, preserves trailing newline', function()
eq({'aa','bb'}, eval("systemlist('cat',['aa','bb'],1)"))
eq({'aa','bb',''}, eval("systemlist('cat',['aa','bb',''],2)"))
end)
end)
if xclip then if xclip then
describe("with a program that doesn't close stdout", function() describe("with a program that doesn't close stdout", function()
it('will exit properly after passing input', function() it('will exit properly after passing input', function()