Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
691da8ec8c | ||
|
|
d12f61f7c4 |
@@ -1,12 +1,16 @@
|
||||
/*** includes ***/
|
||||
|
||||
#include <asm-generic/ioctls.h>
|
||||
#define _DEFAULT_SOURCE
|
||||
#define _BSD_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
|
||||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -30,10 +34,17 @@ enum editorKey {
|
||||
|
||||
/*** data ***/
|
||||
|
||||
typedef struct erow {
|
||||
int size;
|
||||
char *chars;
|
||||
} erow;
|
||||
|
||||
struct editorConfig {
|
||||
int cx, cy;
|
||||
int screenrows;
|
||||
int screencols;
|
||||
int numrows;
|
||||
erow *row;
|
||||
struct termios orig_termios;
|
||||
};
|
||||
|
||||
@@ -183,6 +194,40 @@ int getWindowSize(int *rows, int *cols) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*** row operations ***/
|
||||
|
||||
void editorAppendRow(char *r, size_t len) {
|
||||
E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1));
|
||||
|
||||
int at = E.numrows;
|
||||
E.row[at].size = len;
|
||||
E.row[at].chars = malloc(len+1);
|
||||
memcpy(E.row[at].chars, r, len);
|
||||
E.row[at].chars[len] = '\0';
|
||||
|
||||
E.numrows++;
|
||||
}
|
||||
|
||||
/*** file i/o ***/
|
||||
|
||||
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-1] == '\r'))
|
||||
linelen--;
|
||||
editorAppendRow(line, linelen);
|
||||
}
|
||||
free(line);
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
/*** append buffer ***/
|
||||
|
||||
struct abuf {
|
||||
@@ -210,7 +255,9 @@ void abFree(struct abuf *ab) {
|
||||
|
||||
void editorDrawRows(struct abuf *ab) {
|
||||
for (int y = 0; y < E.screenrows; y++) {
|
||||
if (y == E.screenrows / 3) {
|
||||
if (y >= E.numrows) {
|
||||
// We spilled over the file content.
|
||||
if (E.numrows == 0 && y == E.screenrows / 3) {
|
||||
char welcome[80];
|
||||
int welcomelen = snprintf(
|
||||
welcome,
|
||||
@@ -231,6 +278,12 @@ void editorDrawRows(struct abuf *ab) {
|
||||
} else {
|
||||
abAppend(ab, "~", 1);
|
||||
}
|
||||
} else {
|
||||
int len = E.row[y].size;
|
||||
if (len > E.screencols)
|
||||
len = E.screencols;
|
||||
abAppend(ab, E.row[y].chars, len);
|
||||
}
|
||||
|
||||
// Erase the line content to the right of the cursor.
|
||||
abAppend(ab, "\x1b[K", 3);
|
||||
@@ -333,14 +386,19 @@ void editorProcessKeypress() {
|
||||
void initEditor() {
|
||||
E.cx = 0;
|
||||
E.cy = 0;
|
||||
E.row = NULL;
|
||||
E.numrows = 0;
|
||||
if (getWindowSize(&E.screenrows, &E.screencols) == -1) {
|
||||
die("getWindowSize");
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(int argc, char *argv[]) {
|
||||
enableRawMode();
|
||||
initEditor();
|
||||
if (argc > 1) {
|
||||
editorOpen(argv[1]);
|
||||
}
|
||||
|
||||
while(1) {
|
||||
editorRefreshScreen();
|
||||
|
||||
Reference in New Issue
Block a user