summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2021-02-01 19:30:55 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2021-02-01 19:30:55 -0500
commit01bfdf98e4475e13e7b4bb8b8fbd382fa836986e (patch)
treedcf687f1bc64e726b1a3675312ec0f98dd373c19
parentb376a87775d10dc7a693c0e1ecbe59e867e4634a (diff)
switched syntax highlighting to use a number for the state (will be useful later)
-rw-r--r--Makefile3
-rw-r--r--buffer.c21
-rw-r--r--config.c1
-rw-r--r--main.c18
-rw-r--r--syntax.c24
-rw-r--r--ted.cfg1
-rw-r--r--ted.h24
7 files changed, 63 insertions, 29 deletions
diff --git a/Makefile b/Makefile
index cd60533..e3823f3 100644
--- a/Makefile
+++ b/Makefile
@@ -3,6 +3,7 @@ ALL_CFLAGS=$(CFLAGS) -Wall -Wextra -Wshadow -Wconversion -Wpedantic -pedantic -s
LIBS=-lSDL2 -lGL -ldl -lm
DEBUG_CFLAGS=$(ALL_CFLAGS) -DDEBUG -O0 -g
RELEASE_CFLAGS=$(ALL_CFLAGS) -O3
+PROFILE_CFLAGS=$(ALL_CFLAGS) -O3 -DPROFILE=1
GLOBAL_DATA_DIR=/usr/share/ted
LOCAL_DATA_DIR=/home/`logname`/.local/share/ted
INSTALL_BIN_DIR=/usr/bin
@@ -10,6 +11,8 @@ ted: *.[ch] text.o
$(CC) main.c text.o -o ted $(DEBUG_CFLAGS) $(LIBS)
release: *.[ch]
$(CC) main.c -o ted $(RELEASE_CFLAGS) $(LIBS)
+profile: *.[ch]
+ $(CC) main.c -o ted $(PROFILE_CFLAGS) $(LIBS)
text.o: text.c text.h base.h lib/stb_truetype.h
$(CC) text.c -c -o $@ $(DEBUG_CFLAGS)
clean:
diff --git a/buffer.c b/buffer.c
index fa16c20..a672bdf 100644
--- a/buffer.c
+++ b/buffer.c
@@ -2006,9 +2006,14 @@ void buffer_render(TextBuffer *buffer, Rect r) {
Language language = buffer_language(buffer);
// dynamic array of character types, to be filled by syntax_highlight
SyntaxCharType *char_types = NULL;
- for (u32 line_idx = 0; line_idx < start_line; ++line_idx) {
- Line *line = &lines[line_idx];
- syntax_highlight(&syntax_state, language, line->str, line->len, NULL);
+ bool syntax_highlighting = language && settings->syntax_highlighting;
+ if (syntax_highlighting) {
+ for (u32 line_idx = 0; line_idx < start_line; ++line_idx) {
+ Line *line = &lines[line_idx];
+ syntax_highlight(&syntax_state, language, line->str, line->len, NULL);
+ }
+ } else {
+ gl_color_rgba(colors[COLOR_TEXT]);
}
for (u32 line_idx = start_line; line_idx < nlines; ++line_idx) {
@@ -2016,12 +2021,14 @@ 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, language, line->str, line->len, char_types);
+ if (language)
+ syntax_highlight(&syntax_state, 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];
-
- gl_color_rgba(colors[syntax_char_type_to_color(type)]);
+ if (syntax_highlighting) {
+ SyntaxCharType type = char_types[i];
+ gl_color_rgba(colors[syntax_char_type_to_color(type)]);
+ }
switch (c) {
case '\n': assert(0); break;
case '\r': break; // for CRLF line endings
diff --git a/config.c b/config.c
index bf3b284..15b787d 100644
--- a/config.c
+++ b/config.c
@@ -174,6 +174,7 @@ void config_read(Ted *ted, char const *filename) {
OptionBool const options_bool[] = {
{"auto-indent", &settings->auto_indent},
{"auto-add-newline", &settings->auto_add_newline},
+ {"syntax-highlighting", &settings->syntax_highlighting},
};
OptionU8 const options_u8[] = {
{"tab-width", &settings->tab_width, 1, 100},
diff --git a/main.c b/main.c
index bcad077..12e9549 100644
--- a/main.c
+++ b/main.c
@@ -48,6 +48,12 @@ no_warn_end
#include "command.c"
#include "config.c"
+#if PROFILE
+#define PROFILE_TIME(var) double var = time_get_seconds();
+#else
+#define PROFILE_TIME(var)
+#endif
+
static void die(char const *fmt, ...) {
char buf[256] = {0};
@@ -315,7 +321,6 @@ int main(int argc, char **argv) {
window_width = ted->window_width, window_height = ted->window_height;
while (SDL_PollEvent(&event)) {
-
TextBuffer *buffer = ted->active_buffer;
u32 key_modifier = (u32)ctrl_down << KEY_MODIFIER_CTRL_BIT
| (u32)shift_down << KEY_MODIFIER_SHIFT_BIT
@@ -483,6 +488,8 @@ int main(int argc, char **argv) {
}
glClear(GL_COLOR_BUFFER_BIT);
+ PROFILE_TIME(frame_start);
+
Font *font = ted->font;
if (ted->active_node) {
@@ -570,6 +577,15 @@ int main(int argc, char **argv) {
buffer_check_valid(&ted->line_buffer);
#endif
+
+ PROFILE_TIME(frame_end);
+
+ #if PROFILE
+ {
+ printf("Frame: %.1f ms\n", (frame_end - frame_start) * 1000);
+ }
+ #endif
+
SDL_GL_SwapWindow(window);
}
diff --git a/syntax.c b/syntax.c
index a4378b1..162fe48 100644
--- a/syntax.c
+++ b/syntax.c
@@ -21,12 +21,12 @@ ColorSetting syntax_char_type_to_color(SyntaxCharType t) {
return COLOR_TEXT;
}
-static void syntax_highlight_c(SyntaxStateC *state, char32_t *line, u32 line_len, SyntaxCharType *char_types) {
- (void)state;
- bool in_preprocessor = state->continued_preprocessor;
- bool in_string = state->continued_string;
- bool in_single_line_comment = state->continued_single_line_comment; // this kind of comment :)
- bool in_multi_line_comment = state->multi_line_comment;
+static void syntax_highlight_c(SyntaxState *state_ptr, char32_t *line, u32 line_len, SyntaxCharType *char_types) {
+ SyntaxState state = *state_ptr;
+ bool in_preprocessor = (state & SYNTAX_STATE_PREPROCESSOR) != 0;
+ bool in_string = (state & SYNTAX_STATE_STRING) != 0;
+ bool in_single_line_comment = (state & SYNTAX_STATE_SINGLE_LINE_COMMENT) != 0;
+ bool in_multi_line_comment = (state & SYNTAX_STATE_MULTI_LINE_COMMENT) != 0;
bool in_char = false;
bool in_number = false;
@@ -206,11 +206,11 @@ static void syntax_highlight_c(SyntaxStateC *state, char32_t *line, u32 line_len
char_types[i] = type;
}
}
- state->continued_single_line_comment = backslashes && in_single_line_comment;
- state->continued_preprocessor = backslashes && in_preprocessor;
- state->continued_string = backslashes && in_string;
-
- state->multi_line_comment = in_multi_line_comment;
+ *state_ptr = (SyntaxState)(
+ (backslashes && in_single_line_comment) << SYNTAX_STATE_SINGLE_LINE_COMMENT_SHIFT
+ | (backslashes && in_preprocessor) << SYNTAX_STATE_PREPROCESSOR_SHIFT
+ | (backslashes && in_string) << SYNTAX_STATE_STRING_SHIFT
+ | in_multi_line_comment << SYNTAX_STATE_MULTI_LINE_COMMENT_SHIFT);
}
// This is the main syntax highlighting function. It will determine which colors to use for each character.
@@ -223,7 +223,7 @@ void syntax_highlight(SyntaxState *state, Language lang, char32_t *line, u32 lin
memset(char_types, 0, line_len * sizeof *char_types);
break;
case LANG_C:
- syntax_highlight_c(&state->c, line, line_len, char_types);
+ syntax_highlight_c(state, line, line_len, char_types);
break;
case LANG_COUNT: assert(0); break;
}
diff --git a/ted.cfg b/ted.cfg
index f76c6bd..d16e97d 100644
--- a/ted.cfg
+++ b/ted.cfg
@@ -19,6 +19,7 @@ error-display-time = 10
auto-indent = on
# automatically add a newline at the end of the file on save
auto-add-newline = on
+syntax-highlighting = on
[keyboard]
# motion and selection
diff --git a/ted.h b/ted.h
index 59247fb..a88c460 100644
--- a/ted.h
+++ b/ted.h
@@ -4,16 +4,21 @@
#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;
+enum {
+ SYNTAX_STATE_SINGLE_LINE_COMMENT_SHIFT,
+ SYNTAX_STATE_MULTI_LINE_COMMENT_SHIFT,
+ SYNTAX_STATE_PREPROCESSOR_SHIFT,
+ SYNTAX_STATE_STRING_SHIFT,
+};
+
+enum {
+ SYNTAX_STATE_MULTI_LINE_COMMENT = 1<<SYNTAX_STATE_MULTI_LINE_COMMENT_SHIFT, // are we in a multi-line comment? (delineated by /* */)
+ SYNTAX_STATE_SINGLE_LINE_COMMENT = 1<<SYNTAX_STATE_SINGLE_LINE_COMMENT_SHIFT, // if you add a \ to the end of a single-line comment, it is continued to the next line.
+ SYNTAX_STATE_PREPROCESSOR = 1<<SYNTAX_STATE_PREPROCESSOR_SHIFT, // similar to above
+ SYNTAX_STATE_STRING = 1<<SYNTAX_STATE_STRING_SHIFT,
+};
-typedef union {
- SyntaxStateC c;
-} SyntaxState;
+typedef u16 SyntaxState;
ENUM_U16 {
LANG_NONE,
@@ -49,6 +54,7 @@ typedef struct {
u16 error_display_time;
bool auto_indent;
bool auto_add_newline;
+ bool syntax_highlighting;
u8 tab_width;
u8 cursor_width;
u8 undo_save_time;