2 * Copyright (c) 1981, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char sccsid[] = "@(#)cr_put.c 8.3 (Berkeley) 5/4/94";
45 * Terminal driving and line formatting routines. Basic motion optimizations
46 * are done here as well as formatting lines (printing of control characters,
47 * line numbering and the like).
50 /* Stub function for the users. */
55 return (__mvcur(ly, lx, y, x, 0));
58 static void fgoto __P((int));
59 static int plod __P((int, int));
60 static void plodput __P((int));
61 static int tabcol __P((int, int));
63 static int outcol, outline, destcol, destline;
66 * Sync the position of the output cursor. Most work here is rounding for
67 * terminal boundaries getting the column position implied by wraparound or
68 * the lack thereof and rolling up the screen to get destline on the screen.
71 __mvcur(ly, lx, y, x, in_refresh)
72 int ly, lx, y, x, in_refresh;
75 __CTRACE("mvcur: moving cursor from (%d, %d) to (%d, %d)\n",
82 if (destline != outline || destcol != outcol)
94 if (destcol >= COLS) {
95 destline += destcol / COLS;
99 l = (outcol + 1) / COLS;
106 tputs(CR, 1, __cputchar);
110 tputs(NL, 1, __cputchar);
117 if (outline > LINES - 1) {
118 destline -= outline - (LINES - 1);
122 if (destline >= LINES) {
124 destline = LINES - 1;
125 if (outline < LINES - 1) {
127 if (__pfast == 0 && !CA)
133 /* The following linefeed (or simulation thereof) is
134 * supposed to scroll up the screen, since we are on
135 * the bottom line. We make the assumption that
136 * linefeed will scroll. If ns is in the capability
137 * list this won't work. We should probably have an
138 * sc capability but sf will generally take the place
141 * Superbee glitch: in the middle of the screen have
142 * to use esc B (down) because linefeed screws up in
143 * "Efficient Paging" (what a joke) mode (which is
144 * essential in some SB's because CRLF mode puts
145 * garbage in at end of memory), but you must use
146 * linefeed to scroll since down arrow won't go past
147 * memory end. I turned this off after recieving Paul
148 * Eggert's Superbee description which wins better.
150 if (NL /* && !XB */ && __pfast)
151 tputs(NL, 1, __cputchar);
159 if (destline < outline && !(CA || UP))
162 cgp = tgoto(CM, destcol, destline);
165 * Need this condition due to inconsistent behavior
166 * of backspace on the last column.
168 if (outcol != COLS - 1 && plod(strlen(cgp), in_refresh) > 0)
171 tputs(cgp, 1, __cputchar);
178 * Move (slowly) to destination.
179 * Hard thing here is using home cursor on really deficient terminals.
180 * Otherwise just use cursor motions, hacking use of tabs and overtabbing
184 static int plodcnt, plodflg;
197 plod(cnt, in_refresh)
200 register int i, j, k, soutcol, soutline;
202 plodcnt = plodflg = cnt;
206 * Consider homing and moving down/right from there, vs. moving
207 * directly with local motions to the right spot.
211 * i is the cost to home and tab/space to the right to get to
212 * the proper column. This assumes ND space costs 1 char. So
213 * i + destcol is cost of motion with home.
216 i = (destcol / HARDTABS) + (destcol % HARDTABS);
220 /* j is cost to move locally without homing. */
221 if (destcol >= outcol) { /* if motion is to the right */
222 j = destcol / HARDTABS - outcol / HARDTABS;
224 j += destcol % HARDTABS;
226 j = destcol - outcol;
228 /* leftward motion only works if we can backspace. */
229 if (outcol - destcol <= i && (BS || BC))
230 /* Cheaper to backspace. */
231 i = j = outcol - destcol;
233 /* Impossibly expensive. */
236 /* k is the absolute value of vertical distance. */
237 k = outline - destline;
242 /* Decision. We may not have a choice if no UP. */
243 if (i + destline < j || (!UP && destline < outline)) {
245 * Cheaper to home. Do it now and pretend it's a
246 * regular local motion.
248 tputs(HO, 1, plodput);
249 outcol = outline = 0;
252 * Quickly consider homing down and moving from there.
253 * Assume cost of LL is 2.
255 k = (LINES - 1) - destline;
256 if (i + k + 2 < j && (k <= 0 || UP)) {
257 tputs(LL, 1, plodput);
263 /* No home and no up means it's impossible. */
264 if (!UP && destline < outline)
267 i = destcol % HARDTABS + destcol / HARDTABS;
271 if (BT && outcol > destcol &&
272 (j = (((outcol+7) & ~7) - destcol - 1) >> 3)) {
273 j *= (k = strlen(BT));
274 if ((k += (destcol&7)) > 4)
275 j += 8 - (destcol&7);
281 j = outcol - destcol;
284 * If we will later need a \n which will turn into a \r\n by the
285 * system or the terminal, then don't bother to try to \r.
287 if ((NONL || !__pfast) && outline < destline)
291 * If the terminal will do a \r\n and there isn't room for it, then
292 * we can't afford a \r.
294 if (NC && outline >= destline)
298 * If it will be cheaper, or if we can't back up, then send a return
301 if ((j > i + 1) || (outcol > destcol && !BS && !BC)) {
303 * BUG: this doesn't take the (possibly long) length of CR
307 tputs(CR, 1, plodput);
312 tputs(NL, 1, plodput);
320 dontcr: while (outline < destline) {
323 tputs(NL, 1, plodput);
328 if (NONL || __pfast == 0)
333 while (outcol > destcol) {
337 if (BT && outcol - destcol > k + 4) {
338 tputs(BT, 0, plodput);
346 tputs(BC, 0, plodput);
350 while (outline > destline) {
352 tputs(UP, 1, plodput);
356 if (GT && destcol - outcol > 1) {
358 i = tabcol(outcol, HARDTABS);
362 tputs(TA, 0, plodput);
367 if (destcol - outcol > 4 && i < COLS && (BC || BS)) {
369 tputs(TA, 0, plodput);
373 while (outcol > destcol) {
376 tputs(BC, 0, plodput);
382 while (outcol < destcol) {
384 * Move one char to the right. We don't use ND space because
385 * it's better to just print the char we are moving over.
388 if (plodflg) /* Avoid a complex calculation. */
391 i = curscr->lines[outline]->line[outcol].ch;
392 if ((curscr->lines[outline]->line[outcol].attr
394 (curscr->flags & __WSTANDOUT))
401 tputs(ND, 0, plodput);
417 * Return the column number that results from being in column col and
418 * hitting a tab, where tabs are set every ts columns. Work right for
419 * the case where col > COLS, even if ts does not divide COLS.
428 offset = COLS * (col / COLS);
432 return (col + ts - (col % ts) + offset);