home *** CD-ROM | disk | FTP | other *** search
/ ftp.update.uu.se / ftp.update.uu.se.2014.03.zip / ftp.update.uu.se / pub / rainbow / cpm / emacs / emacssrc.lzh / listbuf.c < prev    next >
C/C++ Source or Header  |  1992-03-11  |  6KB  |  235 lines

  1. #include    "stdio.h"
  2. #include    "ed.h"
  3.  
  4. /*
  5.  * List all of the active
  6.  * buffers. First update the special
  7.  * buffer that holds the list. Next make
  8.  * sure at least 1 window is displaying the
  9.  * buffer list, splitting the screen if this
  10.  * is what it takes. Lastly, repaint all of
  11.  * the windows that are displaying the
  12.  * list. Bound to "C-X C-B".
  13.  */
  14. ovmain(x, f, n)
  15. {
  16. /*    register WINDOW    *wp; */
  17.     register BUFFER    *bp;
  18.     register int    s;
  19.  
  20.     if ( x ) return ( usebuf( x, f, n ));
  21.     if ( ! usebuf( 2, 0, 0 )) return ( 0 );
  22.     if ((s=makelist()) != TRUE)
  23.         return (s);
  24. #ifdef NEVER
  25.     if (blistp->b_nwnd == 0) {        /* Not on screen yet.    */
  26.         if ((wp=wpopup()) == NULL)
  27.             return (FALSE);
  28.         bp = wp->w_bufp;
  29.         if (--bp->b_nwnd == 0) {
  30.             bp->b_dotp  = wp->w_dotp;
  31.             bp->b_doto  = wp->w_doto;
  32.             bp->b_markp = wp->w_markp;
  33.             bp->b_marko = wp->w_marko;
  34.         }
  35.         wp->w_bufp  = blistp;
  36.         ++blistp->b_nwnd;
  37.     }
  38. #endif
  39. #ifdef NEVER
  40.     while (wp != NULL)
  41.     {    if (wp->w_bufp == blistp)
  42.         {
  43.             wp->w_linep =
  44.             wp->w_dotp  = lforw(blistp->b_linep);
  45.             wp->w_markp =
  46.             wp->w_doto  =
  47.             wp->w_marko = 0;
  48.             wp->w_flag |= WFMODE|WFHARD;
  49.         }
  50.         wp = wp->w_wndp;
  51.     }
  52. #endif
  53.     return (TRUE);
  54. }
  55.  
  56. /*
  57.  * This routine rebuilds the
  58.  * text in the special secret buffer
  59.  * that holds the buffer list. It is called
  60.  * by the list buffers command. Return TRUE
  61.  * if everything works. Return FALSE if there
  62.  * is an error (if there is no memory).
  63.  */
  64. makelist()
  65. {
  66.     register char    *cp1;
  67.     register char    *cp2;
  68.     register int    c;
  69.     register BUFFER    *bp;
  70.     register LINE    *lp;
  71.     register int    nbytes;
  72.     register int    s;
  73.     register int    type;
  74.     char        b[6+1];
  75.     char        line[128];
  76.     int nbuff;
  77.  
  78.     nbuff = 0;
  79.     blistp->b_flag &= ~BFCHG;        /* Don't complain!    */
  80.     if ((s=bclear(blistp)) != TRUE)        /* Blow old text away    */
  81.         return (s);
  82.     blistp->b_fname[0] = 0;
  83.     if (addline("    C   Size Buffer           File") == FALSE
  84.     ||  addline("    -   ---- ------           ----") == FALSE)
  85.         return (FALSE);
  86.     bp = bheadp;                /* For all buffers    */
  87.     while (bp != NULL)
  88.     {    if ( bp->b_flag & BFTEMP )
  89.         {    bp = bp->b_bufp;    /* Skip magic ones.    */
  90.             continue;
  91.         }
  92.         cp1 = &line[-1];        /* Start at left edge    */
  93.         cp2 = &b[0];
  94.         itoa( cp2, 3, ++nbuff );    /* 6 digit buffer size.    */
  95.         while (*++cp1 = *cp2++);
  96.         *cp1 = ' ';            /* Gap.            */
  97.         *++cp1 = ( bp->b_flag & BFCHG ) ? '*' : ' ';
  98.             /* "*" if changed    */
  99.         *++cp1 = ' ';            /* Gap.            */
  100.         nbytes = 0;            /* Count bytes in buf.    */
  101.         lp = lforw(bp->b_linep);
  102.         while (lp != bp->b_linep)
  103.         {    nbytes += llength(lp)+1;
  104.             lp = lforw(lp);
  105.         }
  106.         cp2 = &b[0];
  107.         itoa( cp2, 6, nbytes );        /* 6 digit buffer size.    */
  108.         while (*++cp1 = *cp2++);
  109.         *cp1 = ' ';            /* Gap.            */
  110.         cp2 = &bp->b_bname[0];        /* Buffer name        */
  111.         while (*++cp1 = *cp2++);
  112.         cp2 = &bp->b_fname[0];        /* File name        */
  113.         if (*cp2 != 0) {
  114.             while (cp1 < &line[4+1+1+6+1+NBUFN+1])
  115.                 *cp1++ = ' ';        
  116.             while ( *cp1++ = *cp2++ );
  117.         }
  118.         *cp1 = 0;
  119.         if (addline(line) == FALSE)
  120.             return (FALSE);
  121.         bp = bp->b_bufp;
  122.     }
  123.     return (TRUE);                /* All done        */
  124. }
  125.  
  126. /*
  127.  * The argument "text" points to
  128.  * a string. Append this line to the
  129.  * buffer list buffer. Handcraft the EOL
  130.  * on the end. Return TRUE if it worked and
  131.  * FALSE if you ran out of room.
  132.  */
  133. addline(text)
  134. char    *text;
  135. {
  136.     register LINE    *lp;
  137.     register int    ntext;
  138.  
  139.     ntext = strlen(text);
  140.     if ((lp=lalloc(ntext)) == NULL)
  141.         return (FALSE);
  142.     blockmv( &lp->l_text[0], text, ntext );
  143. /****    for (i=0; i<ntext; ++i) ****/
  144. /****        lputc(lp, i, text[i]); ****/
  145.     ( lp->l_bp = blistp->b_linep->l_bp )->l_fp = lp;
  146.     ( lp->l_fp = blistp->b_linep )->l_bp = lp;
  147.     if (blistp->b_dotp == blistp->b_linep)    /* If "." is at the end    */
  148.         blistp->b_dotp = lp;        /* move it to new line    */
  149.     return (TRUE);
  150. }
  151. itoa(buf, width, num)
  152. register char    buf[];
  153. register int    width;
  154. register int    num;
  155. {
  156.     buf[width] = 0;                /* End of string.    */
  157.     clear( buf, width, ' ' );
  158.     while (num >= 10) {            /* Conditional digits.    */
  159.         buf[--width] = (num%10) + '0';
  160.         num /= 10;
  161.     }
  162.     buf[--width] = num + '0';        /* Always 1 digit.    */
  163. #ifdef NEVER
  164.     while (width != 0)            /* Pad with blanks.    */
  165.         buf[--width] = ' ';
  166. #endif
  167. }
  168.  
  169. /*
  170.  * Attach a buffer to a window. The
  171.  * values of dot and mark come from the buffer
  172.  * if the use count is 0. Otherwise, they come
  173.  * from some other window.
  174.  */
  175. usebuf( x, f, n)
  176. {
  177.     register BUFFER    *bp;
  178.     register WINDOW    *wp;
  179.     register int    s;
  180.     char        bufn[NBUFN];
  181.  
  182.     if ( x == 1 )
  183.     {    /* "next buffer" */
  184.         bp = curbp->b_bufp;
  185.         if ( bp == NULL ) bp = bheadp;
  186.     }
  187.     else if ( x == 2 )
  188.     {    if ( ( bp = bfind( "[List]", TRUE, BFTEMP )) == NULL )
  189.             return ( 0 );
  190.     }
  191.     else
  192.     {    /* normal usebuffer() routine. */
  193.         if ((s=mlreply("Use buffer: ", bufn, NBUFN)) != TRUE)
  194.             return (s);
  195.         if ((bp=bfind(bufn, TRUE, 0)) == NULL)
  196.             return (FALSE);
  197.     }
  198.     if (--curbp->b_nwnd == 0) {        /* Last use.        */
  199. /****        curbp->b_dotp  = curwp->w_dotp; ****/
  200. /****        curbp->b_doto  = curwp->w_doto; ****/
  201. /****        curbp->b_markp = curwp->w_markp; ****/
  202. /****        curbp->b_marko = curwp->w_marko; ****/
  203.         blockmv( curbp, curwp, 8 );
  204.     }
  205.     curbp = bp;                /* Switch.        */
  206.     curwp->w_bufp  = bp;
  207.     curwp->w_linep = bp->b_linep;        /* For macros, ignored.    */
  208.     curwp->w_flag |= WFMODE|WFFORCE|WFHARD;    /* Quite nasty.        */
  209.     if (bp->b_nwnd++ == 0) {        /* First use.        */
  210.         blockmv( curwp, curbp, 8 );
  211. #ifdef NEVER
  212.         curwp->w_dotp  = bp->b_dotp;
  213.         curwp->w_doto  = bp->b_doto;
  214.         curwp->w_markp = bp->b_markp;
  215.         curwp->w_marko = bp->b_marko;
  216. #endif
  217.         return (TRUE);
  218.     }
  219.     wp = wheadp;                /* Look for old.    */
  220.     while (wp != NULL) {
  221.         if (wp!=curwp && wp->w_bufp==bp) {
  222.             blockmv( curwp, curbp, 8 );
  223. #ifdef NEVER
  224.             curwp->w_dotp  = wp->w_dotp;
  225.             curwp->w_doto  = wp->w_doto;
  226.             curwp->w_markp = wp->w_markp;
  227.             curwp->w_marko = wp->w_marko;
  228. #endif
  229.             break;
  230.         }
  231.         wp = wp->w_wndp;
  232.     }
  233.     return (TRUE);
  234. }
  235.