insert mode bug fix and cleanup
[unix-history] / usr / src / usr.bin / window / xxflush.c
CommitLineData
1cb6db79
EW
1/*
2 * Copyright (c) 1989 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18#ifndef lint
d9375810 19static char sccsid[] = "@(#)xxflush.c 3.2 (Berkeley) %G%";
1cb6db79
EW
20#endif /* not lint */
21
22#include "ww.h"
23#include "xx.h"
24#include "tt.h"
25
26xxflush(intr)
27 register intr;
28{
29 register struct xx *xp, *xq;
30
31 for (xp = xx_head; xp != 0 && !(intr && wwinterrupt()); xp = xq) {
32 switch (xp->cmd) {
33 case xc_move:
34 if (xp->link == 0)
35 (*tt.tt_move)(xp->arg0, xp->arg1);
36 break;
37 case xc_scroll:
38 xxflush_scroll(xp);
39 break;
40 case xc_inschar:
1cb6db79 41 (*tt.tt_move)(xp->arg0, xp->arg1);
d9375810
EW
42 tt.tt_nmodes = xp->arg3;
43 (*tt.tt_inschar)(xp->arg2);
44 break;
45 case xc_insspace:
46 (*tt.tt_move)(xp->arg0, xp->arg1);
47 (*tt.tt_insspace)(xp->arg2);
1cb6db79
EW
48 break;
49 case xc_delchar:
1cb6db79
EW
50 (*tt.tt_move)(xp->arg0, xp->arg1);
51 (*tt.tt_delchar)(xp->arg2);
52 break;
53 case xc_clear:
54 (*tt.tt_clear)();
55 break;
56 case xc_clreos:
57 (*tt.tt_move)(xp->arg0, xp->arg1);
58 (*tt.tt_clreos)();
59 break;
60 case xc_clreol:
61 (*tt.tt_move)(xp->arg0, xp->arg1);
62 (*tt.tt_clreol)();
63 break;
64 case xc_write:
65 (*tt.tt_move)(xp->arg0, xp->arg1);
66 tt.tt_nmodes = xp->arg3;
67 if (tt.tt_ntoken > 0)
68 xcwrite(xp->buf, xp->arg2, xp->buf - xxbuf);
69 else
70 (*tt.tt_write)(xp->buf, xp->arg2);
71 break;
72 }
73 xq = xp->link;
74 xxfree(xp);
75 }
76 if ((xx_head = xp) == 0) {
77 xx_tail = 0;
78 xxbufp = xxbuf;
79 if (tt.tt_ntoken > 0)
80 xcreset();
81 }
82 ttflush();
83}
84
85xxflush_scroll(xp)
86 register struct xx *xp;
87{
88 register struct xx *xq;
89
90 top:
91 if (xp->arg0 == 0)
92 return;
93 /*
94 * We handle retain (da and db) by putting the burden on scrolling up,
95 * which is the less common operation. It must ensure that
96 * text is not pushed below the screen, so scrolling down doesn't
97 * have to worry about it.
98 *
99 * Try scrolling region (or scrolling the whole screen) first.
100 * Can we assume "sr" doesn't push text below the screen
101 * so we don't have to worry about retain below?
102 * What about scrolling down with a newline? It probably does
103 * push text above (with da). Scrolling up would then have
104 * to take care of that.
105 * It's easy to be fool proof, but that slows things down.
106 * The current solution is to disallow tt_scroll_up if da or db is true
107 * but cs (scrolling region) is not. Again, we sacrifice scrolling
108 * up in favor of scrolling down. The idea is having scrolling regions
109 * probably means we can scroll (even the whole screen) with impunity.
110 * This lets us work efficiently on simple terminals (use newline
111 * on the bottom to scroll), on any terminal without retain, and
112 * on vt100 style scrolling regions (I think).
113 */
114 if (xp->arg0 > 0) {
115 if ((xq = xp->link) != 0 && xq->cmd == xc_scroll &&
116 xp->arg2 == xq->arg2 && xq->arg0 < 0) {
117 if (xp->arg1 < xq->arg1) {
118 if (xp->arg2 - xp->arg0 <= xq->arg1) {
119 xq->arg0 = xp->arg0;
120 xq->arg1 = xp->arg1;
121 xq->arg2 = xp->arg2;
122 return;
123 }
124 xp->arg2 = xq->arg1 + xp->arg0;
125 xq->arg0 += xp->arg0;
126 xq->arg1 = xp->arg2;
127 if (xq->arg0 > 0)
128 xq->arg1 -= xq->arg0;
129 goto top;
130 } else {
131 if (xp->arg1 - xq->arg0 >= xp->arg2)
132 return;
133 xq->arg2 = xp->arg1 - xq->arg0;
134 xp->arg0 += xq->arg0;
135 xp->arg1 = xq->arg2;
136 if (xp->arg0 < 0)
137 xp->arg1 += xp->arg0;
138 goto top;
139 }
140 }
141 if (xp->arg0 > xp->arg2 - xp->arg1)
142 xp->arg0 = xp->arg2 - xp->arg1;
143 if (tt.tt_scroll_down) {
144 if (tt.tt_scroll_top != xp->arg1 ||
145 tt.tt_scroll_bot != xp->arg2 - 1) {
146 if (tt.tt_setscroll == 0)
147 goto down;
148 (*tt.tt_setscroll)(xp->arg1, xp->arg2 - 1);
149 }
150 tt.tt_scroll_down(xp->arg0);
151 } else {
152 down:
153 (*tt.tt_move)(xp->arg1, 0);
154 (*tt.tt_delline)(xp->arg0);
155 if (xp->arg2 < tt.tt_nrow) {
156 (*tt.tt_move)(xp->arg2 - xp->arg0, 0);
157 (*tt.tt_insline)(xp->arg0);
158 }
159 }
160 } else {
161 xp->arg0 = - xp->arg0;
162 if (xp->arg0 > xp->arg2 - xp->arg1)
163 xp->arg0 = xp->arg2 - xp->arg1;
164 if (tt.tt_scroll_up) {
165 if (tt.tt_scroll_top != xp->arg1 ||
166 tt.tt_scroll_bot != xp->arg2 - 1) {
167 if (tt.tt_setscroll == 0)
168 goto up;
169 (*tt.tt_setscroll)(xp->arg1, xp->arg2 - 1);
170 }
171 tt.tt_scroll_up(xp->arg0);
172 } else {
173 up:
174 if (tt.tt_retain || xp->arg2 != tt.tt_nrow) {
175 (*tt.tt_move)(xp->arg2 - xp->arg0, 0);
176 (*tt.tt_delline)(xp->arg0);
177 }
178 (*tt.tt_move)(xp->arg1, 0);
179 (*tt.tt_insline)(xp->arg0);
180 }
181 }
182}