Finish the basic editor

This commit is contained in:
Aaron Fischer 2018-10-18 10:26:13 +02:00
parent 6950181f0c
commit 184827cfd1

99
kilo.c
View file

@ -41,6 +41,9 @@ enum editorKey {
enum editorHighlight { enum editorHighlight {
HL_NORMAL = 0, HL_NORMAL = 0,
HL_COMMENT, HL_COMMENT,
HL_MLCOMMENT,
HL_KEYWORD1,
HL_KEYWORD2,
HL_STRING, HL_STRING,
HL_NUMBER, HL_NUMBER,
HL_MATCH HL_MATCH
@ -55,16 +58,21 @@ enum editorHighlight {
struct editorSyntax { struct editorSyntax {
char *filetype; char *filetype;
char **filematch; char **filematch;
char **keywords;
char *singleline_comment_start; char *singleline_comment_start;
char *multiline_comment_start;
char *multiline_comment_end;
int flags; int flags;
}; };
typedef struct erow { typedef struct erow {
int idx;
int size; int size;
int rsize; int rsize;
char *chars; char *chars;
char *render; char *render;
unsigned char *hl; unsigned char *hl;
int hl_open_comment;
} erow; } erow;
struct editorConfig { struct editorConfig {
@ -89,12 +97,20 @@ struct editorConfig E;
/*** filetypes ***/ /*** filetypes ***/
char *C_HL_extensions[] = { ".c", ".h", ".cpp", NULL }; char *C_HL_extensions[] = { ".c", ".h", ".cpp", NULL };
char *C_HL_keywords[] = {
"switch", "if", "while", "for", "break", "continue", "return", "else",
"struct", "union", "typedef", "static", "enum", "class", "case",
"int|", "long|", "double|", "float|", "char|", "unsigned|", "signed|",
"void|", NULL
};
struct editorSyntax HLDB[] = { struct editorSyntax HLDB[] = {
{ {
"c", "c",
C_HL_extensions, C_HL_extensions,
"//", C_HL_keywords,
"//", "/*", "*/",
HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS
}, },
}; };
@ -236,11 +252,19 @@ void editorUpdateSyntax(erow *row) {
if (E.syntax == NULL) return; if (E.syntax == NULL) return;
char **keywords = E.syntax->keywords;
char *scs = E.syntax->singleline_comment_start; char *scs = E.syntax->singleline_comment_start;
char *mcs = E.syntax->multiline_comment_start;
char *mce = E.syntax->multiline_comment_end;
int scs_len = scs ? strlen(scs) : 0; int scs_len = scs ? strlen(scs) : 0;
int mcs_len = mcs ? strlen(mcs) : 0;
int mce_len = mce ? strlen(mce) : 0;
int prev_sep = 1; int prev_sep = 1;
int in_string = 0; int in_string = 0;
int in_comment = (row->idx > 0 && E.row[row->idx-1].hl_open_comment);
int i = 0; int i = 0;
while (i < row->rsize) { while (i < row->rsize) {
@ -248,13 +272,35 @@ void editorUpdateSyntax(erow *row) {
unsigned char prev_hl = (i > 0) ? row->hl[i -1] : HL_NORMAL; unsigned char prev_hl = (i > 0) ? row->hl[i -1] : HL_NORMAL;
// Comments // Comments
if (scs_len && !in_string) { if (scs_len && !in_string && !in_comment) {
if (!strncmp(&row->render[i], scs, scs_len)) { if (!strncmp(&row->render[i], scs, scs_len)) {
memset(&row->hl[i], HL_COMMENT, row->rsize - i); memset(&row->hl[i], HL_COMMENT, row->rsize - i);
break; break;
} }
} }
// Multi line comments
if (mcs_len && mce_len && !in_string) {
if (in_comment) {
row->hl[i] = HL_MLCOMMENT;
if (!strncmp(&row->render[i], mce, mce_len)) {
memset(&row->hl[i], HL_MLCOMMENT, mce_len);
i += mce_len;
in_comment = 0;
prev_sep = 1;
continue;
} else {
i++;
continue;
}
} else if (!strncmp(&row->render[i], mcs, mcs_len)) {
memset(&row->hl[i], HL_MLCOMMENT, mcs_len);
i += mcs_len;
in_comment = 1;
continue;
}
}
// Strings // Strings
if (E.syntax->flags & HL_HIGHLIGHT_NUMBERS) { if (E.syntax->flags & HL_HIGHLIGHT_NUMBERS) {
if (in_string) { if (in_string) {
@ -289,14 +335,44 @@ void editorUpdateSyntax(erow *row) {
} }
} }
// Keywords
if (prev_sep) {
int j;
for (j=0; keywords[j]; j++) {
int klen = strlen(keywords[j]);
int kw2 = keywords[j][klen - 1] == '|';
if (kw2) klen--;
if (!strncmp(&row->render[i], keywords[j], klen) &&
is_separator(row->render[i + klen])) {
memset(&row->hl[i], kw2 ? HL_KEYWORD2 : HL_KEYWORD1, klen);
i += klen;
break;
}
}
if (keywords[j] != NULL) {
prev_sep = 0;
continue;
}
}
prev_sep = is_separator(c); prev_sep = is_separator(c);
i++; i++;
} }
int changed = (row->hl_open_comment != in_comment);
row->hl_open_comment = in_comment;
if (changed && row->idx+1 < E.numrows) {
editorUpdateSyntax(&E.row[row->idx+1]);
}
} }
int editorSyntaxToColor(int hl) { int editorSyntaxToColor(int hl) {
switch (hl) { switch (hl) {
case HL_COMMENT: return 36; case HL_COMMENT:
case HL_MLCOMMENT: return 36;
case HL_KEYWORD1: return 33;
case HL_KEYWORD2: return 32;
case HL_STRING: return 35; case HL_STRING: return 35;
case HL_NUMBER: return 31; case HL_NUMBER: return 31;
case HL_MATCH: return 34; case HL_MATCH: return 34;
@ -387,6 +463,9 @@ void editorInsertRow(int at, char *s, size_t len) {
E.row = realloc(E.row, sizeof(erow) * (E.numrows +1)); E.row = realloc(E.row, sizeof(erow) * (E.numrows +1));
memmove(&E.row[at + 1], &E.row[at], sizeof(erow) * (E.numrows - at)); memmove(&E.row[at + 1], &E.row[at], sizeof(erow) * (E.numrows - at));
for (int j=at+1; j<= E.numrows; j++) E.row[j].idx++;
E.row[at].idx = at;
E.row[at].size = len; E.row[at].size = len;
E.row[at].chars = malloc(len + 1); E.row[at].chars = malloc(len + 1);
@ -396,6 +475,7 @@ void editorInsertRow(int at, char *s, size_t len) {
E.row[at].rsize = 0; E.row[at].rsize = 0;
E.row[at].render = NULL; E.row[at].render = NULL;
E.row[at].hl = NULL; E.row[at].hl = NULL;
E.row[at].hl_open_comment = 0;
editorUpdateRow(&E.row[at]); editorUpdateRow(&E.row[at]);
E.numrows++; E.numrows++;
@ -412,6 +492,7 @@ void editorDelRow(int at) {
if (at < 0 || at >= E.numrows) return; if (at < 0 || at >= E.numrows) return;
editorFreeRow(&E.row[at]); editorFreeRow(&E.row[at]);
memmove(&E.row[at], &E.row[at + 1], sizeof(erow) * (E.numrows - at -1)); memmove(&E.row[at], &E.row[at + 1], sizeof(erow) * (E.numrows - at -1));
for (int j=at; j<= E.numrows-1; j++) E.row[j].idx--;
E.numrows--; E.numrows--;
E.dirty++; E.dirty++;
} }
@ -704,7 +785,17 @@ void editorDrawRows(struct abuf *ab) {
int current_color = -1; int current_color = -1;
int j; int j;
for (j=0; j<len; j++) { for (j=0; j<len; j++) {
if (hl[j] == HL_NORMAL) { if (iscntrl(c[j])) {
char sym = (c[j] <= 26) ? '@' + c[j] : '?';
abAppend(ab, "\x1b[7m", 4);
abAppend(ab, &sym, 1);
abAppend(ab, "\x1b[m", 3);
if (current_color != -1) {
char buf[16];
int clen = snprintf(buf, sizeof(buf), "\x1b[%dm", current_color);
abAppend(ab, buf, clen);
}
} else if (hl[j] == HL_NORMAL) {
if (current_color != -1) { if (current_color != -1) {
abAppend(ab, "\x1b[39m", 5); abAppend(ab, "\x1b[39m", 5);
current_color = -1; current_color = -1;