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