From f83bebedd49944c6cf2ddd4a35e5bcc615b545f7 Mon Sep 17 00:00:00 2001 From: Caleb Sander <caleb.sander@gmail.com> Date: Wed, 2 Oct 2019 12:09:48 -0700 Subject: [PATCH] Indent line when pressing ENTER between braces --- src/text.c | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/src/text.c b/src/text.c index 5ffdb072..9b9813e2 100644 --- a/src/text.c +++ b/src/text.c @@ -96,6 +96,20 @@ void do_tab(void) 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 /* Add an indent to the given line. */ void indent_a_line(filestruct *line, char *indentation) @@ -142,16 +156,8 @@ void do_indent(void) if (top == bot->next) return; - indentation = charalloc(tabsize + 1); - - /* 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'; - } + indentation = charalloc(tab_char_length() + 1); + set_indentation(indentation); add_undo(INDENT); @@ -865,11 +871,11 @@ void do_redo(void) /* Break the current line at the cursor position. */ void do_enter(void) { - filestruct *newnode = make_new_node(openfile->current); size_t extra = 0; #ifndef NANO_TINY filestruct *sampleline = openfile->current; bool allblanks = FALSE; + filestruct *indented_line = NULL; if (ISSET(AUTOINDENT)) { #ifdef ENABLE_JUSTIFY @@ -886,8 +892,20 @@ void do_enter(void) extra = openfile->current_x; else if (extra == openfile->current_x) 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 */ + filestruct *previous = indented_line ? indented_line : openfile->current; + filestruct *newnode = make_new_node(previous); newnode->data = charalloc(strlen(openfile->current->data + openfile->current_x) + extra + 1); strcpy(&newnode->data[extra], openfile->current->data + @@ -916,12 +934,12 @@ void do_enter(void) #endif /* Insert the newly created line after the current one and renumber. */ - splice_node(openfile->current, newnode); + splice_node(previous, newnode); renumber(newnode); /* Put the cursor on the new line, after any automatic whitespace. */ - openfile->current = newnode; - openfile->current_x = extra; + openfile->current = indented_line ? indented_line : newnode; + openfile->current_x = indented_line ? extra + tab_char_length() : extra; openfile->placewewant = xplustabs(); openfile->totsize++; -- GitLab