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