Annotation of src/usr.bin/mg/display.c, Revision 1.15
1.15 ! vincent 1: /* $OpenBSD: display.c,v 1.14 2002/06/25 14:24:53 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:
1.15 ! vincent 121: if (newrow < 1 || newcol < 1)
! 122: return (FALSE);
1.7 art 123:
124: rowchanged = (newrow != nrow);
125: colchanged = (newcol != ncol);
126:
127: #define TRYREALLOC(a, n) do { \
128: void *tmp; \
129: if ((tmp = realloc((a), (n))) == NULL) { \
130: panic("out of memory in display code"); \
1.15 ! vincent 131: } \
1.7 art 132: (a) = tmp; \
133: } while (0)
134:
135: /* No update needed */
136: if (!first_run && !force && !rowchanged && !colchanged) {
1.15 ! vincent 137: return (TRUE);
1.7 art 138: }
139:
140: if (first_run) {
141: memset(&blanks, 0, sizeof(blanks));
142: }
1.9 deraadt 143:
1.7 art 144: if (rowchanged || first_run) {
145: int vidstart;
146:
147: /*
148: * This is not pretty.
149: */
150: if (nrow == 0)
151: vidstart = 0;
152: else
153: vidstart = 2 * (nrow - 1);
154:
155: /*
156: * We're shrinking, free some internal data
157: */
158: if (newrow < nrow) {
159: for (i = 2 * (newrow - 1); i < 2 * (nrow - 1); i++) {
160: free(video[i].v_text);
161: video[i].v_text = NULL;
162: }
163: }
164:
165: #ifdef GOSLING
166: TRYREALLOC(score, newrow * newrow * sizeof(SCORE));
1.1 deraadt 167: #endif
1.7 art 168: TRYREALLOC(vscreen, (newrow - 1) * sizeof(VIDEO *));
169: TRYREALLOC(pscreen, (newrow - 1) * sizeof(VIDEO *));
170: TRYREALLOC(video, (2 * (newrow - 1)) * sizeof(VIDEO));
171:
172: /*
173: * Zero-out the entries we just allocated
174: */
1.15 ! vincent 175: for (i = vidstart; i < 2 * (newrow - 1); i++)
1.7 art 176: memset(&video[i], 0, sizeof(VIDEO));
177:
178: /*
179: * Reinitialize vscreen and pscreen arrays completely.
180: */
181: vp = &video[0];
182: for (i = 0; i < newrow - 1; ++i) {
183: vscreen[i] = vp;
184: ++vp;
185: pscreen[i] = vp;
186: ++vp;
187: }
188: }
189: if (rowchanged || colchanged || first_run) {
1.15 ! vincent 190: for (i = 0; i < 2 * (newrow - 1); i++)
1.7 art 191: TRYREALLOC(video[i].v_text, newcol * sizeof(char));
192: TRYREALLOC(blanks.v_text, newcol * sizeof(char));
193: }
194:
195: nrow = newrow;
196: ncol = newcol;
1.9 deraadt 197:
1.7 art 198: if (ttrow > nrow)
199: ttrow = nrow;
200: if (ttcol > ncol)
201: ttcol = ncol;
202:
1.9 deraadt 203: first_run = 0;
1.15 ! vincent 204: return (TRUE);
1.7 art 205: }
206:
207: #undef TRYREALLOC
1.1 deraadt 208:
209: /*
210: * Initialize the data structures used
211: * by the display code. The edge vectors used
212: * to access the screens are set up. The operating
213: * system's terminal I/O channel is set up. Fill the
214: * "blanks" array with ASCII blanks. The rest is done
215: * at compile time. The original window is marked
216: * as needing full update, and the physical screen
217: * is marked as garbage, so all the right stuff happens
218: * on the first call to redisplay.
219: */
1.5 art 220: void
1.11 vincent 221: vtinit(void)
1.3 millert 222: {
1.6 mickey 223: int i;
1.1 deraadt 224:
225: ttopen();
226: ttinit();
1.9 deraadt 227:
1.7 art 228: /*
229: * ttinit called ttresize(), which called vtresize(), so our data
230: * structures are setup correctly.
231: */
232:
1.1 deraadt 233: blanks.v_color = CTEXT;
1.7 art 234: for (i = 0; i < ncol; ++i)
1.1 deraadt 235: blanks.v_text[i] = ' ';
236: }
237:
238: /*
239: * Tidy up the virtual display system
240: * in anticipation of a return back to the host
241: * operating system. Right now all we do is position
242: * the cursor to the last line, erase the line, and
243: * close the terminal channel.
244: */
1.5 art 245: void
1.11 vincent 246: vttidy(void)
1.3 millert 247: {
248:
1.1 deraadt 249: ttcolor(CTEXT);
1.3 millert 250: ttnowindow(); /* No scroll window. */
251: ttmove(nrow - 1, 0); /* Echo line. */
1.1 deraadt 252: tteeol();
253: tttidy();
254: ttflush();
255: ttclose();
256: }
257:
258: /*
259: * Move the virtual cursor to an origin
260: * 0 spot on the virtual display screen. I could
261: * store the column as a character pointer to the spot
262: * on the line, which would make "vtputc" a little bit
263: * more efficient. No checking for errors.
264: */
1.5 art 265: void
1.11 vincent 266: vtmove(int row, int col)
1.3 millert 267: {
1.1 deraadt 268: vtrow = row;
269: vtcol = col;
270: }
271:
272: /*
273: * Write a character to the virtual display,
274: * dealing with long lines and the display of unprintable
275: * things like control characters. Also expand tabs every 8
276: * columns. This code only puts printing characters into
277: * the virtual display image. Special care must be taken when
278: * expanding tabs. On a screen whose width is not a multiple
279: * of 8, it is possible for the virtual cursor to hit the
280: * right margin before the next tab stop is reached. This
281: * makes the tab code loop if you are not careful.
282: * Three guesses how we found this.
283: */
1.5 art 284: void
1.11 vincent 285: vtputc(int c)
1.3 millert 286: {
1.6 mickey 287: VIDEO *vp;
1.1 deraadt 288:
1.8 vincent 289: c &= 0xff;
1.9 deraadt 290:
1.1 deraadt 291: vp = vscreen[vtrow];
292: if (vtcol >= ncol)
1.3 millert 293: vp->v_text[ncol - 1] = '$';
1.1 deraadt 294: else if (c == '\t'
295: #ifdef NOTAB
1.3 millert 296: && !(curbp->b_flag & BFNOTAB)
1.1 deraadt 297: #endif
1.3 millert 298: ) {
1.1 deraadt 299: do {
300: vtputc(' ');
1.3 millert 301: } while (vtcol < ncol && (vtcol & 0x07) != 0);
1.1 deraadt 302: } else if (ISCTRL(c)) {
303: vtputc('^');
304: vtputc(CCHR(c));
305: } else
306: vp->v_text[vtcol++] = c;
307: }
308:
1.3 millert 309: /*
310: * Put a character to the virtual screen in an extended line. If we are not
311: * yet on left edge, don't print it yet. Check for overflow on the right
312: * margin.
1.1 deraadt 313: */
1.5 art 314: void
1.11 vincent 315: vtpute(int c)
1.1 deraadt 316: {
1.3 millert 317: VIDEO *vp;
1.1 deraadt 318:
1.8 vincent 319: c &= 0xff;
1.9 deraadt 320:
1.3 millert 321: vp = vscreen[vtrow];
322: if (vtcol >= ncol)
323: vp->v_text[ncol - 1] = '$';
324: else if (c == '\t'
1.1 deraadt 325: #ifdef NOTAB
1.3 millert 326: && !(curbp->b_flag & BFNOTAB)
1.1 deraadt 327: #endif
1.3 millert 328: ) {
329: do {
330: vtpute(' ');
1.8 vincent 331: } while (((vtcol + lbound) & 0x07) != 0 && vtcol < ncol);
1.3 millert 332: } else if (ISCTRL(c) != FALSE) {
333: vtpute('^');
334: vtpute(CCHR(c));
335: } else {
336: if (vtcol >= 0)
337: vp->v_text[vtcol] = c;
338: ++vtcol;
339: }
340: }
341:
342: /*
343: * Erase from the end of the software cursor to the end of the line on which
344: * the software cursor is located. The display routines will decide if a
345: * hardware erase to end of line command should be used to display this.
1.1 deraadt 346: */
1.5 art 347: void
1.11 vincent 348: vteeol(void)
1.3 millert 349: {
350: VIDEO *vp;
1.1 deraadt 351:
352: vp = vscreen[vtrow];
353: while (vtcol < ncol)
354: vp->v_text[vtcol++] = ' ';
355: }
356:
357: /*
358: * Make sure that the display is
359: * right. This is a three part process. First,
360: * scan through all of the windows looking for dirty
361: * ones. Check the framing, and refresh the screen.
362: * Second, make sure that "currow" and "curcol" are
363: * correct for the current window. Third, make the
364: * virtual and physical screens the same.
365: */
1.5 art 366: void
1.11 vincent 367: update(void)
1.3 millert 368: {
1.6 mickey 369: LINE *lp;
370: MGWIN *wp;
371: VIDEO *vp1;
372: VIDEO *vp2;
373: int i, j;
374: int c;
375: int hflag;
376: int currow;
377: int curcol;
378: int offs;
379: int size;
1.1 deraadt 380:
1.3 millert 381: if (typeahead())
382: return;
383: if (sgarbf) { /* must update everything */
1.1 deraadt 384: wp = wheadp;
1.3 millert 385: while (wp != NULL) {
1.1 deraadt 386: wp->w_flag |= WFMODE | WFHARD;
387: wp = wp->w_wndp;
388: }
389: }
1.3 millert 390: hflag = FALSE; /* Not hard. */
1.8 vincent 391: for (wp = wheadp; wp != NULL; wp = wp->w_wndp) {
392: /*
393: * Nothing to be done.
394: */
395: if (wp->w_flag == 0)
396: continue;
1.9 deraadt 397:
1.8 vincent 398: if ((wp->w_flag & WFFORCE) == 0) {
399: lp = wp->w_linep;
400: for (i = 0; i < wp->w_ntrows; ++i) {
401: if (lp == wp->w_dotp)
402: goto out;
403: if (lp == wp->w_bufp->b_linep)
404: break;
405: lp = lforw(lp);
1.1 deraadt 406: }
1.8 vincent 407: }
408: /*
409: * Put the middle-line in place.
410: */
411: i = wp->w_force;
412: if (i > 0) {
413: --i;
414: if (i >= wp->w_ntrows)
415: i = wp->w_ntrows - 1;
416: } else if (i < 0) {
417: i += wp->w_ntrows;
418: if (i < 0)
419: i = 0;
420: } else
1.9 deraadt 421: i = wp->w_ntrows / 2; /* current center, no change */
422:
1.8 vincent 423: /*
424: * Find the line
425: */
426: lp = wp->w_dotp;
427: while (i != 0 && lback(lp) != wp->w_bufp->b_linep) {
428: --i;
429: lp = lback(lp);
430: }
431: wp->w_linep = lp;
432: wp->w_flag |= WFHARD; /* Force full. */
433: out:
434: lp = wp->w_linep; /* Try reduced update. */
435: i = wp->w_toprow;
436: if ((wp->w_flag & ~WFMODE) == WFEDIT) {
437: while (lp != wp->w_dotp) {
438: ++i;
439: lp = lforw(lp);
1.1 deraadt 440: }
1.8 vincent 441: vscreen[i]->v_color = CTEXT;
442: vscreen[i]->v_flag |= (VFCHG | VFHBAD);
443: vtmove(i, 0);
444: for (j = 0; j < llength(lp); ++j)
445: vtputc(lgetc(lp, j));
446: vteeol();
447: } else if ((wp->w_flag & (WFEDIT | WFHARD)) != 0) {
448: hflag = TRUE;
449: while (i < wp->w_toprow + wp->w_ntrows) {
1.1 deraadt 450: vscreen[i]->v_color = CTEXT;
1.3 millert 451: vscreen[i]->v_flag |= (VFCHG | VFHBAD);
1.1 deraadt 452: vtmove(i, 0);
1.8 vincent 453: if (lp != wp->w_bufp->b_linep) {
454: for (j = 0; j < llength(lp); ++j)
455: vtputc(lgetc(lp, j));
456: lp = lforw(lp);
457: }
1.1 deraadt 458: vteeol();
1.8 vincent 459: ++i;
1.1 deraadt 460: }
461: }
1.8 vincent 462: if ((wp->w_flag & WFMODE) != 0)
463: modeline(wp);
464: wp->w_flag = 0;
465: wp->w_force = 0;
1.1 deraadt 466: }
1.3 millert 467: lp = curwp->w_linep; /* Cursor location. */
1.1 deraadt 468: currow = curwp->w_toprow;
469: while (lp != curwp->w_dotp) {
470: ++currow;
471: lp = lforw(lp);
472: }
473: curcol = 0;
474: i = 0;
475: while (i < curwp->w_doto) {
476: c = lgetc(lp, i++);
477: if (c == '\t'
478: #ifdef NOTAB
1.3 millert 479: && !(curbp->b_flag & BFNOTAB)
1.1 deraadt 480: #endif
1.3 millert 481: )
482: curcol |= 0x07;
1.1 deraadt 483: else if (ISCTRL(c) != FALSE)
484: ++curcol;
485: ++curcol;
486: }
1.3 millert 487: if (curcol >= ncol - 1) { /* extended line. */
488: /* flag we are extended and changed */
1.1 deraadt 489: vscreen[currow]->v_flag |= VFEXT | VFCHG;
1.3 millert 490: updext(currow, curcol); /* and output extended line */
491: } else
492: lbound = 0; /* not extended line */
1.1 deraadt 493:
1.3 millert 494: /*
495: * make sure no lines need to be de-extended because the cursor is no
496: * longer on them
497: */
1.1 deraadt 498: wp = wheadp;
499: while (wp != NULL) {
1.3 millert 500: lp = wp->w_linep;
501: i = wp->w_toprow;
502: while (i < wp->w_toprow + wp->w_ntrows) {
503: if (vscreen[i]->v_flag & VFEXT) {
504: /* always flag extended lines as changed */
505: vscreen[i]->v_flag |= VFCHG;
506: if ((wp != curwp) || (lp != wp->w_dotp) ||
507: (curcol < ncol - 1)) {
508: vtmove(i, 0);
509: for (j = 0; j < llength(lp); ++j)
510: vtputc(lgetc(lp, j));
511: vteeol();
512: /* this line no longer is extended */
513: vscreen[i]->v_flag &= ~VFEXT;
514: }
515: }
516: lp = lforw(lp);
517: ++i;
1.1 deraadt 518: }
1.3 millert 519: /* if garbaged then fix up mode lines */
520: if (sgarbf != FALSE)
521: vscreen[i]->v_flag |= VFCHG;
522: /* and onward to the next window */
523: wp = wp->w_wndp;
1.1 deraadt 524: }
525:
1.3 millert 526: if (sgarbf != FALSE) { /* Screen is garbage. */
527: sgarbf = FALSE; /* Erase-page clears */
528: epresf = FALSE; /* the message area. */
529: tttop = HUGE; /* Forget where you set */
530: ttbot = HUGE; /* scroll region. */
531: tthue = CNONE; /* Color unknown. */
1.1 deraadt 532: ttmove(0, 0);
533: tteeop();
1.3 millert 534: for (i = 0; i < nrow - 1; ++i) {
1.1 deraadt 535: uline(i, vscreen[i], &blanks);
536: ucopy(vscreen[i], pscreen[i]);
537: }
538: ttmove(currow, curcol - lbound);
539: ttflush();
540: return;
541: }
542: #ifdef GOSLING
543: if (hflag != FALSE) { /* Hard update? */
1.3 millert 544: for (i = 0; i < nrow - 1; ++i) {/* Compute hash data. */
1.1 deraadt 545: hash(vscreen[i]);
546: hash(pscreen[i]);
547: }
548: offs = 0; /* Get top match. */
1.3 millert 549: while (offs != nrow - 1) {
1.1 deraadt 550: vp1 = vscreen[offs];
551: vp2 = pscreen[offs];
552: if (vp1->v_color != vp2->v_color
1.3 millert 553: || vp1->v_hash != vp2->v_hash)
1.1 deraadt 554: break;
555: uline(offs, vp1, vp2);
556: ucopy(vp1, vp2);
557: ++offs;
558: }
1.3 millert 559: if (offs == nrow - 1) { /* Might get it all. */
1.1 deraadt 560: ttmove(currow, curcol - lbound);
561: ttflush();
562: return;
563: }
1.3 millert 564: size = nrow - 1; /* Get bottom match. */
1.1 deraadt 565: while (size != offs) {
1.3 millert 566: vp1 = vscreen[size - 1];
567: vp2 = pscreen[size - 1];
1.1 deraadt 568: if (vp1->v_color != vp2->v_color
1.3 millert 569: || vp1->v_hash != vp2->v_hash)
1.1 deraadt 570: break;
1.3 millert 571: uline(size - 1, vp1, vp2);
1.1 deraadt 572: ucopy(vp1, vp2);
573: --size;
574: }
575: if ((size -= offs) == 0) /* Get screen size. */
576: panic("Illegal screen size in update");
577: setscores(offs, size); /* Do hard update. */
578: traceback(offs, size, size, size);
1.3 millert 579: for (i = 0; i < size; ++i)
580: ucopy(vscreen[offs + i], pscreen[offs + i]);
1.1 deraadt 581: ttmove(currow, curcol - lbound);
582: ttflush();
583: return;
584: }
585: #endif
1.3 millert 586: for (i = 0; i < nrow - 1; ++i) { /* Easy update. */
1.1 deraadt 587: vp1 = vscreen[i];
588: vp2 = pscreen[i];
1.3 millert 589: if ((vp1->v_flag & VFCHG) != 0) {
1.1 deraadt 590: uline(i, vp1, vp2);
591: ucopy(vp1, vp2);
592: }
593: }
594: ttmove(currow, curcol - lbound);
595: ttflush();
596: }
597:
598: /*
599: * Update a saved copy of a line,
600: * kept in a VIDEO structure. The "vvp" is
601: * the one in the "vscreen". The "pvp" is the one
602: * in the "pscreen". This is called to make the
603: * virtual and physical screens the same when
604: * display has done an update.
605: */
1.5 art 606: void
1.11 vincent 607: ucopy(VIDEO *vvp, VIDEO *pvp)
1.3 millert 608: {
1.1 deraadt 609:
1.3 millert 610: vvp->v_flag &= ~VFCHG; /* Changes done. */
611: pvp->v_flag = vvp->v_flag; /* Update model. */
612: pvp->v_hash = vvp->v_hash;
613: pvp->v_cost = vvp->v_cost;
1.1 deraadt 614: pvp->v_color = vvp->v_color;
615: bcopy(vvp->v_text, pvp->v_text, ncol);
616: }
617:
1.3 millert 618: /*
619: * updext: update the extended line which the cursor is currently on at a
620: * column greater than the terminal width. The line will be scrolled right or
621: * left to let the user see where the cursor is
1.1 deraadt 622: */
1.5 art 623: void
1.11 vincent 624: updext(int currow, int curcol)
1.1 deraadt 625: {
1.6 mickey 626: LINE *lp; /* pointer to current line */
627: int j; /* index into line */
1.1 deraadt 628:
1.13 millert 629: if (ncol < 2)
630: return;
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);
1.13 millert 637:
1.3 millert 638: /*
639: * scan through the line outputing characters to the virtual screen
640: * once we reach the left edge
641: */
642: vtmove(currow, -lbound); /* start scanning offscreen */
643: lp = curwp->w_dotp; /* line to output */
644: for (j = 0; j < llength(lp); ++j) /* until the end-of-line */
645: vtpute(lgetc(lp, j));
646: vteeol(); /* truncate the virtual line */
647: vscreen[currow]->v_text[0] = '$'; /* and put a '$' in column 1 */
1.1 deraadt 648: }
649:
650: /*
651: * Update a single line. This routine only
652: * uses basic functionality (no insert and delete character,
653: * but erase to end of line). The "vvp" points at the VIDEO
654: * structure for the line on the virtual screen, and the "pvp"
655: * is the same for the physical screen. Avoid erase to end of
656: * line when updating CMODE color lines, because of the way that
657: * reverse video works on most terminals.
658: */
1.5 art 659: void
1.11 vincent 660: uline(int row, VIDEO *vvp, VIDEO *pvp)
1.3 millert 661: {
662: char *cp1;
663: char *cp2;
664: char *cp3;
665: char *cp4;
666: char *cp5;
1.6 mickey 667: int nbflag;
1.1 deraadt 668:
1.11 vincent 669: #ifdef MEMMAP
670: putline(row + 1, 1, &vvp->v_text[0]);
671: #else
672:
1.3 millert 673: if (vvp->v_color != pvp->v_color) { /* Wrong color, do a */
674: ttmove(row, 0); /* full redraw. */
1.1 deraadt 675: #ifdef STANDOUT_GLITCH
1.2 millert 676: if (pvp->v_color != CTEXT && magic_cookie_glitch >= 0)
677: tteeol();
1.1 deraadt 678: #endif
679: ttcolor(vvp->v_color);
680: #ifdef STANDOUT_GLITCH
1.2 millert 681: cp1 = &vvp->v_text[magic_cookie_glitch > 0 ? magic_cookie_glitch : 0];
1.3 millert 682: /*
683: * the odd code for magic_cookie_glitch==0 is to avoid
684: * putting the invisable glitch character on the next line.
1.1 deraadt 685: * (Hazeltine executive 80 model 30)
686: */
1.3 millert 687: cp2 = &vvp->v_text[ncol - (magic_cookie_glitch >= 0 ? (magic_cookie_glitch != 0 ? magic_cookie_glitch : 1) : 0)];
1.1 deraadt 688: #else
689: cp1 = &vvp->v_text[0];
690: cp2 = &vvp->v_text[ncol];
691: #endif
692: while (cp1 != cp2) {
693: ttputc(*cp1++);
694: ++ttcol;
695: }
696: #ifndef MOVE_STANDOUT
697: ttcolor(CTEXT);
698: #endif
699: return;
700: }
1.3 millert 701: cp1 = &vvp->v_text[0]; /* Compute left match. */
1.1 deraadt 702: cp2 = &pvp->v_text[0];
1.3 millert 703: while (cp1 != &vvp->v_text[ncol] && cp1[0] == cp2[0]) {
1.1 deraadt 704: ++cp1;
705: ++cp2;
706: }
1.3 millert 707: if (cp1 == &vvp->v_text[ncol]) /* All equal. */
1.1 deraadt 708: return;
709: nbflag = FALSE;
1.3 millert 710: cp3 = &vvp->v_text[ncol]; /* Compute right match. */
1.1 deraadt 711: cp4 = &pvp->v_text[ncol];
712: while (cp3[-1] == cp4[-1]) {
713: --cp3;
714: --cp4;
1.3 millert 715: if (cp3[0] != ' ') /* Note non-blanks in */
716: nbflag = TRUE; /* the right match. */
1.1 deraadt 717: }
1.3 millert 718: cp5 = cp3; /* Is erase good? */
719: if (nbflag == FALSE && vvp->v_color == CTEXT) {
720: while (cp5 != cp1 && cp5[-1] == ' ')
1.1 deraadt 721: --cp5;
722: /* Alcyon hack */
1.3 millert 723: if ((int) (cp3 - cp5) <= tceeol)
1.1 deraadt 724: cp5 = cp3;
725: }
726: /* Alcyon hack */
1.3 millert 727: ttmove(row, (int) (cp1 - &vvp->v_text[0]));
1.1 deraadt 728: #ifdef STANDOUT_GLITCH
1.2 millert 729: if (vvp->v_color != CTEXT && magic_cookie_glitch > 0) {
1.3 millert 730: if (cp1 < &vvp->v_text[magic_cookie_glitch])
731: cp1 = &vvp->v_text[magic_cookie_glitch];
732: if (cp5 > &vvp->v_text[ncol - magic_cookie_glitch])
733: cp5 = &vvp->v_text[ncol - magic_cookie_glitch];
1.2 millert 734: } else if (magic_cookie_glitch < 0)
1.1 deraadt 735: #endif
736: ttcolor(vvp->v_color);
737: while (cp1 != cp5) {
738: ttputc(*cp1++);
739: ++ttcol;
740: }
1.3 millert 741: if (cp5 != cp3) /* Do erase. */
1.1 deraadt 742: tteeol();
743: #endif
744: }
745:
746: /*
1.3 millert 747: * Redisplay the mode line for the window pointed to by the "wp".
748: * This is the only routine that has any idea of how the modeline is
749: * formatted. You can change the modeline format by hacking at this
750: * routine. Called by "update" any time there is a dirty window. Note
751: * that if STANDOUT_GLITCH is defined, first and last magic_cookie_glitch
752: * characters may never be seen.
753: */
1.5 art 754: void
1.11 vincent 755: modeline(MGWIN *wp)
1.3 millert 756: {
1.6 mickey 757: int n;
1.3 millert 758: BUFFER *bp;
1.12 vincent 759: int mode;
1.3 millert 760:
761: n = wp->w_toprow + wp->w_ntrows; /* Location. */
762: vscreen[n]->v_color = CMODE; /* Mode line color. */
763: vscreen[n]->v_flag |= (VFCHG | VFHBAD); /* Recompute, display. */
764: vtmove(n, 0); /* Seek to right line. */
1.1 deraadt 765: bp = wp->w_bufp;
1.3 millert 766: vtputc('-');
767: vtputc('-');
1.12 vincent 768: if ((bp->b_flag & BFREADONLY) != 0) {
769: vtputc('%');
1.14 vincent 770: if ((bp->b_flag & BFCHG) != 0)
771: vtputc('*');
772: else
773: vtputc('%');
1.12 vincent 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