home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / newemacs / word.c < prev   
Text File  |  1985-05-10  |  6KB  |  299 lines

  1. /*
  2.  * The routines in this file
  3.  * implement commands that work word at
  4.  * a time. There are all sorts of word mode
  5.  * commands. If I do any sentence and/or paragraph
  6.  * mode commands, they are likely to be put in
  7.  * this file.
  8.  */
  9. #include    <stdio.h>
  10. #include    "ed.h"
  11.  
  12.  
  13. /* Word wrap on n-spaces.
  14.  * Back-over whatever precedes the point on the current line and
  15.  * stop on the first word-break or the beginning of the line.
  16.  * If we reach the beginning of the line, jump back to the end of the
  17.  * word and start a new line.  Otherwise, break the line at the
  18.  * word-break, eat it, and jump back to the end of the word.
  19.  *    NOTE:  This function may leaving trailing blanks.
  20.  * Returns TRUE on success, FALSE on errors.
  21.  */
  22. wrapword(n)
  23. int n;
  24. {
  25.     register int cnt;
  26.         register LINE *oldp;
  27.     oldp = curwp->w_dotp;
  28.     cnt = -1;
  29.     do {                
  30.         cnt++;
  31.         if (! backchar(NULL, 1))
  32.             return(FALSE);
  33.     }
  34.     while (! inword());
  35.     if (! backword(NULL, 1))
  36.         return(FALSE);
  37.     if (oldp == curwp->w_dotp && curwp->w_doto) {
  38.         if (! backdel(NULL, 1))
  39.             return(FALSE);
  40.         if (! insmodenewlin(NULL, 1))
  41.             return(FALSE);
  42.     }
  43.     return(forwword(NULL, 1) && forwchar(NULL, cnt));
  44. }
  45.                 
  46. /*
  47.  * Move the cursor backward by
  48.  * "n" words. All of the details of motion
  49.  * are performed by the "backchar" and "forwchar"
  50.  * routines. Error if you try to move beyond
  51.  * the buffers.
  52.  */
  53. backword(f, n)
  54. {
  55.     if (n < 0)
  56.         return (forwword(f, -n));
  57.     if (!backchar(FALSE, 1))
  58.         return (FALSE);
  59.     while (n--) {
  60.         while (!inword()) {
  61.             if (!backchar(FALSE, 1))
  62.                 return (FALSE);
  63.         }
  64.         while (inword()) {
  65.             if (!backchar(FALSE, 1))
  66.                 return (FALSE);
  67.         }
  68.     }
  69.     return (forwchar(FALSE, 1));
  70. }
  71.  
  72. /*
  73.  * Move the cursor forward by
  74.  * the specified number of words. All of the
  75.  * motion is done by "forwchar". Error if you
  76.  * try and move beyond the buffer's end.
  77.  */
  78. forwword(f, n)
  79. {
  80.     if (n < 0)
  81.         return (backword(f, -n));
  82.     while (n--) {
  83.         while (!inword()) {
  84.             if (!forwchar(FALSE, 1))
  85.                 return (FALSE);
  86.         }
  87.         while (inword()) {
  88.             if (!forwchar(FALSE, 1))
  89.                 return (FALSE);
  90.         }
  91.     }
  92.     return (TRUE);
  93. }
  94.  
  95. /*
  96.  * Move the cursor forward by
  97.  * the specified number of words. As you move,
  98.  * convert any characters to upper case. Error
  99.  * if you try and move beyond the end of the
  100.  * buffer. Bound to "M-U".
  101.  */
  102. upperword(f, n)
  103. {
  104.     register int    c;
  105.  
  106.     if (n < 0)
  107.         return (FALSE);
  108.     while (n--) {
  109.         while (!inword()) {
  110.             if (!forwchar(FALSE, 1))
  111.                 return (FALSE);
  112.         }
  113.         while (inword()) {
  114.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  115.             if (c>='a' && c<='z') {
  116.                 c -= 'a'-'A';
  117.                 lputc(curwp->w_dotp, curwp->w_doto, c);
  118.                 lchange(WFHARD);
  119.             }
  120.             if (!forwchar(FALSE, 1))
  121.                 return (FALSE);
  122.         }
  123.     }
  124.     return (TRUE);
  125. }
  126.  
  127. /*
  128.  * Move the cursor forward by
  129.  * the specified number of words. As you move
  130.  * convert characters to lower case. Error if you
  131.  * try and move over the end of the buffer.
  132.  * Bound to "M-L".
  133.  */
  134. lowerword(f, n)
  135. {
  136.     register int    c;
  137.  
  138.     if (n < 0)
  139.         return (FALSE);
  140.     while (n--) {
  141.         while (!inword()) {
  142.             if (!forwchar(FALSE, 1))
  143.                 return (FALSE);
  144.         }
  145.         while (inword()) {
  146.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  147.             if (c>='A' && c<='Z') {
  148.                 c += 'a'-'A';
  149.                 lputc(curwp->w_dotp, curwp->w_doto, c);
  150.                 lchange(WFHARD);
  151.             }
  152.             if (!forwchar(FALSE, 1))
  153.                 return (FALSE);
  154.         }
  155.     }
  156.     return (TRUE);
  157. }
  158.  
  159. /*
  160.  * Move the cursor forward by
  161.  * the specified number of words. As you move
  162.  * convert the first character of the word to upper
  163.  * case, and subsequent characters to lower case. Error
  164.  * if you try and move past the end of the buffer.
  165.  * Bound to "M-C".
  166.  */
  167. capword(f, n)
  168. {
  169.     register int    c;
  170.  
  171.     if (n < 0)
  172.         return (FALSE);
  173.     while (n--) {
  174.         while (!inword()) {
  175.             if (!forwchar(FALSE, 1))
  176.                 return (FALSE);
  177.         }
  178.         if (inword()) {
  179.             c = lgetc(curwp->w_dotp, curwp->w_doto);
  180.             if (c>='a' && c<='z') {
  181.                 c -= 'a'-'A';
  182.                 lputc(curwp->w_dotp, curwp->w_doto, c);
  183.                 lchange(WFHARD);
  184.             }
  185.             if (!forwchar(FALSE, 1))
  186.                 return (FALSE);
  187.             while (inword()) {
  188.                 c = lgetc(curwp->w_dotp, curwp->w_doto);
  189.                 if (c>='A' && c<='Z') {
  190.                     c += 'a'-'A';
  191.                     lputc(curwp->w_dotp, curwp->w_doto, c);
  192.                     lchange(WFHARD);
  193.                 }
  194.                 if (!forwchar(FALSE, 1))
  195.                     return (FALSE);
  196.             }
  197.         }
  198.     }
  199.     return (TRUE);
  200. }
  201.  
  202. /*
  203.  * Kill forward by "n" words.
  204.  * Remember the location of dot. Move forward
  205.  * by the right number of words. Put dot back where
  206.  * it was and issue the kill command for the
  207.  * right number of characters. Bound to "M-D".
  208.  */
  209. delfword(f, n)
  210. {
  211.     register int    size;
  212.     register LINE    *dotp;
  213.     register int    doto;
  214.  
  215.     if (n < 0)
  216.         return (FALSE);
  217.     dotp = curwp->w_dotp;
  218.     doto = curwp->w_doto;
  219.     size = 0;
  220.     while (n--) {
  221.         while (!inword()) {
  222.             if (!forwchar(FALSE, 1))
  223.                 return (FALSE);
  224.             ++size;
  225.         }
  226.         while (inword()) {
  227.             if (!forwchar(FALSE, 1))
  228.                 return (FALSE);
  229.             ++size;
  230.         }
  231.     }
  232.     curwp->w_dotp = dotp;
  233.     curwp->w_doto = doto;
  234.     return (ldelete(size, TRUE));
  235. }
  236.  
  237. /*
  238.  * Kill backwards by "n" words.
  239.  * Move backwards by the desired number of
  240.  * words, counting the characters. When dot is
  241.  * finally moved to its resting place, fire off
  242.  * the kill command. Bound to "M-Rubout" and
  243.  * to "M-Backspace".
  244.  */
  245. delbword(f, n)
  246. {
  247.     register int    size;
  248.  
  249.     if (n < 0)
  250.         return (FALSE);
  251.     if (!backchar(FALSE, 1))
  252.         return (FALSE);
  253.     size = 0;
  254.     while (n--) {
  255.         while (!inword()) {
  256.             if (!backchar(FALSE, 1))
  257.                 return (FALSE);
  258.             ++size;
  259.         }
  260.         while (inword()) {
  261.             if (!backchar(FALSE, 1))
  262.                 return (FALSE);
  263.             ++size;
  264.         }
  265.     }
  266.     if (!forwchar(FALSE, 1))
  267.         return (FALSE);
  268.     return (ldelete(size, TRUE));
  269. }
  270.  
  271. /*
  272.  * Return TRUE if the character at dot
  273.  * is a character that is considered to be
  274.  * part of a word. The word character list is hard
  275.  * coded. Should be setable.
  276.  */
  277. inword()
  278. {
  279.     register int    c;
  280.  
  281.     if (curwp->w_doto == llength(curwp->w_dotp))
  282.         return (FALSE);
  283.     c = lgetc(curwp->w_dotp, curwp->w_doto);
  284.     if (c>='a' && c<='z')
  285.         return (TRUE);
  286.     if (c>='A' && c<='Z')
  287.         return (TRUE);
  288.     if (c>='0' && c<='9')
  289.         return (TRUE);
  290.  
  291. /* For identifiers    */
  292.  
  293. /*
  294.     if (c=='$' || c=='_')
  295.         return (TRUE);
  296. */
  297.     return (FALSE);
  298. }
  299.