[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.21

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