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