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