feat: Treat tabs as single characters when navigating.

This commit is contained in:
jmug
2026-11-19 17:37:31 -08:00
parent 8821964375
commit c65fcc5985
+32 -5
View File
@@ -44,6 +44,7 @@ typedef struct erow {
struct editorConfig { struct editorConfig {
int cx, cy; int cx, cy;
int rx;
int rowoff, coloff; int rowoff, coloff;
int screenrows; int screenrows;
int screencols; int screencols;
@@ -200,6 +201,26 @@ int getWindowSize(int *rows, int *cols) {
/*** row operations ***/ /*** row operations ***/
// editorRowCxToRx transforms an index pointing to the chars string
// into an index pointing into the render string.
//
// For now it reverts to the behavior of treating tabs as individual
// characters, even if they're actually rendered as spaces.
int editorRowCxToRx(erow *row, int cx) {
int rx = 0;
int j;
for (j = 0; j < cx; j++) {
if (row->chars[j] == '\t')
// Max size minus how many characters already on the left:
// Assume stop = 8:
// XXXXX\t--
// Second term represents the Xs, i.e. 5.
rx += (KILO_TAB_STOP - 1) - (rx % KILO_TAB_STOP);
rx++;
}
return rx;
}
void editorUpdateRow(erow *row) { void editorUpdateRow(erow *row) {
int tabs = 0; int tabs = 0;
int j; int j;
@@ -289,6 +310,11 @@ void abFree(struct abuf *ab) {
/*** output ***/ /*** output ***/
void editorScroll() { void editorScroll() {
E.rx = 0;
// Last line (emtpy) is always at 0.
if (E.cy < E.numrows)
E.rx = editorRowCxToRx(&E.row[E.cy], E.cx);
// Scrolled up passed the top. // Scrolled up passed the top.
if (E.cy < E.rowoff) if (E.cy < E.rowoff)
E.rowoff = E.cy; E.rowoff = E.cy;
@@ -296,11 +322,11 @@ void editorScroll() {
if (E.cy >= E.rowoff + E.screenrows) if (E.cy >= E.rowoff + E.screenrows)
E.rowoff = E.cy - E.screenrows + 1; E.rowoff = E.cy - E.screenrows + 1;
// Scrolled passed the left. // Scrolled passed the left.
if (E.cx < E.coloff) if (E.rx < E.coloff)
E.coloff = E.cx; E.coloff = E.rx;
// Scrolled passed the right. // Scrolled passed the right.
if (E.cx >= E.coloff + E.screencols) if (E.rx >= E.coloff + E.screencols)
E.coloff = E.cx - E.screencols + 1; E.coloff = E.rx - E.screencols + 1;
} }
void editorDrawRows(struct abuf *ab) { void editorDrawRows(struct abuf *ab) {
@@ -363,7 +389,7 @@ void editorRefreshScreen() {
char posbuf[32]; char posbuf[32];
snprintf(posbuf, sizeof(posbuf), "\x1b[%d;%dH", snprintf(posbuf, sizeof(posbuf), "\x1b[%d;%dH",
(E.cy - E.rowoff) + 1, (E.cy - E.rowoff) + 1,
(E.cx - E.coloff) + 1 (E.rx - E.coloff) + 1
); );
abAppend(&ab, posbuf, strlen(posbuf)); abAppend(&ab, posbuf, strlen(posbuf));
@@ -460,6 +486,7 @@ void editorProcessKeypress() {
void initEditor() { void initEditor() {
E.cx = 0; E.cx = 0;
E.cy = 0; E.cy = 0;
E.rx = 0;
E.rowoff = 0; E.rowoff = 0;
E.coloff = 0; E.coloff = 0;
E.row = NULL; E.row = NULL;