Annotation of src/usr.bin/mg/display.c, Revision 1.20
1.20 ! vincent 1: /* $OpenBSD: display.c,v 1.19 2002/09/15 13:59:16 vincent Exp $ */
1.4 niklas 2:
1.1 deraadt 3: /*
4: * The functions in this file handle redisplay. The
5: * redisplay system knows almost nothing about the editing
6: * process; the editing functions do, however, set some
7: * hints to eliminate a lot of the grinding. There is more
8: * that can be done; the "vtputc" interface is a real
9: * pig. Two conditional compilation flags; the GOSLING
10: * flag enables dynamic programming redisplay, using the
11: * algorithm published by Jim Gosling in SIGOA. The MEMMAP
12: * changes things around for memory mapped video. With
13: * both off, the terminal is a VT52.
14: */
15: #include "def.h"
16: #include "kbd.h"
17:
1.16 vincent 18: #include <ctype.h>
19:
1.1 deraadt 20: /*
21: * You can change these back to the types
22: * implied by the name if you get tight for space. If you
23: * make both of them "int" you get better code on the VAX.
24: * They do nothing if this is not Gosling redisplay, except
25: * for change the size of a structure that isn't used.
26: * A bit of a cheat.
27: */
28: /* These defines really belong in sysdef.h */
29: #ifndef XCHAR
1.3 millert 30: #define XCHAR int
31: #define XSHORT int
1.1 deraadt 32: #endif
33:
34: #ifdef STANDOUT_GLITCH
1.2 millert 35: #include <term.h>
1.1 deraadt 36: #endif
37:
38: /*
39: * A video structure always holds
40: * an array of characters whose length is equal to
1.7 art 41: * the longest line possible. v_text is allocated
42: * dynamically to fit the screen width.
1.1 deraadt 43: */
1.3 millert 44: typedef struct {
1.6 mickey 45: short v_hash; /* Hash code, for compares. */
46: short v_flag; /* Flag word. */
47: short v_color; /* Color of the line. */
48: XSHORT v_cost; /* Cost of display. */
1.7 art 49: char *v_text; /* The actual characters. */
1.3 millert 50: } VIDEO;
51:
52: #define VFCHG 0x0001 /* Changed. */
53: #define VFHBAD 0x0002 /* Hash and cost are bad. */
54: #define VFEXT 0x0004 /* extended line (beond ncol) */
1.1 deraadt 55:
56: /*
57: * SCORE structures hold the optimal
58: * trace trajectory, and the cost of redisplay, when
59: * the dynamic programming redisplay code is used.
60: * If no fancy redisplay, this isn't used. The trace index
1.19 vincent 61: * fields can be "char", and the cost a "short", but
1.1 deraadt 62: * this makes the code worse on the VAX.
63: */
1.3 millert 64: typedef struct {
1.6 mickey 65: XCHAR s_itrace; /* "i" index for track back. */
66: XCHAR s_jtrace; /* "j" index for trace back. */
67: XSHORT s_cost; /* Display cost. */
1.3 millert 68: } SCORE;
69:
1.10 millert 70: void vtmove(int, int);
71: void vtputc(int);
72: void vtpute(int);
1.11 vincent 73: int vtputs(const char *);
1.10 millert 74: void vteeol(void);
75: void updext(int, int);
76: void modeline(MGWIN *);
77: void setscores(int, int);
78: void traceback(int, int, int, int);
79: void ucopy(VIDEO *, VIDEO *);
80: void uline(int, VIDEO *, VIDEO *);
81: void hash(VIDEO *);
1.6 mickey 82:
83:
84: int sgarbf = TRUE; /* TRUE if screen is garbage. */
1.7 art 85: int vtrow = HUGE; /* Virtual cursor row. */
86: int vtcol = HUGE; /* Virtual cursor column. */
1.6 mickey 87: int tthue = CNONE; /* Current color. */
88: int ttrow = HUGE; /* Physical cursor row. */
89: int ttcol = HUGE; /* Physical cursor column. */
90: int tttop = HUGE; /* Top of scroll region. */
91: int ttbot = HUGE; /* Bottom of scroll region. */
92: int lbound = 0; /* leftmost bound of the current line */
1.7 art 93: /* being displayed */
1.3 millert 94:
1.7 art 95: VIDEO **vscreen; /* Edge vector, virtual. */
96: VIDEO **pscreen; /* Edge vector, physical. */
97: VIDEO *video; /* Actual screen data. */
1.6 mickey 98: VIDEO blanks; /* Blank line image. */
1.1 deraadt 99:
100: #ifdef GOSLING
101: /*
102: * This matrix is written as an array because
103: * we do funny things in the "setscores" routine, which
104: * is very compute intensive, to make the subscripts go away.
105: * It would be "SCORE score[NROW][NROW]" in old speak.
106: * Look at "setscores" to understand what is up.
107: */
1.7 art 108: SCORE *score; /* [NROW * NROW] */
109: #endif
110:
111: /*
112: * Reinit the display data structures, this is called when the terminal
113: * size changes.
114: */
115: int
116: vtresize(int force, int newrow, int newcol)
117: {
118: int i;
119: int rowchanged, colchanged;
120: static int first_run = 1;
121: VIDEO *vp;
122:
1.15 vincent 123: if (newrow < 1 || newcol < 1)
124: return (FALSE);
1.7 art 125:
126: rowchanged = (newrow != nrow);
127: colchanged = (newcol != ncol);
128:
129: #define TRYREALLOC(a, n) do { \
130: void *tmp; \
131: if ((tmp = realloc((a), (n))) == NULL) { \
132: panic("out of memory in display code"); \
1.15 vincent 133: } \
1.7 art 134: (a) = tmp; \
135: } while (0)
136:
137: /* No update needed */
138: if (!first_run && !force && !rowchanged && !colchanged) {
1.15 vincent 139: return (TRUE);
1.7 art 140: }
141:
142: if (first_run) {
143: memset(&blanks, 0, sizeof(blanks));
144: }
1.9 deraadt 145:
1.7 art 146: if (rowchanged || first_run) {
147: int vidstart;
148:
149: /*
150: * This is not pretty.
151: */
152: if (nrow == 0)
153: vidstart = 0;
154: else
155: vidstart = 2 * (nrow - 1);
156:
157: /*
158: * We're shrinking, free some internal data
159: */
160: if (newrow < nrow) {
161: for (i = 2 * (newrow - 1); i < 2 * (nrow - 1); i++) {
162: free(video[i].v_text);
163: video[i].v_text = NULL;
164: }
165: }
166:
167: #ifdef GOSLING
168: TRYREALLOC(score, newrow * newrow * sizeof(SCORE));
1.1 deraadt 169: #endif
1.7 art 170: TRYREALLOC(vscreen, (newrow - 1) * sizeof(VIDEO *));
171: TRYREALLOC(pscreen, (newrow - 1) * sizeof(VIDEO *));
172: TRYREALLOC(video, (2 * (newrow - 1)) * sizeof(VIDEO));
173:
174: /*
175: * Zero-out the entries we just allocated
176: */
1.15 vincent 177: for (i = vidstart; i < 2 * (newrow - 1); i++)
1.7 art 178: memset(&video[i], 0, sizeof(VIDEO));
179:
180: /*
181: * Reinitialize vscreen and pscreen arrays completely.
182: */
183: vp = &video[0];
184: for (i = 0; i < newrow - 1; ++i) {
185: vscreen[i] = vp;
186: ++vp;
187: pscreen[i] = vp;
188: ++vp;
189: }
190: }
191: if (rowchanged || colchanged || first_run) {
1.15 vincent 192: for (i = 0; i < 2 * (newrow - 1); i++)
1.7 art 193: TRYREALLOC(video[i].v_text, newcol * sizeof(char));
194: TRYREALLOC(blanks.v_text, newcol * sizeof(char));
195: }
196:
197: nrow = newrow;
198: ncol = newcol;
1.9 deraadt 199:
1.7 art 200: if (ttrow > nrow)
201: ttrow = nrow;
202: if (ttcol > ncol)
203: ttcol = ncol;
204:
1.9 deraadt 205: first_run = 0;
1.15 vincent 206: return (TRUE);
1.7 art 207: }
208:
209: #undef TRYREALLOC
1.1 deraadt 210:
211: /*
212: * Initialize the data structures used
213: * by the display code. The edge vectors used
214: * to access the screens are set up. The operating
215: * system's terminal I/O channel is set up. Fill the
216: * "blanks" array with ASCII blanks. The rest is done
217: * at compile time. The original window is marked
218: * as needing full update, and the physical screen
219: * is marked as garbage, so all the right stuff happens
220: * on the first call to redisplay.
221: */
1.5 art 222: void
1.11 vincent 223: vtinit(void)
1.3 millert 224: {
1.6 mickey 225: int i;
1.1 deraadt 226:
227: ttopen();
228: ttinit();
1.9 deraadt 229:
1.7 art 230: /*
231: * ttinit called ttresize(), which called vtresize(), so our data
232: * structures are setup correctly.
233: */
234:
1.1 deraadt 235: blanks.v_color = CTEXT;
1.7 art 236: for (i = 0; i < ncol; ++i)
1.1 deraadt 237: blanks.v_text[i] = ' ';
238: }
239:
240: /*
241: * Tidy up the virtual display system
242: * in anticipation of a return back to the host
243: * operating system. Right now all we do is position
244: * the cursor to the last line, erase the line, and
245: * close the terminal channel.
246: */
1.5 art 247: void
1.11 vincent 248: vttidy(void)
1.3 millert 249: {
250:
1.1 deraadt 251: ttcolor(CTEXT);
1.3 millert 252: ttnowindow(); /* No scroll window. */
253: ttmove(nrow - 1, 0); /* Echo line. */
1.1 deraadt 254: tteeol();
255: tttidy();
256: ttflush();
257: ttclose();
258: }
259:
260: /*
261: * Move the virtual cursor to an origin
262: * 0 spot on the virtual display screen. I could
263: * store the column as a character pointer to the spot
264: * on the line, which would make "vtputc" a little bit
265: * more efficient. No checking for errors.
266: */
1.5 art 267: void
1.11 vincent 268: vtmove(int row, int col)
1.3 millert 269: {
1.1 deraadt 270: vtrow = row;
271: vtcol = col;
272: }
273:
274: /*
275: * Write a character to the virtual display,
276: * dealing with long lines and the display of unprintable
277: * things like control characters. Also expand tabs every 8
278: * columns. This code only puts printing characters into
279: * the virtual display image. Special care must be taken when
280: * expanding tabs. On a screen whose width is not a multiple
281: * of 8, it is possible for the virtual cursor to hit the
282: * right margin before the next tab stop is reached. This
283: * makes the tab code loop if you are not careful.
284: * Three guesses how we found this.
285: */
1.5 art 286: void
1.11 vincent 287: vtputc(int c)
1.3 millert 288: {
1.6 mickey 289: VIDEO *vp;
1.1 deraadt 290:
1.8 vincent 291: c &= 0xff;
1.9 deraadt 292:
1.1 deraadt 293: vp = vscreen[vtrow];
294: if (vtcol >= ncol)
1.3 millert 295: vp->v_text[ncol - 1] = '$';
1.1 deraadt 296: else if (c == '\t'
297: #ifdef NOTAB
1.17 deraadt 298: && !(curbp->b_flag & BFNOTAB)
1.1 deraadt 299: #endif
1.17 deraadt 300: ) {
1.1 deraadt 301: do {
302: vtputc(' ');
1.3 millert 303: } while (vtcol < ncol && (vtcol & 0x07) != 0);
1.1 deraadt 304: } else if (ISCTRL(c)) {
305: vtputc('^');
306: vtputc(CCHR(c));
1.16 vincent 307: } else if (isprint(c))
1.1 deraadt 308: vp->v_text[vtcol++] = c;
1.16 vincent 309: else {
310: char bf[5];
1.17 deraadt 311:
1.16 vincent 312: snprintf(bf, sizeof bf, "\\%o", c);
313: vtputs(bf);
314: }
1.1 deraadt 315: }
316:
1.3 millert 317: /*
318: * Put a character to the virtual screen in an extended line. If we are not
319: * yet on left edge, don't print it yet. Check for overflow on the right
320: * margin.
1.1 deraadt 321: */
1.5 art 322: void
1.11 vincent 323: vtpute(int c)
1.1 deraadt 324: {
1.3 millert 325: VIDEO *vp;
1.1 deraadt 326:
1.8 vincent 327: c &= 0xff;
1.9 deraadt 328:
1.3 millert 329: vp = vscreen[vtrow];
330: if (vtcol >= ncol)
331: vp->v_text[ncol - 1] = '$';
332: else if (c == '\t'
1.1 deraadt 333: #ifdef NOTAB
1.17 deraadt 334: && !(curbp->b_flag & BFNOTAB)
1.1 deraadt 335: #endif
1.17 deraadt 336: ) {
1.3 millert 337: do {
338: vtpute(' ');
1.8 vincent 339: } while (((vtcol + lbound) & 0x07) != 0 && vtcol < ncol);
1.3 millert 340: } else if (ISCTRL(c) != FALSE) {
341: vtpute('^');
342: vtpute(CCHR(c));
343: } else {
344: if (vtcol >= 0)
345: vp->v_text[vtcol] = c;
346: ++vtcol;
347: }
348: }
1.20 ! vincent 349:
1.3 millert 350:
351: /*
352: * Erase from the end of the software cursor to the end of the line on which
353: * the software cursor is located. The display routines will decide if a
354: * hardware erase to end of line command should be used to display this.
1.1 deraadt 355: */
1.5 art 356: void
1.11 vincent 357: vteeol(void)
1.3 millert 358: {
359: VIDEO *vp;
1.1 deraadt 360:
361: vp = vscreen[vtrow];
362: while (vtcol < ncol)
363: vp->v_text[vtcol++] = ' ';
364: }
365:
366: /*
367: * Make sure that the display is
368: * right. This is a three part process. First,
369: * scan through all of the windows looking for dirty
370: * ones. Check the framing, and refresh the screen.
371: * Second, make sure that "currow" and "curcol" are
372: * correct for the current window. Third, make the
373: * virtual and physical screens the same.
374: */
1.5 art 375: void
1.11 vincent 376: update(void)
1.3 millert 377: {
1.6 mickey 378: LINE *lp;
379: MGWIN *wp;
380: VIDEO *vp1;
381: VIDEO *vp2;
382: int i, j;
383: int c;
384: int hflag;
385: int currow;
386: int curcol;
387: int offs;
388: int size;
1.1 deraadt 389:
1.3 millert 390: if (typeahead())
391: return;
392: if (sgarbf) { /* must update everything */
1.1 deraadt 393: wp = wheadp;
1.3 millert 394: while (wp != NULL) {
1.1 deraadt 395: wp->w_flag |= WFMODE | WFHARD;
396: wp = wp->w_wndp;
397: }
398: }
1.3 millert 399: hflag = FALSE; /* Not hard. */
1.8 vincent 400: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
401: /*
402: * Nothing to be done.
403: */
404: if (wp->w_flag == 0)
405: continue;
1.9 deraadt 406:
1.8 vincent 407: if ((wp->w_flag & WFFORCE) == 0) {
408: lp = wp->w_linep;
409: for (i = 0; i < wp->w_ntrows; ++i) {
410: if (lp == wp->w_dotp)
411: goto out;
412: if (lp == wp->w_bufp->b_linep)
413: break;
414: lp = lforw(lp);
1.1 deraadt 415: }
1.8 vincent 416: }
417: /*
418: * Put the middle-line in place.
419: */
420: i = wp->w_force;
421: if (i > 0) {
422: --i;
423: if (i >= wp->w_ntrows)
424: i = wp->w_ntrows - 1;
425: } else if (i < 0) {
426: i += wp->w_ntrows;
427: if (i < 0)
428: i = 0;
429: } else
1.9 deraadt 430: i = wp->w_ntrows / 2; /* current center, no change */
431:
1.8 vincent 432: /*
433: * Find the line
434: */
435: lp = wp->w_dotp;
436: while (i != 0 && lback(lp) != wp->w_bufp->b_linep) {
437: --i;
438: lp = lback(lp);
439: }
440: wp->w_linep = lp;
441: wp->w_flag |= WFHARD; /* Force full. */
442: out:
443: lp = wp->w_linep; /* Try reduced update. */
444: i = wp->w_toprow;
445: if ((wp->w_flag & ~WFMODE) == WFEDIT) {
446: while (lp != wp->w_dotp) {
447: ++i;
448: lp = lforw(lp);
1.1 deraadt 449: }
1.8 vincent 450: vscreen[i]->v_color = CTEXT;
451: vscreen[i]->v_flag |= (VFCHG | VFHBAD);
452: vtmove(i, 0);
453: for (j = 0; j < llength(lp); ++j)
454: vtputc(lgetc(lp, j));
455: vteeol();
456: } else if ((wp->w_flag & (WFEDIT | WFHARD)) != 0) {
457: hflag = TRUE;
458: while (i < wp->w_toprow + wp->w_ntrows) {
1.1 deraadt 459: vscreen[i]->v_color = CTEXT;
1.3 millert 460: vscreen[i]->v_flag |= (VFCHG | VFHBAD);
1.1 deraadt 461: vtmove(i, 0);
1.8 vincent 462: if (lp != wp->w_bufp->b_linep) {
463: for (j = 0; j < llength(lp); ++j)
464: vtputc(lgetc(lp, j));
465: lp = lforw(lp);
466: }
1.1 deraadt 467: vteeol();
1.8 vincent 468: ++i;
1.1 deraadt 469: }
470: }
1.8 vincent 471: if ((wp->w_flag & WFMODE) != 0)
472: modeline(wp);
473: wp->w_flag = 0;
474: wp->w_force = 0;
1.1 deraadt 475: }
1.3 millert 476: lp = curwp->w_linep; /* Cursor location. */
1.1 deraadt 477: currow = curwp->w_toprow;
478: while (lp != curwp->w_dotp) {
479: ++currow;
480: lp = lforw(lp);
481: }
482: curcol = 0;
483: i = 0;
484: while (i < curwp->w_doto) {
485: c = lgetc(lp, i++);
486: if (c == '\t'
487: #ifdef NOTAB
1.3 millert 488: && !(curbp->b_flag & BFNOTAB)
1.1 deraadt 489: #endif
1.18 vincent 490: ) {
1.3 millert 491: curcol |= 0x07;
1.18 vincent 492: curcol++;
493: } else if (ISCTRL(c) != FALSE)
494: curcol += 2;
495: else if (isprint(c))
496: curcol++;
497: else {
498: char bf[5];
499:
500: snprintf(bf, sizeof bf, "\\%o", c);
501: curcol += strlen(bf);
502: }
1.1 deraadt 503: }
1.3 millert 504: if (curcol >= ncol - 1) { /* extended line. */
505: /* flag we are extended and changed */
1.1 deraadt 506: vscreen[currow]->v_flag |= VFEXT | VFCHG;
1.3 millert 507: updext(currow, curcol); /* and output extended line */
508: } else
509: lbound = 0; /* not extended line */
1.1 deraadt 510:
1.3 millert 511: /*
512: * make sure no lines need to be de-extended because the cursor is no
513: * longer on them
514: */
1.1 deraadt 515: wp = wheadp;
516: while (wp != NULL) {
1.3 millert 517: lp = wp->w_linep;
518: i = wp->w_toprow;
519: while (i < wp->w_toprow + wp->w_ntrows) {
520: if (vscreen[i]->v_flag & VFEXT) {
521: /* always flag extended lines as changed */
522: vscreen[i]->v_flag |= VFCHG;
523: if ((wp != curwp) || (lp != wp->w_dotp) ||
524: (curcol < ncol - 1)) {
525: vtmove(i, 0);
526: for (j = 0; j < llength(lp); ++j)
527: vtputc(lgetc(lp, j));
528: vteeol();
529: /* this line no longer is extended */
530: vscreen[i]->v_flag &= ~VFEXT;
531: }
532: }
533: lp = lforw(lp);
534: ++i;
1.1 deraadt 535: }
1.3 millert 536: /* if garbaged then fix up mode lines */
537: if (sgarbf != FALSE)
538: vscreen[i]->v_flag |= VFCHG;
539: /* and onward to the next window */
540: wp = wp->w_wndp;
1.1 deraadt 541: }
542:
1.3 millert 543: if (sgarbf != FALSE) { /* Screen is garbage. */
544: sgarbf = FALSE; /* Erase-page clears */
545: epresf = FALSE; /* the message area. */
546: tttop = HUGE; /* Forget where you set */
547: ttbot = HUGE; /* scroll region. */
548: tthue = CNONE; /* Color unknown. */
1.1 deraadt 549: ttmove(0, 0);
550: tteeop();
1.3 millert 551: for (i = 0; i < nrow - 1; ++i) {
1.1 deraadt 552: uline(i, vscreen[i], &blanks);
553: ucopy(vscreen[i], pscreen[i]);
554: }
555: ttmove(currow, curcol - lbound);
556: ttflush();
557: return;
558: }
559: #ifdef GOSLING
560: if (hflag != FALSE) { /* Hard update? */
1.3 millert 561: for (i = 0; i < nrow - 1; ++i) {/* Compute hash data. */
1.1 deraadt 562: hash(vscreen[i]);
563: hash(pscreen[i]);
564: }
565: offs = 0; /* Get top match. */
1.3 millert 566: while (offs != nrow - 1) {
1.1 deraadt 567: vp1 = vscreen[offs];
568: vp2 = pscreen[offs];
569: if (vp1->v_color != vp2->v_color
1.3 millert 570: || vp1->v_hash != vp2->v_hash)
1.1 deraadt 571: break;
572: uline(offs, vp1, vp2);
573: ucopy(vp1, vp2);
574: ++offs;
575: }
1.3 millert 576: if (offs == nrow - 1) { /* Might get it all. */
1.1 deraadt 577: ttmove(currow, curcol - lbound);
578: ttflush();
579: return;
580: }
1.3 millert 581: size = nrow - 1; /* Get bottom match. */
1.1 deraadt 582: while (size != offs) {
1.3 millert 583: vp1 = vscreen[size - 1];
584: vp2 = pscreen[size - 1];
1.1 deraadt 585: if (vp1->v_color != vp2->v_color
1.3 millert 586: || vp1->v_hash != vp2->v_hash)
1.1 deraadt 587: break;
1.3 millert 588: uline(size - 1, vp1, vp2);
1.1 deraadt 589: ucopy(vp1, vp2);
590: --size;
591: }
592: if ((size -= offs) == 0) /* Get screen size. */
593: panic("Illegal screen size in update");
594: setscores(offs, size); /* Do hard update. */
595: traceback(offs, size, size, size);
1.3 millert 596: for (i = 0; i < size; ++i)
597: ucopy(vscreen[offs + i], pscreen[offs + i]);
1.1 deraadt 598: ttmove(currow, curcol - lbound);
599: ttflush();
600: return;
601: }
602: #endif
1.3 millert 603: for (i = 0; i < nrow - 1; ++i) { /* Easy update. */
1.1 deraadt 604: vp1 = vscreen[i];
605: vp2 = pscreen[i];
1.3 millert 606: if ((vp1->v_flag & VFCHG) != 0) {
1.1 deraadt 607: uline(i, vp1, vp2);
608: ucopy(vp1, vp2);
609: }
610: }
611: ttmove(currow, curcol - lbound);
612: ttflush();
613: }
614:
615: /*
616: * Update a saved copy of a line,
617: * kept in a VIDEO structure. The "vvp" is
618: * the one in the "vscreen". The "pvp" is the one
619: * in the "pscreen". This is called to make the
620: * virtual and physical screens the same when
621: * display has done an update.
622: */
1.5 art 623: void
1.11 vincent 624: ucopy(VIDEO *vvp, VIDEO *pvp)
1.3 millert 625: {
1.1 deraadt 626:
1.3 millert 627: vvp->v_flag &= ~VFCHG; /* Changes done. */
628: pvp->v_flag = vvp->v_flag; /* Update model. */
629: pvp->v_hash = vvp->v_hash;
630: pvp->v_cost = vvp->v_cost;
1.1 deraadt 631: pvp->v_color = vvp->v_color;
632: bcopy(vvp->v_text, pvp->v_text, ncol);
633: }
634:
1.3 millert 635: /*
636: * updext: update the extended line which the cursor is currently on at a
637: * column greater than the terminal width. The line will be scrolled right or
638: * left to let the user see where the cursor is
1.1 deraadt 639: */
1.5 art 640: void
1.11 vincent 641: updext(int currow, int curcol)
1.1 deraadt 642: {
1.6 mickey 643: LINE *lp; /* pointer to current line */
644: int j; /* index into line */
1.1 deraadt 645:
1.13 millert 646: if (ncol < 2)
647: return;
648:
1.3 millert 649: /*
650: * calculate what column the left bound should be
651: * (force cursor into middle half of screen)
652: */
653: lbound = curcol - (curcol % (ncol >> 1)) - (ncol >> 2);
1.13 millert 654:
1.3 millert 655: /*
656: * scan through the line outputing characters to the virtual screen
657: * once we reach the left edge
658: */
659: vtmove(currow, -lbound); /* start scanning offscreen */
660: lp = curwp->w_dotp; /* line to output */
661: for (j = 0; j < llength(lp); ++j) /* until the end-of-line */
662: vtpute(lgetc(lp, j));
663: vteeol(); /* truncate the virtual line */
664: vscreen[currow]->v_text[0] = '$'; /* and put a '$' in column 1 */
1.1 deraadt 665: }
666:
667: /*
668: * Update a single line. This routine only
669: * uses basic functionality (no insert and delete character,
670: * but erase to end of line). The "vvp" points at the VIDEO
671: * structure for the line on the virtual screen, and the "pvp"
672: * is the same for the physical screen. Avoid erase to end of
673: * line when updating CMODE color lines, because of the way that
674: * reverse video works on most terminals.
675: */
1.5 art 676: void
1.11 vincent 677: uline(int row, VIDEO *vvp, VIDEO *pvp)
1.3 millert 678: {
679: char *cp1;
680: char *cp2;
681: char *cp3;
682: char *cp4;
683: char *cp5;
1.6 mickey 684: int nbflag;
1.1 deraadt 685:
1.11 vincent 686: #ifdef MEMMAP
687: putline(row + 1, 1, &vvp->v_text[0]);
688: #else
689:
1.3 millert 690: if (vvp->v_color != pvp->v_color) { /* Wrong color, do a */
691: ttmove(row, 0); /* full redraw. */
1.1 deraadt 692: #ifdef STANDOUT_GLITCH
1.2 millert 693: if (pvp->v_color != CTEXT && magic_cookie_glitch >= 0)
694: tteeol();
1.1 deraadt 695: #endif
696: ttcolor(vvp->v_color);
697: #ifdef STANDOUT_GLITCH
1.2 millert 698: cp1 = &vvp->v_text[magic_cookie_glitch > 0 ? magic_cookie_glitch : 0];
1.3 millert 699: /*
700: * the odd code for magic_cookie_glitch==0 is to avoid
701: * putting the invisable glitch character on the next line.
1.1 deraadt 702: * (Hazeltine executive 80 model 30)
703: */
1.3 millert 704: cp2 = &vvp->v_text[ncol - (magic_cookie_glitch >= 0 ? (magic_cookie_glitch != 0 ? magic_cookie_glitch : 1) : 0)];
1.1 deraadt 705: #else
706: cp1 = &vvp->v_text[0];
707: cp2 = &vvp->v_text[ncol];
708: #endif
709: while (cp1 != cp2) {
710: ttputc(*cp1++);
711: ++ttcol;
712: }
713: #ifndef MOVE_STANDOUT
714: ttcolor(CTEXT);
715: #endif
716: return;
717: }
1.3 millert 718: cp1 = &vvp->v_text[0]; /* Compute left match. */
1.1 deraadt 719: cp2 = &pvp->v_text[0];
1.3 millert 720: while (cp1 != &vvp->v_text[ncol] && cp1[0] == cp2[0]) {
1.1 deraadt 721: ++cp1;
722: ++cp2;
723: }
1.3 millert 724: if (cp1 == &vvp->v_text[ncol]) /* All equal. */
1.1 deraadt 725: return;
726: nbflag = FALSE;
1.3 millert 727: cp3 = &vvp->v_text[ncol]; /* Compute right match. */
1.1 deraadt 728: cp4 = &pvp->v_text[ncol];
729: while (cp3[-1] == cp4[-1]) {
730: --cp3;
731: --cp4;
1.3 millert 732: if (cp3[0] != ' ') /* Note non-blanks in */
733: nbflag = TRUE; /* the right match. */
1.1 deraadt 734: }
1.3 millert 735: cp5 = cp3; /* Is erase good? */
736: if (nbflag == FALSE && vvp->v_color == CTEXT) {
737: while (cp5 != cp1 && cp5[-1] == ' ')
1.1 deraadt 738: --cp5;
739: /* Alcyon hack */
1.3 millert 740: if ((int) (cp3 - cp5) <= tceeol)
1.1 deraadt 741: cp5 = cp3;
742: }
743: /* Alcyon hack */
1.3 millert 744: ttmove(row, (int) (cp1 - &vvp->v_text[0]));
1.1 deraadt 745: #ifdef STANDOUT_GLITCH
1.2 millert 746: if (vvp->v_color != CTEXT && magic_cookie_glitch > 0) {
1.3 millert 747: if (cp1 < &vvp->v_text[magic_cookie_glitch])
748: cp1 = &vvp->v_text[magic_cookie_glitch];
749: if (cp5 > &vvp->v_text[ncol - magic_cookie_glitch])
750: cp5 = &vvp->v_text[ncol - magic_cookie_glitch];
1.2 millert 751: } else if (magic_cookie_glitch < 0)
1.1 deraadt 752: #endif
753: ttcolor(vvp->v_color);
754: while (cp1 != cp5) {
755: ttputc(*cp1++);
756: ++ttcol;
757: }
1.3 millert 758: if (cp5 != cp3) /* Do erase. */
1.1 deraadt 759: tteeol();
760: #endif
761: }
762:
763: /*
1.3 millert 764: * Redisplay the mode line for the window pointed to by the "wp".
765: * This is the only routine that has any idea of how the modeline is
766: * formatted. You can change the modeline format by hacking at this
767: * routine. Called by "update" any time there is a dirty window. Note
768: * that if STANDOUT_GLITCH is defined, first and last magic_cookie_glitch
769: * characters may never be seen.
770: */
1.5 art 771: void
1.11 vincent 772: modeline(MGWIN *wp)
1.3 millert 773: {
1.6 mickey 774: int n;
1.3 millert 775: BUFFER *bp;
1.12 vincent 776: int mode;
1.3 millert 777:
778: n = wp->w_toprow + wp->w_ntrows; /* Location. */
779: vscreen[n]->v_color = CMODE; /* Mode line color. */
780: vscreen[n]->v_flag |= (VFCHG | VFHBAD); /* Recompute, display. */
781: vtmove(n, 0); /* Seek to right line. */
1.1 deraadt 782: bp = wp->w_bufp;
1.3 millert 783: vtputc('-');
784: vtputc('-');
1.17 deraadt 785: if ((bp->b_flag & BFREADONLY) != 0) {
1.12 vincent 786: vtputc('%');
1.14 vincent 787: if ((bp->b_flag & BFCHG) != 0)
788: vtputc('*');
789: else
790: vtputc('%');
1.12 vincent 791: } else if ((bp->b_flag & BFCHG) != 0) { /* "*" if changed. */
1.3 millert 792: vtputc('*');
793: vtputc('*');
1.17 deraadt 794: } else {
1.3 millert 795: vtputc('-');
796: vtputc('-');
1.1 deraadt 797: }
798: vtputc('-');
1.3 millert 799: n = 5;
1.1 deraadt 800: n += vtputs("Mg: ");
801: if (bp->b_bname[0] != '\0')
802: n += vtputs(&(bp->b_bname[0]));
1.3 millert 803: while (n < 42) { /* Pad out with blanks */
1.1 deraadt 804: vtputc(' ');
805: ++n;
806: }
807: vtputc('(');
808: ++n;
1.11 vincent 809: for (mode = 0; ; ) {
1.3 millert 810: n += vtputs(bp->b_modes[mode]->p_name);
811: if (++mode > bp->b_nmodes)
812: break;
813: vtputc('-');
814: ++n;
1.1 deraadt 815: }
816: vtputc(')');
817: ++n;
1.3 millert 818: while (n < ncol) { /* Pad out. */
1.1 deraadt 819: vtputc('-');
820: ++n;
821: }
822: }
823: /*
824: * output a string to the mode line, report how long it was.
825: */
1.3 millert 826: int
1.11 vincent 827: vtputs(const char *s)
1.3 millert 828: {
1.11 vincent 829: int n = 0;
1.1 deraadt 830:
831: while (*s != '\0') {
832: vtputc(*s++);
833: ++n;
834: }
835: return n;
836: }
1.3 millert 837:
1.1 deraadt 838: #ifdef GOSLING
839: /*
1.3 millert 840: * Compute the hash code for the line pointed to by the "vp".
841: * Recompute it if necessary. Also set the approximate redisplay
842: * cost. The validity of the hash code is marked by a flag bit.
843: * The cost understand the advantages of erase to end of line.
844: * Tuned for the VAX by Bob McNamara; better than it used to be on
1.1 deraadt 845: * just about any machine.
846: */
1.5 art 847: void
1.11 vincent 848: hash(VIDEO *vp)
1.3 millert 849: {
1.6 mickey 850: int i;
851: int n;
1.3 millert 852: char *s;
853:
854: if ((vp->v_flag & VFHBAD) != 0) { /* Hash bad. */
855: s = &vp->v_text[ncol - 1];
856: for (i = ncol; i != 0; --i, --s)
1.1 deraadt 857: if (*s != ' ')
858: break;
1.3 millert 859: n = ncol - i; /* Erase cheaper? */
1.1 deraadt 860: if (n > tceeol)
861: n = tceeol;
1.3 millert 862: vp->v_cost = i + n; /* Bytes + blanks. */
863: for (n = 0; i != 0; --i, --s)
864: n = (n << 5) + n + *s;
865: vp->v_hash = n; /* Hash code. */
866: vp->v_flag &= ~VFHBAD; /* Flag as all done. */
1.1 deraadt 867: }
868: }
869:
870: /*
871: * Compute the Insert-Delete
872: * cost matrix. The dynamic programming algorithm
873: * described by James Gosling is used. This code assumes
874: * that the line above the echo line is the last line involved
875: * in the scroll region. This is easy to arrange on the VT100
876: * because of the scrolling region. The "offs" is the origin 0
877: * offset of the first row in the virtual/physical screen that
878: * is being updated; the "size" is the length of the chunk of
879: * screen being updated. For a full screen update, use offs=0
880: * and size=nrow-1.
881: *
882: * Older versions of this code implemented the score matrix by
883: * a two dimensional array of SCORE nodes. This put all kinds of
884: * multiply instructions in the code! This version is written to
885: * use a linear array and pointers, and contains no multiplication
886: * at all. The code has been carefully looked at on the VAX, with
887: * only marginal checking on other machines for efficiency. In
888: * fact, this has been tuned twice! Bob McNamara tuned it even
889: * more for the VAX, which is a big issue for him because of
890: * the 66 line X displays.
891: *
892: * On some machines, replacing the "for (i=1; i<=size; ++i)" with
893: * i = 1; do { } while (++i <=size)" will make the code quite a
894: * bit better; but it looks ugly.
895: */
1.5 art 896: void
1.11 vincent 897: setscores(int offs, int size)
1.3 millert 898: {
1.6 mickey 899: SCORE *sp;
900: SCORE *sp1;
901: int tempcost;
902: int bestcost;
903: int j, i;
904: VIDEO **vp, **pp;
905: VIDEO **vbase, **pbase;
1.3 millert 906:
907: vbase = &vscreen[offs - 1]; /* By hand CSE's. */
908: pbase = &pscreen[offs - 1];
909: score[0].s_itrace = 0; /* [0, 0] */
1.1 deraadt 910: score[0].s_jtrace = 0;
1.3 millert 911: score[0].s_cost = 0;
912: sp = &score[1]; /* Row 0, inserts. */
1.1 deraadt 913: tempcost = 0;
914: vp = &vbase[1];
1.3 millert 915: for (j = 1; j <= size; ++j) {
1.1 deraadt 916: sp->s_itrace = 0;
1.3 millert 917: sp->s_jtrace = j - 1;
1.1 deraadt 918: tempcost += tcinsl;
919: tempcost += (*vp)->v_cost;
920: sp->s_cost = tempcost;
921: ++vp;
922: ++sp;
923: }
1.7 art 924: sp = &score[nrow]; /* Column 0, deletes. */
1.1 deraadt 925: tempcost = 0;
1.3 millert 926: for (i = 1; i <= size; ++i) {
927: sp->s_itrace = i - 1;
1.1 deraadt 928: sp->s_jtrace = 0;
1.3 millert 929: tempcost += tcdell;
1.1 deraadt 930: sp->s_cost = tempcost;
1.7 art 931: sp += nrow;
1.1 deraadt 932: }
1.7 art 933: sp1 = &score[nrow + 1]; /* [1, 1]. */
1.1 deraadt 934: pp = &pbase[1];
1.3 millert 935: for (i = 1; i <= size; ++i) {
1.1 deraadt 936: sp = sp1;
937: vp = &vbase[1];
1.3 millert 938: for (j = 1; j <= size; ++j) {
939: sp->s_itrace = i - 1;
1.1 deraadt 940: sp->s_jtrace = j;
1.7 art 941: bestcost = (sp - nrow)->s_cost;
1.3 millert 942: if (j != size) /* Cd(A[i])=0 @ Dis. */
1.1 deraadt 943: bestcost += tcdell;
1.3 millert 944: tempcost = (sp - 1)->s_cost;
1.1 deraadt 945: tempcost += (*vp)->v_cost;
1.3 millert 946: if (i != size) /* Ci(B[j])=0 @ Dsj. */
1.1 deraadt 947: tempcost += tcinsl;
948: if (tempcost < bestcost) {
949: sp->s_itrace = i;
1.3 millert 950: sp->s_jtrace = j - 1;
1.1 deraadt 951: bestcost = tempcost;
952: }
1.7 art 953: tempcost = (sp - nrow - 1)->s_cost;
1.1 deraadt 954: if ((*pp)->v_color != (*vp)->v_color
1.3 millert 955: || (*pp)->v_hash != (*vp)->v_hash)
1.1 deraadt 956: tempcost += (*vp)->v_cost;
957: if (tempcost < bestcost) {
1.3 millert 958: sp->s_itrace = i - 1;
959: sp->s_jtrace = j - 1;
1.1 deraadt 960: bestcost = tempcost;
961: }
962: sp->s_cost = bestcost;
1.3 millert 963: ++sp; /* Next column. */
1.1 deraadt 964: ++vp;
965: }
966: ++pp;
1.7 art 967: sp1 += nrow; /* Next row. */
1.1 deraadt 968: }
969: }
970:
971: /*
972: * Trace back through the dynamic programming cost
973: * matrix, and update the screen using an optimal sequence
974: * of redraws, insert lines, and delete lines. The "offs" is
975: * the origin 0 offset of the chunk of the screen we are about to
976: * update. The "i" and "j" are always started in the lower right
977: * corner of the matrix, and imply the size of the screen.
978: * A full screen traceback is called with offs=0 and i=j=nrow-1.
979: * There is some do-it-yourself double subscripting here,
980: * which is acceptable because this routine is much less compute
981: * intensive then the code that builds the score matrix!
982: */
1.5 art 983: void
1.11 vincent 984: traceback(int offs, int size, int i, int j)
1.6 mickey 985: {
986: int itrace;
987: int jtrace;
988: int k;
989: int ninsl;
990: int ndraw;
991: int ndell;
1.1 deraadt 992:
1.3 millert 993: if (i == 0 && j == 0) /* End of update. */
1.1 deraadt 994: return;
1.7 art 995: itrace = score[(nrow * i) + j].s_itrace;
996: jtrace = score[(nrow * i) + j].s_jtrace;
1.3 millert 997: if (itrace == i) { /* [i, j-1] */
998: ninsl = 0; /* Collect inserts. */
1.1 deraadt 999: if (i != size)
1000: ninsl = 1;
1001: ndraw = 1;
1.3 millert 1002: while (itrace != 0 || jtrace != 0) {
1.7 art 1003: if (score[(nrow * itrace) + jtrace].s_itrace != itrace)
1.1 deraadt 1004: break;
1.7 art 1005: jtrace = score[(nrow * itrace) + jtrace].s_jtrace;
1.1 deraadt 1006: if (i != size)
1007: ++ninsl;
1008: ++ndraw;
1009: }
1010: traceback(offs, size, itrace, jtrace);
1011: if (ninsl != 0) {
1012: ttcolor(CTEXT);
1.3 millert 1013: ttinsl(offs + j - ninsl, offs + size - 1, ninsl);
1.1 deraadt 1014: }
1.3 millert 1015: do { /* B[j], A[j] blank. */
1016: k = offs + j - ndraw;
1.1 deraadt 1017: uline(k, vscreen[k], &blanks);
1018: } while (--ndraw);
1019: return;
1020: }
1.3 millert 1021: if (jtrace == j) { /* [i-1, j] */
1022: ndell = 0; /* Collect deletes. */
1.1 deraadt 1023: if (j != size)
1024: ndell = 1;
1.3 millert 1025: while (itrace != 0 || jtrace != 0) {
1.7 art 1026: if (score[(nrow * itrace) + jtrace].s_jtrace != jtrace)
1.1 deraadt 1027: break;
1.7 art 1028: itrace = score[(nrow * itrace) + jtrace].s_itrace;
1.1 deraadt 1029: if (j != size)
1030: ++ndell;
1031: }
1032: if (ndell != 0) {
1033: ttcolor(CTEXT);
1.3 millert 1034: ttdell(offs + i - ndell, offs + size - 1, ndell);
1.1 deraadt 1035: }
1036: traceback(offs, size, itrace, jtrace);
1037: return;
1038: }
1039: traceback(offs, size, itrace, jtrace);
1.3 millert 1040: k = offs + j - 1;
1041: uline(k, vscreen[k], pscreen[offs + i - 1]);
1.1 deraadt 1042: }
1043: #endif