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

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