nano-devel
[Top][All Lists]
Advanced

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

[Nano-devel] [PATCH] editing: avoid creating blank lines when using auto


From: Benno Schulenberg
Subject: [Nano-devel] [PATCH] editing: avoid creating blank lines when using autoindent
Date: Tue, 4 Apr 2017 21:55:57 +0200

When Enter is pressed while the cursor is exactly on the current
indent width, remove the blank characters on that line to avoid
creating a line that consists only of trailing whitespace.

(When Enter is pressed somewhere in the middle of the blanks,
however, the whitespace is preserved.)

Suggested-by: Florian Zeitz <address@hidden>
---
 src/text.c | 21 +++++++++++++++------
 1 file changed, 15 insertions(+), 6 deletions(-)

diff --git a/src/text.c b/src/text.c
index 381480ef..d0ef40d6 100644
--- a/src/text.c
+++ b/src/text.c
@@ -768,11 +768,13 @@ void do_undo(void)
            break;
        }
        undidmsg = _("line break");
+       size_t from_x = (u->begin == 0) ? 0 : u->mark_begin_x;
+       size_t to_x = (u->begin == 0) ? u->mark_begin_x : u->begin;
        f->data = charealloc(f->data, strlen(f->data) +
-                               strlen(&f->next->data[u->mark_begin_x]) + 1);
-       strcat(f->data, &f->next->data[u->mark_begin_x]);
+                               strlen(&u->strdata[from_x]) + 1);
+       strcat(f->data, &u->strdata[from_x]);
        unlink_node(f->next);
-       goto_line_posx(u->lineno, u->begin);
+       goto_line_posx(u->lineno, to_x);
        break;
 #ifdef ENABLE_COMMENT
     case COMMENT:
@@ -982,12 +984,11 @@ void do_enter(void)
 {
     filestruct *newnode = make_new_node(openfile->current);
     size_t extra = 0;
+    bool allblanks = FALSE;
 
     assert(openfile->current != NULL && openfile->current->data != NULL);
 
 #ifndef NANO_TINY
-    add_undo(ENTER);
-
     if (ISSET(AUTOINDENT)) {
        extra = indent_length(openfile->current->data);
 
@@ -995,6 +996,8 @@ void do_enter(void)
         * indentation to the current x position. */
        if (extra > openfile->current_x)
            extra = openfile->current_x;
+       else if (extra == openfile->current_x)
+           allblanks = TRUE;
     }
 #endif
     newnode->data = charalloc(strlen(openfile->current->data +
@@ -1005,13 +1008,19 @@ void do_enter(void)
     if (ISSET(AUTOINDENT)) {
        /* Copy the whitespace from the current line to the new one. */
        strncpy(newnode->data, openfile->current->data, extra);
-       openfile->totsize += extra;
+       /* If there were only blanks before the cursor, trim them. */
+       if (allblanks)
+           openfile->current_x = 0;
+       else
+           openfile->totsize += extra;
     }
 #endif
 
     null_at(&openfile->current->data, openfile->current_x);
 
 #ifndef NANO_TINY
+    add_undo(ENTER);
+
     /* Adjust the mark if it was on the current line after the cursor. */
     if (openfile->mark_set && openfile->current == openfile->mark_begin &&
                openfile->current_x < openfile->mark_begin_x) {
-- 
2.12.1




reply via email to

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