diff --git a/kilo.c b/kilo.c index fdfc5d4..c22c864 100644 --- a/kilo.c +++ b/kilo.c @@ -40,6 +40,7 @@ enum editorKey { enum editorHighlight { HL_NORMAL = 0, + HL_COMMENT, HL_STRING, HL_NUMBER, HL_MATCH @@ -53,6 +54,7 @@ enum editorHighlight { struct editorSyntax { char *filetype; char **filematch; + char *singleline_comment_start; int flags; }; @@ -84,12 +86,14 @@ struct editorConfig { struct editorConfig E; /*** filetypes ***/ + char *C_HL_extensions[] = { ".c", ".h", ".cpp", NULL }; struct editorSyntax HLDB[] = { { "c", C_HL_extensions, + "//", HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS }, }; @@ -154,6 +158,7 @@ int editorReadKey() } } + // The character code starts with the escape sequence (\x1b) if (c == '\x1b') { char seq[3]; @@ -298,6 +303,9 @@ void editorUpdateSyntax(erow *row) return; } + char *scs = E.syntax->singleline_comment_start; + int scs_len = scs ? strlen(scs) : 0; + int prev_sep = 1; int in_string = 0; @@ -307,6 +315,15 @@ void editorUpdateSyntax(erow *row) char c = row->render[i]; unsigned char prev_hl = (i > 0) ? row->hl[i - 1] : HL_NORMAL; + if (scs_len && ! in_string) + { + if ( ! strncmp(&row->render[i], scs, scs_len)) + { + memset(&row->hl[i], HL_COMMENT, row->rsize - i); + break; + } + } + if (E.syntax->flags & HL_HIGHLIGHT_STRINGS) { if (in_string) @@ -360,14 +377,17 @@ int editorSyntaxToColor(int hl) { switch (hl) { + case HL_COMMENT: + return 36; // cyan + case HL_STRING: - return 35; + return 35; // magenta case HL_NUMBER: - return 31; + return 31; // red case HL_MATCH: - return 34; + return 34; // blue default: return 37; @@ -463,7 +483,7 @@ void editorUpdateRow(erow *row) } free(row->render); - row->render = malloc(row->size + tabs*(KILO_TAB_STOP - 1) + 1); + row->render = malloc(row->size + tabs *(KILO_TAB_STOP - 1) + 1); int idx = 0; for (j = 0; j < row->size; j++)