Annotation of src/usr.bin/mg/display.c, Revision 1.13
1.13 ! millert 1: /* $OpenBSD: display.c,v 1.12 2002/03/16 04:17:36 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.13 ! millert 632: if (ncol < 2)
! 633: return;
! 634:
1.3 millert 635: /*
636: * calculate what column the left bound should be
637: * (force cursor into middle half of screen)
638: */
639: lbound = curcol - (curcol % (ncol >> 1)) - (ncol >> 2);
1.13 ! millert 640:
1.3 millert 641: /*
642: * scan through the line outputing characters to the virtual screen
643: * once we reach the left edge
644: */
645: vtmove(currow, -lbound); /* start scanning offscreen */
646: lp = curwp->w_dotp; /* line to output */
647: for (j = 0; j < llength(lp); ++j) /* until the end-of-line */
648: vtpute(lgetc(lp, j));
649: vteeol(); /* truncate the virtual line */
650: vscreen[currow]->v_text[0] = '$'; /* and put a '$' in column 1 */
1.1 deraadt 651: }
652:
653: /*
654: * Update a single line. This routine only
655: * uses basic functionality (no insert and delete character,
656: * but erase to end of line). The "vvp" points at the VIDEO
657: * structure for the line on the virtual screen, and the "pvp"
658: * is the same for the physical screen. Avoid erase to end of
659: * line when updating CMODE color lines, because of the way that
660: * reverse video works on most terminals.
661: */
1.5 art 662: void
1.11 vincent 663: uline(int row, VIDEO *vvp, VIDEO *pvp)
1.3 millert 664: {
665: char *cp1;
666: char *cp2;
667: char *cp3;
668: char *cp4;
669: char *cp5;
1.6 mickey 670: int nbflag;
1.1 deraadt 671:
1.11 vincent 672: #ifdef MEMMAP
673: putline(row + 1, 1, &vvp->v_text[0]);
674: #else
675:
1.3 millert 676: if (vvp->v_color != pvp->v_color) { /* Wrong color, do a */
677: ttmove(row, 0); /* full redraw. */
1.1 deraadt 678: #ifdef STANDOUT_GLITCH
1.2 millert 679: if (pvp->v_color != CTEXT && magic_cookie_glitch >= 0)
680: tteeol();
1.1 deraadt 681: #endif
682: ttcolor(vvp->v_color);
683: #ifdef STANDOUT_GLITCH
1.2 millert 684: cp1 = &vvp->v_text[magic_cookie_glitch > 0 ? magic_cookie_glitch : 0];
1.3 millert 685: /*
686: * the odd code for magic_cookie_glitch==0 is to avoid
687: * putting the invisable glitch character on the next line.
1.1 deraadt 688: * (Hazeltine executive 80 model 30)
689: */
1.3 millert 690: cp2 = &vvp->v_text[ncol - (magic_cookie_glitch >= 0 ? (magic_cookie_glitch != 0 ? magic_cookie_glitch : 1) : 0)];
1.1 deraadt 691: #else
692: cp1 = &vvp->v_text[0];
693: cp2 = &vvp->v_text[ncol];
694: #endif
695: while (cp1 != cp2) {
696: ttputc(*cp1++);
697: ++ttcol;
698: }
699: #ifndef MOVE_STANDOUT
700: ttcolor(CTEXT);
701: #endif
702: return;
703: }
1.3 millert 704: cp1 = &vvp->v_text[0]; /* Compute left match. */
1.1 deraadt 705: cp2 = &pvp->v_text[0];
1.3 millert 706: while (cp1 != &vvp->v_text[ncol] && cp1[0] == cp2[0]) {
1.1 deraadt 707: ++cp1;
708: ++cp2;
709: }
1.3 millert 710: if (cp1 == &vvp->v_text[ncol]) /* All equal. */
1.1 deraadt 711: return;
712: nbflag = FALSE;
1.3 millert 713: cp3 = &vvp->v_text[ncol]; /* Compute right match. */
1.1 deraadt 714: cp4 = &pvp->v_text[ncol];
715: while (cp3[-1] == cp4[-1]) {
716: --cp3;
717: --cp4;
1.3 millert 718: if (cp3[0] != ' ') /* Note non-blanks in */
719: nbflag = TRUE; /* the right match. */
1.1 deraadt 720: }
1.3 millert 721: cp5 = cp3; /* Is erase good? */
722: if (nbflag == FALSE && vvp->v_color == CTEXT) {
723: while (cp5 != cp1 && cp5[-1] == ' ')
1.1 deraadt 724: --cp5;
725: /* Alcyon hack */
1.3 millert 726: if ((int) (cp3 - cp5) <= tceeol)
1.1 deraadt 727: cp5 = cp3;
728: }
729: /* Alcyon hack */
1.3 millert 730: ttmove(row, (int) (cp1 - &vvp->v_text[0]));
1.1 deraadt 731: #ifdef STANDOUT_GLITCH
1.2 millert 732: if (vvp->v_color != CTEXT && magic_cookie_glitch > 0) {
1.3 millert 733: if (cp1 < &vvp->v_text[magic_cookie_glitch])
734: cp1 = &vvp->v_text[magic_cookie_glitch];
735: if (cp5 > &vvp->v_text[ncol - magic_cookie_glitch])
736: cp5 = &vvp->v_text[ncol - magic_cookie_glitch];
1.2 millert 737: } else if (magic_cookie_glitch < 0)
1.1 deraadt 738: #endif
739: ttcolor(vvp->v_color);
740: while (cp1 != cp5) {
741: ttputc(*cp1++);
742: ++ttcol;
743: }
1.3 millert 744: if (cp5 != cp3) /* Do erase. */
1.1 deraadt 745: tteeol();
746: #endif
747: }
748:
749: /*
1.3 millert 750: * Redisplay the mode line for the window pointed to by the "wp".
751: * This is the only routine that has any idea of how the modeline is
752: * formatted. You can change the modeline format by hacking at this
753: * routine. Called by "update" any time there is a dirty window. Note
754: * that if STANDOUT_GLITCH is defined, first and last magic_cookie_glitch
755: * characters may never be seen.
756: */
1.5 art 757: void
1.11 vincent 758: modeline(MGWIN *wp)
1.3 millert 759: {
1.6 mickey 760: int n;
1.3 millert 761: BUFFER *bp;
1.12 vincent 762: int mode;
1.3 millert 763:
764: n = wp->w_toprow + wp->w_ntrows; /* Location. */
765: vscreen[n]->v_color = CMODE; /* Mode line color. */
766: vscreen[n]->v_flag |= (VFCHG | VFHBAD); /* Recompute, display. */
767: vtmove(n, 0); /* Seek to right line. */
1.1 deraadt 768: bp = wp->w_bufp;
1.3 millert 769: vtputc('-');
770: vtputc('-');
1.12 vincent 771: if ((bp->b_flag & BFREADONLY) != 0) {
772: vtputc('%');
773: vtputc('%');
774: } else if ((bp->b_flag & BFCHG) != 0) { /* "*" if changed. */
1.3 millert 775: vtputc('*');
776: vtputc('*');
1.12 vincent 777: } else {
1.3 millert 778: vtputc('-');
779: vtputc('-');
1.1 deraadt 780: }
781: vtputc('-');
1.3 millert 782: n = 5;
1.1 deraadt 783: n += vtputs("Mg: ");
784: if (bp->b_bname[0] != '\0')
785: n += vtputs(&(bp->b_bname[0]));
1.3 millert 786: while (n < 42) { /* Pad out with blanks */
1.1 deraadt 787: vtputc(' ');
788: ++n;
789: }
790: vtputc('(');
791: ++n;
1.11 vincent 792: for (mode = 0; ; ) {
1.3 millert 793: n += vtputs(bp->b_modes[mode]->p_name);
794: if (++mode > bp->b_nmodes)
795: break;
796: vtputc('-');
797: ++n;
1.1 deraadt 798: }
799: vtputc(')');
800: ++n;
1.3 millert 801: while (n < ncol) { /* Pad out. */
1.1 deraadt 802: vtputc('-');
803: ++n;
804: }
805: }
806: /*
807: * output a string to the mode line, report how long it was.
808: */
1.3 millert 809: int
1.11 vincent 810: vtputs(const char *s)
1.3 millert 811: {
1.11 vincent 812: int n = 0;
1.1 deraadt 813:
814: while (*s != '\0') {
815: vtputc(*s++);
816: ++n;
817: }
818: return n;
819: }
1.3 millert 820:
1.1 deraadt 821: #ifdef GOSLING
822: /*
1.3 millert 823: * Compute the hash code for the line pointed to by the "vp".
824: * Recompute it if necessary. Also set the approximate redisplay
825: * cost. The validity of the hash code is marked by a flag bit.
826: * The cost understand the advantages of erase to end of line.
827: * Tuned for the VAX by Bob McNamara; better than it used to be on
1.1 deraadt 828: * just about any machine.
829: */
1.5 art 830: void
1.11 vincent 831: hash(VIDEO *vp)
1.3 millert 832: {
1.6 mickey 833: int i;
834: int n;
1.3 millert 835: char *s;
836:
837: if ((vp->v_flag & VFHBAD) != 0) { /* Hash bad. */
838: s = &vp->v_text[ncol - 1];
839: for (i = ncol; i != 0; --i, --s)
1.1 deraadt 840: if (*s != ' ')
841: break;
1.3 millert 842: n = ncol - i; /* Erase cheaper? */
1.1 deraadt 843: if (n > tceeol)
844: n = tceeol;
1.3 millert 845: vp->v_cost = i + n; /* Bytes + blanks. */
846: for (n = 0; i != 0; --i, --s)
847: n = (n << 5) + n + *s;
848: vp->v_hash = n; /* Hash code. */
849: vp->v_flag &= ~VFHBAD; /* Flag as all done. */
1.1 deraadt 850: }
851: }
852:
853: /*
854: * Compute the Insert-Delete
855: * cost matrix. The dynamic programming algorithm
856: * described by James Gosling is used. This code assumes
857: * that the line above the echo line is the last line involved
858: * in the scroll region. This is easy to arrange on the VT100
859: * because of the scrolling region. The "offs" is the origin 0
860: * offset of the first row in the virtual/physical screen that
861: * is being updated; the "size" is the length of the chunk of
862: * screen being updated. For a full screen update, use offs=0
863: * and size=nrow-1.
864: *
865: * Older versions of this code implemented the score matrix by
866: * a two dimensional array of SCORE nodes. This put all kinds of
867: * multiply instructions in the code! This version is written to
868: * use a linear array and pointers, and contains no multiplication
869: * at all. The code has been carefully looked at on the VAX, with
870: * only marginal checking on other machines for efficiency. In
871: * fact, this has been tuned twice! Bob McNamara tuned it even
872: * more for the VAX, which is a big issue for him because of
873: * the 66 line X displays.
874: *
875: * On some machines, replacing the "for (i=1; i<=size; ++i)" with
876: * i = 1; do { } while (++i <=size)" will make the code quite a
877: * bit better; but it looks ugly.
878: */
1.5 art 879: void
1.11 vincent 880: setscores(int offs, int size)
1.3 millert 881: {
1.6 mickey 882: SCORE *sp;
883: SCORE *sp1;
884: int tempcost;
885: int bestcost;
886: int j, i;
887: VIDEO **vp, **pp;
888: VIDEO **vbase, **pbase;
1.3 millert 889:
890: vbase = &vscreen[offs - 1]; /* By hand CSE's. */
891: pbase = &pscreen[offs - 1];
892: score[0].s_itrace = 0; /* [0, 0] */
1.1 deraadt 893: score[0].s_jtrace = 0;
1.3 millert 894: score[0].s_cost = 0;
895: sp = &score[1]; /* Row 0, inserts. */
1.1 deraadt 896: tempcost = 0;
897: vp = &vbase[1];
1.3 millert 898: for (j = 1; j <= size; ++j) {
1.1 deraadt 899: sp->s_itrace = 0;
1.3 millert 900: sp->s_jtrace = j - 1;
1.1 deraadt 901: tempcost += tcinsl;
902: tempcost += (*vp)->v_cost;
903: sp->s_cost = tempcost;
904: ++vp;
905: ++sp;
906: }
1.7 art 907: sp = &score[nrow]; /* Column 0, deletes. */
1.1 deraadt 908: tempcost = 0;
1.3 millert 909: for (i = 1; i <= size; ++i) {
910: sp->s_itrace = i - 1;
1.1 deraadt 911: sp->s_jtrace = 0;
1.3 millert 912: tempcost += tcdell;
1.1 deraadt 913: sp->s_cost = tempcost;
1.7 art 914: sp += nrow;
1.1 deraadt 915: }
1.7 art 916: sp1 = &score[nrow + 1]; /* [1, 1]. */
1.1 deraadt 917: pp = &pbase[1];
1.3 millert 918: for (i = 1; i <= size; ++i) {
1.1 deraadt 919: sp = sp1;
920: vp = &vbase[1];
1.3 millert 921: for (j = 1; j <= size; ++j) {
922: sp->s_itrace = i - 1;
1.1 deraadt 923: sp->s_jtrace = j;
1.7 art 924: bestcost = (sp - nrow)->s_cost;
1.3 millert 925: if (j != size) /* Cd(A[i])=0 @ Dis. */
1.1 deraadt 926: bestcost += tcdell;
1.3 millert 927: tempcost = (sp - 1)->s_cost;
1.1 deraadt 928: tempcost += (*vp)->v_cost;
1.3 millert 929: if (i != size) /* Ci(B[j])=0 @ Dsj. */
1.1 deraadt 930: tempcost += tcinsl;
931: if (tempcost < bestcost) {
932: sp->s_itrace = i;
1.3 millert 933: sp->s_jtrace = j - 1;
1.1 deraadt 934: bestcost = tempcost;
935: }
1.7 art 936: tempcost = (sp - nrow - 1)->s_cost;
1.1 deraadt 937: if ((*pp)->v_color != (*vp)->v_color
1.3 millert 938: || (*pp)->v_hash != (*vp)->v_hash)
1.1 deraadt 939: tempcost += (*vp)->v_cost;
940: if (tempcost < bestcost) {
1.3 millert 941: sp->s_itrace = i - 1;
942: sp->s_jtrace = j - 1;
1.1 deraadt 943: bestcost = tempcost;
944: }
945: sp->s_cost = bestcost;
1.3 millert 946: ++sp; /* Next column. */
1.1 deraadt 947: ++vp;
948: }
949: ++pp;
1.7 art 950: sp1 += nrow; /* Next row. */
1.1 deraadt 951: }
952: }
953:
954: /*
955: * Trace back through the dynamic programming cost
956: * matrix, and update the screen using an optimal sequence
957: * of redraws, insert lines, and delete lines. The "offs" is
958: * the origin 0 offset of the chunk of the screen we are about to
959: * update. The "i" and "j" are always started in the lower right
960: * corner of the matrix, and imply the size of the screen.
961: * A full screen traceback is called with offs=0 and i=j=nrow-1.
962: * There is some do-it-yourself double subscripting here,
963: * which is acceptable because this routine is much less compute
964: * intensive then the code that builds the score matrix!
965: */
1.5 art 966: void
1.11 vincent 967: traceback(int offs, int size, int i, int j)
1.6 mickey 968: {
969: int itrace;
970: int jtrace;
971: int k;
972: int ninsl;
973: int ndraw;
974: int ndell;
1.1 deraadt 975:
1.3 millert 976: if (i == 0 && j == 0) /* End of update. */
1.1 deraadt 977: return;
1.7 art 978: itrace = score[(nrow * i) + j].s_itrace;
979: jtrace = score[(nrow * i) + j].s_jtrace;
1.3 millert 980: if (itrace == i) { /* [i, j-1] */
981: ninsl = 0; /* Collect inserts. */
1.1 deraadt 982: if (i != size)
983: ninsl = 1;
984: ndraw = 1;
1.3 millert 985: while (itrace != 0 || jtrace != 0) {
1.7 art 986: if (score[(nrow * itrace) + jtrace].s_itrace != itrace)
1.1 deraadt 987: break;
1.7 art 988: jtrace = score[(nrow * itrace) + jtrace].s_jtrace;
1.1 deraadt 989: if (i != size)
990: ++ninsl;
991: ++ndraw;
992: }
993: traceback(offs, size, itrace, jtrace);
994: if (ninsl != 0) {
995: ttcolor(CTEXT);
1.3 millert 996: ttinsl(offs + j - ninsl, offs + size - 1, ninsl);
1.1 deraadt 997: }
1.3 millert 998: do { /* B[j], A[j] blank. */
999: k = offs + j - ndraw;
1.1 deraadt 1000: uline(k, vscreen[k], &blanks);
1001: } while (--ndraw);
1002: return;
1003: }
1.3 millert 1004: if (jtrace == j) { /* [i-1, j] */
1005: ndell = 0; /* Collect deletes. */
1.1 deraadt 1006: if (j != size)
1007: ndell = 1;
1.3 millert 1008: while (itrace != 0 || jtrace != 0) {
1.7 art 1009: if (score[(nrow * itrace) + jtrace].s_jtrace != jtrace)
1.1 deraadt 1010: break;
1.7 art 1011: itrace = score[(nrow * itrace) + jtrace].s_itrace;
1.1 deraadt 1012: if (j != size)
1013: ++ndell;
1014: }
1015: if (ndell != 0) {
1016: ttcolor(CTEXT);
1.3 millert 1017: ttdell(offs + i - ndell, offs + size - 1, ndell);
1.1 deraadt 1018: }
1019: traceback(offs, size, itrace, jtrace);
1020: return;
1021: }
1022: traceback(offs, size, itrace, jtrace);
1.3 millert 1023: k = offs + j - 1;
1024: uline(k, vscreen[k], pscreen[offs + i - 1]);
1.1 deraadt 1025: }
1026: #endif