From 80669c34bb7317803ac4e71715e9d3d1e1dce640 Mon Sep 17 00:00:00 2001 From: David Lawrence Ramsey Date: Fri, 5 May 2006 15:41:43 +0000 Subject: [PATCH] in do_indent_marked(), only use line_indent and line_indent_len when necessary git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@3470 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- src/text.c | 96 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 53 insertions(+), 43 deletions(-) diff --git a/src/text.c b/src/text.c index 440ebb89..8a6d2ce6 100644 --- a/src/text.c +++ b/src/text.c @@ -205,9 +205,9 @@ void do_indent_marked(ssize_t cols) /* Whether any indenting or unindenting was done. */ bool unindent = FALSE; /* Whether we're unindenting text. */ - char *line_indent; + char *line_indent = NULL; /* The text added to each line in order to indent it. */ - size_t line_indent_len; + size_t line_indent_len = 0; /* The length of the text added to each line in order to indent * it. */ filestruct *top, *bot, *f; @@ -242,39 +242,70 @@ void do_indent_marked(ssize_t cols) mark_order((const filestruct **)&top, &top_x, (const filestruct **)&bot, &bot_x, NULL); - /* Set up the text we'll be using as indentation. */ - line_indent = charalloc(cols + 1); + if (!unindent) { + /* Set up the text we'll be using as indentation. */ + line_indent = charalloc(cols + 1); - if (ISSET(TABS_TO_SPACES)) { - /* Set the indentation to cols spaces. */ - charset(line_indent, ' ', cols); - line_indent_len = cols; - } else { - /* Set the indentation to (cols / tabsize) tabs and (cols % - * tabsize) spaces. */ - size_t num_tabs = cols / tabsize; - size_t num_spaces = cols % tabsize; + if (ISSET(TABS_TO_SPACES)) { + /* Set the indentation to cols spaces. */ + charset(line_indent, ' ', cols); + line_indent_len = cols; + } else { + /* Set the indentation to (cols / tabsize) tabs and (cols % + * tabsize) spaces. */ + size_t num_tabs = cols / tabsize; + size_t num_spaces = cols % tabsize; - charset(line_indent, '\t', num_tabs); - charset(line_indent + num_tabs, ' ', num_spaces); + charset(line_indent, '\t', num_tabs); + 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 marked text. */ for (f = top; f != bot->next; f = f->next) { size_t line_len = strlen(f->data); size_t indent_len = indent_length(f->data); - if (unindent) { + if (!unindent) { + /* If we're indenting, add the characters in line_indent to + * the beginning of the non-whitespace text of this line. */ + f->data = charealloc(f->data, line_len + + line_indent_len + 1); + charmove(&f->data[indent_len + line_indent_len], + &f->data[indent_len], line_len - indent_len + 1); + strncpy(f->data + indent_len, line_indent, line_indent_len); + openfile->totsize += line_indent_len; + + /* Keep track of the change in the current line. */ + if (f == openfile->mark_begin && openfile->mark_begin_x >= + indent_len) + openfile->mark_begin_x += line_indent_len; + + if (f == openfile->current && openfile->current_x >= + indent_len) + openfile->current_x += line_indent_len; + + /* If the NO_NEWLINES flag isn't set, and this is the + * magicline, add a new magicline. */ + if (!ISSET(NO_NEWLINES) && f == openfile->filebot) + new_magicline(); + } else { size_t indent_col = strnlenpt(f->data, indent_len); + /* The length in columns of the indentation on this + * line. */ if (cols <= indent_col) { size_t indent_new = actual_x(f->data, indent_col - cols); + /* The length of the indentation remaining on + * this line after we unindent. */ size_t indent_shift = indent_len - indent_new; + /* The change in the indentation on this line + * after we unindent. */ /* If we're unindenting, and there's at least cols * columns' worth of indentation at the beginning of the @@ -305,33 +336,12 @@ void do_indent_marked(ssize_t cols) if (!indent_changed) indent_changed = TRUE; } - } else { - /* If we're indenting, add the characters in line_indent to - * the beginning of the non-whitespace text of this line. */ - f->data = charealloc(f->data, line_len + - line_indent_len + 1); - charmove(&f->data[indent_len + line_indent_len], - &f->data[indent_len], line_len - indent_len + 1); - strncpy(f->data + indent_len, line_indent, line_indent_len); - openfile->totsize += line_indent_len; - - /* Keep track of the change in the current line. */ - if (f == openfile->mark_begin && openfile->mark_begin_x >= - indent_len) - openfile->mark_begin_x += line_indent_len; - - if (f == openfile->current && openfile->current_x >= - indent_len) - openfile->current_x += line_indent_len; - - /* If the NO_NEWLINES flag isn't set, and this is the - * magicline, add a new magicline. */ - if (!ISSET(NO_NEWLINES) && f == openfile->filebot) - new_magicline(); } } - free(line_indent); + /* Clean up. */ + if (!unindent) + free(line_indent); if (indent_changed) { /* Mark the file as modified. */ -- 2.39.5