(* This file contains Mathematica definitions for Converting Expressions into FrameMaker's
     Ascii form of equations.    Equations are placed in a FrameMaker MIF document, and 
     the document is given to Frame to open, using the open command  *)
     
(*  Syntax is FrameForm[expr]   *) 

(* utility routines used in FForm *)
Fnum[x_] := num[x,StringJoin["\"",ToString[x],"\""]]
Fconvertinteger[e_] := If[Head[e] === Integer,Fnum[e],e];
Fconvertsymbol[e_] := If[Head[e] === Symbol,char[e],e];
Fconvertreal[e_] := If[Head[e] === Real,Fnum[e],e];

(* This procedure converts an expression from Mathematic form to Frame form *)
FForm[start_] := 
	Block[{FFv,new}, FFv = start;
	 
		
	(* Calculus *)
	FFv = FFv //. Integrate[g_,x_] -> int[g,diff[x]];
	FFv = FFv //. Integrate[g_,{x_,min_,max_}] ->
						int[g,diff[x],min,max];
	FFv = FFv //. Derivative[1,x_][g_] -> function[optotal[x],g];
	FFv = FFv //. Derivative[n_,x_][g_] -> function[optotal[x,n],g];
	FFv = FFv //. D[g_,x_] -> oppartial[x,g];
	FFv = FFv //. D[g_,{x_,p_}] -> oppartial[x,p,g];
		
	 (* Do Rational before MapAlls. This gets treated as a single object on MapAlls,
	 	so internal numbers are concealed if this step follows the MapAlls *)
		FFv = FFv //. Rational[x_,y_] -> over[x,y];	
		
	(* We use MapAll to convert Symbol,Integer, & Real, because I
	   could not get mapping rules to work *)
	   
		FFv = MapAll[Fconvertsymbol, FFv];
		FFv = MapAll[Fconvertinteger, FFv];
		FFv = MapAll[Fconvertreal, FFv];
		
		(* Some Trig functions *)
		FFv = FFv //. Cos[x_] -> cos[x];
		FFv = FFv //. Sin[x_] -> sin[x];
		FFv = FFv //. Tan[x_] -> tan[x];	
		
		(* Simple Operators *)
		FFv = FFv //. Plus[x_,y_] -> plus[x,y];	
		FFv = FFv //. Times[x_,y_] -> times[x,y];	
		FFv = FFv //. Power[x_,y_] -> power[x,y];	
		FFv = FFv //. Divide[x_,y_] -> over[x,y];	
		FFv = FFv //. Subtract[x_,y_] -> subtract[x,y];	
		FFv = FFv //. Abs[x_] -> abs[x];	
		FFv = FFv //. Minus[x_] -> minus[x];
		FFv = FFv //. Log[x_] -> log[x];
		
		
		FFv
		]

(* We do a bunch of file io and create a tmp file to be opened in FrameMaker *)
FrameForm[x_] := 
	Block[{FFu,f,s}, FFu = x; 
		f = "/tmp/math.framemif";
		s = "/tmp/math.scratch";
		OpenWrite[f]
		WriteString[f,"<MIFFile 2.00>   # Generated From Within Mathematica\n"];
		WriteString[f,"<Document\n"];
		WriteString[f,"<DWindowRect 132 178 475 333 >\n"];   (* window position *)
		WriteString[f,"<DPageSize 6.0 4.0 >\n"];             (* size of document *)
		WriteString[f,">\n"];            
		WriteString [f, "<Math \n"];
		Close[f];
		OpenWrite[s];
		WriteString [s," <MathFullForm `"];
		WriteString[s, FForm[FFu]];
		WriteString [s,"'"];
		Close[s];
	(* We need a special step to waste blanks, because the Frame input
	 cannot handle blanks.  Must have num[3,"3"] not num[3, "3"]
	 This needs to be fixed by Frame *)
		Run["tr -d '\040' </tmp/math.scratch >>/tmp/math.framemif"];
		OpenAppend[f];
		WriteString [f," \n>\n<MathOrigin  3 2 >\n"];   (* center on page *)
		WriteString [f," <MathAlignment Center >\n"];
		WriteString [f," <MathSize MathLarge >\n"];
		WriteString [f," <Angle 0>>\n"];
		Close[f];
		Run["open /tmp/math.framemif"]
		]
"Done Loading FrameMaker Output Functions"

