aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/dvtm/buffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'sys/cmd/dvtm/buffer.c')
-rw-r--r--sys/cmd/dvtm/buffer.c122
1 files changed, 72 insertions, 50 deletions
diff --git a/sys/cmd/dvtm/buffer.c b/sys/cmd/dvtm/buffer.c
index f7e73c0..b903e71 100644
--- a/sys/cmd/dvtm/buffer.c
+++ b/sys/cmd/dvtm/buffer.c
@@ -1,4 +1,4 @@
-#include "buffer.h"
+#include "term.h"
/* row operations */
void
@@ -7,7 +7,7 @@ zero(Row *row, int start, int len)
int i;
Cell cell = {
.r = L'\0',
- .pen = {0};
+ .pen = {0},
};
for (i = start; i < len + start; i++)
@@ -48,9 +48,9 @@ bclear(Buffer *b)
},
};
- for (i = 0; i < b->rows; i++) {
- Row *row = b->lines + i;
- for (int j = 0; j < b->cols; j++) {
+ for (i = 0; i < b->nrow; i++) {
+ Row *row = b->row + i;
+ for (int j = 0; j < b->ncol; j++) {
row->cells[j] = cell;
row->dirty = true;
}
@@ -62,10 +62,10 @@ bfree(Buffer *b)
{
int i;
- for (i = 0; i < b->rows; i++)
- free(b->lines[i].cells);
+ for (i = 0; i < b->nrow; i++)
+ free(b->row[i].cells);
- free(b->lines);
+ free(b->row);
for (i = 0; i < b->scroll.size; i++)
free(b->scroll.buf[i].cells);
@@ -122,67 +122,67 @@ bscroll(Buffer *b, int s)
}
void
-bresize(Buffer *b, int rows, int cols)
+bresize(Buffer *b, int nrow, int ncol)
{
- Row *lines = b->lines;
+ Row *row = b->row;
- if (b->rows != rows) {
- if (b->crow >= lines + rows) {
+ if (b->nrow != nrow) {
+ if (b->crow >= row + nrow) {
/* scroll up instead of simply chopping off bottom */
- bscroll(b, (b->crow - b->lines) - rows + 1);
+ bscroll(b, (b->crow - b->row) - nrow + 1);
}
- while (b->rows > rows) {
- free(lines[b->rows - 1].cells);
- b->rows--;
+ while (b->nrow > nrow) {
+ free(row[b->nrow - 1].cells);
+ b->nrow--;
}
- lines = realloc(lines, sizeof(Row) * rows);
+ row = realloc(row, sizeof(Row) * nrow);
}
- if (b->maxcols < cols) {
- for (int row = 0; row < b->rows; row++) {
- lines[row].cells = realloc(lines[row].cells, sizeof(Cell) * cols);
- if (b->cols < cols)
- zero(lines + row, b->cols, cols - b->cols);
- lines[row].dirty = true;
+ if (b->maxcols < ncol) {
+ for (int r = 0; r < b->nrow; r++) {
+ row[r].cells = realloc(row[r].cells, sizeof(Cell) * ncol);
+ if (b->ncol < ncol)
+ zero(row + r, b->ncol, ncol - b->ncol);
+ row[r].dirty = true;
}
Row *sbuf = b->scroll.buf;
- for (int row = 0; row < b->scroll.size; row++) {
- sbuf[row].cells = realloc(sbuf[row].cells, sizeof(Cell) * cols);
- if (b->cols < cols)
- zero(sbuf + row, b->cols, cols - b->cols);
+ for (int r = 0; r < b->scroll.size; r++) {
+ sbuf[r].cells = realloc(sbuf[r].cells, sizeof(Cell) * ncol);
+ if (b->ncol < ncol)
+ zero(sbuf + r, b->ncol, ncol - b->ncol);
}
- b->tabs = realloc(b->tabs, sizeof(*b->tabs) * cols);
- for (int col = b->cols; col < cols; col++)
- b->tabs[col] = !(col & 7);
- b->maxcols = cols;
- b->cols = cols;
- } else if (b->cols != cols) {
- for (int row = 0; row < b->rows; row++)
- lines[row].dirty = true;
- b->cols = cols;
+ b->tabs = realloc(b->tabs, sizeof(*b->tabs) * ncol);
+ for (int c = b->ncol; c < ncol; c++)
+ b->tabs[c] = !(c & 7);
+ b->maxcols = ncol;
+ b->ncol = ncol;
+ } else if (b->ncol != ncol) {
+ for (int r = 0; r < b->nrow; r++)
+ row[r].dirty = true;
+ b->ncol = ncol;
}
int deltarows = 0;
- if (b->rows < rows) {
- while (b->rows < rows) {
- lines[b->rows].cells = calloc(b->maxcols, sizeof(Cell));
- zero(lines + b->rows, 0, b->maxcols);
- b->rows++;
+ if (b->nrow < nrow) {
+ while (b->nrow < nrow) {
+ row[b->nrow].cells = calloc(b->maxcols, sizeof(Cell));
+ zero(row + b->nrow, 0, b->maxcols);
+ b->nrow++;
}
/* prepare for backfill */
if (b->crow >= b->scroll.bot - 1) {
- deltarows = b->lines + rows - b->crow - 1;
+ deltarows = b->row + nrow - b->crow - 1;
if (deltarows > b->scroll.above)
deltarows = b->scroll.above;
}
}
- b->crow += lines - b->lines;
- b->scroll.top = lines;
- b->scroll.bot = lines + rows;
- b->lines = lines;
+ b->crow += row - b->row;
+ b->scroll.top = row;
+ b->scroll.bot = row + nrow;
+ b->row = row;
/* perform backfill */
if (deltarows > 0) {
@@ -240,7 +240,7 @@ browfirst(Buffer *b)
{
Row *bstart;
if (!b->scroll.size || !b->scroll.above)
- return b->lines;
+ return b->row;
bboundary(b, &bstart, nil, nil, nil);
return bstart;
}
@@ -250,7 +250,7 @@ browlast(Buffer *b)
{
Row *aend;
if (!b->scroll.size || !b->scroll.below)
- return b->lines + b->rows - 1;
+ return b->row + b->nrow - 1;
bboundary(b, nil, nil, nil, &aend);
return aend;
}
@@ -259,7 +259,7 @@ Row *
brownext(Buffer *b, Row *row)
{
Row *before_start, *before_end, *after_start, *after_end;
- Row *first = b->lines, *last = b->lines + b->rows - 1;
+ Row *first = b->row, *last = b->row + b->nrow - 1;
if (!row)
return nil;
@@ -283,7 +283,7 @@ Row *
bprevrow(Buffer *b, Row *row)
{
Row *before_start, *before_end, *after_start, *after_end;
- Row *first = b->lines, *last = b->lines + b->rows - 1;
+ Row *first = b->row, *last = b->row + b->nrow - 1;
if (!row)
return nil;
@@ -302,3 +302,25 @@ bprevrow(Buffer *b, Row *row)
return &b->scroll.buf[b->scroll.size - 1];
return --row;
}
+
+void
+brender(Buffer *b, Term *t)
+{
+ int r, c, n;
+ char u[UTFmax+1];
+ Row *row;
+ Cell *cell;
+
+ for (r = 0; r < b->nrow; r++) {
+ row = b->row + r;
+ if (!row->dirty)
+ continue;
+
+ for (c = 0; c < b->ncol; c++) {
+ cell = row->cells + c;
+ tsetpen(t, cell->pen);
+ n = utf8·runetobyte(u, &cell->r);
+ twrite(t, n, u);
+ }
+ }
+}