Files
test/render/render_syntax.c

186 lines
7.0 KiB
C

#include "render.h"
#include <ctype.h>
#include <string.h>
static const char *c_keywords[] = {
"auto", "break", "case", "char", "const", "continue", "default", "do",
"double", "else", "enum", "extern", "float", "for", "goto", "if",
"inline", "int", "long", "register", "restrict", "return", "short",
"signed", "sizeof", "static", "struct", "switch", "typedef", "union",
"unsigned", "void", "volatile", "while", NULL
};
static const char *c_types[] = {
"bool", "size_t", "ssize_t", "ptrdiff_t", "intptr_t", "uintptr_t",
"int8_t", "int16_t", "int32_t", "int64_t",
"uint8_t", "uint16_t", "uint32_t", "uint64_t",
"FILE", "DIR", "time_t", "clock_t", "off_t", "pid_t", "uid_t", "gid_t",
NULL
};
static const char *c_constants[] = {
"NULL", "true", "false", "EOF", "errno",
"STDIN_FILENO", "STDOUT_FILENO", "STDERR_FILENO",
"EXIT_SUCCESS", "EXIT_FAILURE", NULL
};
static int in_word_list(const char *word, int len, const char **list) {
for (int i = 0; list[i]; i++) {
if ((int)strlen(list[i]) == len && strncmp(list[i], word, len) == 0) {
return 1;
}
}
return 0;
}
int render_calc_comment_state(const Editor *ed, int target_line) {
int in_comment = 0;
for (int y = 0; y < target_line && y < ed->num_lines; y++) {
const char *line = ed->lines[y];
int len = (int)strlen(line);
int i = 0;
while (i < len) {
if (in_comment) {
if (line[i] == '*' && i + 1 < len && line[i + 1] == '/') {
in_comment = 0;
i += 2;
} else {
i++;
}
} else {
if (line[i] == '/' && i + 1 < len && line[i + 1] == '/') {
break;
} else if (line[i] == '/' && i + 1 < len && line[i + 1] == '*') {
in_comment = 1;
i += 2;
} else if (line[i] == '"' || line[i] == '\'') {
char quote = line[i++];
while (i < len && line[i] != quote) {
if (line[i] == '\\' && i + 1 < len) i++;
i++;
}
if (i < len) i++;
} else {
i++;
}
}
}
}
return in_comment;
}
void render_syntax_line(Editor *ed, const char *line, int scroll_x, int x, int y, int max_chars, int *in_comment) {
int len = (int)strlen(line);
int start_byte = utf8_byte_offset(line, scroll_x);
int end_byte = utf8_byte_offset(line, scroll_x + max_chars);
int first_non_space = 0;
while (first_non_space < len && isspace((unsigned char)line[first_non_space])) {
first_non_space++;
}
if (!(*in_comment) && first_non_space < len && line[first_non_space] == '#') {
if (start_byte < len) {
XftDrawStringUtf8(ed->xft_draw, &ed->xft_prep, ed->font, x, y,
(const FcChar8 *)(line + start_byte), end_byte - start_byte);
}
return;
}
int i = 0;
while (i < len) {
int tok_start = i;
int tok_end = i;
XftColor *color = &ed->xft_fg;
if (*in_comment) {
color = &ed->xft_mcom;
while (tok_end < len) {
if (line[tok_end] == '*' && tok_end + 1 < len && line[tok_end + 1] == '/') {
tok_end += 2;
*in_comment = 0;
break;
}
tok_end++;
}
} else if (line[i] == '/' && i + 1 < len && line[i + 1] == '/') {
color = &ed->xft_com;
tok_end = len;
} else if (line[i] == '/' && i + 1 < len && line[i + 1] == '*') {
color = &ed->xft_mcom;
tok_end += 2;
*in_comment = 1;
while (tok_end < len) {
if (line[tok_end] == '*' && tok_end + 1 < len && line[tok_end + 1] == '/') {
tok_end += 2;
*in_comment = 0;
break;
}
tok_end++;
}
} else if (line[i] == '"' || line[i] == '\'') {
char quote = line[i];
tok_end++;
while (tok_end < len && line[tok_end] != quote) {
if (line[tok_end] == '\\' && tok_end + 1 < len) tok_end++;
tok_end++;
}
if (tok_end < len) tok_end++;
color = &ed->xft_str;
} else if (isdigit((unsigned char)line[i])) {
color = &ed->xft_num;
if (line[i] == '0' && i + 1 < len && (line[i + 1] == 'x' || line[i + 1] == 'X')) {
tok_end += 2;
while (tok_end < len && (isxdigit((unsigned char)line[tok_end]) || line[tok_end] == '_')) {
tok_end++;
}
} else if (line[i] == '0' && i + 1 < len && (line[i + 1] == 'b' || line[i + 1] == 'B')) {
tok_end += 2;
while (tok_end < len && (line[tok_end] == '0' || line[tok_end] == '1' || line[tok_end] == '_')) {
tok_end++;
}
} else {
while (tok_end < len && (isdigit((unsigned char)line[tok_end]) || line[tok_end] == '.' || line[tok_end] == '_')) {
tok_end++;
}
if (tok_end < len && (line[tok_end] == 'e' || line[tok_end] == 'E')) {
tok_end++;
if (tok_end < len && (line[tok_end] == '+' || line[tok_end] == '-')) tok_end++;
while (tok_end < len && isdigit((unsigned char)line[tok_end])) tok_end++;
}
}
while (tok_end < len && strchr("uUlLfF", line[tok_end])) {
tok_end++;
}
} else if (isalpha((unsigned char)line[i]) || line[i] == '_') {
while (tok_end < len && (isalnum((unsigned char)line[tok_end]) || line[tok_end] == '_')) {
tok_end++;
}
int wlen = tok_end - tok_start;
if (in_word_list(&line[tok_start], wlen, c_keywords)) {
color = &ed->xft_kw;
} else if (in_word_list(&line[tok_start], wlen, c_types)) {
color = &ed->xft_type;
} else if (in_word_list(&line[tok_start], wlen, c_constants)) {
color = &ed->xft_num;
} else {
color = &ed->xft_fg;
}
} else {
tok_end = utf8_next_char_pos(line, i);
}
if (start_byte < len) {
int vis_start = (tok_start > start_byte) ? tok_start : start_byte;
int vis_end = (tok_end < end_byte) ? tok_end : end_byte;
if (vis_start < vis_end) {
int col_offset = utf8_char_count(line + start_byte, vis_start - start_byte);
XftDrawStringUtf8(ed->xft_draw, color, ed->font,
x + col_offset * ed->char_w, y,
(const FcChar8 *)&line[vis_start], vis_end - vis_start);
}
}
i = tok_end;
}
}