home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2872 / region.c < prev    next >
C/C++ Source or Header  |  1991-02-27  |  5KB  |  178 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 setsize ();
  12. bool getregion ();
  13.  
  14.  
  15. extern    char    MSG_reg_lrg[];
  16. extern    char    MSG_sv_in_b[];
  17. extern    char    MSG_sav_slf[];
  18. extern    char    MSG_no_mark[];
  19.  
  20. #include    "lintfunc.dec"
  21. /*
  22. * Kill the region. Ask "getregion"
  23. * to figure out the bounds of the region.
  24. * Move "." to the start, and kill the characters.
  25. */
  26. char    killregion (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.         kdelete ();             /* clean out k-buffer   */
  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.     writ_echo (okmsg);
  41.     return (error);
  42. }
  43.  
  44.  
  45. /*
  46. * Copy all of the characters in the
  47. * region to the kill buffer. Don't move dot
  48. * at all. This is a bit like a kill region followed
  49. * by a yank.
  50. */
  51. char    copyregion (f, n, k)
  52. {
  53.     register    LINE * linep;
  54.     register int    loffs;
  55.     register char   s;
  56.     REGION region;
  57.  
  58.     if ((s = getregion (®ion)) != TRUE)
  59.         return (s);
  60.     if ((lastflag & CFKILL) == 0)/* Kill type command.   */
  61.         kdelete ();
  62.     thisflag |= CFKILL;
  63.     linep = region.r_linep;     /* Current line.    */
  64.     loffs = region.r_offset;    /* Current offset.  */
  65.     while (region.r_size--)
  66.         {
  67.         if (loffs == llength (linep))
  68.             {
  69.         /* End of line.     */
  70.             if ((s = kinsert ('\n')) != TRUE)
  71.                 return (s);
  72.             linep = lforw (linep);
  73.             loffs = 0;
  74.             }
  75.         else
  76.             {
  77.         /* Middle of line.  */
  78.             if ((s = kinsert (lgetc (linep, loffs))) != TRUE)
  79.                 return (s);
  80.             ++loffs;
  81.             }
  82.         }
  83.     writ_echo (okmsg);
  84.     return (TRUE);
  85. }
  86.  
  87. /*
  88. * This routine figures out the bound of the region
  89. * in the current window, and stores the results into the fields
  90. * of the REGION structure. Dot and mark are usually close together,
  91. * but I don't know the order. The size is kept in a long. At the
  92. * end, after the size is figured out, it is assigned to the size
  93. * field of the region structure. If this assignment loses any bits,
  94. * then we print an error. This is "type independent" overflow
  95. * checking. All of the callers of this routine should be ready to
  96. * get an ABORT status, because I might add a "if regions is big,
  97. * ask before clobberring" flag.
  98. */
  99. bool getregion (rp)
  100. register    REGION * rp;
  101. {
  102.     if (curwp -> w_markp == NULL)
  103.         {
  104.         writ_echo (MSG_no_mark);
  105.         return (FALSE);
  106.         }
  107.  
  108.     if (DOT_POS(curwp) < MARK_POS(curwp))
  109.         {
  110.         rp -> r_linep = curwp -> w_dotp;
  111.         rp -> r_offset = curwp -> w_doto;
  112.         rp -> r_size = (int)(MARK_POS(curwp) - DOT_POS(curwp));
  113.         }
  114.     else
  115.         {
  116.         rp -> r_linep = curwp -> w_markp;
  117.         rp -> r_offset = curwp -> w_marko;
  118.         rp -> r_size = (int)(DOT_POS(curwp) - MARK_POS(curwp));
  119.         }
  120.     return (TRUE);
  121. }
  122.  
  123. /*
  124. * Set size, and check for overflow.
  125. */
  126. bool setsize (rp, size)
  127. register    REGION * rp;
  128. register long   size;
  129. {
  130.     rp -> r_size = size;
  131.     if (rp -> r_size != size)
  132.         {
  133.         writ_echo (MSG_reg_lrg);
  134.         return (FALSE);
  135.         }
  136.     return (TRUE);
  137. }
  138.  
  139.  
  140. /* save some region in a buffer
  141. * (use _usebuffer to handle non-existent buffers)
  142. * hack as it uses kill buffer to transfer stuff (quick and dirty!)
  143. * and doesn't do clever things at all with dot in destination buffer!
  144. */
  145. char    save_region (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_flag |= BFSAV;   /* mark as a saved buffer */
  168.  
  169.     yank (f, n, k);
  170.     kdelete ();                 /* clean out kill buffer */
  171.     _usebuffer (oldbufn);
  172.     writ_echo (okmsg);
  173.     return (TRUE);
  174. }
  175.  
  176.  
  177.