+/Femto

toy text editor

git clone https://git.julia.blue/+/Femto

Joseph RyanFixup973c5b9

master
10.7 KiB320 linesraw
1#include "../inc/display.h"
2struct editorSyntax HL_SETTINGS[NUM_SYNTAX];
3
4int ENABLE_LINE_NUMS = 1;
5int LINE_NUM_WIDTH = 0;
6int MESSAGE_TIMER = 3;
7
8char* ckw[] = {
9    "auto",     "break",    "case",     "char",     "const",     "continue",
10    "default",  "do",       "double",   "else",     "enum",      "extern",
11    "float",    "for",      "goto",     "if",       "int",       "long",
12    "register", "return",   "short",    "signed",   "sizeof",    "static",
13    "struct",   "switch",   "typedef",  "union",    "unsigned",  "void",
14    "volatile", "while",    "getchar|", "putchar|", "printf|",   "scanf|",
15    "strcat|",  "strcmp|",  "strcpy|",  "isdigit|", "isalpha|",  "isalnum|",
16    "islower|", "isupper|", "acos|",    "asin|",    "atan|",     "cos|",
17    "exp|",     "fabs|",    "sqrt|",    "time|",    "difftime|", "clock|",
18    "malloc|",  "realloc|", "rand|",    "srand|",   NULL};
19
20char* pykw[] = {
21    "and",    "assert",      "break",      "class",  "continue",   "def",
22    "del",    "elif",        "else",       "except", "exec",       "finally",
23    "for",    "from",        "global",     "if",     "import",     "in",
24    "is",     "lambda",      "not",        "or",     "pass",       "print",
25    "raise",  "return",      "try",        "while",  "abs|",       "all|",
26    "any|",   "bin|",        "bool|",      "char|",  "cmp|",       "dict|",
27    "dir|",   "divmod|",     "enumerate|", "eval|",  "file|",      "filter|",
28    "float|", "format|",     "hash|",      "help|",  "hex|",       "id|",
29    "input|", "int|",        "iter|",      "len|",   "list|",      "long|",
30    "map|",   "max|",        "min|",       "next|",  "oct|",       "open|",
31    "ord|",   "pow|",        "print|",     "range|", "raw_input|", "reduce|",
32    "repr|",  "reversed|",   "set|",       "slice|", "sorted|",    "str|",
33    "sum|",   "super|",      "tuple|",     "type|",  "unicode|",   "xrange|",
34    "zip|",   "__import__|", NULL};
35
36void initDisplay() {
37    struct editorSyntax* s = &HL_SETTINGS[0];
38    s->filetype = "C";
39    s->filematch = "c h cpp";
40    s->slCommentStart = "//";
41    s->mlCommentStart = "/*";
42    s->mlCommentEnd = "*/";
43    s->keywords = ckw;
44    s->flags = SETTING_NUMBER | SETTING_STRING;
45    s = &HL_SETTINGS[1];
46    s->filetype = "Python";
47    s->filematch = "py";
48    s->slCommentStart = "#";
49    s->mlCommentStart = NULL;
50    s->mlCommentEnd = NULL;
51    s->keywords = pykw;
52    s->flags = SETTING_NUMBER | SETTING_STRING;
53}
54
55int is_separator(int c) {
56    return isspace(c) || c == '\0' || strchr(",.()+-/*=~%<>[];", c) != NULL;
57}
58
59void renderRow(struct erow* row) {
60    row->hl = realloc(row->hl, row->size);
61    memset(row->hl, HL_NORMAL, row->size);
62    if (E.syntax == NULL) { return; }
63
64    int sep = 1;
65    char instr = 0;
66    int incom = row->index > 0 && E.row[row->index - 1].unclosed;
67    char* scs = E.syntax->slCommentStart;
68    char* mcs = E.syntax->mlCommentStart;
69    char* mce = E.syntax->mlCommentEnd;
70    int scslen = scs ? strlen(scs) : 0;
71    int mcslen = mcs ? strlen(mcs) : 0;
72    int mcelen = mce ? strlen(mcs) : 0;
73    char** kw = E.syntax->keywords;
74    int i = 0;
75    while (i < row->size) {
76        char c = row->text[i];
77        char prev = i == 0 ? HL_NORMAL : row->hl[i - 1];
78
79        if (scslen && !instr && !incom) {
80            if (!strncmp(&row->text[i], scs, scslen)) {
81                memset(&row->hl[i], HL_COMMENT, row->size - i);
82                break;
83            }
84        }
85
86        if (mcslen && mcelen && !instr) {
87            if (incom) {
88                row->hl[i] = HL_MLCOMMENT;
89                if (!strncmp(mce, &row->text[i], mcelen)) {
90                    memset(&row->hl[i], HL_MLCOMMENT, mcelen);
91                    i += mcelen;
92                    incom = 0;
93                    sep = 1;
94                    continue;
95                } else {
96                    ++i;
97                    continue;
98                }
99            } else if (!strncmp(mcs, &row->text[i], mcslen)) {
100                memset(&row->hl[i], HL_MLCOMMENT, mcslen);
101                i += mcslen;
102                incom = 1;
103                continue;
104            }
105        }
106
107        if (E.syntax->flags & SETTING_STRING) {
108            if (instr) {
109                if (c == '\\') {
110                    row->hl[i] = HL_ESCAPE;
111                    row->hl[i + 1] = HL_ESCAPE;
112                    i += 2;
113                    continue;
114                }
115                if (c == instr) {
116                    instr = 0;
117                    sep = 1;
118                }
119                row->hl[i] = HL_STRING;
120                ++i;
121                continue;
122            } else {
123                if (c == '"' || c == '\'') {
124                    instr = c;
125                    row->hl[i] = HL_STRING;
126                    ++i;
127                    continue;
128                }
129            }
130        }
131
132        if (E.syntax->flags & SETTING_NUMBER) {
133            if ((isdigit(c) && (sep || prev == HL_NUMBER)) ||
134                (prev == HL_NUMBER && c == '.')) {
135                row->hl[i] = HL_NUMBER;
136                sep = 0;
137                ++i;
138                continue;
139            }
140        }
141
142        if (sep) {
143            int j;
144            for (j = 0; kw[j]; ++j) {
145                int kwlen = strlen(kw[j]);
146                int kw2 = kw[j][kwlen - 1] == '|';
147                kwlen -= kw2;
148                if (!strncmp(&row->text[i], kw[j], kwlen) &&
149                    is_separator(row->text[i + kwlen])) {
150                    memset(&row->hl[i], kw2 ? HL_KEYWORD_2 : HL_KEYWORD_1,
151                           kwlen);
152                    i += kwlen;
153                    break;
154                }
155            }
156            if (kw[j] != NULL) { continue; }
157        }
158
159        sep = is_separator(c);
160        ++i;
161    }
162
163    int changed = incom != row->unclosed;
164    row->unclosed = incom;
165    if (changed && row->index + 1 < E.numrows) {
166        renderRow(&E.row[row->index + 1]);
167    }
168}
169
170int getColor(int hl) {
171    switch (hl) {
172    case HL_NUMBER: return 31;
173    case HL_MATCH: return 34;
174    case HL_STRING: return 35;
175    case HL_ESCAPE: return 34;
176    case HL_COMMENT: return 36;
177    case HL_MLCOMMENT: return 36;
178    case HL_KEYWORD_1: return 32;
179    case HL_KEYWORD_2: return 33;
180    default: return 37;
181    }
182}
183
184void scroll() {
185    if (E.cy < E.scrollRow) { E.scrollRow = E.cy; }
186    if (E.cy >= E.scrollRow + E.height) { E.scrollRow = (E.cy - E.height) + 1; }
187    if (E.cx < E.scrollCol) { E.scrollCol = E.cx; }
188    if (E.cx >= E.scrollCol + E.width - LINE_NUM_WIDTH) {
189        E.scrollCol = (E.cx - (E.width - LINE_NUM_WIDTH)) + 1;
190    }
191}
192
193void drawWelcome(abuf* ab) {
194    char welcome[] = "Welcome to Femto!";
195    int welcomelen = sizeof(welcome);
196    if (welcomelen > E.width) { welcomelen = E.width; }
197    int padding = (E.width - welcomelen) / 2;
198    if (padding) {
199        abappend(ab, "~", 1);
200        --padding;
201        while (padding) {
202            abappend(ab, " ", 1);
203            --padding;
204        }
205    }
206    abappend(ab, welcome, welcomelen);
207}
208
209void drawStatus(abuf* ab) {
210    abappend(ab, "\x1b[7m", 4);
211    char status[80], rstatus[80];
212    char* temp;
213    if (E.filename) {
214        temp = &E.filename[strlen(E.filename) - 20];
215        if (strlen(E.filename) < 20) { temp = E.filename; }
216    }
217    snprintf(status, sizeof(status), "%s - %d lines %s",
218             E.filename ? temp : "[No Name]", E.numrows,
219             E.dirty ? "(modified)" : "");
220    int len = strlen(status);
221    int rlen =
222        snprintf(rstatus, sizeof(rstatus), "Lang-%s | %d:%d",
223                 E.syntax ? E.syntax->filetype : "???", E.cy + 1, E.cx + 1);
224    if (len > E.width) { len = E.width; }
225    abappend(ab, status, len);
226    while (len < E.width) {
227        if (E.width - len == rlen) {
228            abappend(ab, rstatus, rlen);
229            break;
230        }
231        abappend(ab, " ", 1);
232        ++len;
233    }
234    abappend(ab, "\x1b[m", 3);
235    abappend(ab, "\r\n", 2);
236}
237
238void drawMessageBar(abuf* ab) {
239    abappend(ab, "\x1b[K", 3);
240    int msglen = strlen(E.statusmsg);
241    if (msglen > E.width) { msglen = E.width; }
242    if (msglen && time(NULL) - E.messageTime < MESSAGE_TIMER) {
243        abappend(ab, E.statusmsg, msglen);
244    }
245}
246
247void drawRows(abuf* ab) {
248    for (int i = 0; i < E.height; ++i) {
249        int currentRow = E.scrollRow + i;
250        if (currentRow >= E.numrows) {
251            if (E.numrows == 0 && i == E.height / 3) {
252                drawWelcome(ab);
253            } else {
254                abappend(ab, "~", 1);
255            }
256        } else {
257            char lineNum[16];
258            if (ENABLE_LINE_NUMS && LINE_NUM_WIDTH) {
259                snprintf(lineNum, sizeof(lineNum), "%08d", E.scrollRow + i + 1);
260                char buf[32];
261                snprintf(buf, sizeof(buf), "\x1b[7m%s\x1b[m",
262                         &lineNum[8 - LINE_NUM_WIDTH]);
263                abappend(ab, buf, strlen(buf));
264            }
265            int len = E.row[currentRow].size - E.scrollCol;
266            if (len < 0) { len = 0; }
267            if (len > E.width - LINE_NUM_WIDTH) {
268                len = E.width - LINE_NUM_WIDTH;
269            }
270            int color = -1;
271            char* c = &E.row[currentRow].text[E.scrollCol];
272            char* hl = &E.row[currentRow].hl[E.scrollCol];
273            for (int j = 0; j < len; ++j) {
274                if (iscntrl(c[j])) {
275                    char sym = (c[j] <= 26) ? '@' + c[j] : '?';
276                    abappend(ab, "\x1b[7m", 4);
277                    abappend(ab, &sym, 1);
278                    abappend(ab, "\x1b[m", 3);
279                    if (hl[j] != -1) {
280                        char buf[16];
281                        int clen = snprintf(buf, sizeof(buf), "\x1b[%dm",
282                                            getColor(hl[j]));
283                        abappend(ab, buf, clen);
284                    }
285                    continue;
286                }
287                if (hl[j] == HL_NORMAL) {
288                    color = -1;
289                    abappend(ab, "\x1b[39m", 5);
290                } else if (hl[j] != color) {
291                    color = hl[j];
292                    char buf[16];
293                    int clen =
294                        snprintf(buf, sizeof(buf), "\x1b[%dm", getColor(hl[j]));
295                    abappend(ab, buf, clen);
296                }
297                abappend(ab, &c[j], 1);
298            }
299            abappend(ab, "\x1b[39m", 5);
300        }
301        abappend(ab, "\x1b[K\r\n", 5);
302    }
303}
304
305void refresh() {
306    scroll();
307    abuf ab = ABUF_INIT;
308    abappend(&ab, "\x1b[?25l", 6);
309    abappend(&ab, "\x1b[H", 3);
310    drawRows(&ab);
311    drawStatus(&ab);
312    drawMessageBar(&ab);
313    char buf[16];
314    snprintf(buf, sizeof(buf), "\x1b[%d;%dH", E.cy - E.scrollRow + 1,
315             E.cx - E.scrollCol + 1 + (ENABLE_LINE_NUMS ? LINE_NUM_WIDTH : 0));
316    abappend(&ab, buf, strlen(buf));
317    abappend(&ab, "\x1b[?25h", 6);
318    write(STDOUT_FILENO, ab.b, ab.len);
319    abfree(&ab);
320}