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