From 184827cfd130fc25cde0dfff3a2e01f4b42cf8c6 Mon Sep 17 00:00:00 2001 From: Aaron Fischer Date: Thu, 18 Oct 2018 10:26:13 +0200 Subject: [PATCH] Finish the basic editor --- kilo.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 95 insertions(+), 4 deletions(-) diff --git a/kilo.c b/kilo.c index 2514ec3..7736da2 100644 --- a/kilo.c +++ b/kilo.c @@ -41,6 +41,9 @@ enum editorKey { enum editorHighlight { HL_NORMAL = 0, HL_COMMENT, + HL_MLCOMMENT, + HL_KEYWORD1, + HL_KEYWORD2, HL_STRING, HL_NUMBER, HL_MATCH @@ -55,16 +58,21 @@ enum editorHighlight { struct editorSyntax { char *filetype; char **filematch; + char **keywords; char *singleline_comment_start; + char *multiline_comment_start; + char *multiline_comment_end; int flags; }; typedef struct erow { + int idx; int size; int rsize; char *chars; char *render; unsigned char *hl; + int hl_open_comment; } erow; struct editorConfig { @@ -89,12 +97,20 @@ struct editorConfig E; /*** filetypes ***/ 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[] = { { "c", C_HL_extensions, - "//", + C_HL_keywords, + "//", "/*", "*/", HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS }, }; @@ -236,11 +252,19 @@ void editorUpdateSyntax(erow *row) { if (E.syntax == NULL) return; + char **keywords = E.syntax->keywords; + 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 mcs_len = mcs ? strlen(mcs) : 0; + int mce_len = mce ? strlen(mce) : 0; int prev_sep = 1; int in_string = 0; + int in_comment = (row->idx > 0 && E.row[row->idx-1].hl_open_comment); int i = 0; while (i < row->rsize) { @@ -248,13 +272,35 @@ void editorUpdateSyntax(erow *row) { unsigned char prev_hl = (i > 0) ? row->hl[i -1] : HL_NORMAL; // Comments - if (scs_len && !in_string) { + if (scs_len && !in_string && !in_comment) { if (!strncmp(&row->render[i], scs, scs_len)) { memset(&row->hl[i], HL_COMMENT, row->rsize - i); 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 if (E.syntax->flags & HL_HIGHLIGHT_NUMBERS) { 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); 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) { 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_NUMBER: return 31; 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)); 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].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].render = NULL; E.row[at].hl = NULL; + E.row[at].hl_open_comment = 0; editorUpdateRow(&E.row[at]); E.numrows++; @@ -412,6 +492,7 @@ void editorDelRow(int at) { if (at < 0 || at >= E.numrows) return; editorFreeRow(&E.row[at]); 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.dirty++; } @@ -704,7 +785,17 @@ void editorDrawRows(struct abuf *ab) { int current_color = -1; int j; for (j=0; j