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