+/Femto

toy text editor

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

Joseph RyanFixup973c5b9

master
10.3 KiB400 linesraw
1#include "../inc/editor.h"
2#define CTRLKEY(k) ((k)&0x1f)
3
4struct editorConfig E;
5
6extern int ENABLE_LINE_NUMS;
7extern int LINE_NUM_WIDTH;
8
9void initEditor() {
10    for (int i = 0; i < E.numrows; ++i) { freeRow(&E.row[i]); }
11    E.cx = 0;
12    E.oldcx = 0;
13    E.cy = 0;
14    E.numrows = 0;
15    E.scrollRow = 0;
16    E.scrollCol = 0;
17    E.row = NULL;
18    E.filename = NULL;
19    E.tabSize = 4;
20    E.dirty = 0;
21    E.statusmsg[0] = '\0';
22    E.messageTime = 0;
23    E.syntax = NULL;
24    if (getWindowSize(&E.height, &E.width) == -1) { die("getWindowSize"); }
25    E.height -= 2;
26}
27
28void setStatusMessage(const char* fmt, ...) {
29    va_list ap;
30    va_start(ap, fmt);
31    vsnprintf(E.statusmsg, sizeof(E.statusmsg), fmt, ap);
32    va_end(ap);
33    E.messageTime = time(NULL);
34}
35
36char* prompt(char* prompt, void (*callback)(char*, int)) {
37    size_t bufsize = 80;
38    char* buf = malloc(bufsize);
39    size_t buflen = 0;
40    buf[buflen] = '\0';
41
42    while (1) {
43        setStatusMessage(prompt, buf);
44        refresh();
45        int c = readKey();
46        if (c == '\x1b' || c == CTRLKEY('q')) {
47            setStatusMessage("");
48            if (callback) { callback(buf, c); }
49            free(buf);
50            return NULL;
51        } else if (c == '\r') {
52            if (buflen > 0) {
53                setStatusMessage("");
54                if (callback) { callback(buf, c); }
55                return buf;
56            }
57        } else if (c == BACKSPACE || c == DEL_KEY || c == CTRLKEY('h')) {
58            if (buflen > 0) { buf[--buflen] = '\0'; }
59        } else if (!iscntrl(c) && c < 128) {
60            if (buflen == bufsize - 1) {
61                bufsize *= 2;
62                buf = realloc(buf, bufsize);
63            }
64            buf[buflen++] = c;
65            buf[buflen] = '\0';
66        }
67        if (callback) { callback(buf, c); }
68    }
69}
70
71void findcb(char* query, int key) {
72    static int last = -1;
73    static int dir = 1;
74
75    if (key == '\r' || key == '\x1b' || key == CTRLKEY('q')) {
76        last = -1;
77        dir = 1;
78        return;
79    } else if (key == ARROW_UP || key == ARROW_LEFT) {
80        dir = -1;
81    } else if (key == ARROW_DOWN || key == ARROW_RIGHT) {
82        dir = 1;
83    } else {
84        last = -1;
85    }
86
87    if (last == -1) { dir = 1; }
88    int current = last;
89    for (int i = 0; i < E.numrows; ++i) {
90        current += dir;
91        if (current == -1) {
92            current = E.numrows - 1;
93        } else if (current == E.numrows) {
94            current = 0;
95        }
96        erow* row = &E.row[current];
97        char* match = strstr(row->text, query);
98        if (match) {
99            last = current;
100            E.cy = current;
101            E.cx = match - row->text;
102            E.scrollRow = E.numrows;
103            break;
104        }
105    }
106}
107
108void find() {
109    int save_cx = E.cx;
110    int save_cy = E.cy;
111    int save_scrollRow = E.scrollRow;
112    int save_scrollCol = E.scrollCol;
113    char* query =
114        prompt("Find: %s (Arrows/Enter to select, ESC to cancel)", findcb);
115    if (query) {
116        free(query);
117    } else {
118        E.cx = save_cx;
119        E.cy = save_cy;
120        E.scrollRow = save_scrollRow;
121        E.scrollCol = save_scrollCol;
122    }
123}
124
125void insertRow(int at, char* s, size_t len) {
126    if (at > E.numrows) { return; }
127    E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1));
128    memmove(&E.row[at + 1], &E.row[at], (E.numrows - at) * sizeof(erow));
129    for (int i = at + 1; i <= E.numrows; ++i) { ++E.row[i].index; }
130    E.row[at].index = at;
131    E.row[at].size = len;
132    E.row[at].text = malloc(len + 1);
133    memcpy(E.row[at].text, s, len);
134    E.row[at].text[len] = '\0';
135    E.row[at].hl = NULL;
136    E.row[at].unclosed = 0;
137    renderRow(&E.row[at]);
138    ++E.numrows;
139    ++E.dirty;
140    if (ENABLE_LINE_NUMS) {
141        int temp = E.numrows;
142        for (int i = LINE_NUM_WIDTH; i > 0; --i) { temp /= 10; }
143        if (temp != 0) { ++LINE_NUM_WIDTH; }
144    }
145}
146
147void deleteRow(int at) {
148    if (at >= E.numrows) { return; }
149    freeRow(&E.row[at]);
150    memmove(&E.row[at], &E.row[at + 1], (E.numrows - at - 1) * sizeof(erow));
151    for (int i = at; i < E.numrows - 1; ++i) { --E.row[i].index; }
152    --E.numrows;
153    ++E.dirty;
154    if (ENABLE_LINE_NUMS) {
155        int temp = E.numrows;
156        for (int i = LINE_NUM_WIDTH - 1; i > 0; --i) { temp /= 10; }
157        if (temp == 0) { --LINE_NUM_WIDTH; }
158    }
159}
160
161void freeRow(erow* row) {
162    free(row->text);
163    free(row->hl);
164}
165
166void moveCursor(int key) {
167    erow* row = E.cy >= E.numrows ? NULL : &E.row[E.cy];
168    switch (key) {
169    case ARROW_UP:
170        if (E.cy > 0) {
171            --E.cy;
172            E.cx = E.oldcx;
173            row = &E.row[E.cy];
174            int tabs = countTabs(row->text, row->size);
175            if (E.cx < tabs * E.tabSize) { E.cx -= E.cx % E.tabSize; }
176        } else {
177            E.cx = 0;
178        }
179        break;
180    case ARROW_DOWN:
181        if (E.cy < E.numrows) {
182            ++E.cy;
183            if (E.cy < E.numrows) {
184                E.cx = E.oldcx;
185                row = &E.row[E.cy];
186                int tabs = countTabs(row->text, row->size);
187                if (E.cx < tabs * E.tabSize) { E.cx -= E.cx % E.tabSize; }
188            }
189        }
190        break;
191    case ARROW_LEFT:
192        if (E.cx > 0) {
193            int tabs = countTabs(row->text, row->size);
194            --E.cx;
195            while (E.cx < tabs * E.tabSize && E.cx % E.tabSize != 0) { --E.cx; }
196        } else if (E.cy > 0) {
197            --E.cy;
198            E.cx = E.row[E.cy].size;
199        }
200        E.oldcx = E.cx;
201        break;
202    case ARROW_RIGHT:
203        if (row && E.cx < row->size) {
204            int tabs = countTabs(row->text, row->size);
205            ++E.cx;
206            while (E.cx < tabs * E.tabSize && E.cx % E.tabSize != 0) { ++E.cx; }
207        } else if (row && E.cx == row->size) {
208            ++E.cy;
209            E.cx = 0;
210        }
211        E.oldcx = E.cx;
212        break;
213    }
214    row = E.cy >= E.numrows ? NULL : &E.row[E.cy];
215    int rowlen = row ? row->size : 0;
216    if (E.cx > rowlen) { E.cx = rowlen; }
217}
218
219void rowInsertChar(erow* row, int at, char c) {
220    if (at > row->size) { at = row->size; }
221    row->text = realloc(row->text, row->size + 2);
222    memmove(&row->text[at + 1], &row->text[at], row->size - at + 1);
223    ++row->size;
224    row->text[at] = c;
225    renderRow(row);
226}
227
228void rowDeleteChar(erow* row, int at) {
229    if (at >= row->size) { return; }
230    memmove(&row->text[at], &row->text[at + 1], row->size - at);
231    --row->size;
232    renderRow(row);
233    ++E.dirty;
234}
235
236void rowAppendStr(erow* row, char* s, size_t len) {
237    row->text = realloc(row->text, row->size + len + 1);
238    memcpy(&row->text[row->size], s, len);
239    row->size += len;
240    row->text[row->size] = '\0';
241    renderRow(row);
242    ++E.dirty;
243}
244
245void insertNewline() {
246    if (E.cx == 0) {
247        insertRow(E.cy, "", 0);
248    } else {
249        erow* row = &E.row[E.cy];
250        insertRow(E.cy + 1, &row->text[E.cx], row->size - E.cx);
251        row = &E.row[E.cy];
252        row->size = E.cx;
253        row->text[row->size] = '\0';
254        renderRow(row);
255    }
256    E.cx = 0;
257    int tabs = countTabs(E.row[E.cy].text, E.row[E.cy].size) * E.tabSize;
258    ++E.cy;
259    while (tabs) {
260        insertChar(' ');
261        --tabs;
262    }
263}
264
265void insertChar(int c) {
266    ++E.dirty;
267    if (E.cy == E.numrows) { insertRow(E.numrows, "", 0); }
268    rowInsertChar(&E.row[E.cy], E.cx, c);
269    ++E.cx;
270    E.oldcx = E.cx;
271}
272
273void deleteChar() {
274    if ((E.cx == 0 && E.cy == 0) || E.cy == E.numrows) { return; }
275    erow* row = &E.row[E.cy];
276    if (E.cx > 0) {
277        int tabs = countTabs(row->text, row->size);
278        rowDeleteChar(row, E.cx - 1);
279        --E.cx;
280        if (E.cx < tabs * E.tabSize) {
281            while (E.cx % E.tabSize != 0) {
282                rowDeleteChar(row, E.cx - 1);
283                --E.cx;
284            }
285        }
286    } else {
287        E.cx = E.row[E.cy - 1].size;
288        rowAppendStr(&E.row[E.cy - 1], row->text, row->size);
289        deleteRow(E.cy);
290        --E.cy;
291    }
292    E.oldcx = E.cx;
293}
294
295int countTabs(char* s, size_t len) {
296    int indent;
297    for (indent = 0; indent < (int)len && s[indent] == ' '; ++indent)
298        ;
299    return indent / E.tabSize;
300}
301
302void processKeypress() {
303    static int quit_attempt = 0;
304    int c = readKey();
305
306    switch (c) {
307    case '\r': insertNewline(); break;
308
309    case '\t': do { insertChar(' ');
310        } while (E.cx % E.tabSize != 0);
311        break;
312
313    case BACKSPACE:
314    case DEL_KEY:
315    case CTRLKEY('h'):
316        if (c == DEL_KEY) { moveCursor(ARROW_RIGHT); }
317        deleteChar();
318        break;
319
320    case '\x1b':
321    case CTRLKEY('l'): break;
322
323    case CTRLKEY('q'):
324        if (!quit_attempt && E.dirty) {
325            quit_attempt = 1;
326            setStatusMessage(
327                "WARNING: Unsaved changes! Press CTRL-Q again to force quit.");
328            return;
329        }
330        exit(0);
331        break;
332
333    case CTRLKEY('n'):
334        if (!quit_attempt && E.dirty) {
335            quit_attempt = 1;
336            setStatusMessage(
337                "WARNING: Unsaved changes! Press CTRL-N again to discard.");
338            return;
339        }
340        initEditor();
341        break;
342
343    case CTRLKEY('o'):
344        if (!quit_attempt && E.dirty) {
345            quit_attempt = 1;
346            setStatusMessage(
347                "WARNING: Unsaved changes! Press CTRL-O again to discard.");
348            return;
349        }
350        char* newFile = prompt("Open file: %s (ESC to cancel)", NULL);
351        if (newFile != NULL) {
352            initEditor();
353            openFile(newFile);
354        }
355        break;
356
357    case CTRLKEY('s'):
358        if (E.dirty) { saveFile(); }
359        break;
360
361    case CTRLKEY('f'): find(); break;
362
363    case CTRLKEY('r'):
364        if (getWindowSize(&E.height, &E.width) == -1) { die("getWindowSize"); }
365        E.height -= 2;
366        break;
367
368    case ARROW_UP:
369    case ARROW_DOWN:
370    case ARROW_RIGHT:
371    case ARROW_LEFT: moveCursor(c); break;
372
373    case PAGE_UP:
374    case PAGE_DOWN: {
375        if (c == PAGE_UP) {
376            E.cy = E.scrollRow;
377        } else {
378            E.cy = E.scrollRow + E.height - 1;
379            if (E.cy > E.numrows) { E.cy = E.numrows; }
380        }
381        int times = E.height;
382        while (--times) { moveCursor(c == PAGE_UP ? ARROW_UP : ARROW_DOWN); }
383    } break;
384
385    case HOME_KEY:
386        E.cx = 0;
387        E.oldcx = 0;
388        break;
389
390    case END_KEY:
391        if (E.cy < E.numrows) {
392            E.cx = E.row[E.cy].size;
393            E.oldcx = E.cx;
394        }
395        break;
396
397    default: insertChar(c); break;
398    }
399    quit_attempt = 0;
400}