refresh.c revision 1.24 1 1.24 jdc /* $NetBSD: refresh.c,v 1.24 2000/04/27 22:12:36 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.24 jdc __RCSID("$NetBSD: refresh.c,v 1.24 2000/04/27 22:12:36 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.21 blymn /* the following is defined and set up in setterm.c */
51 1.21 blymn extern struct tinfo *_cursesi_genbuf;
52 1.22 jdc
53 1.23 jdc static int curwin = 0;
54 1.4 mycroft static short ly, lx;
55 1.1 cgd
56 1.4 mycroft static void domvcur __P((int, int, int, int));
57 1.23 jdc static int makech __P((int));
58 1.23 jdc static void quickch __P((void));
59 1.23 jdc static void scrolln __P((int, int, int, int, int));
60 1.1 cgd
61 1.17 blymn #ifndef _CURSES_USE_MACROS
62 1.17 blymn
63 1.17 blymn /*
64 1.17 blymn * refresh --
65 1.17 blymn * Make the current screen look like "stdscr" over the area covered by
66 1.17 blymn * stdscr.
67 1.17 blymn */
68 1.17 blymn int
69 1.17 blymn refresh(void)
70 1.17 blymn {
71 1.17 blymn return wrefresh(stdscr);
72 1.17 blymn }
73 1.17 blymn
74 1.17 blymn #endif
75 1.17 blymn
76 1.4 mycroft /*
77 1.23 jdc * wnoutrefresh --
78 1.23 jdc * Add the contents of "win" to the virtual window.
79 1.23 jdc */
80 1.23 jdc int
81 1.23 jdc wnoutrefresh(WINDOW *win)
82 1.23 jdc {
83 1.23 jdc short wy, wx, y_off, x_off;
84 1.23 jdc
85 1.23 jdc #ifdef DEBUG
86 1.23 jdc __CTRACE("wnoutrefresh: win %0.2o, flags 0x%08x\n", win, win->flags);
87 1.23 jdc #endif
88 1.23 jdc
89 1.23 jdc if (curwin)
90 1.23 jdc return(OK);
91 1.23 jdc __virtscr->cury = win->cury + win->begy;
92 1.23 jdc __virtscr->curx = win->curx + win->begx;
93 1.23 jdc
94 1.23 jdc /* Copy the window flags from "win" to "__virtscr" */
95 1.23 jdc if (!(win->flags & __FULLWIN) && (win->flags & __CLEAROK))
96 1.23 jdc win->flags &= ~__CLEAROK;
97 1.23 jdc __virtscr->flags |= win->flags;
98 1.23 jdc if (win->flags & __CLEAROK)
99 1.23 jdc win->flags &= ~__CLEAROK;
100 1.23 jdc
101 1.23 jdc for (wy = 0; wy < win->maxy; wy++) {
102 1.23 jdc #ifdef DEBUG
103 1.23 jdc __CTRACE("wnoutrefresh: wy %d\tf: %d\tl:%d\tflags %x\n", wy,
104 1.23 jdc *win->lines[wy]->firstchp,
105 1.23 jdc *win->lines[wy]->lastchp,
106 1.23 jdc win->lines[wy]->flags);
107 1.23 jdc #endif
108 1.23 jdc y_off = wy + win->begy;
109 1.23 jdc if (*win->lines[wy]->firstchp <= win->maxx + win->ch_off &&
110 1.23 jdc *win->lines[wy]->lastchp >= win->ch_off) {
111 1.23 jdc /* Copy line from "win" to "__virtscr". */
112 1.23 jdc for (wx = 0; wx < win->maxx; wx++) {
113 1.23 jdc x_off = wx + win->begx;
114 1.23 jdc __virtscr->lines[y_off]->line[x_off].attr =
115 1.23 jdc win->lines[wy]->line[wx].attr;
116 1.23 jdc if (!(win->lines[wy]->line[wx].attr & __COLOR)
117 1.23 jdc && (win->lines[wy]->line[wx].battr &
118 1.23 jdc __COLOR))
119 1.23 jdc __virtscr->lines[y_off]->line[x_off].
120 1.23 jdc attr |=
121 1.23 jdc win->lines[wy]->line[wx].battr &
122 1.23 jdc __COLOR;
123 1.23 jdc if (win->lines[wy]->line[wx].ch == ' ' &&
124 1.23 jdc win->lines[wy]->line[wx].bch != ' ')
125 1.23 jdc __virtscr->lines[y_off]->line[x_off].ch
126 1.23 jdc = win->lines[wy]->line[wx].bch;
127 1.23 jdc else
128 1.23 jdc __virtscr->lines[y_off]->line[x_off].ch
129 1.23 jdc = win->lines[wy]->line[wx].ch;
130 1.23 jdc }
131 1.23 jdc
132 1.23 jdc /* Set flags on "__virtscr" and unset on "win". */
133 1.23 jdc if (win->lines[wy]->flags & __FORCEPAINT) {
134 1.23 jdc __virtscr->lines[y_off]->flags |= __FORCEPAINT;
135 1.23 jdc win->lines[wy]->flags &= ~__FORCEPAINT;
136 1.23 jdc }
137 1.23 jdc if (win->lines[wy]->flags & __ISPASTEOL)
138 1.23 jdc __virtscr->lines[y_off]->flags |= __ISPASTEOL;
139 1.23 jdc else
140 1.23 jdc __virtscr->lines[y_off]->flags &= ~__ISPASTEOL;
141 1.23 jdc if (win->lines[wy]->flags & __ISDIRTY)
142 1.23 jdc __virtscr->lines[y_off]->flags |= __ISDIRTY;
143 1.23 jdc
144 1.23 jdc #ifdef DEBUG
145 1.23 jdc __CTRACE("win: firstch = %d, lastch = %d\n",
146 1.23 jdc *win->lines[wy]->firstchp,
147 1.23 jdc *win->lines[wy]->lastchp);
148 1.23 jdc #endif
149 1.23 jdc /* Set change pointers on "__virtscr". */
150 1.23 jdc if (*__virtscr->lines[y_off]->firstchp >
151 1.23 jdc *win->lines[wy]->firstchp + win->begx - win->ch_off)
152 1.23 jdc *__virtscr->lines[y_off]->firstchp =
153 1.23 jdc *win->lines[wy]->firstchp + win->begx -
154 1.23 jdc win->ch_off;
155 1.23 jdc if (*__virtscr->lines[y_off]->lastchp <
156 1.23 jdc *win->lines[wy]->lastchp + win->begx - win->ch_off)
157 1.23 jdc *__virtscr->lines[y_off]->lastchp =
158 1.23 jdc *win->lines[wy]->lastchp + win->begx -
159 1.23 jdc win->ch_off;
160 1.23 jdc #ifdef DEBUG
161 1.23 jdc __CTRACE("__virtscr: firstch = %d, lastch = %d\n",
162 1.23 jdc *__virtscr->lines[y_off]->firstchp,
163 1.23 jdc *__virtscr->lines[y_off]->lastchp);
164 1.23 jdc #endif
165 1.23 jdc
166 1.23 jdc /* Set change pointers on "win". */
167 1.23 jdc if (*win->lines[wy]->firstchp >= win->ch_off)
168 1.23 jdc *win->lines[wy]->firstchp = win->maxx +
169 1.23 jdc win->ch_off;
170 1.23 jdc if (*win->lines[wy]->lastchp < win->maxx + win->ch_off)
171 1.23 jdc *win->lines[wy]->lastchp = win->ch_off;
172 1.23 jdc if (*win->lines[wy]->lastchp <
173 1.23 jdc *win->lines[wy]->firstchp) {
174 1.23 jdc #ifdef DEBUG
175 1.23 jdc __CTRACE("wnoutrefresh: line %d notdirty\n",
176 1.23 jdc wy);
177 1.23 jdc #endif
178 1.23 jdc win->lines[wy]->flags &= ~__ISDIRTY;
179 1.23 jdc }
180 1.23 jdc }
181 1.23 jdc }
182 1.23 jdc
183 1.23 jdc return (OK);
184 1.23 jdc }
185 1.23 jdc
186 1.23 jdc /*
187 1.4 mycroft * wrefresh --
188 1.4 mycroft * Make the current screen look like "win" over the area coverd by
189 1.4 mycroft * win.
190 1.4 mycroft */
191 1.4 mycroft int
192 1.17 blymn wrefresh(WINDOW *win)
193 1.1 cgd {
194 1.11 mrg int retval;
195 1.23 jdc
196 1.23 jdc curwin = (win == curscr);
197 1.23 jdc if (!curwin)
198 1.23 jdc retval = wnoutrefresh(win);
199 1.23 jdc else
200 1.23 jdc retval = OK;
201 1.23 jdc if (retval == OK) {
202 1.23 jdc retval = doupdate();
203 1.24 jdc if (!win->flags & __LEAVEOK) {
204 1.24 jdc win->cury = max(0, curscr->cury - win->begy);
205 1.24 jdc win->curx = max(0, curscr->curx - win->begx);
206 1.24 jdc }
207 1.23 jdc }
208 1.23 jdc curwin = 0;
209 1.23 jdc return(retval);
210 1.23 jdc }
211 1.23 jdc
212 1.23 jdc /*
213 1.23 jdc * doupdate --
214 1.23 jdc * Make the current screen look like the virtual window "__virtscr".
215 1.23 jdc */
216 1.23 jdc int
217 1.23 jdc doupdate(void)
218 1.23 jdc {
219 1.23 jdc WINDOW *win;
220 1.23 jdc __LINE *wlp;
221 1.23 jdc int retval;
222 1.23 jdc short wy;
223 1.23 jdc int dnum;
224 1.9 phil
225 1.9 phil /* Check if we need to restart ... */
226 1.9 phil if (__endwin) {
227 1.9 phil __endwin = 0;
228 1.9 phil __restartwin();
229 1.9 phil }
230 1.7 cgd
231 1.23 jdc if (curwin)
232 1.23 jdc win = curscr;
233 1.23 jdc else
234 1.23 jdc win = __virtscr;
235 1.23 jdc
236 1.4 mycroft /* Initialize loop parameters. */
237 1.5 cgd ly = curscr->cury;
238 1.5 cgd lx = curscr->curx;
239 1.1 cgd wy = 0;
240 1.1 cgd
241 1.5 cgd if (!curwin)
242 1.5 cgd for (wy = 0; wy < win->maxy; wy++) {
243 1.5 cgd wlp = win->lines[wy];
244 1.5 cgd if (wlp->flags & __ISDIRTY)
245 1.12 christos wlp->hash = __hash((char *)(void *)wlp->line,
246 1.11 mrg (int) (win->maxx * __LDATASIZE));
247 1.5 cgd }
248 1.5 cgd
249 1.20 jdc if ((win->flags & __CLEAROK) || (curscr->flags & __CLEAROK) || curwin) {
250 1.23 jdc if (curscr->wattr & __COLOR)
251 1.23 jdc __unsetattr(0);
252 1.23 jdc tputs(CL, 0, __cputchar);
253 1.23 jdc ly = 0;
254 1.23 jdc lx = 0;
255 1.23 jdc if (!curwin) {
256 1.23 jdc curscr->flags &= ~__CLEAROK;
257 1.23 jdc curscr->cury = 0;
258 1.23 jdc curscr->curx = 0;
259 1.23 jdc werase(curscr);
260 1.1 cgd }
261 1.23 jdc __touchwin(win);
262 1.5 cgd win->flags &= ~__CLEAROK;
263 1.1 cgd }
264 1.1 cgd if (!CA) {
265 1.5 cgd if (win->curx != 0)
266 1.4 mycroft putchar('\n');
267 1.1 cgd if (!curwin)
268 1.1 cgd werase(curscr);
269 1.1 cgd }
270 1.4 mycroft #ifdef DEBUG
271 1.23 jdc __CTRACE("doupdate: (%0.2o): curwin = %d\n", win, curwin);
272 1.23 jdc __CTRACE("doupdate: \tfirstch\tlastch\n");
273 1.5 cgd #endif
274 1.5 cgd
275 1.23 jdc if (!curwin) {
276 1.5 cgd /*
277 1.5 cgd * Invoke quickch() only if more than a quarter of the lines
278 1.5 cgd * in the window are dirty.
279 1.5 cgd */
280 1.5 cgd for (wy = 0, dnum = 0; wy < win->maxy; wy++)
281 1.23 jdc if (win->lines[wy]->flags &
282 1.23 jdc (__ISDIRTY | __FORCEPAINT))
283 1.5 cgd dnum++;
284 1.5 cgd if (!__noqch && dnum > (int) win->maxy / 4)
285 1.23 jdc quickch();
286 1.5 cgd }
287 1.5 cgd
288 1.5 cgd #ifdef DEBUG
289 1.11 mrg {
290 1.11 mrg int i, j;
291 1.11 mrg
292 1.5 cgd __CTRACE("#####################################\n");
293 1.5 cgd for (i = 0; i < curscr->maxy; i++) {
294 1.5 cgd __CTRACE("C: %d:", i);
295 1.5 cgd __CTRACE(" 0x%x \n", curscr->lines[i]->hash);
296 1.7 cgd for (j = 0; j < curscr->maxx; j++)
297 1.11 mrg __CTRACE("%c", curscr->lines[i]->line[j].ch);
298 1.5 cgd __CTRACE("\n");
299 1.14 simonb __CTRACE(" attr:");
300 1.7 cgd for (j = 0; j < curscr->maxx; j++)
301 1.14 simonb __CTRACE(" %x", curscr->lines[i]->line[j].attr);
302 1.5 cgd __CTRACE("\n");
303 1.5 cgd __CTRACE("W: %d:", i);
304 1.23 jdc __CTRACE(" 0x%x \n", win->lines[i]->hash);
305 1.23 jdc __CTRACE(" 0x%x ", win->lines[i]->flags);
306 1.23 jdc for (j = 0; j < win->maxx; j++)
307 1.23 jdc __CTRACE("%c", win->lines[i]->line[j].ch);
308 1.23 jdc __CTRACE("\n");
309 1.23 jdc __CTRACE(" attr:");
310 1.23 jdc for (j = 0; j < win->maxx; j++)
311 1.23 jdc __CTRACE(" %x",
312 1.23 jdc win->lines[i]->line[j].attr);
313 1.23 jdc __CTRACE("\n");
314 1.5 cgd }
315 1.11 mrg }
316 1.11 mrg #endif /* DEBUG */
317 1.5 cgd
318 1.5 cgd for (wy = 0; wy < win->maxy; wy++) {
319 1.23 jdc /* XXX: remove this debug */
320 1.4 mycroft #ifdef DEBUG
321 1.23 jdc __CTRACE("doupdate: wy %d\tf: %d\tl:%d\tflags %x\n", wy,
322 1.23 jdc *win->lines[wy]->firstchp,
323 1.23 jdc *win->lines[wy]->lastchp,
324 1.13 simonb win->lines[wy]->flags);
325 1.4 mycroft #endif
326 1.5 cgd if (!curwin)
327 1.5 cgd curscr->lines[wy]->hash = win->lines[wy]->hash;
328 1.5 cgd if (win->lines[wy]->flags & (__ISDIRTY | __FORCEPAINT)) {
329 1.23 jdc if (makech(wy) == ERR)
330 1.4 mycroft return (ERR);
331 1.1 cgd else {
332 1.23 jdc if (*win->lines[wy]->firstchp >= 0)
333 1.23 jdc *win->lines[wy]->firstchp =
334 1.23 jdc win->maxx;
335 1.23 jdc if (*win->lines[wy]->lastchp <
336 1.23 jdc win->maxx)
337 1.23 jdc *win->lines[wy]->lastchp = 0;
338 1.7 cgd if (*win->lines[wy]->lastchp <
339 1.5 cgd *win->lines[wy]->firstchp) {
340 1.5 cgd #ifdef DEBUG
341 1.23 jdc __CTRACE("doupdate: line %d notdirty\n", wy);
342 1.5 cgd #endif
343 1.5 cgd win->lines[wy]->flags &= ~__ISDIRTY;
344 1.5 cgd }
345 1.1 cgd }
346 1.5 cgd
347 1.5 cgd }
348 1.4 mycroft #ifdef DEBUG
349 1.7 cgd __CTRACE("\t%d\t%d\n", *win->lines[wy]->firstchp,
350 1.11 mrg *win->lines[wy]->lastchp);
351 1.4 mycroft #endif
352 1.1 cgd }
353 1.7 cgd
354 1.5 cgd #ifdef DEBUG
355 1.23 jdc __CTRACE("doupdate: ly=%d, lx=%d\n", ly, lx);
356 1.5 cgd #endif
357 1.1 cgd
358 1.23 jdc if (curwin)
359 1.11 mrg domvcur(ly, lx, (int) win->cury, (int) win->curx);
360 1.1 cgd else {
361 1.5 cgd if (win->flags & __LEAVEOK) {
362 1.5 cgd curscr->cury = ly;
363 1.5 cgd curscr->curx = lx;
364 1.4 mycroft } else {
365 1.23 jdc domvcur(ly, lx, win->cury, win->curx);
366 1.23 jdc curscr->cury = win->cury;
367 1.23 jdc curscr->curx = win->curx;
368 1.1 cgd }
369 1.1 cgd }
370 1.23 jdc
371 1.23 jdc win->flags = 0;
372 1.1 cgd retval = OK;
373 1.4 mycroft
374 1.11 mrg (void) fflush(stdout);
375 1.4 mycroft return (retval);
376 1.1 cgd }
377 1.1 cgd
378 1.1 cgd /*
379 1.4 mycroft * makech --
380 1.4 mycroft * Make a change on the screen.
381 1.1 cgd */
382 1.4 mycroft static int
383 1.23 jdc makech(wy)
384 1.11 mrg int wy;
385 1.1 cgd {
386 1.23 jdc WINDOW *win;
387 1.5 cgd static __LDATA blank = {' ', 0};
388 1.7 cgd __LDATA *nsp, *csp, *cp, *cep;
389 1.11 mrg u_int force;
390 1.11 mrg int clsp, nlsp; /* Last space in lines. */
391 1.23 jdc int lch, wx;
392 1.21 blymn char *ce, cm_buff[1024];
393 1.23 jdc attr_t lspc; /* Last space colour */
394 1.5 cgd
395 1.8 mikel #ifdef __GNUC__
396 1.23 jdc nlsp = lspc = 0; /* XXX gcc -Wuninitialized */
397 1.8 mikel #endif
398 1.23 jdc if (curwin)
399 1.23 jdc win = curscr;
400 1.23 jdc else
401 1.23 jdc win = __virtscr;
402 1.5 cgd /* Is the cursor still on the end of the last line? */
403 1.5 cgd if (wy > 0 && win->lines[wy - 1]->flags & __ISPASTEOL) {
404 1.5 cgd domvcur(ly, lx, ly + 1, 0);
405 1.5 cgd ly++;
406 1.5 cgd lx = 0;
407 1.5 cgd }
408 1.23 jdc wx = *win->lines[wy]->firstchp;
409 1.5 cgd if (wx < 0)
410 1.5 cgd wx = 0;
411 1.11 mrg else
412 1.11 mrg if (wx >= win->maxx)
413 1.11 mrg return (OK);
414 1.23 jdc lch = *win->lines[wy]->lastchp;
415 1.1 cgd if (lch < 0)
416 1.4 mycroft return (OK);
417 1.11 mrg else
418 1.11 mrg if (lch >= (int) win->maxx)
419 1.11 mrg lch = win->maxx - 1;
420 1.1 cgd
421 1.1 cgd if (curwin)
422 1.5 cgd csp = ␣
423 1.1 cgd else
424 1.23 jdc csp = &curscr->lines[wy]->line[wx];
425 1.1 cgd
426 1.5 cgd nsp = &win->lines[wy]->line[wx];
427 1.5 cgd force = win->lines[wy]->flags & __FORCEPAINT;
428 1.5 cgd win->lines[wy]->flags &= ~__FORCEPAINT;
429 1.1 cgd if (CE && !curwin) {
430 1.20 jdc cp = &win->lines[wy]->line[win->maxx - 1];
431 1.23 jdc lspc = cp->attr & __COLOR;
432 1.23 jdc while (cp->ch == ' ' && cp->attr == lspc)
433 1.20 jdc if (cp-- <= win->lines[wy]->line)
434 1.1 cgd break;
435 1.5 cgd nlsp = cp - win->lines[wy]->line;
436 1.1 cgd }
437 1.1 cgd if (!curwin)
438 1.4 mycroft ce = CE;
439 1.1 cgd else
440 1.4 mycroft ce = NULL;
441 1.1 cgd
442 1.5 cgd if (force) {
443 1.21 blymn if (CM) {
444 1.21 blymn t_goto(_cursesi_genbuf, CM, lx, ly, cm_buff, 1023);
445 1.21 blymn tputs(cm_buff, 0, __cputchar);
446 1.21 blymn } else {
447 1.5 cgd tputs(HO, 0, __cputchar);
448 1.5 cgd __mvcur(0, 0, ly, lx, 1);
449 1.5 cgd }
450 1.5 cgd }
451 1.7 cgd
452 1.1 cgd while (wx <= lch) {
453 1.5 cgd if (!force && memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
454 1.4 mycroft if (wx <= lch) {
455 1.5 cgd while (wx <= lch &&
456 1.7 cgd memcmp(nsp, csp, sizeof(__LDATA)) == 0) {
457 1.7 cgd nsp++;
458 1.7 cgd if (!curwin)
459 1.7 cgd ++csp;
460 1.7 cgd ++wx;
461 1.7 cgd }
462 1.4 mycroft continue;
463 1.4 mycroft }
464 1.4 mycroft break;
465 1.4 mycroft }
466 1.23 jdc domvcur(ly, lx, wy, wx);
467 1.5 cgd
468 1.4 mycroft #ifdef DEBUG
469 1.7 cgd __CTRACE("makech: 1: wx = %d, ly= %d, lx = %d, newy = %d, newx = %d, force =%d\n",
470 1.23 jdc wx, ly, lx, wy, wx, force);
471 1.4 mycroft #endif
472 1.23 jdc ly = wy;
473 1.23 jdc lx = wx;
474 1.7 cgd while ((force || memcmp(nsp, csp, sizeof(__LDATA)) != 0)
475 1.5 cgd && wx <= lch) {
476 1.5 cgd
477 1.7 cgd if (ce != NULL &&
478 1.23 jdc wx >= nlsp && nsp->ch == ' ' && nsp->attr == lspc) {
479 1.4 mycroft /* Check for clear to end-of-line. */
480 1.5 cgd cep = &curscr->lines[wy]->line[win->maxx - 1];
481 1.23 jdc while (cep->ch == ' ' && cep->attr == lspc)
482 1.5 cgd if (cep-- <= csp)
483 1.4 mycroft break;
484 1.7 cgd clsp = cep - curscr->lines[wy]->line -
485 1.11 mrg win->begx * __LDATASIZE;
486 1.4 mycroft #ifdef DEBUG
487 1.20 jdc __CTRACE("makech: clsp = %d, nlsp = %d\n",
488 1.20 jdc clsp, nlsp);
489 1.4 mycroft #endif
490 1.20 jdc if (((clsp - nlsp >= strlen(CE) &&
491 1.20 jdc clsp < win->maxx * __LDATASIZE) ||
492 1.20 jdc wy == win->maxy - 1) &&
493 1.23 jdc (!(lspc & __COLOR) ||
494 1.23 jdc ((lspc & __COLOR) && UT))) {
495 1.23 jdc __unsetattr(0);
496 1.23 jdc if ((lspc & __COLOR) !=
497 1.20 jdc (curscr->wattr & __COLOR)) {
498 1.23 jdc __set_color(lspc);
499 1.20 jdc curscr->wattr &= ~__COLOR;
500 1.23 jdc curscr->wattr |= lspc & __COLOR;
501 1.20 jdc }
502 1.4 mycroft tputs(CE, 0, __cputchar);
503 1.5 cgd lx = wx + win->begx;
504 1.5 cgd while (wx++ <= clsp) {
505 1.5 cgd csp->ch = ' ';
506 1.23 jdc csp->attr = lspc;
507 1.5 cgd csp++;
508 1.5 cgd }
509 1.4 mycroft return (OK);
510 1.1 cgd }
511 1.4 mycroft ce = NULL;
512 1.4 mycroft }
513 1.4 mycroft
514 1.7 cgd /*
515 1.16 jdc * Unset colour if appropriate. Check to see
516 1.16 jdc * if we also turn off standout, underscore and
517 1.16 jdc * attributes.
518 1.16 jdc */
519 1.16 jdc if (!(nsp->attr & __COLOR) &&
520 1.16 jdc (curscr->wattr & __COLOR)) {
521 1.16 jdc if (OC != NULL && CC == NULL)
522 1.16 jdc tputs(OC, 0, __cputchar);
523 1.16 jdc if (OP != NULL) {
524 1.16 jdc tputs(OP, 0, __cputchar);
525 1.20 jdc if (SE != NULL &&
526 1.20 jdc !strcmp(OP, SE))
527 1.20 jdc curscr->wattr &=
528 1.20 jdc ~__STANDOUT;
529 1.20 jdc if (UE != NULL &&
530 1.20 jdc !strcmp(OP, UE))
531 1.20 jdc curscr->wattr &=
532 1.20 jdc ~__UNDERSCORE;
533 1.20 jdc if (ME != NULL &&
534 1.20 jdc !strcmp(OP, ME))
535 1.20 jdc curscr->wattr &=
536 1.20 jdc ~__TERMATTR;
537 1.16 jdc }
538 1.16 jdc curscr->wattr &= ~__COLOR;
539 1.16 jdc }
540 1.16 jdc
541 1.16 jdc /*
542 1.11 mrg * Unset attributes as appropriate. Unset first
543 1.11 mrg * so that the relevant attributes can be reset
544 1.16 jdc * (because 'me' unsets 'mb', 'md', 'mh', 'mk',
545 1.16 jdc * 'mp' and 'mr'). Check to see if we also turn off
546 1.16 jdc * standout, attributes and colour.
547 1.11 mrg */
548 1.23 jdc if ((nsp->attr & __TERMATTR) !=
549 1.20 jdc (curscr->wattr & __TERMATTR)) {
550 1.15 blymn tputs(ME, 0, __cputchar);
551 1.20 jdc curscr->wattr &= ~__TERMATTR;
552 1.15 blymn if (SE != NULL && !strcmp(ME, SE))
553 1.15 blymn curscr->wattr &= ~__STANDOUT;
554 1.15 blymn if (UE != NULL && !strcmp(ME, UE))
555 1.15 blymn curscr->wattr &= ~__UNDERSCORE;
556 1.16 jdc if (OP != NULL && !strcmp(ME, OP))
557 1.16 jdc curscr->wattr &= ~__COLOR;
558 1.11 mrg }
559 1.11 mrg
560 1.11 mrg /*
561 1.11 mrg * Exit underscore mode if appropriate.
562 1.16 jdc * Check to see if we also turn off standout,
563 1.16 jdc * attributes and colour.
564 1.11 mrg */
565 1.11 mrg if (!(nsp->attr & __UNDERSCORE) &&
566 1.15 blymn (curscr->wattr & __UNDERSCORE)) {
567 1.11 mrg tputs(UE, 0, __cputchar);
568 1.15 blymn curscr->wattr &= ~__UNDERSCORE;
569 1.15 blymn if (SE != NULL && !strcmp(UE, SE))
570 1.15 blymn curscr->wattr &= ~__STANDOUT;
571 1.15 blymn if (ME != NULL && !strcmp(UE, ME))
572 1.20 jdc curscr->wattr &= ~__TERMATTR;
573 1.16 jdc if (OP != NULL && !strcmp(UE, OP))
574 1.16 jdc curscr->wattr &= ~__COLOR;
575 1.11 mrg }
576 1.11 mrg
577 1.11 mrg /*
578 1.7 cgd * Enter/exit standout mode as appropriate.
579 1.16 jdc * Check to see if we also turn off underscore,
580 1.16 jdc * attributes and colour.
581 1.7 cgd * XXX
582 1.7 cgd * Should use UC if SO/SE not available.
583 1.7 cgd */
584 1.23 jdc if (nsp->attr & __STANDOUT) {
585 1.15 blymn if (!(curscr->wattr & __STANDOUT) &&
586 1.7 cgd SO != NULL && SE != NULL) {
587 1.4 mycroft tputs(SO, 0, __cputchar);
588 1.15 blymn curscr->wattr |= __STANDOUT;
589 1.7 cgd }
590 1.11 mrg } else {
591 1.15 blymn if (curscr->wattr & __STANDOUT) {
592 1.4 mycroft tputs(SE, 0, __cputchar);
593 1.15 blymn curscr->wattr &= ~__STANDOUT;
594 1.15 blymn if (UE != NULL && !strcmp(SE, UE))
595 1.16 jdc curscr->wattr &=
596 1.15 blymn ~__UNDERSCORE;
597 1.15 blymn if (ME != NULL && !strcmp(SE, ME))
598 1.20 jdc curscr->wattr &= ~__TERMATTR;
599 1.16 jdc if (OP != NULL && !strcmp(SE, OP))
600 1.16 jdc curscr->wattr &= ~__COLOR;
601 1.11 mrg }
602 1.11 mrg }
603 1.11 mrg
604 1.11 mrg /*
605 1.11 mrg * Enter underscore mode if appropriate.
606 1.11 mrg * XXX
607 1.11 mrg * Should use UC if US/UE not available.
608 1.11 mrg */
609 1.23 jdc if ((nsp->attr & __UNDERSCORE) &&
610 1.15 blymn !(curscr->wattr & __UNDERSCORE) &&
611 1.11 mrg US != NULL && UE != NULL) {
612 1.11 mrg tputs(US, 0, __cputchar);
613 1.15 blymn curscr->wattr |= __UNDERSCORE;
614 1.11 mrg }
615 1.11 mrg
616 1.11 mrg /*
617 1.11 mrg * Set other attributes as appropriate.
618 1.11 mrg */
619 1.23 jdc if ((nsp->attr & __BLINK) &&
620 1.20 jdc !(curscr->wattr & __BLINK) &&
621 1.20 jdc MB != NULL && ME != NULL) {
622 1.20 jdc tputs(MB, 0, __cputchar);
623 1.20 jdc curscr->wattr |= __BLINK;
624 1.20 jdc }
625 1.23 jdc if ((nsp->attr & __BOLD) &&
626 1.20 jdc !(curscr->wattr & __BOLD) &&
627 1.20 jdc MD != NULL && ME != NULL) {
628 1.20 jdc tputs(MD, 0, __cputchar);
629 1.20 jdc curscr->wattr |= __BOLD;
630 1.20 jdc }
631 1.23 jdc if ((nsp->attr & __DIM) &&
632 1.20 jdc !(curscr->wattr & __DIM) &&
633 1.20 jdc MH != NULL && ME != NULL) {
634 1.20 jdc tputs(MH, 0, __cputchar);
635 1.20 jdc curscr->wattr |= __DIM;
636 1.20 jdc }
637 1.23 jdc if ((nsp->attr & __BLANK) &&
638 1.20 jdc !(curscr->wattr & __BLANK) &&
639 1.20 jdc MK != NULL && ME != NULL) {
640 1.20 jdc tputs(MK, 0, __cputchar);
641 1.20 jdc curscr->wattr |= __BLANK;
642 1.20 jdc }
643 1.23 jdc if ((nsp->attr & __PROTECT) &&
644 1.20 jdc !(curscr->wattr & __PROTECT) &&
645 1.20 jdc MP != NULL && ME != NULL) {
646 1.20 jdc tputs(MP, 0, __cputchar);
647 1.20 jdc curscr->wattr |= __PROTECT;
648 1.20 jdc }
649 1.23 jdc if ((nsp->attr & __REVERSE) &&
650 1.20 jdc !(curscr->wattr & __REVERSE) &&
651 1.20 jdc MR != NULL && ME != NULL) {
652 1.20 jdc tputs(MR, 0, __cputchar);
653 1.20 jdc curscr->wattr |= __REVERSE;
654 1.15 blymn }
655 1.15 blymn
656 1.16 jdc /* Set/change colour as appropriate. */
657 1.23 jdc if ((nsp->attr & __COLOR) &&
658 1.20 jdc cO != NULL && (OC != NULL || OP != NULL)) {
659 1.23 jdc if ((nsp->attr & __COLOR) !=
660 1.23 jdc (curscr->wattr & __COLOR)) {
661 1.23 jdc __set_color(nsp->attr);
662 1.23 jdc curscr->wattr &= ~__COLOR;
663 1.23 jdc curscr->wattr |= nsp->attr &
664 1.23 jdc __COLOR;
665 1.16 jdc }
666 1.16 jdc }
667 1.16 jdc
668 1.15 blymn /* Enter/exit altcharset mode as appropriate. */
669 1.23 jdc if (nsp->attr & __ALTCHARSET) {
670 1.15 blymn if (!(curscr->wattr & __ALTCHARSET) &&
671 1.15 blymn AS != NULL && AE != NULL) {
672 1.15 blymn tputs(AS, 0, __cputchar);
673 1.15 blymn curscr->wattr |= __ALTCHARSET;
674 1.15 blymn }
675 1.15 blymn } else {
676 1.15 blymn if (curscr->wattr & __ALTCHARSET) {
677 1.15 blymn tputs(AE, 0, __cputchar);
678 1.15 blymn curscr->wattr &= ~__ALTCHARSET;
679 1.11 mrg }
680 1.11 mrg }
681 1.4 mycroft
682 1.4 mycroft wx++;
683 1.23 jdc if (wx >= win->maxx &&
684 1.23 jdc wy == win->maxy - 1 && !curwin)
685 1.5 cgd if (win->flags & __SCROLLOK) {
686 1.19 jdc if (win->flags & __ENDLINE){
687 1.23 jdc __unsetattr(1);
688 1.19 jdc }
689 1.5 cgd if (!(win->flags & __SCROLLWIN)) {
690 1.5 cgd if (!curwin) {
691 1.23 jdc csp->attr = nsp->attr;
692 1.23 jdc putchar((int)
693 1.23 jdc (csp->ch =
694 1.23 jdc nsp->ch));
695 1.5 cgd } else
696 1.15 blymn putchar((int) nsp->ch);
697 1.5 cgd }
698 1.23 jdc if (wx < curscr->maxx) {
699 1.23 jdc domvcur(ly, wx,
700 1.23 jdc (int) (win->maxy - 1),
701 1.23 jdc (int) (win->maxx - 1));
702 1.5 cgd }
703 1.23 jdc ly = win->maxy - 1;
704 1.23 jdc lx = win->maxx - 1;
705 1.4 mycroft return (OK);
706 1.7 cgd }
707 1.7 cgd if (wx < win->maxx || wy < win->maxy - 1 ||
708 1.5 cgd !(win->flags & __SCROLLWIN)) {
709 1.5 cgd if (!curwin) {
710 1.23 jdc csp->attr = nsp->attr;
711 1.23 jdc putchar((int) (csp->ch = nsp->ch));
712 1.5 cgd csp++;
713 1.7 cgd } else
714 1.15 blymn putchar((int) nsp->ch);
715 1.5 cgd }
716 1.4 mycroft #ifdef DEBUG
717 1.5 cgd __CTRACE("makech: putchar(%c)\n", nsp->ch & 0177);
718 1.4 mycroft #endif
719 1.11 mrg if (UC && ((nsp->attr & __STANDOUT) ||
720 1.11 mrg (nsp->attr & __UNDERSCORE))) {
721 1.4 mycroft putchar('\b');
722 1.4 mycroft tputs(UC, 0, __cputchar);
723 1.1 cgd }
724 1.4 mycroft nsp++;
725 1.4 mycroft #ifdef DEBUG
726 1.11 mrg __CTRACE("makech: 2: wx = %d, lx = %d\n", wx, lx);
727 1.4 mycroft #endif
728 1.5 cgd }
729 1.23 jdc if (lx == wx) /* If no change. */
730 1.4 mycroft break;
731 1.23 jdc lx = wx;
732 1.5 cgd if (lx >= COLS && AM)
733 1.5 cgd lx = COLS - 1;
734 1.11 mrg else
735 1.11 mrg if (wx >= win->maxx) {
736 1.23 jdc domvcur(ly, lx, ly,
737 1.23 jdc (int) (win->maxx - 1));
738 1.23 jdc lx = win->maxx - 1;
739 1.11 mrg }
740 1.4 mycroft #ifdef DEBUG
741 1.5 cgd __CTRACE("makech: 3: wx = %d, lx = %d\n", wx, lx);
742 1.4 mycroft #endif
743 1.1 cgd }
744 1.7 cgd
745 1.20 jdc /*
746 1.20 jdc * Don't leave the screen with attributes set.
747 1.23 jdc * XXX Should this be at the end of doupdate() and not here?
748 1.20 jdc */
749 1.23 jdc __unsetattr(0);
750 1.4 mycroft return (OK);
751 1.1 cgd }
752 1.1 cgd
753 1.1 cgd /*
754 1.4 mycroft * domvcur --
755 1.19 jdc * Do a mvcur, leaving attributes if necessary.
756 1.1 cgd */
757 1.4 mycroft static void
758 1.1 cgd domvcur(oy, ox, ny, nx)
759 1.11 mrg int oy, ox, ny, nx;
760 1.4 mycroft {
761 1.23 jdc __unsetattr(1);
762 1.5 cgd __mvcur(oy, ox, ny, nx, 1);
763 1.5 cgd }
764 1.5 cgd
765 1.5 cgd /*
766 1.5 cgd * Quickch() attempts to detect a pattern in the change of the window
767 1.7 cgd * in order to optimize the change, e.g., scroll n lines as opposed to
768 1.5 cgd * repainting the screen line by line.
769 1.5 cgd */
770 1.5 cgd
771 1.5 cgd static void
772 1.23 jdc quickch(void)
773 1.5 cgd {
774 1.23 jdc #define THRESH (int) __virtscr->maxy / 4
775 1.5 cgd
776 1.10 perry __LINE *clp, *tmp1, *tmp2;
777 1.11 mrg int bsize, curs, curw, starts, startw, i, j;
778 1.11 mrg int n, target, cur_period, bot, top, sc_region;
779 1.5 cgd __LDATA buf[1024];
780 1.11 mrg u_int blank_hash;
781 1.22 jdc attr_t bcolor;
782 1.5 cgd
783 1.23 jdc return;
784 1.8 mikel #ifdef __GNUC__
785 1.11 mrg curs = curw = starts = startw = 0; /* XXX gcc -Wuninitialized */
786 1.8 mikel #endif
787 1.7 cgd /*
788 1.5 cgd * Find how many lines from the top of the screen are unchanged.
789 1.5 cgd */
790 1.23 jdc for (top = 0; top < __virtscr->maxy; top++)
791 1.23 jdc if (__virtscr->lines[top]->flags & __FORCEPAINT ||
792 1.23 jdc __virtscr->lines[top]->hash != curscr->lines[top]->hash
793 1.23 jdc || memcmp(__virtscr->lines[top]->line,
794 1.11 mrg curscr->lines[top]->line,
795 1.23 jdc (size_t) __virtscr->maxx * __LDATASIZE) != 0)
796 1.5 cgd break;
797 1.5 cgd else
798 1.23 jdc __virtscr->lines[top]->flags &= ~__ISDIRTY;
799 1.11 mrg /*
800 1.11 mrg * Find how many lines from bottom of screen are unchanged.
801 1.11 mrg */
802 1.23 jdc for (bot = __virtscr->maxy - 1; bot >= 0; bot--)
803 1.23 jdc if (__virtscr->lines[bot]->flags & __FORCEPAINT ||
804 1.23 jdc __virtscr->lines[bot]->hash != curscr->lines[bot]->hash
805 1.23 jdc || memcmp(__virtscr->lines[bot]->line,
806 1.11 mrg curscr->lines[bot]->line,
807 1.23 jdc (size_t) __virtscr->maxx * __LDATASIZE) != 0)
808 1.5 cgd break;
809 1.5 cgd else
810 1.23 jdc __virtscr->lines[bot]->flags &= ~__ISDIRTY;
811 1.23 jdc
812 1.23 jdc /*
813 1.23 jdc * Work round an xterm bug where inserting lines causes all the
814 1.23 jdc * inserted lines to be covered with the background colour we
815 1.23 jdc * set on the first line (even if we unset it for subsequent
816 1.23 jdc * lines).
817 1.23 jdc */
818 1.23 jdc bcolor = __virtscr->lines[min(top,
819 1.23 jdc __virtscr->maxy - 1)]->line[0].attr & __COLOR;
820 1.23 jdc for (i = top + 1, j = 0; i < bot; i++) {
821 1.23 jdc if ((__virtscr->lines[i]->line[0].attr & __COLOR) != bcolor) {
822 1.23 jdc bcolor = __virtscr->lines[i]->line[__virtscr->maxx].
823 1.23 jdc attr & __COLOR;
824 1.23 jdc j = i - top;
825 1.23 jdc } else
826 1.23 jdc break;
827 1.23 jdc }
828 1.23 jdc top += j;
829 1.5 cgd
830 1.5 cgd #ifdef NO_JERKINESS
831 1.5 cgd /*
832 1.5 cgd * If we have a bottom unchanged region return. Scrolling the
833 1.5 cgd * bottom region up and then back down causes a screen jitter.
834 1.5 cgd * This will increase the number of characters sent to the screen
835 1.5 cgd * but it looks better.
836 1.5 cgd */
837 1.23 jdc if (bot < __virtscr->maxy - 1)
838 1.5 cgd return;
839 1.11 mrg #endif /* NO_JERKINESS */
840 1.5 cgd
841 1.5 cgd /*
842 1.5 cgd * Search for the largest block of text not changed.
843 1.5 cgd * Invariants of the loop:
844 1.23 jdc * - Startw is the index of the beginning of the examined block in
845 1.23 jdc * __virtscr.
846 1.11 mrg * - Starts is the index of the beginning of the examined block in
847 1.23 jdc * curscr.
848 1.7 cgd * - Curw is the index of one past the end of the exmined block in
849 1.23 jdc * __virtscr.
850 1.23 jdc * - Curs is the index of one past the end of the exmined block in
851 1.5 cgd * curscr.
852 1.5 cgd * - bsize is the current size of the examined block.
853 1.11 mrg */
854 1.23 jdc
855 1.5 cgd for (bsize = bot - top; bsize >= THRESH; bsize--) {
856 1.5 cgd for (startw = top; startw <= bot - bsize; startw++)
857 1.7 cgd for (starts = top; starts <= bot - bsize;
858 1.11 mrg starts++) {
859 1.5 cgd for (curw = startw, curs = starts;
860 1.11 mrg curs < starts + bsize; curw++, curs++)
861 1.23 jdc if (__virtscr->lines[curw]->flags &
862 1.5 cgd __FORCEPAINT ||
863 1.23 jdc (__virtscr->lines[curw]->hash !=
864 1.11 mrg curscr->lines[curs]->hash ||
865 1.23 jdc memcmp(__virtscr->lines[curw]
866 1.23 jdc ->line,
867 1.11 mrg curscr->lines[curs]->line,
868 1.23 jdc (size_t) __virtscr->maxx *
869 1.23 jdc __LDATASIZE) != 0))
870 1.5 cgd break;
871 1.5 cgd if (curs == starts + bsize)
872 1.5 cgd goto done;
873 1.5 cgd }
874 1.5 cgd }
875 1.11 mrg done:
876 1.22 jdc
877 1.5 cgd /* Did not find anything */
878 1.7 cgd if (bsize < THRESH)
879 1.5 cgd return;
880 1.5 cgd
881 1.5 cgd #ifdef DEBUG
882 1.7 cgd __CTRACE("quickch:bsize=%d,starts=%d,startw=%d,curw=%d,curs=%d,top=%d,bot=%d\n",
883 1.11 mrg bsize, starts, startw, curw, curs, top, bot);
884 1.5 cgd #endif
885 1.5 cgd
886 1.7 cgd /*
887 1.7 cgd * Make sure that there is no overlap between the bottom and top
888 1.5 cgd * regions and the middle scrolled block.
889 1.5 cgd */
890 1.5 cgd if (bot < curs)
891 1.5 cgd bot = curs - 1;
892 1.5 cgd if (top > starts)
893 1.5 cgd top = starts;
894 1.5 cgd
895 1.5 cgd n = startw - starts;
896 1.5 cgd
897 1.5 cgd #ifdef DEBUG
898 1.11 mrg __CTRACE("#####################################\n");
899 1.11 mrg for (i = 0; i < curscr->maxy; i++) {
900 1.11 mrg __CTRACE("C: %d:", i);
901 1.11 mrg __CTRACE(" 0x%x \n", curscr->lines[i]->hash);
902 1.11 mrg for (j = 0; j < curscr->maxx; j++)
903 1.11 mrg __CTRACE("%c", curscr->lines[i]->line[j].ch);
904 1.11 mrg __CTRACE("\n");
905 1.14 simonb __CTRACE(" attr:");
906 1.11 mrg for (j = 0; j < curscr->maxx; j++)
907 1.14 simonb __CTRACE(" %x", curscr->lines[i]->line[j].attr);
908 1.11 mrg __CTRACE("\n");
909 1.11 mrg __CTRACE("W: %d:", i);
910 1.23 jdc __CTRACE(" 0x%x \n", __virtscr->lines[i]->hash);
911 1.23 jdc __CTRACE(" 0x%x ", __virtscr->lines[i]->flags);
912 1.23 jdc for (j = 0; j < __virtscr->maxx; j++)
913 1.23 jdc __CTRACE("%c", __virtscr->lines[i]->line[j].ch);
914 1.11 mrg __CTRACE("\n");
915 1.14 simonb __CTRACE(" attr:");
916 1.23 jdc for (j = 0; j < __virtscr->maxx; j++)
917 1.23 jdc __CTRACE(" %x", __virtscr->lines[i]->line[j].attr);
918 1.11 mrg __CTRACE("\n");
919 1.11 mrg }
920 1.7 cgd #endif
921 1.7 cgd
922 1.5 cgd /* So we don't have to call __hash() each time */
923 1.23 jdc for (i = 0; i < __virtscr->maxx; i++) {
924 1.5 cgd buf[i].ch = ' ';
925 1.20 jdc buf[i].bch = ' ';
926 1.5 cgd buf[i].attr = 0;
927 1.20 jdc buf[i].battr = 0;
928 1.5 cgd }
929 1.12 christos blank_hash = __hash((char *)(void *)buf,
930 1.23 jdc (int) (__virtscr->maxx * __LDATASIZE));
931 1.5 cgd
932 1.5 cgd /*
933 1.5 cgd * Perform the rotation to maintain the consistency of curscr.
934 1.5 cgd * This is hairy since we are doing an *in place* rotation.
935 1.5 cgd * Invariants of the loop:
936 1.5 cgd * - I is the index of the current line.
937 1.5 cgd * - Target is the index of the target of line i.
938 1.5 cgd * - Tmp1 points to current line (i).
939 1.5 cgd * - Tmp2 and points to target line (target);
940 1.7 cgd * - Cur_period is the index of the end of the current period.
941 1.5 cgd * (see below).
942 1.5 cgd *
943 1.5 cgd * There are 2 major issues here that make this rotation non-trivial:
944 1.5 cgd * 1. Scrolling in a scrolling region bounded by the top
945 1.5 cgd * and bottom regions determined (whose size is sc_region).
946 1.7 cgd * 2. As a result of the use of the mod function, there may be a
947 1.5 cgd * period introduced, i.e., 2 maps to 4, 4 to 6, n-2 to 0, and
948 1.5 cgd * 0 to 2, which then causes all odd lines not to be rotated.
949 1.7 cgd * To remedy this, an index of the end ( = beginning) of the
950 1.7 cgd * current 'period' is kept, cur_period, and when it is reached,
951 1.7 cgd * the next period is started from cur_period + 1 which is
952 1.5 cgd * guaranteed not to have been reached since that would mean that
953 1.5 cgd * all records would have been reached. (think about it...).
954 1.7 cgd *
955 1.5 cgd * Lines in the rotation can have 3 attributes which are marked on the
956 1.5 cgd * line so that curscr is consistent with the visual screen.
957 1.5 cgd * 1. Not dirty -- lines inside the scrolled block, top region or
958 1.5 cgd * bottom region.
959 1.7 cgd * 2. Blank lines -- lines in the differential of the scrolling
960 1.7 cgd * region adjacent to top and bot regions
961 1.5 cgd * depending on scrolling direction.
962 1.5 cgd * 3. Dirty line -- all other lines are marked dirty.
963 1.5 cgd */
964 1.5 cgd sc_region = bot - top + 1;
965 1.5 cgd i = top;
966 1.5 cgd tmp1 = curscr->lines[top];
967 1.5 cgd cur_period = top;
968 1.5 cgd for (j = top; j <= bot; j++) {
969 1.5 cgd target = (i - top + n + sc_region) % sc_region + top;
970 1.5 cgd tmp2 = curscr->lines[target];
971 1.5 cgd curscr->lines[target] = tmp1;
972 1.5 cgd /* Mark block as clean and blank out scrolled lines. */
973 1.5 cgd clp = curscr->lines[target];
974 1.5 cgd #ifdef DEBUG
975 1.5 cgd __CTRACE("quickch: n=%d startw=%d curw=%d i = %d target=%d ",
976 1.11 mrg n, startw, curw, i, target);
977 1.5 cgd #endif
978 1.7 cgd if ((target >= startw && target < curw) || target < top
979 1.5 cgd || target > bot) {
980 1.5 cgd #ifdef DEBUG
981 1.22 jdc __CTRACE("-- notdirty\n");
982 1.5 cgd #endif
983 1.23 jdc __virtscr->lines[target]->flags &= ~__ISDIRTY;
984 1.11 mrg } else
985 1.11 mrg if ((n > 0 && target >= top && target < top + n) ||
986 1.11 mrg (n < 0 && target <= bot && target > bot + n)) {
987 1.11 mrg if (clp->hash != blank_hash || memcmp(clp->line,
988 1.23 jdc buf, (size_t) __virtscr->maxx * __LDATASIZE) !=0) {
989 1.11 mrg (void)memcpy(clp->line, buf,
990 1.23 jdc (size_t) __virtscr->maxx * __LDATASIZE);
991 1.11 mrg #ifdef DEBUG
992 1.22 jdc __CTRACE("-- blanked out: dirty\n");
993 1.11 mrg #endif
994 1.11 mrg clp->hash = blank_hash;
995 1.23 jdc __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1, 0);
996 1.11 mrg } else {
997 1.5 cgd #ifdef DEBUG
998 1.22 jdc __CTRACE(" -- blank line already: dirty\n");
999 1.5 cgd #endif
1000 1.23 jdc __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1, 0);
1001 1.11 mrg }
1002 1.5 cgd } else {
1003 1.5 cgd #ifdef DEBUG
1004 1.22 jdc __CTRACE(" -- dirty\n");
1005 1.5 cgd #endif
1006 1.23 jdc __touchline(__virtscr, target, 0, (int) __virtscr->maxx - 1, 0);
1007 1.5 cgd }
1008 1.5 cgd if (target == cur_period) {
1009 1.5 cgd i = target + 1;
1010 1.5 cgd tmp1 = curscr->lines[i];
1011 1.5 cgd cur_period = i;
1012 1.5 cgd } else {
1013 1.5 cgd tmp1 = tmp2;
1014 1.5 cgd i = target;
1015 1.5 cgd }
1016 1.5 cgd }
1017 1.5 cgd #ifdef DEBUG
1018 1.11 mrg __CTRACE("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
1019 1.11 mrg for (i = 0; i < curscr->maxy; i++) {
1020 1.11 mrg __CTRACE("C: %d:", i);
1021 1.11 mrg for (j = 0; j < curscr->maxx; j++)
1022 1.11 mrg __CTRACE("%c", curscr->lines[i]->line[j].ch);
1023 1.11 mrg __CTRACE("\n");
1024 1.11 mrg __CTRACE("W: %d:", i);
1025 1.23 jdc for (j = 0; j < __virtscr->maxx; j++)
1026 1.23 jdc __CTRACE("%c", __virtscr->lines[i]->line[j].ch);
1027 1.11 mrg __CTRACE("\n");
1028 1.11 mrg }
1029 1.5 cgd #endif
1030 1.23 jdc if (n != 0)
1031 1.23 jdc scrolln(starts, startw, curs, bot, top);
1032 1.5 cgd }
1033 1.5 cgd
1034 1.5 cgd /*
1035 1.7 cgd * scrolln --
1036 1.7 cgd * Scroll n lines, where n is starts - startw.
1037 1.5 cgd */
1038 1.11 mrg static void /* ARGSUSED */
1039 1.23 jdc scrolln(starts, startw, curs, bot, top)
1040 1.11 mrg int starts, startw, curs, bot, top;
1041 1.5 cgd {
1042 1.11 mrg int i, oy, ox, n;
1043 1.5 cgd
1044 1.5 cgd oy = curscr->cury;
1045 1.5 cgd ox = curscr->curx;
1046 1.5 cgd n = starts - startw;
1047 1.5 cgd
1048 1.7 cgd /*
1049 1.7 cgd * XXX
1050 1.7 cgd * The initial tests that set __noqch don't let us reach here unless
1051 1.7 cgd * we have either CS + HO + SF/sf/SR/sr, or AL + DL. SF/sf and SR/sr
1052 1.7 cgd * scrolling can only shift the entire scrolling region, not just a
1053 1.7 cgd * part of it, which means that the quickch() routine is going to be
1054 1.7 cgd * sadly disappointed in us if we don't have CS as well.
1055 1.7 cgd *
1056 1.7 cgd * If CS, HO and SF/sf are set, can use the scrolling region. Because
1057 1.7 cgd * the cursor position after CS is undefined, we need HO which gives us
1058 1.7 cgd * the ability to move to somewhere without knowledge of the current
1059 1.7 cgd * location of the cursor. Still call __mvcur() anyway, to update its
1060 1.7 cgd * idea of where the cursor is.
1061 1.7 cgd *
1062 1.7 cgd * When the scrolling region has been set, the cursor has to be at the
1063 1.7 cgd * last line of the region to make the scroll happen.
1064 1.7 cgd *
1065 1.7 cgd * Doing SF/SR or AL/DL appears faster on the screen than either sf/sr
1066 1.7 cgd * or al/dl, and, some terminals have AL/DL, sf/sr, and CS, but not
1067 1.7 cgd * SF/SR. So, if we're scrolling almost all of the screen, try and use
1068 1.7 cgd * AL/DL, otherwise use the scrolling region. The "almost all" is a
1069 1.7 cgd * shameless hack for vi.
1070 1.7 cgd */
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.23 jdc top > 3 || bot + 3 < __virtscr->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.23 jdc tputs(__tscroll(CS, 0, (int) __virtscr->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.23 jdc top > 3 || bot + 3 < __virtscr->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.23 jdc tputs(__tscroll(CS, 0, (int) __virtscr->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.23 jdc if (SR != NULL && bot == __virtscr->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.23 jdc if (sr != NULL && bot == __virtscr->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.19 jdc }
1181 1.19 jdc }
1182 1.19 jdc
1183 1.19 jdc /*
1184 1.23 jdc * __unsetattr --
1185 1.19 jdc * Unset attributes on curscr. Leave standout, attribute and colour
1186 1.19 jdc * modes if necessary (!MS). Always leave altcharset (xterm at least
1187 1.19 jdc * ignores a cursor move if we don't).
1188 1.19 jdc */
1189 1.23 jdc void /* ARGSUSED */
1190 1.23 jdc __unsetattr(int checkms)
1191 1.19 jdc {
1192 1.19 jdc int isms;
1193 1.19 jdc
1194 1.19 jdc if (checkms)
1195 1.19 jdc if (!MS) {
1196 1.19 jdc isms = 1;
1197 1.19 jdc } else {
1198 1.19 jdc isms = 0;
1199 1.19 jdc }
1200 1.19 jdc else
1201 1.19 jdc isms = 1;
1202 1.20 jdc #ifdef DEBUG
1203 1.23 jdc __CTRACE("__unsetattr: checkms = %d, MS = %s, wattr = %08x\n",
1204 1.20 jdc checkms, MS ? "TRUE" : "FALSE", curscr->wattr);
1205 1.20 jdc #endif
1206 1.19 jdc
1207 1.20 jdc /*
1208 1.20 jdc * Don't leave the screen in standout mode (check against MS). Check
1209 1.20 jdc * to see if we also turn off underscore, attributes and colour.
1210 1.20 jdc */
1211 1.19 jdc if (curscr->wattr & __STANDOUT && isms) {
1212 1.19 jdc tputs(SE, 0, __cputchar);
1213 1.19 jdc curscr->wattr &= ~__STANDOUT;
1214 1.19 jdc if (UE != NULL && !strcmp(SE, UE))
1215 1.19 jdc curscr->wattr &= ~__UNDERSCORE;
1216 1.19 jdc if (ME != NULL && !strcmp(SE, ME))
1217 1.20 jdc curscr->wattr &= ~__TERMATTR;
1218 1.19 jdc if (OP != NULL && !strcmp(SE, OP))
1219 1.19 jdc curscr->wattr &= ~__COLOR;
1220 1.19 jdc }
1221 1.20 jdc /*
1222 1.20 jdc * Don't leave the screen in underscore mode (check against MS).
1223 1.20 jdc * Check to see if we also turn off attributes and colour.
1224 1.20 jdc */
1225 1.19 jdc if (curscr->wattr & __UNDERSCORE && isms) {
1226 1.19 jdc tputs(UE, 0, __cputchar);
1227 1.19 jdc curscr->wattr &= ~__UNDERSCORE;
1228 1.19 jdc if (ME != NULL && !strcmp(UE, ME))
1229 1.20 jdc curscr->wattr &= ~__TERMATTR;
1230 1.19 jdc if (OP != NULL && !strcmp(UE, OP))
1231 1.19 jdc curscr->wattr &= ~__COLOR;
1232 1.19 jdc }
1233 1.20 jdc /*
1234 1.20 jdc * Don't leave the screen with attributes set (check against MS).
1235 1.20 jdc * Check to see if we also turn off colour.
1236 1.20 jdc */
1237 1.20 jdc if (curscr->wattr & __TERMATTR && isms) {
1238 1.19 jdc tputs(ME, 0, __cputchar);
1239 1.20 jdc curscr->wattr &= ~__TERMATTR;
1240 1.19 jdc if (OP != NULL && !strcmp(ME, OP))
1241 1.19 jdc curscr->wattr &= ~__COLOR;
1242 1.19 jdc }
1243 1.20 jdc /* Don't leave the screen with altcharset set (don't check MS). */
1244 1.19 jdc if (curscr->wattr & __ALTCHARSET) {
1245 1.19 jdc tputs(AE, 0, __cputchar);
1246 1.19 jdc curscr->wattr &= ~__ALTCHARSET;
1247 1.19 jdc }
1248 1.19 jdc /* Don't leave the screen with colour set (check against MS). */
1249 1.19 jdc if (curscr->wattr & __COLOR && isms) {
1250 1.19 jdc if (OC != NULL && CC == NULL)
1251 1.19 jdc tputs(OC, 0, __cputchar);
1252 1.19 jdc if (OP != NULL)
1253 1.19 jdc tputs(OP, 0, __cputchar);
1254 1.19 jdc curscr->wattr &= ~__COLOR;
1255 1.7 cgd }
1256 1.1 cgd }
1257