feat: Treat tabs as single characters when navigating.
This commit is contained in:
@@ -44,6 +44,7 @@ typedef struct erow {
|
||||
|
||||
struct editorConfig {
|
||||
int cx, cy;
|
||||
int rx;
|
||||
int rowoff, coloff;
|
||||
int screenrows;
|
||||
int screencols;
|
||||
@@ -200,6 +201,26 @@ int getWindowSize(int *rows, int *cols) {
|
||||
|
||||
/*** 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) {
|
||||
int tabs = 0;
|
||||
int j;
|
||||
@@ -289,6 +310,11 @@ void abFree(struct abuf *ab) {
|
||||
/*** output ***/
|
||||
|
||||
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.
|
||||
if (E.cy < E.rowoff)
|
||||
E.rowoff = E.cy;
|
||||
@@ -296,11 +322,11 @@ void editorScroll() {
|
||||
if (E.cy >= E.rowoff + E.screenrows)
|
||||
E.rowoff = E.cy - E.screenrows + 1;
|
||||
// Scrolled passed the left.
|
||||
if (E.cx < E.coloff)
|
||||
E.coloff = E.cx;
|
||||
if (E.rx < E.coloff)
|
||||
E.coloff = E.rx;
|
||||
// Scrolled passed the right.
|
||||
if (E.cx >= E.coloff + E.screencols)
|
||||
E.coloff = E.cx - E.screencols + 1;
|
||||
if (E.rx >= E.coloff + E.screencols)
|
||||
E.coloff = E.rx - E.screencols + 1;
|
||||
}
|
||||
|
||||
void editorDrawRows(struct abuf *ab) {
|
||||
@@ -363,7 +389,7 @@ void editorRefreshScreen() {
|
||||
char posbuf[32];
|
||||
snprintf(posbuf, sizeof(posbuf), "\x1b[%d;%dH",
|
||||
(E.cy - E.rowoff) + 1,
|
||||
(E.cx - E.coloff) + 1
|
||||
(E.rx - E.coloff) + 1
|
||||
);
|
||||
abAppend(&ab, posbuf, strlen(posbuf));
|
||||
|
||||
@@ -460,6 +486,7 @@ void editorProcessKeypress() {
|
||||
void initEditor() {
|
||||
E.cx = 0;
|
||||
E.cy = 0;
|
||||
E.rx = 0;
|
||||
E.rowoff = 0;
|
||||
E.coloff = 0;
|
||||
E.row = NULL;
|
||||
|
||||
Reference in New Issue
Block a user