home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / code / microema.sit / src / display.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-09  |  27.2 KB  |  1,306 lines  |  [TEXT/Earl]

  1. /*
  2.  * The functions in this file handle redisplay. There are two halves, the
  3.  * ones that update the virtual display screen, and the ones that make the
  4.  * physical display screen the same as the virtual display screen. These
  5.  * functions use hints that are left in the windows by the commands.
  6.  *
  7.  */
  8.  
  9. #include    <stdio.h>
  10. #include    "estruct.h"
  11. #include    "edef.h"
  12.  
  13. typedef struct    VIDEO {
  14.     int    v_flag;         /* Flags */
  15. #if    COLOR
  16.     int    v_fcolor;        /* current forground color */
  17.     int    v_bcolor;        /* current background color */
  18.     int    v_rfcolor;        /* requested forground color */
  19.     int    v_rbcolor;        /* requested background color */
  20. #endif
  21.     char    v_text[1];        /* Screen data. */
  22. }    VIDEO;
  23.  
  24. #define VFCHG    0x0001            /* Changed flag         */
  25. #define VFEXT    0x0002            /* extended (beyond column 80)    */
  26. #define VFREV    0x0004            /* reverse video status     */
  27. #define VFREQ    0x0008            /* reverse video request    */
  28. #define VFCOL    0x0010            /* color change requested    */
  29.  
  30. VIDEO    **vscreen;            /* Virtual screen. */
  31. #if    MEMMAP == 0
  32. VIDEO    **pscreen;            /* Physical screen. */
  33. #endif
  34. #if MAC
  35. char *spaces;
  36. #endif
  37.  
  38. /*
  39.  * Initialize the data structures used by the display code. The edge vectors
  40.  * used to access the screens are set up. The operating system's terminal I/O
  41.  * channel is set up. All the other things get initialized at compile time.
  42.  * The original window has "WFCHG" set, so that it will get completely
  43.  * redrawn on the first call to "update".
  44.  */
  45. #define __SEG__ INIT
  46. vtinit()
  47. {
  48.     register int i;
  49.     register VIDEO *vp;
  50.     char *malloc();
  51.  
  52.     TTopen();        /* open the screen */
  53.     TTkopen();        /* open the keyboard */
  54.     TTrev(FALSE);
  55.     vscreen = (VIDEO **) malloc(term.t_mrow*sizeof(VIDEO *));
  56.  
  57.     if (vscreen == NULL)
  58.     exit(1);
  59.  
  60. #if    MEMMAP == 0
  61.     pscreen = (VIDEO **) malloc(term.t_mrow*sizeof(VIDEO *));
  62.  
  63.     if (pscreen == NULL)
  64.     exit(1);
  65. #endif
  66.  
  67.     for (i = 0; i < term.t_mrow; ++i)
  68.     {
  69.     vp = (VIDEO *) malloc(sizeof(VIDEO)+term.t_mcol);
  70.  
  71.     if (vp == NULL)
  72.         exit(1);
  73.  
  74.     vp->v_flag = 0;
  75. #if    COLOR
  76. #if MAC
  77.     vp->v_rfcolor = 0;
  78.     vp->v_rbcolor = 7;
  79. #else
  80.     vp->v_rfcolor = 7;
  81.     vp->v_rbcolor = 0;
  82. #endif
  83. #endif
  84.     vscreen[i] = vp;
  85. #if    MEMMAP == 0
  86.     vp = (VIDEO *) malloc(sizeof(VIDEO)+term.t_mcol);
  87.  
  88.     if (vp == NULL)
  89.         exit(1);
  90.  
  91.     vp->v_flag = 0;
  92.     pscreen[i] = vp;
  93. #endif
  94.     }
  95. #if MAC
  96.     spaces = malloc(term.t_mcol);
  97.     for(i=term.t_mcol;i--;){
  98.         spaces[i] = ' ';
  99.     }
  100. #endif
  101. }
  102. #define __SEG__ display
  103.  
  104. #if    CLEAN
  105. /* free up all the dynamically allocated video structures */
  106.  
  107. vtfree()
  108. {
  109.     int i;
  110.     for (i = 0; i < term.t_mrow; ++i) {
  111.         free(vscreen[i]);
  112. #if    MEMMAP == 0
  113.         free(pscreen[i]);
  114. #endif
  115.     }
  116.     free(vscreen);
  117. #if    MEMMAP == 0
  118.     free(pscreen);
  119. #endif
  120. }
  121. #endif
  122.  
  123. /*
  124.  * Clean up the virtual terminal system, in anticipation for a return to the
  125.  * operating system. Move down to the last line and clear it out (the next
  126.  * system prompt will be written in the line). Shut down the channel to the
  127.  * terminal.
  128.  */
  129. vttidy()
  130. {
  131.     mlerase();
  132.     movecursor(term.t_nrow, 0);
  133.     TTflush();
  134.     TTclose();
  135.     TTkclose();
  136. }
  137.  
  138. /*
  139.  * Set the virtual cursor to the specified row and column on the virtual
  140.  * screen. There is no checking for nonsense values; this might be a good
  141.  * idea during the early stages.
  142.  */
  143. vtmove(row, col)
  144. {
  145.     vtrow = row;
  146.     vtcol = col;
  147. }
  148.  
  149. /* Write a character to the virtual screen. The virtual row and
  150.    column are updated. If we are not yet on left edge, don't print
  151.    it yet. If the line is too long put a "$" in the last column.
  152.    This routine only puts printing characters into the virtual
  153.    terminal buffers. Only column overflow is checked.
  154. */
  155.  
  156. vtputc(c)
  157.  
  158. int c;
  159.  
  160. {
  161.     register VIDEO *vp;    /* ptr to line being updated */
  162.     register short tabs = tabstops;
  163.  
  164.     vp = vscreen[vtrow];
  165.     c &= 0xFF;
  166.  
  167.     if (c == '\t') {
  168.         do {
  169.             vtputc(' ');
  170.         } while (((vtcol + taboff) % tabs) != 0);
  171.     } else if (vtcol >= term.t_ncol) {
  172.         ++vtcol;
  173. #ifndef FINDER
  174.         vp->v_text[term.t_ncol - 1] = '$';
  175. #else
  176.         vp->v_text[term.t_ncol - 1] = 0xA5;    /* big round dot */
  177. #endif
  178.     } else if (c < 0x20 || c == 0x7F) {
  179.         vtputc('^');
  180.         vtputc(c ^ 0x40);
  181.     } else {
  182.         if (vtcol >= 0)
  183.             vp->v_text[vtcol] = c;
  184.         ++vtcol;
  185.     }
  186. }
  187.  
  188. /*
  189.  * Erase from the end of the software cursor to the end of the line on which
  190.  * the software cursor is located.
  191.  */
  192. vteeol()
  193. {
  194.     register VIDEO    *vp;
  195.  
  196.     vp = vscreen[vtrow];
  197. #if MAC
  198.     BlockMove(spaces,&vp->v_text[vtcol],(long)(term.t_ncol-vtcol));
  199.     vtcol = term.t_ncol;
  200. #else
  201.     while (vtcol < term.t_ncol)
  202.     vp->v_text[vtcol++] = ' ';
  203. #endif
  204. }
  205. #if FINDER
  206. /*
  207.  * Functions to scroll a MicroEMACS window one line up or one line down.
  208.  * Used on the Macintosh to support the vertical scroll up and down
  209.  * buttons.  Rearrange the pointers to the virtual and physical screens,
  210.  * then perform the scroll action requested, then update the cursor location.
  211.  */
  212. /*
  213.  * Scroll up a window, insert the correct line at the top, mark the window
  214.  * as reframed. 
  215.  */
  216. vtscrollup()
  217. {
  218.     register VIDEO    *vp,*pp;
  219.     register LINE *lp;
  220.     int i,col,row,width = 0;
  221.     lp = curwp->w_linep;    /* Calculate width of screen in characters to scroll. */
  222.     for(i=curwp->w_ntrows;i--;){
  223.         if(llength(lp) > width)width=llength(lp);
  224.         lp = lforw(lp);
  225.     }
  226.     col = vtcol;                /* Save software cursor position. */
  227.     row = vtrow;
  228.     vtrow = curwp->w_toprow;        /* Move software cursor to top of window. */
  229.     vtcol = 0;
  230.     vp = vscreen[vtrow+curwp->w_ntrows-1];    /* Rearrange virtual and physical screen lines. */
  231.     pp = pscreen[vtrow+curwp->w_ntrows-1];
  232.     for(i=vtrow+curwp->w_ntrows-1;i-->vtrow;){
  233.         vscreen[i+1] = vscreen[i];
  234.         pscreen[i+1] = pscreen[i];
  235.     }
  236.     vscreen[vtrow] = vp;
  237.     pscreen[vtrow] = pp;
  238.     lp = curwp->w_linep;            /* Update the line which was scrolled in. */
  239.     for (i=0;i<llength(lp);i++){
  240.         vtputc(lgetc(lp, i));
  241.     }
  242.     vteeol();
  243.     BlockMove(spaces,&pp->v_text[0],(long)term.t_ncol);
  244.     mac_ttyscroll(vtrow,vtrow+curwp->w_ntrows,1,width);
  245.     updateline(vtrow,vp,pp);        /* Draw the text for the scrolled in line. */
  246.     vtcol = col;                /* Restore software cursor. */
  247.     vtrow = row;
  248.     updpos();                /* Fix up the hardware cursor. */
  249.     movecursor(currow, curcol - lbound);
  250.     curwp->w_flag &= ~WFHARD;        /* Window is all fixed up. */
  251. }
  252. /*
  253.  * Scroll down a window, insert the correct line at the bottom, mark the window
  254.  * as reframed. 
  255.  */
  256. vtscrolldown()
  257. {
  258.     register VIDEO    *vp,*pp;
  259.     register LINE *lp;
  260.     int i,col,row,width = 0;
  261.     lp = curwp->w_linep;
  262.     for(i=curwp->w_ntrows;--i;){
  263.         if(llength(lp) > width)width=llength(lp);
  264.         if (lp != curbp->b_linep) lp = lforw(lp);
  265.     }
  266.     col = vtcol;
  267.     row = vtrow;
  268.     vtrow = curwp->w_toprow+curwp->w_ntrows-1;
  269.     vtcol = 0;
  270.     vp = vscreen[curwp->w_toprow];            /* Save the top row. */
  271.     pp = pscreen[curwp->w_toprow];
  272.     for(i=curwp->w_toprow;i<vtrow;i++){
  273.         vscreen[i] = vscreen[i+1];        /* Bump row pointers up. */
  274.         pscreen[i] = pscreen[i+1];
  275.     }
  276.     vscreen[vtrow] = vp;                /* Put top row back at bottom. */
  277.     pscreen[vtrow] = pp;
  278.     for (i=0;i<llength(lp);i++){
  279.         vtputc(lgetc(lp, i));
  280.     }
  281.     vteeol();
  282.     BlockMove(spaces,&pp->v_text[0],(long)term.t_ncol);
  283.     mac_ttyscroll(curwp->w_toprow,vtrow+1,-1,width);
  284.     updateline(vtrow,vp,pp);
  285.     vtcol = col;
  286.     vtrow = row;
  287.     updpos();
  288.     movecursor(currow, curcol - lbound);
  289.     curwp->w_flag &= ~WFHARD;        /* Window is all fixed up. */
  290. }
  291. #endif
  292. /* upscreen:    user routine to force a screen update
  293.         always finishes complete update     */
  294.  
  295. upscreen(f, n)
  296.  
  297. {
  298.     update(TRUE);
  299.     return(TRUE);
  300. }
  301.  
  302. /*
  303.  * Make sure that the display is right. This is a three part process. First,
  304.  * scan through all of the windows looking for dirty ones. Check the framing,
  305.  * and refresh the screen. Second, make sure that "currow" and "curcol" are
  306.  * correct for the current window. Third, make the virtual and physical
  307.  * screens the same.
  308.  */
  309. update(force)
  310.  
  311. int force;    /* force update past type ahead? */
  312.  
  313. {
  314.     register WINDOW *wp;
  315.  
  316. #if    TYPEAH
  317.     if (force == FALSE && typahead())
  318.         return(TRUE);
  319. #endif
  320. #if    VISMAC == 0
  321.     if (force == FALSE && kbdmode == PLAY)
  322.         return(TRUE);
  323. #endif
  324.  
  325.     /* update any windows that need refreshing */
  326.     wp = wheadp;
  327.     while (wp != NULL) {
  328.         if (wp->w_flag) {
  329.             /* if the window has changed, service it */
  330.             reframe(wp);    /* check the framing */
  331.             if ((wp->w_flag & ~WFMODE) == WFEDIT)
  332.                 updone(wp);    /* update EDITed line */
  333.             else if (wp->w_flag & ~WFMOVE)
  334.                 updall(wp);    /* update all lines */
  335.             if (wp->w_flag & WFMODE)
  336.                 modeline(wp);    /* update modeline */
  337.             wp->w_flag = 0;
  338.             wp->w_force = 0;
  339.         }
  340.         /* on to the next window */
  341.         wp = wp->w_wndp;
  342.     }
  343.  
  344.     /* recalc the current hardware cursor location */
  345.     updpos();
  346.  
  347. #if    MEMMAP
  348.     /* update the cursor and flush the buffers */
  349.     movecursor(currow, curcol - lbound);
  350. #endif
  351.  
  352.     /* check for lines to de-extend */
  353.     upddex();
  354.  
  355.     /* if screen is garbage, re-plot it */
  356.     if (sgarbf != FALSE)
  357.         updgar();
  358.  
  359.     /* update the virtual screen to the physical screen */
  360.     updupd(force);
  361.  
  362.     /* update the cursor and flush the buffers */
  363.     movecursor(currow, curcol - lbound);
  364.     TTflush();
  365.     return(TRUE);
  366. }
  367.  
  368. /*    reframe:    check to see if the cursor is on in the window
  369.  *            and re-frame it if needed or wanted
  370.  *
  371.  *            On the Macintosh, if the window is off by one line
  372.  *            then scroll it so dot is at top or bottom.
  373.  */
  374.  
  375. reframe(wp)
  376.  
  377. WINDOW *wp;
  378.  
  379. {
  380.     register LINE *lp;
  381.     register int i;
  382.  
  383.     /* if not a requested reframe, check for a needed one */
  384.     if ((wp->w_flag & WFFORCE) == 0) {
  385.         lp = wp->w_linep;
  386. #if FINDER
  387.         if(wp->w_flag == WFMOVE){
  388.             if(lback(lp) == wp->w_dotp && lp != lforw(curbp->b_linep)){
  389.                 uEmacs_ScrollLine(1);
  390.                 wp->w_flag &= ~WFMOVE;
  391.                 return TRUE;
  392.             }
  393.         }
  394. #endif
  395.         for (i = 0; i < wp->w_ntrows; i++) {
  396.  
  397.             /* if the line is in the window, no reframe */
  398.             if (lp == wp->w_dotp)
  399.                 return(TRUE);
  400.  
  401.             /* if we are at the end of the file, reframe */
  402.             if (lp == wp->w_bufp->b_linep)
  403.                 break;
  404.  
  405.             /* on to the next line */
  406.             lp = lforw(lp);
  407.         }
  408.     }
  409. #if FINDER
  410.     if(wp->w_flag == WFMOVE){
  411.         if(lp == wp->w_dotp){
  412.             uEmacs_ScrollLine(-1);
  413.             wp->w_flag &= ~WFMOVE;
  414.             return TRUE;
  415.         }
  416.     }
  417. #endif
  418.  
  419.     /* reaching here, we need a window refresh */
  420.     i = wp->w_force;
  421.  
  422.     /* how far back to reframe? */
  423.     if (i > 0) {        /* only one screen worth of lines max */
  424.         if (--i >= wp->w_ntrows)
  425.             i = wp->w_ntrows - 1;
  426.     } else if (i < 0) {    /* negative update???? */
  427.         i += wp->w_ntrows;
  428.         if (i < 0)
  429.             i = 0;
  430.     } else
  431.         i = wp->w_ntrows / 2;
  432.  
  433.     /* backup to new line at top of window */
  434.     lp = wp->w_dotp;
  435.     while (i != 0 && lback(lp) != wp->w_bufp->b_linep) {
  436.         --i;
  437.         lp = lback(lp);
  438.     }
  439.  
  440.     /* and reset the current line at top of window */
  441.     wp->w_linep = lp;
  442.     wp->w_flag |= WFHARD;
  443.     wp->w_flag &= ~WFFORCE;
  444.     return(TRUE);
  445. }
  446.  
  447. /*    updone: update the current line to the virtual screen        */
  448.  
  449. updone(wp)
  450.  
  451. WINDOW *wp;    /* window to update current line in */
  452.  
  453. {
  454.     register LINE *lp;    /* line to update */
  455.     register int sline;    /* physical screen line to update */
  456.     register int i;
  457.  
  458.     /* search down the line we want */
  459.     lp = wp->w_linep;
  460.     sline = wp->w_toprow;
  461.     while (lp != wp->w_dotp) {
  462.         ++sline;
  463.         lp = lforw(lp);
  464.     }
  465.  
  466.     /* and update the virtual line */
  467.     vscreen[sline]->v_flag |= VFCHG;
  468.     vscreen[sline]->v_flag &= ~VFREQ;
  469.     vtmove(sline, 0);
  470.     for (i=0; i < llength(lp); ++i)
  471.         vtputc(lgetc(lp, i));
  472. #if    COLOR
  473.     vscreen[sline]->v_rfcolor = wp->w_fcolor;
  474.     vscreen[sline]->v_rbcolor = wp->w_bcolor;
  475. #endif
  476.     vteeol();
  477. }
  478.  
  479. /*    updall: update all the lines in a window on the virtual screen */
  480.  
  481. updall(wp)
  482.  
  483. WINDOW *wp;    /* window to update lines in */
  484.  
  485. {
  486.     register LINE *lp;    /* line to update */
  487.     register int sline;    /* physical screen line to update */
  488.     register int i;
  489.  
  490.     /* search down the lines, updating them */
  491.     lp = wp->w_linep;
  492.     sline = wp->w_toprow;
  493.     while (sline < wp->w_toprow + wp->w_ntrows) {
  494.  
  495.         /* and update the virtual line */
  496.         vscreen[sline]->v_flag |= VFCHG;
  497.         vscreen[sline]->v_flag &= ~VFREQ;
  498.         vtmove(sline, 0);
  499.         if (lp != wp->w_bufp->b_linep) {
  500.             /* if we are not at the end */
  501.             for (i=0; i < llength(lp); ++i)
  502.                 vtputc(lgetc(lp, i));
  503.             lp = lforw(lp);
  504.         }
  505.  
  506.         /* on to the next one */
  507. #if    COLOR
  508.         vscreen[sline]->v_rfcolor = wp->w_fcolor;
  509.         vscreen[sline]->v_rbcolor = wp->w_bcolor;
  510. #endif
  511.         vteeol();
  512.         ++sline;
  513.     }
  514.  
  515. }
  516.  
  517. /*    updpos: update the position of the hardware cursor and handle extended
  518.         lines. This is the only update for simple moves.    */
  519.  
  520. updpos()
  521.  
  522. {
  523.     register LINE *lp;
  524.     register int c;
  525.     register int i;
  526.     register short tabs = tabstops;
  527.  
  528.     /* find the current row */
  529.     lp = curwp->w_linep;
  530.     currow = curwp->w_toprow;
  531.     while (lp != curwp->w_dotp) {
  532.         ++currow;
  533.         lp = lforw(lp);
  534.     }
  535.  
  536.     /* find the current column */
  537.     curcol = 0;
  538.     i = 0;
  539.     while (i < curwp->w_doto) {
  540.         c = lgetc(lp, i++);
  541.         if (c == '\t')
  542.             curcol = (((curcol / tabs) + 1) * tabs) - 1;
  543.         else
  544.             if ((c & 0xFF) < 0x20 || c == 0x7f)
  545.                 ++curcol;
  546.  
  547.         ++curcol;
  548.     }
  549.  
  550.     /* if extended, flag so and update the virtual line image */
  551.     if (curcol >=  term.t_ncol - 1) {
  552.         vscreen[currow]->v_flag |= (VFEXT | VFCHG);
  553.         updext();
  554.     } else
  555.         lbound = 0;
  556. }
  557.  
  558. /*    upddex: de-extend any line that derserves it        */
  559.  
  560. upddex()
  561.  
  562. {
  563.     register WINDOW *wp;
  564.     register LINE *lp;
  565.     register int i,j;
  566.  
  567.     wp = wheadp;
  568.  
  569.     while (wp != NULL) {
  570.         lp = wp->w_linep;
  571.         i = wp->w_toprow;
  572.  
  573.         while (i < wp->w_toprow + wp->w_ntrows) {
  574.             if (vscreen[i]->v_flag & VFEXT) {
  575.                 if ((wp != curwp) || (lp != wp->w_dotp) ||
  576.                    (curcol < term.t_ncol - 1)) {
  577.                     vtmove(i, 0);
  578.                     for (j = 0; j < llength(lp); ++j)
  579.                         vtputc(lgetc(lp, j));
  580.                     vteeol();
  581.  
  582.                     /* this line no longer is extended */
  583.                     vscreen[i]->v_flag &= ~VFEXT;
  584.                     vscreen[i]->v_flag |= VFCHG;
  585.                 }
  586.             }
  587.             lp = lforw(lp);
  588.             ++i;
  589.         }
  590.         /* and onward to the next window */
  591.         wp = wp->w_wndp;
  592.     }
  593. }
  594.  
  595. /*    updgar: if the screen is garbage, clear the physical screen and
  596.         the virtual screen and force a full update        */
  597.  
  598. updgar()
  599.  
  600. {
  601.     register char *txt;
  602.     register int i,j;
  603.  
  604.     for (i = 0; i < term.t_nrow; ++i) {
  605.         vscreen[i]->v_flag |= VFCHG;
  606. #if    REVSTA
  607.         vscreen[i]->v_flag &= ~VFREV;
  608. #endif
  609. #if    COLOR
  610.         vscreen[i]->v_fcolor = gfcolor;
  611.         vscreen[i]->v_bcolor = gbcolor;
  612. #endif
  613. #if    MEMMAP == 0
  614.         txt = pscreen[i]->v_text;
  615.         for (j = 0; j < term.t_ncol; ++j)
  616.             txt[j] = ' ';
  617. #endif
  618.     }
  619.  
  620.     movecursor(0, 0);         /* Erase the screen. */
  621.     (*term.t_eeop)();
  622.     sgarbf = FALSE;          /* Erase-page clears */
  623.     mpresf = FALSE;          /* the message area. */
  624. #if    COLOR
  625.     mlerase();            /* needs to be cleared if colored */
  626. #endif
  627. }
  628.  
  629. /*    updupd: update the physical screen from the virtual screen    */
  630.  
  631. updupd(force)
  632.  
  633. int force;    /* forced update flag */
  634.  
  635. {
  636.     register VIDEO *vp1;
  637.     register int i;
  638.  
  639.     for (i = 0; i < term.t_nrow; ++i) {
  640.         vp1 = vscreen[i];
  641.  
  642.         /* for each line that needs to be updated*/
  643.         if ((vp1->v_flag & VFCHG) != 0) {
  644. #if    TYPEAH
  645.             if (force == FALSE && typahead())
  646.                 return(TRUE);
  647. #endif
  648. #if    MEMMAP
  649.             updateline(i, vp1);
  650. #else
  651.             updateline(i, vp1, pscreen[i]);
  652. #endif
  653.         }
  654.     }
  655.     return(TRUE);
  656. }
  657.  
  658. /*    updext: update the extended line which the cursor is currently
  659.         on at a column greater than the terminal width. The line
  660.         will be scrolled right or left to let the user see where
  661.         the cursor is
  662.                                 */
  663.  
  664. updext()
  665.  
  666. {
  667.     register int rcursor;    /* real cursor location */
  668.     register LINE *lp;    /* pointer to current line */
  669.     register int j;     /* index into line */
  670.  
  671.     /* calculate what column the real cursor will end up in */
  672.     rcursor = ((curcol - term.t_ncol) % term.t_scrsiz) + term.t_margin;
  673.     taboff = lbound = curcol - rcursor + 1;
  674.  
  675.     /* scan through the line outputing characters to the virtual screen */
  676.     /* once we reach the left edge                    */
  677.     vtmove(currow, -lbound);    /* start scanning offscreen */
  678.     lp = curwp->w_dotp;        /* line to output */
  679.     for (j=0; j<llength(lp); ++j)    /* until the end-of-line */
  680.         vtputc(lgetc(lp, j));
  681.  
  682.     /* truncate the virtual line, restore tab offset */
  683.     vteeol();
  684.     taboff = 0;
  685.  
  686.     /* and put a '$' in column 1 */
  687. #ifndef FINDER
  688.     vscreen[currow]->v_text[0] = '$';
  689. #else
  690.     vscreen[currow]->v_text[0] = 0xA5;
  691. #endif
  692. }
  693.  
  694. /*
  695.  * Update a single line. This does not know how to use insert or delete
  696.  * character sequences; we are using VT52 functionality. Update the physical
  697.  * row and column variables. It does try an exploit erase to end of line. The
  698.  * RAINBOW version of this routine uses fast video.
  699.  */
  700. #if    MEMMAP
  701. /*    UPDATELINE specific code for the IBM-PC and other compatables */
  702.  
  703. updateline(row, vp1)
  704.  
  705. int row;        /* row of screen to update */
  706. struct VIDEO *vp1;    /* virtual screen image */
  707.  
  708. {
  709. #if    COLOR
  710.     scwrite(row, vp1->v_text, vp1->v_rfcolor, vp1->v_rbcolor);
  711.     vp1->v_fcolor = vp1->v_rfcolor;
  712.     vp1->v_bcolor = vp1->v_rbcolor;
  713. #else
  714.     if (vp1->v_flag & VFREQ)
  715.         scwrite(row, vp1->v_text, 0, 7);
  716.     else
  717.         scwrite(row, vp1->v_text, 7, 0);
  718. #endif
  719.     vp1->v_flag &= ~(VFCHG | VFCOL);    /* flag this line as changed */
  720.  
  721. }
  722.  
  723. #else
  724.  
  725. updateline(row, vp1, vp2)
  726.  
  727. int row;        /* row of screen to update */
  728. struct VIDEO *vp1;    /* virtual screen image */
  729. struct VIDEO *vp2;    /* physical screen image */
  730.  
  731. {
  732. #if MAC
  733. /*    UPDATELINE specific code for the Macintosh    */
  734.  
  735.     register char *cp1;
  736.     register char *cp2;
  737.     register char *cp3;
  738.     register char *cp4;
  739.     register int nch,col;
  740.     extern char    *uEmacsTTY_W;
  741.  
  742.     if( (vp1->v_flag & VFREQ) != (vp1->v_flag & VFREV) ){
  743.         TTrev(vp1->v_flag & VFREQ != 0);
  744.     }
  745.     nch = term.t_ncol;
  746.     cp1 = &vp1->v_text[0];
  747.     cp2 = &vp2->v_text[0];
  748.     cp3 = cp1 + nch - 1;
  749.     cp4 = cp2 + nch - 1;
  750.     while(*cp3 == *cp4 && nch > 0){
  751.         --nch;
  752.         --cp3;
  753.         --cp4;
  754.     }
  755.            if(nch != 0){
  756.             col = 0;
  757.             while(*cp1 == *cp2){
  758.                 --nch;
  759.                 ++cp1;
  760.                 ++cp2;
  761.                 ++col;
  762.             }
  763.                 if(nch != 0){
  764.                     TTY_WPutText(row,col,nch,cp1);
  765.                     do{
  766.                         *cp2 = *cp1;
  767.                         ++cp2;
  768.                         ++cp1;
  769.                     }while (nch--);
  770.             }
  771.     }
  772.     vp1->v_flag &= ~VFCHG;
  773.     TTrev(FALSE);
  774. #else
  775. /*    UPDATELINE code for all other versions        */
  776.  
  777.     register char *cp1;
  778.     register char *cp2;
  779.     register char *cp3;
  780.     register char *cp4;
  781.     register char *cp5;
  782.     register int nbflag;    /* non-blanks to the right flag? */
  783.     int rev;        /* reverse video flag */
  784.     int req;        /* reverse video request flag */
  785.  
  786.  
  787.     /* set up pointers to virtual and physical lines */
  788.     cp1 = &vp1->v_text[0];
  789.     cp2 = &vp2->v_text[0];
  790.  
  791. #if    COLOR
  792.     TTforg(vp1->v_rfcolor);
  793.     TTbacg(vp1->v_rbcolor);
  794. #endif
  795.  
  796. #if    REVSTA | COLOR
  797.     /* if we need to change the reverse video status of the
  798.        current line, we need to re-write the entire line     */
  799.     rev = (vp1->v_flag & VFREV) == VFREV;
  800.     req = (vp1->v_flag & VFREQ) == VFREQ;
  801.     if ((rev != req)
  802. #if    COLOR
  803.         || (vp1->v_fcolor != vp1->v_rfcolor) || (vp1->v_bcolor != vp1->v_rbcolor)
  804. #endif
  805. #if    HP150
  806.     /* the HP150 has some reverse video problems */
  807.         || req || rev
  808. #endif
  809.             ) {
  810.         movecursor(row, 0);    /* Go to start of line. */
  811.         /* set rev video if needed */
  812.         if (rev != req)
  813.             (*term.t_rev)(req);
  814.  
  815.         /* scan through the line and dump it to the screen and
  816.            the virtual screen array                */
  817.         cp3 = &vp1->v_text[term.t_ncol];
  818.         while (cp1 < cp3) {
  819.             TTputc(*cp1);
  820.             ++ttcol;
  821.             *cp2++ = *cp1++;
  822.         }
  823.         /* turn rev video off */
  824.         if (rev != req)
  825.             (*term.t_rev)(FALSE);
  826.  
  827.         /* update the needed flags */
  828.         vp1->v_flag &= ~VFCHG;
  829.         if (req)
  830.             vp1->v_flag |= VFREV;
  831.         else
  832.             vp1->v_flag &= ~VFREV;
  833. #if    COLOR
  834.         vp1->v_fcolor = vp1->v_rfcolor;
  835.         vp1->v_bcolor = vp1->v_rbcolor;
  836. #endif
  837.         return(TRUE);
  838.     }
  839. #endif
  840.  
  841.     /* advance past any common chars at the left */
  842.     while (cp1 != &vp1->v_text[term.t_ncol] && cp1[0] == cp2[0]) {
  843.         ++cp1;
  844.         ++cp2;
  845.     }
  846.  
  847. /* This can still happen, even though we only call this routine on changed
  848.  * lines. A hard update is always done when a line splits, a massive
  849.  * change is done, or a buffer is displayed twice. This optimizes out most
  850.  * of the excess updating. A lot of computes are used, but these tend to
  851.  * be hard operations that do a lot of update, so I don't really care.
  852.  */
  853.     /* if both lines are the same, no update needs to be done */
  854.     if (cp1 == &vp1->v_text[term.t_ncol]) {
  855.         vp1->v_flag &= ~VFCHG;        /* flag this line is changed */
  856.         return(TRUE);
  857.     }
  858.  
  859.     /* find out if there is a match on the right */
  860.     nbflag = FALSE;
  861.     cp3 = &vp1->v_text[term.t_ncol];
  862.     cp4 = &vp2->v_text[term.t_ncol];
  863.  
  864.     while (cp3[-1] == cp4[-1]) {
  865.         --cp3;
  866.         --cp4;
  867.         if (cp3[0] != ' ')        /* Note if any nonblank */
  868.             nbflag = TRUE;        /* in right match. */
  869.     }
  870.  
  871.     cp5 = cp3;
  872.  
  873.     /* Erase to EOL ? */
  874.     if (nbflag == FALSE && eolexist == TRUE && (req != TRUE)) {
  875.         while (cp5!=cp1 && cp5[-1]==' ')
  876.             --cp5;
  877.  
  878.         if (cp3-cp5 <= 3)        /* Use only if erase is */
  879.             cp5 = cp3;        /* fewer characters. */
  880.     }
  881.  
  882.     movecursor(row, cp1 - &vp1->v_text[0]); /* Go to start of line. */
  883. #if    REVSTA
  884.     TTrev(rev);
  885. #endif
  886.  
  887.     while (cp1 != cp5) {        /* Ordinary. */
  888.         TTputc(*cp1);
  889.         ++ttcol;
  890.         *cp2++ = *cp1++;
  891.     }
  892.  
  893.     if (cp5 != cp3) {        /* Erase. */
  894.         TTeeol();
  895.         while (cp1 != cp3)
  896.             *cp2++ = *cp1++;
  897.     }
  898. #if    REVSTA
  899.     TTrev(FALSE);
  900. #endif
  901.     vp1->v_flag &= ~VFCHG;        /* flag this line as updated */
  902.     return(TRUE);
  903. #endif
  904. }
  905. #endif
  906.  
  907. /*
  908.  * Redisplay the mode line for the window pointed to by the "wp". This is the
  909.  * only routine that has any idea of how the modeline is formatted. You can
  910.  * change the modeline format by hacking at this routine. Called by "update"
  911.  * any time there is a dirty window.
  912.  */
  913. modeline(wp)
  914.     WINDOW *wp;
  915. {
  916.     register char *cp;
  917.     register int c;
  918.     register int n;        /* cursor position count */
  919.     register BUFFER *bp;
  920.     register int i;        /* loop index */
  921.     register int lchar;     /* character to draw line in buffer with */
  922.     register int firstm;    /* is this the first mode? */
  923.     char tline[NLINE];        /* buffer for part of mode line */
  924.  
  925.     n = wp->w_toprow+wp->w_ntrows;        /* Location. */
  926.     vscreen[n]->v_flag |= VFCHG | VFREQ | VFCOL;/* Redraw next time. */
  927. #if    COLOR
  928.     vscreen[n]->v_rfcolor = 0;            /* black on */
  929.     vscreen[n]->v_rbcolor = 7;            /* white.....*/
  930. #endif
  931.     vtmove(n, 0);                /* Seek to right line. */
  932.     if (wp == curwp)                /* mark the current buffer */
  933. #ifndef FINDER
  934.     lchar = '=';
  935. #else
  936.     lchar = 0xD9;    /* long triple dash */
  937. #endif
  938.     else
  939. #if    REVSTA
  940.     if (revexist)
  941.         lchar = ' ';
  942.     else
  943. #endif
  944. #ifndef FINDER
  945.         lchar = '-';
  946. #else
  947.         lchar = 0xD1;    /* dash */
  948. #endif
  949.  
  950.     bp = wp->w_bufp;
  951.     if ((bp->b_flag&BFTRUNC) != 0)
  952.     vtputc('#');
  953.     else
  954.     vtputc(lchar);
  955.  
  956.     if ((bp->b_flag&BFCHG) != 0)        /* "*" if changed. */
  957.     vtputc('*');
  958.     else
  959.     vtputc(lchar);
  960.  
  961.     n  = 2;
  962.     strcpy(tline, " ");             /* Buffer name. */
  963.     strcat(tline, PROGNAME);
  964.     strcat(tline, " ");
  965.     strcat(tline, VERSION);
  966.     strcat(tline, " (");
  967.  
  968.     /* display the modes */
  969.  
  970.     firstm = TRUE;
  971.     for (i = 0; i < NUMMODES; i++)    /* add in the mode flags */
  972.         if (wp->w_bufp->b_mode & (1 << i)) {
  973.             if (firstm != TRUE)
  974.                 strcat(tline, " ");
  975.             firstm = FALSE;
  976.             strcat(tline, modename[i]);
  977.         }
  978.     strcat(tline,") ");
  979.  
  980.     cp = &tline[0];
  981.     while ((c = *cp++) != 0)
  982.     {
  983.     vtputc(c);
  984.     ++n;
  985.     }
  986.  
  987. #if 0
  988.     vtputc(lchar);
  989.     vtputc((wp->w_flag&WFCOLR) != 0  ? 'C' : lchar);
  990.     vtputc((wp->w_flag&WFMODE) != 0  ? 'M' : lchar);
  991.     vtputc((wp->w_flag&WFHARD) != 0  ? 'H' : lchar);
  992.     vtputc((wp->w_flag&WFEDIT) != 0  ? 'E' : lchar);
  993.     vtputc((wp->w_flag&WFMOVE) != 0  ? 'V' : lchar);
  994.     vtputc((wp->w_flag&WFFORCE) != 0 ? 'F' : lchar);
  995.     vtputc(lchar);
  996.     n += 8;
  997. #endif
  998.  
  999.     vtputc(lchar);
  1000.     vtputc(lchar);
  1001.     vtputc(' ');
  1002.     n += 3;
  1003.     cp = &bp->b_bname[0];
  1004.  
  1005.     while ((c = *cp++) != 0)
  1006.     {
  1007.     vtputc(c);
  1008.     ++n;
  1009.     }
  1010.  
  1011.     vtputc(' ');
  1012.     vtputc(lchar);
  1013.     vtputc(lchar);
  1014.     n += 3;
  1015.  
  1016.     if (bp->b_fname[0] != 0)        /* File name. */
  1017.     {
  1018.     vtputc(' ');
  1019.     ++n;
  1020.     cp = "File: ";
  1021.  
  1022.     while ((c = *cp++) != 0)
  1023.         {
  1024.         vtputc(c);
  1025.         ++n;
  1026.         }
  1027.  
  1028.     cp = &bp->b_fname[0];
  1029.  
  1030.     while ((c = *cp++) != 0)
  1031.         {
  1032.         vtputc(c);
  1033.         ++n;
  1034.         }
  1035.  
  1036.     vtputc(' ');
  1037.     ++n;
  1038.     }
  1039.  
  1040.     while (n < term.t_ncol)        /* Pad to full width. */
  1041.     {
  1042.     vtputc(lchar);
  1043.     ++n;
  1044.     }
  1045. }
  1046.  
  1047. upmode()    /* update all the mode lines */
  1048.  
  1049. {
  1050.     register WINDOW *wp;
  1051.  
  1052.     wp = wheadp;
  1053.     while (wp != NULL) {
  1054.         wp->w_flag |= WFMODE;
  1055.         wp = wp->w_wndp;
  1056.     }
  1057. }
  1058.  
  1059. /*
  1060.  * Send a command to the terminal to move the hardware cursor to row "row"
  1061.  * and column "col". The row and column arguments are origin 0. Optimize out
  1062.  * random calls. Update "ttrow" and "ttcol".
  1063.  */
  1064. movecursor(row, col)
  1065.     {
  1066.     if (row!=ttrow || col!=ttcol)
  1067.     {
  1068.     ttrow = row;
  1069.     ttcol = col;
  1070.     TTmove(row, col);
  1071.     }
  1072.     }
  1073.  
  1074. /*
  1075.  * Erase the message line. This is a special routine because the message line
  1076.  * is not considered to be part of the virtual screen. It always works
  1077.  * immediately; the terminal buffer is flushed via a call to the flusher.
  1078.  */
  1079. mlerase()
  1080.     {
  1081.     int i;
  1082.  
  1083.     movecursor(term.t_nrow, 0);
  1084.     if (discmd == FALSE)
  1085.     return;
  1086.  
  1087. #if    COLOR
  1088. #if MAC
  1089.      TTforg(0);
  1090.      TTbacg(7);
  1091. #else
  1092.      TTforg(7);
  1093.      TTbacg(0);
  1094. #endif
  1095. #endif
  1096.     if (eolexist == TRUE)
  1097.         TTeeol();
  1098.     else {
  1099.     for (i = 0; i < term.t_ncol - 1; i++)
  1100.         TTputc(' ');
  1101.     movecursor(term.t_nrow, 1);    /* force the move! */
  1102.     movecursor(term.t_nrow, 0);
  1103.     }
  1104.     TTflush();
  1105.     mpresf = FALSE;
  1106.     }
  1107.  
  1108. /*
  1109.  * Write a message into the message line. Keep track of the physical cursor
  1110.  * position. A small class of printf like format items is handled. Assumes the
  1111.  * stack grows down; this assumption is made by the "++" in the argument scan
  1112.  * loop. Set the "message line" flag TRUE.
  1113.  */
  1114.  
  1115. mlwrite(fmt, arg)
  1116.  
  1117. char *fmt;    /* format string for output */
  1118. char *arg;    /* pointer to first argument to print */
  1119.  
  1120. {
  1121.     register int c;     /* current char in format string */
  1122.     register char *ap;    /* ptr to current data field */
  1123.  
  1124.     /* if we are not currently echoing on the command line, abort this */
  1125.     if (discmd == FALSE) {
  1126.         movecursor(term.t_nrow, 0);
  1127.         return;
  1128.     }
  1129.  
  1130. #if    COLOR
  1131.     /* set up the proper colors for the command line */
  1132. #if MAC
  1133.     TTforg(0);
  1134.     TTbacg(7);
  1135. #else
  1136.     TTforg(7);
  1137.     TTbacg(0);
  1138. #endif
  1139. #endif
  1140.  
  1141.     /* if we can not erase to end-of-line, do it manually */
  1142.     if (eolexist == FALSE) {
  1143.         mlerase();
  1144.         TTflush();
  1145.     }
  1146.  
  1147.     movecursor(term.t_nrow, 0);
  1148.     ap = (char *) &arg;
  1149.     while ((c = *fmt++) != 0) {
  1150.         if (c != '%') {
  1151.             TTputc(c);
  1152.             ++ttcol;
  1153.         } else {
  1154.             c = *fmt++;
  1155.             switch (c) {
  1156.                 case 'd':
  1157.                     mlputi(*(int *)ap, 10);
  1158.                     ap += sizeof(int);
  1159.                     break;
  1160.  
  1161.                 case 'o':
  1162.                     mlputi(*(int *)ap,  8);
  1163.                     ap += sizeof(int);
  1164.                     break;
  1165.  
  1166.                 case 'x':
  1167.                     mlputi(*(int *)ap, 16);
  1168.                     ap += sizeof(int);
  1169.                     break;
  1170.  
  1171.                 case 'D':
  1172.                     mlputli(*(long *)ap, 10);
  1173.                     ap += sizeof(long);
  1174.                     break;
  1175.  
  1176.                 case 's':
  1177.                     mlputs(*(char **)ap);
  1178.                     ap += sizeof(char *);
  1179.                     break;
  1180.  
  1181.                 case 'f':
  1182.                     mlputf(*(int *)ap);
  1183.                     ap += sizeof(int);
  1184.                     break;
  1185.  
  1186.                 default:
  1187.                     TTputc(c);
  1188.                     ++ttcol;
  1189.             }
  1190.         }
  1191.     }
  1192.  
  1193.     /* if we can, erase to the end of screen */
  1194.     if (eolexist == TRUE)
  1195.         TTeeol();
  1196.     TTflush();
  1197.     mpresf = TRUE;
  1198. }
  1199.  
  1200. /*    Force a string out to the message line regardless of the
  1201.     current $discmd setting. This is needed when $debug is TRUE
  1202.     and for the write-message and clear-message-line commands
  1203. */
  1204.  
  1205. mlforce(s)
  1206.  
  1207. char *s;    /* string to force out */
  1208.  
  1209. {
  1210.     register int oldcmd;    /* original command display flag */
  1211.  
  1212.     oldcmd = discmd;    /* save the discmd value */
  1213.     discmd = TRUE;        /* and turn display on */
  1214.     mlwrite(s);        /* write the string out */
  1215.     discmd = oldcmd;    /* and restore the original setting */
  1216. }
  1217.  
  1218. /*
  1219.  * Write out a string. Update the physical cursor position. This assumes that
  1220.  * the characters in the string all have width "1"; if this is not the case
  1221.  * things will get screwed up a little.
  1222.  */
  1223. mlputs(s)
  1224.     char *s;
  1225.     {
  1226.     register int c;
  1227.  
  1228.     while ((c = *s++) != 0)
  1229.     {
  1230.     TTputc(c);
  1231.     ++ttcol;
  1232.     }
  1233.     }
  1234.  
  1235. /*
  1236.  * Write out an integer, in the specified radix. Update the physical cursor
  1237.  * position.
  1238.  */
  1239. mlputi(i, r)
  1240.     {
  1241.     register int q;
  1242.     static char hexdigits[] = "0123456789ABCDEF";
  1243.  
  1244.     if (i < 0)
  1245.         {
  1246.         i = -i;
  1247.         TTputc('-');
  1248.         }
  1249.  
  1250.     q = i/r;
  1251.  
  1252.     if (q != 0)
  1253.         mlputi(q, r);
  1254.  
  1255.     TTputc(hexdigits[i%r]);
  1256.     ++ttcol;
  1257.     }
  1258.  
  1259. /*
  1260.  * do the same except as a long integer.
  1261.  */
  1262. mlputli(l, r)
  1263.     long l;
  1264.     {
  1265.     register long q;
  1266.  
  1267.     if (l < 0)
  1268.         {
  1269.         l = -l;
  1270.         TTputc('-');
  1271.         }
  1272.  
  1273.     q = l/r;
  1274.  
  1275.     if (q != 0)
  1276.         mlputli(q, r);
  1277.  
  1278.     TTputc((int)(l%r)+'0');
  1279.     ++ttcol;
  1280.     }
  1281.  
  1282. /*
  1283.  *    write out a scaled integer with two decimal places
  1284.  */
  1285.  
  1286. mlputf(s)
  1287.  
  1288. int s;    /* scaled integer to output */
  1289.  
  1290. {
  1291.     int i;    /* integer portion of number */
  1292.     int f;    /* fractional portion of number */
  1293.  
  1294.     /* break it up */
  1295.     i = s / 100;
  1296.     f = s % 100;
  1297.  
  1298.     /* send out the integer portion */
  1299.     mlputi(i, 10);
  1300.     TTputc('.');
  1301.     TTputc((f / 10) + '0');
  1302.     TTputc((f % 10) + '0');
  1303.     ttcol += 3;
  1304. }    
  1305.  
  1306.