[BACK]Return to display.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mg

Annotation of src/usr.bin/mg/display.c, Revision 1.5

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