$Macro_File WordStar;
{******************************************************************************}
{                                MULTI-EDIT MACRO                              }
{                                                                              }
{ Name: WordStar                                                               }
{                                                                              }
{                                                                              }
{******************************************************************************}

$Macro WordStar;

	Right_Margin := 80;
	Set_Global_Int('Margin_Released',FALSE);
	Set_Global_Int('Left_Margin',1);
	Set_Global_Int('Right_Margin',Right_Margin);
	Set_Global_Int('Name_La',0);
	Set_Global_Int('Stat_La',0);
	Word_Delimits := ' .,:;!?';
	Indent_Style := 0;
	Goto_Col(1);
	Set_Indent_Level;

End_Macro;

$Macro CtrlQ;
	Def_Char(Temp_Char);
	Def_Str(Temp_String);
	Make_Message('<CtrlQ>');
	Read_Key;
	Make_Message('');
	If (Key1 <> 0) Then
		Temp_Char := Caps(Char(Key1));
		If ((Temp_Char = 'S') OR (Temp_Char = Char(19))) Then
			Goto_Col(1);
		End;
		If ((Temp_Char = 'Y') OR (Temp_Char = Char(25))) Then
			Push_Undo;
			While NOT(At_Eol) Do
				Del_Char;
			End;
			Pop_Undo;
		End;
		If ((Temp_Char = 'D') OR (Temp_Char = Char(04))) Then
			Refresh := FALSE;
			Push_Undo;
			Eol;
			If C_Col > 80 Then
				Goto_Col(1);
				Goto_Col(80);
			End;
			Pop_Undo;
			Refresh := TRUE;
			Redraw;
		End;
		If ((Temp_Char = 'E') OR (Temp_Char = Char(05))) Then
			Run_Macro('TopWin');
			If At_Eol Then
				Eol;
			End;
		End;
		If ((Temp_Char = 'X') OR (Temp_Char = Char(24))) Then
			Run_Macro('BotWin');
			If At_Eol Then
				Eol;
			End;
		End;
		If ((Temp_Char = 'R') OR (Temp_Char = Char(18))) Then
			Tof;
		End;
		If ((Temp_Char = 'C') OR (Temp_Char = Char(03))) Then
			Eof;
		End;
		If (Block_Stat) Then
			If ((Temp_Char = 'B') OR (Temp_Char = Char(02))) Then
				run_macro('TOPBLOCK');
				Redraw;
			End;
			If ((Temp_Char = 'K') OR (Temp_Char = Char(11))) Then
				run_macro('ENDBLOCK');
			End;
		End;
		If ((Temp_Char = 'F') OR (Temp_Char = Char(06))) Then
			Run_Macro('Meutil2^Search');
		End;
		If ((Temp_Char = 'A') OR (Temp_Char = Char(01))) Then
			Run_Macro('Meutil2^S_Repl');
		End;
		If ((Temp_Char = 'P') OR (Temp_Char = Char(16))) Then
			Run_Macro('WS_Undo');
		End;
	Else
		Run_Macro('Keycode^Makekey /K1='+Str(Key1)+' /K2='+Str(Key2));
		Temp_String := Caps(Return_Str);
		If (Temp_String = '<DEL>') Then
			Push_Undo;
			Refresh := FALSE;
			While (C_Col > 1) Do
				Back_Space;
			End;
			Refresh := TRUE;
			Redraw;
			Pop_Undo;
		End;
	End;
End_Macro;

$Macro CtrlK;
	Def_Char(Temp_Char);
	Def_Str(Temp_String, FName);
	Def_Int(Temp_Integer, Status);
	Make_Message('<CtrlK>');
	Read_Key;
	Make_Message('');
	If (Key1 <> 0) Then
		Temp_Char := Caps(Char(Key1));
		If ((Temp_Char = 'Y') OR (Temp_Char = Char(25))) Then
			Run_Macro('DelBlock');
		End;
		If ((Temp_Char = 'J') OR (Temp_Char = Char(10))) Then
			FName := '';
			Status := String_In(FName,'NAME OF FILE TO DELETE? ',55,1,1,'');
			If Status Then
				If File_Exists(FName) Then
					Del_File(FName);
				Else
					Beep;
					Make_Message('FILE '+Caps(FName)+' NOT FOUND - Press <Esc>');
					Run_Macro('MsgEsc');
				End;
			End;
		End;
		If ((Temp_Char = 'D') OR (Temp_Char = Char(04))) Then
			Run_Macro('AutoSave');
			Run_Macro('MEUTIL1^LOADFILE');
		End;
		If ((Temp_Char = 'Q') OR (Temp_Char = Char(17))) Then
			Run_Macro('EXIT');
		 {Rest_Dos_Screen;
			Quit(0);}
		End;
		If ((Temp_Char = 'S') OR (Temp_Char = Char(19))) Then
			Run_Macro('AutoSave');
		End;
		If ((Temp_Char = 'X') OR (Temp_Char = Char(24))) Then
			Run_Macro('AutoSave');
			Run_Macro('EXIT');
		End;
		If ((Temp_Char = 'P') OR (Temp_Char = Char(16))) Then
			Run_Macro('MEUTIL3^Print');
		End;
		If ((Temp_Char = 'N') OR (Temp_Char = Char(14))) Then
			{????????????????????????????}
		End;
		If ((Temp_Char = 'B') OR (Temp_Char = Char(02))) Then
			Run_Macro('WSBlckKB');
		End;
		If ((Temp_Char = 'K') OR (Temp_Char = Char(11))) Then
			Run_Macro('WSBlckKK');
		End;
		If ((Temp_Char = 'H') OR (Temp_Char = Char(08))) Then
			Run_Macro('BlckHide');
		End;
		If ((Temp_Char = 'C') OR (Temp_Char = Char(03))) Then
			Run_Macro('MoveCopy /MC=COPY');
		End;
		If ((Temp_Char = 'V') OR (Temp_Char = Char(22))) Then
			Run_Macro('MoveCopy /MC=MOVE');
		End;
		If ((Temp_Char = 'W') OR (Temp_Char = Char(23))) Then
			Run_Macro('SaveBlck');
		End;
		If ((Temp_Char = 'R') OR (Temp_Char = Char(18))) Then
			Run_Macro('Splice');
		End;
	ELSE
		Run_Macro('Keycode^Makekey /K1='+Str(Key1)+' /K2='+Str(Key2));
		Temp_String := Caps(Return_Str);
		If (Temp_String = '<>') Then
		End;
	End;
End_Macro;

$Macro CtrlO;
	Def_Char(Temp_Char);
	Def_Str(Temp_String);
	Def_Int(J2,J3);
	Make_Message('<CtrlO>');
	Read_Key;
	If (Key1 <> 0) Then
		Temp_Char := Caps(Char(Key1));
		If ((Temp_Char = 'C') OR (Temp_Char = Char(03))) Then
			Run_Macro('Center');
		End;
		If ((Temp_Char = 'W') OR (Temp_Char = Char(23))) Then
			Wrap_Stat := NOT(Wrap_Stat);
		End;
		If ((Temp_Char = 'R') OR (Temp_Char = Char(18))) Then
			Run_Macro('RightMar');
		End;
		If ((Temp_Char = 'L') OR (Temp_Char = Char(12))) Then
			Run_Macro('LeftMar');
		End;
		If ((Temp_Char = 'X') OR (Temp_Char = Char(24))) Then
			Run_Macro('ResetMar');
		End;
	Else
		Run_Macro('Keycode^Makekey /K1='+Str(Key1)+' /K2='+Str(Key2));
		Temp_String := Caps(Return_Str);
		If (Temp_String = '<>') Then
		End;
	End;
	Make_Message('');
End_Macro;


$Macro Center;
{******************************************************************************}
{ Name: CENTER                                                                 }
{                                                                              }
{ Description: Centers the line of text between the current cursor position and}
{ the currently defined right margin(be sure right margin is set!).            }
{                                                                              }
{******************************************************************************}
	Def_Int(Temp_Integer,Left_Margin,Temp_Insert);
	Refresh := False;
	Left_Margin := C_Col - 1;
	Temp_Insert := Insert_Mode;
	Insert_Mode := True;
	If (Get_Line = '') Then
		Goto Macro_Exit;
	End;
	Push_Undo;
	First_Word;
	Temp_Integer := C_Col;
	Eol;
	If (C_Col - Temp_Integer > Right_Margin) Then
		Goto Macro_Exit;
	End;
	Temp_Integer := (((Right_Margin - (C_Col - Temp_Integer)) + Left_Margin) / 2);
	First_Word;
	If (C_Col < Temp_Integer) Then
		Text(Copy(' ',
		1,Temp_Integer - C_Col));
	End;
	If (C_Col > Temp_Integer) Then
		Temp_Integer := C_Col - Temp_Integer;
		Goto_Col(C_Col - Temp_Integer);
		Del_Chars(Temp_Integer);
	End;
Macro_Exit:
	Pop_Undo;
	Insert_Mode := Temp_Insert;
	Refresh := True;
	Redraw;
End_Macro;


$Macro CtrlW;
	Push_Undo;
	Run_Macro('ScrollDn');
	If WhereY < (Win_Y2 - 1) Then
		Down;
	End;
	Pop_Undo;
End_Macro;

$Macro CtrlZ;
	Push_Undo;
	Run_Macro('ScrollUp');
	If WhereY > (Win_Y1 + 1) Then
		Up;
	End;
	Pop_Undo;
End_Macro;


$Macro MsgEsc;
	While TRUE Do
		Read_Key;
		If Key1 = 27 Then
			Goto Macro_Exit;
		End;
	End;
Macro_Exit:
	Make_Message('');
End_Macro;


$Macro DelWordR;
	Def_Str(Temp_Line);
	Messages := FALSE;
	Push_Undo;
	If Cur_Char = ' ' Then
		While Cur_Char = ' ' Do
			Del_Char;
		End;
		Goto Macro_Exit;
	End;
	If XPos(Cur_Char,'.,:;!?',1) Then
		Del_Char;
		Goto Macro_Exit;
	End;
	While ((XPos(Cur_Char,' .,:;!?',1) = 0) AND (NOT(At_Eol) OR NOT(At_Eof))) Do
		If (NOT(At_Eol AND (Cur_Char = char(255)))) Then
			Del_Char;
		Else
			Down;
			Temp_Line := Get_Line;
			Del_Line;
			Up;
			Eol;
			Mark_Pos;
			Text(Temp_Line);
			Goto_Mark;
			Goto Macro_Exit;
		End;
	End;
	If XPos(Cur_Char,'.,:;!?',1) Then
		Del_Char;
	End;
Macro_Exit:
	Pop_Undo;
	Refresh := TRUE;
	Messages := TRUE;
End_Macro;

$Macro WSBlckKB;
	Str_Block_Begin;
End_Macro;


$Macro WSBlckKK;
	Block_End;
End_Macro;


$Macro No_Block;
	Block_Off;
End_Macro;


$Macro BlckHide;
End_Macro;


$Macro MoveCopy;
		If Parse_Str('/MC=',MParm_Str) = 'COPY' Then
			run_macro('MEUTIL2^BLOCKOP /BT=0');
		Else
			run_macro('MEUTIL2^BLOCKOP /BT=1');
		End;
End_Macro;


$Macro DelBlock;
	run_macro('MEUTIL2^BLOCKOP /BT=2');
End_Macro;


$Macro HomeKey;
	Push_Undo;
	If Global_Int('WS_Mode') Then
		Run_Macro('^TopWin');
		Goto_Col(1);
	Else
		If C_Col <> 1 Then
			Goto_Col(1);
		Else
			Run_Macro('^TopWin');
			Goto_Col(1);
		End;
	End;
	Pop_Undo;
End_Macro;


$Macro EndKey;
	Push_Undo;
	If Global_Int('WS_Mode') Then
		Run_Macro('^BotWin');
		If At_Eol Then
			Eol;
		End;
	Else
		If NOT(At_Eol) Then
			Eol;
		Else
			Run_Macro('^BotWin');
			If At_Eol Then
				Eol;
			End;
		End;
	End;
	Pop_Undo;
End_Macro;


$Macro RightMar;
	Def_Int(J2,J3);
	Def_Str(Temp_String);
	Put_Box(6,2,69,5,0,Stat_Color,'RIGHT MARGIN',true);
RmAgain:
	Temp_String := Str(Right_Margin);
	If String_In(Temp_String,'RIGHT MARGIN COLUMN NUMBER (Escape for cursor column)?',3,8,3,'WP') Then
		If Val(J2,Temp_String) <> 0 Then
			Beep;
			Goto RmAgain;
		End;
		If (J2 < 1) or (J2 > 254) Then
			Beep;
			Goto RmAgain;
		End;
		Kill_Box;
	Else
		Kill_Box;
		J2 := C_Col;
	End;
	Temp_String := Format_Line;
	J3 := Pos('R',Format_Line);
	If J3 = 0 Then
		J3 := Pos('r',Format_Line);
	End;
	If J3 <> 0 Then
		Format_Line := Str_Del(Format_Line,J3,1);
		Format_Line := Str_Ins(' ',Format_Line,J3);
	End;
	Format_Line := Str_Del(Format_Line,J2,1);
	Format_Line := Str_Ins('R',Format_Line,J2);
	Right_Margin := J2;
	Set_Global_Int('Right_Margin',J2);
End_Macro;


$Macro LeftMar;
	Def_Int(J2);
	Def_Str(Temp_String);
	Push_Undo;
	Put_Box(6,2,68,5,0,Stat_Color,'LEFT MARGIN',true);
LmAgain:
	Temp_String := Str(Indent_Level);
	If String_In(Temp_String,'LEFT MARGIN COLUMN NUMBER (Escape for cursor column)?',3,8,3,'WP') Then
		If Val(J2,Temp_String) <> 0 Then
			Beep;
			Goto LmAgain;
		End;
		If (J2 < 1) or (J2 > 254) Then
			Beep;
			Goto LmAgain;
		End;
		Kill_Box;
		Refresh := FALSE;
		Messages := FALSE;
		Mark_Pos;
		Goto_Col(J2);
		Set_Indent_Level;
		Goto_Mark;
		Messages := True;
		Refresh := TRUE;
	Else
		Kill_Box;
		Set_Indent_Level;
		J2 := C_Col;
	End;
	Set_Global_Int('Left_Margin',J2);
	Pop_Undo;
End_Macro;


$Macro ResetMar;
	Refresh := FALSE;
	Messages := FALSE;
	Mark_Pos;
	If NOT(Global_Int('Margin_Released')) Then
		Set_Global_Int('Margin_Released',TRUE);
		Right_Margin := 254;
		Goto_Col(1);
	Else
		Set_Global_Int('Margin_Released',FALSE);
		Goto_Col(Global_Int('Left_Margin'));
		Right_Margin := Global_Int('Right_Margin');
	End;
	Set_Indent_Level;
	Goto_Mark;
	Refresh := TRUE;
	Redraw;
	Messages := TRUE;
End_Macro;


$Macro Indent;
	Indent;
	Set_Global_Int('Left_Margin',C_Col);
End_Macro;


$Macro Undent;
	Undent;
	Set_Global_Int('Left_Margin',C_Col);
End_Macro;

$macro wshelp;
	help('WORDSTAR.HLP^*');
end_macro;

$Macro WS_Last;
End_Macro;
