refresh.c revision 1.7 1 1.1 cgd /*
2 1.7 cgd * Copyright (c) 1981, 1993, 1994
3 1.5 cgd * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.7 cgd static char sccsid[] = "@(#)refresh.c 8.7 (Berkeley) 8/13/94";
36 1.1 cgd #endif /* not lint */
37 1.1 cgd
38 1.4 mycroft #include <string.h>
39 1.1 cgd
40 1.7 cgd #include "curses.h"
41 1.7 cgd
42 1.4 mycroft static int curwin;
43 1.4 mycroft static short ly, lx;
44 1.1 cgd
45 1.4 mycroft static void domvcur __P((int, int, int, int));
46 1.4 mycroft static int makech __P((WINDOW *, int));
47 1.7 cgd static void quickch __P((WINDOW *));
48 1.5 cgd static void scrolln __P((WINDOW *, int, int, int, int, int));
49 1.1 cgd
50 1.4 mycroft /*
51 1.4 mycroft * wrefresh --
52 1.4 mycroft * Make the current screen look like "win" over the area coverd by
53 1.4 mycroft * win.
54 1.4 mycroft */
55 1.4 mycroft int
56 1.1 cgd wrefresh(win)
57 1.4 mycroft register WINDOW *win;
58 1.1 cgd {
59 1.5 cgd register __LINE *wlp;
60 1.4 mycroft register int retval;
61 1.4 mycroft register short wy;
62 1.5 cgd int dnum;
63 1.7 cgd
64 1.4 mycroft /* Initialize loop parameters. */
65 1.5 cgd ly = curscr->cury;
66 1.5 cgd lx = curscr->curx;
67 1.1 cgd wy = 0;
68 1.1 cgd curwin = (win == curscr);
69 1.1 cgd
70 1.5 cgd if (!curwin)
71 1.5 cgd for (wy = 0; wy < win->maxy; wy++) {
72 1.5 cgd wlp = win->lines[wy];
73 1.5 cgd if (wlp->flags & __ISDIRTY)
74 1.7 cgd wlp->hash = __hash((char *)wlp->line,
75 1.7 cgd win->maxx * __LDATASIZE);
76 1.5 cgd }
77 1.5 cgd
78 1.5 cgd if (win->flags & __CLEAROK || curscr->flags & __CLEAROK || curwin) {
79 1.5 cgd if ((win->flags & __FULLWIN) || curscr->flags & __CLEAROK) {
80 1.4 mycroft tputs(CL, 0, __cputchar);
81 1.1 cgd ly = 0;
82 1.1 cgd lx = 0;
83 1.1 cgd if (!curwin) {
84 1.5 cgd curscr->flags &= ~__CLEAROK;
85 1.5 cgd curscr->cury = 0;
86 1.5 cgd curscr->curx = 0;
87 1.1 cgd werase(curscr);
88 1.1 cgd }
89 1.5 cgd __touchwin(win);
90 1.1 cgd }
91 1.5 cgd win->flags &= ~__CLEAROK;
92 1.1 cgd }
93 1.1 cgd if (!CA) {
94 1.5 cgd if (win->curx != 0)
95 1.4 mycroft putchar('\n');
96 1.1 cgd if (!curwin)
97 1.1 cgd werase(curscr);
98 1.1 cgd }
99 1.4 mycroft #ifdef DEBUG
100 1.5 cgd __CTRACE("wrefresh: (%0.2o): curwin = %d\n", win, curwin);
101 1.5 cgd __CTRACE("wrefresh: \tfirstch\tlastch\n");
102 1.5 cgd #endif
103 1.5 cgd
104 1.5 cgd #ifndef NOQCH
105 1.5 cgd if ((win->flags & __FULLWIN) && !curwin) {
106 1.5 cgd /*
107 1.5 cgd * Invoke quickch() only if more than a quarter of the lines
108 1.5 cgd * in the window are dirty.
109 1.5 cgd */
110 1.5 cgd for (wy = 0, dnum = 0; wy < win->maxy; wy++)
111 1.5 cgd if (win->lines[wy]->flags & (__ISDIRTY | __FORCEPAINT))
112 1.5 cgd dnum++;
113 1.5 cgd if (!__noqch && dnum > (int) win->maxy / 4)
114 1.5 cgd quickch(win);
115 1.5 cgd }
116 1.4 mycroft #endif
117 1.5 cgd
118 1.5 cgd #ifdef DEBUG
119 1.5 cgd { int i, j;
120 1.5 cgd __CTRACE("#####################################\n");
121 1.5 cgd for (i = 0; i < curscr->maxy; i++) {
122 1.5 cgd __CTRACE("C: %d:", i);
123 1.5 cgd __CTRACE(" 0x%x \n", curscr->lines[i]->hash);
124 1.7 cgd for (j = 0; j < curscr->maxx; j++)
125 1.7 cgd __CTRACE("%c",
126 1.5 cgd curscr->lines[i]->line[j].ch);
127 1.5 cgd __CTRACE("\n");
128 1.7 cgd for (j = 0; j < curscr->maxx; j++)
129 1.7 cgd __CTRACE("%x",
130 1.5 cgd curscr->lines[i]->line[j].attr);
131 1.5 cgd __CTRACE("\n");
132 1.5 cgd __CTRACE("W: %d:", i);
133 1.5 cgd __CTRACE(" 0x%x \n", win->lines[i]->hash);
134 1.5 cgd __CTRACE(" 0x%x ", win->lines[i]->flags);
135 1.7 cgd for (j = 0; j < win->maxx; j++)
136 1.7 cgd __CTRACE("%c",
137 1.5 cgd win->lines[i]->line[j].ch);
138 1.5 cgd __CTRACE("\n");
139 1.7 cgd for (j = 0; j < win->maxx; j++)
140 1.7 cgd __CTRACE("%x",
141 1.5 cgd win->lines[i]->line[j].attr);
142 1.5 cgd __CTRACE("\n");
143 1.5 cgd }
144 1.5 cgd }
145 1.5 cgd #endif /* DEBUG */
146 1.5 cgd
147 1.5 cgd for (wy = 0; wy < win->maxy; wy++) {
148 1.4 mycroft #ifdef DEBUG
149 1.5 cgd __CTRACE("%d\t%d\t%d\n",
150 1.5 cgd wy, *win->lines[wy]->firstchp, *win->lines[wy]->lastchp);
151 1.4 mycroft #endif
152 1.5 cgd if (!curwin)
153 1.5 cgd curscr->lines[wy]->hash = win->lines[wy]->hash;
154 1.5 cgd if (win->lines[wy]->flags & (__ISDIRTY | __FORCEPAINT)) {
155 1.1 cgd if (makech(win, wy) == ERR)
156 1.4 mycroft return (ERR);
157 1.1 cgd else {
158 1.5 cgd if (*win->lines[wy]->firstchp >= win->ch_off)
159 1.5 cgd *win->lines[wy]->firstchp = win->maxx +
160 1.5 cgd win->ch_off;
161 1.5 cgd if (*win->lines[wy]->lastchp < win->maxx +
162 1.5 cgd win->ch_off)
163 1.5 cgd *win->lines[wy]->lastchp = win->ch_off;
164 1.7 cgd if (*win->lines[wy]->lastchp <
165 1.5 cgd *win->lines[wy]->firstchp) {
166 1.5 cgd #ifdef DEBUG
167 1.5 cgd __CTRACE("wrefresh: line %d notdirty \n", wy);
168 1.5 cgd #endif
169 1.5 cgd win->lines[wy]->flags &= ~__ISDIRTY;
170 1.5 cgd }
171 1.1 cgd }
172 1.5 cgd
173 1.5 cgd }
174 1.4 mycroft #ifdef DEBUG
175 1.7 cgd __CTRACE("\t%d\t%d\n", *win->lines[wy]->firstchp,
176 1.5 cgd *win->lines[wy]->lastchp);
177 1.4 mycroft #endif
178 1.1 cgd }
179 1.7 cgd
180 1.5 cgd #ifdef DEBUG
181 1.5 cgd __CTRACE("refresh: ly=%d, lx=%d\n", ly, lx);
182 1.5 cgd #endif
183 1.1 cgd
184 1.1 cgd if (win == curscr)
185 1.5 cgd domvcur(ly, lx, win->cury, win->curx);
186 1.1 cgd else {
187 1.5 cgd if (win->flags & __LEAVEOK) {
188 1.5 cgd curscr->cury = ly;
189 1.5 cgd curscr->curx = lx;
190 1.5 cgd ly -= win->begy;
191 1.5 cgd lx -= win->begx;
192 1.5 cgd if (ly >= 0 && ly < win->maxy && lx >= 0 &&
193 1.5 cgd lx < win->maxx) {
194 1.5 cgd win->cury = ly;
195 1.5 cgd win->curx = lx;
196 1.4 mycroft } else
197 1.5 cgd win->cury = win->curx = 0;
198 1.4 mycroft } else {
199 1.5 cgd domvcur(ly, lx, win->cury + win->begy,
200 1.5 cgd win->curx + win->begx);
201 1.5 cgd curscr->cury = win->cury + win->begy;
202 1.5 cgd curscr->curx = win->curx + win->begx;
203 1.1 cgd }
204 1.1 cgd }
205 1.1 cgd retval = OK;
206 1.4 mycroft
207 1.4 mycroft (void)fflush(stdout);
208 1.4 mycroft return (retval);
209 1.1 cgd }
210 1.1 cgd
211 1.1 cgd /*
212 1.4 mycroft * makech --
213 1.4 mycroft * Make a change on the screen.
214 1.1 cgd */
215 1.4 mycroft static int
216 1.1 cgd makech(win, wy)
217 1.4 mycroft register WINDOW *win;
218 1.4 mycroft int wy;
219 1.1 cgd {
220 1.5 cgd static __LDATA blank = {' ', 0};
221 1.7 cgd __LDATA *nsp, *csp, *cp, *cep;
222 1.5 cgd u_int force;
223 1.7 cgd int clsp, nlsp; /* Last space in lines. */
224 1.7 cgd int lch, wx, y;
225 1.5 cgd char *ce;
226 1.5 cgd
227 1.5 cgd /* Is the cursor still on the end of the last line? */
228 1.5 cgd if (wy > 0 && win->lines[wy - 1]->flags & __ISPASTEOL) {
229 1.5 cgd domvcur(ly, lx, ly + 1, 0);
230 1.5 cgd ly++;
231 1.5 cgd lx = 0;
232 1.5 cgd }
233 1.5 cgd wx = *win->lines[wy]->firstchp - win->ch_off;
234 1.5 cgd if (wx < 0)
235 1.5 cgd wx = 0;
236 1.5 cgd else if (wx >= win->maxx)
237 1.4 mycroft return (OK);
238 1.5 cgd lch = *win->lines[wy]->lastchp - win->ch_off;
239 1.1 cgd if (lch < 0)
240 1.4 mycroft return (OK);
241 1.5 cgd else if (lch >= (int) win->maxx)
242 1.5 cgd lch = win->maxx - 1;
243 1.5 cgd y = wy + win->begy;
244 1.1 cgd
245 1.1 cgd if (curwin)
246 1.5 cgd csp = ␣
247 1.1 cgd else
248 1.5 cgd csp = &curscr->lines[wy + win->begy]->line[wx + win->begx];
249 1.1 cgd
250 1.5 cgd nsp = &win->lines[wy]->line[wx];
251 1.5 cgd force = win->lines[wy]->flags & __FORCEPAINT;
252 1.5 cgd win->lines[wy]->flags &= ~__FORCEPAINT;
253 1.1 cgd if (CE && !curwin) {
254 1.7 cgd for (cp = &win->lines[wy]->line[win->maxx - 1];
255 1.5 cgd cp->ch == ' ' && cp->attr == 0; cp--)
256 1.5 cgd if (cp <= win->lines[wy]->line)
257 1.1 cgd break;
258 1.5 cgd nlsp = cp - win->lines[wy]->line;
259 1.1 cgd }
260 1.1 cgd if (!curwin)
261 1.4 mycroft ce = CE;
262 1.1 cgd else
263 1.4 mycroft ce = NULL;
264 1.1 cgd
265 1.5 cgd if (force) {
266 1.5 cgd if (CM)
267 1.5 cgd tputs(tgoto(CM, lx, ly), 0, __cputchar);
268 1.5 cgd else {
269 1.5 cgd tputs(HO, 0, __cputchar);
270 1.5 cgd __mvcur(0, 0, ly, lx, 1);
271 1.5 cgd }
272 1.5 cgd }
273 1.7 cgd
274 1.1 cgd while (wx <= lch) {
275 1.5 cgd if (!force && memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
276 1.4 mycroft if (wx <= lch) {
277 1.5 cgd while (wx <= lch &&
278 1.7 cgd memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
279 1.7 cgd nsp++;
280 1.7 cgd if (!curwin)
281 1.7 cgd ++csp;
282 1.7 cgd ++wx;
283 1.7 cgd }
284 1.4 mycroft continue;
285 1.4 mycroft }
286 1.4 mycroft break;
287 1.4 mycroft }
288 1.5 cgd domvcur(ly, lx, y, wx + win->begx);
289 1.5 cgd
290 1.4 mycroft #ifdef DEBUG
291 1.7 cgd __CTRACE("makech: 1: wx = %d, ly= %d, lx = %d, newy = %d, newx = %d, force =%d\n",
292 1.5 cgd wx, ly, lx, y, wx + win->begx, force);
293 1.4 mycroft #endif
294 1.4 mycroft ly = y;
295 1.5 cgd lx = wx + win->begx;
296 1.7 cgd while ((force || memcmp(nsp, csp, sizeof(__LDATA)) != 0)
297 1.5 cgd && wx <= lch) {
298 1.5 cgd
299 1.7 cgd if (ce != NULL &&
300 1.7 cgd win->maxx + win->begx == curscr->maxx &&
301 1.7 cgd wx >= nlsp && nsp->ch == ' ' && nsp->attr == 0) {
302 1.4 mycroft /* Check for clear to end-of-line. */
303 1.5 cgd cep = &curscr->lines[wy]->line[win->maxx - 1];
304 1.5 cgd while (cep->ch == ' ' && cep->attr == 0)
305 1.5 cgd if (cep-- <= csp)
306 1.4 mycroft break;
307 1.7 cgd clsp = cep - curscr->lines[wy]->line -
308 1.5 cgd win->begx * __LDATASIZE;
309 1.4 mycroft #ifdef DEBUG
310 1.5 cgd __CTRACE("makech: clsp = %d, nlsp = %d\n", clsp, nlsp);
311 1.4 mycroft #endif
312 1.7 cgd if ((clsp - nlsp >= strlen(CE)
313 1.5 cgd && clsp < win->maxx * __LDATASIZE) ||
314 1.5 cgd wy == win->maxy - 1) {
315 1.7 cgd if (curscr->flags & __WSTANDOUT) {
316 1.7 cgd tputs(SE, 0, __cputchar);
317 1.7 cgd curscr->flags &= ~__WSTANDOUT;
318 1.7 cgd }
319 1.4 mycroft tputs(CE, 0, __cputchar);
320 1.5 cgd lx = wx + win->begx;
321 1.5 cgd while (wx++ <= clsp) {
322 1.5 cgd csp->ch = ' ';
323 1.5 cgd csp->attr = 0;
324 1.5 cgd csp++;
325 1.5 cgd }
326 1.4 mycroft return (OK);
327 1.1 cgd }
328 1.4 mycroft ce = NULL;
329 1.4 mycroft }
330 1.4 mycroft
331 1.7 cgd /*
332 1.7 cgd * Enter/exit standout mode as appropriate.
333 1.7 cgd * XXX
334 1.7 cgd * Should use UC if SO/SE not available.
335 1.7 cgd */
336 1.7 cgd if (nsp->attr & __STANDOUT) {
337 1.7 cgd if (!(curscr->flags & __WSTANDOUT) &&
338 1.7 cgd SO != NULL && SE != NULL) {
339 1.4 mycroft tputs(SO, 0, __cputchar);
340 1.5 cgd curscr->flags |= __WSTANDOUT;
341 1.7 cgd }
342 1.7 cgd } else
343 1.7 cgd if (curscr->flags & __WSTANDOUT &&
344 1.7 cgd SE != NULL) {
345 1.4 mycroft tputs(SE, 0, __cputchar);
346 1.5 cgd curscr->flags &= ~__WSTANDOUT;
347 1.1 cgd }
348 1.4 mycroft
349 1.4 mycroft wx++;
350 1.5 cgd if (wx >= win->maxx && wy == win->maxy - 1 && !curwin)
351 1.5 cgd if (win->flags & __SCROLLOK) {
352 1.5 cgd if (curscr->flags & __WSTANDOUT
353 1.5 cgd && win->flags & __ENDLINE)
354 1.4 mycroft if (!MS) {
355 1.4 mycroft tputs(SE, 0,
356 1.4 mycroft __cputchar);
357 1.5 cgd curscr->flags &=
358 1.5 cgd ~__WSTANDOUT;
359 1.4 mycroft }
360 1.5 cgd if (!(win->flags & __SCROLLWIN)) {
361 1.5 cgd if (!curwin) {
362 1.5 cgd csp->attr = nsp->attr;
363 1.5 cgd putchar(csp->ch = nsp->ch);
364 1.5 cgd } else
365 1.5 cgd putchar(nsp->ch);
366 1.5 cgd }
367 1.5 cgd if (wx + win->begx < curscr->maxx) {
368 1.7 cgd domvcur(ly, wx + win->begx,
369 1.5 cgd win->begy + win->maxy - 1,
370 1.5 cgd win->begx + win->maxx - 1);
371 1.5 cgd }
372 1.5 cgd ly = win->begy + win->maxy - 1;
373 1.5 cgd lx = win->begx + win->maxx - 1;
374 1.4 mycroft return (OK);
375 1.7 cgd }
376 1.7 cgd if (wx < win->maxx || wy < win->maxy - 1 ||
377 1.5 cgd !(win->flags & __SCROLLWIN)) {
378 1.5 cgd if (!curwin) {
379 1.5 cgd csp->attr = nsp->attr;
380 1.5 cgd putchar(csp->ch = nsp->ch);
381 1.5 cgd csp++;
382 1.7 cgd } else
383 1.5 cgd putchar(nsp->ch);
384 1.5 cgd }
385 1.4 mycroft #ifdef DEBUG
386 1.5 cgd __CTRACE("makech: putchar(%c)\n", nsp->ch & 0177);
387 1.4 mycroft #endif
388 1.5 cgd if (UC && (nsp->attr & __STANDOUT)) {
389 1.4 mycroft putchar('\b');
390 1.4 mycroft tputs(UC, 0, __cputchar);
391 1.1 cgd }
392 1.4 mycroft nsp++;
393 1.4 mycroft #ifdef DEBUG
394 1.5 cgd __CTRACE("makech: 2: wx = %d, lx = %d\n", wx, lx);
395 1.4 mycroft #endif
396 1.5 cgd }
397 1.5 cgd if (lx == wx + win->begx) /* If no change. */
398 1.4 mycroft break;
399 1.5 cgd lx = wx + win->begx;
400 1.5 cgd if (lx >= COLS && AM)
401 1.5 cgd lx = COLS - 1;
402 1.5 cgd else if (wx >= win->maxx) {
403 1.5 cgd domvcur(ly, lx, ly, win->maxx + win->begx - 1);
404 1.5 cgd lx = win->maxx + win->begx - 1;
405 1.1 cgd }
406 1.5 cgd
407 1.4 mycroft #ifdef DEBUG
408 1.5 cgd __CTRACE("makech: 3: wx = %d, lx = %d\n", wx, lx);
409 1.4 mycroft #endif
410 1.1 cgd }
411 1.7 cgd
412 1.7 cgd /* Don't leave the screen in standout mode. */
413 1.7 cgd if (curscr->flags & __WSTANDOUT) {
414 1.7 cgd tputs(SE, 0, __cputchar);
415 1.7 cgd curscr->flags &= ~__WSTANDOUT;
416 1.7 cgd }
417 1.4 mycroft return (OK);
418 1.1 cgd }
419 1.1 cgd
420 1.1 cgd /*
421 1.4 mycroft * domvcur --
422 1.4 mycroft * Do a mvcur, leaving standout mode if necessary.
423 1.1 cgd */
424 1.4 mycroft static void
425 1.1 cgd domvcur(oy, ox, ny, nx)
426 1.4 mycroft int oy, ox, ny, nx;
427 1.4 mycroft {
428 1.5 cgd if (curscr->flags & __WSTANDOUT && !MS) {
429 1.4 mycroft tputs(SE, 0, __cputchar);
430 1.5 cgd curscr->flags &= ~__WSTANDOUT;
431 1.5 cgd }
432 1.5 cgd
433 1.5 cgd __mvcur(oy, ox, ny, nx, 1);
434 1.5 cgd }
435 1.5 cgd
436 1.5 cgd /*
437 1.5 cgd * Quickch() attempts to detect a pattern in the change of the window
438 1.7 cgd * in order to optimize the change, e.g., scroll n lines as opposed to
439 1.5 cgd * repainting the screen line by line.
440 1.5 cgd */
441 1.5 cgd
442 1.5 cgd static void
443 1.5 cgd quickch(win)
444 1.5 cgd WINDOW *win;
445 1.5 cgd {
446 1.5 cgd #define THRESH (int) win->maxy / 4
447 1.5 cgd
448 1.5 cgd register __LINE *clp, *tmp1, *tmp2;
449 1.5 cgd register int bsize, curs, curw, starts, startw, i, j;
450 1.5 cgd int n, target, cur_period, bot, top, sc_region;
451 1.5 cgd __LDATA buf[1024];
452 1.5 cgd u_int blank_hash;
453 1.5 cgd
454 1.7 cgd /*
455 1.5 cgd * Find how many lines from the top of the screen are unchanged.
456 1.5 cgd */
457 1.7 cgd for (top = 0; top < win->maxy; top++)
458 1.5 cgd if (win->lines[top]->flags & __FORCEPAINT ||
459 1.7 cgd win->lines[top]->hash != curscr->lines[top]->hash
460 1.7 cgd || memcmp(win->lines[top]->line,
461 1.7 cgd curscr->lines[top]->line,
462 1.5 cgd win->maxx * __LDATASIZE) != 0)
463 1.5 cgd break;
464 1.5 cgd else
465 1.5 cgd win->lines[top]->flags &= ~__ISDIRTY;
466 1.5 cgd /*
467 1.7 cgd * Find how many lines from bottom of screen are unchanged.
468 1.5 cgd */
469 1.5 cgd for (bot = win->maxy - 1; bot >= 0; bot--)
470 1.5 cgd if (win->lines[bot]->flags & __FORCEPAINT ||
471 1.7 cgd win->lines[bot]->hash != curscr->lines[bot]->hash
472 1.7 cgd || memcmp(win->lines[bot]->line,
473 1.7 cgd curscr->lines[bot]->line,
474 1.5 cgd win->maxx * __LDATASIZE) != 0)
475 1.5 cgd break;
476 1.5 cgd else
477 1.5 cgd win->lines[bot]->flags &= ~__ISDIRTY;
478 1.5 cgd
479 1.5 cgd #ifdef NO_JERKINESS
480 1.5 cgd /*
481 1.5 cgd * If we have a bottom unchanged region return. Scrolling the
482 1.5 cgd * bottom region up and then back down causes a screen jitter.
483 1.5 cgd * This will increase the number of characters sent to the screen
484 1.5 cgd * but it looks better.
485 1.5 cgd */
486 1.5 cgd if (bot < win->maxy - 1)
487 1.5 cgd return;
488 1.5 cgd #endif /* NO_JERKINESS */
489 1.5 cgd
490 1.5 cgd /*
491 1.5 cgd * Search for the largest block of text not changed.
492 1.5 cgd * Invariants of the loop:
493 1.5 cgd * - Startw is the index of the beginning of the examined block in win.
494 1.7 cgd * - Starts is the index of the beginning of the examined block in
495 1.5 cgd * curscr.
496 1.5 cgd * - Curs is the index of one past the end of the exmined block in win.
497 1.7 cgd * - Curw is the index of one past the end of the exmined block in
498 1.5 cgd * curscr.
499 1.5 cgd * - bsize is the current size of the examined block.
500 1.5 cgd */
501 1.5 cgd for (bsize = bot - top; bsize >= THRESH; bsize--) {
502 1.5 cgd for (startw = top; startw <= bot - bsize; startw++)
503 1.7 cgd for (starts = top; starts <= bot - bsize;
504 1.5 cgd starts++) {
505 1.5 cgd for (curw = startw, curs = starts;
506 1.5 cgd curs < starts + bsize; curw++, curs++)
507 1.5 cgd if (win->lines[curw]->flags &
508 1.5 cgd __FORCEPAINT ||
509 1.5 cgd (win->lines[curw]->hash !=
510 1.5 cgd curscr->lines[curs]->hash ||
511 1.7 cgd memcmp(win->lines[curw]->line,
512 1.7 cgd curscr->lines[curs]->line,
513 1.5 cgd win->maxx * __LDATASIZE) != 0))
514 1.5 cgd break;
515 1.5 cgd if (curs == starts + bsize)
516 1.5 cgd goto done;
517 1.5 cgd }
518 1.5 cgd }
519 1.5 cgd done:
520 1.5 cgd /* Did not find anything */
521 1.7 cgd if (bsize < THRESH)
522 1.5 cgd return;
523 1.5 cgd
524 1.5 cgd #ifdef DEBUG
525 1.7 cgd __CTRACE("quickch:bsize=%d,starts=%d,startw=%d,curw=%d,curs=%d,top=%d,bot=%d\n",
526 1.5 cgd bsize, starts, startw, curw, curs, top, bot);
527 1.5 cgd #endif
528 1.5 cgd
529 1.7 cgd /*
530 1.7 cgd * Make sure that there is no overlap between the bottom and top
531 1.5 cgd * regions and the middle scrolled block.
532 1.5 cgd */
533 1.5 cgd if (bot < curs)
534 1.5 cgd bot = curs - 1;
535 1.5 cgd if (top > starts)
536 1.5 cgd top = starts;
537 1.5 cgd
538 1.5 cgd n = startw - starts;
539 1.5 cgd
540 1.5 cgd #ifdef DEBUG
541 1.5 cgd __CTRACE("#####################################\n");
542 1.5 cgd for (i = 0; i < curscr->maxy; i++) {
543 1.5 cgd __CTRACE("C: %d:", i);
544 1.5 cgd __CTRACE(" 0x%x \n", curscr->lines[i]->hash);
545 1.7 cgd for (j = 0; j < curscr->maxx; j++)
546 1.7 cgd __CTRACE("%c",
547 1.5 cgd curscr->lines[i]->line[j].ch);
548 1.5 cgd __CTRACE("\n");
549 1.7 cgd for (j = 0; j < curscr->maxx; j++)
550 1.7 cgd __CTRACE("%x",
551 1.5 cgd curscr->lines[i]->line[j].attr);
552 1.5 cgd __CTRACE("\n");
553 1.5 cgd __CTRACE("W: %d:", i);
554 1.5 cgd __CTRACE(" 0x%x \n", win->lines[i]->hash);
555 1.5 cgd __CTRACE(" 0x%x ", win->lines[i]->flags);
556 1.7 cgd for (j = 0; j < win->maxx; j++)
557 1.7 cgd __CTRACE("%c",
558 1.5 cgd win->lines[i]->line[j].ch);
559 1.5 cgd __CTRACE("\n");
560 1.7 cgd for (j = 0; j < win->maxx; j++)
561 1.7 cgd __CTRACE("%x",
562 1.5 cgd win->lines[i]->line[j].attr);
563 1.5 cgd __CTRACE("\n");
564 1.5 cgd }
565 1.7 cgd #endif
566 1.7 cgd
567 1.5 cgd /* So we don't have to call __hash() each time */
568 1.5 cgd for (i = 0; i < win->maxx; i++) {
569 1.5 cgd buf[i].ch = ' ';
570 1.5 cgd buf[i].attr = 0;
571 1.5 cgd }
572 1.5 cgd blank_hash = __hash((char *) buf, win->maxx * __LDATASIZE);
573 1.5 cgd
574 1.5 cgd /*
575 1.5 cgd * Perform the rotation to maintain the consistency of curscr.
576 1.5 cgd * This is hairy since we are doing an *in place* rotation.
577 1.5 cgd * Invariants of the loop:
578 1.5 cgd * - I is the index of the current line.
579 1.5 cgd * - Target is the index of the target of line i.
580 1.5 cgd * - Tmp1 points to current line (i).
581 1.5 cgd * - Tmp2 and points to target line (target);
582 1.7 cgd * - Cur_period is the index of the end of the current period.
583 1.5 cgd * (see below).
584 1.5 cgd *
585 1.5 cgd * There are 2 major issues here that make this rotation non-trivial:
586 1.5 cgd * 1. Scrolling in a scrolling region bounded by the top
587 1.5 cgd * and bottom regions determined (whose size is sc_region).
588 1.7 cgd * 2. As a result of the use of the mod function, there may be a
589 1.5 cgd * period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
590 1.5 cgd * 0 to 2, which then causes all odd lines not to be rotated.
591 1.7 cgd * To remedy this, an index of the end ( = beginning) of the
592 1.7 cgd * current 'period' is kept, cur_period, and when it is reached,
593 1.7 cgd * the next period is started from cur_period + 1 which is
594 1.5 cgd * guaranteed not to have been reached since that would mean that
595 1.5 cgd * all records would have been reached. (think about it...).
596 1.7 cgd *
597 1.5 cgd * Lines in the rotation can have 3 attributes which are marked on the
598 1.5 cgd * line so that curscr is consistent with the visual screen.
599 1.5 cgd * 1. Not dirty -- lines inside the scrolled block, top region or
600 1.5 cgd * bottom region.
601 1.7 cgd * 2. Blank lines -- lines in the differential of the scrolling
602 1.7 cgd * region adjacent to top and bot regions
603 1.5 cgd * depending on scrolling direction.
604 1.5 cgd * 3. Dirty line -- all other lines are marked dirty.
605 1.5 cgd */
606 1.5 cgd sc_region = bot - top + 1;
607 1.5 cgd i = top;
608 1.5 cgd tmp1 = curscr->lines[top];
609 1.5 cgd cur_period = top;
610 1.5 cgd for (j = top; j <= bot; j++) {
611 1.5 cgd target = (i - top + n + sc_region) % sc_region + top;
612 1.5 cgd tmp2 = curscr->lines[target];
613 1.5 cgd curscr->lines[target] = tmp1;
614 1.5 cgd /* Mark block as clean and blank out scrolled lines. */
615 1.5 cgd clp = curscr->lines[target];
616 1.5 cgd #ifdef DEBUG
617 1.5 cgd __CTRACE("quickch: n=%d startw=%d curw=%d i = %d target=%d ",
618 1.5 cgd n, startw, curw, i, target);
619 1.5 cgd #endif
620 1.7 cgd if ((target >= startw && target < curw) || target < top
621 1.5 cgd || target > bot) {
622 1.5 cgd #ifdef DEBUG
623 1.5 cgd __CTRACE("-- notdirty");
624 1.5 cgd #endif
625 1.5 cgd win->lines[target]->flags &= ~__ISDIRTY;
626 1.5 cgd } else if ((n > 0 && target >= top && target < top + n) ||
627 1.5 cgd (n < 0 && target <= bot && target > bot + n)) {
628 1.7 cgd if (clp->hash != blank_hash || memcmp(clp->line,
629 1.5 cgd buf, win->maxx * __LDATASIZE) !=0) {
630 1.5 cgd (void)memcpy(clp->line, buf,
631 1.5 cgd win->maxx * __LDATASIZE);
632 1.5 cgd #ifdef DEBUG
633 1.5 cgd __CTRACE("-- blanked out: dirty");
634 1.5 cgd #endif
635 1.5 cgd clp->hash = blank_hash;
636 1.5 cgd __touchline(win, target, 0, win->maxx - 1, 0);
637 1.5 cgd } else {
638 1.5 cgd __touchline(win, target, 0, win->maxx - 1, 0);
639 1.5 cgd #ifdef DEBUG
640 1.5 cgd __CTRACE(" -- blank line already: dirty");
641 1.5 cgd #endif
642 1.5 cgd }
643 1.5 cgd } else {
644 1.5 cgd #ifdef DEBUG
645 1.5 cgd __CTRACE(" -- dirty");
646 1.5 cgd #endif
647 1.5 cgd __touchline(win, target, 0, win->maxx - 1, 0);
648 1.5 cgd }
649 1.5 cgd #ifdef DEBUG
650 1.5 cgd __CTRACE("\n");
651 1.5 cgd #endif
652 1.5 cgd if (target == cur_period) {
653 1.5 cgd i = target + 1;
654 1.5 cgd tmp1 = curscr->lines[i];
655 1.5 cgd cur_period = i;
656 1.5 cgd } else {
657 1.5 cgd tmp1 = tmp2;
658 1.5 cgd i = target;
659 1.5 cgd }
660 1.5 cgd }
661 1.5 cgd #ifdef DEBUG
662 1.5 cgd __CTRACE("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
663 1.5 cgd for (i = 0; i < curscr->maxy; i++) {
664 1.5 cgd __CTRACE("C: %d:", i);
665 1.7 cgd for (j = 0; j < curscr->maxx; j++)
666 1.7 cgd __CTRACE("%c",
667 1.5 cgd curscr->lines[i]->line[j].ch);
668 1.5 cgd __CTRACE("\n");
669 1.5 cgd __CTRACE("W: %d:", i);
670 1.7 cgd for (j = 0; j < win->maxx; j++)
671 1.7 cgd __CTRACE("%c", win->lines[i]->line[j].ch);
672 1.5 cgd __CTRACE("\n");
673 1.5 cgd }
674 1.5 cgd #endif
675 1.5 cgd if (n != 0) {
676 1.5 cgd WINDOW *wp;
677 1.5 cgd scrolln(win, starts, startw, curs, bot, top);
678 1.5 cgd /*
679 1.5 cgd * Need to repoint any subwindow lines to the rotated
680 1.7 cgd * line structured.
681 1.5 cgd */
682 1.5 cgd for (wp = win->nextp; wp != win; wp = wp->nextp)
683 1.5 cgd __set_subwin(win, wp);
684 1.1 cgd }
685 1.5 cgd }
686 1.5 cgd
687 1.5 cgd /*
688 1.7 cgd * scrolln --
689 1.7 cgd * Scroll n lines, where n is starts - startw.
690 1.5 cgd */
691 1.5 cgd static void
692 1.5 cgd scrolln(win, starts, startw, curs, bot, top)
693 1.5 cgd WINDOW *win;
694 1.5 cgd int starts, startw, curs, bot, top;
695 1.5 cgd {
696 1.5 cgd int i, oy, ox, n;
697 1.5 cgd
698 1.5 cgd oy = curscr->cury;
699 1.5 cgd ox = curscr->curx;
700 1.5 cgd n = starts - startw;
701 1.5 cgd
702 1.7 cgd /*
703 1.7 cgd * XXX
704 1.7 cgd * The initial tests that set __noqch don't let us reach here unless
705 1.7 cgd * we have either CS + HO + SF/sf/SR/sr, or AL + DL. SF/sf and SR/sr
706 1.7 cgd * scrolling can only shift the entire scrolling region, not just a
707 1.7 cgd * part of it, which means that the quickch() routine is going to be
708 1.7 cgd * sadly disappointed in us if we don't have CS as well.
709 1.7 cgd *
710 1.7 cgd * If CS, HO and SF/sf are set, can use the scrolling region. Because
711 1.7 cgd * the cursor position after CS is undefined, we need HO which gives us
712 1.7 cgd * the ability to move to somewhere without knowledge of the current
713 1.7 cgd * location of the cursor. Still call __mvcur() anyway, to update its
714 1.7 cgd * idea of where the cursor is.
715 1.7 cgd *
716 1.7 cgd * When the scrolling region has been set, the cursor has to be at the
717 1.7 cgd * last line of the region to make the scroll happen.
718 1.7 cgd *
719 1.7 cgd * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
720 1.7 cgd * or al/dl, and, some terminals have AL/DL, sf/sr, and CS, but not
721 1.7 cgd * SF/SR. So, if we're scrolling almost all of the screen, try and use
722 1.7 cgd * AL/DL, otherwise use the scrolling region. The "almost all" is a
723 1.7 cgd * shameless hack for vi.
724 1.7 cgd */
725 1.5 cgd if (n > 0) {
726 1.7 cgd if (CS != NULL && HO != NULL && (SF != NULL ||
727 1.7 cgd (AL == NULL || DL == NULL ||
728 1.7 cgd top > 3 || bot + 3 < win->maxy) && sf != NULL)) {
729 1.7 cgd tputs(__tscroll(CS, top, bot + 1), 0, __cputchar);
730 1.7 cgd __mvcur(oy, ox, 0, 0, 1);
731 1.7 cgd tputs(HO, 0, __cputchar);
732 1.7 cgd __mvcur(0, 0, bot, 0, 1);
733 1.7 cgd if (SF != NULL)
734 1.7 cgd tputs(__tscroll(SF, n, 0), 0, __cputchar);
735 1.7 cgd else
736 1.7 cgd for (i = 0; i < n; i++)
737 1.7 cgd tputs(sf, 0, __cputchar);
738 1.7 cgd tputs(__tscroll(CS, 0, win->maxy), 0, __cputchar);
739 1.7 cgd __mvcur(bot, 0, 0, 0, 1);
740 1.7 cgd tputs(HO, 0, __cputchar);
741 1.7 cgd __mvcur(0, 0, oy, ox, 1);
742 1.7 cgd return;
743 1.7 cgd }
744 1.7 cgd
745 1.7 cgd /* Scroll up the block. */
746 1.7 cgd if (SF != NULL && top == 0) {
747 1.7 cgd __mvcur(oy, ox, bot, 0, 1);
748 1.7 cgd tputs(__tscroll(SF, n, 0), 0, __cputchar);
749 1.7 cgd } else if (DL != NULL) {
750 1.7 cgd __mvcur(oy, ox, top, 0, 1);
751 1.7 cgd tputs(__tscroll(DL, n, 0), 0, __cputchar);
752 1.7 cgd } else if (dl != NULL) {
753 1.7 cgd __mvcur(oy, ox, top, 0, 1);
754 1.7 cgd for (i = 0; i < n; i++)
755 1.5 cgd tputs(dl, 0, __cputchar);
756 1.7 cgd } else if (sf != NULL && top == 0) {
757 1.7 cgd __mvcur(oy, ox, bot, 0, 1);
758 1.7 cgd for (i = 0; i < n; i++)
759 1.7 cgd tputs(sf, 0, __cputchar);
760 1.7 cgd } else
761 1.7 cgd abort();
762 1.5 cgd
763 1.7 cgd /* Push down the bottom region. */
764 1.5 cgd __mvcur(top, 0, bot - n + 1, 0, 1);
765 1.7 cgd if (AL != NULL)
766 1.7 cgd tputs(__tscroll(AL, n, 0), 0, __cputchar);
767 1.7 cgd else if (al != NULL)
768 1.7 cgd for (i = 0; i < n; i++)
769 1.7 cgd tputs(al, 0, __cputchar);
770 1.5 cgd else
771 1.7 cgd abort();
772 1.5 cgd __mvcur(bot - n + 1, 0, oy, ox, 1);
773 1.5 cgd } else {
774 1.7 cgd /*
775 1.7 cgd * !!!
776 1.7 cgd * n < 0
777 1.7 cgd *
778 1.7 cgd * If CS, HO and SR/sr are set, can use the scrolling region.
779 1.7 cgd * See the above comments for details.
780 1.7 cgd */
781 1.7 cgd if (CS != NULL && HO != NULL && (SR != NULL ||
782 1.7 cgd (AL == NULL || DL == NULL ||
783 1.7 cgd top > 3 || bot + 3 < win->maxy) && sr != NULL)) {
784 1.7 cgd tputs(__tscroll(CS, top, bot + 1), 0, __cputchar);
785 1.7 cgd __mvcur(oy, ox, 0, 0, 1);
786 1.7 cgd tputs(HO, 0, __cputchar);
787 1.7 cgd __mvcur(0, 0, top, 0, 1);
788 1.7 cgd
789 1.7 cgd if (SR != NULL)
790 1.7 cgd tputs(__tscroll(SR, -n, 0), 0, __cputchar);
791 1.7 cgd else
792 1.7 cgd for (i = n; i < 0; i++)
793 1.7 cgd tputs(sr, 0, __cputchar);
794 1.7 cgd tputs(__tscroll(CS, 0, win->maxy), 0, __cputchar);
795 1.7 cgd __mvcur(top, 0, 0, 0, 1);
796 1.7 cgd tputs(HO, 0, __cputchar);
797 1.7 cgd __mvcur(0, 0, oy, ox, 1);
798 1.7 cgd return;
799 1.7 cgd }
800 1.7 cgd
801 1.7 cgd /* Preserve the bottom lines. */
802 1.7 cgd __mvcur(oy, ox, bot + n + 1, 0, 1);
803 1.7 cgd if (SR != NULL && bot == win->maxy)
804 1.7 cgd tputs(__tscroll(SR, -n, 0), 0, __cputchar);
805 1.7 cgd else if (DL != NULL)
806 1.7 cgd tputs(__tscroll(DL, -n, 0), 0, __cputchar);
807 1.7 cgd else if (dl != NULL)
808 1.7 cgd for (i = n; i < 0; i++)
809 1.7 cgd tputs(dl, 0, __cputchar);
810 1.7 cgd else if (sr != NULL && bot == win->maxy)
811 1.7 cgd for (i = n; i < 0; i++)
812 1.7 cgd tputs(sr, 0, __cputchar);
813 1.5 cgd else
814 1.7 cgd abort();
815 1.7 cgd
816 1.7 cgd /* Scroll the block down. */
817 1.5 cgd __mvcur(bot + n + 1, 0, top, 0, 1);
818 1.7 cgd if (AL != NULL)
819 1.7 cgd tputs(__tscroll(AL, -n, 0), 0, __cputchar);
820 1.7 cgd else if (al != NULL)
821 1.7 cgd for (i = n; i < 0; i++)
822 1.7 cgd tputs(al, 0, __cputchar);
823 1.5 cgd else
824 1.7 cgd abort();
825 1.5 cgd __mvcur(top, 0, oy, ox, 1);
826 1.7 cgd }
827 1.1 cgd }
828