Adds auto-indent feature
This commit is contained in:
parent
5bef5486ed
commit
6be07008e0
5
config.h
5
config.h
|
@ -9,9 +9,12 @@
|
||||||
// Set global tab width for the editor here
|
// Set global tab width for the editor here
|
||||||
static const int tabwidth = 4;
|
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;
|
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
|
* Values from 1 to 256 representing 256 color/8-bit pallate
|
||||||
* see: https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
|
* see: https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
|
||||||
|
|
34
hermes.c
34
hermes.c
|
@ -555,6 +555,22 @@ void editorInsertNewline() {
|
||||||
}
|
}
|
||||||
E.cy++;
|
E.cy++;
|
||||||
E.cx = 0;
|
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) {
|
void editorUpdatePaste(char *str, int len) {
|
||||||
|
@ -1290,19 +1306,23 @@ void editorCommandKp(int c) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
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':
|
case 'o':
|
||||||
E.cx = (E.cy >= E.numRows) ? 0 : E.row[E.cy].size;
|
E.cx = (E.cy >= E.numRows) ? 0 : E.row[E.cy].size;
|
||||||
E.mode = InputMode;
|
E.mode = InputMode;
|
||||||
editorMoveCursor(ARROW_RIGHT);
|
editorMoveCursor(ARROW_RIGHT);
|
||||||
editorInsertNewline();
|
editorInsertNewline();
|
||||||
break;
|
break;
|
||||||
case 'O':
|
|
||||||
E.cx = 0;
|
|
||||||
E.mode = InputMode;
|
|
||||||
editorInsertNewline();
|
|
||||||
editorMoveCursor(ARROW_UP);
|
|
||||||
editorMoveCursor(ARROW_RIGHT);
|
|
||||||
break;
|
|
||||||
case 'x':
|
case 'x':
|
||||||
while (counter > 0) {
|
while (counter > 0) {
|
||||||
editorUpdatePaste(&E.row[E.cy].chars[E.cx], 1);
|
editorUpdatePaste(&E.row[E.cy].chars[E.cx], 1);
|
||||||
|
|
Loading…
Reference in New Issue