Commit eb937f81 authored by Benno Schulenberg's avatar Benno Schulenberg
Browse files

text: allow unindenting when not all lines are indented


And also allow it when lines are only partially indented.

This makes it possible to equalize the indentations of (accidentally)
unevenly indented lines: by first fully unindenting a group of lines,
and then reindenting them to the desired amount.
Suggested-by: default avatarLiu Hao <lh_mouse@126.com>
parent 5144162a
Showing with 13 additions and 13 deletions
+13 -13
...@@ -347,23 +347,22 @@ void do_indent(void) ...@@ -347,23 +347,22 @@ void do_indent(void)
refresh_needed = TRUE; refresh_needed = TRUE;
} }
/* If the given text starts with a tab's worth of whitespace, return the /* Return the number of bytes of whitespace at the start of the given text,
* number of bytes this whitespace occupies. Otherwise, return zero. */ * but at most a tab's worth. */
size_t length_of_white(const char *text) size_t length_of_white(const char *text)
{ {
size_t bytes_of_white = 1; size_t bytes_of_white = 0;
while (TRUE) { while (TRUE) {
if (*text == '\t') if (*text == '\t')
return bytes_of_white; return ++bytes_of_white;
if (*text != ' ') if (*text != ' ')
return 0; return bytes_of_white;
if (bytes_of_white == tabsize) if (++bytes_of_white == tabsize)
return tabsize; return tabsize;
bytes_of_white++;
text++; text++;
} }
} }
...@@ -414,15 +413,16 @@ void do_unindent(void) ...@@ -414,15 +413,16 @@ void do_unindent(void)
bot = top; bot = top;
} }
/* If any of the lines cannot be unindented and does not consist of /* Check if there is a line that can be unindented. */
* only whitespace, we don't change anything. */
for (line = top; line != bot->next; line = line->next) { for (line = top; line != bot->next; line = line->next) {
if (length_of_white(line->data) == 0 && !white_string(line->data)) { if (length_of_white(line->data) != 0)
statusline(HUSH, _("Can unindent only by a full tab size")); break;
return;
}
} }
/* If none of the lines can be unindented, there is nothing to do. */
if (line == bot->next)
return;
add_undo(UNINDENT); add_undo(UNINDENT);
/* Go through each of the lines and remove their leading indent. */ /* Go through each of the lines and remove their leading indent. */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment