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

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