This commit is contained in:
Tyrel Souza 2021-10-21 00:54:25 -04:00
parent 221a6569e8
commit 1a5dd5dcc3
2 changed files with 84 additions and 6 deletions

View File

@ -2,3 +2,6 @@ Tyrel's Text Editor
https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#vertical-scrolling
https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#horizontal-scrolling
https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#tabs-and-the-cursor
https://viewsourcecode.org/snaptoken/kilo/04.aTextViewer.html#scrolling-with-page-up-and-page-down

83
tted.c
View File

@ -16,6 +16,7 @@
/** defines */
#define TTED_VERSION "0.0.1"
#define CTRL_KEY(k) ((k) & 0x1f)
#define TTED_TAB_STOP 8
enum editorKey {
ARROW_LEFT = 1000,
@ -33,12 +34,16 @@ enum editorKey {
/** data */
typedef struct erow {
int size;
int rsize;
char *chars;
char *render;
} erow;
struct editorConfig {
int cx, cy;
int rx;
int rowoff;
int coloff;
int screenrows;
int screencols;
int numrows;
@ -168,6 +173,43 @@ int getWindowSize(int *rows, int *cols) {
}
/** Row operations */
int editorRowCxToRx(erow *row, int cx) {
int rx = 0;
int j;
for (j=0; j < cx; j++){
if (row->chars[j] == '\t') {
rx += (TTED_TAB_STOP -1) - (rx % TTED_TAB_STOP);
}
rx++;
}
return rx;
}
void editorUpdateRow(erow *row) {
int tabs = 0;
int j;
for (j = 0; j < row->size; j++) {
if (row->chars[j] == '\t') {
tabs++;
}
}
free(row->render);
row->render = malloc(row->size + tabs*(TTED_TAB_STOP - 1) + 1);
int idx = 0;
for (j = 0; j < row->size; j++) {
if (row->chars[j] == '\t') {
row->render[idx++] = ' ';
while (idx % TTED_TAB_STOP != 0) row->render[idx++] = ' ';
} else {
row->render[idx++] = row->chars[j];
}
}
row->render[idx] = '\0';
row->rsize = idx;
}
void editorAppendRow(char *s, size_t len) {
E.row = realloc(E.row, sizeof(erow) * (E.numrows + 1));
@ -176,6 +218,9 @@ void editorAppendRow(char *s, size_t len) {
E.row[at].chars = malloc(len + 1);
memcpy(E.row[at].chars, s, len);
E.row[at].chars[len] = '\0';
E.row[at].rsize = 0;
E.row[at].render = NULL;
editorUpdateRow(&E.row[at]);
E.numrows++;
}
@ -221,12 +266,24 @@ void abFree(struct abuf *ab) {
/** output */
void editorScroll() {
E.rx = 0;
if (E.cy < E.numrows){
E.rx = editorRowCxToRx(&E.row[E.cy], E.cx);
}
if (E.cy < E.rowoff) {
E.rowoff = E.cy;
}
if (E.cy >= E.rowoff + E.screenrows) {
E.rowoff = E.cy - E.screenrows + 1;
}
if (E.rx < E.coloff) {
E.coloff = E.rx;
}
if (E.rx >= E.coloff + E.screencols) {
E.coloff = E.rx - E.screencols + 1;
}
}
void editorDrawRows(struct abuf *ab) {
@ -250,9 +307,10 @@ void editorDrawRows(struct abuf *ab) {
abAppend(ab, "~", 1);
}
} else { // draw buffer
int len = E.row[filerow].size;
int len = E.row[filerow].rsize - E.coloff;
if (len < 0) len = 0;
if (len > E.screencols) len = E.screencols; // truncate if further than columns
abAppend(ab, E.row[filerow].chars, len);
abAppend(ab, &E.row[filerow].render[E.coloff], len);
}
abAppend(ab, "\x1b[K", 3);
@ -271,7 +329,7 @@ void editorRefreshScreen() {
editorDrawRows(&ab);
char buf[32];
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) +1, E.cx + 1);
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", (E.cy - E.rowoff) + 1, (E.rx - E.coloff) + 1);
abAppend(&ab, buf, strlen(buf));
abAppend(&ab, "\x1b[?25h", 6);
@ -282,15 +340,23 @@ void editorRefreshScreen() {
/** input */
void editorMoveCursor(int key) {
erow *row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy];
switch (key) {
case ARROW_LEFT:
if (E.cx != 0) {
E.cx--;
} else if (E.cy > 0) {
E.cy--;
E.cx = E.row[E.cy].size;
}
break;
case ARROW_RIGHT:
if (E.cx != E.screencols - 1) {
if (row && E.cx < row->size) {
E.cx++;
} else if (row && E.cx == row->size) {
E.cy++;
E.cx = 0;
}
break;
case ARROW_UP:
@ -304,6 +370,13 @@ void editorMoveCursor(int key) {
}
break;
}
// Snap cursor
row = (E.cy >= E.numrows) ? NULL : &E.row[E.cy];
int rowlen = row ? row->size : 0;
if (E.cx > rowlen) {
E.cx = rowlen;
}
}
void editorProcessKeypress() {
@ -343,7 +416,9 @@ void editorProcessKeypress() {
void initEditor() {
E.cx = 0;
E.cy = 0;
E.rx = 0;
E.rowoff = 0;
E.coloff = 0;
E.numrows = 0;
E.row = NULL;
if (getWindowSize(&E.screenrows, &E.screencols) == -1) die("getWindowSize");