From 8fce33afe074bb769eb3bf73d8d15582463a027b Mon Sep 17 00:00:00 2001 From: Benno Schulenberg Date: Fri, 26 Apr 2019 10:37:04 +0200 Subject: [PATCH] text: copy leading quote characters when automatic hard-wrapping occurs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the user is typing a long text and --breaklonglines is in effect, then any leading comment or quoting characters are automatically added to each automatic new line. This fulfills https://savannah.gnu.org/bugs/?56042. Requested-by: Sébastien Desreux --- src/proto.h | 1 + src/text.c | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/proto.h b/src/proto.h index 8fb5ce8c..7b58b093 100644 --- a/src/proto.h +++ b/src/proto.h @@ -533,6 +533,7 @@ ssize_t break_line(const char *line, ssize_t goal, bool snap_at_nl); size_t indent_length(const char *line); #endif #ifdef ENABLE_JUSTIFY +size_t quote_length(const char *line); bool begpar(const linestruct *const foo, int depth); bool inpar(const linestruct *const foo); void do_justify(bool full_justify); diff --git a/src/text.c b/src/text.c index 24eb0788..be125f0e 100644 --- a/src/text.c +++ b/src/text.c @@ -1426,6 +1426,13 @@ bool do_wrap(void) add_undo(SPLIT_BEGIN); #endif +#ifdef ENABLE_JUSTIFY + bool autowhite = ISSET(AUTOINDENT); + size_t lead_len = quote_length(line->data); + + if (lead_len > 0) + UNSET(AUTOINDENT); +#endif /* The remainder is the text that will be wrapped to the next line. */ remainder = line->data + wrap_loc; @@ -1455,10 +1462,19 @@ bool do_wrap(void) #endif } - /* Join the next line to this one, and delete any extra blanks. */ - do { + /* Join the next line to this one. */ + do_delete(); + +#ifdef ENABLE_JUSTIFY + /* If the quoting part of the current line equals the quoting part of + * what was the next line, then strip this second quoting part. */ + if (strncmp(line->data, line->data + openfile->current_x, lead_len) == 0) + for (size_t i = lead_len; i > 0; i--) + do_delete(); +#endif + /* Remove any extra blanks. */ + while (is_blank_mbchar(&line->data[openfile->current_x])) do_delete(); - } while (is_blank_mbchar(&line->data[openfile->current_x])); } /* Go to the wrap location. */ @@ -1480,6 +1496,27 @@ bool do_wrap(void) /* Now split the line. */ do_enter(); +#ifdef ENABLE_JUSTIFY + /* If the original line has quoting, copy it to the spillage line. */ + if (lead_len > 0) { + lead_len += indent_length(line->data + lead_len); + + line = line->next; + line_len = strlen(line->data); + line->data = charealloc(line->data, lead_len + line_len + 1); + + charmove(line->data + lead_len, line->data, line_len + 1); + strncpy(line->data, line->prev->data, lead_len); + + openfile->current_x += lead_len; +#ifndef NANO_TINY + update_undo(ENTER); +#endif + if (autowhite) + SET(AUTOINDENT); + } +#endif + openfile->spillage_line = openfile->current; if (cursor_x < wrap_loc) {