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