diff options
Diffstat (limited to 'src/nvim/eval.c')
-rw-r--r-- | src/nvim/eval.c | 52 |
1 files changed, 38 insertions, 14 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index a899dbcd3a..bedfe2d6d0 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -1521,7 +1521,9 @@ heredoc_get(exarg_T *eap, char_u *cmd) { char_u *marker; char_u *p; - int indent_len = 0; + int marker_indent_len = 0; + int text_indent_len = 0; + char_u *text_indent = NULL; if (eap->getline == NULL) { EMSG(_("E991: cannot use =<< here")); @@ -1534,14 +1536,16 @@ heredoc_get(exarg_T *eap, char_u *cmd) && (cmd[4] == NUL || ascii_iswhite(cmd[4]))) { cmd = skipwhite(cmd + 4); - // Trim the indentation from all the lines in the here document + // Trim the indentation from all the lines in the here document. // The amount of indentation trimmed is the same as the indentation of - // the :let command line. + // the first line after the :let command line. To find the end marker + // the indent of the :let command line is trimmed. p = *eap->cmdlinep; while (ascii_iswhite(*p)) { p++; - indent_len++; + marker_indent_len++; } + text_indent_len = -1; } // The marker is the next word. Default marker is "." @@ -1559,28 +1563,48 @@ heredoc_get(exarg_T *eap, char_u *cmd) list_T *l = tv_list_alloc(0); for (;;) { - int i = 0; + int mi = 0; + int ti = 0; char_u *theline = eap->getline(NUL, eap->cookie, 0, false); - if (theline != NULL && indent_len > 0) { - // trim the indent matching the first line - if (STRNCMP(theline, *eap->cmdlinep, indent_len) == 0) { - i = indent_len; - } - } - if (theline == NULL) { EMSG2(_("E990: Missing end marker '%s'"), marker); break; } - if (STRCMP(marker, theline + i) == 0) { + + // with "trim": skip the indent matching the :let line to find the + // marker + if (marker_indent_len > 0 + && STRNCMP(theline, *eap->cmdlinep, marker_indent_len) == 0) { + mi = marker_indent_len; + } + if (STRCMP(marker, theline + mi) == 0) { xfree(theline); break; } + if (text_indent_len == -1 && *theline != NUL) { + // set the text indent from the first line. + p = theline; + text_indent_len = 0; + while (ascii_iswhite(*p)) { + p++; + text_indent_len++; + } + text_indent = vim_strnsave(theline, text_indent_len); + } + // with "trim": skip the indent matching the first line + if (text_indent != NULL) { + for (ti = 0; ti < text_indent_len; ti++) { + if (theline[ti] != text_indent[ti]) { + break; + } + } + } - tv_list_append_string(l, (char *)(theline + i), -1); + tv_list_append_string(l, (char *)(theline + ti), -1); xfree(theline); } + xfree(text_indent); return l; } |