Skip to content

Commit 883cf97

Browse files
committed
patch 8.2.2356: Vim9: ":put =expr" does not handle a list properly
Problem: Vim9: ":put =expr" does not handle a list properly. Solution: Use the same logic as eval_to_string_eap(). (closes #7684)
1 parent 97c6943 commit 883cf97

5 files changed

Lines changed: 47 additions & 26 deletions

File tree

src/eval.c

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,45 @@ skip_expr_concatenate(
466466
return res;
467467
}
468468

469+
/*
470+
* Convert "tv" to a string.
471+
* When "convert" is TRUE convert a List into a sequence of lines and convert
472+
* a Float to a String.
473+
* Returns an allocated string (NULL when out of memory).
474+
*/
475+
char_u *
476+
typval2string(typval_T *tv, int convert)
477+
{
478+
garray_T ga;
479+
char_u *retval;
480+
#ifdef FEAT_FLOAT
481+
char_u numbuf[NUMBUFLEN];
482+
#endif
483+
484+
if (convert && tv->v_type == VAR_LIST)
485+
{
486+
ga_init2(&ga, (int)sizeof(char), 80);
487+
if (tv->vval.v_list != NULL)
488+
{
489+
list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
490+
if (tv->vval.v_list->lv_len > 0)
491+
ga_append(&ga, NL);
492+
}
493+
ga_append(&ga, NUL);
494+
retval = (char_u *)ga.ga_data;
495+
}
496+
#ifdef FEAT_FLOAT
497+
else if (convert && tv->v_type == VAR_FLOAT)
498+
{
499+
vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
500+
retval = vim_strsave(numbuf);
501+
}
502+
#endif
503+
else
504+
retval = vim_strsave(tv_get_string(tv));
505+
return retval;
506+
}
507+
469508
/*
470509
* Top level evaluation function, returning a string. Does not handle line
471510
* breaks.
@@ -481,38 +520,14 @@ eval_to_string_eap(
481520
{
482521
typval_T tv;
483522
char_u *retval;
484-
garray_T ga;
485-
#ifdef FEAT_FLOAT
486-
char_u numbuf[NUMBUFLEN];
487-
#endif
488523
evalarg_T evalarg;
489524

490525
fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
491526
if (eval0(arg, &tv, NULL, &evalarg) == FAIL)
492527
retval = NULL;
493528
else
494529
{
495-
if (convert && tv.v_type == VAR_LIST)
496-
{
497-
ga_init2(&ga, (int)sizeof(char), 80);
498-
if (tv.vval.v_list != NULL)
499-
{
500-
list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
501-
if (tv.vval.v_list->lv_len > 0)
502-
ga_append(&ga, NL);
503-
}
504-
ga_append(&ga, NUL);
505-
retval = (char_u *)ga.ga_data;
506-
}
507-
#ifdef FEAT_FLOAT
508-
else if (convert && tv.v_type == VAR_FLOAT)
509-
{
510-
vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
511-
retval = vim_strsave(numbuf);
512-
}
513-
#endif
514-
else
515-
retval = vim_strsave(tv_get_string(&tv));
530+
retval = typval2string(&tv, convert);
516531
clear_tv(&tv);
517532
}
518533
clear_evalarg(&evalarg, NULL);

src/proto/eval.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ int eval_expr_to_bool(typval_T *expr, int *error);
1111
char_u *eval_to_string_skip(char_u *arg, exarg_T *eap, int skip);
1212
int skip_expr(char_u **pp, evalarg_T *evalarg);
1313
int skip_expr_concatenate(char_u **arg, char_u **start, char_u **end, evalarg_T *evalarg);
14+
char_u *typval2string(typval_T *tv, int convert);
1415
char_u *eval_to_string_eap(char_u *arg, int convert, exarg_T *eap);
1516
char_u *eval_to_string(char_u *arg, int convert);
1617
char_u *eval_to_string_safe(char_u *arg, int use_sandbox);

src/testdir/test_vim9_cmd.vim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,9 @@ def Test_put_command()
736736
assert_equal('above', getline(3))
737737
assert_equal('below', getline(4))
738738

739+
:2put =['a', 'b', 'c']
740+
assert_equal(['ppp', 'a', 'b', 'c', 'above'], getline(2, 6))
741+
739742
# compute range at runtime
740743
setline(1, range(1, 8))
741744
@a = 'aaa'

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,8 @@ static char *(features[]) =
750750

751751
static int included_patches[] =
752752
{ /* Add new patch number below this line */
753+
/**/
754+
2356,
753755
/**/
754756
2355,
755757
/**/

src/vim9execute.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3357,7 +3357,7 @@ call_def_function(
33573357
expr = tv->vval.v_string;
33583358
else
33593359
{
3360-
expr = typval_tostring(tv); // allocates value
3360+
expr = typval2string(tv, TRUE); // allocates value
33613361
clear_tv(tv);
33623362
}
33633363
--ectx.ec_stack.ga_len;

0 commit comments

Comments
 (0)