Adds auto-indent feature

This commit is contained in:
sloum 2020-02-09 22:44:16 -08:00
parent 5bef5486ed
commit 6be07008e0
2 changed files with 31 additions and 8 deletions

View File

@ -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

View File

@ -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);