home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d3xx / d352 / mg.lha / MG / src.LZH / mg / word.c < prev   
C/C++ Source or Header  |  1990-05-23  |  6KB  |  257 lines

  1. /*
  2.  * Word mode commands. The routines in this file implement commands that work
  3.  * word at a time. There are all sorts of word mode commands.
  4.  */
  5. #include    "def.h"
  6. #include    "line.h"
  7. #include    "window.h"
  8.  
  9. /*
  10.  * Move the cursor backward by "n" words. All of the details of motion are
  11.  * performed by the "backchar" and "forwchar" routines.
  12.  */
  13. /* ARGSUSED */
  14. backword(f, n)
  15. {
  16.     if (n < 0)
  17.         return forwword(f | FFRAND, -n);
  18.     if (backchar(FFRAND, 1) == FALSE)
  19.         return FALSE;
  20.     while (n--) {
  21.         while (inword() == FALSE) {
  22.             if (backchar(FFRAND, 1) == FALSE)
  23.                 return TRUE;
  24.         }
  25.         while (inword() != FALSE) {
  26.             if (backchar(FFRAND, 1) == FALSE)
  27.                 return TRUE;
  28.         }
  29.     }
  30.     return forwchar(FFRAND, 1);
  31. }
  32.  
  33. /*
  34.  * Move the cursor forward by the specified number of words. All of the
  35.  * motion is done by "forwchar".
  36.  */
  37. /* ARGSUSED */
  38. forwword(f, n)
  39. {
  40.     if (n < 0)
  41.         return backword(f | FFRAND, -n);
  42.     while (n--) {
  43.         while (inword() == FALSE) {
  44.             if (forwchar(FFRAND, 1) == FALSE)
  45.                 return TRUE;
  46.         }
  47.         while (inword() != FALSE) {
  48.             if (forwchar(FFRAND, 1) == FALSE)
  49.                 return TRUE;
  50.         }
  51.     }
  52.     return TRUE;
  53. }
  54.  
  55. /*
  56.  * Move the cursor forward by the specified number of words. As you move,
  57.  * convert any characters to upper case.
  58.  */
  59. /* ARGSUSED */
  60. upperword(f, n)
  61. {
  62.     register int    c;
  63.  
  64.     if (n < 0)
  65.         backword(f | FFRAND, -n);
  66.     while (n--) {
  67.         while (inword() == FALSE) {
  68.             if (forwchar(FFRAND, 1) == FALSE)
  69.                 return TRUE;
  70.         }
  71.         while (inword() != FALSE) {
  72.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  73.             if (ISLOWER(c) != FALSE) {
  74.                 c = TOUPPER(c);
  75.                 lputc(curwp->w_dotp, curwp->w_doto, c);
  76.                 lchange(WFHARD);
  77.             }
  78.             if (forwchar(FFRAND, 1) == FALSE)
  79.                 return TRUE;
  80.         }
  81.     }
  82.     return TRUE;
  83. }
  84.  
  85. /*
  86.  * Move the cursor forward by the specified number of words. As you move
  87.  * convert characters to lower case.
  88.  */
  89. /* ARGSUSED */
  90. lowerword(f, n)
  91. {
  92.     register int    c;
  93.  
  94.     if (n < 0) {
  95.         n = -n;
  96.         backword(f | FFRAND, n);
  97.     }
  98.     while (n--) {
  99.         while (inword() == FALSE) {
  100.             if (forwchar(FFRAND, 1) == FALSE)
  101.                 return TRUE;
  102.         }
  103.         while (inword() != FALSE) {
  104.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  105.             if (ISUPPER(c) != FALSE) {
  106.                 c = TOLOWER(c);
  107.                 lputc(curwp->w_dotp, curwp->w_doto, c);
  108.                 lchange(WFHARD);
  109.             }
  110.             if (forwchar(FFRAND, 1) == FALSE)
  111.                 return TRUE;
  112.         }
  113.     }
  114.     return TRUE;
  115. }
  116.  
  117. /*
  118.  * Move the cursor forward by the specified number of words. As you move
  119.  * convert the first character of the word to upper case, and subsequent
  120.  * characters to lower case. Error if you try and move past the end of the
  121.  * buffer.
  122.  */
  123. /* ARGSUSED */
  124. capword(f, n)
  125. {
  126.     register int    c;
  127.     VOID            lchange();
  128.  
  129.     if (n < 0) {
  130.         n = -n;
  131.         backword(f | FFRAND, n);
  132.     }
  133.     while (n--) {
  134.         while (inword() == FALSE) {
  135.             if (forwchar(FFRAND, 1) == FALSE)
  136.                 return TRUE;
  137.         }
  138.         if (inword() != FALSE) {
  139.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  140.             if (ISLOWER(c) != FALSE) {
  141.                 c = TOUPPER(c);
  142.                 lputc(curwp->w_dotp, curwp->w_doto, c);
  143.                 lchange(WFHARD);
  144.             }
  145.             if (forwchar(FFRAND, 1) == FALSE)
  146.                 return TRUE;
  147.             while (inword() != FALSE) {
  148.                 c = lgetc(curwp->w_dotp, curwp->w_doto);
  149.                 if (ISUPPER(c) != FALSE) {
  150.                     c = TOLOWER(c);
  151.                     lputc(curwp->w_dotp, curwp->w_doto, c);
  152.                     lchange(WFHARD);
  153.                 }
  154.                 if (forwchar(FFRAND, 1) == FALSE)
  155.                     return TRUE;
  156.             }
  157.         }
  158.     }
  159.     return TRUE;
  160. }
  161.  
  162. /*
  163.  * Kill forward by "n" words.
  164.  */
  165. /* ARGSUSED */
  166. delfword(f, n)
  167. {
  168.     register RSIZE  size;
  169.     register struct line *dotp;
  170.     register int    doto;
  171.  
  172.     if (n < 0)
  173.         return delbword(f | FFRAND, -n);
  174.     if ((lastflag & CFKILL) == 0)    /* Purge kill buffer.     */
  175.         kdelete();
  176.     thisflag |= CFKILL;
  177.     dotp = curwp->w_dotp;
  178.     doto = curwp->w_doto;
  179.     size = 0;
  180.     while (n--) {
  181.         while (inword() == FALSE) {
  182.             if (forwchar(FFRAND, 1) == FALSE)
  183.                 goto out;    /* Hit end of buffer.     */
  184.             ++size;
  185.         }
  186.         while (inword() != FALSE) {
  187.             if (forwchar(FFRAND, 1) == FALSE)
  188.                 goto out;    /* Hit end of buffer.     */
  189.             ++size;
  190.         }
  191.     }
  192. out:
  193.     curwp->w_dotp = dotp;
  194.     curwp->w_doto = doto;
  195.     return (fdelete(size, TRUE));
  196. }
  197.  
  198. /*
  199.  * Kill backwards by "n" words. The rules for success and failure are now
  200.  * different, to prevent strange behavior at the start of the buffer. The
  201.  * command only fails if something goes wrong with the actual delete of the
  202.  * characters. It is successful even if no characters are deleted, or if you
  203.  * say delete 5 words, and there are only 4 words left. I considered making
  204.  * the first call to "backchar" special, but decided that that would just be
  205.  * wierd. Normally this is bound to "M-Rubout" and to "M-Backspace".
  206.  */
  207. /* ARGSUSED */
  208. delbword(f, n)
  209. {
  210.     register RSIZE  size;
  211.     register struct line *dotp;
  212.     register int    doto;
  213.  
  214.     if (n < 0)
  215.         return delfword(f | FFRAND, -n);
  216.     if ((lastflag & CFKILL) == 0)    /* Purge kill buffer.     */
  217.         kdelete();
  218.     thisflag |= CFKILL;
  219.     dotp = curwp->w_dotp;
  220.     doto = curwp->w_doto;
  221.     size = 0;
  222.     if (backchar(FFRAND, 1) == FALSE)
  223.         return (TRUE);    /* Hit buffer start.     */
  224.     size = 1;        /* One deleted.         */
  225.     while (n--) {
  226.         while (inword() == FALSE) {
  227.             if (backchar(FFRAND, 1) == FALSE)
  228.                 goto out;    /* Hit buffer start.     */
  229.             ++size;
  230.         }
  231.         while (inword() != FALSE) {
  232.             if (backchar(FFRAND, 1) == FALSE)
  233.                 goto out;    /* Hit buffer start.     */
  234.             ++size;
  235.         }
  236.     }
  237.     if (forwchar(FFRAND, 1) == FALSE)
  238.         return FALSE;
  239.     --size;            /* Undo assumed delete. */
  240. out:
  241.     curwp->w_dotp = dotp;
  242.     curwp->w_doto = doto;
  243.     return bdelete(size, TRUE);
  244. }
  245.  
  246. /*
  247.  * Return TRUE if the character at dot is a character that is considered to
  248.  * be part of a word. The word character list is hard coded. Should be
  249.  * setable.
  250.  */
  251. inword()
  252. {
  253.     /* can't use lgetc in ISWORD due to bug in OSK cpp */
  254.     return curwp->w_doto != llength(curwp->w_dotp) &&
  255.         ISWORD(ltext(curwp->w_dotp)[curwp->w_doto]);
  256. }
  257.