Highlight keywords

This commit is contained in:
Timothy Warren 2019-08-20 17:05:25 -04:00
parent 2f768daf31
commit f5ba357eac

46
kilo.c
View File

@ -41,6 +41,8 @@ enum editorKey {
enum editorHighlight { enum editorHighlight {
HL_NORMAL = 0, HL_NORMAL = 0,
HL_COMMENT, HL_COMMENT,
HL_KEYWORD1,
HL_KEYWORD2,
HL_STRING, HL_STRING,
HL_NUMBER, HL_NUMBER,
HL_MATCH HL_MATCH
@ -54,6 +56,7 @@ enum editorHighlight {
struct editorSyntax { struct editorSyntax {
char *filetype; char *filetype;
char **filematch; char **filematch;
char **keywords;
char *singleline_comment_start; char *singleline_comment_start;
int flags; int flags;
}; };
@ -88,11 +91,19 @@ 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
}, },
@ -303,6 +314,8 @@ void editorUpdateSyntax(erow *row)
return; return;
} }
char **keywords = E.syntax->keywords;
char *scs = E.syntax->singleline_comment_start; char *scs = E.syntax->singleline_comment_start;
int scs_len = scs ? strlen(scs) : 0; 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); prev_sep = is_separator(c);
i++; i++;
} }
@ -380,6 +420,12 @@ int editorSyntaxToColor(int hl)
case HL_COMMENT: case HL_COMMENT:
return 36; // cyan return 36; // cyan
case HL_KEYWORD1:
return 33; // yellow
case HL_KEYWORD2:
return 32; // green
case HL_STRING: case HL_STRING:
return 35; // magenta return 35; // magenta