tShow tabs with > symbols - ve - a minimal text editor (work in progress) (HTM) git clone git://src.adamsgaard.dk/ve (DIR) Log (DIR) Files (DIR) Refs (DIR) README (DIR) LICENSE --- (DIR) commit ec3976286cbc8ff16cf02429d735951bb80a29d4 (DIR) parent d459a5c2e81581c2019aa6ef69c99bc08d8180f2 (HTM) Author: Anders Damsgaard <anders@adamsgaard.dk> Date: Tue, 6 Aug 2019 13:46:18 +0200 Show tabs with > symbols Diffstat: M byote.h | 8 ++++++-- M io.c | 40 ++++++++++++++++++++++++++++++- M output.c | 4 ++-- 3 files changed, 47 insertions(+), 5 deletions(-) --- (DIR) diff --git a/byote.h b/byote.h t@@ -6,10 +6,14 @@ #define PROGNAME "byote" #define VERSION "0.0.1" +#define TAB_WIDTH 4 + /* editor row: stores a row of text */ typedef struct eRow { - int size; - char *chars; + char *chars; /* text read from file */ + char *rchars; /* text to render */ + int size; /* length of chars */ + int rsize; /* length of rchars */ } eRow; struct editor_config { (DIR) diff --git a/io.c b/io.c t@@ -10,7 +10,40 @@ #include "byote.h" #include "terminal.h" -void editor_append_row(char *s, size_t len) +/* translate tabs before display */ +void +editor_update_row(eRow* row) +{ + int j, idx, tabs; + + free(row->rchars); + row->rchars = malloc(row->size + 1); + + tabs = 0; + for (j=0; j<row->size; ++j) + if (row->chars[j] == '\t') + tabs++; + + free(row->rchars); + row->rchars = malloc(row->size + tabs*(TAB_WIDTH - 1) + 1); + + idx = 0; + for (j=0; j<row->size; ++j) { + if (row->chars[j] == '\t') { + row->rchars[idx++] = '>'; + while (idx % TAB_WIDTH != 0) + row->rchars[idx++] = ' '; + } else { + row->rchars[idx++] = row->chars[j]; + } + } + row->rchars[idx] = '\0'; + row->rsize = idx; +} + +/* add row to buffer */ +void +editor_append_row(char *s, size_t len) { int i; t@@ -20,6 +53,11 @@ void editor_append_row(char *s, size_t len) E.row[i].chars = malloc(len + 1); memcpy(E.row[i].chars, s, len); E.row[i].chars[len] = '\0'; + + E.row[i].rsize = 0; + E.row[i].rchars = NULL; + editor_update_row(&E.row[i]); + ++E.num_rows; } (DIR) diff --git a/output.c b/output.c t@@ -94,12 +94,12 @@ editor_draw_rows(struct abuf *ab) if (y == E.screen_rows-1) { draw_status(ab); } else if (file_row < E.num_rows) { - len = E.row[file_row].size - E.column_offset; + len = E.row[file_row].rsize - E.column_offset; if (len < 0) len = 0; if (len > E.screen_columns) len = E.screen_columns; - ab_append(ab, &E.row[file_row].chars[E.column_offset], len); + ab_append(ab, &E.row[file_row].rchars[E.column_offset], len); } else { ab_append(ab, "~", 1); }