diff --git a/src/eve.h b/src/eve.h index 74c5260..bf4744c 100644 --- a/src/eve.h +++ b/src/eve.h @@ -3,7 +3,7 @@ /* eve.h */ /* */ /* Created: 2023/11/27 17:17:10 by cezelot */ -/* Updated: 2024/09/08 22:03:23 by cezelot */ +/* Updated: 2024/10/01 08:39:14 by cezelot */ /* */ /* Copyright (C) 2024 Ismael Benjara, Alberto Rodriguez */ /* */ @@ -77,6 +77,8 @@ enum e_highlight_flags { enum e_highlight { HL_NORMAL = 0, HL_COMMENT, + HL_KEYWORD1, + HL_KEYWORD2, HL_STRING, HL_NUMBER, HL_MATCH @@ -85,6 +87,7 @@ enum e_highlight { typedef struct s_syntax { char *filetype; char **filematch; + char **keywords; char *singleline_comment_start; int flags; } t_syntax; @@ -158,6 +161,7 @@ void update_syntax(t_env *env, t_erow *row); int is_separator(int c); int match_extension(t_env *env, char *ext, t_syntax *syntax); // -------------------------------------------------- syntax_highlighting_3.c -- +int highlight_keyword(t_erow *row, char **keywords, int *i, int *prev_sep); int highlight_number(t_erow *row, int *i, int *prev_sep, unsigned char prev_hl); int highlight_string(t_erow *row, int *i, int *prev_sep, int *in_string); diff --git a/src/syntax_highlighting/syntax_highlighting.c b/src/syntax_highlighting/syntax_highlighting.c index ffd1c58..f363387 100644 --- a/src/syntax_highlighting/syntax_highlighting.c +++ b/src/syntax_highlighting/syntax_highlighting.c @@ -3,7 +3,7 @@ /* syntax_highlighting.c */ /* */ /* Created: 2024/09/01 15:58:19 by cezelot */ -/* Updated: 2024/09/08 22:44:55 by cezelot */ +/* Updated: 2024/10/03 19:16:39 by cezelot */ /* */ /* Copyright (C) 2024 Ismael Benjara */ /* */ @@ -73,6 +73,12 @@ update_syntax(t_env *env, t_erow *row) continue ; } } + if (prev_sep) { + if (highlight_keyword(row, env->syntax->keywords, &i, + &prev_sep)) { + continue ; + } + } prev_sep = is_separator(row->render[i]); ++i; } @@ -87,10 +93,18 @@ select_syntax_highlight(t_env *env) ".cpp", NULL }; + static char *c_keywords[] = { + "char", "short", "int", "long", "double", "float", "signed", + "unsigned", "void", "const", "static", "extern", "if", "else", + "while", "for", "switch", "case", "break", "continue", "return", + + "struct|", "union|", "typedef|", "enum|", "class|", NULL + }; static t_syntax hldb[] = { { "c", c_extensions, + c_keywords, "//", HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS } @@ -117,15 +131,19 @@ syntax_to_color(int hl) case HL_COMMENT: /* foreground dark gray */ return (37); + case HL_MATCH: + case HL_KEYWORD1: + /* foreground light blue */ + return (94); + case HL_KEYWORD2: + /* foreground green */ + return (32); case HL_STRING: /* foreground cyan */ return (36); case HL_NUMBER: /* foreground light red */ return (91); - case HL_MATCH: - /* foreground light blue */ - return (94); default: /* default foreground color */ return (39); diff --git a/src/syntax_highlighting/syntax_highlighting_3.c b/src/syntax_highlighting/syntax_highlighting_3.c index ca0738e..b48553c 100644 --- a/src/syntax_highlighting/syntax_highlighting_3.c +++ b/src/syntax_highlighting/syntax_highlighting_3.c @@ -3,7 +3,7 @@ /* syntax_highlighting_3.c */ /* */ /* Created: 2024/09/08 10:58:46 by cezelot */ -/* Updated: 2024/09/08 15:38:45 by cezelot */ +/* Updated: 2024/10/01 08:42:49 by cezelot */ /* */ /* Copyright (C) 2024 Ismael Benjara */ /* */ @@ -26,6 +26,35 @@ #include "../eve.h" +int +highlight_keyword(t_erow *row, char **keywords, int *i, int *prev_sep) +{ + size_t n = 0; + size_t len; + size_t kw2; + + while (keywords[n]) { + len = strlen(keywords[n]); + kw2 = keywords[n][len - 1] == '|'; + if (kw2) { + --len; + } + if (!strncmp(&row->render[*i], keywords[n], len) + && is_separator(row->render[*i + len])) { + memset(&row->hl[*i], kw2 ? HL_KEYWORD2 : HL_KEYWORD1, + len); + *i += len; + break ; + } + ++n; + } + if (keywords[n] != NULL) { + *prev_sep = 0; + return (1); + } + return (0); +} + int highlight_string(t_erow *row, int *i, int *prev_sep, int *in_string) {