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