From f5ba357eac9d570a2b5f1373bdf33f0d34db122d Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Tue, 20 Aug 2019 17:05:25 -0400 Subject: [PATCH] Highlight keywords --- kilo.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/kilo.c b/kilo.c index c22c864..7bf97ff 100644 --- a/kilo.c +++ b/kilo.c @@ -41,6 +41,8 @@ enum editorKey { enum editorHighlight { HL_NORMAL = 0, HL_COMMENT, + HL_KEYWORD1, + HL_KEYWORD2, HL_STRING, HL_NUMBER, HL_MATCH @@ -54,6 +56,7 @@ enum editorHighlight { struct editorSyntax { char *filetype; char **filematch; + char **keywords; char *singleline_comment_start; int flags; }; @@ -88,11 +91,19 @@ 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 }, @@ -303,6 +314,8 @@ void editorUpdateSyntax(erow *row) return; } + char **keywords = E.syntax->keywords; + char *scs = E.syntax->singleline_comment_start; int scs_len = scs ? strlen(scs) : 0; @@ -368,6 +381,33 @@ void editorUpdateSyntax(erow *row) } } + 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++; } @@ -380,6 +420,12 @@ int editorSyntaxToColor(int hl) case HL_COMMENT: return 36; // cyan + case HL_KEYWORD1: + return 33; // yellow + + case HL_KEYWORD2: + return 32; // green + case HL_STRING: return 35; // magenta