From 74593be462ef3c73ec92c36c39c6ce1eac5f18c5 Mon Sep 17 00:00:00 2001 From: Liu Hao Date: Tue, 5 Sep 2017 15:30:01 +0800 Subject: [PATCH 7/9] src/text.c: When unindenting lines, strip all leading spaces if they are shorter than a TAB, rather than do nothing. Signed-off-by: Liu Hao --- src/text.c | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/src/text.c b/src/text.c index b5ba75e6..27b12ad6 100644 --- a/src/text.c +++ b/src/text.c @@ -348,24 +348,28 @@ void do_indent(void) } /* If the given text starts with a tab's worth of whitespace, return the - * number of bytes this whitespace occupies. Otherwise, return zero. */ + * number of bytes this whitespace occupies. Otherwise, if there are + spaces at all, returns the number of bytes of the whitespace. + Otherwise, return zero. */ size_t length_of_white(const char *text) { - size_t bytes_of_white = 1; + size_t bytes = 0, width = 0; while (TRUE) { - if (*text == '\t') - return bytes_of_white; - - if (*text != ' ') - return 0; - - if (bytes_of_white == tabsize) - return tabsize; - - bytes_of_white++; - text++; + if (text[bytes] == '\t') { + width += tabsize; + ++bytes; + } else if (text[bytes] == ' ') { + width += 1; + ++bytes; + } else { + break; + } + if (width >= tabsize) { + break; + } } + return bytes; } /* Remove an indent from the given line. */ @@ -404,6 +408,7 @@ void do_unindent(void) { filestruct *top, *bot, *line; size_t top_x, bot_x; + int can_unindent; /* Use either all the marked lines or just the current line. */ if (openfile->mark) @@ -414,14 +419,19 @@ void do_unindent(void) bot = top; } - /* If any of the lines cannot be unindented and does not consist of - * only whitespace, we don't change anything. */ + /* If any of the lines cannot be unindented, we don't change + * anything. */ + can_unindent = FALSE; for (line = top; line != bot->next; line = line->next) { - if (length_of_white(line->data) == 0 && !white_string(line->data)) { - statusline(HUSH, _("Can unindent only by a full tab size")); - return; + if (length_of_white(line->data) != 0) { + can_unindent = TRUE; + break; } } + if (!can_unindent) { + statusline(HUSH, _("No line can be unindented")); + return; + } add_undo(UNINDENT); -- 2.15.0