kilo

Kilo: minimal text editor written in C
git clone git://git.dimitrijedobrota.com/kilo.git
Log | Files | Refs

commit ee4547f1f7c069b9aebf6899e50025a3f31f9cb3
parent 5be27f6bdaff408ce8ec92b0133876f9085419ea
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Sun,  9 Apr 2023 15:46:55 +0200

Syntax highlighting

Diffstat:
Mkilo.c | 270++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 268 insertions(+), 2 deletions(-)

diff --git a/kilo.c b/kilo.c @@ -51,13 +51,40 @@ enum editorKey { PAGE_DOWN }; +enum editorHighlight { + HL_NORMAL = 0, + HL_COMMENT, + HL_MLCOMMENT, + HL_KEYWORD1, + HL_KEYWORD2, + HL_STRING, + HL_NUMBER, + HL_MATCH +}; + +#define HL_HIGHLIGHT_NUMBERS (1 << 0) +#define HL_HIGHLIGHT_STRINGS (1 << 1) + /*** data ***/ +struct editorSyntax { + char *filetype; + char **filematch; + char **keywords; + char *singleline_comment_start; + char *multiline_comment_start; + char *multiline_comment_end; + int flags; +}; + typedef struct erow { + int idx; int size; int rsize; char *chars; char *render; + unsigned char *hl; + int hl_open_comment; } erow; struct editorConfig { @@ -73,11 +100,28 @@ struct editorConfig { char *filename; char statusmsg[80]; time_t statusmsg_time; + struct editorSyntax *syntax; struct termios orig_termios; }; struct editorConfig E; +/*** filetypes ***/ + +char *C_HL_extensions[] = {".c", ".h", ".cpp", NULL}; +char *C_HL_keywords[] = {"switch", "if", "while", "for", "break", + "continue", "return", "else", "struct", "union", + "typedef", "static", "enum", "class", "case", + "int|", "long|", "double|", "float|", "char|", + "unsigned|", "signed|", "void|", NULL}; + +struct editorSyntax HLDB[] = { + {"c", C_HL_extensions, C_HL_keywords, "//", "/*", "*/", + HL_HIGHLIGHT_NUMBERS | HL_HIGHLIGHT_STRINGS}, +}; + +#define HLDB_ENTRIES (sizeof(HLDB) / sizeof(HLDB[0])) + /*** prototypes ***/ void editorSetStatusMessage(const char *fmt, ...); @@ -196,6 +240,166 @@ int getWindowSize(int *rows, int *cols) { return 0; } +/*** syntax highlighting ***/ + +int is_separator(int c) { + return isspace(c) || c == '\0' || strchr(",.()+-/*=~%<>[];", c) != NULL; +} + +void editorUpdateSyntax(erow *row) { + row->hl = realloc(row->hl, row->rsize); + memset(row->hl, HL_NORMAL, row->rsize); + + if (E.syntax == NULL) return; + + char **keywords = E.syntax->keywords; + + char *scs = E.syntax->singleline_comment_start; + char *mcs = E.syntax->multiline_comment_start; + char *mce = E.syntax->multiline_comment_end; + + int scs_len = scs ? strlen(scs) : 0; + int mcs_len = mcs ? strlen(mcs) : 0; + int mce_len = mce ? strlen(mce) : 0; + + int prev_sep = 1, in_string = 0; + int in_comment = (row->idx > 0 && E.row[row->idx - 1].hl_open_comment); + int i = 0; + while (i < row->rsize) { + char c = row->render[i]; + unsigned char prev_hl = (i > 0) ? row->hl[i - 1] : HL_NORMAL; + + if (scs_len && !in_string && !in_comment) { + if (!strncmp(&row->render[i], scs, scs_len)) { + memset(&row->hl[i], HL_COMMENT, row->rsize - i); + break; + } + } + + if (mcs_len && mce_len && !in_string) { + if (in_comment) { + row->hl[i] = HL_MLCOMMENT; + if (!strncmp(&row->render[i], mce, mce_len)) { + memset(&row->hl[i], HL_MLCOMMENT, mce_len); + i += mce_len; + in_comment = 0; + prev_sep = 1; + continue; + } else { + i++; + continue; + } + } else if (!strncmp(&row->render[i], mcs, mcs_len)) { + memset(&row->hl[i], HL_MLCOMMENT, mcs_len); + i += mcs_len; + in_comment = 1; + continue; + } + } + + if (E.syntax->flags & HL_HIGHLIGHT_STRINGS) { + if (in_string) { + row->hl[i] = HL_STRING; + if (c == '\\' && i + 1 < row->rsize) { + row->hl[i + 1] = HL_STRING; + i += 2; + continue; + } + if (c == in_string) in_string = 0; + i++; + prev_sep = 1; + continue; + } else { + if (c == '"' || c == '\'') { + in_string = c; + row->hl[i] = HL_STRING; + i++; + continue; + } + } + } + + if (E.syntax->flags & HL_HIGHLIGHT_NUMBERS) { + if ((isdigit(c) && (prev_sep || prev_hl == HL_NUMBER)) || + (c == '.' && prev_hl == HL_NUMBER)) { + row->hl[i] = HL_NUMBER; + i++; + prev_sep = 0; + continue; + } + } + + if (prev_sep) { + int j; + + for (j = 0; keywords[j]; j++) { + int klen = strlen(keywords[j]); + int kw2 = keywords[j][klen - 1] == '|'; + if (kw2) klen--; + + if (!strncmp(&row->render[i], keywords[j], klen) && + is_separator(row->render[i + klen])) { + memset(&row->hl[i], kw2 ? HL_KEYWORD2 : HL_KEYWORD1, klen); + i += klen; + break; + } + } + + if (keywords[j] != NULL) { + prev_sep = 0; + continue; + } + } + + prev_sep = is_separator(c); + i++; + } + + int changed = (row->hl_open_comment != in_comment); + row->hl_open_comment = in_comment; + if (changed && row->idx + 1 < E.numrows) + editorUpdateSyntax(&E.row[row->idx + 1]); +} + +int editorSyntaxToColor(int hl) { + switch (hl) { + case HL_COMMENT: + case HL_MLCOMMENT: return 36; + case HL_STRING: return 35; + case HL_KEYWORD1: return 33; + case HL_KEYWORD2: return 32; + case HL_NUMBER: return 31; + case HL_MATCH: return 34; + default: return 37; + } +} + +void editorSelectSyntaxHighlight() { + E.syntax = NULL; + if (E.filename == NULL) return; + + char *ext = strrchr(E.filename, '.'); + + for (unsigned int j = 0; j < HLDB_ENTRIES; j++) { + struct editorSyntax *s = &HLDB[j]; + unsigned int i = 0; + while (s->filematch[i]) { + int is_ext = (s->filematch[i][0] == '.'); + if ((is_ext && ext && !strcmp(ext, s->filematch[i])) || + (!is_ext && strstr(E.filename, s->filematch[i]))) { + E.syntax = s; + + for (int filerow = 0; filerow < E.numrows; filerow++) { + editorUpdateSyntax(&E.row[filerow]); + } + + return; + } + i++; + } + } +} + /*** row operations ***/ int editorRowCxToRx(erow *row, int cx) { @@ -237,6 +441,8 @@ void editorUpdateRow(erow *row) { } row->render[idx] = '\0'; row->rsize = idx; + + editorUpdateSyntax(row); } void editorInsertRow(int at, char *s, size_t len) { @@ -244,6 +450,9 @@ void editorInsertRow(int at, char *s, size_t len) { E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1)); memmove(&E.row[at + 1], &E.row[at], sizeof(erow) * (E.numrows - at)); + for (int j = at + 1; j <= E.numrows; j++) E.row[j].idx++; + + E.row[at].idx = at; E.row[at].size = len; E.row[at].chars = malloc(len + 1); @@ -252,6 +461,8 @@ void editorInsertRow(int at, char *s, size_t len) { E.row[at].rsize = 0; E.row[at].render = NULL; + E.row[at].hl = NULL; + E.row[at].hl_open_comment = 0; editorUpdateRow(&E.row[at]); E.numrows++; @@ -261,12 +472,14 @@ void editorInsertRow(int at, char *s, size_t len) { void editorFreeRow(erow *row) { free(row->render); free(row->chars); + free(row->hl); } void editorDelRow(int at) { if (at < 0 || at >= E.numrows) return; editorFreeRow(&E.row[at]); memmove(&E.row[at], &E.row[at + 1], sizeof(erow) * (E.numrows - at - 1)); + for (int j = at; j < E.numrows - 1; j++) E.row[j].idx--; E.numrows--; E.dirty++; } @@ -358,6 +571,8 @@ void editorOpen(char *filename) { free(E.filename); E.filename = strdup(filename); + editorSelectSyntaxHighlight(); + FILE *fp = fopen(filename, "r"); if (!fp) die("fopen"); @@ -383,6 +598,7 @@ void editorSave() { editorSetStatusMessage("Save aborted"); return; } + editorSelectSyntaxHighlight(); } int len; @@ -410,6 +626,14 @@ void editorSave() { void editorFindCallback(char *query, int key) { static int last_match = -1; static int direction = 1; + static int saved_hl_line; + static char *saved_hl = NULL; + + if (saved_hl) { + memcpy(E.row[saved_hl_line].hl, saved_hl, E.row[saved_hl_line].rsize); + free(saved_hl); + saved_hl = NULL; + } if (key == '\r' || key == '\x1b') { last_match = -1; @@ -441,6 +665,11 @@ void editorFindCallback(char *query, int key) { E.cy = current; E.cx = editorRowRxtoCx(row, match - row->render); E.rowoff = E.numrows; + + saved_hl_line = current; + saved_hl = malloc(row->rsize); + memcpy(saved_hl, row->hl, row->rsize); + memset(&row->hl[match - row->render], HL_MATCH, strlen(query)); break; } } @@ -656,7 +885,41 @@ void editorDrawRows(struct abuf *ab) { } else { int len = E.row[filerow].rsize - E.coloff; CLAMP(len, 0, E.screencols); - abAppend(ab, E.row[filerow].render + E.coloff, len); + char *c = &E.row[filerow].render[E.coloff]; + unsigned char *hl = &E.row[filerow].hl[E.coloff]; + int current_color = -1; + for (int j = 0; j < len; j++) { + if (iscntrl(c[j])) { + char sym = (c[j] <= 26) ? '@' + c[j] : '?'; + abAppend(ab, "\x1b[7m", 4); + abAppend(ab, &sym, 1); + abAppend(ab, "\x1b[m", 3); + + // restore current color as <esc>[m turns off all text formatting, + // including colors + if (current_color != -1) { + char buf[16]; + int clen = snprintf(buf, sizeof(buf), "\x1b[%dm", current_color); + abAppend(ab, buf, clen); + } + } else if (hl[j] == HL_NORMAL) { + if (current_color != -1) { + abAppend(ab, "\x1b[39m", 5); + current_color = -1; + } + abAppend(ab, &c[j], 1); + } else { + int color = editorSyntaxToColor(hl[j]); + if (color != current_color) { + current_color = color; + char buf[16]; + int clen = snprintf(buf, sizeof(buf), "\x1b[%dm", color); + abAppend(ab, buf, clen); + } + abAppend(ab, &c[j], 1); + } + } + abAppend(ab, "\x1b[39m", 5); } abAppend(ab, "\x1b[K", 3); // clear current row abAppend(ab, "\r\n", 2); @@ -669,7 +932,9 @@ void editorDrawStatusBar(struct abuf *ab) { int len = snprintf(status, sizeof(status), "%.20s - %d lines %s", E.filename ? E.filename : "[No Name]", E.numrows, E.dirty ? "(modified)" : ""); - int rlen = snprintf(rstatus, sizeof(rstatus), "%d/%d", E.cy + 1, E.numrows); + int rlen = + snprintf(rstatus, sizeof(rstatus), "%s | %d/%d", + E.syntax ? E.syntax->filetype : "no ft", E.cy + 1, E.numrows); CLAMP_RIGHT(len, E.screencols); abAppend(ab, status, len); while (len < E.screencols) { @@ -738,6 +1003,7 @@ void initEditor() { E.filename = NULL; E.statusmsg[0] = '\0'; E.statusmsg_time = 0; + E.syntax = NULL; if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize"); E.screenrows -= 2;