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