refresh.c revision 1.16 1 1.15 blymn /* $NetBSD: refresh.c,v 1.16 2000/04/12 21:47:20 jdc Exp $ */
2 1.8 mikel
3 1.1 cgd /*
4 1.7 cgd * Copyright (c) 1981, 1993, 1994
5 1.5 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.8 mikel #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.8 mikel #if 0
39 1.7 cgd static char sccsid[] = "@(#)refresh.c 8.7 (Berkeley) 8/13/94";
40 1.8 mikel #else
41 1.15 blymn __RCSID("$NetBSD: refresh.c,v 1.16 2000/04/12 21:47:20 jdc Exp $");
42 1.8 mikel #endif
43 1.11 mrg #endif /* not lint */
44 1.1 cgd
45 1.4 mycroft #include <string.h>
46 1.1 cgd
47 1.7 cgd #include "curses.h"
48 1.15 blymn #include "curses_private.h"
49 1.7 cgd
50 1.4 mycroft static int curwin;
51 1.4 mycroft static short ly, lx;
52 1.1 cgd
53 1.4 mycroft static void domvcur __P((int, int, int, int));
54 1.4 mycroft static int makech __P((WINDOW *, int));
55 1.7 cgd static void quickch __P((WINDOW *));
56 1.5 cgd static void scrolln __P((WINDOW *, int, int, int, int, int));
57 1.1 cgd
58 1.4 mycroft /*
59 1.4 mycroft * wrefresh --
60 1.4 mycroft * Make the current screen look like "win" over the area coverd by
61 1.4 mycroft * win.
62 1.4 mycroft */
63 1.4 mycroft int
64 1.1 cgd wrefresh(win)
65 1.10 perry WINDOW *win;
66 1.1 cgd {
67 1.10 perry __LINE *wlp;
68 1.11 mrg int retval;
69 1.11 mrg short wy;
70 1.11 mrg int dnum;
71 1.9 phil
72 1.9 phil /* Check if we need to restart ... */
73 1.9 phil if (__endwin) {
74 1.9 phil __endwin = 0;
75 1.9 phil __restartwin();
76 1.9 phil }
77 1.7 cgd
78 1.4 mycroft /* Initialize loop parameters. */
79 1.5 cgd ly = curscr->cury;
80 1.5 cgd lx = curscr->curx;
81 1.1 cgd wy = 0;
82 1.1 cgd curwin = (win == curscr);
83 1.1 cgd
84 1.5 cgd if (!curwin)
85 1.5 cgd for (wy = 0; wy < win->maxy; wy++) {
86 1.5 cgd wlp = win->lines[wy];
87 1.5 cgd if (wlp->flags & __ISDIRTY)
88 1.12 christos wlp->hash = __hash((char *)(void *)wlp->line,
89 1.11 mrg (int) (win->maxx * __LDATASIZE));
90 1.5 cgd }
91 1.5 cgd
92 1.5 cgd if (win->flags & __CLEAROK || curscr->flags & __CLEAROK || curwin) {
93 1.5 cgd if ((win->flags & __FULLWIN) || curscr->flags & __CLEAROK) {
94 1.16 jdc /* XXX: check for background colour and bce here */
95 1.4 mycroft tputs(CL, 0, __cputchar);
96 1.1 cgd ly = 0;
97 1.1 cgd lx = 0;
98 1.1 cgd if (!curwin) {
99 1.5 cgd curscr->flags &= ~__CLEAROK;
100 1.5 cgd curscr->cury = 0;
101 1.5 cgd curscr->curx = 0;
102 1.1 cgd werase(curscr);
103 1.1 cgd }
104 1.5 cgd __touchwin(win);
105 1.1 cgd }
106 1.5 cgd win->flags &= ~__CLEAROK;
107 1.1 cgd }
108 1.1 cgd if (!CA) {
109 1.5 cgd if (win->curx != 0)
110 1.4 mycroft putchar('\n');
111 1.1 cgd if (!curwin)
112 1.1 cgd werase(curscr);
113 1.1 cgd }
114 1.4 mycroft #ifdef DEBUG
115 1.5 cgd __CTRACE("wrefresh: (%0.2o): curwin = %d\n", win, curwin);
116 1.5 cgd __CTRACE("wrefresh: \tfirstch\tlastch\n");
117 1.5 cgd #endif
118 1.5 cgd
119 1.5 cgd if ((win->flags & __FULLWIN) && !curwin) {
120 1.5 cgd /*
121 1.5 cgd * Invoke quickch() only if more than a quarter of the lines
122 1.5 cgd * in the window are dirty.
123 1.5 cgd */
124 1.5 cgd for (wy = 0, dnum = 0; wy < win->maxy; wy++)
125 1.5 cgd if (win->lines[wy]->flags & (__ISDIRTY | __FORCEPAINT))
126 1.5 cgd dnum++;
127 1.5 cgd if (!__noqch && dnum > (int) win->maxy / 4)
128 1.5 cgd quickch(win);
129 1.5 cgd }
130 1.5 cgd
131 1.5 cgd #ifdef DEBUG
132 1.11 mrg {
133 1.11 mrg int i, j;
134 1.11 mrg
135 1.5 cgd __CTRACE("#####################################\n");
136 1.5 cgd for (i = 0; i < curscr->maxy; i++) {
137 1.5 cgd __CTRACE("C: %d:", i);
138 1.5 cgd __CTRACE(" 0x%x \n", curscr->lines[i]->hash);
139 1.7 cgd for (j = 0; j < curscr->maxx; j++)
140 1.11 mrg __CTRACE("%c", curscr->lines[i]->line[j].ch);
141 1.5 cgd __CTRACE("\n");
142 1.14 simonb __CTRACE(" attr:");
143 1.7 cgd for (j = 0; j < curscr->maxx; j++)
144 1.14 simonb __CTRACE(" %x", curscr->lines[i]->line[j].attr);
145 1.5 cgd __CTRACE("\n");
146 1.5 cgd __CTRACE("W: %d:", i);
147 1.15 blymn /* Handle small windows */
148 1.15 blymn if (i >= win->begy && i < (win->begy + win->maxy)) {
149 1.15 blymn __CTRACE(" 0x%x \n",
150 1.15 blymn win->lines[i - win->begy]->hash);
151 1.15 blymn __CTRACE(" 0x%x ",
152 1.15 blymn win->lines[i - win->begy]->flags);
153 1.15 blymn for (j = 0; j < win->maxx; j++)
154 1.15 blymn __CTRACE("%c", win->lines[i - win
155 1.15 blymn ->begy]->line[j].ch);
156 1.15 blymn __CTRACE("\n");
157 1.15 blymn __CTRACE(" attr:");
158 1.15 blymn for (j = 0; j < win->maxx; j++)
159 1.15 blymn __CTRACE(" %x", win->lines[i - win
160 1.15 blymn ->begy]->line[j].attr);
161 1.15 blymn __CTRACE("\n");
162 1.15 blymn }
163 1.5 cgd }
164 1.11 mrg }
165 1.11 mrg #endif /* DEBUG */
166 1.5 cgd
167 1.5 cgd for (wy = 0; wy < win->maxy; wy++) {
168 1.4 mycroft #ifdef DEBUG
169 1.13 simonb __CTRACE("wy %d\tf: %d\tl:%d\tflags %x\n",
170 1.13 simonb wy, *win->lines[wy]->firstchp, *win->lines[wy]->lastchp,
171 1.13 simonb win->lines[wy]->flags);
172 1.4 mycroft #endif
173 1.5 cgd if (!curwin)
174 1.5 cgd curscr->lines[wy]->hash = win->lines[wy]->hash;
175 1.5 cgd if (win->lines[wy]->flags & (__ISDIRTY | __FORCEPAINT)) {
176 1.1 cgd if (makech(win, wy) == ERR)
177 1.4 mycroft return (ERR);
178 1.1 cgd else {
179 1.5 cgd if (*win->lines[wy]->firstchp >= win->ch_off)
180 1.5 cgd *win->lines[wy]->firstchp = win->maxx +
181 1.5 cgd win->ch_off;
182 1.5 cgd if (*win->lines[wy]->lastchp < win->maxx +
183 1.5 cgd win->ch_off)
184 1.5 cgd *win->lines[wy]->lastchp = win->ch_off;
185 1.7 cgd if (*win->lines[wy]->lastchp <
186 1.5 cgd *win->lines[wy]->firstchp) {
187 1.5 cgd #ifdef DEBUG
188 1.5 cgd __CTRACE("wrefresh: line %d notdirty \n", wy);
189 1.5 cgd #endif
190 1.5 cgd win->lines[wy]->flags &= ~__ISDIRTY;
191 1.5 cgd }
192 1.1 cgd }
193 1.5 cgd
194 1.5 cgd }
195 1.4 mycroft #ifdef DEBUG
196 1.7 cgd __CTRACE("\t%d\t%d\n", *win->lines[wy]->firstchp,
197 1.11 mrg *win->lines[wy]->lastchp);
198 1.4 mycroft #endif
199 1.1 cgd }
200 1.7 cgd
201 1.5 cgd #ifdef DEBUG
202 1.5 cgd __CTRACE("refresh: ly=%d, lx=%d\n", ly, lx);
203 1.5 cgd #endif
204 1.1 cgd
205 1.1 cgd if (win == curscr)
206 1.11 mrg domvcur(ly, lx, (int) win->cury, (int) win->curx);
207 1.1 cgd else {
208 1.5 cgd if (win->flags & __LEAVEOK) {
209 1.5 cgd curscr->cury = ly;
210 1.5 cgd curscr->curx = lx;
211 1.5 cgd ly -= win->begy;
212 1.5 cgd lx -= win->begx;
213 1.5 cgd if (ly >= 0 && ly < win->maxy && lx >= 0 &&
214 1.5 cgd lx < win->maxx) {
215 1.5 cgd win->cury = ly;
216 1.5 cgd win->curx = lx;
217 1.4 mycroft } else
218 1.5 cgd win->cury = win->curx = 0;
219 1.4 mycroft } else {
220 1.11 mrg domvcur(ly, lx, (int) (win->cury + win->begy),
221 1.11 mrg (int) (win->curx + win->begx));
222 1.5 cgd curscr->cury = win->cury + win->begy;
223 1.5 cgd curscr->curx = win->curx + win->begx;
224 1.1 cgd }
225 1.1 cgd }
226 1.1 cgd retval = OK;
227 1.4 mycroft
228 1.11 mrg (void) fflush(stdout);
229 1.4 mycroft return (retval);
230 1.1 cgd }
231 1.1 cgd
232 1.1 cgd /*
233 1.4 mycroft * makech --
234 1.4 mycroft * Make a change on the screen.
235 1.1 cgd */
236 1.4 mycroft static int
237 1.1 cgd makech(win, wy)
238 1.10 perry WINDOW *win;
239 1.11 mrg int wy;
240 1.1 cgd {
241 1.5 cgd static __LDATA blank = {' ', 0};
242 1.7 cgd __LDATA *nsp, *csp, *cp, *cep;
243 1.11 mrg u_int force;
244 1.11 mrg int clsp, nlsp; /* Last space in lines. */
245 1.11 mrg int lch, wx, y;
246 1.11 mrg char *ce;
247 1.5 cgd
248 1.8 mikel #ifdef __GNUC__
249 1.8 mikel nlsp = 0; /* XXX gcc -Wuninitialized */
250 1.8 mikel #endif
251 1.5 cgd /* Is the cursor still on the end of the last line? */
252 1.5 cgd if (wy > 0 && win->lines[wy - 1]->flags & __ISPASTEOL) {
253 1.5 cgd domvcur(ly, lx, ly + 1, 0);
254 1.5 cgd ly++;
255 1.5 cgd lx = 0;
256 1.5 cgd }
257 1.5 cgd wx = *win->lines[wy]->firstchp - win->ch_off;
258 1.5 cgd if (wx < 0)
259 1.5 cgd wx = 0;
260 1.11 mrg else
261 1.11 mrg if (wx >= win->maxx)
262 1.11 mrg return (OK);
263 1.5 cgd lch = *win->lines[wy]->lastchp - win->ch_off;
264 1.1 cgd if (lch < 0)
265 1.4 mycroft return (OK);
266 1.11 mrg else
267 1.11 mrg if (lch >= (int) win->maxx)
268 1.11 mrg lch = win->maxx - 1;
269 1.5 cgd y = wy + win->begy;
270 1.1 cgd
271 1.1 cgd if (curwin)
272 1.5 cgd csp = ␣
273 1.1 cgd else
274 1.5 cgd csp = &curscr->lines[wy + win->begy]->line[wx + win->begx];
275 1.1 cgd
276 1.5 cgd nsp = &win->lines[wy]->line[wx];
277 1.5 cgd force = win->lines[wy]->flags & __FORCEPAINT;
278 1.5 cgd win->lines[wy]->flags &= ~__FORCEPAINT;
279 1.1 cgd if (CE && !curwin) {
280 1.7 cgd for (cp = &win->lines[wy]->line[win->maxx - 1];
281 1.11 mrg cp->ch == ' ' && cp->attr == 0; cp--)
282 1.5 cgd if (cp <= win->lines[wy]->line)
283 1.1 cgd break;
284 1.5 cgd nlsp = cp - win->lines[wy]->line;
285 1.1 cgd }
286 1.1 cgd if (!curwin)
287 1.4 mycroft ce = CE;
288 1.1 cgd else
289 1.4 mycroft ce = NULL;
290 1.1 cgd
291 1.5 cgd if (force) {
292 1.5 cgd if (CM)
293 1.5 cgd tputs(tgoto(CM, lx, ly), 0, __cputchar);
294 1.5 cgd else {
295 1.5 cgd tputs(HO, 0, __cputchar);
296 1.5 cgd __mvcur(0, 0, ly, lx, 1);
297 1.5 cgd }
298 1.5 cgd }
299 1.7 cgd
300 1.1 cgd while (wx <= lch) {
301 1.5 cgd if (!force && memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
302 1.4 mycroft if (wx <= lch) {
303 1.5 cgd while (wx <= lch &&
304 1.7 cgd memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
305 1.7 cgd nsp++;
306 1.7 cgd if (!curwin)
307 1.7 cgd ++csp;
308 1.7 cgd ++wx;
309 1.7 cgd }
310 1.4 mycroft continue;
311 1.4 mycroft }
312 1.4 mycroft break;
313 1.4 mycroft }
314 1.11 mrg domvcur(ly, lx, y, (int) (wx + win->begx));
315 1.5 cgd
316 1.4 mycroft #ifdef DEBUG
317 1.7 cgd __CTRACE("makech: 1: wx = %d, ly= %d, lx = %d, newy = %d, newx = %d, force =%d\n",
318 1.5 cgd wx, ly, lx, y, wx + win->begx, force);
319 1.4 mycroft #endif
320 1.4 mycroft ly = y;
321 1.5 cgd lx = wx + win->begx;
322 1.7 cgd while ((force || memcmp(nsp, csp, sizeof(__LDATA)) != 0)
323 1.5 cgd && wx <= lch) {
324 1.5 cgd
325 1.7 cgd if (ce != NULL &&
326 1.7 cgd win->maxx + win->begx == curscr->maxx &&
327 1.7 cgd wx >= nlsp && nsp->ch == ' ' && nsp->attr == 0) {
328 1.4 mycroft /* Check for clear to end-of-line. */
329 1.5 cgd cep = &curscr->lines[wy]->line[win->maxx - 1];
330 1.5 cgd while (cep->ch == ' ' && cep->attr == 0)
331 1.5 cgd if (cep-- <= csp)
332 1.4 mycroft break;
333 1.7 cgd clsp = cep - curscr->lines[wy]->line -
334 1.11 mrg win->begx * __LDATASIZE;
335 1.4 mycroft #ifdef DEBUG
336 1.11 mrg __CTRACE("makech: clsp = %d, nlsp = %d\n", clsp, nlsp);
337 1.4 mycroft #endif
338 1.16 jdc /* XXX: check for background colour and bce here */
339 1.7 cgd if ((clsp - nlsp >= strlen(CE)
340 1.5 cgd && clsp < win->maxx * __LDATASIZE) ||
341 1.5 cgd wy == win->maxy - 1) {
342 1.15 blymn if (curscr->wattr & __STANDOUT) {
343 1.7 cgd tputs(SE, 0, __cputchar);
344 1.15 blymn curscr->wattr &= ~__STANDOUT;
345 1.15 blymn if (UE != NULL &&
346 1.15 blymn !strcmp(SE, UE))
347 1.16 jdc curscr->wattr &=
348 1.15 blymn ~__UNDERSCORE;
349 1.15 blymn if (ME != NULL &&
350 1.15 blymn !strcmp(SE, ME))
351 1.16 jdc curscr->wattr &=
352 1.16 jdc ~__ATTRIBUTES |
353 1.16 jdc __ALTCHARSET |
354 1.16 jdc __COLOR;
355 1.16 jdc if (OP != NULL &&
356 1.16 jdc !strcmp(SE, OP))
357 1.16 jdc curscr->wattr &=
358 1.16 jdc __COLOR;
359 1.11 mrg }
360 1.15 blymn if (curscr->wattr & __UNDERSCORE) {
361 1.11 mrg tputs(UE, 0, __cputchar);
362 1.15 blymn curscr->wattr &= ~__UNDERSCORE;
363 1.15 blymn if (ME != NULL &&
364 1.15 blymn !strcmp(UE, ME))
365 1.16 jdc curscr->wattr &=
366 1.16 jdc ~__ATTRIBUTES |
367 1.16 jdc __ALTCHARSET |
368 1.16 jdc __COLOR;
369 1.16 jdc if (OP != NULL &&
370 1.16 jdc !strcmp(UE, OP))
371 1.16 jdc curscr->wattr &=
372 1.16 jdc __COLOR;
373 1.15 blymn }
374 1.16 jdc if (curscr->wattr & __ATTRIBUTES) {
375 1.15 blymn tputs(ME, 0, __cputchar);
376 1.16 jdc if (OP != NULL &&
377 1.16 jdc !strcmp(ME, OP))
378 1.16 jdc curscr->wattr &=
379 1.16 jdc __COLOR;
380 1.11 mrg }
381 1.15 blymn if (curscr->wattr & __ALTCHARSET) {
382 1.15 blymn tputs(AE, 0, __cputchar);
383 1.15 blymn curscr->wattr &= ~__ALTCHARSET;
384 1.7 cgd }
385 1.16 jdc if (curscr->wattr & __COLOR) {
386 1.16 jdc if (OC != NULL && CC == NULL)
387 1.16 jdc tputs(OC, 0,
388 1.16 jdc __cputchar);
389 1.16 jdc if (OP != NULL)
390 1.16 jdc tputs(OP, 0,
391 1.16 jdc __cputchar);
392 1.16 jdc curscr->wattr &= ~__COLOR;
393 1.16 jdc }
394 1.4 mycroft tputs(CE, 0, __cputchar);
395 1.5 cgd lx = wx + win->begx;
396 1.5 cgd while (wx++ <= clsp) {
397 1.5 cgd csp->ch = ' ';
398 1.5 cgd csp->attr = 0;
399 1.5 cgd csp++;
400 1.5 cgd }
401 1.4 mycroft return (OK);
402 1.1 cgd }
403 1.4 mycroft ce = NULL;
404 1.4 mycroft }
405 1.4 mycroft
406 1.7 cgd /*
407 1.16 jdc * Unset colour if appropriate. Check to see
408 1.16 jdc * if we also turn off standout, underscore and
409 1.16 jdc * attributes.
410 1.16 jdc */
411 1.16 jdc if (!(nsp->attr & __COLOR) &&
412 1.16 jdc (curscr->wattr & __COLOR)) {
413 1.16 jdc if (OC != NULL && CC == NULL)
414 1.16 jdc tputs(OC, 0, __cputchar);
415 1.16 jdc if (OP != NULL) {
416 1.16 jdc tputs(OP, 0, __cputchar);
417 1.16 jdc if (SE != NULL && !strcmp(OP, SE))
418 1.16 jdc curscr->wattr &= ~__STANDOUT;
419 1.16 jdc if (UE != NULL && !strcmp(OP, UE))
420 1.16 jdc curscr->wattr &= ~__UNDERSCORE;
421 1.16 jdc if (ME != NULL && !strcmp(OP, ME))
422 1.16 jdc curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET;
423 1.16 jdc }
424 1.16 jdc curscr->wattr &= ~__COLOR;
425 1.16 jdc }
426 1.16 jdc
427 1.16 jdc /*
428 1.11 mrg * Unset attributes as appropriate. Unset first
429 1.11 mrg * so that the relevant attributes can be reset
430 1.16 jdc * (because 'me' unsets 'mb', 'md', 'mh', 'mk',
431 1.16 jdc * 'mp' and 'mr'). Check to see if we also turn off
432 1.16 jdc * standout, attributes and colour.
433 1.11 mrg */
434 1.11 mrg if ((!(nsp->attr & __BLINK) &&
435 1.15 blymn curscr->wattr & __BLINK) ||
436 1.11 mrg (!(nsp->attr & __BOLD) &&
437 1.15 blymn curscr->wattr & __BOLD) ||
438 1.11 mrg (!(nsp->attr & __DIM) &&
439 1.15 blymn curscr->wattr & __DIM) ||
440 1.11 mrg (!(nsp->attr & __BLANK) &&
441 1.15 blymn curscr->wattr & __BLANK) ||
442 1.11 mrg (!(nsp->attr & __PROTECT) &&
443 1.15 blymn curscr->wattr & __PROTECT) ||
444 1.11 mrg (!(nsp->attr & __REVERSE) &&
445 1.15 blymn curscr->wattr & __REVERSE)) {
446 1.15 blymn tputs(ME, 0, __cputchar);
447 1.16 jdc curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET
448 1.16 jdc | __COLOR;
449 1.15 blymn if (SE != NULL && !strcmp(ME, SE))
450 1.15 blymn curscr->wattr &= ~__STANDOUT;
451 1.15 blymn if (UE != NULL && !strcmp(ME, UE))
452 1.15 blymn curscr->wattr &= ~__UNDERSCORE;
453 1.16 jdc if (OP != NULL && !strcmp(ME, OP))
454 1.16 jdc curscr->wattr &= ~__COLOR;
455 1.11 mrg }
456 1.11 mrg
457 1.11 mrg /*
458 1.11 mrg * Exit underscore mode if appropriate.
459 1.16 jdc * Check to see if we also turn off standout,
460 1.16 jdc * attributes and colour.
461 1.11 mrg */
462 1.11 mrg if (!(nsp->attr & __UNDERSCORE) &&
463 1.15 blymn (curscr->wattr & __UNDERSCORE)) {
464 1.11 mrg tputs(UE, 0, __cputchar);
465 1.15 blymn curscr->wattr &= ~__UNDERSCORE;
466 1.15 blymn if (SE != NULL && !strcmp(UE, SE))
467 1.15 blymn curscr->wattr &= ~__STANDOUT;
468 1.15 blymn if (ME != NULL && !strcmp(UE, ME))
469 1.16 jdc curscr->wattr &= ~__ATTRIBUTES |
470 1.16 jdc __ALTCHARSET | __COLOR;
471 1.16 jdc if (OP != NULL && !strcmp(UE, OP))
472 1.16 jdc curscr->wattr &= ~__COLOR;
473 1.11 mrg }
474 1.11 mrg
475 1.11 mrg /*
476 1.7 cgd * Enter/exit standout mode as appropriate.
477 1.16 jdc * Check to see if we also turn off underscore,
478 1.16 jdc * attributes and colour.
479 1.7 cgd * XXX
480 1.7 cgd * Should use UC if SO/SE not available.
481 1.7 cgd */
482 1.7 cgd if (nsp->attr & __STANDOUT) {
483 1.15 blymn if (!(curscr->wattr & __STANDOUT) &&
484 1.7 cgd SO != NULL && SE != NULL) {
485 1.4 mycroft tputs(SO, 0, __cputchar);
486 1.15 blymn curscr->wattr |= __STANDOUT;
487 1.7 cgd }
488 1.11 mrg } else {
489 1.15 blymn if (curscr->wattr & __STANDOUT) {
490 1.4 mycroft tputs(SE, 0, __cputchar);
491 1.15 blymn curscr->wattr &= ~__STANDOUT;
492 1.15 blymn if (UE != NULL && !strcmp(SE, UE))
493 1.16 jdc curscr->wattr &=
494 1.15 blymn ~__UNDERSCORE;
495 1.15 blymn if (ME != NULL && !strcmp(SE, ME))
496 1.16 jdc curscr->wattr &= ~__ATTRIBUTES |
497 1.16 jdc __ALTCHARSET | __COLOR;
498 1.16 jdc if (OP != NULL && !strcmp(SE, OP))
499 1.16 jdc curscr->wattr &= ~__COLOR;
500 1.11 mrg }
501 1.11 mrg }
502 1.11 mrg
503 1.11 mrg /*
504 1.11 mrg * Enter underscore mode if appropriate.
505 1.11 mrg * XXX
506 1.11 mrg * Should use UC if US/UE not available.
507 1.11 mrg */
508 1.11 mrg if (nsp->attr & __UNDERSCORE &&
509 1.15 blymn !(curscr->wattr & __UNDERSCORE) &&
510 1.11 mrg US != NULL && UE != NULL) {
511 1.11 mrg tputs(US, 0, __cputchar);
512 1.15 blymn curscr->wattr |= __UNDERSCORE;
513 1.11 mrg }
514 1.11 mrg
515 1.11 mrg /*
516 1.11 mrg * Set other attributes as appropriate.
517 1.11 mrg */
518 1.11 mrg if (nsp->attr & __BLINK) {
519 1.15 blymn if (!(curscr->wattr & __BLINK) &&
520 1.11 mrg MB != NULL && ME != NULL) {
521 1.11 mrg tputs(MB, 0, __cputchar);
522 1.15 blymn curscr->wattr |= __BLINK;
523 1.15 blymn }
524 1.15 blymn }
525 1.15 blymn if (nsp->attr & __BOLD) {
526 1.15 blymn if (!(curscr->wattr & __BOLD) &&
527 1.15 blymn MD != NULL && ME != NULL) {
528 1.15 blymn tputs(MD, 0, __cputchar);
529 1.15 blymn curscr->wattr |= __BOLD;
530 1.11 mrg }
531 1.11 mrg }
532 1.11 mrg if (nsp->attr & __DIM) {
533 1.15 blymn if (!(curscr->wattr & __DIM) &&
534 1.11 mrg MH != NULL && ME != NULL) {
535 1.11 mrg tputs(MH, 0, __cputchar);
536 1.15 blymn curscr->wattr |= __DIM;
537 1.11 mrg }
538 1.11 mrg }
539 1.11 mrg if (nsp->attr & __BLANK) {
540 1.15 blymn if (!(curscr->wattr & __BLANK) &&
541 1.11 mrg MK != NULL && ME != NULL) {
542 1.11 mrg tputs(MK, 0, __cputchar);
543 1.15 blymn curscr->wattr |= __BLANK;
544 1.11 mrg }
545 1.11 mrg }
546 1.11 mrg if (nsp->attr & __PROTECT) {
547 1.15 blymn if (!(curscr->wattr & __PROTECT) &&
548 1.11 mrg MP != NULL && ME != NULL) {
549 1.11 mrg tputs(MP, 0, __cputchar);
550 1.15 blymn curscr->wattr |= __PROTECT;
551 1.15 blymn }
552 1.15 blymn }
553 1.15 blymn if (nsp->attr & __REVERSE) {
554 1.15 blymn if (!(curscr->wattr & __REVERSE) &&
555 1.15 blymn MR != NULL && ME != NULL) {
556 1.15 blymn tputs(MR, 0, __cputchar);
557 1.15 blymn curscr->wattr |= __REVERSE;
558 1.15 blymn }
559 1.15 blymn }
560 1.15 blymn
561 1.16 jdc /* Set/change colour as appropriate. */
562 1.16 jdc if (nsp->attr & __COLOR) {
563 1.16 jdc if ((!(curscr->wattr & __COLOR) &&
564 1.16 jdc cO != NULL && (OC != NULL || OP != NULL)) ||
565 1.16 jdc ((nsp->attr & __COLOR) !=
566 1.16 jdc (curscr->wattr & __COLOR))) {
567 1.16 jdc __set_color(nsp->attr);
568 1.16 jdc curscr->wattr &= ~__COLOR;
569 1.16 jdc curscr->wattr |= nsp->attr & __COLOR;
570 1.16 jdc }
571 1.16 jdc }
572 1.16 jdc
573 1.15 blymn /* Enter/exit altcharset mode as appropriate. */
574 1.15 blymn if (nsp->attr & __ALTCHARSET) {
575 1.15 blymn if (!(curscr->wattr & __ALTCHARSET) &&
576 1.15 blymn AS != NULL && AE != NULL) {
577 1.15 blymn tputs(AS, 0, __cputchar);
578 1.15 blymn curscr->wattr |= __ALTCHARSET;
579 1.15 blymn }
580 1.15 blymn } else {
581 1.15 blymn if (curscr->wattr & __ALTCHARSET) {
582 1.15 blymn tputs(AE, 0, __cputchar);
583 1.15 blymn curscr->wattr &= ~__ALTCHARSET;
584 1.11 mrg }
585 1.11 mrg }
586 1.4 mycroft
587 1.4 mycroft wx++;
588 1.5 cgd if (wx >= win->maxx && wy == win->maxy - 1 && !curwin)
589 1.5 cgd if (win->flags & __SCROLLOK) {
590 1.15 blymn if (curscr->wattr & __STANDOUT
591 1.5 cgd && win->flags & __ENDLINE)
592 1.15 blymn if (!MS && SE != NULL) {
593 1.4 mycroft tputs(SE, 0,
594 1.4 mycroft __cputchar);
595 1.16 jdc curscr->wattr &=
596 1.15 blymn ~__STANDOUT;
597 1.15 blymn if (UE != NULL &&
598 1.15 blymn !strcmp(SE, UE))
599 1.16 jdc curscr->wattr &=
600 1.15 blymn ~__UNDERSCORE;
601 1.15 blymn if (ME != NULL &&
602 1.15 blymn !strcmp(SE, ME))
603 1.16 jdc curscr->wattr &=
604 1.16 jdc ~__ATTRIBUTES |
605 1.16 jdc __ALTCHARSET |
606 1.16 jdc __COLOR;
607 1.16 jdc if (OP != NULL &&
608 1.16 jdc !strcmp(SE, OP))
609 1.16 jdc curscr->wattr &=
610 1.16 jdc __COLOR;
611 1.4 mycroft }
612 1.15 blymn if (curscr->wattr & __UNDERSCORE
613 1.11 mrg && win->flags & __ENDLINE)
614 1.15 blymn if (!MS && UE != NULL) {
615 1.11 mrg tputs(UE, 0,
616 1.11 mrg __cputchar);
617 1.16 jdc curscr->wattr &=
618 1.15 blymn ~__UNDERSCORE;
619 1.15 blymn if (ME != NULL &&
620 1.16 jdc !strcmp(UE, ME))
621 1.16 jdc curscr->wattr &=
622 1.16 jdc ~__ATTRIBUTES |
623 1.16 jdc __ALTCHARSET |
624 1.16 jdc __COLOR;
625 1.16 jdc if (OP != NULL &&
626 1.16 jdc !strcmp(SE, OP))
627 1.16 jdc curscr->wattr &=
628 1.16 jdc __COLOR;
629 1.11 mrg }
630 1.16 jdc if (curscr->wattr & __ATTRIBUTES
631 1.11 mrg && win->flags & __ENDLINE)
632 1.15 blymn if (!MS && ME != NULL) {
633 1.11 mrg tputs(ME, 0,
634 1.11 mrg __cputchar);
635 1.16 jdc curscr->wattr &=
636 1.16 jdc ~__ATTRIBUTES |
637 1.16 jdc __ALTCHARSET |
638 1.16 jdc __COLOR;
639 1.16 jdc if (OP != NULL &&
640 1.16 jdc !strcmp(SE, OP))
641 1.16 jdc curscr->wattr &=
642 1.16 jdc __COLOR;
643 1.11 mrg }
644 1.5 cgd if (!(win->flags & __SCROLLWIN)) {
645 1.5 cgd if (!curwin) {
646 1.5 cgd csp->attr = nsp->attr;
647 1.15 blymn putchar((int) (csp->ch = nsp->ch));
648 1.15 blymn
649 1.5 cgd } else
650 1.15 blymn putchar((int) nsp->ch);
651 1.5 cgd }
652 1.5 cgd if (wx + win->begx < curscr->maxx) {
653 1.11 mrg domvcur(ly, (int) (wx + win->begx),
654 1.11 mrg (int) (win->begy + win->maxy - 1),
655 1.11 mrg (int) (win->begx + win->maxx - 1));
656 1.5 cgd }
657 1.5 cgd ly = win->begy + win->maxy - 1;
658 1.5 cgd lx = win->begx + win->maxx - 1;
659 1.4 mycroft return (OK);
660 1.7 cgd }
661 1.7 cgd if (wx < win->maxx || wy < win->maxy - 1 ||
662 1.5 cgd !(win->flags & __SCROLLWIN)) {
663 1.5 cgd if (!curwin) {
664 1.5 cgd csp->attr = nsp->attr;
665 1.15 blymn putchar((int) (csp->ch = nsp->ch));
666 1.5 cgd csp++;
667 1.7 cgd } else
668 1.15 blymn putchar((int) nsp->ch);
669 1.5 cgd }
670 1.4 mycroft #ifdef DEBUG
671 1.5 cgd __CTRACE("makech: putchar(%c)\n", nsp->ch & 0177);
672 1.4 mycroft #endif
673 1.11 mrg if (UC && ((nsp->attr & __STANDOUT) ||
674 1.11 mrg (nsp->attr & __UNDERSCORE))) {
675 1.4 mycroft putchar('\b');
676 1.4 mycroft tputs(UC, 0, __cputchar);
677 1.1 cgd }
678 1.4 mycroft nsp++;
679 1.4 mycroft #ifdef DEBUG
680 1.11 mrg __CTRACE("makech: 2: wx = %d, lx = %d\n", wx, lx);
681 1.4 mycroft #endif
682 1.5 cgd }
683 1.5 cgd if (lx == wx + win->begx) /* If no change. */
684 1.4 mycroft break;
685 1.5 cgd lx = wx + win->begx;
686 1.5 cgd if (lx >= COLS && AM)
687 1.5 cgd lx = COLS - 1;
688 1.11 mrg else
689 1.11 mrg if (wx >= win->maxx) {
690 1.11 mrg domvcur(ly, lx, ly, (int) (win->maxx + win->begx - 1));
691 1.11 mrg lx = win->maxx + win->begx - 1;
692 1.11 mrg }
693 1.4 mycroft #ifdef DEBUG
694 1.5 cgd __CTRACE("makech: 3: wx = %d, lx = %d\n", wx, lx);
695 1.4 mycroft #endif
696 1.1 cgd }
697 1.7 cgd
698 1.7 cgd /* Don't leave the screen in standout mode. */
699 1.15 blymn if (curscr->wattr & __STANDOUT) {
700 1.7 cgd tputs(SE, 0, __cputchar);
701 1.15 blymn curscr->wattr &= ~__STANDOUT;
702 1.15 blymn if (UE != NULL && !strcmp(SE, UE))
703 1.15 blymn curscr->wattr &= ~__UNDERSCORE;
704 1.15 blymn if (ME != NULL && !strcmp(SE, ME))
705 1.16 jdc curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET | __COLOR;
706 1.16 jdc if (OP != NULL && !strcmp(SE, OP))
707 1.16 jdc curscr->wattr &= ~__COLOR;
708 1.7 cgd }
709 1.11 mrg /* Don't leave the screen in underscore mode. */
710 1.15 blymn if (curscr->wattr & __UNDERSCORE) {
711 1.11 mrg tputs(UE, 0, __cputchar);
712 1.15 blymn curscr->wattr &= ~__UNDERSCORE;
713 1.15 blymn if (ME != NULL && !strcmp(UE, ME))
714 1.16 jdc curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET | __COLOR;
715 1.16 jdc if (OP != NULL && !strcmp(UE, OP))
716 1.16 jdc curscr->wattr &= ~__COLOR;
717 1.11 mrg }
718 1.11 mrg /* Don't leave the screen with attributes set. */
719 1.16 jdc if (curscr->wattr & __ATTRIBUTES) {
720 1.11 mrg tputs(ME, 0, __cputchar);
721 1.16 jdc curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET | __COLOR;
722 1.16 jdc if (OP != NULL && !strcmp(ME, OP))
723 1.16 jdc curscr->wattr &= ~__COLOR;
724 1.15 blymn }
725 1.15 blymn /* Don't leave the screen with altcharset set. */
726 1.15 blymn if (curscr->wattr & __ALTCHARSET) {
727 1.15 blymn tputs(AE, 0, __cputchar);
728 1.15 blymn curscr->wattr &= ~__ALTCHARSET;
729 1.11 mrg }
730 1.16 jdc /* Don't leave the screen with colour set. */
731 1.16 jdc if (curscr->wattr & __COLOR) {
732 1.16 jdc if (OC != NULL && CC == NULL)
733 1.16 jdc tputs(OC, 0, __cputchar);
734 1.16 jdc if (OP != NULL)
735 1.16 jdc tputs(OP, 0, __cputchar);
736 1.16 jdc curscr->wattr &= ~__COLOR;
737 1.16 jdc }
738 1.4 mycroft return (OK);
739 1.1 cgd }
740 1.1 cgd
741 1.1 cgd /*
742 1.4 mycroft * domvcur --
743 1.15 blymn * Do a mvcur, leaving standout, attribute and altcharset modes if
744 1.15 blymn * necessary.
745 1.1 cgd */
746 1.4 mycroft static void
747 1.1 cgd domvcur(oy, ox, ny, nx)
748 1.11 mrg int oy, ox, ny, nx;
749 1.4 mycroft {
750 1.15 blymn if (curscr->wattr & __STANDOUT && !MS) {
751 1.4 mycroft tputs(SE, 0, __cputchar);
752 1.15 blymn curscr->wattr &= ~__STANDOUT;
753 1.15 blymn if (UE != NULL && !strcmp(SE, UE))
754 1.15 blymn curscr->wattr &= ~__UNDERSCORE;
755 1.15 blymn if (ME != NULL && !strcmp(SE, ME))
756 1.16 jdc curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET | __COLOR;
757 1.16 jdc if (OP != NULL && !strcmp(SE, OP))
758 1.16 jdc curscr->wattr &= ~__COLOR;
759 1.16 jdc
760 1.5 cgd }
761 1.15 blymn if (curscr->wattr & __UNDERSCORE && !MS) {
762 1.11 mrg tputs(UE, 0, __cputchar);
763 1.15 blymn curscr->wattr &= ~__UNDERSCORE;
764 1.15 blymn if (ME != NULL && !strcmp(UE, ME))
765 1.16 jdc curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET | __COLOR;
766 1.16 jdc if (OP != NULL && !strcmp(UE, OP))
767 1.16 jdc curscr->wattr &= ~__COLOR;
768 1.11 mrg }
769 1.16 jdc if (curscr->wattr & __ATTRIBUTES && !MS) {
770 1.11 mrg tputs(ME, 0, __cputchar);
771 1.16 jdc curscr->wattr &= ~__ATTRIBUTES | __ALTCHARSET | __COLOR;
772 1.16 jdc if (OP != NULL && !strcmp(ME, OP))
773 1.16 jdc curscr->wattr &= ~__COLOR;
774 1.11 mrg }
775 1.5 cgd
776 1.5 cgd __mvcur(oy, ox, ny, nx, 1);
777 1.5 cgd }
778 1.5 cgd
779 1.5 cgd /*
780 1.5 cgd * Quickch() attempts to detect a pattern in the change of the window
781 1.7 cgd * in order to optimize the change, e.g., scroll n lines as opposed to
782 1.5 cgd * repainting the screen line by line.
783 1.5 cgd */
784 1.5 cgd
785 1.5 cgd static void
786 1.5 cgd quickch(win)
787 1.5 cgd WINDOW *win;
788 1.5 cgd {
789 1.5 cgd #define THRESH (int) win->maxy / 4
790 1.5 cgd
791 1.10 perry __LINE *clp, *tmp1, *tmp2;
792 1.11 mrg int bsize, curs, curw, starts, startw, i, j;
793 1.11 mrg int n, target, cur_period, bot, top, sc_region;
794 1.5 cgd __LDATA buf[1024];
795 1.11 mrg u_int blank_hash;
796 1.5 cgd
797 1.8 mikel #ifdef __GNUC__
798 1.11 mrg curs = curw = starts = startw = 0; /* XXX gcc -Wuninitialized */
799 1.8 mikel #endif
800 1.7 cgd /*
801 1.5 cgd * Find how many lines from the top of the screen are unchanged.
802 1.5 cgd */
803 1.7 cgd for (top = 0; top < win->maxy; top++)
804 1.5 cgd if (win->lines[top]->flags & __FORCEPAINT ||
805 1.7 cgd win->lines[top]->hash != curscr->lines[top]->hash
806 1.7 cgd || memcmp(win->lines[top]->line,
807 1.11 mrg curscr->lines[top]->line,
808 1.15 blymn (size_t) win->maxx * __LDATASIZE) != 0)
809 1.5 cgd break;
810 1.5 cgd else
811 1.5 cgd win->lines[top]->flags &= ~__ISDIRTY;
812 1.11 mrg /*
813 1.11 mrg * Find how many lines from bottom of screen are unchanged.
814 1.11 mrg */
815 1.5 cgd for (bot = win->maxy - 1; bot >= 0; bot--)
816 1.5 cgd if (win->lines[bot]->flags & __FORCEPAINT ||
817 1.7 cgd win->lines[bot]->hash != curscr->lines[bot]->hash
818 1.7 cgd || memcmp(win->lines[bot]->line,
819 1.11 mrg curscr->lines[bot]->line,
820 1.15 blymn (size_t) win->maxx * __LDATASIZE) != 0)
821 1.5 cgd break;
822 1.5 cgd else
823 1.5 cgd win->lines[bot]->flags &= ~__ISDIRTY;
824 1.5 cgd
825 1.5 cgd #ifdef NO_JERKINESS
826 1.5 cgd /*
827 1.5 cgd * If we have a bottom unchanged region return. Scrolling the
828 1.5 cgd * bottom region up and then back down causes a screen jitter.
829 1.5 cgd * This will increase the number of characters sent to the screen
830 1.5 cgd * but it looks better.
831 1.5 cgd */
832 1.5 cgd if (bot < win->maxy - 1)
833 1.5 cgd return;
834 1.11 mrg #endif /* NO_JERKINESS */
835 1.5 cgd
836 1.5 cgd /*
837 1.5 cgd * Search for the largest block of text not changed.
838 1.5 cgd * Invariants of the loop:
839 1.5 cgd * - Startw is the index of the beginning of the examined block in win.
840 1.11 mrg * - Starts is the index of the beginning of the examined block in
841 1.5 cgd * curscr.
842 1.5 cgd * - Curs is the index of one past the end of the exmined block in win.
843 1.7 cgd * - Curw is the index of one past the end of the exmined block in
844 1.5 cgd * curscr.
845 1.5 cgd * - bsize is the current size of the examined block.
846 1.11 mrg */
847 1.5 cgd for (bsize = bot - top; bsize >= THRESH; bsize--) {
848 1.5 cgd for (startw = top; startw <= bot - bsize; startw++)
849 1.7 cgd for (starts = top; starts <= bot - bsize;
850 1.11 mrg starts++) {
851 1.5 cgd for (curw = startw, curs = starts;
852 1.11 mrg curs < starts + bsize; curw++, curs++)
853 1.5 cgd if (win->lines[curw]->flags &
854 1.5 cgd __FORCEPAINT ||
855 1.5 cgd (win->lines[curw]->hash !=
856 1.11 mrg curscr->lines[curs]->hash ||
857 1.11 mrg memcmp(win->lines[curw]->line,
858 1.11 mrg curscr->lines[curs]->line,
859 1.15 blymn (size_t) win->maxx * __LDATASIZE) != 0))
860 1.5 cgd break;
861 1.5 cgd if (curs == starts + bsize)
862 1.5 cgd goto done;
863 1.5 cgd }
864 1.5 cgd }
865 1.11 mrg done:
866 1.5 cgd /* Did not find anything */
867 1.7 cgd if (bsize < THRESH)
868 1.5 cgd return;
869 1.5 cgd
870 1.5 cgd #ifdef DEBUG
871 1.7 cgd __CTRACE("quickch:bsize=%d,starts=%d,startw=%d,curw=%d,curs=%d,top=%d,bot=%d\n",
872 1.11 mrg bsize, starts, startw, curw, curs, top, bot);
873 1.5 cgd #endif
874 1.5 cgd
875 1.7 cgd /*
876 1.7 cgd * Make sure that there is no overlap between the bottom and top
877 1.5 cgd * regions and the middle scrolled block.
878 1.5 cgd */
879 1.5 cgd if (bot < curs)
880 1.5 cgd bot = curs - 1;
881 1.5 cgd if (top > starts)
882 1.5 cgd top = starts;
883 1.5 cgd
884 1.5 cgd n = startw - starts;
885 1.5 cgd
886 1.5 cgd #ifdef DEBUG
887 1.11 mrg __CTRACE("#####################################\n");
888 1.11 mrg for (i = 0; i < curscr->maxy; i++) {
889 1.11 mrg __CTRACE("C: %d:", i);
890 1.11 mrg __CTRACE(" 0x%x \n", curscr->lines[i]->hash);
891 1.11 mrg for (j = 0; j < curscr->maxx; j++)
892 1.11 mrg __CTRACE("%c", curscr->lines[i]->line[j].ch);
893 1.11 mrg __CTRACE("\n");
894 1.14 simonb __CTRACE(" attr:");
895 1.11 mrg for (j = 0; j < curscr->maxx; j++)
896 1.14 simonb __CTRACE(" %x", curscr->lines[i]->line[j].attr);
897 1.11 mrg __CTRACE("\n");
898 1.11 mrg __CTRACE("W: %d:", i);
899 1.11 mrg __CTRACE(" 0x%x \n", win->lines[i]->hash);
900 1.11 mrg __CTRACE(" 0x%x ", win->lines[i]->flags);
901 1.11 mrg for (j = 0; j < win->maxx; j++)
902 1.11 mrg __CTRACE("%c", win->lines[i]->line[j].ch);
903 1.11 mrg __CTRACE("\n");
904 1.14 simonb __CTRACE(" attr:");
905 1.11 mrg for (j = 0; j < win->maxx; j++)
906 1.14 simonb __CTRACE(" %x", win->lines[i]->line[j].attr);
907 1.11 mrg __CTRACE("\n");
908 1.11 mrg }
909 1.7 cgd #endif
910 1.7 cgd
911 1.5 cgd /* So we don't have to call __hash() each time */
912 1.5 cgd for (i = 0; i < win->maxx; i++) {
913 1.5 cgd buf[i].ch = ' ';
914 1.5 cgd buf[i].attr = 0;
915 1.5 cgd }
916 1.12 christos blank_hash = __hash((char *)(void *)buf,
917 1.12 christos (int) (win->maxx * __LDATASIZE));
918 1.5 cgd
919 1.5 cgd /*
920 1.5 cgd * Perform the rotation to maintain the consistency of curscr.
921 1.5 cgd * This is hairy since we are doing an *in place* rotation.
922 1.5 cgd * Invariants of the loop:
923 1.5 cgd * - I is the index of the current line.
924 1.5 cgd * - Target is the index of the target of line i.
925 1.5 cgd * - Tmp1 points to current line (i).
926 1.5 cgd * - Tmp2 and points to target line (target);
927 1.7 cgd * - Cur_period is the index of the end of the current period.
928 1.5 cgd * (see below).
929 1.5 cgd *
930 1.5 cgd * There are 2 major issues here that make this rotation non-trivial:
931 1.5 cgd * 1. Scrolling in a scrolling region bounded by the top
932 1.5 cgd * and bottom regions determined (whose size is sc_region).
933 1.7 cgd * 2. As a result of the use of the mod function, there may be a
934 1.5 cgd * period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
935 1.5 cgd * 0 to 2, which then causes all odd lines not to be rotated.
936 1.7 cgd * To remedy this, an index of the end ( = beginning) of the
937 1.7 cgd * current 'period' is kept, cur_period, and when it is reached,
938 1.7 cgd * the next period is started from cur_period + 1 which is
939 1.5 cgd * guaranteed not to have been reached since that would mean that
940 1.5 cgd * all records would have been reached. (think about it...).
941 1.7 cgd *
942 1.5 cgd * Lines in the rotation can have 3 attributes which are marked on the
943 1.5 cgd * line so that curscr is consistent with the visual screen.
944 1.5 cgd * 1. Not dirty -- lines inside the scrolled block, top region or
945 1.5 cgd * bottom region.
946 1.7 cgd * 2. Blank lines -- lines in the differential of the scrolling
947 1.7 cgd * region adjacent to top and bot regions
948 1.5 cgd * depending on scrolling direction.
949 1.5 cgd * 3. Dirty line -- all other lines are marked dirty.
950 1.5 cgd */
951 1.5 cgd sc_region = bot - top + 1;
952 1.5 cgd i = top;
953 1.5 cgd tmp1 = curscr->lines[top];
954 1.5 cgd cur_period = top;
955 1.5 cgd for (j = top; j <= bot; j++) {
956 1.5 cgd target = (i - top + n + sc_region) % sc_region + top;
957 1.5 cgd tmp2 = curscr->lines[target];
958 1.5 cgd curscr->lines[target] = tmp1;
959 1.5 cgd /* Mark block as clean and blank out scrolled lines. */
960 1.5 cgd clp = curscr->lines[target];
961 1.5 cgd #ifdef DEBUG
962 1.5 cgd __CTRACE("quickch: n=%d startw=%d curw=%d i = %d target=%d ",
963 1.11 mrg n, startw, curw, i, target);
964 1.5 cgd #endif
965 1.7 cgd if ((target >= startw && target < curw) || target < top
966 1.5 cgd || target > bot) {
967 1.5 cgd #ifdef DEBUG
968 1.5 cgd __CTRACE("-- notdirty");
969 1.5 cgd #endif
970 1.5 cgd win->lines[target]->flags &= ~__ISDIRTY;
971 1.11 mrg } else
972 1.11 mrg if ((n > 0 && target >= top && target < top + n) ||
973 1.11 mrg (n < 0 && target <= bot && target > bot + n)) {
974 1.11 mrg if (clp->hash != blank_hash || memcmp(clp->line,
975 1.15 blymn buf, (size_t) win->maxx * __LDATASIZE) !=0) {
976 1.11 mrg (void)memcpy(clp->line, buf,
977 1.15 blymn (size_t) win->maxx * __LDATASIZE);
978 1.11 mrg #ifdef DEBUG
979 1.11 mrg __CTRACE("-- blanked out: dirty");
980 1.11 mrg #endif
981 1.11 mrg clp->hash = blank_hash;
982 1.11 mrg __touchline(win, target, 0, (int) win->maxx - 1, 0);
983 1.11 mrg } else {
984 1.11 mrg __touchline(win, target, 0, (int) win->maxx - 1, 0);
985 1.5 cgd #ifdef DEBUG
986 1.11 mrg __CTRACE(" -- blank line already: dirty");
987 1.5 cgd #endif
988 1.11 mrg }
989 1.5 cgd } else {
990 1.5 cgd #ifdef DEBUG
991 1.11 mrg __CTRACE(" -- dirty");
992 1.5 cgd #endif
993 1.11 mrg __touchline(win, target, 0, (int) win->maxx - 1, 0);
994 1.5 cgd }
995 1.5 cgd #ifdef DEBUG
996 1.5 cgd __CTRACE("\n");
997 1.5 cgd #endif
998 1.5 cgd if (target == cur_period) {
999 1.5 cgd i = target + 1;
1000 1.5 cgd tmp1 = curscr->lines[i];
1001 1.5 cgd cur_period = i;
1002 1.5 cgd } else {
1003 1.5 cgd tmp1 = tmp2;
1004 1.5 cgd i = target;
1005 1.5 cgd }
1006 1.5 cgd }
1007 1.5 cgd #ifdef DEBUG
1008 1.11 mrg __CTRACE("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
1009 1.11 mrg for (i = 0; i < curscr->maxy; i++) {
1010 1.11 mrg __CTRACE("C: %d:", i);
1011 1.11 mrg for (j = 0; j < curscr->maxx; j++)
1012 1.11 mrg __CTRACE("%c", curscr->lines[i]->line[j].ch);
1013 1.11 mrg __CTRACE("\n");
1014 1.11 mrg __CTRACE("W: %d:", i);
1015 1.11 mrg for (j = 0; j < win->maxx; j++)
1016 1.11 mrg __CTRACE("%c", win->lines[i]->line[j].ch);
1017 1.11 mrg __CTRACE("\n");
1018 1.11 mrg }
1019 1.5 cgd #endif
1020 1.5 cgd if (n != 0) {
1021 1.5 cgd WINDOW *wp;
1022 1.5 cgd scrolln(win, starts, startw, curs, bot, top);
1023 1.5 cgd /*
1024 1.5 cgd * Need to repoint any subwindow lines to the rotated
1025 1.7 cgd * line structured.
1026 1.5 cgd */
1027 1.5 cgd for (wp = win->nextp; wp != win; wp = wp->nextp)
1028 1.5 cgd __set_subwin(win, wp);
1029 1.1 cgd }
1030 1.5 cgd }
1031 1.5 cgd
1032 1.5 cgd /*
1033 1.7 cgd * scrolln --
1034 1.7 cgd * Scroll n lines, where n is starts - startw.
1035 1.5 cgd */
1036 1.11 mrg static void /* ARGSUSED */
1037 1.5 cgd scrolln(win, starts, startw, curs, bot, top)
1038 1.5 cgd WINDOW *win;
1039 1.11 mrg int starts, startw, curs, bot, top;
1040 1.5 cgd {
1041 1.11 mrg int i, oy, ox, n;
1042 1.5 cgd
1043 1.5 cgd oy = curscr->cury;
1044 1.5 cgd ox = curscr->curx;
1045 1.5 cgd n = starts - startw;
1046 1.5 cgd
1047 1.7 cgd /*
1048 1.7 cgd * XXX
1049 1.7 cgd * The initial tests that set __noqch don't let us reach here unless
1050 1.7 cgd * we have either CS + HO + SF/sf/SR/sr, or AL + DL. SF/sf and SR/sr
1051 1.7 cgd * scrolling can only shift the entire scrolling region, not just a
1052 1.7 cgd * part of it, which means that the quickch() routine is going to be
1053 1.7 cgd * sadly disappointed in us if we don't have CS as well.
1054 1.7 cgd *
1055 1.7 cgd * If CS, HO and SF/sf are set, can use the scrolling region. Because
1056 1.7 cgd * the cursor position after CS is undefined, we need HO which gives us
1057 1.7 cgd * the ability to move to somewhere without knowledge of the current
1058 1.7 cgd * location of the cursor. Still call __mvcur() anyway, to update its
1059 1.7 cgd * idea of where the cursor is.
1060 1.7 cgd *
1061 1.7 cgd * When the scrolling region has been set, the cursor has to be at the
1062 1.7 cgd * last line of the region to make the scroll happen.
1063 1.7 cgd *
1064 1.7 cgd * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
1065 1.7 cgd * or al/dl, and, some terminals have AL/DL, sf/sr, and CS, but not
1066 1.7 cgd * SF/SR. So, if we're scrolling almost all of the screen, try and use
1067 1.7 cgd * AL/DL, otherwise use the scrolling region. The "almost all" is a
1068 1.7 cgd * shameless hack for vi.
1069 1.7 cgd */
1070 1.16 jdc /* XXX: check for background colour and bce here */
1071 1.5 cgd if (n > 0) {
1072 1.7 cgd if (CS != NULL && HO != NULL && (SF != NULL ||
1073 1.8 mikel ((AL == NULL || DL == NULL ||
1074 1.8 mikel top > 3 || bot + 3 < win->maxy) && sf != NULL))) {
1075 1.7 cgd tputs(__tscroll(CS, top, bot + 1), 0, __cputchar);
1076 1.7 cgd __mvcur(oy, ox, 0, 0, 1);
1077 1.7 cgd tputs(HO, 0, __cputchar);
1078 1.7 cgd __mvcur(0, 0, bot, 0, 1);
1079 1.7 cgd if (SF != NULL)
1080 1.7 cgd tputs(__tscroll(SF, n, 0), 0, __cputchar);
1081 1.7 cgd else
1082 1.7 cgd for (i = 0; i < n; i++)
1083 1.7 cgd tputs(sf, 0, __cputchar);
1084 1.11 mrg tputs(__tscroll(CS, 0, (int) win->maxy), 0, __cputchar);
1085 1.7 cgd __mvcur(bot, 0, 0, 0, 1);
1086 1.7 cgd tputs(HO, 0, __cputchar);
1087 1.7 cgd __mvcur(0, 0, oy, ox, 1);
1088 1.7 cgd return;
1089 1.7 cgd }
1090 1.7 cgd
1091 1.7 cgd /* Scroll up the block. */
1092 1.7 cgd if (SF != NULL && top == 0) {
1093 1.7 cgd __mvcur(oy, ox, bot, 0, 1);
1094 1.7 cgd tputs(__tscroll(SF, n, 0), 0, __cputchar);
1095 1.7 cgd } else
1096 1.11 mrg if (DL != NULL) {
1097 1.11 mrg __mvcur(oy, ox, top, 0, 1);
1098 1.11 mrg tputs(__tscroll(DL, n, 0), 0, __cputchar);
1099 1.11 mrg } else
1100 1.11 mrg if (dl != NULL) {
1101 1.11 mrg __mvcur(oy, ox, top, 0, 1);
1102 1.11 mrg for (i = 0; i < n; i++)
1103 1.11 mrg tputs(dl, 0, __cputchar);
1104 1.11 mrg } else
1105 1.11 mrg if (sf != NULL && top == 0) {
1106 1.11 mrg __mvcur(oy, ox, bot, 0, 1);
1107 1.11 mrg for (i = 0; i < n; i++)
1108 1.11 mrg tputs(sf, 0, __cputchar);
1109 1.11 mrg } else
1110 1.11 mrg abort();
1111 1.5 cgd
1112 1.7 cgd /* Push down the bottom region. */
1113 1.5 cgd __mvcur(top, 0, bot - n + 1, 0, 1);
1114 1.7 cgd if (AL != NULL)
1115 1.7 cgd tputs(__tscroll(AL, n, 0), 0, __cputchar);
1116 1.5 cgd else
1117 1.11 mrg if (al != NULL)
1118 1.11 mrg for (i = 0; i < n; i++)
1119 1.11 mrg tputs(al, 0, __cputchar);
1120 1.11 mrg else
1121 1.11 mrg abort();
1122 1.5 cgd __mvcur(bot - n + 1, 0, oy, ox, 1);
1123 1.5 cgd } else {
1124 1.7 cgd /*
1125 1.7 cgd * !!!
1126 1.7 cgd * n < 0
1127 1.7 cgd *
1128 1.7 cgd * If CS, HO and SR/sr are set, can use the scrolling region.
1129 1.7 cgd * See the above comments for details.
1130 1.7 cgd */
1131 1.7 cgd if (CS != NULL && HO != NULL && (SR != NULL ||
1132 1.8 mikel ((AL == NULL || DL == NULL ||
1133 1.8 mikel top > 3 || bot + 3 < win->maxy) && sr != NULL))) {
1134 1.7 cgd tputs(__tscroll(CS, top, bot + 1), 0, __cputchar);
1135 1.7 cgd __mvcur(oy, ox, 0, 0, 1);
1136 1.7 cgd tputs(HO, 0, __cputchar);
1137 1.7 cgd __mvcur(0, 0, top, 0, 1);
1138 1.7 cgd
1139 1.7 cgd if (SR != NULL)
1140 1.7 cgd tputs(__tscroll(SR, -n, 0), 0, __cputchar);
1141 1.7 cgd else
1142 1.7 cgd for (i = n; i < 0; i++)
1143 1.7 cgd tputs(sr, 0, __cputchar);
1144 1.11 mrg tputs(__tscroll(CS, 0, (int) win->maxy), 0, __cputchar);
1145 1.7 cgd __mvcur(top, 0, 0, 0, 1);
1146 1.7 cgd tputs(HO, 0, __cputchar);
1147 1.7 cgd __mvcur(0, 0, oy, ox, 1);
1148 1.7 cgd return;
1149 1.7 cgd }
1150 1.7 cgd
1151 1.7 cgd /* Preserve the bottom lines. */
1152 1.7 cgd __mvcur(oy, ox, bot + n + 1, 0, 1);
1153 1.7 cgd if (SR != NULL && bot == win->maxy)
1154 1.7 cgd tputs(__tscroll(SR, -n, 0), 0, __cputchar);
1155 1.5 cgd else
1156 1.11 mrg if (DL != NULL)
1157 1.11 mrg tputs(__tscroll(DL, -n, 0), 0, __cputchar);
1158 1.11 mrg else
1159 1.11 mrg if (dl != NULL)
1160 1.11 mrg for (i = n; i < 0; i++)
1161 1.11 mrg tputs(dl, 0, __cputchar);
1162 1.11 mrg else
1163 1.11 mrg if (sr != NULL && bot == win->maxy)
1164 1.11 mrg for (i = n; i < 0; i++)
1165 1.11 mrg tputs(sr, 0, __cputchar);
1166 1.11 mrg else
1167 1.11 mrg abort();
1168 1.7 cgd
1169 1.7 cgd /* Scroll the block down. */
1170 1.5 cgd __mvcur(bot + n + 1, 0, top, 0, 1);
1171 1.7 cgd if (AL != NULL)
1172 1.7 cgd tputs(__tscroll(AL, -n, 0), 0, __cputchar);
1173 1.5 cgd else
1174 1.11 mrg if (al != NULL)
1175 1.11 mrg for (i = n; i < 0; i++)
1176 1.11 mrg tputs(al, 0, __cputchar);
1177 1.11 mrg else
1178 1.11 mrg abort();
1179 1.5 cgd __mvcur(top, 0, oy, ox, 1);
1180 1.7 cgd }
1181 1.1 cgd }
1182