home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / text / textra / scripts / paragraph.textra < prev    next >
Text File  |  1995-02-27  |  7KB  |  258 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. /* Usage:  Paragraph <1st-line-indent> <left-margin> <right-margin>
  10.  *
  11.  * Paragraph is a general purpose formatting script.  It operates on
  12.  * the current SELECT range and accepts 3 arguments, all required...
  13.  *
  14.  * <1st-line-indent> - the amount to indent the FIRST line of
  15.  *                     each paragraph (counted from leftmost column)
  16.  *
  17.  * <left-margin> - the amount to indent the rest of each paragraph
  18.  *
  19.  * <right-margin> - the 'word wrap' point for all lines of all
  20.  *                  selected paragraphs
  21.  *
  22.  * Paragraphs are recognized as single-spaced blocks of text which
  23.  * are separated by a blank line.
  24.  *
  25.  * NOTE: Paragraph.textra supercedes the earlier WrapAt.textra script.
  26.  *       This script does all that one did plus more.
  27.  *
  28.  */
  29.  
  30. /* 00001 mdh 10-nov-92  Added ability to cancel script */
  31. /* 00002 mdh 20-nov-92  check version (cause of 'CheckCancel') */
  32.  
  33. OPTIONS results
  34.  
  35. rex = 0; result = "NOTSUPPORTED"    /*00002*/
  36. textraversion
  37. parse var result maj min rex
  38. if (result == "NOTSUPPORTED") | (rex < 3) then do
  39.     notify "Textra V1.13 or later required for this script."
  40.     exit
  41. end
  42.  
  43.  
  44. parse arg numIndent leftMargin rightMargin   /* get the arguments */
  45.  
  46. if ((numIndent == "") | (leftMargin == "") | (rightMargin == "")) then do
  47.     notify "Usage:  Paragraph <1st-line-indent> <left-margin> <right-margin>"
  48.     exit
  49. end
  50.  
  51. /* make sure rightMargin is legal */
  52.  
  53. if ((rightMargin > 120) | (rightMargin <= leftMargin)) then
  54.     do
  55.         notify "Right-Margin must > Left-Margin AND < 120"
  56.         exit
  57.     end
  58.  
  59. get select position
  60.  
  61. if (result == "NO SELECT") then   /* is nothing selected? */
  62.  
  63.     do
  64.         notify "There MUST be a select range to use the Paragraph script."
  65.         exit
  66.     end
  67.  
  68.  
  69. /* yes, there is a selection, get it's boundaries */
  70. parse var result   startx ' ' starty ' ' endx ' ' endy
  71.  
  72. currx = startx
  73. curry = starty
  74. nomore = 0
  75. bsto = -1
  76.  
  77. /* temporarily make sure auto indent is off */
  78. prefs autoindent read; autoistate = result
  79. prefs autoindent off
  80.  
  81. /* if nothing on the endline is actually selected, don't include it */
  82. if (endx == 0) then  endy = endy - 1
  83.  
  84. gotoxy 0 curry
  85. get cursor char
  86. do while ((result == " ") | (result == "    ") /*TAB*/)
  87.    del
  88.    get cursor char
  89. end
  90. do for (numIndent)
  91.     text '" "'
  92. end
  93.  
  94. do while ((curry <= endy) & (nomore == 0))
  95.  
  96.     CheckCancel; if (result == CANCEL) then exit   /*00001*/
  97.  
  98.     do
  99.  
  100.         if (bsto == -1) then do
  101.             gotoxy 0 curry
  102.             currx = 0; nowony = curry
  103.         end
  104.         else do
  105.             currx = bsto; bsto = -1
  106.         end
  107.         
  108.         gotoxy rightMargin nowony
  109.         get cursor char; rmchar = result
  110.         left 1
  111.         get cursor char; rmprevchar = result
  112.         right 1
  113.         if ((rmchar ~= -1) & ~(((rmchar == ' ') | (rmchar == '    ')) & ((rmprevchar ~= ' ') & (rmprevchar ~= '    '))))  then
  114.             hopto prev blank
  115.         
  116.         hopselect next word
  117.         if (result == "NOT FOUND") then do
  118.             nomore = 1
  119.             gotoxy 1000 curry
  120.         end
  121.         else do
  122.             get select position
  123.             parse var result dummy' 'dummy' 'currx' 'nowony
  124.             gotoxy currx nowony
  125.         end
  126.  
  127. /*
  128.         get cursor position
  129.         parse var result cx' 'cy
  130.         if (cx ~= rightMargin) then do
  131.            hopselect next word
  132.         end
  133.         
  134.         if (dummyx
  135.         
  136.         
  137.         
  138.         
  139.         do while ((currx <= rightMargin) & (nowony == curry) & (nomore == 0))
  140.             hopselect next word
  141.             if (result == "NOT FOUND") then do
  142.                 nomore = 1
  143.                 gotoxy 1000 curry
  144.             end
  145.             else do
  146.                 get select position
  147.                 parse var result dummy' 'dummy' 'currx' 'nowony
  148.                 gotoxy currx nowony
  149.             end
  150.         end
  151. */
  152.  
  153.         inbetween = (nowony - 1) - curry
  154.  
  155. /* COMMENTED OUT delete any blank lines between
  156.         notify "deleting"
  157.         todelete = (nowony - 1) - curry
  158.         if (todelete > 0) then do
  159.            tmp = todelete
  160.            do while (tmp > 0)
  161.               selectline curry+1; del
  162.               tmp = tmp - 1
  163.            end
  164.            endy = endy - todelete
  165.            nowony = nowony - todelete
  166.            gotoxy currx nowony
  167.         end
  168. */
  169.  
  170.         if (currx > rightMargin) then do
  171.            
  172.            /* line is too long */
  173.            
  174.            hopto prev word;
  175.            get cursor position; parse var result PrevEndsAt' 'dummy
  176.            
  177.            if (PrevEndsAt > rightMargin) then
  178.                hopto prev word
  179.            
  180.            hopto next word
  181.            newline
  182.            
  183.            get cursor char
  184.            do while ((result == " ") | (result == "    ") /*TAB*/)
  185.               del
  186.               get cursor char
  187.            end
  188.            do for (leftMargin)
  189.                text '" "'
  190.            end
  191.  
  192.  
  193.            endy = endy + 1     /* cause we inserted a newline */
  194.            curry = curry + 1
  195.            nomore = 0
  196.     
  197.         end
  198.         else if (nomore == 0) then do
  199.         
  200.            /* curr line is not long enough */
  201.     
  202.            gotoxy 0 nowony
  203.            
  204.            if ((curry < endy) & (inbetween == 0)) then do
  205.                backspace; get cursor position; parse var result tempx' 'tempy
  206.                thechar = " "
  207.                do while (((thechar == " ") | (thechar == "    ") /*TAB*/ ) & (tempx ~= 0))
  208.                    do
  209.                        left 1
  210.                        tempx = tempx - (1)
  211.                        get cursor char; thechar = result
  212.                    end
  213.                end
  214.                if (tempx ~= 0) then
  215.                    right 1
  216.                get cursor char
  217.                do while  ((result == " ") | (result == "    ") /*TAB*/ )
  218.                   do
  219.                      del
  220.                      get cursor char
  221.                   end
  222.                end
  223.                if (tempx ~= 0) then do
  224.                    if (thechar == ".") then
  225.                       text '"  "'
  226.                    else
  227.                       text '" "'
  228.                end
  229.                if (tempx == 0) then
  230.                    do for (numIndent)
  231.                        text '" "'
  232.                    end               
  233.                get cursor position; parse var result bsto' 'nowony
  234.                endy = endy - 1
  235.            end
  236.            else
  237.                if (curry < endy) then do
  238.                    get cursor char
  239.                    do while ((result == " ") | (result == "    ") /*TAB*/)
  240.                       del
  241.                       get cursor char
  242.                    end
  243.                    do for (numIndent)
  244.                        text '" "'
  245.                    end
  246.                end
  247.                curry = nowony
  248.         end
  249. /*
  250.         ask "Do it again?"
  251.         if (result == "NO") then exit
  252. */
  253.     end
  254. end
  255.  
  256. /* restore autoindent */
  257. prefs autoindent autoistate
  258.