home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / editor / me / wp.m < prev   
Text File  |  1994-01-31  |  5KB  |  198 lines

  1. /**********************************************************************/
  2. /*                                                                    */
  3. /*  WP - some simple macros to do some word-processing thingies.      */
  4. /*     1) Allows you to reformat a paragraph. This means cramming     */
  5. /*        as many words into a line as possible.                      */
  6. /*     2) Allows to to justify those lines so the last char aligns    */
  7. /*        at the right margin.                                        */
  8. /*                                                                    */
  9. /* Kludged together by Marc Adler/Magma Systems                       */
  10. /*                                                                    */
  11. /**********************************************************************/
  12.  
  13. #define CTRL_C  3
  14. #define ALT_J   164
  15. #define ALT_K   165
  16.  
  17. init()
  18. {
  19.   assign_key("just_para", ALT_J);
  20.   assign_key("reform",    ALT_K);
  21.   assign_key("center",    CTRL_C);
  22. }
  23.  
  24. reform()
  25. {
  26.   int len;
  27.   int margin;
  28.   int oldlineno;
  29.   string buf;
  30.  
  31.   /* prompt the user for the right margin */
  32.   buf = get_tty_str("What is the right margin? ");
  33.   if ((margin = atoi(buf)) <= 0) return;
  34.  
  35.   oldlineno = currlinenum();    /* save the current line number */
  36.  
  37.   /* March down till the end of the paragraph */
  38.   while (!is_line_blank(currline()))
  39.   {
  40.     if ((len = strlen(currline())) == 0)        /* no more chars - bye */
  41.       break;
  42.  
  43.     if (len > margin)                   /* we must split the line */
  44.     {
  45.       setcol(margin);                   /* move to the first word before */
  46.       if (currchar() != ' ')            /*  the right margin */
  47.         prevword();
  48.       if (!is_bol())                     /* move backwards to the first blank */
  49.       {
  50.         left();
  51.         while (!is_bol() && currchar() == ' ')
  52.           left();
  53.         right();
  54.       }
  55.       insert("\n");                     /* split it */
  56.       while (!is_eol() && currchar() == ' ')  /* trim leading blanks */
  57.         delchar();
  58.     }
  59.  
  60.     else if (len <= margin)     /* too short - join next line if we can */
  61.     {
  62.       if (down() && is_line_blank(currline())) break;  /* no next line */
  63.  
  64.       /* trim the blanks off the end of the current line */
  65.       up();
  66.       goeol();
  67.       left();
  68.       while (currchar() == ' ' && currcol() > 1)
  69.       {
  70.         delchar();   left();
  71.       }
  72.       
  73.       /* make sure there's a blank between the last word of this line */
  74.       /* and the first word of the joined line */
  75.       goeol();
  76.       insert(" ");
  77.       delchar();                /* join 'em */
  78.     }
  79.   } /* end while */
  80.  
  81.   /* Go to the original position */
  82.   goline(oldlineno);
  83.   gobol();
  84. }
  85.  
  86.  
  87. /* just_para - right justifies all lines downwards until the end */
  88. /*              of the paragraph */
  89. just_para()
  90. {
  91.   while (!is_line_blank(currline()))
  92.   {
  93.     expand();
  94.     down();
  95.   }
  96. }
  97.  
  98.  
  99. /* expand() - right-justifies a line by inserting spaces between words */
  100. expand()
  101. {
  102.   string text, new;
  103.   int    margin, nextra, nholes, nb;
  104.  
  105.   margin = 65;
  106.  
  107.   squash();                             /* compress multiple blanks */
  108.   text   = currline();
  109.   nholes = wordcount(text) - 1;         /* get the number of gaps   */
  110.   nextra = margin - strlen(text);       /*  and the # of padding blks */
  111.  
  112.   new = "";                             /* 'new' holds the new line */
  113.   gobol();
  114.  
  115.   while (!is_eol() && nholes > 0)
  116.   {
  117.     c = currchar();
  118.     new = strcat(new, chr(c));
  119.     if (c == ' ')
  120.     {
  121.       /* pad with the appropriate number of blanks */
  122.       new = strcat(new, repstr(" ", nb = nextra/nholes));
  123.       nextra = nextra - nb;
  124.       nholes = nholes - 1;
  125.     }
  126.     right();
  127.   }
  128.  
  129.   /* Concat the remainder of the old line to the new line */
  130.   new = strcat(new, substr(text, currcol(), 100));
  131.  
  132.   /* delete the old line and insert the new one */
  133.   gobol();
  134.   deleol();
  135.   insert(new);
  136.   gobol();
  137. }
  138.  
  139.  
  140. /* squash() - compressed sequences of mult. blanks into one blank */
  141. squash()
  142. {
  143.   gobol();                      /* start at the front of the curr line */
  144.  
  145.   while (!is_eol())
  146.   {
  147.     if (currchar() == ' ')
  148.     {
  149.       right();
  150.       while (!is_eol() && currchar() == ' ')  delchar();    /* compress */
  151.     }
  152.     else
  153.       right();
  154.   }
  155.  
  156.   gobol();                      /* go back to the front */
  157. }
  158.  
  159.  
  160. /* Wordcount() - counts the # of words in the string str. Assumes that   */
  161. /*               the string has no multiple blanks (performed by squash) */
  162. wordcount(str)
  163.   string str;
  164. {
  165.   int count, i, len;
  166.  
  167.   count = 1;
  168.   len = strlen(str);
  169.  
  170.   for (i = 1;  i <= len;  i = i + 1)
  171.     if (substr(str, i, 1) == " ")  count = count + 1;
  172.   return count;
  173. }
  174.  
  175. is_line_blank(str)
  176.   string str;
  177. {
  178.   rtrim(str);
  179.   return (str == "");
  180. }
  181.  
  182. /* This macro centers the current line */
  183. center()
  184. {
  185.   string text;
  186.  
  187.   text = currline();
  188.   gobol();         /* get rid of the characters in the line */
  189.   deleol();
  190.   ltrim(text);     /* remove leading & trailing blanks      */
  191.   rtrim(text);
  192.   setcol((80 - strlen(text)) / 2);   /* go to midway point  */
  193.   insert(text);    /* re-insert the text                    */
  194.   gobol();         /* move to the next line down            */
  195.   down();
  196. }
  197.  
  198.