[dev] [st] [PATCH 2/2] Keep end of lines in memory when resizing terminal

From: Alexander Krotov <ilabdsf_AT_gmail.com>
Date: Sat, 25 Mar 2017 23:02:55 +0300

---
 st.c | 32 +++++++++++++++++---------------
 st.h |  1 +
 2 files changed, 18 insertions(+), 15 deletions(-)
diff --git a/st.c b/st.c
index ae93ade..2eab32a 100644
--- a/st.c
+++ b/st.c
_AT_@ -1238,8 +1238,8 @@ tclearregion(int x1, int y1, int x2, int y2)
 	if (y1 > y2)
 		temp = y1, y1 = y2, y2 = temp;
 
-	LIMIT(x1, 0, term.col-1);
-	LIMIT(x2, 0, term.col-1);
+	LIMIT(x1, 0, term.maxcol-1);
+	LIMIT(x2, 0, term.maxcol-1);
 	LIMIT(y1, 0, term.row-1);
 	LIMIT(y2, 0, term.row-1);
 
_AT_@ -2492,7 +2492,8 @@ tresize(int col, int row)
 {
 	int i;
 	int minrow = MIN(row, term.row);
-	int mincol = MIN(col, term.col);
+	int oldmaxcol = term.maxcol;
+	int newmaxcol = MAX(term.maxcol, col);
 	int *bp;
 	TCursor c;
 
_AT_@ -2522,29 +2523,29 @@ tresize(int col, int row)
 	}
 
 	/* resize to new width */
-	term.specbuf = xrealloc(term.specbuf, col * sizeof(GlyphFontSpec));
+	term.specbuf = xrealloc(term.specbuf, newmaxcol * sizeof(GlyphFontSpec));
 
 	/* resize to new height */
 	term.line = xrealloc(term.line, row * sizeof(Line));
 	term.alt  = xrealloc(term.alt,  row * sizeof(Line));
 	term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
-	term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
+	term.tabs = xrealloc(term.tabs, newmaxcol * sizeof(*term.tabs));
 
 	/* resize each row to new width, zero-pad if needed */
 	for (i = 0; i < minrow; i++) {
-		term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
-		term.alt[i]  = xrealloc(term.alt[i],  col * sizeof(Glyph));
+		term.line[i] = xrealloc(term.line[i], newmaxcol * sizeof(Glyph));
+		term.alt[i]  = xrealloc(term.alt[i],  newmaxcol * sizeof(Glyph));
 	}
 
 	/* allocate any new rows */
 	for (/* i = minrow */; i < row; i++) {
-		term.line[i] = xmalloc(col * sizeof(Glyph));
-		term.alt[i] = xmalloc(col * sizeof(Glyph));
+		term.line[i] = xmalloc(newmaxcol * sizeof(Glyph));
+		term.alt[i] = xmalloc(newmaxcol * sizeof(Glyph));
 	}
-	if (col > term.col) {
+	if (newmaxcol > term.col) {
 		bp = term.tabs + term.col;
 
-		memset(bp, 0, sizeof(*term.tabs) * (col - term.col));
+		memset(bp, 0, sizeof(*term.tabs) * (newmaxcol - term.col));
 		while (--bp > term.tabs && !*bp)
 			/* nothing */ ;
 		for (bp += tabspaces; bp < term.tabs + col; bp += tabspaces)
_AT_@ -2552,6 +2553,7 @@ tresize(int col, int row)
 	}
 	/* update terminal size */
 	term.col = col;
+	term.maxcol = newmaxcol;
 	term.row = row;
 	/* reset scrolling region */
 	tsetscroll(0, row-1);
_AT_@ -2560,11 +2562,11 @@ tresize(int col, int row)
 	/* Clearing both screens (it makes dirty all lines) */
 	c = term.c;
 	for (i = 0; i < 2; i++) {
-		if (mincol < col && 0 < minrow) {
-			tclearregion(mincol, 0, col - 1, minrow - 1);
+		if (oldmaxcol < term.maxcol && 0 < minrow) {
+			tclearregion(oldmaxcol, 0, term.maxcol - 1, minrow - 1);
 		}
-		if (0 < col && minrow < row) {
-			tclearregion(0, minrow, col - 1, row - 1);
+		if (0 < term.maxcol && minrow < row) {
+			tclearregion(0, minrow, term.maxcol - 1, row - 1);
 		}
 		tswapscreen();
 		tcursor(CURSOR_LOAD);
diff --git a/st.h b/st.h
index 44d4938..dc57b78 100644
--- a/st.h
+++ b/st.h
_AT_@ -112,6 +112,7 @@ typedef struct {
 typedef struct {
 	int row;      /* nb row */
 	int col;      /* nb col */
+	int maxcol;   /* maximum of col during terminal lifetime */
 	Line *line;   /* screen */
 	Line *alt;    /* alternate screen */
 	int *dirty;  /* dirtyness of lines */
-- 
2.11.0
Received on Sat Mar 25 2017 - 21:02:55 CET

This archive was generated by hypermail 2.3.0 : Sat Mar 25 2017 - 21:12:19 CET