home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / progc / beav132s.arj / REGION.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-08  |  4.3 KB  |  173 lines

  1. /*
  2. *       Region based commands.
  3. * The routines in this file
  4. * deal with the region, that magic space
  5. * between "." and mark. Some functions are
  6. * commands. Some functions are just for
  7. * internal use.
  8. */
  9. #include    "def.h"
  10.  
  11. bool getregion ();
  12.  
  13. extern    char    MSG_sv_in_b[];
  14. extern    char    MSG_sav_slf[];
  15. extern    char    MSG_no_mark[];
  16. extern    char    MSG_procing[];
  17.  
  18. extern    BUFFER  sav_buf;
  19.  
  20. /*
  21. * Kill the region. Ask "getregion"
  22. * to figure out the bounds of the region.
  23. * Move "." to the start, and kill the characters.
  24. */
  25. char    killregion (f, n, k)
  26. {
  27.     register char   s;
  28.     REGION region;
  29.     int     error;
  30.  
  31.     if ((s = getregion (®ion)) != TRUE)
  32.         return (s);
  33.     if ((lastflag & CFKILL) == 0)/* This is a kill type  */
  34.         bclear (&sav_buf);
  35.     thisflag |= CFKILL;         /* kill buffer stuff.   */
  36.     curwp -> w_dotp = region.r_linep;
  37.     curwp -> w_doto = region.r_offset;
  38.     error = ldelete (region.r_size, TRUE);
  39.     lchange (WFHARD);
  40.     /* cause the save buffer display to be updated if needed */
  41.     if (sav_buf.b_nwnd != 0)
  42.         showsavebuf ();
  43.     writ_echo (okmsg);
  44.     return (error);
  45. }
  46.  
  47.  
  48. /*
  49. * Copy all of the characters in the
  50. * region to the kill buffer. Don't move dot
  51. * at all. This is a bit like a kill region followed
  52. * by a yank.
  53. */
  54. char    copyregion (f, n, k)
  55. {
  56.     register    LINE * linep;
  57.     register int    loffs, j;
  58.     register char   s;
  59.     REGION region;
  60.     char        buf[NCOL], buf1[NCOL];
  61.  
  62.     j = 0;
  63.     if ((s = getregion (®ion)) != TRUE)
  64.         return (s);
  65.     if ((lastflag & CFKILL) == 0)/* Kill type command.   */
  66.         bclear (&sav_buf);
  67.     thisflag |= CFKILL;
  68.     linep = region.r_linep;     /* Current line.    */
  69.     loffs = region.r_offset;    /* Current offset.  */
  70.     while (region.r_size--)
  71.     {
  72.         if ((s = b_append_c (&sav_buf, lgetc (linep, loffs))) != TRUE)
  73.             return (s);
  74.         ++loffs;
  75.         if ((j++ & 0x2ff) == 0)
  76.         {
  77.             sprintf (buf1, MSG_procing, R_POS_FMT(curwp));
  78.             sprintf (buf, buf1, DOT_POS (curwp));
  79.             writ_echo (buf);
  80.             /* check if we should quit */
  81.             if (ttkeyready ())
  82.             {
  83.                 wind_on_dot_all();
  84.                 if (ttgetc () == '*')
  85.                     return (FALSE);
  86.             }
  87.         }
  88.     }
  89.     /* cause the save buffer display to be updated if needed */
  90.     if (sav_buf.b_nwnd != 0)
  91.         showsavebuf ();
  92.     /* update buffer display */
  93.     if ((blistp -> b_nwnd != 0) &&
  94.         (blistp -> b_type == BTLIST))
  95.         listbuffers ();
  96.     writ_echo (okmsg);
  97.     return (TRUE);
  98. }
  99.  
  100. /*
  101. * This routine figures out the bound of the region
  102. * in the current window, and stores the results into the fields
  103. * of the REGION structure. Dot and mark are usually close together,
  104. * but I don't know the order. The size is kept in a long. At the
  105. * end, after the size is figured out, it is assigned to the size
  106. * field of the region structure. If this assignment loses any bits,
  107. * then we print an error. This is "type independent" overflow
  108. * checking. All of the callers of this routine should be ready to
  109. * get an ABORT status, because I might add a "if regions is big,
  110. * ask before clobberring" flag.
  111. */
  112. bool getregion (rp)
  113. register    REGION * rp;
  114. {
  115.     if (curwp -> w_markp == NULL)
  116.     {
  117.         writ_echo (MSG_no_mark);
  118.         return (FALSE);
  119.     }
  120.  
  121.     if (DOT_POS(curwp) < MARK_POS(curwp))
  122.     {
  123.         rp -> r_linep = curwp -> w_dotp;
  124.         rp -> r_offset = curwp -> w_doto;
  125.         rp -> r_size = MARK_POS(curwp) - DOT_POS(curwp);
  126.     }
  127.     else
  128.     {
  129.         rp -> r_linep = curwp -> w_markp;
  130.         rp -> r_offset = curwp -> w_marko;
  131.         rp -> r_size = DOT_POS(curwp) - MARK_POS(curwp);
  132.     }
  133.     return (TRUE);
  134. }
  135.  
  136. /* save some region in a buffer
  137. * (use _usebuffer to handle non-existent buffers)
  138. * hack as it uses kill buffer to transfer stuff (quick and dirty!)
  139. * and doesn't do clever things at all with dot in destination buffer!
  140. */
  141. char    save_region (f, n, k)
  142. {
  143.     char    bufn[NBUFN];
  144.     char    oldbufn[NBUFN];
  145.     register char   s;
  146.  
  147.     if ((s = ereply (MSG_sv_in_b, bufn, NBUFN, NULL)) != TRUE)
  148.         return (s);
  149.  
  150.     if (strcmp (bufn, curbp -> b_bname) == 0)
  151.     {
  152.         writ_echo (MSG_sav_slf);
  153.         return (FALSE);
  154.     }
  155.  
  156.     /* save this name for ughly reversal */
  157.     strcpy (oldbufn, curbp -> b_bname);
  158.  
  159.     /* copy stuff using killbuffer as work space -  hack !! * than move it to
  160.     named place using yank - Quick AND Dirty */
  161.     copyregion (f, n, k);
  162.     _usebuffer (bufn);
  163.     curbp -> b_type = BTSAVE;   /* mark as a saved buffer */
  164.  
  165.     yank (f, n, k);
  166.     _usebuffer (oldbufn);
  167.     writ_echo (okmsg);
  168.     return (TRUE);
  169. }
  170.  
  171.  
  172.