home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / UE311C.ZIP / BUFFER.C < prev    next >
C/C++ Source or Header  |  1991-10-10  |  14KB  |  591 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 : mainbuf, 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 : mainbuf, 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 : mainbuf, 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.     /* we can not kill a displayed buffer */
  218.     if (bp->b_nwnd != 0) {
  219.         mlwrite(TEXT28);
  220. /*            "Buffer is being displayed" */
  221.         return(FALSE);
  222.     }
  223.  
  224.     /* we can not kill an executing buffer */
  225.     if (bp->b_exec != 0) {
  226.         mlwrite(TEXT226);
  227. /*            "%%Can not delete an executing buffer" */
  228.         return(FALSE);
  229.     }
  230.  
  231.     if ((s=bclear(bp)) != TRUE)        /* Blow text away.    */
  232.         return(s);
  233.     free((char *) bp->b_linep);        /* Release header line. */
  234.     bp1 = NULL;                /* Find the header.    */
  235.     bp2 = bheadp;
  236.     while (bp2 != bp) {
  237.         bp1 = bp2;
  238.         bp2 = bp2->b_bufp;
  239.     }
  240.     bp2 = bp2->b_bufp;            /* Next one in chain.    */
  241.     if (bp1 == NULL)            /* Unlink it.        */
  242.         bheadp = bp2;
  243.     else
  244.         bp1->b_bufp = bp2;
  245.     free((char *) bp);            /* Release buffer block */
  246.     return(TRUE);
  247. }
  248.  
  249. PASCAL NEAR namebuffer(f,n)    /*    Rename the current buffer    */
  250.  
  251. int f, n;        /* default Flag & Numeric arg */
  252.  
  253. {
  254.     register BUFFER *bp;    /* pointer to scan through all buffers */
  255.     char bufn[NBUFN];    /* buffer to hold buffer name */
  256.  
  257.     /* prompt for and get the new buffer name */
  258. ask:    if (mlreply(TEXT29, bufn, NBUFN) != TRUE)
  259. /*            "Change buffer name to: " */
  260.         return(FALSE);
  261.  
  262.     /* and check for duplicates */
  263.     bp = bheadp;
  264.     while (bp != NULL) {
  265.         if (bp != curbp) {
  266.             /* if the names the same */
  267.             if (strcmp(bufn, bp->b_bname) == 0)
  268.                 goto ask;  /* try again */
  269.         }
  270.         bp = bp->b_bufp;    /* onward */
  271.     }
  272.  
  273.     strcpy(curbp->b_bname, bufn);    /* copy buffer name to structure */
  274.     upmode();            /* make all mode lines replot */
  275.     mlerase();
  276.     return(TRUE);
  277. }
  278.  
  279. /*    Build and popup a buffer containing the list of all buffers.
  280.     Bound to "C-X C-B". A numeric argument forces it to list
  281.     invisable buffers as well.
  282. */
  283.  
  284. PASCAL NEAR listbuffers(f, n)
  285.  
  286. int f,n;    /* prefix flag and argument */
  287.  
  288. {
  289.     register int status;    /* stutus return */
  290.  
  291.     if ((status = makelist(f)) != TRUE)
  292.         return(status);
  293.     return(wpopup(blistp));
  294. }
  295.  
  296. /*
  297.  * This routine rebuilds the
  298.  * text in the special secret buffer
  299.  * that holds the buffer list. It is called
  300.  * by the list buffers command. Return TRUE
  301.  * if everything works. Return FALSE if there
  302.  * is an error (if there is no memory). Iflag
  303.  * indecates weather to list hidden buffers.
  304.  */
  305. PASCAL NEAR makelist(iflag)
  306.  
  307. int iflag;    /* list hidden buffer flag */
  308.  
  309. {
  310.     register char    *cp1;
  311.     register char    *cp2;
  312.     register int    c;
  313.     register BUFFER *bp;
  314.     register LINE    *lp;
  315.     register int    s;
  316.     register int    i;
  317.     long nbytes;        /* # of bytes in current buffer */
  318.     char b[7+1];
  319.     char line[128];
  320.  
  321.     blistp->b_flag &= ~BFCHG;        /* Don't complain!    */
  322.     if ((s=bclear(blistp)) != TRUE)     /* Blow old text away    */
  323.         return(s);
  324.     strcpy(blistp->b_fname, "");
  325.     if (addline(blistp, TEXT30) == FALSE
  326. /*            "ACT   Modes      Size Buffer       File" */
  327.     ||  addline(blistp, "--- --------- ------- --------------- ----") == FALSE)
  328.         return(FALSE);
  329.     bp = bheadp;                /* For all buffers    */
  330.  
  331.     /* build line to report global mode settings */
  332.     cp1 = &line[0];
  333.     *cp1++ = ' ';
  334.     *cp1++ = ' ';
  335.     *cp1++ = ' ';
  336.     *cp1++ = ' ';
  337.  
  338.     /* output the mode codes */
  339.     for (i = 0; i < NUMMODES; i++)
  340.         if (gmode & (1 << i))
  341.             *cp1++ = modecode[i];
  342.         else
  343.             *cp1++ = '.';
  344.     strcpy(cp1, TEXT31);
  345. /*            "          Global Modes" */
  346.     if (addline(blistp, line) == FALSE)
  347.         return(FALSE);
  348.  
  349.     /* output the list of buffers */
  350.     while (bp != NULL) {
  351.         /* skip invisable buffers if iflag is false */
  352.         if (((bp->b_flag&BFINVS) != 0) && (iflag != TRUE)) {
  353.             bp = bp->b_bufp;
  354.             continue;
  355.         }
  356.         cp1 = &line[0];         /* Start at left edge    */
  357.  
  358.         /* output status of ACTIVE flag (has the file been read in? */
  359.         if (bp->b_active == TRUE)    /* "@" if activated       */
  360.             *cp1++ = '@';
  361.         else
  362.             *cp1++ = ' ';
  363.  
  364.         /* output status of changed flag */
  365.         if ((bp->b_flag&BFCHG) != 0)    /* "*" if changed    */
  366.             *cp1++ = '*';
  367.         else
  368.             *cp1++ = ' ';
  369.  
  370.         /* report if the file is truncated */
  371.         if ((bp->b_flag&BFTRUNC) != 0)
  372.             *cp1++ = '#';
  373.         else
  374.             *cp1++ = ' ';
  375.  
  376.         *cp1++ = ' ';    /* space */
  377.  
  378.         /* output the mode codes */
  379.         for (i = 0; i < NUMMODES; i++) {
  380.             if (bp->b_mode & (1 << i))
  381.                 *cp1++ = modecode[i];
  382.             else
  383.                 *cp1++ = '.';
  384.         }
  385.         *cp1++ = ' ';            /* Gap.         */
  386.         nbytes = 0L;            /* Count bytes in buf.    */
  387.         lp = lforw(bp->b_linep);
  388.         while (lp != bp->b_linep) {
  389.             nbytes += (long)llength(lp)+1L;
  390.             lp = lforw(lp);
  391.         }
  392.         long_asc(b, 7, nbytes);         /* 6 digit buffer size. */
  393.         cp2 = &b[0];
  394.         while (*cp2)
  395.             *cp1++ = *cp2++;
  396.         *cp1++ = ' ';            /* Gap.         */
  397.         cp2 = &bp->b_bname[0];        /* Buffer name        */
  398.         while (*cp2)
  399.             *cp1++ = *cp2++;
  400.         *cp1++ = ' ';            /* Gap.         */
  401.         cp2 = &bp->b_fname[0];        /* File name        */
  402.         if (*cp2 != 0) {
  403.             while (cp1 < &line[38])
  404.                 *cp1++ = ' ';
  405.             while (*cp2)
  406.                 *cp1++ = *cp2++;
  407.         }
  408.         *cp1 = 0;          /* Add to the buffer.   */
  409.         if (addline(blistp, line) == FALSE)
  410.             return(FALSE);
  411.         bp = bp->b_bufp;
  412.     }
  413.     return(TRUE);                   /* All done           */
  414. }
  415.  
  416. /* Translate a long to ascii form. Don't trust various systems
  417.    ltoa() routines.. they aren't consistant                */
  418.  
  419. PASCAL NEAR long_asc(buf, width, num)
  420.  
  421. char   buf[];
  422. int    width;
  423. long   num;
  424.  
  425. {
  426.     buf[width] = 0;             /* End of string.    */
  427.     while (num >= 10) {            /* Conditional digits.    */
  428.         buf[--width] = (int)(num%10L) + '0';
  429.         num /= 10L;
  430.     }
  431.     buf[--width] = (int)num + '0';        /* Always 1 digit.    */
  432.     while (width != 0)            /* Pad with blanks.    */
  433.         buf[--width] = ' ';
  434. }
  435.  
  436. /*
  437.  * Look through the list of
  438.  * buffers. Return TRUE if there
  439.  * are any changed buffers. Buffers
  440.  * that hold magic internal stuff are
  441.  * not considered; who cares if the
  442.  * list of buffer names is hacked.
  443.  * Return FALSE if no buffers
  444.  * have been changed.
  445.  */
  446. PASCAL NEAR anycb()
  447. {
  448.     register BUFFER *bp;
  449.  
  450.     bp = bheadp;
  451.     while (bp != NULL) {
  452.         if ((bp->b_flag&BFINVS)==0 && (bp->b_flag&BFCHG)!=0)
  453.             return(TRUE);
  454.         bp = bp->b_bufp;
  455.     }
  456.     return(FALSE);
  457. }
  458.  
  459. /*
  460.  * Find a buffer, by name. Return a pointer
  461.  * to the BUFFER structure associated with it.
  462.  * If the buffer is not found
  463.  * and the "cflag" is TRUE, create it. The "bflag" is
  464.  * the settings for the flags in in buffer.
  465.  */
  466. BUFFER *PASCAL NEAR bfind(bname, cflag, bflag)
  467.  
  468. register char    *bname; /* name of buffer to find */
  469. int cflag;        /* create it if not found? */
  470. int bflag;        /* bit settings for a new buffer */
  471.  
  472. {
  473.     register BUFFER *bp;
  474.     register BUFFER *sb;    /* buffer to insert after */
  475.     register LINE    *lp;
  476.     int cmark;        /* current mark */
  477.  
  478.     bp = bheadp;
  479.     while (bp != NULL) {
  480.         if (strcmp(bname, bp->b_bname) == 0)
  481.             return(bp);
  482.         bp = bp->b_bufp;
  483.     }
  484.  
  485.     /* no such buffer exists, create it? */
  486.     if (cflag != FALSE) {
  487.  
  488.         /* allocate the needed memory */
  489.         if ((bp=(BUFFER *)malloc(sizeof(BUFFER))) == NULL)
  490.             return(NULL);
  491.         if ((lp=lalloc(0)) == NULL) {
  492.             free((char *) bp);
  493.             return(NULL);
  494.         }
  495.  
  496.         /* find the place in the list to insert this buffer */
  497.         if (bheadp == NULL || strcmp(bheadp->b_bname, bname) > 0) {
  498.             /* insert at the beginning */
  499.             bp->b_bufp = bheadp;
  500.             bheadp = bp;
  501.         } else {
  502.             sb = bheadp;
  503.             while (sb->b_bufp != NULL) {
  504.                 if (strcmp(sb->b_bufp->b_bname, bname) > 0)
  505.                     break;
  506.                 sb = sb->b_bufp;
  507.             }
  508.  
  509.             /* and insert it */
  510.             bp->b_bufp = sb->b_bufp;
  511.             sb->b_bufp = bp;
  512.         }
  513.  
  514.         /* and set up the other buffer fields */
  515.         bp->b_topline = NULL;
  516.         bp->b_botline = NULL;
  517.         bp->b_active = TRUE;
  518.         bp->b_dotp  = lp;
  519.         bp->b_doto  = 0;
  520.         for (cmark = 0; cmark < NMARKS; cmark++) {
  521.             bp->b_markp[cmark] = NULL;
  522.             bp->b_marko[cmark] = 0;
  523.         }
  524.         bp->b_fcol  = 0;
  525.         bp->b_flag  = bflag;
  526.         bp->b_mode  = gmode;
  527.         bp->b_nwnd  = 0;
  528.         bp->b_exec  = 0;
  529.         bp->b_linep = lp;
  530.         strcpy(bp->b_fname, "");
  531.         strcpy(bp->b_bname, bname);
  532. #if    CRYPT
  533.         bp->b_key[0] = 0;
  534. #endif
  535.         lp->l_fp = lp;
  536.         lp->l_bp = lp;
  537.     }
  538.     return(bp);
  539. }
  540.  
  541. /*
  542.  * This routine blows away all of the text
  543.  * in a buffer. If the buffer is marked as changed
  544.  * then we ask if it is ok to blow it away; this is
  545.  * to save the user the grief of losing text. The
  546.  * window chain is nearly always wrong if this gets
  547.  * called; the caller must arrange for the updates
  548.  * that are required. Return TRUE if everything
  549.  * looks good.
  550.  */
  551. PASCAL NEAR bclear(bp)
  552. register BUFFER *bp;
  553. {
  554.     register LINE    *lp;
  555.     register int    s;
  556.     int cmark;        /* current mark */
  557.  
  558.     if ((bp->b_flag&BFINVS) == 0        /* Not scratch buffer.    */
  559.     && (bp->b_flag&BFCHG) != 0        /* Something changed    */
  560.     && (s=mlyesno(TEXT32)) != TRUE)
  561. /*              "Discard changes" */
  562.         return(s);
  563.     bp->b_flag  &= ~BFCHG;            /* Not changed        */
  564.     while ((lp=lforw(bp->b_linep)) != bp->b_linep)
  565.         lfree(lp);
  566.     bp->b_dotp  = bp->b_linep;        /* Fix "."        */
  567.     bp->b_doto  = 0;
  568.     for (cmark = 0; cmark < NMARKS; cmark++) {
  569.         bp->b_markp[cmark] = NULL;  /* Invalidate "mark"    */
  570.         bp->b_marko[cmark] = 0;
  571.     }
  572.     bp->b_fcol = 0;
  573.     return(TRUE);
  574. }
  575.  
  576. PASCAL NEAR unmark(f, n)    /* unmark the current buffers change flag */
  577.  
  578. int f, n;    /* unused command arguments */
  579.  
  580. {
  581.     register WINDOW *wp;
  582.  
  583.     /* unmark the buffer */
  584.     curbp->b_flag &= ~BFCHG;
  585.  
  586.     /* unmark all windows as well */
  587.     upmode();
  588.  
  589.     return(TRUE);
  590. }
  591.