This commit is contained in:
Tyrel Souza 2021-10-23 00:09:01 -04:00
parent 510faabc16
commit f03f084d72

98
tted.c
View File

@ -19,7 +19,7 @@
/** defines */
#define TTED_VERSION "0.0.1"
#define TTED_TAB_STOP 8
#define TTED_QUIT_TIMES 3
#define TTED_QUIT_TIMES 1
#define CTRL_KEY(k) ((k) & 0x1f)
enum editorKey {
@ -35,13 +35,28 @@ enum editorKey {
PAGE_DOWN
};
enum editorHighlight {
HL_NORMAL = 0,
HL_NUMBER,
HL_MATCH
};
#define HL_HIGHLIGHT_NUMBERS (1<<0)
/** data */
struct editorSyntax {
char *filetype;
char **filematch;
int flags;
};
typedef struct erow {
int size;
int rsize;
char *chars;
char *render;
unsigned char *hl;
} erow;
struct editorConfig {
@ -186,6 +201,45 @@ int getWindowSize(int *rows, int *cols) {
}
}
/** 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);
int prev_sep = 1;
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 ((isdigit(c) && (prev_sep || prev_hl == HL_NUMBER))||
(c == '.' && prev_hl == HL_NUMBER)){
row->hl[i] = HL_NUMBER;
i++;
prev_sep = 0;
continue;
}
prev_sep = is_separator(c);
i++;
}
}
int editorSyntaxToColor(int hl){
switch(hl){
case HL_NUMBER: return 31;
case HL_MATCH: return 34;
default: return 37;
}
}
/** row operations */
int editorRowCxToRx(erow *row, int cx) {
int rx = 0;
@ -235,6 +289,8 @@ void editorUpdateRow(erow *row) {
}
row->render[idx] = '\0';
row->rsize = idx;
editorUpdateSyntax(row);
}
@ -249,6 +305,7 @@ void editorInsertRow(int at, char *s, size_t len) {
E.row[at].chars[len] = '\0';
E.row[at].rsize = 0;
E.row[at].render = NULL;
E.row[at].hl = NULL;
editorUpdateRow(&E.row[at]);
E.numrows++;
E.dirty++;
@ -257,6 +314,7 @@ 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) {
@ -412,6 +470,15 @@ 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;
direction = 1;
@ -446,6 +513,11 @@ void editorFindCallback(char *query, int key) {
E.cy = current;
E.cx = editorRowCxToRx(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;
}
}
@ -538,7 +610,29 @@ void editorDrawRows(struct abuf *ab) {
int len = E.row[filerow].rsize - E.coloff;
if (len < 0) len = 0;
if (len > E.screencols) len = E.screencols; // truncate if further than columns
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;
int j;
for (j=0;j< len;j++){
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);