Commit d02c1993 authored by David Lawrence Ramsey's avatar David Lawrence Ramsey Committed by Benno Schulenberg
Browse files

text: normalize the indentation in do_indent() and do_unindent()

Also remove one unneeded blank line.
No related merge requests found
Showing with 75 additions and 76 deletions
+75 -76
...@@ -304,68 +304,68 @@ void do_indent(ssize_t cols) ...@@ -304,68 +304,68 @@ void do_indent(ssize_t cols)
bot = top; bot = top;
} }
/* Set up the text we'll be using as indentation. */ /* Set up the text we'll be using as indentation. */
line_indent = charalloc(cols + 1); line_indent = charalloc(cols + 1);
if (ISSET(TABS_TO_SPACES)) { if (ISSET(TABS_TO_SPACES)) {
/* Set the indentation to cols spaces. */ /* Set the indentation to cols spaces. */
charset(line_indent, ' ', cols); charset(line_indent, ' ', cols);
line_indent_len = cols; line_indent_len = cols;
} else { } else {
/* Set the indentation to (cols / tabsize) tabs and (cols % /* Set the indentation to (cols / tabsize) tabs and (cols %
* tabsize) spaces. */ * tabsize) spaces. */
size_t num_tabs = cols / tabsize; size_t num_tabs = cols / tabsize;
size_t num_spaces = cols % tabsize; size_t num_spaces = cols % tabsize;
charset(line_indent, '\t', num_tabs); charset(line_indent, '\t', num_tabs);
charset(line_indent + num_tabs, ' ', num_spaces); charset(line_indent + num_tabs, ' ', num_spaces);
line_indent_len = num_tabs + num_spaces; line_indent_len = num_tabs + num_spaces;
} }
line_indent[line_indent_len] = '\0'; line_indent[line_indent_len] = '\0';
/* Go through each line of the text. */ /* Go through each line of the text. */
for (f = top; f != bot->next; f = f->next) { for (f = top; f != bot->next; f = f->next) {
size_t line_len = strlen(f->data); size_t line_len = strlen(f->data);
size_t indent_len = indent_length(f->data); size_t indent_len = indent_length(f->data);
/* If we're indenting, add the characters in line_indent to /* If we're indenting, add the characters in line_indent to
* the beginning of the non-whitespace text of this line. */ * the beginning of the non-whitespace text of this line. */
f->data = charealloc(f->data, line_len + line_indent_len + 1); f->data = charealloc(f->data, line_len + line_indent_len + 1);
charmove(&f->data[indent_len + line_indent_len], charmove(&f->data[indent_len + line_indent_len],
&f->data[indent_len], line_len - indent_len + 1); &f->data[indent_len], line_len - indent_len + 1);
strncpy(f->data + indent_len, line_indent, line_indent_len); strncpy(f->data + indent_len, line_indent, line_indent_len);
openfile->totsize += line_indent_len; openfile->totsize += line_indent_len;
/* Keep track of the change in the current line. */ /* Keep track of the change in the current line. */
if (openfile->mark_set && f == openfile->mark_begin && if (openfile->mark_set && f == openfile->mark_begin &&
openfile->mark_begin_x >= indent_len) openfile->mark_begin_x >= indent_len)
openfile->mark_begin_x += line_indent_len; openfile->mark_begin_x += line_indent_len;
if (f == openfile->current && openfile->current_x >= indent_len) { if (f == openfile->current && openfile->current_x >= indent_len) {
openfile->current_x += line_indent_len; openfile->current_x += line_indent_len;
openfile->placewewant = xplustabs(); openfile->placewewant = xplustabs();
} }
/* If the NO_NEWLINES flag isn't set, and this is the /* If the NO_NEWLINES flag isn't set, and this is the
* magicline, add a new magicline. */ * magicline, add a new magicline. */
if (!ISSET(NO_NEWLINES) && f == openfile->filebot) if (!ISSET(NO_NEWLINES) && f == openfile->filebot)
new_magicline(); new_magicline();
} }
/* Clean up. */ /* Clean up. */
free(line_indent); free(line_indent);
/* Throw away the undo stack, to prevent making mistakes when /* Throw away the undo stack, to prevent making mistakes when
* the user tries to undo something in the reindented text. */ * the user tries to undo something in the reindented text. */
discard_until(NULL, openfile); discard_until(NULL, openfile);
/* Mark the file as modified. */ /* Mark the file as modified. */
set_modified(); set_modified();
/* Update the screen. */ /* Update the screen. */
refresh_needed = TRUE; refresh_needed = TRUE;
} }
/* Indent the current line, or all lines covered by the mark if the mark /* Indent the current line, or all lines covered by the mark if the mark
...@@ -403,47 +403,46 @@ void do_unindent(ssize_t cols) ...@@ -403,47 +403,46 @@ void do_unindent(ssize_t cols)
for (f = top; f != bot->next; f = f->next) { for (f = top; f != bot->next; f = f->next) {
size_t line_len = strlen(f->data); size_t line_len = strlen(f->data);
size_t indent_len = indent_length(f->data); size_t indent_len = indent_length(f->data);
size_t indent_col = strnlenpt(f->data, indent_len);
size_t indent_col = strnlenpt(f->data, indent_len);
/* The length in columns of the indentation on this line. */ /* The length in columns of the indentation on this line. */
if (cols <= indent_col) { if (cols <= indent_col) {
size_t indent_new = actual_x(f->data, indent_col - cols); size_t indent_new = actual_x(f->data, indent_col - cols);
/* The length of the indentation remaining on /* The length of the indentation remaining on
* this line after we unindent. */ * this line after we unindent. */
size_t indent_shift = indent_len - indent_new; size_t indent_shift = indent_len - indent_new;
/* The change in the indentation on this line /* The change in the indentation on this line
* after we unindent. */ * after we unindent. */
/* If we're unindenting, and there's at least cols /* If we're unindenting, and there's at least cols
* columns' worth of indentation at the beginning of the * columns' worth of indentation at the beginning of the
* non-whitespace text of this line, remove it. */ * non-whitespace text of this line, remove it. */
charmove(&f->data[indent_new], &f->data[indent_len], charmove(&f->data[indent_new], &f->data[indent_len],
line_len - indent_shift - indent_new + 1); line_len - indent_shift - indent_new + 1);
null_at(&f->data, line_len - indent_shift + 1); null_at(&f->data, line_len - indent_shift + 1);
openfile->totsize -= indent_shift; openfile->totsize -= indent_shift;
/* Keep track of the change in the current line. */ /* Keep track of the change in the current line. */
if (openfile->mark_set && f == openfile->mark_begin && if (openfile->mark_set && f == openfile->mark_begin &&
openfile->mark_begin_x > indent_new) { openfile->mark_begin_x > indent_new) {
if (openfile->mark_begin_x <= indent_len) if (openfile->mark_begin_x <= indent_len)
openfile->mark_begin_x = indent_new; openfile->mark_begin_x = indent_new;
else else
openfile->mark_begin_x -= indent_shift; openfile->mark_begin_x -= indent_shift;
} }
if (f == openfile->current && if (f == openfile->current &&
openfile->current_x > indent_new) { openfile->current_x > indent_new) {
if (openfile->current_x <= indent_len) if (openfile->current_x <= indent_len)
openfile->current_x = indent_new; openfile->current_x = indent_new;
else else
openfile->current_x -= indent_shift; openfile->current_x -= indent_shift;
openfile->placewewant = xplustabs(); openfile->placewewant = xplustabs();
}
/* We've unindented, so the indentation changed. */
indent_changed = TRUE;
} }
/* We've unindented, so the indentation changed. */
indent_changed = TRUE;
}
} }
if (indent_changed) { if (indent_changed) {
......
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