@@ -779,8 +779,10 @@ heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
779779 int eval_failed = FALSE;
780780 cctx_T * cctx = vim9compile ? eap -> cookie : NULL ;
781781 int count = 0 ;
782+ int heredoc_in_string = FALSE;
783+ char_u * line_arg = NULL ;
782784
783- if (eap -> ea_getline == NULL )
785+ if (eap -> ea_getline == NULL && vim_strchr ( cmd , '\n' ) == NULL )
784786 {
785787 emsg (_ (e_cannot_use_heredoc_here ));
786788 return NULL ;
@@ -824,8 +826,14 @@ heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
824826 if (* cmd != NUL && * cmd != comment_char )
825827 {
826828 marker = skipwhite (cmd );
827- p = skiptowhite (marker );
828- if (* skipwhite (p ) != NUL && * skipwhite (p ) != comment_char )
829+ p = skiptowhite_or_nl (marker );
830+ if (* p == NL )
831+ {
832+ // heredoc in a string
833+ line_arg = p + 1 ;
834+ heredoc_in_string = TRUE;
835+ }
836+ else if (* skipwhite (p ) != NUL && * skipwhite (p ) != comment_char )
829837 {
830838 semsg (_ (e_trailing_characters_str ), p );
831839 return NULL ;
@@ -859,12 +867,38 @@ heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
859867 int mi = 0 ;
860868 int ti = 0 ;
861869
862- vim_free (theline );
863- theline = eap -> ea_getline (NUL , eap -> cookie , 0 , FALSE);
864- if (theline == NULL )
870+ if (heredoc_in_string )
865871 {
866- semsg (_ (e_missing_end_marker_str ), marker );
867- break ;
872+ char_u * next_line ;
873+
874+ // heredoc in a string separated by newlines. Get the next line
875+ // from the string.
876+
877+ if (* line_arg == NUL )
878+ {
879+ semsg (_ (e_missing_end_marker_str ), marker );
880+ break ;
881+ }
882+
883+ theline = line_arg ;
884+ next_line = vim_strchr (theline , '\n' );
885+ if (next_line == NULL )
886+ line_arg += STRLEN (line_arg );
887+ else
888+ {
889+ * next_line = NUL ;
890+ line_arg = next_line + 1 ;
891+ }
892+ }
893+ else
894+ {
895+ vim_free (theline );
896+ theline = eap -> ea_getline (NUL , eap -> cookie , 0 , FALSE);
897+ if (theline == NULL )
898+ {
899+ semsg (_ (e_missing_end_marker_str ), marker );
900+ break ;
901+ }
868902 }
869903
870904 // with "trim": skip the indent matching the :let line to find the
@@ -911,6 +945,8 @@ heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
911945 }
912946 else
913947 {
948+ int free_str = FALSE;
949+
914950 if (evalstr && !eap -> skip )
915951 {
916952 str = eval_all_expr_in_str (str );
@@ -920,15 +956,20 @@ heredoc_get(exarg_T *eap, char_u *cmd, int script_get, int vim9compile)
920956 eval_failed = TRUE;
921957 continue ;
922958 }
923- vim_free (theline );
924- theline = str ;
959+ free_str = TRUE;
925960 }
926961
927962 if (list_append_string (l , str , -1 ) == FAIL )
928963 break ;
964+ if (free_str )
965+ vim_free (str );
929966 }
930967 }
931- vim_free (theline );
968+ if (heredoc_in_string )
969+ // Next command follows the heredoc in the string.
970+ eap -> nextcmd = line_arg ;
971+ else
972+ vim_free (theline );
932973 vim_free (text_indent );
933974
934975 if (vim9compile && cctx -> ctx_skip != SKIP_YES && !eval_failed )
0 commit comments