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

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