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