diff --git a/CMakeLists.txt b/CMakeLists.txt index b7a0a3b..e61adaf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,4 @@ project(editor) set(CMAKE_CXX_STANDARD 17) -add_executable(editor kilo.c) +add_executable(editor tte.c) diff --git a/README.md b/README.md new file mode 100644 index 0000000..8ffd262 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Tyrel's Text Editor diff --git a/kilo.c b/tte.c similarity index 91% rename from kilo.c rename to tte.c index e32e879..f8c8657 100644 --- a/kilo.c +++ b/tte.c @@ -1,4 +1,8 @@ /** Includes */ +#define _DEFAULT_SOURCE +#define _BSD_SOURCE +#define _GNU_SOURCE + #include #include #include @@ -37,7 +41,7 @@ struct editorConfig { int screenrows; int screencols; int numrows; - erow row; + erow *row; struct termios orig_termios; }; struct editorConfig E; @@ -161,6 +165,16 @@ int getWindowSize(int *rows, int *cols) { return 0; } } +/** Row operations */ +void editorAppendRow(char *s, 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, s, len); + E.row[at].chars[len] = '\0'; + E.numrows++; +} /** file i/o */ @@ -169,23 +183,21 @@ void editorOpen(char *filename) { if (!fp) die("fopen"); char *line = NULL; - ssize_t linecap = 0; + size_t linecap = 0; ssize_t linelen; linelen = getline(&line, &linecap, fp); - if (linelen != -1) { - while (linelen > 0 && (line[linelen - 1] == '\r' || + while((linelen = getline(&line, &linecap, fp)) != -1) { + while (linelen > 0 && (line[linelen - 1] == '\n' || line[linelen - 1] == '\r')) linelen--; - E.row.size = linelen; - E.row.chars = malloc(linelen + 1); - memcpy(E.row.chars, line, linelen); - E.row.chars[linelen] = '\0'; - E.numrows = 1; + editorAppendRow(line, linelen); } free(line); fclose(fp); } + + /** append buffer */ struct abuf { char *b; @@ -212,7 +224,7 @@ void editorDrawRows(struct abuf *ab) { int y; for (y = 0; y < E.screenrows; y++) { if (y >= E.numrows) { // Draw rows after buffer - if (y == E.screenrows / 3) { + if (E.numrows == 0 && y == E.screenrows / 3) { char welcome[80]; int welcomelen = snprintf(welcome, sizeof welcome, "Tyrel Editor -- version %s", TYREL_VERSION); int padding = (E.screencols - welcomelen) / 2; @@ -227,9 +239,9 @@ void editorDrawRows(struct abuf *ab) { abAppend(ab, "~", 1); } } else { // draw buffer - int len = E.row.size; + int len = E.row[y].size; if (len > E.screencols) len = E.screencols; // truncate if further than columns - abAppend(ab, E.row.chars, len); + abAppend(ab, E.row[y].chars, len); } abAppend(ab, "\x1b[K", 3); @@ -319,6 +331,7 @@ void initEditor() { E.cx = 0; E.cy = 0; E.numrows = 0; + E.row = NULL; if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize"); }