diff options
author | cflip <cflip@cflip.net> | 2023-01-18 15:39:52 -0700 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2023-01-18 15:45:25 -0700 |
commit | 40e9a891fd2d09d56465353be379abc3f127e99d (patch) | |
tree | 02f0cef84e164177e012961565752374bcb92024 | |
parent | 8d4c8b32faadd97631904e411ff39cb8282030f5 (diff) |
Reimplement syntax highlighting
-rw-r--r-- | syntax.c | 14 | ||||
-rw-r--r-- | window.c | 25 |
2 files changed, 28 insertions, 11 deletions
@@ -158,13 +158,13 @@ int editor_syntax_to_colour(int highlight) { switch (highlight) { case HIGHLIGHT_MULTILINE_COMMENT: - case HIGHLIGHT_COMMENT: return 36; - case HIGHLIGHT_KEYWORD1: return 33; - case HIGHLIGHT_KEYWORD2: return 32; - case HIGHLIGHT_STRING: return 35; - case HIGHLIGHT_NUMBER: return 31; - case HIGHLIGHT_MATCH: return 34; - default: return 37; + case HIGHLIGHT_COMMENT: return 0x00ff00; + case HIGHLIGHT_KEYWORD1: return 0x00ffff; + case HIGHLIGHT_KEYWORD2: return 0xff00ff; + case HIGHLIGHT_STRING: return 0x7f7fff; + case HIGHLIGHT_NUMBER: return 0xff0000; + case HIGHLIGHT_MATCH: return 0xff7fff; + default: return 0xfffffff; } } @@ -7,6 +7,7 @@ #include "error.h" #include "font.h" #include "input.h" +#include "syntax.h" static SDL_Window *window = NULL; static SDL_Renderer *renderer = NULL; @@ -72,10 +73,11 @@ int window_handle_event(struct editor_state *editor) return 1; } -static void draw_string(const char *str, size_t len, int x, int y) +static void draw_string(const char *str, const uint8_t *highlight, size_t len, int x, int y) { SDL_Rect dstrect = { x, y, font.width, font.height }; SDL_Rect srcrect = { 0, 0, font.width, font.height }; + int current_colour = 0xffffff; for (int i = 0; i < len; i++) { const char letter = str[i]; @@ -99,6 +101,18 @@ static void draw_string(const char *str, size_t len, int x, int y) glyph_index = font.unicode_desc[glyph_index]; srcrect.x = glyph_index * font.width; + int colour = current_colour; + if (highlight != NULL) + colour = editor_syntax_to_colour(highlight[i]); + + if (colour != current_colour) { + current_colour = colour; + unsigned char r = (colour >> 16) & 0xff; + unsigned char g = (colour >> 8) & 0xff; + unsigned char b = colour & 0xff; + SDL_SetTextureColorMod(font_texture, r, g, b); + } + SDL_RenderCopy(renderer, font_texture, &srcrect, &dstrect); dstrect.x += font.width; @@ -114,7 +128,8 @@ static void draw_editor(struct editor_state *editor) line_y = i * font.height; if (i + editor->line_offset >= editor->num_lines) { - draw_string("~", 1, 0, line_y); + SDL_SetTextureColorMod(font_texture, 0xcc, 0x00, 0xcc); + draw_string("~", NULL, 1, 0, line_y); continue; } @@ -122,9 +137,10 @@ static void draw_editor(struct editor_state *editor) /* Size and length of the text including the scroll offset. */ char *printed_text = &line->render[editor->col_offset]; + unsigned char *printed_highlight = &line->highlight[editor->col_offset]; size_t printed_size = line->render_size - editor->col_offset; if (line->render_size >= printed_size) - draw_string(printed_text, printed_size, 0, line_y); + draw_string(printed_text, printed_highlight, printed_size, 0, line_y); } /* Draw the statusline containing file information */ @@ -134,7 +150,8 @@ static void draw_editor(struct editor_state *editor) editor_draw_message_bar(editor, &statusbuf); line_y = window_height - (font.height * 2); - draw_string(statusbuf.buffer, statusbuf.length, 0, line_y); + SDL_SetTextureColorMod(font_texture, 0xff, 0xff, 0xff); + draw_string(statusbuf.buffer, NULL, statusbuf.length, 0, line_y); ab_free(&statusbuf); } |