/************************************************************************/
/*                                                                      */
/*     File : FormatBlock.epxx                                          */
/*   Author : Martin Reddy                                              */
/*     Date : 22/5/94                                                   */
/*  Purpose : An ARexx script used to control the text editor EdWord    */
/* Function : This script will reformat all of the text withing the     */
/*            currently marked block so that it aligns to a specified   */
/*            right margin. If not specified, then the default margin   */
/*            is taken as the current Word Wrap margin of EdWord.       */
/*                                                                      */
/************************************************************************/

        
	HOST = ADDRESS()
	ADDRESS VALUE HOST

	OPTIONS RESULTS

	/************* Get the optional argument if present ************/

	IF ARG(1) ~= "" THEN DO
		Margin = MAX(VALUE(ARG(1)),10)
	END; ELSE DO
		GetMargin
		Margin = RESULT
	END

	/********** Make sure there is a block to format first *********/

	IsSelected
	IF ( RESULT == 0 ) THEN DO
		Inform "No Block Has Been Currently Defined|Please Drag Select Region First."
		EXIT
	END

	/***** Display a little message first of all ****/
		
	Message "Formatting Block ..."
	SetView OFF
	ProgressIndicator
		
	/******* Now The Actual Loop To Format The Block *******/

	JumpToBlock
	Finished = 1
	DO UNTIL ( Finished == 0 )

		GetLineLen
		IF ( RESULT < Margin ) THEN DO

			/* Line is shorter than the specified margin length */

				MoveNextBOL

				IsEOL
				IF ( RESULT ~= 0 ) THEN DO

					MoveNextBlockLine   /* Leave blank lines intact */
					Finished = RESULT

				END; ELSE DO

					IsInBlockLine
					IF ( RESULT == 0 ) THEN DO
						Finished = 0
					END; ELSE DO
						BackSpaceChar
						InsertASCII 32
					END

				END;

		END; ELSE DO

			/* Line is longer than the specified margin length */

			SetColumn Margin-3

			GetColumn
			DO WHILE ( RESULT < Margin )
				MoveNextWord
				GetColumn
			END

			MovePrevWord
			LineFeed

		END

	END

	/*** Return Things to normal (or as normal as they were!) ***/

	Message ""
	ProgressIndicator
	SetView ON

