Commit 34fd9915 authored by Caleb C. Sander's avatar Caleb C. Sander
Browse files

Make do_indent, do_unindent, and do_enter clearer

No related merge requests found
Showing with 43 additions and 42 deletions
+43 -42
......@@ -129,17 +129,17 @@ void indent_a_line(filestruct *line, char *indentation)
void do_indent(void)
{
char *indentation;
filestruct *top, *bot, *line;
filestruct *line, *bot;
/* Use either all the marked lines or just the current line. */
get_range((const filestruct **)&top, (const filestruct **)&bot);
get_range((const filestruct **)&line, (const filestruct **)&bot);
/* Skip any leading empty lines. */
while (top != bot->next && top->data[0] == '\0')
top = top->next;
while (line != bot->next && line->data[0] == '\0')
line = line->next;
/* If all lines are empty, there is nothing to do. */
if (top == bot->next)
if (line == bot->next)
return;
indentation = charalloc(tabsize + 1);
......@@ -149,19 +149,22 @@ void do_indent(void)
charset(indentation, ' ', tabsize);
indentation[tabsize] = '\0';
} else {
indentation[0] = '\t';
indentation[1] = '\0';
strcpy(indentation, "\t");
}
add_undo(INDENT);
/* Go through each of the lines, adding an indent to the non-empty ones,
* and recording whatever was added in the undo item. */
for (line = top; line != bot->next; line = line->next) {
char *real_indent = (line->data[0] == '\0') ? "" : indentation;
indent_a_line(line, real_indent);
update_multiline_undo(line->lineno, real_indent);
while (line != bot->next) {
if (line->data[0] == '\0') {
update_multiline_undo(line->lineno, "");
}
else {
indent_a_line(line, indentation);
update_multiline_undo(line->lineno, indentation);
}
line = line->next;
}
free(indentation);
......@@ -232,33 +235,34 @@ void unindent_a_line(filestruct *line, size_t indent_len)
* The removed indent can be a mixture of spaces plus at most one tab. */
void do_unindent(void)
{
filestruct *top, *bot, *line;
filestruct *line, *bot;
/* Use either all the marked lines or just the current line. */
get_range((const filestruct **)&top, (const filestruct **)&bot);
get_range((const filestruct **)&line, (const filestruct **)&bot);
/* Skip any leading lines that cannot be unindented. */
while (top != bot->next && length_of_white(top->data) == 0)
top = top->next;
while (line != bot->next && !length_of_white(line->data))
line = line->next;
/* If none of the lines can be unindented, there is nothing to do. */
if (top == bot->next)
if (line == bot->next)
return;
add_undo(UNINDENT);
/* Go through each of the lines, removing their leading indent where
* possible, and saving the removed whitespace in the undo item. */
for (line = top; line != bot->next; line = line->next) {
while (line != bot->next) {
size_t indent_len = length_of_white(line->data);
char *indentation = mallocstrncpy(NULL, line->data, indent_len + 1);
unindent_a_line(line, indent_len);
char *indentation = charalloc(indent_len + 1);
strncpy(indentation, line->data, indent_len);
indentation[indent_len] = '\0';
unindent_a_line(line, indent_len);
update_multiline_undo(line->lineno, indentation);
free(indentation);
line = line->next;
}
set_modified();
......@@ -866,7 +870,7 @@ void do_redo(void)
void do_enter(void)
{
filestruct *newnode = make_new_node(openfile->current);
size_t extra = 0;
size_t indent_chars = 0;
#ifndef NANO_TINY
filestruct *sampleline = openfile->current;
bool allblanks = FALSE;
......@@ -879,27 +883,24 @@ void do_enter(void)
inpar(sampleline->next) && !begpar(sampleline->next, 0))
sampleline = sampleline->next;
#endif
extra = indent_length(sampleline->data);
indent_chars = indent_length(sampleline->data);
/* When breaking in the indentation, limit the automatic one. */
if (extra > openfile->current_x)
extra = openfile->current_x;
else if (extra == openfile->current_x)
if (indent_chars > openfile->current_x)
indent_chars = openfile->current_x;
else if (indent_chars == openfile->current_x)
allblanks = TRUE;
}
#endif /* NANO_TINY */
newnode->data = charalloc(strlen(openfile->current->data +
openfile->current_x) + extra + 1);
strcpy(&newnode->data[extra], openfile->current->data +
openfile->current_x);
char *current_rest = &openfile->current->data[openfile->current_x];
newnode->data = charalloc(indent_chars + strlen(current_rest) + 1);
strcpy(&newnode->data[indent_chars], current_rest);
#ifndef NANO_TINY
if (ISSET(AUTOINDENT)) {
/* Copy the whitespace from the sample line to the new one. */
strncpy(newnode->data, sampleline->data, extra);
/* If there were only blanks before the cursor, trim them. */
if (allblanks)
openfile->current_x = 0;
}
/* Copy the whitespace from the sample line to the new one. */
strncpy(newnode->data, sampleline->data, indent_chars);
/* If there were only blanks before the cursor, trim them. */
if (allblanks)
openfile->current_x = 0;
#endif
null_at(&openfile->current->data, openfile->current_x);
......@@ -911,7 +912,7 @@ void do_enter(void)
if (openfile->mark == openfile->current &&
openfile->mark_x > openfile->current_x) {
openfile->mark = newnode;
openfile->mark_x += extra - openfile->current_x;
openfile->mark_x += indent_chars - openfile->current_x;
}
#endif
......@@ -921,15 +922,15 @@ void do_enter(void)
/* Put the cursor on the new line, after any automatic whitespace. */
openfile->current = newnode;
openfile->current_x = extra;
openfile->current_x = indent_chars;
openfile->placewewant = xplustabs();
openfile->totsize++;
openfile->totsize++; // count the '\n' character
set_modified();
#ifndef NANO_TINY
if (ISSET(AUTOINDENT) && !allblanks)
openfile->totsize += extra;
openfile->totsize += indent_chars;
update_undo(ENTER);
#endif
......
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