From fe4d14a5bb135a925bb47fe88b1a78df0d03cd49 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Mon, 1 Feb 2021 14:51:11 -0500 Subject: start per-buffer language --- buffer.c | 4 ++-- syntax.c | 29 ++++------------------------- ted.h | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 27 deletions(-) diff --git a/buffer.c b/buffer.c index 9a0627f..2b9957c 100644 --- a/buffer.c +++ b/buffer.c @@ -1980,7 +1980,7 @@ void buffer_render(TextBuffer *buffer, Rect r) { SyntaxCharType *char_types = NULL; for (u32 line_idx = 0; line_idx < start_line; ++line_idx) { Line *line = &lines[line_idx]; - syntax_highlight(&syntax_state, LANG_C, line->str, line->len, NULL); + syntax_highlight(&syntax_state, buffer->language, line->str, line->len, NULL); } for (u32 line_idx = start_line; line_idx < nlines; ++line_idx) { @@ -1988,7 +1988,7 @@ void buffer_render(TextBuffer *buffer, Rect r) { if (arr_len(char_types) < line->len) { arr_set_len(char_types, line->len); } - syntax_highlight(&syntax_state, LANG_C, line->str, line->len, char_types); + syntax_highlight(&syntax_state, buffer->language, line->str, line->len, char_types); for (u32 i = 0; i < line->len; ++i) { char32_t c = line->str[i]; SyntaxCharType type = char_types[i]; diff --git a/syntax.c b/syntax.c index 1c426e8..eff776c 100644 --- a/syntax.c +++ b/syntax.c @@ -1,28 +1,3 @@ -typedef struct { - bool multi_line_comment:1; // are we in a multi-line comment? (delineated by /* */) - bool continued_single_line_comment:1; // if you add a \ to the end of a single-line comment, it is continued to the next line. - bool continued_preprocessor:1; // similar to above - bool continued_string:1; -} SyntaxStateC; - -typedef union { - SyntaxStateC c; -} SyntaxState; - -ENUM_U16 { - LANG_C -} ENUM_U16_END(Language); - -ENUM_U8 { - SYNTAX_NORMAL, - SYNTAX_KEYWORD, - SYNTAX_COMMENT, - SYNTAX_PREPROCESSOR, - SYNTAX_STRING, - SYNTAX_CHARACTER, - SYNTAX_CONSTANT -} ENUM_U8_END(SyntaxCharType); - // NOTE: returns the color setting, not the color ColorSetting syntax_char_type_to_color(SyntaxCharType t) { switch (t) { @@ -235,8 +210,12 @@ static void syntax_highlight_c(SyntaxStateC *state, char32_t *line, u32 line_len // You can set char_types to NULL if you just want to advance the state, and don't care about the character types. void syntax_highlight(SyntaxState *state, Language lang, char32_t *line, u32 line_len, SyntaxCharType *char_types) { switch (lang) { + case LANG_TEXT: + memset(char_types, 0, line_len * sizeof *char_types); + break; case LANG_C: syntax_highlight_c(&state->c, line, line_len, char_types); break; + case LANG_COUNT: assert(0); break; } } diff --git a/ted.h b/ted.h index 9df0208..2fcc541 100644 --- a/ted.h +++ b/ted.h @@ -4,6 +4,33 @@ #define TEXT_SIZE_MIN 6 #define TEXT_SIZE_MAX 70 +typedef struct { + bool multi_line_comment:1; // are we in a multi-line comment? (delineated by /* */) + bool continued_single_line_comment:1; // if you add a \ to the end of a single-line comment, it is continued to the next line. + bool continued_preprocessor:1; // similar to above + bool continued_string:1; +} SyntaxStateC; + +typedef union { + SyntaxStateC c; +} SyntaxState; + +ENUM_U16 { + LANG_TEXT, + LANG_C, + LANG_COUNT +} ENUM_U16_END(Language); + +ENUM_U8 { + SYNTAX_NORMAL, + SYNTAX_KEYWORD, + SYNTAX_COMMENT, + SYNTAX_PREPROCESSOR, + SYNTAX_STRING, + SYNTAX_CHARACTER, + SYNTAX_CONSTANT +} ENUM_U8_END(SyntaxCharType); + typedef struct { float cursor_blink_time_on, cursor_blink_time_off; u32 colors[COLOR_COUNT]; @@ -64,6 +91,7 @@ typedef struct { double scroll_x, scroll_y; // number of characters scrolled in the x/y direction BufferPos cursor_pos; BufferPos selection_pos; // if selection is true, the text between selection_pos and cursor_pos is selected. + Language language; bool is_line_buffer; // "line buffers" are buffers which can only have one line of text (used for inputs) bool selection; bool store_undo_events; // set to false to disable undo events -- cgit v1.2.3