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

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