From 6be07008e07886f69511fef16a83b7a99518d830 Mon Sep 17 00:00:00 2001 From: sloum Date: Sun, 9 Feb 2020 22:44:16 -0800 Subject: [PATCH] Adds auto-indent feature --- config.h | 5 ++++- hermes.c | 34 +++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/config.h b/config.h index 2bbe5b6..f7c5c8e 100644 --- a/config.h +++ b/config.h @@ -9,9 +9,12 @@ // Set global tab width for the editor here static const int tabwidth = 4; -// Turn on or off paren matching for: (, {, [ +// Turn on or off paren matching for: (, {, [, ", ` static const int MATCH_PARENS = 1; +// Auto-indent on known filetypes +static const int AUTO_INDENT = 1; + /* * Values from 1 to 256 representing 256 color/8-bit pallate * see: https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit diff --git a/hermes.c b/hermes.c index 86d1530..3b1082b 100644 --- a/hermes.c +++ b/hermes.c @@ -555,6 +555,22 @@ void editorInsertNewline() { } E.cy++; E.cx = 0; + if (AUTO_INDENT && E.syntax) { + erow *row = &E.row[E.cy-1]; + int tabs = 0; + int i = 0; + while (i < row->size && row->chars[i] == '\t') { + tabs++; + i++; + } + char lastChar = row->chars[row->size - 1]; + if (lastChar == '{' || lastChar == '[') tabs++; + if (E.row[E.cy].size > E.cx && E.row[E.cy].chars[E.cx + 1] == '}') tabs--; + while (tabs > 0) { + editorInsertChar('\t'); + tabs--; + } + } } void editorUpdatePaste(char *str, int len) { @@ -1290,19 +1306,23 @@ void editorCommandKp(int c) { } } break; + case 'O': + { + int docStart = E.cy ? 0 : 1; + editorMoveCursor(ARROW_UP); + E.cx = (E.cy >= E.numRows || docStart) ? 0 : E.row[E.cy].size; + E.mode = InputMode; + if (!docStart) editorMoveCursor(ARROW_RIGHT); + editorInsertNewline(); + if (docStart) editorMoveCursor(ARROW_UP); + } + break; case 'o': E.cx = (E.cy >= E.numRows) ? 0 : E.row[E.cy].size; E.mode = InputMode; editorMoveCursor(ARROW_RIGHT); editorInsertNewline(); break; - case 'O': - E.cx = 0; - E.mode = InputMode; - editorInsertNewline(); - editorMoveCursor(ARROW_UP); - editorMoveCursor(ARROW_RIGHT); - break; case 'x': while (counter > 0) { editorUpdatePaste(&E.row[E.cy].chars[E.cx], 1);