From e2096868355341b8dcfc1a80f3a1ce680e951d69 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Tue, 10 Mar 2020 10:19:24 +0100 Subject: [PATCH] justify: give the first line of a marked region its proper indentation When justifying a selection from the middle of a paragraph, the first line of both that selection and of the new paragraph after it should get the same indentation as the first line of the original paragraph. This fulfills https://savannah.gnu.org/bugs/?56308. --- src/text.c | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/text.c b/src/text.c index bf7e0d04..2502e67c 100644 --- a/src/text.c +++ b/src/text.c @@ -1764,21 +1764,31 @@ void do_justify(bool full_justify) return; } - /* Copy the leading part that is to be used for the new paragraph. */ quot_len = quote_length(startline->data); lead_len = quot_len + indent_length(startline->data + quot_len); - the_lead = measured_copy(startline->data, lead_len); /* When the region starts IN the lead, take the whole lead. */ if (start_x <= lead_len) start_x = 0; + sampleline = startline; + + /* Find the first line of the paragraph in which the region starts. */ + while (sampleline->prev && inpar(sampleline) && !begpar(sampleline, 0)) + sampleline = sampleline->prev; + + /* Store the leading part that is to be used for the new paragraph. */ + quot_len = quote_length(sampleline->data); + lead_len = quot_len + indent_length(sampleline->data + quot_len); + the_lead = measured_copy(sampleline->data, lead_len); + /* Recede over any preceding whitespace. This effectively snips * trailing blanks from what will become the preceding paragraph. */ while (start_x > 0 && is_blank_mbchar(&startline->data[start_x - 1])) start_x--; - sampleline = (startline == endline) ? startline : startline->next; + if (sampleline->next && startline != endline) + sampleline = sampleline->next; /* Copy the leading part that is to be used for the new paragraph after * its first line (if any): the quoting of the first line, plus the @@ -1864,25 +1874,24 @@ void do_justify(bool full_justify) if (openfile->mark) { size_t line_len = strlen(cutbuffer->data); linestruct *line = cutbuffer; - size_t white_len; - char *afterlead; + size_t quot_len, fore_len; - /* If the marked region started in the middle of a line, and this line - * has a leading part, then prepend this same leading part also to the - * first line of the extracted region. */ - if (start_x > 0 && lead_len > 0) { + quot_len = quote_length(cutbuffer->data); + fore_len = quot_len + indent_length(cutbuffer->data + quot_len); + + /* If the extracted region begins with any leading part, trim it. */ + if (fore_len > 0) { + memmove(line->data, line->data + fore_len, line_len - fore_len + 1); + line_len -= fore_len; + } + + /* Then copy back in the leading part that it should have. */ + if (lead_len > 0) { cutbuffer->data = charealloc(cutbuffer->data, line_len + lead_len + 1); memmove(cutbuffer->data + lead_len, cutbuffer->data, line_len + 1); strncpy(cutbuffer->data, the_lead, lead_len); } - afterlead = cutbuffer->data + lead_len; - white_len = indent_length(afterlead); - - /* If the marked region started with whitespace, trim it. */ - if (white_len > 0) - memmove(afterlead, afterlead + white_len, line_len - white_len + 1); - /* Now justify the extracted region. */ concat_paragraph(cutbuffer, linecount); squeeze(cutbuffer, lead_len);