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