home *** CD-ROM | disk | FTP | other *** search
/ Carousel Volume 2 #1 / carousel.iso / mactosh / code / microema.sit / src / region.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-09  |  5.0 KB  |  213 lines  |  [TEXT/Earl]

  1. /*
  2.  * The routines in this file
  3.  * deal with the region, that magic space
  4.  * between "." and mark. Some functions are
  5.  * commands. Some functions are just for
  6.  * internal use.
  7.  */
  8. #include    <stdio.h>
  9. #include    "estruct.h"
  10. #include    "edef.h"
  11.  
  12. #if    MEGAMAX & ST520
  13. overlay "region"
  14. #endif
  15.  
  16. /*
  17.  * Kill the region. Ask "getregion"
  18.  * to figure out the bounds of the region.
  19.  * Move "." to the start, and kill the characters.
  20.  * Bound to "C-W".
  21.  */
  22. killregion(f, n)
  23. {
  24.     register int    s;
  25.     REGION        region;
  26.  
  27.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if  */
  28.         return(rdonly());    /* we are in read only mode    */
  29.     if ((s=getregion(®ion)) != TRUE)
  30.         return (s);
  31.     if ((lastflag&CFKILL) == 0)        /* This is a kill type    */
  32.         kdelete();            /* command, so do magic */
  33.     thisflag |= CFKILL;            /* kill buffer stuff.    */
  34.     curwp->w_dotp = region.r_linep;
  35.     curwp->w_doto = region.r_offset;
  36.     return (ldelete(region.r_size, TRUE));
  37. }
  38.  
  39. /*
  40.  * Copy all of the characters in the
  41.  * region to the kill buffer. Don't move dot
  42.  * at all. This is a bit like a kill region followed
  43.  * by a yank. Bound to "M-W".
  44.  */
  45. copyregion(f, n)
  46. {
  47.     register LINE    *linep;
  48.     register int    loffs;
  49.     register int    s;
  50.     REGION        region;
  51.  
  52.     if ((s=getregion(®ion)) != TRUE)
  53.         return (s);
  54.     if ((lastflag&CFKILL) == 0)        /* Kill type command.    */
  55.         kdelete();
  56.     thisflag |= CFKILL;
  57.     linep = region.r_linep;         /* Current line.    */
  58.     loffs = region.r_offset;        /* Current offset.    */
  59.     while (region.r_size--) {
  60.         if (loffs == llength(linep)) {    /* End of line.     */
  61.             if ((s=kinsert(EOLCHAR)) != TRUE)
  62.                 return (s);
  63.             linep = lforw(linep);
  64.             loffs = 0;
  65.         } else {            /* Middle of line.    */
  66.             if ((s=kinsert(lgetc(linep, loffs))) != TRUE)
  67.                 return (s);
  68.             ++loffs;
  69.         }
  70.     }
  71.     mlwrite("[region copied]");
  72.     return (TRUE);
  73. }
  74.  
  75. /*
  76.  * Lower case region. Zap all of the upper
  77.  * case characters in the region to lower case. Use
  78.  * the region code to set the limits. Scan the buffer,
  79.  * doing the changes. Call "lchange" to ensure that
  80.  * redisplay is done in all buffers. Bound to
  81.  * "C-X C-L".
  82.  */
  83. lowerregion(f, n)
  84. {
  85.     register LINE    *linep;
  86.     register int    loffs;
  87.     register int    c;
  88.     register int    s;
  89.     REGION        region;
  90.  
  91.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if  */
  92.         return(rdonly());    /* we are in read only mode    */
  93.     if ((s=getregion(®ion)) != TRUE)
  94.         return (s);
  95.     lchange(WFHARD);
  96.     linep = region.r_linep;
  97.     loffs = region.r_offset;
  98.     while (region.r_size--) {
  99.         if (loffs == llength(linep)) {
  100.             linep = lforw(linep);
  101.             loffs = 0;
  102.         } else {
  103.             c = lgetc(linep, loffs);
  104.             if (c>='A' && c<='Z')
  105.                 lputc(linep, loffs, c+'a'-'A');
  106.             ++loffs;
  107.         }
  108.     }
  109.     return (TRUE);
  110. }
  111.  
  112. /*
  113.  * Upper case region. Zap all of the lower
  114.  * case characters in the region to upper case. Use
  115.  * the region code to set the limits. Scan the buffer,
  116.  * doing the changes. Call "lchange" to ensure that
  117.  * redisplay is done in all buffers. Bound to
  118.  * "C-X C-L".
  119.  */
  120. upperregion(f, n)
  121. {
  122.     register LINE    *linep;
  123.     register int    loffs;
  124.     register int    c;
  125.     register int    s;
  126.     REGION        region;
  127.  
  128.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if  */
  129.         return(rdonly());    /* we are in read only mode    */
  130.     if ((s=getregion(®ion)) != TRUE)
  131.         return (s);
  132.     lchange(WFHARD);
  133.     linep = region.r_linep;
  134.     loffs = region.r_offset;
  135.     while (region.r_size--) {
  136.         if (loffs == llength(linep)) {
  137.             linep = lforw(linep);
  138.             loffs = 0;
  139.         } else {
  140.             c = lgetc(linep, loffs);
  141.             if (c>='a' && c<='z')
  142.                 lputc(linep, loffs, c-'a'+'A');
  143.             ++loffs;
  144.         }
  145.     }
  146.     return (TRUE);
  147. }
  148.  
  149. /*
  150.  * This routine figures out the
  151.  * bounds of the region in the current window, and
  152.  * fills in the fields of the "REGION" structure pointed
  153.  * to by "rp". Because the dot and mark are usually very
  154.  * close together, we scan outward from dot looking for
  155.  * mark. This should save time. Return a standard code.
  156.  * Callers of this routine should be prepared to get
  157.  * an "ABORT" status; we might make this have the
  158.  * conform thing later.
  159.  */
  160. getregion(rp)
  161. register REGION *rp;
  162. {
  163.     register LINE    *flp;
  164.     register LINE    *blp;
  165.     long fsize;
  166.     long bsize;
  167.  
  168.     if (curwp->w_markp == NULL) {
  169.         mlwrite("No mark set in this window");
  170.         return (FALSE);
  171.     }
  172.     if (curwp->w_dotp == curwp->w_markp) {
  173.         rp->r_linep = curwp->w_dotp;
  174.         if (curwp->w_doto < curwp->w_marko) {
  175.             rp->r_offset = curwp->w_doto;
  176.             rp->r_size = (long)(curwp->w_marko-curwp->w_doto);
  177.         } else {
  178.             rp->r_offset = curwp->w_marko;
  179.             rp->r_size = (long)(curwp->w_doto-curwp->w_marko);
  180.         }
  181.         return (TRUE);
  182.     }
  183.     blp = curwp->w_dotp;
  184.     bsize = (long)curwp->w_doto;
  185.     flp = curwp->w_dotp;
  186.     fsize = (long)(llength(flp)-curwp->w_doto+1);
  187.     while (flp!=curbp->b_linep || lback(blp)!=curbp->b_linep) {
  188.         if (flp != curbp->b_linep) {
  189.             flp = lforw(flp);
  190.             if (flp == curwp->w_markp) {
  191.                 rp->r_linep = curwp->w_dotp;
  192.                 rp->r_offset = curwp->w_doto;
  193.                 rp->r_size = fsize+curwp->w_marko;
  194.                 return (TRUE);
  195.             }
  196.             fsize += llength(flp)+1;
  197.         }
  198.         if (lback(blp) != curbp->b_linep) {
  199.             blp = lback(blp);
  200.             bsize += llength(blp)+1;
  201.             if (blp == curwp->w_markp) {
  202.                 rp->r_linep = blp;
  203.                 rp->r_offset = curwp->w_marko;
  204.                 rp->r_size = bsize - curwp->w_marko;
  205.                 return (TRUE);
  206.             }
  207.         }
  208.     }
  209.     mlwrite("Bug: lost mark");
  210.     return (FALSE);
  211. }
  212.  
  213.