@@ -631,8 +631,12 @@ get_function_body(
631631 char_u * skip_until = NULL ;
632632 int ret = FAIL ;
633633 int is_heredoc = FALSE;
634+ int heredoc_concat_len = 0 ;
635+ garray_T heredoc_ga ;
634636 char_u * heredoc_trimmed = NULL ;
635637
638+ ga_init2 (& heredoc_ga , 1 , 500 );
639+
636640 // Detect having skipped over comment lines to find the return
637641 // type. Add NULL lines to keep the line count correct.
638642 sourcing_lnum_off = get_sourced_lnum (eap -> getline , eap -> cookie );
@@ -733,6 +737,20 @@ get_function_body(
733737 getline_options = vim9_function
734738 ? GETLINE_CONCAT_CONTBAR : GETLINE_CONCAT_CONT ;
735739 is_heredoc = FALSE;
740+
741+ if (heredoc_concat_len > 0 )
742+ {
743+ // Replace the starting line with all the concatenated
744+ // lines.
745+ ga_concat (& heredoc_ga , theline );
746+ vim_free (((char_u * * )(newlines -> ga_data ))[
747+ heredoc_concat_len - 1 ]);
748+ ((char_u * * )(newlines -> ga_data ))[
749+ heredoc_concat_len - 1 ] = heredoc_ga .ga_data ;
750+ ga_init (& heredoc_ga );
751+ heredoc_concat_len = 0 ;
752+ theline += STRLEN (theline ); // skip the "EOF"
753+ }
736754 }
737755 }
738756 }
@@ -886,6 +904,8 @@ get_function_body(
886904 skip_until = vim_strnsave (p , skiptowhite (p ) - p );
887905 getline_options = GETLINE_NONE ;
888906 is_heredoc = TRUE;
907+ if (eap -> cmdidx == CMD_def )
908+ heredoc_concat_len = newlines -> ga_len + 1 ;
889909 }
890910
891911 // Check for ":cmd v =<< [trim] EOF"
@@ -928,10 +948,21 @@ get_function_body(
928948 if (ga_grow (newlines , 1 + sourcing_lnum_off ) == FAIL )
929949 goto theend ;
930950
931- // Copy the line to newly allocated memory. get_one_sourceline()
932- // allocates 250 bytes per line, this saves 80% on average. The cost
933- // is an extra alloc/free.
934- p = vim_strsave (theline );
951+ if (heredoc_concat_len > 0 )
952+ {
953+ // For a :def function "python << EOF" concatenats all the lines,
954+ // to be used for the instruction later.
955+ ga_concat (& heredoc_ga , theline );
956+ ga_concat (& heredoc_ga , (char_u * )"\n" );
957+ p = vim_strsave ((char_u * )"" );
958+ }
959+ else
960+ {
961+ // Copy the line to newly allocated memory. get_one_sourceline()
962+ // allocates 250 bytes per line, this saves 80% on average. The
963+ // cost is an extra alloc/free.
964+ p = vim_strsave (theline );
965+ }
935966 if (p == NULL )
936967 goto theend ;
937968 ((char_u * * )(newlines -> ga_data ))[newlines -> ga_len ++ ] = p ;
@@ -953,6 +984,7 @@ get_function_body(
953984theend :
954985 vim_free (skip_until );
955986 vim_free (heredoc_trimmed );
987+ vim_free (heredoc_ga .ga_data );
956988 need_wait_return |= saved_wait_return ;
957989 return ret ;
958990}
@@ -1436,6 +1468,7 @@ deref_func_name(
14361468
14371469 cc = name [* lenp ];
14381470 name [* lenp ] = NUL ;
1471+
14391472 v = find_var (name , & ht , no_autoload );
14401473 name [* lenp ] = cc ;
14411474 if (v != NULL )
0 commit comments