- newtext = smalloc ((newrows+newsavelines)*(newcols+1)*TSIZE);
- sbbot = newtext + newsavelines*(newcols+1);
- for (i=0; i<(newrows+newsavelines)*(newcols+1); i++)
- newtext[i] = erase_char;
- if (rows != -1) {
- crows = rows + (scrtop - sbtop) / (cols+1);
- if (crows > newrows+newsavelines)
- crows = newrows+newsavelines;
- if (newrows>crows)
- disptop = newtext;
- else
- disptop = newtext + (crows-newrows)*(newcols+1);
- ccols = (cols < newcols ? cols : newcols);
- for (i=0; i<crows; i++) {
- int oldidx = (rows - crows + i) * (cols+1);
- int newidx = (newrows - crows + i) * (newcols+1);
-
- for (j=0; j<ccols; j++)
- disptop[newidx+j] = scrtop[oldidx+j];
- disptop[newidx+newcols] =
- (cols == newcols ? scrtop[oldidx+cols]
- : (scrtop[oldidx+cols]&LATTR_MODE));
+ if (rows == -1) {
+ scrollback = newtree234(NULL);
+ screen = newtree234(NULL);
+ rows = 0;
+ }
+
+ /*
+ * Resize the screen and scrollback. We only need to shift
+ * lines around within our data structures, because lineptr()
+ * will take care of resizing each individual line if
+ * necessary. So:
+ *
+ * - If the new screen and the old screen differ in length, we
+ * must shunt some lines in from the scrollback or out to
+ * the scrollback.
+ *
+ * - If doing that fails to provide us with enough material to
+ * fill the new screen (i.e. the number of rows needed in
+ * the new screen exceeds the total number in the previous
+ * screen+scrollback), we must invent some blank lines to
+ * cover the gap.
+ *
+ * - Then, if the new scrollback length is less than the
+ * amount of scrollback we actually have, we must throw some
+ * away.
+ */
+ sblen = count234(scrollback);
+ debug(("resizing rows=%d sblen=%d newrows=%d newsb=%d\n",
+ rows, sblen, newrows, newsavelines));
+ if (newrows > rows) {
+ for (i = rows; i < newrows; i++) {
+ if (sblen > 0) {
+ line = delpos234(scrollback, --sblen);
+ } else {
+ line = smalloc(TSIZE * (newcols+2));
+ line[0] = newcols;
+ for (j = 0; j <= newcols; j++)
+ line[j+1] = ERASE_CHAR;
+ }
+ addpos234(screen, line, 0);
+ }
+ } else if (newrows < rows) {
+ for (i = newrows; i < rows; i++) {
+ line = delpos234(screen, 0);
+ addpos234(scrollback, line, sblen++);