diff options
| -rw-r--r-- | config.def.h | 62 | ||||
| -rw-r--r-- | editor.c | 14 | ||||
| -rw-r--r-- | editor.h | 4 | ||||
| -rw-r--r-- | syntax.h | 2 | ||||
| -rw-r--r-- | window.c | 3 |
5 files changed, 51 insertions, 34 deletions
diff --git a/config.def.h b/config.def.h index b0b836c..6db472d 100644 --- a/config.def.h +++ b/config.def.h @@ -730,9 +730,26 @@ static Config editors[] = { /* default editor configuration to use */ static Config *config = &editors[0]; -/* Color definitions, by default the i-th color is used for the i-th syntax - * rule below. A color value of -1 specifies the default terminal color. - */ +/* Color definitions for use in the sytax highlighting rules below. A fore + * or background color of -1 specifies the default terminal color. */ +enum { + COLOR_SYNTAX0, + COLOR_SYNTAX1, + COLOR_SYNTAX2, + COLOR_SYNTAX3, + COLOR_SYNTAX4, + COLOR_SYNTAX5, + COLOR_SYNTAX6, + COLOR_SYNTAX7, + COLOR_KEYWORD = COLOR_SYNTAX1, + COLOR_DATATYPE = COLOR_SYNTAX2, + COLOR_CONTROL = COLOR_SYNTAX3, + COLOR_PREPROCESSOR = COLOR_SYNTAX4, + COLOR_BRACKETS = COLOR_SYNTAX5, + COLOR_STRING = COLOR_SYNTAX6, + COLOR_COMMENT = COLOR_SYNTAX7, +}; + static Color colors[] = { { .fg = COLOR_RED, .bg = -1, .attr = A_BOLD }, { .fg = COLOR_GREEN, .bg = -1, .attr = A_BOLD }, @@ -742,19 +759,12 @@ static Color colors[] = { { .fg = COLOR_BLUE, .bg = -1, .attr = A_BOLD }, { .fg = COLOR_RED, .bg = -1, .attr = A_NORMAL }, { .fg = COLOR_BLUE, .bg = -1, .attr = A_NORMAL }, - { .fg = COLOR_BLUE, .bg = -1, .attr = A_NORMAL }, { /* empty last element, array terminator */ } }; -/* Syntax color definition, you can define up to SYNTAX_REGEX_RULES +/* Syntax color definition, you can define up to TODO SYNTAX_REGEX_RULES * number of regex rules per file type. Each rule is requires a regular - * expression and corresponding compilation flags. Optionally a color in - * the form - * - * { .fg = COLOR_YELLOW, .bg = -1, .attr = A_BOLD } - * - * can be specified. If such a color definition is missing the i-th element - * of the colors array above is used instead. + * expression and corresponding compilation flags as well as a color. * * The array of syntax definition must be terminated with an empty element. */ @@ -763,40 +773,48 @@ static Color colors[] = { // #define B "^| |\t|\\(|\\)|\\[|\\]|\\{|\\}|\\||$ // changes wrt sandy #precoressor: # idfdef, #include <file.h> between brackets +/* these rules are applied top to bottom, first match wins. Therefore more 'greedy' + * rules such as for comments should be the first entries */ static Syntax syntaxes[] = {{ .name = "c", .file = "\\.(c(pp|xx)?|h(pp|xx)?|cc)$", .rules = {{ - "$^", + "(/\\*([^*]|\\*[^/])*\\*/|/\\*([^*]|\\*[^/])*$|^([^/]|/[^*])*\\*/)", + 0, + &colors[COLOR_COMMENT], + },{ + "(//.*)", REG_NEWLINE, + &colors[COLOR_COMMENT], + },{ + "(\"(\\\\.|[^\"])*\")", + //"([\"<](\\\\.|[^ \">])*[\">])", + REG_NEWLINE, + &colors[COLOR_STRING], },{ B"(for|if|while|do|else|case|default|switch|try|throw|catch|operator|new|delete)"B, REG_NEWLINE, + &colors[COLOR_KEYWORD], },{ B"(float|double|bool|char|int|short|long|sizeof|enum|void|static|const|struct|union|" "typedef|extern|(un)?signed|inline|((s?size)|((u_?)?int(8|16|32|64|ptr)))_t|class|" "namespace|template|public|protected|private|typename|this|friend|virtual|using|" "mutable|volatile|register|explicit)"B, REG_NEWLINE, + &colors[COLOR_DATATYPE], },{ B"(goto|continue|break|return)"B, REG_NEWLINE, + &colors[COLOR_CONTROL], },{ "(^#[\\t ]*(define|include(_next)?|(un|ifn?)def|endif|el(if|se)|if|warning|error|pragma))|" B"[A-Z_][0-9A-Z_]+"B"", REG_NEWLINE, + &colors[COLOR_PREPROCESSOR], },{ "(\\(|\\)|\\{|\\}|\\[|\\])", REG_NEWLINE, - },{ - "(\"(\\\\.|[^\"])*\")", - //"([\"<](\\\\.|[^ \">])*[\">])", - REG_NEWLINE, - },{ - "(//.*)", - REG_NEWLINE, - },{ - "(/\\*([^*]|\\*[^/])*\\*/|/\\*([^*]|\\*[^/])*$|^([^/]|/[^*])*\\*/)", + &colors[COLOR_BRACKETS], }}, },{ /* empty last element, array terminator */ @@ -200,20 +200,20 @@ static void editor_windows_invalidate(Editor *ed, size_t start, size_t end) { bool editor_syntax_load(Editor *ed, Syntax *syntaxes, Color *colors) { bool success = true; ed->syntaxes = syntaxes; + + for (Color *color = colors; color && color->fg; color++) { + if (color->attr == 0) + color->attr = A_NORMAL; + color->attr |= COLOR_PAIR(editor_color_get(color->fg, color->bg)); + } + for (Syntax *syn = syntaxes; syn && syn->name; syn++) { if (regcomp(&syn->file_regex, syn->file, REG_EXTENDED|REG_NOSUB|REG_ICASE|REG_NEWLINE)) success = false; - Color *color = colors; for (int j = 0; j < LENGTH(syn->rules); j++) { SyntaxRule *rule = &syn->rules[j]; if (!rule->rule) break; - if (rule->color.fg == 0 && color && color->fg != 0) - rule->color = *color++; - if (rule->color.attr == 0) - rule->color.attr = A_NORMAL; - if (rule->color.fg != 0) - rule->color.attr |= COLOR_PAIR(editor_color_get(rule->color.fg, rule->color.bg)); if (regcomp(&rule->regex, rule->rule, REG_EXTENDED|rule->cflags)) success = false; } @@ -119,9 +119,7 @@ void editor_delete(Editor*, size_t pos, size_t len); /* load a set of syntax highlighting definitions which will be associated * to the underlying window based on the file type loaded. * - * both *syntaxes and *colors must point to a NULL terminated array. - * it the i-th syntax definition does not specifiy a fore ground color - * then the i-th entry of the color array will be used instead + * both *syntaxes and *colors must point to a NULL terminated arrays. */ bool editor_syntax_load(Editor*, Syntax *syntaxes, Color *colors); void editor_syntax_unload(Editor*); @@ -13,7 +13,7 @@ typedef struct { typedef struct { char *rule; /* regex to search for */ int cflags; /* compilation flags (REG_*) used when compiling */ - Color color; /* settings to apply in case of a match */ + Color *color; /* settings to apply in case of a match */ regex_t regex; /* compiled form of the above rule */ } SyntaxRule; @@ -381,7 +381,8 @@ void window_draw(Win *win) { if (text + match[i][0].rm_so <= cur && cur < text + match[i][0].rm_eo) { /* within matched expression */ - attrs = rule->color.attr; + attrs = rule->color->attr; + break; /* first match wins */ } } } |
