{@ME.FORMAT                                                                                   }
{@ME.FORMAT                                                                                   }
$MACRO_FILE MULTI_ED;
{******************************************************************************}
{                                MULTI-EDIT MACRO                              }
{                                                                              }
{Name: MUL_IND                                                                 }
{                                                                              }
{Description:  Performs smart indent for the Multi-Edit macro language.        }
{  Is called by the macro CR.                                                  }
{                                                                              }
{               (C) Copyright 1988 by American Cybernetics, Inc.               }
{******************************************************************************}

$MACRO MUL_IND;

	DEF_STR(C_STR);
	DEF_INT(T_COL,T_COL2);

	T_COL2 := C_COL;
	FIRST_WORD;
	T_COL := C_COL;
	IF T_COL2 < T_COL THEN
		T_COL := T_COL2;
		GOTO_COL(T_COL);
	END;
	IF AT_EOL = FALSE THEN
		SET_INDENT_LEVEL;
	END;

	C_STR := ' ' + REMOVE_SPACE(CAPS( GET_WORD('; (,{') )) + ' ';
	GOTO_COL(T_COL2);
	CR;
	IF (T_COL <> T_COL2) AND (LENGTH(C_STR) <> 0) AND (POS(C_STR,
	 ' IF WHILE $MACRO '
	 ) <> 0) THEN
		INDENT;
	END;
MAC_EXIT:
END_MACRO;



{******************************************************************************}
{                                MULTI-EDIT MACRO                              }
{                                                                              }
{Name: MULTEMP                                                                 }
{                                                                              }
{Description:  Creates language constructs for the Multi-Edit macro language   }
{  based on a single character to the left of the cursor.                      }
{                                                                              }
{               (C) Copyright 1988 by American Cybernetics, Inc.               }
{******************************************************************************}
$MACRO MULTEMP;

	DEF_INT(Temp_Col,Temp_Insert,Choice, UC);
	DEF_STR(XStr);
	Temp_Insert := Insert_Mode;

	If NOT(AT_EOL) Then
		Goto END_OF_MAC;
	End;

	Insert_Mode := True;
	Temp_Col := C_COL;

	Left;

	If (C_Col > 1) Then
		Left;
		{Check for a non word character, including tabs and virtual space}
		If (Pos(Cur_Char,' ;})$|255|9') = 0) Then
			Goto_Col(Temp_Col);
			Goto END_OF_MAC;
		Else
			Right;
		End;
	End;

	UC := true;

	If (Cur_Char = 'I') Then
		Goto MAKEIF;
	End;

	If (Cur_Char = 'W') Then
		Goto MAKEWHILE;
	End;


	IF (Cur_Char = 'M') THEN
		Goto MAKEMACRO;
	END;

	UC := false;

	If (Cur_Char = 'i') Then
		Goto MAKEIF;
	End;

	If (Cur_Char = 'w') Then
		Goto MAKEWHILE;
	End;

	IF (Cur_Char = 'm') THEN
		Goto MAKEMACRO;
	END;


	Goto_Col(Temp_Col);
	Goto END_OF_MAC;
MAKEIF:
	Goto_Col(Temp_Col);
	XStr := 'f () then';
	CALL XTEXT;
	Cr;
	Cr;
	Goto_Col(Temp_Col);
	left;
	XStr := 'end;';
	Call XText;
	Up;
	Goto_Col(Temp_Col);
	Indent;
	Up;
	Goto_Col(Temp_Col + 3);
	Goto END_OF_MAC;

MAKEMACRO:
	Goto_Col(Temp_Col);
	XStr := 'acro ;';
	CALL XTEXT;
	Cr;
	Cr;
	Goto_Col(Temp_Col - 2);
	XStr := 'end_macro;';
	Call XText;
	Up;
	Goto_Col(Temp_Col);
	Left;
	Indent;
	Up;
	Goto_Col(Temp_Col + 5);
	Goto END_OF_MAC;


MAKEWHILE:
	Goto_Col(Temp_Col);
	XSTR := 'hile () Do';
	CALL XTEXT;
	Cr;
	Cr;
	Goto_Col(Temp_Col);
	left;
	XStr := 'end;';
	Call XText;
	Up;
	Goto_Col(Temp_Col);
	Indent;
	Up;
	Goto_Col(Temp_Col + 6);
	Goto END_OF_MAC;

XTEXT:
	IF UC THEN
		XSTR := CAPS(XSTR);
	END;
	TEXT( XSTR );
	RET;

END_OF_MAC:
	Insert_Mode := Temp_Insert;

END_MACRO;

{******************************************************************************}
{                                MULTI-EDIT MACRO                              }
{                                                                              }
{Name: MULMTCH                                                                 }
{                                                                              }
{               (C) Copyright 1988 by American Cybernetics, Inc.               }
{                         Modified by Kevin Jackson                            }
{******************************************************************************}

$MACRO MULMTCH TRANS;

	DEF_STR( Str1,     {Match strings}
					 EStr1,
					 T_Str, S_Str, FStr );

	DEF_INT( Direction,   {1 = search forward, 0 = backward}
					 B_Count,     {Match count.  0 = match found}
					 S_Res,       {Search results}
					 Second_Time );


	Second_Time := False;
	Refresh := False;     {Turn screen refresh off}
	Str1 := '';
	EStr1 := '';

Find_Match_Str:

	IF (Cur_Char = '(') THEN   {Setup match for '('}
		Str1 := '(';
		EStr1 := ')';
		Direction := 1;
		S_Str := '[()'']';
		GOTO Start_Match;
	END;

	IF (Cur_Char = ')') THEN   {Setup match for ')'}
		Str1 := ')';
		EStr1 := '(';
		Direction := 0;
		S_Str := '[()'']';
		GOTO Start_Match;
	END;

	IF (Cur_Char = '{') THEN   {Setup match for '{'}
		Str1 := '{';
		EStr1 := '}';
		Direction := 1;
		S_Str := '[@{@}'']';
		GOTO Start_Match;
	END;

	IF (Cur_Char = '}') THEN   {Setup match for '}'}
		Str1 := '}';
		EStr1 := '{';
		Direction := 0;
		S_Str := '[@{@}'']';
		GOTO Start_Match;
	END;

	IF At_EOL THEN     {If we are at the end of a line the go to the first word}
		First_Word;
	END;

	IF (XPos(Cur_Char,' |9|255',1)) THEN{If we are on a blank space then find a word}
		Word_Right;
	END;

	S_Str := '['']||{%||[|9 ;)]{END}||{IF}||{WHILE}$||[|9 ;.(]}||{@{}||{@}}';

	T_Str := Caps( Get_Word(';. |9|255') );  {Get the current word}

	IF (T_Str = 'IF') OR (T_Str = 'WHILE') OR (T_Str = 'ELSE') THEN
		Str1 := 'IF WHILE';
		EStr1 := 'END';
		Direction := 1;
		GOTO Start_Match;
	END;

	IF T_Str = 'END' THEN
		Str1 := 'END';
		Estr1 := 'IF WHILE';
		Direction := 0;
		Word_Left;
		Left;
		GOTO Start_Match;
	END;

	{If we didn't find a word to match the first time then try again}
	If NOT( Second_Time ) THEN
		Second_Time := True;
		First_Word;
		GOTO Find_Match_Str;
	END;

	Make_Message('NOTHING to Match');
	GOTO Macro_Exit;

Start_Match:
	Reg_Exp_Stat := True;
	Ignore_Case := True;
	B_Count := 1;
	S_Res := 1;
	Make_Message('Matching...  Hit <ESC> to Stop.');
	Working;

MATCH_LOOP:   {Main loop}
					{If the <ESC> key is pressed while matching then abort the search}
	if check_key then
		if key1 = 27 then
			Make_Message('Match Aborted.');
			goto macro_exit;
		end;
	end;

	If S_Res = 0 THEN   {If last search result was false then exit}
		GOTO Error_Exit;
	END;

	If B_Count = 0 THEN {If match count is 0 then success}
		GOTO Found_Exit;
	END;

	If Direction = 1 THEN {Perform search based on direction}
		Right;
		While NOT (At_EOL) and ((Cur_CHar = '|255') or (Cur_Char = '|9')) DO
			Right;
		END;
		S_Res := Search_Fwd(S_Str,0);
	ELSE
		Left;
		While (Cur_Char = '|255') or
					(Cur_Char = '|9') DO
			Left;
		END;
		S_Res := Search_Bwd(S_Str,0);
	END;

	If S_Res = 0 THEN   {If search failed then exit}
		GOTO Macro_Exit;
	END;

	FStr := Caps(Found_Str);

	IF Length(FStr) > 2 THEN
		IF XPOS(Copy(FStr,1,1),'|9 ;()',1)  THEN  {If the first char is a space or a ;}
			FStr := Copy(FStr,2,20);         {  then eliminate it}
		END;
																			{If it ended in a space, ; or . then}
		IF XPOS(Copy(FStr,Length(FStr),1),'|9 ;.()',1) THEN
			FStr := Copy(FStr,1,Length(FStr) - 1);  {eliminate that char}
		END;
	END;

												      {If we found the first match string then}
	IF XPOS(FStr,STR1,1) THEN
		++B_Count;   {Inc the match count}
		GOTO Match_Loop;
	END;

	IF XPOS(FStr,ESTR1,1) THEN          {If we found the second match string then}
		--B_Count;    {  decrement the match count}
		GOTO Match_Loop;
	END;

	{If we found a comment then match it}
	IF (Direction = 1) AND (FStr = '{') THEN
			S_Res := Search_Fwd('@}',0);
			GOTO Match_Loop;
	END;

	{If we found a comment then match it}
	IF (Direction = 0) AND (FStr = '}') THEN
			S_Res := Search_Bwd('@{',0);
			GOTO Match_Loop;
	END;

	If FStr = '''''' THEN        {If we found two single quotes the skip it}
		If Direction = 1 THEN
			RIGHT;
		ELSE
			LEFT;
		END;
		GOTO Match_Loop;
	END;
															{If we found a single quote then match it}
	IF FStr = '''' THEN

		Quote_Loop:

			If Direction = 1 THEN
				RIGHT;
			ELSE
				LEFT;
			END;
			IF Direction = 1 THEN
				S_Res := Search_Fwd('''',0);
			ELSE
				S_Res := Search_Bwd('''',0);
			END;
			If S_Res = 0 THEN
				GOTO Macro_Exit;
			END;
			FStr := Found_Str;
			If FStr = '''''' THEN
				GOTO Quote_Loop;
			END;
			GOTO Match_Loop;

	END;

Error_Exit:     {Go here for unsucessfull match}
	Make_Message('Match NOT Found');
	GOTO Macro_Exit;

Found_Exit:     {Go here for successfull match}
	Make_Message('Match Found');
Macro_Exit:
	Refresh := True;
	Redraw;
END_MACRO;
