- newsb = newtree234(NULL);
- newscreen = newtree234(NULL);
- ccols = (cols < newcols ? cols : newcols);
- oldsbsize = scrollback ? count234(scrollback) : 0;
- furthest_back = newrows - rows - oldsbsize;
- if (furthest_back > 0)
- furthest_back = 0;
- if (furthest_back < -newsavelines)
- furthest_back = -newsavelines;
- for (posn = newrows; posn-- > furthest_back ;) {
- oldposn = posn - newrows + rows;
- if (rows == -1 || oldposn < -oldsbsize) {
- line = smalloc(TSIZE * (newcols + 1));
- for (j = 0; j < newcols; j++)
- line[j] = erase_char;
- line[newcols] = 0;
- } else {
- oldline = (oldposn >= 0 ?
- delpos234(screen, count234(screen)-1) :
- delpos234(scrollback, count234(scrollback)-1));
- if (newcols != cols) {
- line = smalloc(TSIZE * (newcols + 1));
- for (j = 0; j < ccols; j++)
- line[j] = oldline[j];
- for (j = ccols; j < newcols; j++)
- line[j] = erase_char;
- line[newcols] = oldline[cols] & LATTR_MODE;
- sfree(oldline);
+ debug(("term_size, old r,c,s (%d,%d,%d), new rcs (%d,%d,%d)\n",
+ rows, cols, savelines, newrows, newcols, newsavelines));
+
+ 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(("newrows=%d rows=%d sblen=%d\n", newrows, rows, sblen));
+ if (newrows > rows) {
+ for (i = rows; i < newrows; i++) {
+ debug(("increase screen: i=%d\n", i));
+ if (sblen > 0) {
+ debug(("sblen=%d so use line from scrollback\n", sblen));
+ line = delpos234(scrollback, --sblen);