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

Indent line when pressing ENTER between braces

No related merge requests found
Showing with 32 additions and 14 deletions
+32 -14
...@@ -96,6 +96,20 @@ void do_tab(void) ...@@ -96,6 +96,20 @@ void do_tab(void)
do_output((char *)"\t", 1, TRUE); do_output((char *)"\t", 1, TRUE);
} }
size_t tab_char_length(void) {
return ISSET(TABS_TO_SPACES) ? tabsize : 1;
}
void set_indentation(char *indentation) {
/* Set the indentation to either a bunch of spaces or a single tab. */
if (ISSET(TABS_TO_SPACES)) {
charset(indentation, ' ', tabsize);
indentation[tabsize] = '\0';
} else {
strcpy(indentation, "\t");
}
}
#ifndef NANO_TINY #ifndef NANO_TINY
/* Add an indent to the given line. */ /* Add an indent to the given line. */
void indent_a_line(filestruct *line, char *indentation) void indent_a_line(filestruct *line, char *indentation)
...@@ -142,16 +156,8 @@ void do_indent(void) ...@@ -142,16 +156,8 @@ void do_indent(void)
if (top == bot->next) if (top == bot->next)
return; return;
indentation = charalloc(tabsize + 1); indentation = charalloc(tab_char_length() + 1);
set_indentation(indentation);
/* Set the indentation to either a bunch of spaces or a single tab. */
if (ISSET(TABS_TO_SPACES)) {
charset(indentation, ' ', tabsize);
indentation[tabsize] = '\0';
} else {
indentation[0] = '\t';
indentation[1] = '\0';
}
add_undo(INDENT); add_undo(INDENT);
...@@ -865,11 +871,11 @@ void do_redo(void) ...@@ -865,11 +871,11 @@ void do_redo(void)
/* Break the current line at the cursor position. */ /* Break the current line at the cursor position. */
void do_enter(void) void do_enter(void)
{ {
filestruct *newnode = make_new_node(openfile->current);
size_t extra = 0; size_t extra = 0;
#ifndef NANO_TINY #ifndef NANO_TINY
filestruct *sampleline = openfile->current; filestruct *sampleline = openfile->current;
bool allblanks = FALSE; bool allblanks = FALSE;
filestruct *indented_line = NULL;
if (ISSET(AUTOINDENT)) { if (ISSET(AUTOINDENT)) {
#ifdef ENABLE_JUSTIFY #ifdef ENABLE_JUSTIFY
...@@ -886,8 +892,20 @@ void do_enter(void) ...@@ -886,8 +892,20 @@ void do_enter(void)
extra = openfile->current_x; extra = openfile->current_x;
else if (extra == openfile->current_x) else if (extra == openfile->current_x)
allblanks = TRUE; allblanks = TRUE;
else if (
openfile->current->data[openfile->current_x - 1] == '{' &&
openfile->current->data[openfile->current_x ] == '}'
) {
indented_line = make_new_node(openfile->current);
indented_line->data = charalloc(extra + tab_char_length() + 1);
strncpy(indented_line->data, sampleline->data, extra);
set_indentation(indented_line->data + extra);
splice_node(openfile->current, indented_line);
}
} }
#endif /* NANO_TINY */ #endif /* NANO_TINY */
filestruct *previous = indented_line ? indented_line : openfile->current;
filestruct *newnode = make_new_node(previous);
newnode->data = charalloc(strlen(openfile->current->data + newnode->data = charalloc(strlen(openfile->current->data +
openfile->current_x) + extra + 1); openfile->current_x) + extra + 1);
strcpy(&newnode->data[extra], openfile->current->data + strcpy(&newnode->data[extra], openfile->current->data +
...@@ -916,12 +934,12 @@ void do_enter(void) ...@@ -916,12 +934,12 @@ void do_enter(void)
#endif #endif
/* Insert the newly created line after the current one and renumber. */ /* Insert the newly created line after the current one and renumber. */
splice_node(openfile->current, newnode); splice_node(previous, newnode);
renumber(newnode); renumber(newnode);
/* 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 = indented_line ? indented_line : newnode;
openfile->current_x = extra; openfile->current_x = indented_line ? extra + tab_char_length() : extra;
openfile->placewewant = xplustabs(); openfile->placewewant = xplustabs();
openfile->totsize++; openfile->totsize++;
......
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