home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 579a.lha / Textra_v1.11 / Scripts / WrapAt.textra < prev   
Text File  |  1991-10-23  |  5KB  |  176 lines

  1.     /*******************************************************************
  2.      *   TEXTRA AREXX script -- Mike Haas, 1991, All Rights Reserved.  *
  3.      * Freely distributable ONLY as a component of the TEXTRA package. *
  4.      * This banner may not be removed or altered (improvements to the  *
  5.      *    actual program welcome).  Please document and send to me.    *
  6.      *        !!! PLACE THIS FILE IN YOUR REXX: DIRECTORY !!!          *
  7.      *******************************************************************/
  8.  
  9. /* WrapAt <num>
  10.  *
  11.  * This routine takes all lines of text that the selection falls on
  12.  * and word-wraps the text at column <num>, 0-based.  This script
  13.  * also left-justifies the text, except for the first line.
  14.  *
  15.  * When lines are joined, they are separated by a space, unless the
  16.  * last character of the topmost of the pair being joined is a period
  17.  * (assumed to mark the end of a sentence).  If this is the case,
  18.  * two spaces follow the period.
  19.  *
  20.  * The <num> argument is limited between 20 and 120 because of the
  21.  * quick & dirty way this scripts checks for stuff.  For example,
  22.  * this script often "joins" lines, and their combined length cannot
  23.  * exceed 255 (TEXTRA limitation), so keeping things under 120 keeps
  24.  * this from happening.
  25.  *
  26.  *                                               Enjoy, Mike Haas
  27.  */
  28.  
  29.  
  30. options results
  31.  
  32. parse arg maxcolumn
  33.  
  34. /* make sure maxcolumn is legal */
  35. if (maxcolumn == "") then
  36.     do
  37.         notify "Usage:   WrapAt <maxcolumn>"
  38.         exit
  39.     end
  40.  
  41. if ((maxcolumn > 120) | (maxcolumn < 20)) then
  42.     do
  43.         notify "WrapAt parameter must be between 20 and 120"
  44.         exit
  45.     end
  46.  
  47. get select position
  48.  
  49. if (result == "NO SELECT") then   /* is nothing selected? */
  50.  
  51.     do
  52.         notify "There MUST be a select range to use the WrapAt script."
  53.         exit
  54.     end
  55.  
  56.  
  57. /* yes, there is a selection, get it's boundaries */
  58. parse var result   startx ' ' starty ' ' endx ' ' endy
  59.  
  60. currx = startx
  61. curry = starty
  62. nomore = 0
  63. bsto = -1
  64.  
  65. /* temporarily make sure auto indent is off */
  66. prefs autoindent read; autoistate = result
  67. prefs autoindent off
  68.  
  69. /* if nothing on the endline is actually selected, don't include it */
  70. if (endx == 0) then  endy = endy - 1
  71.  
  72.  
  73. do while ((curry <= endy) & (nomore == 0))
  74.     do
  75.  
  76.         if (bsto == -1) then do
  77.             gotoxy 0 curry
  78.             currx = 0; nowony = curry
  79.         end
  80.         else do
  81.             currx = bsto; bsto = -1
  82.         end
  83.         
  84.         do while ((currx <= maxcolumn) & (nowony == curry) & (nomore == 0))
  85.             hopselect next word
  86.             if (result == "NOT FOUND") then do
  87.                 nomore = 1
  88.                 gotoxy 1000 curry
  89.             end
  90.             else do
  91.                 get select position
  92.                 parse var result dummy' 'dummy' 'currx' 'nowony
  93.                 gotoxy currx nowony
  94.             end
  95.         end
  96.  
  97.         inbetween = (nowony - 1) - curry
  98.  
  99.         /* delete any blank lines between
  100.         notify "deleting"
  101.         todelete = (nowony - 1) - curry
  102.         if (todelete > 0) then do
  103.            tmp = todelete
  104.            do while (tmp > 0)
  105.               selectline curry+1; del
  106.               tmp = tmp - 1
  107.            end
  108.            endy = endy - todelete
  109.            nowony = nowony - todelete
  110.            gotoxy currx nowony
  111.         end
  112. */
  113.  
  114.         if (currx > maxcolumn) then do
  115.            
  116.            /* line is too long */
  117.            
  118.            hopto prev word;
  119.            get cursor position; parse var result PrevEndsAt' 'dummy
  120.            
  121.            if (PrevEndsAt > maxcolumn) then
  122.                hopto prev word
  123.            
  124.            hopto next word
  125.            newline
  126.            
  127.            endy = endy + 1     /* cause we inserted a newline */
  128.            curry = curry + 1
  129.            nomore = 0
  130.     
  131.         end
  132.         else if (nomore == 0) then do
  133.         
  134.            /* curry is not long enough */
  135.     
  136.            gotoxy 0 nowony
  137.            get cursor char; firstchar = result
  138.            
  139.            if ((curry < endy) & (inbetween == 0)) then do
  140.                backspace
  141.                thechar = " "
  142.                do while ((thechar == " ") | (thechar == "    ") /*TAB*/)
  143.                    do
  144.                        left 1
  145.                        get cursor char; thechar = result
  146.                    end
  147.                end
  148.                right 1
  149.                get cursor char
  150.                do while  ((result == " ") | (result == "    ") /*TAB*/)
  151.                   do
  152.                      del
  153.                      get cursor char
  154.                   end
  155.                end
  156.                if (thechar == ".") then
  157.                   text '"  "'
  158.                else
  159.                   text '" "'
  160.                get cursor position; parse var result bsto' 'nowony
  161.                endy = endy - 1
  162.            end
  163.            else
  164.                curry = nowony
  165.            
  166.         end
  167. /*
  168.         ask "Do it again?"
  169.         if (result == "NO") then exit
  170. */
  171.     end
  172. end
  173.  
  174. /* restore autoindent */
  175. prefs autoindent autoistate
  176.