home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / jove-4.16-src.tgz / tar.out / bsd / jove / case.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  3KB  |  195 lines

  1. /************************************************************************
  2.  * This program is Copyright (C) 1986-1996 by Jonathan Payne.  JOVE is  *
  3.  * provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is *
  5.  * included in all the files.                                           *
  6.  ************************************************************************/
  7.  
  8. #include "jove.h"
  9. #include "disp.h"
  10. #include "case.h"
  11. #include "jctype.h"
  12. #include "marks.h"
  13. #include "move.h"
  14.  
  15. /* Commands:
  16.  *    CapChar
  17.  *    CapWord
  18.  *    CasRegLower
  19.  *    CasRegUpper
  20.  *    LowWord
  21.  *    UppWord
  22.  */
  23.  
  24. private    bool
  25.     lower proto((char *)),
  26.     upper proto((char *));
  27.  
  28. private void
  29.     CaseReg proto((bool up)),
  30.     case_reg proto((LinePtr line1,int char1,LinePtr line2,int char2,bool up));
  31.  
  32. void
  33. CapChar()
  34. {
  35.     register int    num;
  36.     bool    restore = NO;
  37.     Bufpos    b;
  38.  
  39.     DOTsave(&b);
  40.  
  41.     num = arg_value();
  42.     if (num < 0) {
  43.         restore = YES;
  44.         num = -num;
  45.         b_char(num);    /* Cap previous EXP chars */
  46.     }
  47.     while (num--) {
  48.         if (upper(&linebuf[curchar])) {
  49.             modify();
  50.             makedirty(curline);
  51.         }
  52.         if (eolp()) {
  53.             if (curline->l_next == NULL)
  54.                 break;
  55.             SetLine(curline->l_next);
  56.         } else
  57.             curchar += 1;
  58.     }
  59.     if (restore)
  60.         SetDot(&b);
  61. }
  62.  
  63. void
  64. CapWord()
  65. {
  66.     register int    num,
  67.             restore = NO;
  68.     Bufpos    b;
  69.  
  70.     DOTsave(&b);
  71.     num = arg_value();
  72.     if (num < 0) {
  73.         restore = YES;
  74.         f_word(num);        /* Cap previous EXP words */
  75.         num = -num;
  76.     }
  77.     while (num--) {
  78.         to_word(FORWARD);    /* Go to the beginning of the next word. */
  79.         if (eobp())
  80.             break;
  81.         if (upper(&linebuf[curchar])) {
  82.             modify();
  83.             makedirty(curline);
  84.         }
  85.         curchar += 1;
  86.         while (!eolp() && jisword(linebuf[curchar])) {
  87.             if (lower(&linebuf[curchar])) {
  88.                 modify();
  89.                 makedirty(curline);
  90.             }
  91.             curchar += 1;
  92.         }
  93.     }
  94.     if (restore)
  95.         SetDot(&b);
  96. }
  97.  
  98. private void
  99. case_word(up)
  100. bool    up;
  101. {
  102.     Bufpos    before;
  103.  
  104.     DOTsave(&before);
  105.     ForWord();    /* this'll go backward if negative argument */
  106.     case_reg(before.p_line, before.p_char, curline, curchar, up);
  107. }
  108.  
  109. /* Convert *p to upper case.  Return YES iff it was changed. */
  110.  
  111. private bool
  112. upper(p)
  113. register char    *p;
  114. {
  115.     if (jislower(*p)) {
  116.         *p = CharUpcase(*p);
  117.         return YES;
  118.     }
  119.     return NO;
  120. }
  121.  
  122. /* Convert *p to lower case.  Return YES iff it was changed. */
  123.  
  124. private bool
  125. lower(p)
  126. char    *p;
  127. {
  128.     char c = *p;
  129.  
  130.     if (jisupper(c)) {
  131.         *p = CharDowncase(c);
  132.         return YES;
  133.     }
  134.     return NO;
  135. }
  136.  
  137. private void
  138. case_reg(line1, char1, line2, char2, up)
  139. LinePtr    line1,
  140.     line2;
  141. int    char1,
  142.     char2;
  143. bool    up;
  144. {
  145.     (void) fixorder(&line1, &char1, &line2, &char2);
  146.     DotTo(line1, char1);
  147.  
  148.     for (;;) {
  149.         if (curline == line2 && curchar == char2)
  150.             break;
  151.         if (!eolp())
  152.             if ((up) ? upper(&linebuf[curchar]) : lower(&linebuf[curchar])) {
  153.                 makedirty(curline);
  154.                 modify();
  155.             }
  156.         f_char(1);
  157.     }
  158. }
  159.  
  160. void
  161. CasRegLower()
  162. {
  163.     CaseReg(NO);
  164. }
  165.  
  166. void
  167. CasRegUpper()
  168. {
  169.     CaseReg(YES);
  170. }
  171.  
  172. private void
  173. CaseReg(up)
  174. bool    up;
  175. {
  176.     register Mark    *mp = CurMark();
  177.     Bufpos    savedot;
  178.  
  179.     DOTsave(&savedot);
  180.     case_reg(curline, curchar, mp->m_line, mp->m_char, up);
  181.     SetDot(&savedot);
  182. }
  183.  
  184. void
  185. UppWord()
  186. {
  187.     case_word(YES);
  188. }
  189.  
  190. void
  191. LowWord()
  192. {
  193.     case_word(NO);
  194. }
  195.