home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / memacs / ue311c.arc / BUFFER.C < prev    next >
C/C++ Source or Header  |  1991-03-11  |  14KB  |  576 lines

  1. /*  BUFFER.C:    buffer mgmt. routines
  2.         MicroEMACS 3.10
  3.  
  4.  * Buffer management.
  5.  * Some of the functions are internal,
  6.  * and some are actually attached to user
  7.  * keys. Like everyone else, they set hints
  8.  * for the display system.
  9.  */
  10. #include    <stdio.h>
  11. #include    "estruct.h"
  12. #include    "eproto.h"
  13. #include    "edef.h"
  14. #include    "elang.h"
  15.  
  16. /*
  17.  * Attach a buffer to a window. The
  18.  * values of dot and mark come from the buffer
  19.  * if the use count is 0. Otherwise, they come
  20.  * from some other window.
  21.  */
  22. PASCAL NEAR usebuffer(f, n)
  23.  
  24. int f,n;    /* prefix flag and argument */
  25.  
  26. {
  27.     register BUFFER *bp;    /* temporary buffer pointer */
  28.  
  29.     /* get the buffer name to switch to */
  30.     bp = getdefb();
  31.     bp = getcbuf(TEXT24, bp ? bp->b_bname : "main", TRUE);
  32. /*                "Use buffer" */
  33.     if (!bp)
  34.         return(ABORT);
  35.  
  36.     /* make it invisable if there is an argument */
  37.     if (f == TRUE)
  38.         bp->b_flag |= BFINVS;
  39.  
  40.     /* switch to it in any case */
  41.     return(swbuffer(bp));
  42. }
  43.  
  44. PASCAL NEAR nextbuffer(f, n)    /* switch to the next buffer in the buffer list */
  45.  
  46. int f, n;    /* default flag, numeric argument */
  47. {
  48.     register BUFFER *bp;    /* current eligable buffer */
  49.     register int status;
  50.  
  51.     /* make sure the arg is legit */
  52.     if (f == FALSE)
  53.         n = 1;
  54.     if (n < 1)
  55.         return(FALSE);
  56.  
  57.     /* cycle thru buffers until n runs out */
  58.     while (n-- > 0) {
  59.         bp = getdefb();
  60.         if (bp == NULL)
  61.             return(FALSE);
  62.         status = swbuffer(bp);
  63.         if (status != TRUE)
  64.             return(status);
  65.     }
  66.     return(status);
  67. }
  68.  
  69. PASCAL NEAR swbuffer(bp)    /* make buffer BP current */
  70.  
  71. BUFFER *bp;
  72.  
  73. {
  74.     register WINDOW *wp;
  75.     SCREEN *scrp;        /* screen to fix pointers in */
  76.     register int cmark;        /* current mark */
  77.  
  78.     /* let a user macro get hold of things...if he wants */
  79.     execkey(&exbhook, FALSE, 1);
  80.  
  81.     if (--curbp->b_nwnd == 0) {        /* Last use.        */
  82.         curbp->b_dotp  = curwp->w_dotp;
  83.         curbp->b_doto  = curwp->w_doto;
  84.         for (cmark = 0; cmark < NMARKS; cmark++) {
  85.             curbp->b_markp[cmark] = curwp->w_markp[cmark];
  86.             curbp->b_marko[cmark] = curwp->w_marko[cmark];
  87.         }
  88.         curbp->b_fcol  = curwp->w_fcol;
  89.     }
  90.     curbp = bp;                /* Switch.        */
  91.     if (curbp->b_active != TRUE) {        /* buffer not active yet*/
  92.         /* read it in and activate it */
  93.         readin(curbp->b_fname, ((curbp->b_mode&MDVIEW) == 0));
  94.         curbp->b_dotp = lforw(curbp->b_linep);
  95.         curbp->b_doto = 0;
  96.         curbp->b_active = TRUE;
  97.     }
  98.     curwp->w_bufp  = bp;
  99.     curwp->w_linep = bp->b_linep;        /* For macros, ignored. */
  100.     curwp->w_flag |= WFMODE|WFFORCE|WFHARD; /* Quite nasty.     */
  101.     if (bp->b_nwnd++ == 0) {        /* First use.        */
  102.         curwp->w_dotp  = bp->b_dotp;
  103.         curwp->w_doto  = bp->b_doto;
  104.         for (cmark = 0; cmark < NMARKS; cmark++) {
  105.             curwp->w_markp[cmark] = bp->b_markp[cmark];
  106.             curwp->w_marko[cmark] = bp->b_marko[cmark];
  107.         }
  108.         curwp->w_fcol  = bp->b_fcol;
  109.     } else {
  110.         /* in all screens.... */
  111.         scrp = first_screen;
  112.         while (scrp) {
  113.             wp = scrp->s_first_window;
  114.             while (wp != NULL) {
  115.                 if (wp!=curwp && wp->w_bufp==bp) {
  116.                     curwp->w_dotp  = wp->w_dotp;
  117.                     curwp->w_doto  = wp->w_doto;
  118.                     for (cmark = 0; cmark < NMARKS; cmark++) {
  119.                         curwp->w_markp[cmark] = wp->w_markp[cmark];
  120.                         curwp->w_marko[cmark] = wp->w_marko[cmark];
  121.                     }
  122.                     curwp->w_fcol  = wp->w_fcol;
  123.                     break;
  124.                 }
  125.                 /* next window */
  126.                 wp = wp->w_wndp;
  127.             }
  128.  
  129.             /* next screen! */
  130.             scrp = scrp->s_next_screen;
  131.         }
  132.     }
  133.  
  134.     /* let a user macro get hold of things...if he wants */
  135.     execkey(&bufhook, FALSE, 1);
  136.  
  137.     return(TRUE);
  138. }
  139.  
  140. /*
  141.  * Dispose of a buffer, by name.
  142.  * Ask for the name. Look it up (don't get too
  143.  * upset if it isn't there at all!). Get quite upset
  144.  * if the buffer is being displayed. Clear the buffer (ask
  145.  * if the buffer has been changed). Then free the header
  146.  * line and the buffer header. Bound to "C-X K".
  147.  */
  148. PASCAL NEAR killbuffer(f, n)
  149.  
  150. int f,n;    /* prefix flag and argument */
  151.  
  152. {
  153.     register BUFFER *bp;    /* ptr to buffer to dump */
  154.  
  155.     /* get the buffer name to kill */
  156.     bp = getdefb();
  157.     bp = getcbuf(TEXT26, bp ? bp->b_bname : "main", TRUE);
  158. /*             "Kill buffer" */
  159.     if (bp == NULL)
  160.         return(ABORT);
  161.  
  162.     return(zotbuf(bp));
  163. }
  164.  
  165. /*    Allow the user to pop up a buffer, like we do.... */
  166.  
  167. PASCAL NEAR popbuffer(f, n)
  168.  
  169. int f, n;    /* default and numeric arguments */
  170.  
  171. {
  172.     register BUFFER *bp;    /* ptr to buffer to dump */
  173.  
  174.     /* get the buffer name to pop */
  175.     bp = getdefb();
  176.     bp = getcbuf(TEXT27, bp ? bp->b_bname : "main", TRUE);
  177. /*             "Pop buffer" */
  178.     if (bp == NULL)
  179.         return(ABORT);
  180.  
  181.     return(pop(bp));
  182. }
  183.  
  184. BUFFER *PASCAL NEAR getdefb()    /* get the default buffer for a use or kill */
  185.  
  186. {
  187.     BUFFER *bp;    /* default buffer */
  188.  
  189.     /* Find the next buffer, which will be the default */
  190.     bp = curbp->b_bufp;
  191.  
  192.     /* cycle through the buffers to find an eligable one */
  193.     while (bp == NULL || bp->b_flag & BFINVS) {
  194.         if (bp == NULL)
  195.             bp = bheadp;
  196.         else
  197.             bp = bp->b_bufp;
  198.  
  199.         /* don't get caught in an infinite loop! */
  200.         if (bp == curbp) {
  201.             bp = NULL;
  202.             break;
  203.         }
  204.     }            
  205.     return(bp);
  206. }
  207.  
  208. PASCAL NEAR zotbuf(bp)    /* kill the buffer pointed to by bp */
  209.  
  210. register BUFFER *bp;
  211.  
  212. {
  213.     register BUFFER *bp1;
  214.     register BUFFER *bp2;
  215.     register int    s;
  216.  
  217.     if (bp->b_nwnd != 0) {            /* Error if on screen.    */
  218.         mlwrite(TEXT28);
  219. /*            "Buffer is being displayed" */
  220.         return(FALSE);
  221.     }
  222.     if ((s=bclear(bp)) != TRUE)        /* Blow text away.    */
  223.         return(s);
  224.     free((char *) bp->b_linep);        /* Release header line. */
  225.     bp1 = NULL;                /* Find the header.    */
  226.     bp2 = bheadp;
  227.     while (bp2 != bp) {
  228.         bp1 = bp2;
  229.         bp2 = bp2->b_bufp;
  230.     }
  231.     bp2 = bp2->b_bufp;            /* Next one in chain.    */
  232.     if (bp1 == NULL)            /* Unlink it.        */
  233.         bheadp = bp2;
  234.     else
  235.         bp1->b_bufp = bp2;
  236.     free((char *) bp);            /* Release buffer block */
  237.     return(TRUE);
  238. }
  239.  
  240. PASCAL NEAR namebuffer(f,n)    /*    Rename the current buffer    */
  241.  
  242. int f, n;        /* default Flag & Numeric arg */
  243.  
  244. {
  245.     register BUFFER *bp;    /* pointer to scan through all buffers */
  246.     char bufn[NBUFN];    /* buffer to hold buffer name */
  247.  
  248.     /* prompt for and get the new buffer name */
  249. ask:    if (mlreply(TEXT29, bufn, NBUFN) != TRUE)
  250. /*            "Change buffer name to: " */
  251.         return(FALSE);
  252.  
  253.     /* and check for duplicates */
  254.     bp = bheadp;
  255.     while (bp != NULL) {
  256.         if (bp != curbp) {
  257.             /* if the names the same */
  258.             if (strcmp(bufn, bp->b_bname) == 0)
  259.                 goto ask;  /* try again */
  260.         }
  261.         bp = bp->b_bufp;    /* onward */
  262.     }
  263.  
  264.     strcpy(curbp->b_bname, bufn);    /* copy buffer name to structure */
  265.     curwp->w_flag |= WFMODE;    /* make mode line replot */
  266.     mlerase();
  267.     return(TRUE);
  268. }
  269.  
  270. /*    Build and popup a buffer containing the list of all buffers.
  271.     Bound to "C-X C-B". A numeric argument forces it to list
  272.     invisable buffers as well.
  273. */
  274.  
  275. PASCAL NEAR listbuffers(f, n)
  276.  
  277. int f,n;    /* prefix flag and argument */
  278.  
  279. {
  280.     register int status;    /* stutus return */
  281.  
  282.     if ((status = makelist(f)) != TRUE)
  283.         return(status);
  284.     return(wpopup(blistp));
  285. }
  286.  
  287. /*
  288.  * This routine rebuilds the
  289.  * text in the special secret buffer
  290.  * that holds the buffer list. It is called
  291.  * by the list buffers command. Return TRUE
  292.  * if everything works. Return FALSE if there
  293.  * is an error (if there is no memory). Iflag
  294.  * indecates weather to list hidden buffers.
  295.  */
  296. PASCAL NEAR makelist(iflag)
  297.  
  298. int iflag;    /* list hidden buffer flag */
  299.  
  300. {
  301.     register char    *cp1;
  302.     register char    *cp2;
  303.     register int    c;
  304.     register BUFFER *bp;
  305.     register LINE    *lp;
  306.     register int    s;
  307.     register int    i;
  308.     long nbytes;        /* # of bytes in current buffer */
  309.     char b[7+1];
  310.     char line[128];
  311.  
  312.     blistp->b_flag &= ~BFCHG;        /* Don't complain!    */
  313.     if ((s=bclear(blistp)) != TRUE)     /* Blow old text away    */
  314.         return(s);
  315.     strcpy(blistp->b_fname, "");
  316.     if (addline(blistp, TEXT30) == FALSE
  317. /*            "ACT   Modes      Size Buffer       File" */
  318.     ||  addline(blistp, "--- --------- ------- --------------- ----") == FALSE)
  319.         return(FALSE);
  320.     bp = bheadp;                /* For all buffers    */
  321.  
  322.     /* build line to report global mode settings */
  323.     cp1 = &line[0];
  324.     *cp1++ = ' ';
  325.     *cp1++ = ' ';
  326.     *cp1++ = ' ';
  327.     *cp1++ = ' ';
  328.  
  329.     /* output the mode codes */
  330.     for (i = 0; i < NUMMODES; i++)
  331.         if (gmode & (1 << i))
  332.             *cp1++ = modecode[i];
  333.         else
  334.             *cp1++ = '.';
  335.     strcpy(cp1, TEXT31);
  336. /*            "          Global Modes" */
  337.     if (addline(blistp, line) == FALSE)
  338.         return(FALSE);
  339.  
  340.     /* output the list of buffers */
  341.     while (bp != NULL) {
  342.         /* skip invisable buffers if iflag is false */
  343.         if (((bp->b_flag&BFINVS) != 0) && (iflag != TRUE)) {
  344.             bp = bp->b_bufp;
  345.             continue;
  346.         }
  347.         cp1 = &line[0];         /* Start at left edge    */
  348.  
  349.         /* output status of ACTIVE flag (has the file been read in? */
  350.         if (bp->b_active == TRUE)    /* "@" if activated       */
  351.             *cp1++ = '@';
  352.         else
  353.             *cp1++ = ' ';
  354.  
  355.         /* output status of changed flag */
  356.         if ((bp->b_flag&BFCHG) != 0)    /* "*" if changed    */
  357.             *cp1++ = '*';
  358.         else
  359.             *cp1++ = ' ';
  360.  
  361.         /* report if the file is truncated */
  362.         if ((bp->b_flag&BFTRUNC) != 0)
  363.             *cp1++ = '#';
  364.         else
  365.             *cp1++ = ' ';
  366.  
  367.         *cp1++ = ' ';    /* space */
  368.  
  369.         /* output the mode codes */
  370.         for (i = 0; i < NUMMODES; i++) {
  371.             if (bp->b_mode & (1 << i))
  372.                 *cp1++ = modecode[i];
  373.             else
  374.                 *cp1++ = '.';
  375.         }
  376.         *cp1++ = ' ';            /* Gap.         */
  377.         nbytes = 0L;            /* Count bytes in buf.    */
  378.         lp = lforw(bp->b_linep);
  379.         while (lp != bp->b_linep) {
  380.             nbytes += (long)llength(lp)+1L;
  381.             lp = lforw(lp);
  382.         }
  383.         long_asc(b, 7, nbytes);         /* 6 digit buffer size. */
  384.         cp2 = &b[0];
  385.         while (*cp2)
  386.             *cp1++ = *cp2++;
  387.         *cp1++ = ' ';            /* Gap.         */
  388.         cp2 = &bp->b_bname[0];        /* Buffer name        */
  389.         while (*cp2)
  390.             *cp1++ = *cp2++;
  391.         *cp1++ = ' ';            /* Gap.         */
  392.         cp2 = &bp->b_fname[0];        /* File name        */
  393.         if (*cp2 != 0) {
  394.             while (cp1 < &line[38])
  395.                 *cp1++ = ' ';
  396.             while (*cp2)
  397.                 *cp1++ = *cp2++;
  398.         }
  399.         *cp1 = 0;          /* Add to the buffer.   */
  400.         if (addline(blistp, line) == FALSE)
  401.             return(FALSE);
  402.         bp = bp->b_bufp;
  403.     }
  404.     return(TRUE);                   /* All done           */
  405. }
  406.  
  407. /* Translate a long to ascii form. Don't trust various systems
  408.    ltoa() routines.. they aren't consistant                */
  409.  
  410. PASCAL NEAR long_asc(buf, width, num)
  411.  
  412. char   buf[];
  413. int    width;
  414. long   num;
  415.  
  416. {
  417.     buf[width] = 0;             /* End of string.    */
  418.     while (num >= 10) {            /* Conditional digits.    */
  419.         buf[--width] = (int)(num%10L) + '0';
  420.         num /= 10L;
  421.     }
  422.     buf[--width] = (int)num + '0';        /* Always 1 digit.    */
  423.     while (width != 0)            /* Pad with blanks.    */
  424.         buf[--width] = ' ';
  425. }
  426.  
  427. /*
  428.  * Look through the list of
  429.  * buffers. Return TRUE if there
  430.  * are any changed buffers. Buffers
  431.  * that hold magic internal stuff are
  432.  * not considered; who cares if the
  433.  * list of buffer names is hacked.
  434.  * Return FALSE if no buffers
  435.  * have been changed.
  436.  */
  437. PASCAL NEAR anycb()
  438. {
  439.     register BUFFER *bp;
  440.  
  441.     bp = bheadp;
  442.     while (bp != NULL) {
  443.         if ((bp->b_flag&BFINVS)==0 && (bp->b_flag&BFCHG)!=0)
  444.             return(TRUE);
  445.         bp = bp->b_bufp;
  446.     }
  447.     return(FALSE);
  448. }
  449.  
  450. /*
  451.  * Find a buffer, by name. Return a pointer
  452.  * to the BUFFER structure associated with it.
  453.  * If the buffer is not found
  454.  * and the "cflag" is TRUE, create it. The "bflag" is
  455.  * the settings for the flags in in buffer.
  456.  */
  457. BUFFER *PASCAL NEAR bfind(bname, cflag, bflag)
  458.  
  459. register char    *bname; /* name of buffer to find */
  460. int cflag;        /* create it if not found? */
  461. int bflag;        /* bit settings for a new buffer */
  462.  
  463. {
  464.     register BUFFER *bp;
  465.     register BUFFER *sb;    /* buffer to insert after */
  466.     register LINE    *lp;
  467.     int cmark;        /* current mark */
  468.  
  469.     bp = bheadp;
  470.     while (bp != NULL) {
  471.         if (strcmp(bname, bp->b_bname) == 0)
  472.             return(bp);
  473.         bp = bp->b_bufp;
  474.     }
  475.     if (cflag != FALSE) {
  476.         if ((bp=(BUFFER *)malloc(sizeof(BUFFER))) == NULL)
  477.             return(NULL);
  478.         if ((lp=lalloc(0)) == NULL) {
  479.             free((char *) bp);
  480.             return(NULL);
  481.         }
  482.         /* find the place in the list to insert this buffer */
  483.         if (bheadp == NULL || strcmp(bheadp->b_bname, bname) > 0) {
  484.             /* insert at the beginning */
  485.             bp->b_bufp = bheadp;
  486.             bheadp = bp;
  487.         } else {
  488.             sb = bheadp;
  489.             while (sb->b_bufp != NULL) {
  490.                 if (strcmp(sb->b_bufp->b_bname, bname) > 0)
  491.                     break;
  492.                 sb = sb->b_bufp;
  493.             }
  494.  
  495.             /* and insert it */
  496.             bp->b_bufp = sb->b_bufp;
  497.             sb->b_bufp = bp;
  498.         }
  499.  
  500.         /* and set up the other buffer fields */
  501.         bp->b_topline = NULL;
  502.         bp->b_botline = NULL;
  503.         bp->b_active = TRUE;
  504.         bp->b_dotp  = lp;
  505.         bp->b_doto  = 0;
  506.         for (cmark = 0; cmark < NMARKS; cmark++) {
  507.             bp->b_markp[cmark] = NULL;
  508.             bp->b_marko[cmark] = 0;
  509.         }
  510.         bp->b_fcol = 0;
  511.         bp->b_flag  = bflag;
  512.         bp->b_mode  = gmode;
  513.         bp->b_nwnd  = 0;
  514.         bp->b_linep = lp;
  515.         strcpy(bp->b_fname, "");
  516.         strcpy(bp->b_bname, bname);
  517. #if    CRYPT
  518.         bp->b_key[0] = 0;
  519. #endif
  520.         lp->l_fp = lp;
  521.         lp->l_bp = lp;
  522.     }
  523.     return(bp);
  524. }
  525.  
  526. /*
  527.  * This routine blows away all of the text
  528.  * in a buffer. If the buffer is marked as changed
  529.  * then we ask if it is ok to blow it away; this is
  530.  * to save the user the grief of losing text. The
  531.  * window chain is nearly always wrong if this gets
  532.  * called; the caller must arrange for the updates
  533.  * that are required. Return TRUE if everything
  534.  * looks good.
  535.  */
  536. PASCAL NEAR bclear(bp)
  537. register BUFFER *bp;
  538. {
  539.     register LINE    *lp;
  540.     register int    s;
  541.     int cmark;        /* current mark */
  542.  
  543.     if ((bp->b_flag&BFINVS) == 0        /* Not scratch buffer.    */
  544.     && (bp->b_flag&BFCHG) != 0        /* Something changed    */
  545.     && (s=mlyesno(TEXT32)) != TRUE)
  546. /*              "Discard changes" */
  547.         return(s);
  548.     bp->b_flag  &= ~BFCHG;            /* Not changed        */
  549.     while ((lp=lforw(bp->b_linep)) != bp->b_linep)
  550.         lfree(lp);
  551.     bp->b_dotp  = bp->b_linep;        /* Fix "."        */
  552.     bp->b_doto  = 0;
  553.     for (cmark = 0; cmark < NMARKS; cmark++) {
  554.         bp->b_markp[cmark] = NULL;  /* Invalidate "mark"    */
  555.         bp->b_marko[cmark] = 0;
  556.     }
  557.     bp->b_fcol = 0;
  558.     return(TRUE);
  559. }
  560.  
  561. PASCAL NEAR unmark(f, n)    /* unmark the current buffers change flag */
  562.  
  563. int f, n;    /* unused command arguments */
  564.  
  565. {
  566.     register WINDOW *wp;
  567.  
  568.     /* unmark the buffer */
  569.     curbp->b_flag &= ~BFCHG;
  570.  
  571.     /* unmark all windows as well */
  572.     upmode();
  573.  
  574.     return(TRUE);
  575. }
  576.