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