From 95fb88ddc71cdc5e1b11833923d82e93e69fa40c Mon Sep 17 00:00:00 2001 From: Florian Zeitz Date: Sat, 22 Oct 2016 10:13:58 +0200 Subject: [PATCH] text: Avoid creating indent-only lines in autoindent This avoids leaving indentation behind on otherwise empty lines. This is achieved by removing indentation when Enter is hit while current_x is at the end of the current lines indentation. This requires temporarily disabling autoindent, when creating whitespace-only lines is desired. Signed-off-by: Florian Zeitz --- src/text.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/text.c b/src/text.c index bdc4517..f9392a4 100644 --- a/src/text.c +++ b/src/text.c @@ -975,6 +975,9 @@ void do_enter() { filestruct *newnode = make_new_node(openfile->current); size_t extra = 0; +#ifndef NANO_TINY + size_t indent_len = 0; +#endif assert(openfile->current != NULL && openfile->current->data != NULL); @@ -986,7 +989,7 @@ void do_enter() /* If we are breaking the line in the indentation, the new * indentation should have only current_x characters, and * current_x should not change. */ - extra = indent_length(openfile->current->data); + extra = indent_len = indent_length(openfile->current->data); if (extra > openfile->current_x) extra = openfile->current_x; } @@ -999,9 +1002,17 @@ void do_enter() if (ISSET(AUTOINDENT)) { strncpy(newnode->data, openfile->current->data, extra); openfile->totsize += extra; + if (indent_len == openfile->current_x) { + null_at(&openfile->current->data, 0); + openfile->totsize -= indent_len; + } else + null_at(&openfile->current->data, openfile->current_x); + } else { + null_at(&openfile->current->data, openfile->current_x); } -#endif +#else null_at(&openfile->current->data, openfile->current_x); +#endif #ifndef NANO_TINY if (openfile->mark_set && openfile->current == openfile->mark_begin && openfile->current_x < openfile->mark_begin_x) { -- 2.10.1