
(*********************************************************************

        Adapted from
        Roman E. Maeder: Programming in Mathematica,
        Second Edition, Addison-Wesley, 1991.

 *********************************************************************)


Transcript::usage = "Transcript[filename] opens filename for a transcript
	of the current session. It clobbers $Post"
EndTranscript::usage = "EndTranscript[] closes the transcript opened with Transcript[]."

Begin["`Private`"]

`transcript=""

Transcript[filename_] := (
	transcript = OpenWrite[filename];
	If[ transcript === $Failed, Return[transcript] ];
	$Post := ($Post := MakeTranscript; Identity);
	transcript
	)

EndTranscript[] := (
	MakeTranscript;
	$Post =. ;
	Close[transcript]
	)

GetInputForm[lhs_ :> rhs_] :=
	Module[{input},
		input = HoldForm[rhs];
		input = Characters[ToString[InputForm[input]]];
		input = Drop[input, {1, 9}]; (* HoldForm[ *)
		input = Drop[input, -1];     (* ]         *)
		StringJoin @@ input
	]
	
MakeTranscript :=
	Module[{input, output},
		input  = GetInputForm[ DownValues[In][[-2]] ];
		output = GetInputForm[ DownValues[Out][[-1]] ];
		WriteString[transcript, "(* In[", $Line-1, "]=\n", input, "\n*)\n"];
		WriteString[transcript, "\n", output, "\n\n"];
		Identity
	]

End[]
Null
