-
Notifications
You must be signed in to change notification settings - Fork 0
/
kilo.c
346 lines (283 loc) · 8.76 KB
/
kilo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#define _DEFAULT_SOURCE
#define _BSD_SOURCE
#define _GNU_SOURCE
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#define CTRL_KEY(k) ((k) & 0x1f)
#define BUFFER_INIT {NULL, 0}
#define KILO_VERSION "0.0.1"
typedef struct editorRow {
int size;
char *chars;
} editorRow;
struct editorConfig {
int cx, cy; // cursor coordenates
int screenrows;
int screencols;
int numRows;
int rowOffset;
int numColumns;
int columnsOffset;
editorRow *row;
struct termios originalTermAttrs;
};
struct editorConfig state;
enum moveKeys {
MOVE_LEFT = 'h',
MOVE_RIGHT = 'l',
MOVE_UP = 'k',
MOVE_DOWN = 'j'
};
struct buffer {
char *b;
int len;
};
void clearEditorScreen();
void appendToBuffer(struct buffer *buf, const char *s, int len) {
char *new = realloc(buf->b, buf->len + len);
if (new == NULL) return;
memcpy(&new[buf->len], s, len);
buf->b = new;
buf->len += len;
}
void freeBuffer(struct buffer *buf) {
free(buf->b);
}
void die(const char *s) {
clearEditorScreen();
perror(s);
exit(1);
}
void disableRawMode() {
if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &state.originalTermAttrs) == -1) die("tcsetattr");
}
// enable raw mode
void enableCustomTerminalMode() {
// read terminal settings into "originalTermAttrs"
if(tcgetattr(STDIN_FILENO, &state.originalTermAttrs) == -1) die("tcgetattr");
// rollback terminal changes when program exits
atexit(disableRawMode);
struct termios termAttrs = state.originalTermAttrs;
// disable ctrl+s/ctrl+q freezing data from being transmitted to the terminal
termAttrs.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
// disable output processing like: `\n` to `\r\n`. `\r` carriage return, it moves cursor to the begining of the current line
termAttrs.c_oflag &= ~(OPOST);
// disable miscellaneous flags
termAttrs.c_cflag |= (CS8);
// disable echoing mode, canonical mode and signint and also sigtstp
termAttrs.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
// read() returns if it doesnt get any input for a certain amount of time
termAttrs.c_cc[VMIN] = 0; // min. number of bytes input needed before read() can return
termAttrs.c_cc[VTIME] = 1; // max. amount of time to wait before read() returns
// apply changes to the terminal
if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &termAttrs) == -1) die("tcsetattr");
}
char readKeysFromInput() {
int nread;
char c;
while ((nread = read(STDIN_FILENO, &c, 1)) != 1) {
if (nread == -1 && errno != EAGAIN) die("read");
}
if (c == '\x1b') {
// read and map arrow keys to keys defined in moveKeys
char seq[3];
if (read(STDIN_FILENO, &seq[0], 1) != 1) return '\x1b';
if (read(STDIN_FILENO, &seq[1], 1) != 1) return '\x1b';
if (seq[0] == '[') {
switch (seq[1]) {
case 'A': return MOVE_UP;
case 'B': return MOVE_DOWN;
case 'C': return MOVE_RIGHT;
case 'D': return MOVE_LEFT;
}
}
return '\x1b';
} else {
return c;
}
}
void editorMoveCursor(char key) {
switch (key) {
case MOVE_LEFT:
if (state.cx != 0)
state.cx--;
break;
case MOVE_RIGHT:
state.cx++;
break;
case MOVE_UP:
if (state.cy != 0)
state.cy--;
break;
case MOVE_DOWN:
if (state.cy < state.numRows)
state.cy++;
break;
}
}
void mapEditorKeys() {
char c = readKeysFromInput();
switch (c) {
case CTRL_KEY('q'):
clearEditorScreen();
exit(0);
break;
case MOVE_LEFT:
case MOVE_RIGHT:
case MOVE_UP:
case MOVE_DOWN:
editorMoveCursor(c);
break;
}
}
// read cursor position by sending status report char escape to VT100
int getCursorPosition(int *rows, int *cols) {
// send cursor to right bottom of the screen
if (write(STDOUT_FILENO, "\x1b[999C\x1b[999B", 12) != 12) return -1;
// write out status report escape char
if (write(STDOUT_FILENO, "\x1b[6n]", 4) != 4) return -1;
char buffer[32];
unsigned int i = 0;
while (i < sizeof(buffer) - 1) {
if (read(STDIN_FILENO, &buffer[i], 1) != 1) break;
if (buffer[i] == 'R') break;
i++;
}
buffer[i] = '\0';
if (buffer[0] != '\x1b' || buffer[1] != '[') return -1;
if (sscanf(&buffer[2], "%d;%d", rows, cols) != 2) return -1;
return 0;
}
int getWindowSize(int *rows, int *cols) {
struct winsize ws;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0) {
return getCursorPosition(rows, cols);
} else {
*cols = ws.ws_col;
*rows = ws.ws_row;
return 0;
}
}
void editorAppendRow(char *s, size_t len) {
state.row = realloc(state.row, sizeof(editorRow) * (state.numRows + 1));
int at = state.numRows;
state.row[at].size = len;
state.row[at].chars = malloc(len+ 1);
memcpy(state.row[at].chars, s, len);
state.row[at].chars[len] = '\0';
state.numRows++;
}
// open a file and read it to memory
void editorOpen(char *filename) {
FILE *fp = fopen(filename, "r");
if (!fp) die("fopen");
char *line = NULL;
size_t lineCap = 0;
ssize_t lineLen;
while ((lineLen = getline(&line, &lineCap, fp)) != -1) {
while (lineLen > 0 && (line[lineLen - 1] == '\n' || line[lineLen] == '\r'))
lineLen--;
editorAppendRow(line, lineLen);
}
free(line);
fclose(fp);
}
int drawWelcomeMessage(int *row, struct buffer *buf) {
if (state.numRows == 0 && *row == state.screenrows / 3) {
char message[80];
int messageLen = snprintf(message, sizeof(message), "Kilo editor -- version %s", KILO_VERSION);
// truncate message in case screen is too tiny to fit message
if (messageLen > state.screencols)
messageLen = state.screencols;
// padding
int padding = (state.screencols - messageLen) / 2;
if (padding) {
appendToBuffer(buf, "~", 1);
padding--;
}
while (padding--) appendToBuffer(buf, " ", 1);
appendToBuffer(buf, message, messageLen);
return 0;
} else {
return -1;
}
}
void editorScroll() {
// vertically
if (state.cy < state.rowOffset)
state.rowOffset = state.cy;
if (state.cy >= state.rowOffset + state.screenrows)
state.rowOffset = state.cy - state.screenrows + 1;
// horizontally
if (state.cx < state.columnsOffset)
state.columnsOffset = state.cx;
if (state.cx >= state.columnsOffset + state.screencols)
state.columnsOffset = state.cx - state.screencols + 1;
}
void drawRows(struct buffer *buf) {
int y;
for (y = 0; y < state.screenrows; y++) {
int fileRow = y + state.rowOffset;
if (fileRow >= state.numRows) {
if (drawWelcomeMessage(&y, buf) == -1) {
appendToBuffer(buf, "~", 1);
}
} else {
int len = state.row[fileRow].size - state.columnsOffset;
if (len < 0) len = 0;
if (len > state.screencols) len = state.screencols;
appendToBuffer(buf, &state.row[fileRow].chars[state.columnsOffset], len);
}
// clear lines as we write out new ones
appendToBuffer(buf, "\x1b[K", 3);
if (y < state.screenrows - 1) {
appendToBuffer(buf, "\r\n", 2);
}
}
}
void clearEditorScreen() {
write(STDOUT_FILENO, "\x1b[2J", 4);
write(STDOUT_FILENO, "\x1b[H", 3);
}
void refreshEditorScreen() {
editorScroll();
struct buffer buf = BUFFER_INIT;
appendToBuffer(&buf, "\x1b[?25l", 6);
appendToBuffer(&buf, "\x1b[H", 3);
drawRows(&buf);
char cursorBuf[32];
snprintf(cursorBuf, sizeof(cursorBuf), "\x1b[%d;%dH", (state.cy - state.rowOffset) + 1, (state.cx - state.columnsOffset) + 1);
appendToBuffer(&buf, cursorBuf, strlen(cursorBuf));
//appendToBuffer(&buf, "\x1b[H", 3);
appendToBuffer(&buf, "\x1b[?25h", 6);
write(STDOUT_FILENO, buf.b, buf.len);
freeBuffer(&buf);
}
// initalize editor global state
void initEditor() {
// initialy cursor coordenates
state.cx = 0;
state.cy = 0;
state.row = NULL;
state.rowOffset = 0;
state.numRows = 0;
if (getWindowSize(&state.screenrows, &state.screencols) == -1) die("getWindowSize");
}
int main(int argc, char *argv[]) {
enableCustomTerminalMode();
initEditor();
if (argc >= 2) {
editorOpen(argv[1]);
}
while (1) {
refreshEditorScreen();
mapEditorKeys();
}
return 0;
}