--- /dev/null
+fgoto()
+{
+ register int l, c;
+
+ if (destcol > COLUMNS - 1) {
+ destline += destcol / COLUMNS;
+ destcol %= COLUMNS;
+ }
+ if (outcol > COLUMNS - 1) {
+ l = (outcol + 1) / COLUMNS;
+ outline += l;
+ outcol %= COLUMNS;
+ if (AM == 0) {
+ while (l > 0) {
+ if (pfast)
+ putch('\r');
+ putch('\n');
+ l--;
+ }
+ outcol = 0;
+ }
+ if (outline > LINES - 1) {
+ destline -= outline - (LINES - 1);
+ outline = LINES - 1;
+ }
+ }
+ if (destline > LINES - 1) {
+ l = destline;
+ destline = LINES - 1;
+ if (outline < LINES - 1) {
+ c = destcol;
+ if (pfast == 0 && (!CA || holdcm))
+ destcol = 0;
+ fgoto();
+ destcol = c;
+ }
+ while (l > LINES - 1) {
+ putch('\n');
+ l--;
+ if (pfast == 0)
+ outcol = 0;
+ }
+ }
+ if (destline < outline && !(CA && !holdcm || UP != NOSTR))
+ destline = outline;
+ if (CA && !holdcm) {
+ char *cgp;
+
+ cgp = tgoto(CM, destcol, destline);
+ if (plod(strlen(cgp)) > 0)
+ plod(0);
+ else
+ tputs(cgp, 0, putch);
+ } else
+ plod(0);
+ outline = destline;
+ outcol = destcol;
+}
+
+/*
+ * Tab to column col by flushing and then setting destcol.
+ * Used by "set all".
+ */
+tab(col)
+ int col;
+{
+
+ flush1();
+ destcol = col;
+}
+
+/*
+ * Move (slowly) to destination.
+ * Hard thing here is using home cursor on really deficient terminals.
+ * Otherwise just use cursor motions, hacking use of tabs and overtabbing
+ * and backspace.
+ */
+
+static int plodcnt, plodflg;
+
+plodput(c)
+{
+ if (plodflg)
+ plodcnt--;
+ else
+ putch(c);
+}
+
+plod(cnt)
+{
+ register int i, j, k;
+ int soutcol, soutline, sdestcol, sdestline;
+
+ plodcnt = plodflg = cnt;
+ soutcol = outcol;
+ soutline = outline;
+ sdestcol = destcol;
+ sdestline = destline;
+ if (HO) {
+ if (GT)
+ i = (destcol >> 3) + (destcol & 07);
+ else
+ i = destcol;
+ if (destcol >= outcol)
+ if (GT && (j = ((destcol - (outcol &~ 07)) >> 3)))
+ j += destcol & 07;
+ else
+ j = destcol - outcol;
+ else
+ if (outcol - destcol <= i && (BS || BC))
+ i = j = outcol - destcol;
+ else
+ j = i + 1;
+ k = outline - destline;
+ if (k < 0)
+ k = -k;
+ j += k;
+ if (i + destline < j) {
+ tputs(HO, 0, plodput);
+ outcol = outline = 0;
+ } else if (LL) {
+ k = (LINES - 1) - destline;
+ if (i + k + 2 < j) {
+ tputs(LL, 0, plodput);
+ outcol = 0;
+ outline = LINES - 1;
+ }
+ }
+ }
+ if (GT)
+ i = (destcol >> 3) + (destcol & 07);
+ else
+ i = destcol;
+ if (BT && outcol > destcol) {
+ j = (((outcol+7) & ~7) - destcol - 1) >> 3;
+ j *= (k = strlen(BT));
+ if ((k = (destcol&7)+k) > 4)
+ j += 8 - (destcol&7);
+ else
+ j += k;
+ }
+ else
+ j = outcol - destcol;
+ if ((!NONL || outline >= destline) && (!NC || outline < destline) &&
+ (j > i + 1 || outcol > destcol && !BS && !BC)) {
+ plodput('\r');
+ if (NC) {
+ plodput('\n');
+ outline++;
+ }
+ outcol = 0;
+ }
+ while (outline < destline) {
+ outline++;
+ plodput('\n');
+ if (plodflg && plodcnt < 0)
+ goto out;
+ if (NONL || pfast == 0)
+ outcol = 0;
+ }
+ if (BT)
+ k = strlen(BT);
+ while (outcol > destcol) {
+ if (plodflg && plodcnt < 0)
+ goto out;
+ if (BT && outcol - destcol > 4+k) {
+ tputs(BT, 0, plodput);
+ outcol--;
+ outcol &= ~7;
+ continue;
+ }
+ outcol--;
+ if (BC)
+ tputs(BC, 0, plodput);
+ else
+ plodput('\b');
+ }
+ while (outline > destline) {
+ outline--;
+ tputs(UP, 0, plodput);
+ if (plodflg && plodcnt < 0)
+ goto out;
+ }
+ if (GT && destcol - outcol > 1) {
+ while ((i = ((outcol + 8) &~ 7)) <= destcol) {
+ if (TA)
+ tputs(TA, 0, plodput);
+ else
+ plodput('\t');
+ outcol = i;
+ }
+ if (destcol - outcol > 4 && (BC || BS)) {
+ if (TA)
+ tputs(TA, 0, plodput);
+ else
+ plodput('\t');
+ outcol = i;
+ while (outcol > destcol) {
+ outcol--;
+ if (BC)
+ tputs(BC, 0, plodput);
+ else
+ plodput('\b');
+ }
+ }
+ }
+ while (outcol < destcol) {
+ if (inopen && ND)
+ tputs(ND, 0, plodput);
+ else
+ plodput(' ');
+ outcol++;
+ if (plodflg && plodcnt < 0)
+ goto out;
+ }
+out:
+ if (plodflg) {
+ outcol = soutcol;
+ outline = soutline;
+ destcol = sdestcol;
+ destline = sdestline;
+ }
+ return(plodcnt);
+}