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