nano-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Nano-devel] [PATCH] text: copy leading quote characters when automatic


From: Benno Schulenberg
Subject: [Nano-devel] [PATCH] text: copy leading quote characters when automatic hard-wrapping occurs
Date: Sat, 27 Apr 2019 17:54:03 +0200

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 <address@hidden>

[If this is a useful feature, then the question is: should this be
the standard behavior, or should it be behind an option, or should
it be the default behavior that can be disabled with an option?]
---
 src/text.c | 44 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 41 insertions(+), 3 deletions(-)

diff --git a/src/text.c b/src/text.c
index 84a05aab..999c5124 100644
--- a/src/text.c
+++ b/src/text.c
@@ -1407,6 +1407,9 @@ void update_undo(undo_type action)
 }
 #endif /* !NANO_TINY */
 
+// Temporary forward declaration.
+size_t quote_length(const char *line);
+
 #ifdef ENABLE_WRAPPING
 /* When the current line is overlong, hard-wrap it at the furthest possible
  * whitespace character, and (if possible) prepend the remainder of the line
@@ -1446,6 +1449,12 @@ bool do_wrap(void)
                return FALSE;
 
        add_undo(SPLIT_BEGIN);
+
+       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. */
@@ -1476,10 +1485,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();
+
+#ifndef NANO_TINY
+               /* 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. */
@@ -1501,6 +1519,26 @@ bool do_wrap(void)
        /* Now split the line. */
        do_enter();
 
+#ifndef NANO_TINY
+       /* 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;
+               update_undo(ENTER);
+
+               if (autowhite)
+                       SET(AUTOINDENT);
+       }
+#endif
+
        openfile->spillage_line = openfile->current;
 
        if (cursor_x < wrap_loc) {
-- 
2.20.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]